// JavaScript Document

function submitForm(){
	
 var cform = document["cform"];
 var error_msg = document.getElementById("error_msg");
 var valid = true;
 //check for empty values
 if(cform["name"].value==""){ valid = false; cform["name"].style.backgroundColor = "rgb(255,255,180)"; }
 if(cform["email"].value==""){ valid = false; cform["email"].style.backgroundColor = "rgb(255,255,180)"; }
 if(cform["message"].value==""){ valid = false; cform["message"].style.backgroundColor = "rgb(255,255,180)"; }

 //if valid is false at this point return with blank fields error message
 //prepare error message and display
 if(!valid){
   var msg = "Please fill all required fields";
   error_msg.innerHTML = msg;
 }
 //move on to email validation *only run this if the email is not empty
 if(cform["email"].value != ""){
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(cform["email"].value) == false){ 
   	 valid = false; 
	 cform["email"].style.backgroundColor = "rgb(255,255,180)"; 
  	 var msg = "Please enter a valid email";
   	 error_msg.innerHTML = msg;
   }
 }
 //finally if still valid send ajax request and submit form data
 if(valid){
   //prep vars
   var name = cform["name"].value;
   var email = cform["email"].value;
   var message = escape(cform["message"].value);
   
   //before sending let's disable the form
   cform["name"].disabled = true;
   cform["email"].disabled = true;
   cform["message"].disabled = true;
   cform["submit"].disabled = true;
   
   var url= "contact-proc.php?name="+name+"&email="+email+"&message="+message;
   var el = error_msg;
   
   var obj = {
		
		runResponse : function(response){
			this.el.innerHTML = response;
		},
		
		el : el
		
   }
   
   ajaxGet(url, obj);
 }
 return false;
}


function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to
      // build the message. Message includes i (the object's property name)
      // then the object's property value on a new line
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they
      // click "CANCEL" then quit this level of recursion
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object
      if (typeof obj[i] == "object") {
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}

function dump(obj){
	
	var msg = '';
	
	for(prop in obj){
		
		if(typeof prop == 'object'){
			msg += dump(prop);
		}
		else{
			
			msg += prop + " = " + obj[prop] + "\n";	
		}
	}
	
	return prop;
}
