

function validateForm() {
	document.getElementById("submit2").disabled = true;
	var objForm = document.getElementById("bestilling");
	var objFirma = document.getElementById("Firma");
	var objVat = document.getElementById("vat");
	var objVatcountrycode = document.getElementById("vatcountrycode");
	var objFornavn = document.getElementById("Fornavn");
	var objEfternavn = document.getElementById("Efternavn");
	var objAdresse = document.getElementById("Adresse");
	var objPostnr = document.getElementById("Postnr");
	var objBy = document.getElementById("By");
	var objTelefon = document.getElementById("Telefon");
	var objEmail = document.getElementById("email");
	var objCountry = document.getElementById("country");
	var objCountriesEU = document.getElementById("countriesEU");
	var objMethodofpayment = document.getElementById("methodofpayment");
	var isEUold = (("\n"+objCountriesEU.value+"\n").indexOf("\n" + objCountry.options[objCountry.selectedIndex].value + "\n") > -1);
	var isEU = (objCountry.options[objCountry.selectedIndex].value in arrEUKeys);
	if (arrEUusrid == "23898") {
		alert(objCountriesEU.value);
		alert(objCountry.options[objCountry.selectedIndex].value);
		alert(isEU);
	}
	var formErrorsObject = {};
	var formErrors = null;
	var i, obj;
	//alert('test');
	//return false;
	
	if (objCountry.options[objCountry.selectedIndex].value == "") {
		formErrors = true;
		objCountry.style.backgroundColor = "#FFFFA0";
	}
	else
		objCountry.style.backgroundColor = "";
	
	if (document.getElementById("methodofpaymentccdibs") && document.getElementById("methodofpaymentpaypal") && document.getElementById("methodofpaymentbank")) {
		if (!document.getElementById("methodofpaymentccdibs").checked && !document.getElementById("methodofpaymentpaypal").checked && !document.getElementById("methodofpaymentbank").checked) {
			formErrors = true;
			document.getElementById("methodofpaymentccdibs").style.backgroundColor = "#FFFFA0";
			document.getElementById("methodofpaymentpaypal").style.backgroundColor = "#FFFFA0";
			document.getElementById("methodofpaymentbank").style.backgroundColor = "#FFFFA0";
		}
		else {
			document.getElementById("methodofpaymentccdibs").style.backgroundColor = "";
			document.getElementById("methodofpaymentpaypal").style.backgroundColor = "";
			document.getElementById("methodofpaymentbank").style.backgroundColor = "";
		}
	}
	else {
		if (objMethodofpayment.options[objMethodofpayment.selectedIndex].value == "") {
			formErrors = true;
			objMethodofpayment.style.backgroundColor = "#FFFFA0";
		}
		else
			objMethodofpayment.style.backgroundColor = "";
	}
	
	if (!validateStringNoNumbers(objFornavn.value, 2, 50)) {
		formErrors = true;
		objFornavn.style.backgroundColor = "#FFFFA0";
	}
	else
		objFornavn.style.backgroundColor = "";
	
	if (!validateStringNoNumbers(objEfternavn.value, 2, 50)) {
		formErrors = true;
		objEfternavn.style.backgroundColor = "#FFFFA0";
	}
	else
		objEfternavn.style.backgroundColor = "";
	
	if (!validateString(objAdresse.value, 4, 75)) {
		formErrors = true;
		objAdresse.style.backgroundColor = "#FFFFA0";
	}
	else
		objAdresse.style.backgroundColor = "";
	
	if (!validateString(objPostnr.value, 2, 15)) {
		formErrors = true;
		objPostnr.style.backgroundColor = "#FFFFA0";
	}
	else
		objPostnr.style.backgroundColor = "";
	
	if (!validateString(objBy.value, 2, 75)) {
		formErrors = true;
		objBy.style.backgroundColor = "#FFFFA0";
	}
	else
		objBy.style.backgroundColor = "";
	
	if (!validateTelephone(objTelefon.value)) {
		formErrors = true;
		objTelefon.style.backgroundColor = "#FFFFA0";
	}
	else
		objTelefon.style.backgroundColor = "";
	
	if (!validateEmail(objEmail.value)) {
		formErrors = true;
		objEmail.style.backgroundColor = "#FFFFA0";
	}
	else
		objEmail.style.backgroundColor = "";
	
	//alert(objCountry.value);
	//alert(arrEUusrid);
	var companyVatVali1 = (isEU);
	var companyVatVali2 = ((objVat.value != "" && objFirma.value == "") || (objFirma.value != "" && objVat.value == ""));
	var companyVatVali3 = (objCountry.value != "57");
	if (arrEUusrid == "1") alert(1);
	//alert("isEI = " + isEU);
	if (companyVatVali1 && companyVatVali2) {
		//alert(1);
		formErrors = true;
		objFirma.style.backgroundColor = "#FFFFA0";
		objVat.style.backgroundColor = "#FFFFA0";
		if (arrEUusrid == "1") alert(2);
	}
	else {
		//alert(1);
		objFirma.style.backgroundColor = "";
		objVat.style.backgroundColor = "";
		if (arrEUusrid == "1") alert(3);
		if (isEU) {
			if (arrEUusrid == "1") alert(4);
			//alert(companyVatVali3);
			//return false;
			//alert(isEU);
			//alert(objVat.value);
			//alert(checkVATNumber(objVat.value));
			var tmp = objVat.value.substr(0, 2);
			tmp = tmp.replace(/[^\d]*/gim, "");
			if (arrEUusrid == "1") alert(tmp);
			if (arrEUusrid == "1") alert(objVat.value);
			if (arrEUusrid == "1") alert(objVat.value.substr(2));
			if (arrEUusrid == "1") alert(tmp + objVat.value.substr(2));
			//objVat.value = tmp + objVat.value.substr(2);
			if (arrEUusrid == "1") {
				alert(objVat.value);
				alert("isEU = " + isEU);
				alert(objCountry.options[objCountry.selectedIndex].value);
				alert(objCountry.selectedIndex);
				alert(objCountriesEU.value);
				alert(objCountriesEU.value.indexOf("\n"+objCountry.options[objCountry.selectedIndex].value));
				alert("checkVATNumber = " + checkVATNumber(objVatcountrycode.value + objVat.value));
			}
			if (isEU && objVat.value != "" && !checkVATNumber(objVatcountrycode.value + objVat.value)) {
				if (arrEUusrid == "1")
					alert("final warning 1");
				formErrors = true;
				objVat.style.backgroundColor = "#FFFFA0";
			}
			else if (isEU && objVat.value != "" && !formErrors) {
				if (arrEUusrid == "1")
					alert("final warning 2");
				validateEUVATForm();
				if (arrEUusrid == "1")
					alert("final warning 3");
				return false;
				//formErrors = true;
				//objVat.style.backgroundColor = "#FFFFA0";
			}
			else
				objVat.style.backgroundColor = "";
		}
		else {
		
		}
	}
	
	if (formErrors == true){
		document.getElementById("submit2").disabled = false;
		alert(checkoutErrorMessage);
	}
	else
		setOrderType();
	return (!formErrors);
}


