// JavaScript Document
function validatePropertyAlertForm(form){

	var name = trim(form.name_field.value);
	var telephone = trim(form.telephone_field.value);
	var email = trim(form.email_field.value);
	var region = form.country.value;
	
	var errors = false;
	
	//Validate name 
	if(name==""){
		DWRUtil.setValue('name', 'Name:* <br> <font color="#D38069">Invalid Name</font>');
		errors = true;
	}
	else{
		DWRUtil.setValue('name', 'Name:* ');
	}
	
	//Validate telephone number
	if(telephone==""){
		DWRUtil.setValue('telephone', 'Telephone:* <br> <font color="#D38069">Invalid Telephone</font>');
		errors = true;
	}
	else if(isNaN(telephone)){
		DWRUtil.setValue('telephone', 'Telephone:* <br><font color="#D38069">Telephone must be a number</font>');
		errors = true;
	}
	else if(telephone.length<8){
		DWRUtil.setValue('telephone', 'Telephone:* <br><font color="#D38069">Telephone is too short</font>');
		errors = true;
	}	
	else{
		DWRUtil.setValue('telephone', 'Telephone:* ');
	}

	//Validate if the email is valid
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){
		DWRUtil.setValue('email', 'Email:* <br> <font color="#D38069">Invalid Email</font>');
 		errors = true;
	}
	else{
		DWRUtil.setValue('email', 'Email:* ');
	}
	
	//Validate if a country/Region was selected
	if(region==0){
		DWRUtil.setValue('region', 'Where looking to buy?* <font color="#D38069">Please select a Region</font>');
		errors = true;
	}
	else{
		DWRUtil.setValue('region', 'Where looking to buy?* ');
	}
	
	if(!errors){
		form.submit();
	}
}


function trim(sString)
{
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}	

	
	