	/**********************************************************************************
05.22.09: compuware 
based on the form action js files with additional functions specific to the request a brochure gui
**********************************************************************************/
//toggle visibility of tabs
function showHide(n){
	var stepsArray = new Array("#brochure_step_1", "#brochure_step_2", "#brochure_step_3");
	for(var i= 0; i<stepsArray.length; i++){
		if(stepsArray[i] != n){
			$(stepsArray[i]).css('display','none');
		}else{
			$(stepsArray[i]).css('display','block');
		}
	}
	if(n == "#brochure_step_1"){
		initSelectionTabs();
	}else if(n == "#brochure_step_2"){
		omnitureTrackStep('1');//brochures selected and 'next' button clicked
	}else if(n == "#brochure_step_3"){
		omnitureTrackStep('3'); //user landed on step 3 after submitting info
		floodlightTrackStep('3');
	}

}

//array of selected brochures w/delivery and language selections
var resultsArray = new Array();
//keeping track of the number of brochures selected to mail as well as the total number of brochures selected
var numMailSelected = 0;
var numSelected = 0;

//returns array of brochures selected with vehicle,  language and delivery method
function returnResults(){
	return resultsArray;
}

//sets all brochure selections not yet visible (not yet selected) to a default delivery method selection of 'download' and a default language value of 'english'
function initSelectionTabs(){
	$("#brochure_step_1").find("#selectionDropdowns").each(function(){		
		var styleString = jQuery.trim(String($(this).attr('style'))).toLowerCase();
		var a = styleString.split(':');
		var styleCheck = jQuery.trim(a[1]);	
		if(styleCheck == 'none' || styleCheck == 'none;'){ 
			$(this).find("select#brochure_delivery option[value='download']").attr("selected","selected");
			$(this).find("select#brochure_language option[value='english']").attr("selected","selected");
		}
	});
}

//called when page loaded, set per model so that model-specific brochure is selected
function selectInitBrochure(n){
	brochureToggleOn(n);
}

//disable the mail option on brochures not yet selected and on those where download was selected as the delivery method
function mailOptionOff(){
			
	$("#brochure_step_1").find("#brochureSelection").each(function(){				
		
		var styleString = jQuery.trim(String($(this).find("#brochureOn").attr('style'))).toLowerCase();
		var a = styleString.split(':');
		var styleCheck = jQuery.trim(a[1]);
		
		var delivery = jQuery.trim($(this).find('select#brochure_delivery :selected').text().toLowerCase()); 
		
		//brochures not yet selected
		if(styleCheck == 'none' || styleCheck == 'none;'){ 
			
			$(this).find("select#brochure_delivery option[value='mail']").text("*Mail");
			$(this).find("select#brochure_delivery option[value='download']").attr("selected","selected");
			
			if ($.browser.msie){
				//msie does not support 'disabled' attribute
				$(this).find("select#brochure_delivery option[value='mail']").remove();
				$(this).find("select#brochure_delivery optgroup").remove();
				var element = '<optgroup class="optStyle" label="*Mail"></optgroup>';
				$(element).insertBefore($(this).find("option[value='download']"));
				
			}else{
				$(this).find("select#brochure_delivery option[value='mail']").attr("disabled","disabled");//works in firefox
			}
			
		}else{
			
			//brochures selected with 'download' as the chosen delivery method
			if(delivery == "download"){
				
				$(this).find("select#brochure_delivery option[value='mail']").text("*Mail");
				
				if ($.browser.msie){
					//msie does not support 'disabled' attribute
					$(this).find("select#brochure_delivery option[value='mail']").remove();
					$(this).find("select#brochure_delivery optgroup").remove();
					var element = '<optgroup class="optStyle" label="*Mail"></optgroup>';
					$(element).insertBefore($(this).find("option[value='download']"));
					
				}else{
					$(this).find("select#brochure_delivery option[value='mail']").attr("disabled","disabled");//works in firefox
				}
				
			}
		}
	});
	
	//delivery method of 'mail' is no longer an option so show the '2 is the limit for mail' warning message 
	mailLimitWarningOn();
	
}

