
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	//alert("1");
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Mozilla, Safari ...
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display our error message
		alert("Your browser doesn't support the XmlHttpRequest object");
	}
}

//Our XmlHttpRequest object
var receiveReq = getXmlHttpRequestObject();
//alert("2");

//Initiate the AJAX request
function makeRequest(url, param) {
	//alert("4");
	//If our readystate is either not started or finished, initiate a new request
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
		receiveReq.open("POST", url, true);
		//Set the function that will be called when the XmlHttpRequest objects state changes
		receiveReq.onreadystatechange = updatePage; 

		//Add HTTP headers to the request
		receiveReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		receiveReq.setRequestHeader("Content-length", param.length);
		receiveReq.setRequestHeader("Connection", "close");

		//Make the request
		receiveReq.send(param);
	}   
}

//Called every time our XmlHttpRequest objects state changes
function updatePage() {
	//alert("5");
	//Check if our response is ready
	if (receiveReq.readyState == 4) {
		//alert(receiveReq.responseText);
		//Set the content of the DIV element with the response text, if the security string is valid
		if (receiveReq.responseText.indexOf("Invalid code") >= 0)
		{
			fieldObj = document.getElementById("txtCaptcha");
			fieldObj.setAttribute("class","frmContactError");
			fieldObj.setAttribute("className","frmContactError");
			fieldObj.focus();				
			alert(receiveReq.responseText);				
		}	
		else
			document.getElementById('result').innerHTML = "<h1>" + receiveReq.responseText + "</h1>";
		//Colleen - do not bother creating a new image - 1 on page refresh is sufficient?
		//Get a reference to CAPTCHA image
		//img = document.getElementById('imgCaptcha'); 
		//Change the image
		//img.src = 'security/captcha_image.php?' + Math.random();
		}
	}

//Called every time when form is perfomed
function getParam(theForm) {
	//alert("3");
	document.getElementById('result').innerHTML = "&nbsp;";
	if (validateContactForm() == true)
	{
		//Set the URL
		var url = 'security/captcha.php';
		//Set up the parameters of our AJAX call
		var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value );
		
		var postStr = theForm.txtCaptcha.name + "=" + encodeURIComponent( theForm.txtCaptcha.value ) +
 			"&" + theForm.txtName.name + "=" + encodeURIComponent( theForm.txtName.value ) +
 			"&" + theForm.txtOrg.name + "=" + encodeURIComponent( theForm.txtOrg.value ) + 	
 			"&" + theForm.txtEmail.name + "=" + encodeURIComponent( theForm.txtEmail.value ) +
 			"&" + theForm.txtPhone.name + "=" + encodeURIComponent( theForm.txtPhone.value ) + 
 			"&" + theForm.txtMsg.name + "=" + encodeURIComponent( theForm.txtMsg.value ); 	 		
		
		//Call the function that initiate the AJAX request
		makeRequest(url, postStr);
	}
}

// Contact form validation
function validateContactForm()
{
	retError = 0;

    retError += validateField('txtCaptcha','text',1);
    retError += validateField('txtMsg','textarea',1); 
    retError += validateField('txtPhone','text',0);  
    retError += validateField('txtEmail','email',1);  
    retError += validateField('txtOrg','text',0);           
	retError += validateField('txtName','text',1);

	if (retError == 10)
	{
		    alert('Please enter a valid email address');
		    return false;
	}
	else
	if (retError > 0)
	{
		alert('Please correct the errors: fields marked with an asterisk (*) require a valid entry');
		return false;
	}
	else
	{	      
		return true;
	}
}

function validateField(fieldId, fieldType, required)
{
	//fieldBox = document.getElementById(fieldBoxId);
	fieldObj = document.getElementById(fieldId);
	
	retError = 0;	
	
	if(fieldType == 'text'  ||  fieldType == 'textarea'  || fieldType == 'email')
	{
		// Ensure that any existing highlighting is cleared (added by CH)
		fieldObj.setAttribute("class","frmContactErrorClear");
		fieldObj.setAttribute("className","frmContactErrorClear");
	}			

	if(fieldType == 'text'  ||  fieldType == 'textarea')
	{	
		if(required == 1 && fieldObj.value == '')
		{
			fieldObj.setAttribute("class","frmContactError");
			fieldObj.setAttribute("className","frmContactError");
			fieldObj.focus();
			//return false;	
			retError = 1;					
		}		

	}
	else if(fieldType == 'email')
	{	
		if((required == 1 && fieldObj.value=='')  ||  (fieldObj.value!=''  && !validate_email(fieldObj.value)))
		{				
			fieldObj.setAttribute("class","frmContactError");
			fieldObj.setAttribute("className","frmContactError");
			fieldObj.focus();
			if (fieldObj.value=='')
				    retError = 1;
			else
				    retError = 10;
			//return false;					
		}
	}
	return(retError);
}

function validate_email(emailStr)
{
	/*		
	apos=emailStr.indexOf("@");
	dotpos=emailStr.lastIndexOf(".");

	if (apos<1||dotpos-apos<2) 
	{
		return false;
	}
	else
	{
		return true;
	}
	*/
	
	//alert(emailStr);
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	return re.test(emailStr);		
}
	