function imageSwap(){
	var baseUrl = window.location.href;			
	if (baseUrl.indexOf("emailing_proefplaatsing_minihotcold") >= 0){
		document.getElementById("bannersmall").src = "images/vis_banner_hot_and_cold.jpg";
	}
	if (baseUrl.indexOf("emailing_proefplaatsing_minihotcold") >= 0){
		document.getElementById("bannersmall").src = "images/vis_banner_hot_and_cold.jpg";
	}
	return baseUrl;
}

function gotoContactPage(topic){
	var targetUrl 	= '';
	var page 		= 'contact.aspx';
	var productName	= getParam('koffiemachine');
	
	if(productName != ""){
		targetUrl = page+'?koffiemachine='+productName+ '&contacttopic=' + topic;
	} else {
		targetUrl = page+ '?contacttopic=' + topic;
	}
	document.location.href = targetUrl;
}

// minimal check if field is empty
function isNameFieldEmpty(objElement,errorLayerId) {

	var str = objElement.value;
	var trimmed = str.replace(/^\s+|\s+$/g, '') ;
	if (trimmed == "") {
		document.getElementById(errorLayerId).style.visibility = "visible";
		document.getElementById(errorLayerId).style.display = "block";
		return true;
	}
	return false;
}

//Number validation
function validateNumber(objElement, errorDiv)
{

    var strString = objElement.value.replace(/^\s+|\s+$/g, '') ;
	var strValidChars = "0123456789"; 
	var strChar;
	var strLen = strString.length;
	
	// empty field
	if (strString == "") {
		document.getElementById(errorDiv).style.display = "block";
		document.getElementById(errorDiv).style.visibility = "visible";
		return false;
	}
	
	// test strString consists of valid characters listed above
	for (i = 0; i < strString.length; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			document.getElementById(errorDiv).style.display = "block";
			document.getElementById(errorDiv).style.visibility = "visible";
			return false;
		}
	}
	return true;
}

function validatePostcode(objElement) {
    
    var postcodeInputValue = objElement.value.replace(/^\s+|\s+$/g,''); //cuts off spaces before and after string
	var postcodeFormat = /^[0-9]{4}\s{1}[a-z|A-Z]{2}$/;
	
	if(postcodeInputValue == ''){
	    return true;
	} else if (!postcodeFormat.test(postcodeInputValue)){
	    $('#contact_postcode_error').show();
	    document.getElementById("contact_postcode_error").style.visibility = "visible";
	    return false;
	} else {
        return true;
    }
}


//Phone Validation
function validatePhone(objElement)
{
    var strString = objElement.value.replace(/^\s+|\s+$/g, '') ;
	var strLen = strString.length;
	
	// must be exactly 10 digits
    if (strLen != 10) {
	    document.getElementById("divTelInvalidError").style.display = "block";
	    document.getElementById("divTelInvalidError").style.visibility = "visible";
	    return false;
    }
	if (!validateNumber(objElement, "divTelNonNumberError")) {
	    return false;
	}
	
	return true;
}

//
//Email Validation
//
function validateEmail(objElement) {

	var strValue = objElement.value.replace(/^\s+|\s+$/g, '') ;
	
	var len = strValue.length;
	//empty email field
	if (len < 0 || strValue == "") {
		document.getElementById("divEmptyFieldError").style.display = "block";
		document.getElementById("divEmptyFieldError").style.visibility = "visible";
		return false;
	}
	else {
		var in_space = strValue.indexOf(" ");
		if (in_space != -1)
		{ 
			document.getElementById("divEmailInvalidError").style.display = "block";
			document.getElementById("divEmailInvalidError").style.visibility = "visible";
			return false;
		}
		
		var alpha = strValue.indexOf("@");
		var last_alpha = strValue.lastIndexOf("@");
		
		// More than one @, No @, in first position, or name too short
		if (alpha != last_alpha && (alpha == -1 || alpha == 0 || len<6))
		{ 
			document.getElementById("divEmailInvalidError").style.display = "block";
			document.getElementById("divEmailInvalidError").style.visibility = "visible";
			return false;
		}

		var last_p = strValue.lastIndexOf(".");
		// Be sure period at least two spaces after @, but not last char.
		if (last_p - alpha < 2 || last_p == (len - 1) )
		{ 
			document.getElementById("divEmailInvalidError").style.display = "block";
			document.getElementById("divEmailInvalidError").style.visibility = "visible";
			return false;
		}
	}
	return true;
}