//enable mail option on all brochure selections
function mailOptionOn(){
	
	$("#brochure_step_1").find("#selectionDropdowns").each(function(){
		
		$(this).find("select#brochure_delivery option[value='mail']").text("Mail");
		$(this).find("select#brochure_delivery option[value='mail']").removeAttr("disabled");
		
		//msie does not support 'disabled' attribute
		if ($.browser.msie){
			$(this).find("select#brochure_delivery").each(function (){
				$(this).find("optgroup").remove();
				if(($(this).find("option[value='mail']").length == 0)){
					var element = '<option value="mail">Mail</option>';
					$(element).insertBefore($(this).find("option[value='download']"));
				}
			});	
			
		}
		
	});
	
	//delivery method of 'mail' is now an option so hide the '2 is the limit for mail' warning message 
	mailLimitWarningOff();
	
}

// '2 is the limit for mail' warning message is on
function mailLimitWarningOn(){
	$("#brochure_step_1").find("#mail_limit_warning").css('display','block');
}

// '2 is the limit for mail' warning message is off
function mailLimitWarningOff(){
	$("#brochure_step_1").find("#mail_limit_warning").css('display','none');
}

//determines what has and has not been selected and triggers the ui to react appropriately
function selectionResults(){
	
	//array based on names of brochure selections
	var sectionArray = new Array('vwfam','routan','cpl','newbeetle','passat','jetta','gti','touareg','rabbit','tiguan','eos','cc', 'golf');
	//clearing out the vars to repopulate them
	resultsArray = new Array();
	numMailSelected = 0;
	numSelected = 0;
	
	for(var i = 0; i<sectionArray.length; i++){
			
		$("#brochure_" + sectionArray[i]).find("#brochureSelection").each(function(){
			
			var vehicle = sectionArray[i];
			var styleString = jQuery.trim(String($(this).find("#brochureOn").attr('style'))).toLowerCase();
			var a = styleString.split(':');
			var styleCheck = jQuery.trim(a[1]);
						
			if(styleCheck == "block;" || styleCheck == "block"){
				
				var language = $(this).find('select#brochure_language :selected').text();
				var delivery = $(this).find('select#brochure_delivery :selected').text();
				
				var vehicleArray = new Array(vehicle,language,delivery);
				resultsArray.push(vehicleArray);
				
				numSelected = numSelected + 1;
				if(delivery.toLowerCase() == 'mail'){
					numMailSelected = numMailSelected + 1;
				}
				
			}
		});
	}
	
	//changing the ui according to the user selections
	setUIResults();
	
}

//user selections determine ui
function setUIResults(){
	
	if(numMailSelected == 2){
		mailOptionOff();
	}else{
		mailOptionOn();
	}
	if(numSelected > 0){
		nextButtonOn();
	}else{
		nextButtonOff();
	}
}

//make the next button visible
function nextButtonOn(){
	$('#bottomFormButton').css('display','block');
}

//hide the next button
function nextButtonOff(){
	$('#bottomFormButton').css('display','none');
}

//selecting a brochure
function brochureToggleOn(n){					
	$(n).find('#brochureOn').css('display','block');
	$(n).find('#selectionDropdowns').css('display','block');
	$(n).find('#brochureOff').css('display','none');
	selectionResults();
}

//deselecting a brochure
function brochureToggleOff(n){				
	$(n).find('#brochureOn').css('display','none');
	$(n).find('#selectionDropdowns').css('display','none');
	$(n).find('#brochureOff').css('display','block');
	selectionResults();
}

