Array.prototype.contains = function (element) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == element) {
			return true;
		}
	}
	return false;
};




function setOtherFunction() {
	// afh. van keuze 'otherfunction' legen
	objSelect = document.getElementById('functie');
	var objOpt = objSelect.options[objSelect.selectedIndex];
	if (objOpt.value != 0) {
		document.frmContact1.otherfunction.value = "";
	}
}
			
			
			
			
function Validate_String(string, return_invalid_chars) {
	valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	invalid_chars = '';
	
	if(string == null || string == '')
		return(true);
	
	// voor elk karakter van de string
	for (index = 0; index < string.length; index++) {
		char = string.substr(index, 1);
		
		// is het een valid karakter?
		if(valid_chars.indexOf(char) == -1) {
			// if not, is it already on the list of invalid characters?
			if(invalid_chars.indexOf(char) == -1) {
				// if it's not, add it
				if(invalid_chars == '')
					invalid_chars += char;
				else
					invalid_chars += ', ' + char;
			}
		}
	}
	
	// if the string does not contain invalid characters, the function will return true.
	// if it does, it will either return false or a list of the invalid characters used
	// in the string, depending on the value of the second parameter.
	if(return_invalid_chars == true && invalid_chars != '') {
		last_comma = invalid_chars.lastIndexOf(',');
		
		if(last_comma != -1)
			invalid_chars = invalid_chars.substr(0, $last_comma) + 
			' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
		
		return(invalid_chars);
	}
	else
		return(invalid_chars == '');
}




function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	
	return true;
}


submitOK = true;



function doAjaxSubmit(){
	validateContact();
	if (submitOK === true){
		doCartStep('info', 'make');
		return true;
	}
	else{
		return false;
	}
}
function doLinkCheck(nextlink){
	validateContact();
	if (submitOK === true){
		doLink(nextlink);
	}
	else{
		doLink('info');
	}
}

function disableDelAddress(check){
	x = document.infobasketform;
	if(check.checked){
		x.address2.disabled = true;
		x.post2.disabled = true;
		x.city2.disabled = true;
		x.country2.disabled = true;
	}
	else{
		x.address2.disabled = false;
		x.post2.disabled = false;
		x.city2.disabled = false;
		x.country2.disabled = false;
	}
}

function seperator(errormessage){
	if (errormessage != "Complete this form with your "){
		return ", ";
	}
	else{
		return "";
	}
}

function validateContact() {
	// 1. settings
	
	x = document.infobasketform;
	errormessage = "Complete this form with your ";
	emailmessage = "";
	submitOK = true;
	
	// 2. values
	address = x.address.value;
	address = address.replace(/\s+$|^\s*/gi, "");
	address2 = x.address2.value;
	address2 = address2.replace(/\s+$|^\s*/gi, "");
	post = x.post.value;
	post = post.replace(/\s+$|^\s*/gi, "");
	post2 = x.post2.value;
	post2 = post2.replace(/\s+$|^\s*/gi, "");
	city = x.city.value;
	city = city.replace(/\s+$|^\s*/gi, "");
	city2 = x.city2.value;
	city2 = city2.replace(/\s+$|^\s*/gi, "");
	
	country = x.country;
	country2 = x.country2;
	country = country[country.selectedIndex].value;
	country2 = country2[country2.selectedIndex].value;
	
	orderref = x.orderref.value;
	orderref = orderref.replace(/\s+$|^\s*/gi, "");
	firstname = x.firstname.value;
	firstname = firstname.replace(/\s+$|^\s*/gi, "");
	lastname = x.lastname.value;
	lastname = lastname.replace(/\s+$|^\s*/gi, "");
	email = x.email.value;
	email = email.replace(/\s+$|^\s*/gi, "");

	company = x.company.value;
	company = company.replace(/\s+$|^\s*/gi, "");
	phone = x.phone.value;
	phone = phone.replace(/\s+$|^\s*/gi, "");
	fax = x.fax.value;
	fax = fax.replace(/\s+$|^\s*/gi, "");
	vat = x.vat.value;
	vat = vat.replace(/\s+$|^\s*/gi, "");
	
	// 3. set stripped values in textfields
	x.address.value = address;
	x.address2.value = address2;
	x.post.value = post;
	x.post2.value = post2;
	x.city.value = city;
	x.city2.value = city2;
	x.orderref.value = orderref;
	x.firstname.value = firstname;
	x.lastname.value = lastname;
	x.email.value = email;
	
	x.company.value = company;
	x.phone.value = phone;
	x.fax.value = fax;
	x.vat.value = vat;
	
	// 4. control content
		// 4.1. address
		if (address.length == 0) {
			errormessage += "billing address"
			submitOK = "False";
		}
		if (address2.length == 0 && x.delcheck.checked == false) {
			errormessage += seperator(errormessage);
			errormessage += "delivery address"
			submitOK = "False";
		}
		
		// 4.2. post
		if (post.length == 0) {
			errormessage += seperator(errormessage);
			errormessage += "billing post"
			submitOK = "False";
		}
		if (post2.length == 0 && x.delcheck.checked == false) {
			errormessage += seperator(errormessage);
			errormessage += "delivery post"
			submitOK = "False";
		}
		
		// 4.3. city
		if (city.length == 0) {
			errormessage += seperator(errormessage);
			errormessage += "billing city"
			submitOK = "False";
		}
		if (city2.length == 0 && x.delcheck.checked == false) {
			errormessage += seperator(errormessage);
			errormessage += "delivery city"
			submitOK = "False";
		}
		
		// 4.4. country
		if (country.length == 0) {
			errormessage += seperator(errormessage);
			errormessage += "billing country"
			submitOK = "False";
		}
		if (country2.length == 0 && x.delcheck.checked == false) {
			errormessage += seperator(errormessage);
			errormessage += "delivery country"
			submitOK = "False";
		}
		
		// 4.5. orderref 
		/*
		if (orderref.length == 0) {
			errormessage += seperator(errormessage);
			errormessage += "orderref"
			submitOK = "False";
		}*/
		
		// 4.6. firstname
		if (firstname.length == 0) {
			errormessage += seperator(errormessage);
			errormessage += "firstname"
			submitOK = "False";
		}
		
		// 4.7. lastname
		if (lastname.length == 0) {
			errormessage += seperator(errormessage);
			errormessage += "lastname"
			submitOK = "False";
		}
		
		// 4.8. email
		if (email.length == 0) {
			errormessage += seperator(errormessage);
			errormessage += "email"
			submitOK = "False";
		} else {
			// er van uitgaan dat emailadres van type: user_name@domain.tld
			at = email.indexOf('@');
			dot = email.lastIndexOf('.');
			
			if(at == -1 || dot == -1 || dot <= at + 1 || dot == 0 || dot == email.length - 1) {
				emailmessage += "\Use a correct e-mail address please."
				submitOK = "False";
			}
            else{
				user_name = email.substr(0, at);
				domain_name = email.substr(at + 1, email.length);
				
				if(Validate_String(user_name) === false || Validate_String(domain_name) === false) {
					emailmessage += "\Use a correct e-mail address please."
					submitOK = "False";
				}
			}
		}

	// 5. evaluate submitOK
	if(submitOK === true) {
		return true;
	}else{
		if (emailmessage != "") {
			if (errormessage != "Complete this form with your ") {
				errormessage += " please."
				alert(errormessage + "\n(* are required fields)\n" + emailmessage);
			} else {
				alert(emailmessage);
			}
		} else {
			errormessage += " please."
			alert(errormessage + "\n(* are required fields)");
		}
		return false;
	}
}