var http=createRequestObject();

function processContact(obj){
	
	document.getElementById("success_message").style.display = "none";	
	document.getElementById("error_message").style.display = "none";	
	
	var yourname = obj.yourname;
	var email = obj.email;
	var phone = obj.phone;
	
	var enquiry_about = obj.enquiry_about;

	var notes = obj.notes;

	if(yourname.value.trim() == ''){
		errshowDiv('yourname');
		yourname.focus();
		return;
	}
	errhideDiv('yourname');
	

	if(email.value.trim() == ''){
		errshowDiv('email');
		email.focus();
		return;
	}else if(!emailAddresscheck(email.value)){
		alert("Please provide valid email address.");
		email.focus();
		return;
	}
	errhideDiv('email');	
	
	var postdata = "yourname="+yourname.value;
	postdata += "&email="+email.value;
	postdata += "&phone="+phone.value;
	postdata += "&enquiry_about="+enquiry_about.value;
	postdata += "&notes="+notes.value;
	

	http.open("POST",  "../../../index.php?action=contact");
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = _contact_handle;
	http.send(postdata);
	
}


function _contact_handle(){
	 if((http.readyState == 4)&&(http.status == 200)){
		var response = http.responseText;	
		if(response == 1){
			document.getElementById("success_message").style.display = "";	
			document.getElementById("contactForm").style.display = "none";	
			
		}else if(response == 2){
			document.getElementById("error_message").style.display = "";	
		}
	 }
}


function emailAddresscheck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ 
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		
		return false
	 }

	 return true					
}

function errhideDiv(divid){
	document.getElementById("sp_"+divid).style.display = "none";
}

function errshowDiv(divid){
	document.getElementById("sp_"+divid).style.display = "";
}