/*
 * 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();
			$("#topic_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();
	$("#topic_error").empty().hide();
	$("#msg_error").empty().hide();
	
	var name    = $("#name").val(); 
    var email   = $("#email").val(); 
	var msg   	= $("#msg").val();
	
	var radioChecked = false;
	var radioValue	 = "";
	
	$("input[@name=grpTopic]").each(function(){
		//Only one can be checked since in a radio group
		if (this.checked){
			radioChecked = true	;
			radioValue = this.value;
		}
	});
	
	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 (radioChecked){
		//Set the emailTo field based on radio button selected
		if (radioValue == "hair"){
			$("#emailTo").val("drbaxa@pilc.com");
		}else{//radioValue = "skin"
			$("#emailTo").val("diane@pilc.com");
		}
	}else{
		$("#topic_error").show().append("Please select a Topic!");
		errors++;
		if (!firstError){
			//Don't set focus on radio buttons
			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
	}  
			
    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'); 
	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.")
	}
} 