//build the display of selected brochures on the step 3 (confirmation) tab
function buildStep3ListDisplay(){
		
	//hiding the empty divs before populating them
	$("#step3_mail_selection_list").css("display","none");
	$("#step3_download_selection_list").css("display","none");
		
	//Display Name Hash Array (name as it appears in selection rows)
	var displayName = new Array();
	displayName['vwfam'] = 'Volkswagen Family';
	displayName['routan'] = 'Routan';
	displayName['cpl'] = 'Certified Pre-Loved';
	displayName['newbeetle'] = 'New Beetle/New Beetle Convertible';
	displayName['passat'] = 'Passat<br />Passat Wagon';
	displayName['jetta'] = 'Jetta/Jetta TDI<br />Jetta SportWagen';
	displayName['gti'] = 'GTI';
	displayName['touareg'] = 'Touareg';
	displayName['rabbit'] = 'Rabbit';
	displayName['tiguan'] = 'Tiguan';
	displayName['eos'] = 'Eos';
	displayName['golf'] = 'Golf';
	displayName['cc'] = 'CC';
	displayName['english'] = 'English';
	displayName['spanish'] = 'Spanish';
	
	var brochureArray = returnResults();
	var numMail = 0;
	var numDownload = 0;
	var isMail = false;
	var isDownload = false;
	
	//clearing out divs that will be populated
	$("#step3_mail_selection_list").find("#step3_mail_selection_row").html('');
	$("#step3_download_selection_list").find("#step3_download_selection_row").html('');
		
	for(var i=0; i<brochureArray.length; i++){
		
		var addData = '';
		var addToDiv = '';
		var vehicle = jQuery.trim(brochureArray[i][0].toLowerCase());
		var language = jQuery.trim(brochureArray[i][1].toLowerCase());
		var delivery = jQuery.trim(brochureArray[i][2].toLowerCase());
		
		if(delivery == 'mail'){
			isMail = true;
		}else{
			isDownload = true;
		}	
		
		if(delivery == 'mail'){
			
			numMail++;
			
			addToDiv = "#step3_mail_selection_row";
			addData = '<div class="step3SelectionItem"><img src="/global/images/requestabrochure/step3/brochures/';
			addData += vehicle + '.jpg"/><p class="xSmallType">';
			addData += displayName[vehicle] + '<br/>(' + displayName[language] + ')</p></div>';

		}else{
			
			numDownload++;
			
			addToDiv = "#step3_download_selection_row";
			addData = '<div class="step3SelectionItem"><img src="/global/images/requestabrochure/step3/brochures/';
			addData += vehicle+'.jpg"/><p class="xSmallType">';
			addData += displayName[vehicle]+'<br/>('+displayName[language]+')</p></div>';			
		
		}
		
		//adding the brochure selection to the appropriate div
		$(addToDiv).append(addData);
		
		if(i == brochureArray.length - 1){
			if(isMail){
				//brochures were selected to be mailed, so the 'mail' list is being shown
				$("#step3_mail_selection_list").css("display","block");
			}
			if(isDownload){
				//brochures were selected to be downloaded, so the 'download' list is being shown
				 $("#step3_download_selection_list").css("display","block");
			}
		}
		
	}
	
	
	var onlyOneRow = true;
	if(isMail && isDownload || !isMail && numDownload > 5){
		onlyOneRow = false;
	}
	
	//clear out dividing line so doesn't factor into height measurement
	$('.step3RightColumnInner').find('div:first').find('p').html('');
	//determine appropriate height
	var adjustToHeight = $('#brochure_step_3').height() - 120;
	var compAdjustHeight = 250; //shortest height the line should be
	if(compAdjustHeight > adjustToHeight || onlyOneRow){
		adjustToHeight = compAdjustHeight;
	}
	//create div and place in DOM
	var imgSwap = '<img src="/global/images/myvw/sc_hr.jpg" style="width:1px; height:' + adjustToHeight + 'px;" />';
	$('.step3RightColumnInner').find('div:first').find('p').html(imgSwap);
	
	
}


