// Contact Page JavaScript - geotechnicalgroup.com
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function initialStuff(){
	//Preload a few images
	preload1 = new Image(); 
	preload1.src="../images/inputerror.jpg"; 
	preload2 = new Image(); 
	preload2.src="../images/check.jpg"; 
	preload3 = new Image(); 
	preload3.src="../images/modalbg.jpg";
	preload4 = new Image(); 
	preload4.src="../images/button-ok.jpg"; 
	preload5 = new Image(); 
	preload5.src="../images/sending.gif";  
}

//Proposal +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function goContact(){
	var yourname = document.getElementById("thename").value;
	var emailaddress = document.getElementById("theemailaddress").value;
	var companyname = document.getElementById("thecompany").value;	
	var subject = document.getElementById("thesubject").value;
	var message = document.getElementById("themessage").value;
	
	//Check to see if the fields are filled
	if(yourname.length > 0 && emailaddress.length > 0 && validEmail(emailaddress) && subject != "Please make a selection..." && message.length > 0){ 
	
		//Everything is here, ship the data...
		//Clear all the error messages
			$('label').removeClass("error"); 
			$('#error-text').hide();
			
		//If they don't enter anything for the company or web address, set it to "None specified"
			if(companyname.length == 0){ companyname = "None specified"; }
		
		//Sending... indicator text
			$('#sending-text').show();
		//Send the info to the PHP processor
			var url = "ui/scripts/contact.php";
				request.open("POST",url,true);
				request.onreadystatechange = showConfirmation;
				request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
				request.send("theirname="+escape(yourname)+"&theircompany="+escape(companyname)+"&theiremail="+escape(emailaddress)+"&theirsubject="+escape(subject)+"&theirmessage="+escape(message));
	}
	else{ 
		//There's some missing info somewhere so set the individual errors
			if(yourname.length == 0){ $('#thename').prev().addClass("error"); }
				else{ $('#thename').prev().removeClass("error"); }	
			//This will check the validity of the email address
			checkyEmail = validEmail(emailaddress); 
			if(emailaddress.length == 0 || checkyEmail == false){ $('#theemailaddress').prev().addClass("error"); }
				else{ $('#theemailaddress').prev().removeClass("error"); }		
			if(subject == "Please make a selection..."){ $('#thesubject').prev().addClass("error"); }
				else{ $('#thesubject').prev().removeClass("error"); }	
			if(message.length == 0){ $('#themessage').prev().addClass("error"); }
				else{ $('#themessage').prev().removeClass("error"); }	
			
			$('#error-text').show();
	}
}
//Proposal Confirmation
function showConfirmation(){
		if(request.readyState == 4){
			if(request.status == 200){

				//Clear Form so it looks reset
				$("#thename").val('');
				$("#theemailaddress").val('');
				$("#thecompany").val('');
				$("#thesubject").val('Please make a selection...');
				$("#themessage").val('');
				$('#sending-text').hide();
				
				//Show the confirmation modal
				$('#dialog').jqmShow();
			
			} else {
				var message = request.getResponseHeader("Status");
				if((message.length == null) || (message.length <= 0)){
					alert("Error! Request status is "+ request.status);
				} else {
				alert(message);
			}
		}
	}
}

//Uses a regular expression to verify that the email address is real (or at least has the appearance of a real address)
function validEmail(notifiedEmail){
	var goodEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	return goodEmail.test(notifiedEmail);
}

//Ajax Jazz
var request = null;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }
if (request == null)
    alert("Error creating request object!");