function validateForm(thisform)
{
    var ret = true;
    
	clearErrors();
	if (isNameFieldEmpty(thisform.contact_name,"divNameFieldError")) {
		ret = false;
	}
    if (isNameFieldEmpty(thisform.contact_lastname,"divLastnameFieldError")) {
		ret = false;
	}
    if (isNameFieldEmpty(thisform.contact_company_name,"companyname_error")) {
		ret = false;
	}
    if (isNameFieldEmpty(thisform.contact_address,"address_error")) {
		ret = false;
	}
    if (isNameFieldEmpty(thisform.contact_address2,"address2_error")) {
		ret = false;
	}
    if (isNameFieldEmpty(thisform.contact_city,"city_error")) {
		ret = false;
	}
    if (isNameFieldEmpty(thisform.contact_postcode,"postcode_error")) {
		ret = false;
	}
	
	/*
	if (isNameFieldEmpty(thisform.contact_employees,"contact_employees_error")) {
        ret = false;
    }*/
	
	if (!validateEmail(thisform.contact_email)) {
		ret = false;
	}
	if (!validatePhone(thisform.contact_phone)) {
		ret = false;
	}
	if (!validatePostcode(thisform.contact_postcode)) {
		ret = false;
	}
	if (thisform.contact_employees.value != "" && !validateNumber(thisform.contact_employees, "divEmployeeNoInvalidError")) {
	    ret = false;
	}
	return ret;
}

function clearErrors() {

	document.getElementById("address_error").style.display = "none";
	document.getElementById("address_error").style.visibility = "hidden";
	
    document.getElementById("address2_error").style.display = "none";
	document.getElementById("address2_error").style.visibility = "hidden";
	
    document.getElementById("companyname_error").style.display = "none";
	document.getElementById("companyname_error").style.visibility = "hidden";
	
    document.getElementById("city_error").style.display = "none";
	document.getElementById("city_error").style.visibility = "hidden";
	
    document.getElementById("postcode_error").style.display = "none";
	document.getElementById("postcode_error").style.visibility = "hidden";
	
    document.getElementById("divEmailInvalidError").style.display = "none";
	document.getElementById("divEmailInvalidError").style.visibility = "hidden";
	
	document.getElementById("divTelInvalidError").style.display = "none";
	document.getElementById("divTelInvalidError").style.visibility = "hidden";
	
	document.getElementById("divTelNonNumberError").style.display = "none";
	document.getElementById("divTelNonNumberError").style.visibility = "hidden";
	
	document.getElementById("divEmptyFieldError").style.display = "none";
	document.getElementById("divEmptyFieldError").style.visibility = "hidden";
	
	document.getElementById("divNameFieldError").style.visibility = "hidden";
	document.getElementById("divNameFieldError").style.display = "none";
	
    document.getElementById("divLastnameFieldError").style.visibility = "hidden";
	document.getElementById("divLastnameFieldError").style.display = "none";
	
	document.getElementById("divEmployeeNoInvalidError").style.display = "none";
	document.getElementById("divEmployeeNoInvalidError").style.visibility = "hidden";

	document.getElementById("divWSEmptyFieldError").style.display = "none";
	document.getElementById("divWSEmptyFieldError").style.visibility = "hidden";
	
	document.getElementById("contact_employees_error").style.display = "none";
	document.getElementById("contact_employees_error").style.visibility = "hidden";
    
    $('#contact_postcode_error').hide();
}

