//==================================================================================================
//AJAX FORM stuff
//==================================================================================================

function doPostcodeSearch(value, country){
   var opt = {
			method: 'post',
			postBody: 'postal_code='+value,
			onSuccess: function(t) {
				//alert(t.responseText);
				$('city').value = t.responseText;
				$('city').className='inputTextActive';
			},
			on404: function(t) {
				alert('Error 404: location "' + t.statusText + '" was not found.');
			},
			onFailure: function(t) {
				alert('Error ' + t.status + ' -- ' + t.statusText);
			}
	}
	if(country=='be'){
		new Ajax.Request('contact/postcodeBE.php', opt);
	} else if(country=='nl'){
		new Ajax.Request('contact/postcodeNL.php', opt);
	}
}



function startAutoCompleter(country){
	if(country=='be'){
		new Ajax.Autocompleter("city", "autocomplete_city_choices", "contact/autocompleteCityPostBE.php", {updateElement: updateCityListFunctionBE});
	} else if(country=='nl'){
		new Ajax.Autocompleter("city", "autocomplete_city_choices", "contact/autocompleteCityPostNL.php", {updateElement: updateCityListFunctionNL});
	} else if(country=='us'){
		new Ajax.Autocompleter("city", "autocomplete_city_choices", "contact/autocompleteCityPostUS.php", {updateElement: updateCityListFunctionUS});
		//new Ajax.Request('contact/postcode.php', opt);
	}
}

function updateCityListFunctionBE(selected)
{
	$('city').value=selected.getAttribute('city');
	$('postcode').value=selected.getAttribute('postalcode');
	$('postcode').className ='inputTextActive';
}

function updateCityListFunctionNL(selected)
{
	$('city').value=selected.getAttribute('city');
}

function updateCityListFunctionUS(selected)
{
	$('city').value=selected.getAttribute('city');
	$('state').value=selected.getAttribute('state');
	$('state').className ='inputTextActive';
}








function checkWholeForm() {
    var why = "";
	var error = "";
	var id = "";
	
	id = 'firstName';
    error = checkFirstName($F(id), $(id).title);
	if (error.length != 0) {
		displayError(id, error, id+'Error');
		why += error;
	} 
	
	id = 'lastName';
    error = checkLastName($F(id), $(id).title);
	if (error.length != 0) {
		displayError(id, error, id+'Error');
		why += error;
	} 
	
	id = 'email';
    error = checkEmail($F(id), $(id).title);
	if (error.length != 0) {
		displayError(id, error, id+'Error');
		why += error;
	} 
	
	id = 'subject';
    error = checkDropdown($(id).selectedIndex);
	if (error.length != 0) {
		displayError(id, error, id+'Error');
		why += error;
	}
	
	id = 'question';
    error = checkTextArea($F(id), $(id).title);
	if (error.length != 0) {
		displayError(id, error, id+'Error');
		why += error;
	}

    if (why != "") {
		$('submitError').innerHTML = "<span class='error'>Not all required fields are filled in correctly.<br />Please check the above page for errors.</span>";
		$('submitError').style.display='block';
       return false;
    }
	return true;
}

/*
function blurFormField2(id, errorid, e) {
	var targ
	if (!e) {
		var e = window.event;
	}
	//IE
	//alert(e.srcElement + " " + e.fromElement);
	//mozilla
	alert(e.explicitOriginalTarget.id + " " + e.target.id);
	//alert(Event.element(e));
	if (e.target) {
		targ = e.target;
		//alert('target  ' + targ.id);
	}
	if (targ.nodeType == 3){ // defeat Safari bug
	   targ = targ.parentNode;
	}
	var tname;
	tname=targ.tagName;
	tid=targ.id;
	//alert("You clicked on a " + tname + " " + tid + " element.");

}
*/

var submitClicked = false;
	
function editFormField(id, errorid) {
	if($F(id)!='' && $F(id)!=$(id).title) {
		var error = '';
		if(id =='firstName'){
			error = checkFirstName($(id).value, $(id).title);
		} else if(id =='lastName'){
			error = checkLastName($(id).value, $(id).title);
		} else if(id == 'email'){
			error = checkEmail($(id).value, $(id).title);
		} else if(id == 'subject'){
			error = checkDropdown($(id).selectedIndex);
		} else if(id == 'question'){
			error = checkTextArea($(id).value, $(id).title);
		}
		if (error.length != 0) {
			displayError(id, error, errorid);
		} else {
			//-----------------------
			//alert(submitClicked);
			//if(!submitClicked) {
				removeError(id, errorid);
			//}
		}
	}
}
	
function blurFormField(id, errorid) {
	if($F(id)=='' || $F(id)==$(id).title){	
		//alert(submitClicked);
		//if(!submitClicked) {
			removeError(id, errorid);
		//}
		setClass(id, 'Passive');
		$(id).value=$(id).title;
	} 
	else {
		var error = '';
		if(id =='firstName'){
			error = checkFirstName($(id).value, $(id).title);
		} else if(id =='lastName'){
			error = checkLastName($(id).value, $(id).title);
		} else if(id == 'email'){
			error = checkEmail($(id).value, $(id).title);
		} else if(id == 'subject'){
			error = checkDropdown($(id).selectedIndex);
		} else if(id == 'question'){
			error = checkTextArea($(id).value, $(id).title);
		}
		if (error.length != 0) {
			displayError(id, error, errorid);
		} else {
			//-----------------------
			//alert(submitClicked);
			//if(!submitClicked) {
				removeError(id, errorid);
			//}
		}
	}
}

function displayError(id, errorMsg, errorId) {
	$(errorId).innerHTML = "<span class='error'>" + errorMsg + "</span>";
	setClass(id, 'Invalid');
	$(errorId).style.display='block';
}
function removeError(id, errorId) {
	$(errorId).innerHTML = "";
	setClass(id, 'Active');
	$(errorId).style.display='none';
}

function setClass(id, state) {
	if ($(id).type == 'text')
		$(id).className='inputText' + state;
	else if ($(id).type == 'select-one')
		$(id).className='select' + state;
	else if ($(id).type == 'textarea')
		$(id).className='inputText' + state;
}





function checkFirstName(string, defaultString) {
	var error = "";
	if (string.length == 0 || string == defaultString){
		error = "You didn't enter your first name.\n";
		//$('firstName').className = 'inputTextInvalid';
	}
	return error;
}

function checkLastName(string, defaultString) {
	var error = "";
	if (string.length == 0 || string == defaultString){
		error = "You didn't enter your last name.\n";
		//$('lastName').className = 'inputTextInvalid';
	}
	return error;
}

function checkEmail(string, defaultString) {
	var error="";
	if (string.length == 0 || string == defaultString) {
		error = "You didn't enter an email address.\n";
	}
	
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(string))) { 
		error = "Not a valid email address.\n";
	}
	else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/;
		if (string.match(illegalChars)) {
			error = "Illegal characters in email address.\n";
		}
	}
	return error;
}

function checkDropdown(choice) {
    var error = "";
    if (choice == 0) {
       error = "You didn't choose an option from the drop-down list.\n";
    }    
	return error;
}

function checkTextArea(string, defaultString) {
	var error = "";
	if (string.length == 0 || string == defaultString){
		error = "You didn't enter any text.\n";
		//$('firstName').className = 'inputTextInvalid';
	}
	return error;
}
