// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var empty = new Image(); empty.src = "images/misc/fieldError.gif";
var haveerrors = 0;

function showImage(imagename, imageurl, errors) {
document[imagename].src = imageurl;
if (!haveerrors && errors) haveerrors = errors;
}

function countLeasingSum(imagename, imageurl, errors) {
document[imagename].src = imageurl;

if (!haveerrors && errors) haveerrors = errors;
}

function validateNewsletterRequest() {
	var oFrm = document.oForm;

haveerrors = 0;
(oFrm.oCompany.value.length < 1)
? showImage("oCompanyError", "images/misc/fieldError.gif", true)
: showImage("oCompanyError", "images/misc/fieldBlank.gif", false);

(oFrm.oFirstname.value.length < 1)
? showImage("oFirstnameError", "images/misc/fieldError.gif", true)
: showImage("oFirstnameError", "images/misc/fieldBlank.gif", false);

(oFrm.oLastname.value.length < 1)
? showImage("oLastnameError", "images/misc/fieldError.gif", true)
: showImage("oLastnameError", "images/misc/fieldBlank.gif", false);

(oForm.oEmail.value.search("@") == -1 || oForm.oEmail.value.search(".") == -1)
? showImage("oEmailError", "images/misc/fieldError.gif", true)
: showImage("oEmailError", "images/misc/fieldBlank.gif", false);

return (!haveerrors);
}

function validateCatalogueRequest() {
	var oFrm = document.oForm;

haveerrors = 0;
(oFrm.oCompany.value.length < 1)
? showImage("oCompanyError", "images/misc/fieldError.gif", true)
: showImage("oCompanyError", "images/misc/fieldBlank.gif", false);

(oFrm.oAddress.value.length < 1)
? showImage("oAdressError", "images/misc/fieldError.gif", true)
: showImage("oAdressError", "images/misc/fieldBlank.gif", false);

(oFrm.oPostal.value.length < 1)
? showImage("oPostalError", "images/misc/fieldError.gif", true)
: showImage("oPostalError", "images/misc/fieldBlank.gif", false);

(oFrm.oCity.value.length < 1)
? showImage("oCityError", "images/misc/fieldError.gif", true)
: showImage("oCityError", "images/misc/fieldBlank.gif", false);
(oFrm.oPhone.value.length < 1)
? showImage("oPhoneError", "images/misc/fieldError.gif", true)
: showImage("oPhoneError", "images/misc/fieldBlank.gif", false);

return (!haveerrors);
}

function validateLeasing() {
var oFrm = document.oForm;

haveerrors = 0;
(oFrm.oSum.value.length < 1)
? countLeasingSum("oSumError", "images/misc/fieldError.gif", true)
: countLeasingSum("oSumError", "images/misc/fieldBlank.gif", false);

(oFrm.oMonths.value.length < 1)
? countLeasingSum("oMonthsError", "images/misc/fieldError.gif", true)
: countLeasingSum("oMonthsError", "images/misc/fieldBlank.gif", false);

return (!haveerrors);

}

function validateLeasingRequest() {
	var oFrm = document.oForm;

haveerrors = 0;
(oFrm.oCompany.value.length < 1)
? showImage("oCompanyError", "images/misc/fieldError.gif", true)
: showImage("oCompanyError", "images/misc/fieldBlank.gif", false);

(oFrm.oFirstname.value.length < 1)
? showImage("oFirstnameError", "images/misc/fieldError.gif", true)
: showImage("oFirstnameError", "images/misc/fieldBlank.gif", false);

(oFrm.oLastname.value.length < 1)
? showImage("oLastnameError", "images/misc/fieldError.gif", true)
: showImage("oLastnameError", "images/misc/fieldBlank.gif", false);

(oForm.oEmail.value.search("@") == -1 || oForm.oEmail.value.search(".") == -1)
? showImage("oEmailError", "images/misc/fieldError.gif", true)
: showImage("oEmailError", "images/misc/fieldBlank.gif", false);

(oFrm.oPhone.value.length < 1)
? showImage("oPhoneError", "images/misc/fieldError.gif", true)
: showImage("oPhoneError", "images/misc/fieldBlank.gif", false);

return (!haveerrors);
}