//showing/hiding/populating buttons, text, etc. based on whether or not the user opted for/against being contacted by a dealer
function buildStep3GUI(id){
	
	//hiding all elements first in case page shows up before information is correct
	$('#brochure_step_3').find('#selected_dealer_name').css('display','none');
	$('#brochure_step_3').find('#selected_dealer_contact_details').css('display','none');
	$('#brochure_step_3').find('#selected_download_message').css('display','none');
	$('#brochure_step_3').find('#step3FormButton').find('.vso_button').css('display','none');
	$('#brochure_step_3').find('#step3FormButton').find('.gaq_button').css('display','none');
	$('#brochure_step_3').find('#step3FormButton').find('.lds_button').css('display','none');

	//'I'd like a dealer to contact me' check box
	var checked = $('#brochure_step_2').find('#dealer_contact_preference').find('input[@name="dealer_contact"]:checked').val();
	
	//The user has opted for a dealer to contact them
	if(checked != undefined){
			
		//dealerDataArray created when dealer list xml retrieved, uses dealer id as hash key
		var name = dealerDataArray[id]['name'];
		var url = dealerDataArray[id]['url'];
		var address = dealerDataArray[id]['address'];
		var city = dealerDataArray[id]['city'];
		var state = dealerDataArray[id]['state'];
		var zip = dealerDataArray[id]['zip'];
		var phone = dealerDataArray[id]['phone'];
		var infoStringHTML = name.toUpperCase() + '<br/>' + address + '<br/>' + city + ',&nbsp;' + state + '&nbsp;' + zip + '<br/>' + phone;
		
		//dynamic population dealer information
		$('#brochure_step_3').find('#selected_dealer_name').html(name + '&nbsp;will contact you soon.');
		$('#brochure_step_3').find('#selected_dealer_contact_details').html(infoStringHTML );
		//Changed for tracking campaign parameters
		$('#brochure_step_3').find('#step3FormButton a.lds_button').attr('href',"#");
		$('#brochure_step_3').find('#step3FormButton a.lds_button').attr('target',"_self");
		$('#brochure_step_3').find('#step3FormButton a.lds_button').click(function(){
		cb.appendCampaign(url);
		});
		
		
		
		//toggling divs on/off depending on contact by dealer selection
		$('#brochure_step_3').find('#selected_dealer_name').css('display','block');
		$('#brochure_step_3').find('#selected_dealer_contact_details').css('display','block');
		$('#brochure_step_3').find('#selected_download_message').css('display','none');
		$('#brochure_step_3').find('#step3FormButton').find('.vso_button').css('display','block');
		$('#brochure_step_3').find('#step3FormButton').find('.gaq_button').css('display','none');
		$('#brochure_step_3').find('#step3FormButton').find('.lds_button').css('display','block');
	
	}else{
		
		//toggling divs on/off depending on contact by dealer selection
		$('#brochure_step_3').find('#selected_dealer_name').css('display','none');
		$('#brochure_step_3').find('#selected_dealer_contact_details').css('display','none');
		$('#brochure_step_3').find('#selected_download_message').css('display','block');
		$('#brochure_step_3').find('#step3FormButton').find('.vso_button').css('display','block');
		$('#brochure_step_3').find('#step3FormButton').find('.gaq_button').css('display','block');
		$('#brochure_step_3').find('#step3FormButton').find('.lds_button').css('display','none');
	
	}
		
	//initialize the brochure download after two seconds
	setTimeout('downloadBrochures()', 2);
	//$('#step3_download_selection_list a:first').css('background-color', 'red').trigger('click');
	//downloadBrochures();
}

//download the selected brochures
function downloadBrochures(){
	
	//there are brochures to download
	if(numSelected > numMailSelected){
		
		var brochureArray = returnResults();
		
		//Display Name Hash Array (name as it appears in selection rows)//
		var brochureNameHash = new Array();
		brochureNameHash['vwfam'] = 'family';
		brochureNameHash['routan'] = 'routan';
		brochureNameHash['cpl'] = 'cpl';
		brochureNameHash['newbeetle'] = 'beetle';
		brochureNameHash['passat'] = 'passat';
		brochureNameHash['jetta'] = 'jetta';
		brochureNameHash['gti'] = 'gti';
		brochureNameHash['touareg'] = 'touareg';
		brochureNameHash['rabbit'] = 'rabbit';
		brochureNameHash['tiguan'] = 'tiguan';
		brochureNameHash['eos'] = 'eos';
		brochureNameHash['golf'] = 'golf';
		brochureNameHash['cc'] = 'cc';
		/////////////////////////////////////////////////////////
		
		var requestedDownloadBrochures = new Array();
		var downloadRequest = '';
		
		for(var i=0; i<brochureArray.length; i++){
			
			var vehicle = jQuery.trim(brochureArray[i][0].toLowerCase());
			var language = jQuery.trim(brochureArray[i][1].toLowerCase());
			var delivery = jQuery.trim(brochureArray[i][2].toLowerCase());
			
			var brochureItemRequest = "";
			
			if(delivery == 'download'){
				
				brochureItemRequest = brochureNameHash[vehicle];
				
				if(language == 'spanish'){
					brochureItemRequest += "_es";
				}
				
				requestedDownloadBrochures.push(brochureItemRequest);
				
			}
			
			if(i == brochureArray.length - 1){
				
				downloadRequest = String(requestedDownloadBrochures);
				
		
				if(requestedDownloadBrochures.length > 1){
				
					var url="/global/brochure/makezip.php?files=" + downloadRequest + "&ran=" +  Math.floor(Math.random()*11000);
					
					//  try this with an iFrame
					$('<iframe src="' + url + '" name="filedownload" id="filedownload" style="position:absolute; top:0; left:-2000px;"	/>').appendTo('body');
					
					
					//window.open(url,'Download');
					
				
				
				//only 1 brochure requested for download, so no need to call the php script
				}else if(requestedDownloadBrochures.length == 1){
					var url="/global/brochure/vw_brochures/" + downloadRequest + ".pdf";
					window.open(url,'Download');  
				
				}
				
			}
			
		}
	
	}
	
}

