/*
 * Contact Us jQuery onReady event
 */
         $(document).ready(function() { 
			// Numeric only fields
			$("input.numeric").numeric()
			
			// prepare Options Object 
			var options = { 
			    beforeSubmit:	validateForm,  // pre-submit callback 
				success:		formResponse,
				resetForm: 		true
			}; 
			 
			// pass options to ajaxForm 
			$('#mySurvey').ajaxForm(options); 
			
			//hide error containers
			$("#name_error").empty().hide();
			$("#email_error").empty().hide();
			$("#bday_error").empty().hide();
			$("#phone_error").empty().hide();
			$("#table1_error").empty().hide();
			$("#msg1_error").empty().hide();
			$("#msg1_error").empty().hide();
			
			// bind form using ajaxForm 
    	//	$('#mySurvey').ajaxForm( { beforeSubmit: validate } ); 

//End Ready			
        }); 
// ---------------------------------------------------------------------------
function validateForm(formData, jqForm, options) { 
	// pre-submit callback 
		$("#name_error").empty().hide();
		$("#email_error").empty().hide();
		$("#bday_error").empty().hide();
		$("#phone_error").empty().hide();
		$("#table1_error").empty().hide();
		$("#msg1_error").empty().hide();
		$("#msg1_error").empty().hide();
	
	var name    	= $("#name").val(); 
	var email  		= $("#email").val(); 
    var bmonth  	= $("#bmonth option:selected").val(); 
    var bday   		= $("#bday option:selected").val(); 
	var areacode   	= $("#areacode").val();
	var phone1   	= $("#phone1").val();
	var phone2   	= $("#phone2").val();
	var answer1   	= $("#answer1 option:selected").val(); 
	var answer2   	= $("#answer2 option:selected").val(); 
	var answer3   	= $("#answer3 option:selected").val(); 
	var answer4   	= $("#answer4 option:selected").val();
	var answer5   	= $("#answer5 option:selected").val(); 
	var answer6   	= $("#answer6 option:selected").val(); 
	var answer7   	= $("#answer7 option:selected").val(); 
	var answer8   	= $("#answer8 option:selected").val(); 
	var answer9   	= $("#answer9 option:selected").val(); 
	var answer10   	= $("#answer10 option:selected").val(); 
	var msg1   		= $("#msg1").val();
	var msg2   		= $("#msg2").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;
			}
  		}
	}
	var msg = "";
	var monthEmpty = false;
	var dayEmpty = false;
	if (bmonth == '- Month -' || bday == '- Day -' ){
		msg = "Please select ";
		if (bmonth == '- Month -'){
			monthEmpty = true;
			msg = msg + "a Birth Month";
		}
		if (bday == '- Day -'){
			if (monthEmpty){
				msg = msg + " and ";	
			}
			msg = msg + "a Birth Day";
		}
		msg = msg + "!"; 
		$("#bday_error").show().append(msg);
		errors++;
		if (!firstError){
			if (monthEmpty){
				$("#bmonth").focus();
				firstError = true;
			}else{
				$("#bday").focus();
				firstError = true;
			}
		}
	}			

	msg = "";
	if (answer1 == '- Select -' || answer2 == '- Select -' || answer3 == '- Select -'|| answer4 == '- Select -' || answer5 == '- Select -'){
		msg = "The following questions below are required:";
		if (answer1 == '- Select -'){
			msg = msg + " #1";
		}
		if (answer2 == '- Select -'){
			msg = msg + " #2";
		}
		if (answer3 == '- Select -'){
			msg = msg + " #3";
		}
		if (answer4 == '- Select -'){
			msg = msg + " #4";
		}
		if (answer5 == '- Select -'){
			msg = msg + " #5";
		}
		$("#table1_error").show().append(msg);
		errors++;	
	}
/*	for (var x = 1; x < 6; x++) {
		alert(x);
	}
*/		 


/*	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
	}  

	//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.'); 
    
	
	//unblock screen
	 $.unblockUI();
	
	//alert('status: ' + statusText + '\n\nresponseText: \n'); 
	if (statusText == "success"){
		alert('Thanks for completing our survey!'); 
	}else{
		alert("There was a problem sending your email.\n\nPlease try again.")
	}
} 