// General
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}


// Send contact_us form
function sendContactUs(theElement){

	var theForm = theElement.form, z = 0;
	var contactUsName = document.getElementById('contact_us_name').value;
	var contactUsEmail = document.getElementById('contact_us_email').value;
	var contactUsBody = document.getElementById('contact_us_body').value;
		
	var errorFlag = 0;
	var errorText = 'Following fields have missing or invalid information:\r';
	var elems;

	var url;
	var response;
	
	// creates a new XMLHttpRequest object to talk to the web server
	var xmlHttp = false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try {
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	  try {
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (e2) {
	    xmlHttp = false;
	  }
	}
	@end @*/

	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
	  xmlHttp = new XMLHttpRequest();
	}


	//alert(contactUsName);
	//alert(contactUsEmail);
	//alert(contactUsBody);
	
	
	
	if(contactUsName == ""){
		errorText += "- Full Name\r";
		errorFlag = 1;
	}

	if(!isValidEmail(contactUsEmail)){
		errorText += "- Email Address\r";
		errorFlag = 1;
	}
	
	if(contactUsBody == ""){
		errorText += "- Body\r";
		errorFlag = 1;
	}
	
	if(errorFlag == 1) {
		alert(errorText);
	}
	
	if(errorFlag == 0){

	  // build URL to connect to
	  url = "contact_us_send.php?type=contact_us&name=" + encodeURIComponent(contactUsName) + "&email=" + encodeURIComponent(contactUsEmail) + "&body=" + encodeURIComponent(contactUsBody);
		//alert("before open" + url);
		
	  // open a connection to the server
	  xmlHttp.open("POST", url, true);
	
		//alert("before status: " + url);
		
	  // setup a function for the server to run when it is done
	  xmlHttp.onreadystatechange = getSendState;

		//alert("after status: " + url);

	  // send the request
	  xmlHttp.send(null);
	
		alert("Thank you for contacting us.");
		document.getElementById('contact_us_name').value = "";
		document.getElementById('contact_us_email').value = "";
		document.getElementById('contact_us_body').value = "";

	}
	
	
	function getSendState() {
			
	
		  if (xmlHttp.readyState == 4) {

				 // if "OK"
				if (xmlHttp.status != 200)
				{
					alert("Problem updating.")
				} else {
	
			    response = xmlHttp.responseText;
			    //alert(response);
	
				} // end status check
	    
		  } // end readyState check
	
		} // end getSubscribeState()	
	
}