//clear omniture variables 
function clearOmnitureVars() {
	s.pageName="";
	s.server=vwServerName;
	s.domain=vwDomain;
	s.channel="" ;
	s.nameplate="" ;
	s.modelyear=vwModelYear;
	s.microsite="";
	s.events="";
	for(var i=0;i<=50;i++){ 
      	 s["prop"+i]='';
      	 s["eVar"+i]='';
   	 }
}

//omniture tracking called for step 1 (after user hits 'next'), step 2 (after user hits 'submit' and the submission is successful)  and step 3 upon landing on step 3 'confirmation' tab
function omnitureTrackStep(stepNum){
		
	//clear out Omniture vars first
	if(stepNum == '1'){
		jQuery.ajax({
			url: "/global/js/omniture/s_code.js",
			dataType: "script",
			async: false,
			success: function(js){
				clearOmnitureVars();
			}
		});
	}else{
		clearOmnitureVars();
	}
	
	//anything outside of array is undefined or ''
	var modelNameHash = new Array();
	modelNameHash['cc'] = 'cc';
	modelNameHash['eos'] = 'eos';
	modelNameHash['gli'] = 'gli';
	modelNameHash['gti'] = 'gti';
	modelNameHash['jetta'] = 'jetta';
	modelNameHash['newbeetle'] = 'newbeetle';
	modelNameHash['newbeetlecon'] = 'newbeetlecon';
	modelNameHash['passat'] = 'passat';
	modelNameHash['passatwagon'] = 'passatwagon';
	modelNameHash['rabbit'] = 'rabbit';
	modelNameHash['golf'] = 'golf';
	modelNameHash['routan'] = 'routan';
	modelNameHash['touareg'] = 'touareg';
	modelNameHash['tiguan'] = 'tiguan';
	modelNameHash['jettasportwagen'] = 'jettasportwagen';
	
	var location = window.location.href.split("/");
	var model = String(modelNameHash[location[3]]);
	//var model = String(location[2]);
	var mailChosen = false;
	var downloadChosen = false;
	if(numMailSelected > 0){
		mailChosen = true;
	}
	if(numSelected > numMailSelected){
		downloadChosen = true;
	}
		
	//global props for all rab omniture tags
	var languageID = $("#languageId").val();
	s.prop1= languageID;
	s.prop2='www.vw.com';
	s.prop7="2009";
	s.eVar7="2009";
	s.eVar5='www.vw.com';
	s.prop16='www.vw.com';
	s.prop6 = model;
	s.eVar6 = model;
	
	switch (stepNum) {

		case "1":
			
			//alert('track step1');
			
			//Tag for Step 1 after user hits 'Next'
			var vehicleNames = new Array(); //used to translate brochure names to names accepted by omniture tracking
			vehicleNames['jetta'] = 'jetta';
			vehicleNames['gti'] = 'gti';
			vehicleNames['eos'] = 'eos';
			vehicleNames['touareg'] = 'touareg';
			vehicleNames['tiguan'] = 'tiguan';
			vehicleNames['passat'] = 'passat';
			vehicleNames['newbeetle'] = 'newbeetle';
			vehicleNames['routan'] = 'routan';
			vehicleNames['cpl'] = 'cpl';
			vehicleNames['rabbit'] = 'rabbit';
			vehicleNames['golf'] = 'golf';
			vehicleNames['cc'] = 'cc';
			vehicleNames['vwfam'] = 'family';
						
			var brochures = returnResults();
			var selectedStr = '';

			for(var i = 0; i<brochures.length; i++){
				var vehicle = jQuery.trim(brochures[i][0].toLowerCase());
				var language = jQuery.trim(brochures[i][1].toLowerCase());
				var delivery = jQuery.trim(brochures[i][2].toLowerCase());
				selectedStr += vehicleNames[vehicle] + ' | ';
			}
			
			s.pageName = "models:brochure:step 1";	
			s.channel = "brochure";
			s.prop17 = selectedStr;
			s.eVar17 = selectedStr;
			s.events = "event11";	
			
		break;
		
		
		case "2":
			
			//alert('track step2');
			s.pageName = "models:brochure:step 2";
		
		break;
		
		case "3":
			
			s.pageName = "models:brochures:thank you:step 3";

			if(mailChosen && downloadChosen){ //brochures downloaded and mailed
				//alert('track step3 both');
				s.eVar2 = "Both";
				s.prop19 = "Both";
				s.events = "event4, event16";
				s.products = model;
			}else if (!mailChosen && downloadChosen){ //brochures downloaded only
				//alert('track step3 download only');
				s.eVar2 = "Download Brochure";
				s.prop19 = "Download Brochure";
				s.events = "event4, event16";
			}else{ //brochures mail only
				//alert('track step3 mail only');
				s.eVar2 = "Mail Brochure";
				s.prop19 = "Mail Brochure";
				s.events = "event4";
				s.products = model;
			}
			
		break;
	
	}

	void(s.t());

}



