$(document).ready(function(){
	// init validator
	jQuery.validator.addMethod("zipcode", 
		function(zip){
			if(zip.length == 0) {
				return true;
			}
			else if(zip.match(/^\d{5}?$/)) {
				return true;
			}
			return false;
		}, ""
	);
	jQuery.validator.addMethod("phone", 
		function(phone) {
			if(phone.match(/^(((\(\d{3}\)\s??)|(\(\d{3}\)\-??)|(\d{3}\-??))\d{3}\-\d{4})$|^\d{10}?$/)){ // if ya don't know, now ya know!
				return true;
			}
			else if( $("#email:blank").length == 0){
				return true;
			}
			return false;
		}, ""
	);
	
	jQuery.validator.messages.required = "";	
 	jQuery.validator.messages.phone = "";	
 	jQuery.validator.messages.digits = "";	
 	jQuery.validator.messages.zipCode = "";	
 	jQuery.validator.messages.email = "";	
	
	var container = $('div.container');	
	var validator = 
	$("#F1").bind("invalid-form.validate", function() {
			//debugger;
			$("#dialog p.A").html('The form contains the following errors: ');
			$("#dialog").jqmShow(); 
	}).validate({
		focusInvalid: false,
		errorContainer: container,
		errorLabelContainer: $("ol", container),
		wrapper: 'li',
		meta: "validate",
		submitHandler: procForm		
	});	
	// init masked inputs
	$("input.phone").mask("(999) 999-9999");	
	// init charcounter										
	$("#comments").charCounter(500, {
		container: "#charcount"
	});
	// init modal dialogs
	$('#dialog').jqm({toTop: false});
	$('#thankYouBox').jqm({toTop: false});
	$('input.jqmdX')
	.hover(
	  function(){ $(this).addClass('jqmdXFocus'); }, 
	  function(){ $(this).removeClass('jqmdXFocus'); })
	.focus( 
	  function(){ this.hideFocus=true; $(this).addClass('jqmdXFocus'); })
	.blur( 
	  function(){ $(this).removeClass('jqmdXFocus'); });
	//init slideshow
	$('#slideBg').cycle({
		fx:  'fade',
		timeout:  8000,			
		speed:  2500
	 }); 	
});

procForm = function()
{
    $("#submitBtn").hide();
    var thisSerial = 0;
    if (typeof $("#serialNo").val() != "undefined")
    {
    	thisSerial = $("#serialNo").val();
    }
    var thisModel = 0;
    if (typeof $("#modelNo").val() != "undefined")
    {
    	thisModel = $("#modelNo").val();
    }
	$.post("/com/XAJA.cfm",
	{ com: "formProc",
		method: "directLendingProc",
		source: 	 	$("#source").val(),
		storeId: 	 	$("#storeId").val(),
		firstName:  $("#firstName").val(),
		lastName: 	$("#lastName").val(),
		email: 			$("#email").val(),
		phone: 			$("#phone").val(),
		addr:		    $("#addr").val(),
		referer:    $("#referer").val(),
		zipCode: 		$("#zipCode").val(),
		serial:		thisSerial,
		model:		thisModel,
		comments: 	$("#comments").val()
	 },
	function(result){
		if (result.FAIL == true){
			$('#dialog').jqmShow();
            $("#submitBtn").show();
		}
		else {
			// goal!
			var lastId = result.RESULT;
			//pageTracker._trackPageview("/direct_lending/first_release.goal?lastId=" + lastId);
			var fn = $("#firstName").val();
			var zc = $("#zipCode").val();
			$('#clientFirstName').val(fn);
			$('#outZip').val(zc);
			setTimeout("$('#F2').submit();", 500);
		}
	}, "json" );
}

