// JavaScript Document

var xmlHttp = null;

function sendEnquiry(){
	var txtName = getObj("txt_name").value;
	var txtEmail = getObj("txt_email").value;
	var txtEnq = getObj("txt_enquiry").value;
	
	
	//validate
	if(!txtName){
		alert("You must provide a name");
		return;
	}	
	if(!txtEmail){
		alert("You must specify an email address");
		return;
	}
	if(!txtEnq){
		alert("You need to fill in the enquiry field");
		return;
	}
	
	if(!isValidEmail(txtEmail)){
		alert("The email address you specified is not in a valid format. Please check it and try again.");
		return;
	}
	
	
	//Do the AJAX stuff here
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null){
        alert ("Browser does not support HTTP Request. Cannot perform delete operation.");
        return;
    }
    
    var sendURL = "Email_Send.asp?fromName=" + txtName + "&fromEmail=" + txtEmail + "&enquiry=" + txtEnq;
        
    xmlHttp.onreadystatechange=stateChanged ;
    xmlHttp.open("GET",sendURL,true);
    xmlHttp.send(null);
    
    showSending();
	
	
	
}

/**
*returns a boolean indicating if the given email address is valid
*/
function isValidEmail(emailAddress){
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}

function getObj(id){
	var x = document.getElementById(id);
	return x;
}

var sdivId = "sdiv"


function showSending(){
  var sdiv = document.getElementById(sdivId);
  var anchor = document.getElementById("txt_enquiry");
  sdiv.innerHTML = "Sending...";
  //sdiv.style.borderColor = "red";
  //sdiv.style.color = "red";
  //sdiv.style.backgroundColor = "#F8D6D6";
  sdiv.style.top = (anchor.offsetTop + 20) + "px";  
  sdiv.style.left = (anchor.offsetLeft + 20) + "px";     

  //document.title = sdiv.style.top + ", " + sdiv.style.left;
}

function showSent(){
  var sdiv = document.getElementById(sdivId);
  sdiv.innerHTML = "Message Sent";
  //sdiv.style.borderColor = "#39B54A";
  //sdiv.style.color = "#39B54A";
  //sdiv.style.backgroundColor = "#CAFFCC";   
}

function hideSent(){
  var sdiv = document.getElementById(sdivId);
  sdiv.style.top = "-1000px";
  sdiv.style.left = "-1000px";   
}

function clearInputs(){
	var txtName = getObj("txt_name");
	var txtEmail = getObj("txt_email");
	var txtEnq = getObj("txt_enquiry");
	
	
	txtName.value = "";
	txtEmail.value = "";
	txtEnq.value = "";
}

////////// AJAX FUNCTIONS

function stateChanged(){
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
        clearInputs();  
        showSent();
        setTimeout("hideSent()", 1500);

    }
}

function GetXmlHttpObject(){ 
    var objXMLHttp=null
    if (window.XMLHttpRequest){
        objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject){
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
return objXMLHttp
}

////////// END AJAX FUNCTIONS