function justNumbers() {
	var key = event.keyCode;

	if( key == 45 || key == 48 || key == 49 || key == 50 || key == 51 || key == 52 || key == 53 || key == 54 || key == 55 || key == 56 || key == 57){
	}
	else{
	return false;
	}
}

function addToBasket(itemForm) {
var itemForm;
var oForm = document.forms[itemForm];
var ammount = oForm.ammount.value;

	if(ammount.length < 1){
	ammount = 0;
	}

	if(parseFloat(ammount) < parseFloat(oForm.lowestAmmount.value)){
	alert("Var god fyll i fältet Antal för den produkt du vill lägga i varukorgen.\nLägsta antal: " + oForm.lowestAmmount.value + " st.")
	return false;
	}
	else{
	return true;
	}
}

function updateToBasket(itemForm) {
var itemForm;
var oForm = document.forms[itemForm];
var ammount = oForm.ammount.value;

	if(ammount.length < 1){
	ammount = 0;
	}

	if(oForm.btnEvent.value != "erase"){
		if(parseFloat(ammount) < parseFloat(oForm.lowestAmmount.value)){
		alert("Var god fyll i fältet Antal för den produkt du vill lägga i varukorgen.\nLägsta antal: " + oForm.lowestAmmount.value + " st.")
		return false;
		}
		else{
		return true;
		}
	}
	else{
	return true;
	}
}

function useSameImage() {
var el = document.forms["oForm"].elements;
	for(var i = 0 ; i < el.length ; ++i){
		if(el[i].type == "file"){
		var filegroup = el[el[i+1].name]; // get the whole set of radio buttons.
		var itemchecked = false;
			for(var j = 0 ; j < filegroup.length ; ++j){
				if(document.oForm.sameImage.checked) {
				el[i].disabled = true;
				}
				else{
				el[i].disabled = false;
				}
			}
		}
	}
}

function condPurchase() {
smallWin = window.open('conditionsPurchase.asp','conditionsPurchase','_blank');
}

function validatePaymentType4() {
var oForm = document.oForm;
var strStatement;
var strFields;
var requiredFields;

requiredFields = 0;
strStatement = "Du har inte fyllt i alternativ i följande fält:\n\n";
strFields = "";

	if(oForm.oCompany.value.length < 1){
	strFields = strFields + " - Företag\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.registrationNumber.value.length < 10){
	strFields = strFields + " - Organisationsnummer\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.oFirstname.value.length < 1){
	strFields = strFields + " - Förnamn\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.oLastname.value.length < 1){
	strFields = strFields + " - Efternamn\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.oAddress.value.length < 1){
	strFields = strFields + "Adress\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.oPostal.value.length < 4){
	strFields = strFields + "Postnr\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.oCity.value.length < 1){
	strFields = strFields + "Ort\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.oPhone.value.length < 1){
	strFields = strFields + "Telefon\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.oEmail.value.search("@") == -1 || oForm.oEmail.value.search(".") == -1){
	strFields = strFields + "E-post\n";
	requiredFields = requiredFields + 1;
	}

if(oForm.conditionsPurchase.checked != true){
	strFields = strFields + "Godkännande av köpvillkor\n";
	requiredFields = requiredFields + 1;
	}

	if(requiredFields == 0){
	return true;
	}
	else{
	alert(strStatement + strFields);
	return false;
	}
}

function getImage(oImage) {
var oImage;
document.images["productImageLarge"].src = oImage;
}

function validateForm2() {
var oForm = document.oForm;
var strStatement;
var strFields;
var requiredFields;

requiredFields = 0;
strStatement = "Du har inte fyllt i alternativ i följande fält:\n\n";
strFields = "\n";

	if(oForm.oName.value.length < 1){
	strFields = strFields + "Namn\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.oEmail.value.length < 1){
	strFields = strFields + "E-post\n";
	requiredFields = requiredFields + 1;
	}
	if(oForm.memoText.innerText.length < 1){
	strFields = strFields + "Meddelande\n";
	requiredFields = requiredFields + 1;
	}

strFields = strFields + "\n";

	if(requiredFields == 0){
	return true;
	}
	else{
	alert(strStatement + strFields);
	return false;
	}
}