function floodlightTrackStep(stepNum){
		
	var location = window.location.href.split("/");
	var axel = Math.random()+"";
	var a = axel * 10000000000000;
	
	/* SAMPLE FLOODLIGHT TAG
	<IFRAME SRC="http://fls.doubleclick.net/activityi;src=1033942;type=cc;cat=ccbro3fl;ord=1;num=1?" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>
	*/
		
	switch (stepNum) {
	
		case "3":
			
			var flpreStr = 'http://fls.doubleclick.net/activityi;src=1033942;';
			
			var typeNameHash = new Array();
			typeNameHash['cc'] = 'cc';
			typeNameHash['eos'] = 'eos';
			typeNameHash['gli'] = 'gli';
			typeNameHash['gti'] = 'gti';
			typeNameHash['jetta'] = 'jetta';
			typeNameHash['jettasportwagen'] = 'jettawag';
			typeNameHash['newbeetle'] = 'newbeet';
			typeNameHash['newbeetlecon'] = 'nbc';
			typeNameHash['passat'] = 'passat';
			typeNameHash['passatwagon'] = 'passwagn';
			typeNameHash['rabbit'] = 'rabbit';
			typeNameHash['routan'] = 'routan';
			typeNameHash['tiguan'] = 'tiguan';
			typeNameHash['touareg'] = 'toureg';
			typeNameHash['golf'] = 'rabbit';
			typeNameHash['brochure'] = 'generic';
			
			var catNameHash = new Array();
			catNameHash['cc'] = 'ccbro3fl';
			catNameHash['eos'] = 'eosbr3fl';
			catNameHash['gli'] = 'glibr3fl';
			catNameHash['gti'] = 'gtibr3fl';
			catNameHash['jetta'] = 'jetbr3fl';
			catNameHash['jettasportwagen'] = 'jswbr3fl';
			catNameHash['newbeetle'] = 'nbbro3fl';
			catNameHash['newbeetlecon'] = 'nbcbr3fl';
			catNameHash['passat'] = 'pasbr3fl';
			catNameHash['passatwagon'] = 'pwgbr3fl';
			catNameHash['rabbit'] = 'rabbr3fl';
			catNameHash['routan'] = 'roubr3fl';
			catNameHash['tiguan'] = 'tigbr3fl';
			catNameHash['touareg'] = 'toubr3fl';
			catNameHash['golf'] = 'rabbr3fl';
			catNameHash['brochure'] = 'broc3fl';
			
			var type = 'type=' + String(typeNameHash[location[3]]);
			var cat =  ';cat=' + String(catNameHash[location[3]]);
			
			var flpostStr = ';ord=1;num='+ a + '?';
			
			var flTagString = flpreStr + type + cat + flpostStr;

			var flightTag = '<IFRAME SRC="' + flTagString + '" WIDTH=1 HEIGHT=1 FRAMEBORDER=0></IFRAME>';
			$('body').append(flightTag);
				
		break;
	
	}

}
						