var validateEUVATInet = null;

function vatcountriesOnChange() {
	var oVatcountries = document.getElementById("country");
	var oVatcountrycode = document.getElementById("vatcountrycode");
	var vatcountriesValue = oVatcountries.options[oVatcountries.options.selectedIndex].value;
	var varcountrycodeIsEU = (vatcountriesValue in arrEUKeys);
	var vardisplayEUVatNo = varcountrycodeIsEU ? "" : "none";
	//alert(vatcountriesValue);
	//alert(arrEUKeys);
	//alert(vatcountriesValue in arrEUKeys);
	//alert(arrEUKeys[vatcountriesValue]);
	//alert(oVatcountrycode.value);
	oVatcountrycode.value = (varcountrycodeIsEU ? arrEUKeys[vatcountriesValue] : "");
	document.getElementById("vatcountrycodeTR1").style.display = vardisplayEUVatNo;
	document.getElementById("vatcountrycodeTR2").style.display = vardisplayEUVatNo;
}
function validateEUVATForm() {
	if (arrEUusrid == "1") alert(5);
	vatcountriesOnChange();
	var langcode = document.getElementById("vatcountrycode").value;
	var vatno = document.getElementById("vat").value;
	validateEUVAT(langcode, vatno);
}
function validateEUVAT(langcode, vatno) {
	if (arrEUusrid == "1") alert(6);
	var returnValue = false;
	//alert("validateEUVAT()");
	validateEUVATInet = new InetRequest();
	validateEUVATInet.xmlSrc = "/include/ajax/validateeuvat.php";
	validateEUVATInet.httpNative.onreadystatechange = validateEUVATOnData;
	validateEUVATInet.setRequestMethod("GET");
	validateEUVATInet.addRequestParameter("langcode", langcode);
	validateEUVATInet.addRequestParameter("vatno", vatno);
	validateEUVATInet.sendRequest();
	return returnValue;
}
function validateEUVATOnData() {
	try {
		if (validateEUVATInet.httpNative.readyState == 4) {
			if (arrEUusrid == "1") alert(7 + " " + validateEUVATInet.httpNative.responseText);
			if (typeof(validateEUVATInet.httpNative.responseText) == "unknown") {
				document.getElementById("vat").style.backgroundColor = "";
				setOrderType();
				objForm.onSubmit = "return true;";
				objForm.submit();
			}
			else {
				var output = validateEUVATInet.httpNative.responseText.toString();
				//alert("validateEUVATOnData OUTPUT\n\n" + output);
				/////    No error from EU vat validator when service unavaible!!!!
				//if (output == "0") {
				//	document.getElementById("vat").style.backgroundColor = "#FFFFA0";
				//	alert(checkoutErrorMessage);
				//}
				//else {
					var objForm = document.getElementById("bestilling");
					document.getElementById("vat").style.backgroundColor = "";
					setOrderType();
					//alert(objForm.onSubmit);
					objForm.onSubmit = "return true;";
					objForm.submit();
				//}
			}
		}
	}
	catch(e) {
		validateEUVATInet = null;
		document.getElementById("vat").style.backgroundColor = "";
		setOrderType();
		objForm.onSubmit = "return true;";
		objForm.submit();
	}
}

