/*
 * Contact Us jQuery onReady event
 */
         $(document).ready(function() { 

			// prepare Options Object 
			var options = { 
			    beforeSubmit:	validateForm,  // pre-submit callback 
				success:		formResponse,
				resetForm: 		true
			}; 
			 
			// pass options to ajaxForm 
			$('#myForm').ajaxForm(options); 
			
			//hide error containers
			$("#name_error").empty().hide();
			$("#email_error").empty().hide();
			$("#msg_error").empty().hide();
			
			// bind form using ajaxForm 
    	//	$('#myForm').ajaxForm( { beforeSubmit: validate } ); 

//End Ready			
        }); 
// ---------------------------------------------------------------------------
function validateForm(formData, jqForm, options) { 

	// pre-submit callback 
	$("#name_error").empty().hide();
	$("#email_error").empty().hide();
	$("#msg_error").empty().hide();
	
	var name    = $("#name").val(); 
    var email   = $("#email").val(); 
	var msg   	= $("#msg").val();
	

	var errors 	= 0;
	var firstError = false;
			
	if (name == null || name == ''){
		$("#name_error").show().append("Name is required!");
		errors++;
		if (!firstError){
			$("#name").focus();
			firstError = true;
		}
	}			
	if (email == null || email == ''){
		$("#email_error").show().append("Email Address is required!");
		errors++;
		if (!firstError){
			$("#email").focus();
			firstError = true;
		}
	}else{//Email format check
		if (!/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test(email)){
			$("#email_error").show().append("Email Address is invalid!");
			errors++;
			if (!firstError){
				$("#email").focus();
				firstError = true;
			}
  		}
	}

	if (msg == null || msg == ''){
		$("#msg_error").show().append("Comments/Questions is required!");
		errors++;
		if (!firstError){
			$("#msg").focus();
			firstError = true;
		}
	}			
	

	if (errors > 0){
		
		alert ("Errors were found on the form!");
		return false; //Stop form submit
	}  
	
	//block screen
	$.blockUI('<p class="blockit"><img src="../images/busy.gif" />&nbsp;&nbsp; Just a moment...</p>'); 
		
    return true; //Allow form submit
} 		 

// post-submit callback 
function formResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
//    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
//        '\n\nThe output div should have already been updated with the responseText.'); 
    
	//alert('status: ' + statusText + '\n\nresponseText: \n'); 
	
	//unblock screen
	 $.unblockUI();
	 
	if (statusText == "success"){
		alert('Thanks for your comment!\n\nWe will get back to you as soon as possible.'); 
	}else{
		alert("There was a problem sending your email.\n\nPlease try again.")
	}
} 