//calls findDealer function
jQuery.checkZipCode = function() {
	e.preventDefault();
	$.findDealer($("#zipCodeBrochure").val(), "#dealer_list");
	//check for find dealer spotlight action
	if(typeof spotlightFindDealer == 'function') {
		spotlightFindDealer();
	}
};

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

/************************* DOCUMENT ACTIONS ******************************/
$(document).ready (function() {
	
	// no dealer session version of RAB
	/*$(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"; ///where does this go?
			}else if(dealersession.isDealerSession == false){
				
			}
		}
	}catch(e){
	}*/
	
	
	/* display fixes */	
	//form fields height hack for safari
	if($.browser.safari){
		$("#stateProvince").css("height","20px");
		$("#brochure_step_2").find("input").css("padding","0");
		$("#brochure_step_2").find("input").css("padding-left","1px");
	}
	
	//ie 6 universal nav fix
	if ($.browser.msie && $.browser.version < 7.0){
		//alert('ie6 widget fix');	
		var ie6Fix = '<iframe id="widgetShim" scrolling="no" frameborder="0" style="display: none;"></iframe>';
		$(ie6Fix).insertBefore($('#mainContent'));	
		$('#hybridFooter').removeClass('copyrightAdjust');
	}
	/* end display fixes */
	
	
	
	//if already checked show dealer listing
	if($("#dealer_contact_preference").find("input[type=checkbox]").attr('checked')){
		$.findDealer($("#zipCodeBrochure").val(), "#dealer_list");
		if(typeof spotlightFindDealer == 'function') {
			spotlightFindDealer();
		}
	}
	 
	//msie checkbox behavior workaround, by default doesn't show change in checkbox until blur
	if ($.browser.msie){
		$("input[type='checkbox']").unbind( 'click' );
		$("input[type='checkbox']").bind( 'click', function() {
			$(this).trigger( 'change' );
		});
		
		$("#dealer_contact_preference").find("input[type=checkbox]").change(function (e) {
		$(this).blur();//ie check box fix
		var checkedString = String($(this).attr('checked'));
		//only show dealers if the change made to the checkbox is to check it
		if(checkedString == 'true'){
			e.preventDefault();
			$.findDealer($("#zipCodeBrochure").val(), "#dealer_list");
			//check for find dealer spotlight action
			if(typeof spotlightFindDealer == 'function') {
				spotlightFindDealer();
			}
		}
		});
		
	}else{
	
		$("#dealer_contact_preference").find("input[type=checkbox]").change(function (e) {
			var checkedString = String($(this).attr('checked'));
			//only show dealers if the change made to the checkbox is to check it
			if(checkedString == 'true'){
				e.preventDefault();
				$.findDealer($("#zipCodeBrochure").val(), "#dealer_list");
				//check for find dealer spotlight action
				if(typeof spotlightFindDealer == 'function') {
					spotlightFindDealer();
				}
			}
		});
		
	}
	
	
	$("#zipCodeBrochure").keypress(function(e){
		//only will work if dealer contact checkbox checked and zip code is entered
    	if(e.keyCode == 13 && $("#dealer_contact_preference").find("input[type=checkbox]").attr('checked')) {
			//zipcode is too short, attempt to validate (result will be error)
			if($("#zipCodeBrochure").val().length < 5){
				setTimeout('$.checkZipCode()', 10);
				e.preventDefault();
			}else {
				e.preventDefault();
				$.findDealer($("#zipCodeBrochure").val(), "#dealer_list");
				//check for find dealer spotlight action
				if(typeof spotlightFindDealer == 'function') {
					spotlightFindDealer();
				}
			}
    		return false;
    	}
	});
	
	
	//pre-populate form based on cookie vars
	initCookie();
	
	/* restrict input to certain form fields */
	$("#firstName").alpha({allow:"' -"});
	$("#lastName").alpha({allow:"' -"});
	$("#address1").alphanumeric({allow:".:-', "});
	//$("#address2").alphanumeric({allow:".`-~ "});
	$("#city").alpha({allow:".:-', "});
	$("#zipCodeBrochure").numeric();
	$("#email1").alphanumeric({allow:".@-_`!#$%*+-~"});
	$("#phone").numeric({allow:".()- "});
	
	/* validate and submit data */
	$("#request_a_brochure").submit(function(e) {
		
		e.preventDefault();
		
		$.showLoader();
					
		if($("#request_a_brochure").validate("none",getFailureColor())) {
			
			
			
			//If the 'dealer contact me' checkbox is checked and the dealer list has not been populated (no search performed) or a search has been performed but the zip code that was searched is different from what is now in the form
			var dealerListedStr = $.trim($("#dealer_list").find(".dealer").html());
			if($("#dealer_contact_preference").find("input[type=checkbox]").attr('checked') && (dealerListedStr == "" ||  $("#zipCodeBrochure").val() != searchedZip)){
				$.findDealer($("#zipCodeBrochure").val(), "#dealer_list");
				//check for find dealer spotlight action
				if(typeof spotlightFindDealer == 'function') {
					spotlightFindDealer();
				}
				return false;
			}
			//
			
			omnitureTrackStep('2'); //tracking step 2, on successfully submitting
			
			//populate user info cookie, if values that this form does not have are populated, will not overwrite those
			var userDataObj;
			var userInfo = buildVwFormsUserInfoCookie();
			if(userInfo != undefined){
				var cookieArray = userInfo.split("~");
				userDataObj = $("#firstName").val()+"~"+$("#lastName").val()+"~"+cookieArray[2]+"~"+$("#phone").val()+"~"+$("#email1").val()+"~"+$("#email1").val()+"~"+$("#zipCodeBrochure").val()+"~"+cookieArray[7]+"~"+cookieArray[8]+"~"+cookieArray[9]+"~"+cookieArray[10]+"~"+cookieArray[11]+"~"+cookieArray[12]+"~"+cookieArray[13]+"~"+"rab"+"~"+$("#address1").val()+"~"+$("#city").val()+"~"+$("#stateProvince").val();
			}else{
				userDataObj = $("#firstName").val()+"~"+$("#lastName").val()+"~"+""+"~"+$("#phone").val()+"~"+$("#email1").val()+"~"+$("#email1").val()+"~"+$("#zipCodeBrochure").val()+"~"+""+"~"+""+"~"+""+"~"+""+"~"+""+"~"+""+"~"+""+"~"+"rab"+"~"+$("#address1").val()+"~"+$("#city").val()+"~"+$("#stateProvince").val();
			}
			
			buildVwFormsUserInfoCookie(userDataObj);
			
			//dealerID passed on to requestabrochure_assembleXML and buildStep3GUI and so that dealer data can be accessed from the dealerDataArray
			var dealerID = $('#dealer_list').find('input[@name="dealerId"]:checked').val();
			if(dealerID == undefined){
				dealerID = '';
			}
						
			//data passed validation submitting lead 
			$("#request_a_brochure").requestabrochure_assembleXML("#request_a_brochure", dealerID);
			
			/*  all called after lead successfully processed */
			//show third tab and build GUI
			//buildStep3GUI(dealerID);
			//populate selected brochure lists
			//buildStep3ListDisplay();
			//show the third tab
			//showHide('#brochure_step_3');
			
			$.hideLoader();
			
			//check for submit spotlight action
			if(typeof spotlightSubmit == 'function') {
				spotlightSubmit();
			}
			
      		return true;
			
      	}
		
		//data did not validate
		$.hideLoader();
      	return false;
		
    });
	
});