

//THIS IS THE INITAL FUNCTION CALLED FROM THE FORM.
function contact_form_submit(){
	//FIRST VERFIY THAT ALL INFORMATION IS FILLED OUT
	if (document.getElementById('txtname').value =="" || document.getElementById('txtemail').value =="" || document.getElementById('comments').value ==""){
		alert("Please complete the entire form.");
		return;
	}else{
		//ENSURE EMAIL ADDRESS IS VALID
		if (is_valid_email(document.getElementById('txtemail').value)!=false){
			//POST INFORMATION VIA AJAX SUBMISSION OVER A SIMPLE URL TRANSFER.
			var subURL = "txtname="+removeAmp(document.getElementById('txtname').value); 
			subURL += "&txtemail="+removeAmp(document.getElementById('txtemail').value); 
			subURL += "&comments="+removeAmp(document.getElementById('comments').value); 
			var url = ("interface.php");
			
			//CALL AJAX FUCNTION AND PAS IT THE URL AND POSTED ELEMENTS
			makePOSTRequest(url, subURL)
			
			//DISPLAY FORM SUBMISSION MESSAGE
			document.getElementById('contactForm').innerHTML = "<h1>Thanks, your message has been sent!</h1>"
			
		}else{
			//GENERIC FORM MESSAGE FOR MISSING FIELDS.
			alert("You must enter a valid email address.")
		}
	}
}

//REMOVE A FEW CHARACTERS AND WE WILL REPLACE THEM ON SERVERSIDE TO ENSURE FORM POSING FUNCTIONALITY.
function removeAmp(str){
	var ret;
	ret = str.split("&").join("[amp]");
	ret = ret.split("?").join("[ques]");
	ret = ret.split("#").join("[pnd]");
	return ret
}

//THESE ARE A COUPLE OF REUSED FUNCTIONS IN THIS CODE. ONE CHECKS AN EMAIL ADDRESS THROUGH REGULAR EXPRESSION AND AN AJAX COMMUNICATION FUNCTION.
function is_valid_email(email){
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}
function makePOSTRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	// Cannot create XMLHTTP instance --so the form will never de delivered
	 return false;
  }
  
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

