jQuery.setDefaultModelDetails = function(modelCodeID) {

	var model = getLocationModelTitle();

	if(model) {

		var defaultTrans = new String();
		var basePrice = 9999999999;
		
		$.ajax({
			type:		"GET",
			url:		"/global/forms/data/trims_en_us.xml", 
			async:		false,
			cache:		false,
			dataType:	"xml",
			success:	function(xml) {
			
				$(xml).find('model').filter(function() {
							return $(this).find('title:first').text() == model;
						}).find('transmission').each(function() {
							currentPrice = parseInt(stripAlphaChars($(this).find('price').text()));
							if(currentPrice < basePrice)
								basePrice = currentPrice;
						});
						
	
				$(xml).find('model').filter(function() {
							return $(this).find('title:first').text() == model;
						}).find('transmission').filter(function() {
							return parseInt(stripAlphaChars($(this).find('price').text())) == basePrice;
						}).each(function (){
							$(modelCodeID).val($(this).find('modelcode').text());
						});	
			}	
		}); 
	}
}


jQuery.checkZipCode = function() {
	$("#find_dealer").trigger('click');
}

function onDealerSessionSet(e, obj){
	if(obj.isDealerSession){
		$('body').css("display", "none");
		window.location = "http://"+obj.dealerDomain+"/ContactUsForm";
	}else{
		
	}
	
}


/************************* DOCUMENT ACTIONS ******************************/
$(document).ready (function() {
	$(document).bind("session:dealer", onDealerSessionSet); //used if cached
	try{
		if(dealersession){ //used if not cached
			if(dealersession.isDealerSession == true){
				$('body').css("display", "none");
				window.location = "http://"+dealersession.dealerDomain+"/ContactUsForm";
			}else if(dealersession.isDealerSession == false){
				
			}
		}
	}catch(e){
	}
	
	$.preloadImages("/global/images/forms/loading_bg.png");
	
	$("#comments").keypress(function(e) {
  		if(e.charCode >= 48 )
    		if($("#comments").val().length > 500) 
     			return false;
	});
	
	/* bind find dealer event */
	
	$("#find_dealer").click(function (e) {	
		e.preventDefault();
		$.findDealer($("#zipCode").val(), "#dealer_list");
		//check for find dealer spotlight action
		if(typeof spotlightFindDealer == 'function') {
			spotlightFindDealer();
		}
	});
	
	/* prevent "return" keypress in zipCode field from submitting the form */
	
	$("#zipCode").keypress(function(e){
    	if(e.keyCode == 13) {
			if($("#zipCode").val().length < 5){
				//quick fix for autocomplete
				setTimeout('$.checkZipCode()', 10);
				e.preventDefault();
			}else {
				$("#find_dealer").trigger('click');
			}
    		return false;
    	}
	});
	
	$.attachVWModels("#modelName", "", true);
	initCookie();
	
	// This causes an issue in IE6/IE7 with call's to jQuery's append
	// due to the fact that it uses the native function appendChild
	//$.attachVWModels("#modelName", "#trim");
	
	$.setDefaultModelDetails("#modelCode");
	
	/* set contact info requirements for phone and email based on response */
	
	$("#modelName").change(function () {
		$.setModelDetails("#modelName", "#trim", "#transmission", "#modelCode", "#msrpTotal");
	});
	
	$("[name=preferredContactMethod]").click(function () {
		$.setContactRequirements($("[name=preferredContactMethod]:checked").val(), "#phone", "#email1", "#email2");
	});
	
	/* restrict input to certain form fields */
	
	$("#comments").alphanumeric({allow:".?@-_`!#$%&*+-~ "});
	$("#firstName").alpha({allow:"' -"});
	$("#lastName").alpha({allow:"' -"});
	$("#phone").numeric({allow:".()- "});
	$("#email1").alphanumeric({allow:".@-_`!#$%*+-~"});
	$("#email2").alphanumeric({allow:".@-_`!#$%*+-~"});
	$("#zipCode").numeric();
	
	/* validate and submit data */
	
	$("#contact_a_dealer").submit(function(e) {
		e.preventDefault();

    	if($("#contact_a_dealer").validate({verify:"#email1, #email2"}, getFailureColor())) {

    		if(!$('[name="dealerId"]:checked').val()) {
    			$("#find_dealer").trigger("click");
    			return false;
			}
			//populateCookie user info cookie
			var userDataObj = $("#firstName").val()+"~"+$("#lastName").val()+"~"+$("#preferredContactMethod").val()+"~"+$("#phone").val()+"~"+$("#email1").val()+"~"+$("#email2").val()+"~"+$("#zipCode").val()+"~"+"false";
			buildVwFormsUserInfoCookie(userDataObj);
		
			$("#contact_a_dealer").contact_assembleXML("#form_wrapper");
			
			//check for submit spotlight action
			if(typeof spotlightSubmit == 'function') {
				spotlightSubmit();
			}
			
      		return true;
      	}
      	return false;
    });
	
});