function doCallMe() {
	$("#modalBody").show(); // this belongs to callme widget
	$("#modalBodyProcessing").hide(); // this is the animated icon
	revealModal('modalPage');	
}

jQuery(function($) {
	/* $("#phone").mask("(999) 999-9999"); */
	jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
	    phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, "Please enter a phone number");

	$("#callme_name").keydown(function(e){
	        if (e.keyCode == 13) {
	            validatePhone();
				return false;
	        }
	    });
	$("#callme_phone").keydown(function(e){
	        if (e.keyCode == 13) {
	            validatePhone();
				return false;
	        }
	    });
	$("#callme_email").keydown(function(e){
	        if (e.keyCode == 13) {
	            validatePhone();
				return false;
	        }
	    });
	$("#dc_signup_entry_form").validate({
  		rules: {
			callme_phone: {
				required: true,
				phoneUS: true
			}
  		},
  		messages: {
			callme_phone: {
				required: "please enter a phone number"
			}
		}
	});	
});

function validatePhone() {
	var lead_info = {};
    var mc_ajax_url = src_dir + 'ajax/callme_lead.php';
    var cms_ajax_url = ""; // recv end of Chris's central architecture goes here

	if (navigator.appName.indexOf('Explorer') != -1) {
		if ($("#callme_phone").val().length != 10 || !($("#callme_phone").val().match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/))) {
			alert('Please enter a valid 10 digit phone number, thanks!');
			return false;
		}
	}

	if (!$("#dc_signup_entry_form").valid()) {
		return false;
	} else {
		lead_info.name = $("#callme_name").val();
		lead_info.phone = $("#callme_phone").val();
		lead_info.email = $("#callme_email").val();
		lead_info.num_contractors = 0;

		if (lead_info.email) {
			if (echeck(lead_info.email)) {
				$.post(mc_ajax_url, lead_info, showCallMeResult, "text");
				$('div.modalBody').hide();
				$('div.modalBodyProcessing').show('fast');
			} else {
				alert("Doesn't appear to be a valid email address!");
				return false;
			}
		} else { // no email 
			document.dc_signup_entry_form.submit();
			hideModal('modalPage');
		}
		
		/*
		$.ajax({ url: cms_ajax_url,
		    data: { payload: JSON.stringify(lead_info) },
		        dataType: "jsonp",
		        error: function() {
		            alert("Hm, AJAX error!");
		        }
		    });
		*/
		
	}
}

function showCallMeResult(data, textStatus) {
	if (textStatus == 'success') {
		alert('Thank you, we will be contacting you shortly!');
	} else {
		alert("Hm, must not be connected to the Internet!");
	}
	document.dc_signup_entry_form.submit();
	hideModal('modalPage');
}

