jQuery(document).ready(function(){

	//var already_submitted = false;
	
	/*
jQuery('#CalloutBoxSubmit').click(function(){
		jQuery('#CalloutBoxSubmit').hide();
	});
*/
	
	jQuery('#CalloutForm').submit(function(){
		//if (!already_submitted) {
			sendCalloutRequest();
			//already_submitted = true;
		//}
		return false;	
	});
});

function ValidateCallout() {
	
	// for debugging
	//return true;
	
	
	// Make sure all fields are filled in
	var name = jQuery("#CalloutBoxName").val();
	var contact = jQuery("#CalloutBoxContact").val();
	var market = jQuery("#CalloutBoxMarket").val();
	var err = "";
	if (name == "" || contact == "" || market == "") {
		alert("Please complete all fields to submit this form.");
		return false;
	}
	

	// Check the phone number on the server side
	jQuery.ajax({
		type: "POST",
		async: false,
		url: "/includes/functions_form_validation.php", 
		data: {validation : "phone", test_str : jQuery("#CalloutBoxContact").val()},
		success: function(data){
			if (data == '0') {
				alert("Invalid phone number");
				retval = false;
			} else {
				retval = true;
			}
		}
	});
	
	return retval;
	
}



	
function sendCalloutRequest(){

	//var isValid = ValidateCallout();
	
	if (ValidateCallout()) {

		var parms = {};
		
		parms.CalloutBoxName = jQuery("#CalloutBoxName").val();
		parms.CalloutBoxContact = jQuery("#CalloutBoxContact").val();
		parms.CalloutBoxMarket = jQuery("#CalloutBoxMarket").val();
		parms.CalloutBoxReferringUrl = jQuery('#CalloutBoxReferringUrl').val();
		parms.PageTitle = jQuery('#PageTitle').val();
		parms.adwordsReporter = jQuery('#adwordsReporter').val();
		
		jQuery.post('/contact/order_service_processor.php', parms, function(){
			//alert("Your request has been received - thank you.\nAn Integra representative will contact you shortly.");
			//jQuery("#CalloutBoxName").val('');
			//jQuery("#CalloutBoxContact").val('');
			window.location.replace("/services/contact_confirm.php");
		});
	
	}	
	
				//jQuery('#CalloutBoxSubmit').show();
}




