//Functions to load, erase and create cookies
function loadStartPageCookies(InputsCookies){
	readAllSBToolsCookies();
	
	if ((make == null) || (make == "")){
		loadVehicleSelectedCookies(InputsCookies);
	}
}

function eraseCookiesVL(){
	eraseSBTCookie("sbtZipCode");
	eraseSBTCookie("sbtZipProximity");
	eraseSBTCookie("sbtCityName");
	eraseSBTCookie("sbtCityState");
	eraseSBTCookie("sbtCityProximity");
	eraseSBTCookie("sbtSearchDealerName");
	eraseSBTCookie("sbtCityNameDealer");
	eraseSBTCookie("sbtCityStateDealer");
	if (multiBrandEnabled)eraseSBTCookie("mySelectedBrand");
}

function createCookiesVLZipCode(){
	eraseCookiesVL();
	
	createSBTCookie("sbtZipCode",document.SearchByZipCode.postalCode.value,30);
	createSBTCookie("sbtZipProximity",document.SearchByZipCode.proximity.options[document.SearchByZipCode.proximity.selectedIndex].value,30);
	if (multiBrandEnabled)createSBTCookie("mySelectedBrand",document.SearchByZipCode.SLBrand.value,30);
}

function createCookiesVLCityState(){
	eraseCookiesVL();
		
	createSBTCookie("sbtCityName",document.SearchBycityState.cityState.value,30);
	createSBTCookie("sbtCityState",document.SearchBycityState.statesSCity.options[document.SearchBycityState.statesSCity.selectedIndex].value,30);
	createSBTCookie("sbtCityProximity",document.SearchBycityState.mileSelectState.options[document.SearchBycityState.mileSelectState.selectedIndex].value,30);
	if (multiBrandEnabled)createSBTCookie("mySelectedBrand",document.SearchBycityState.SLBrand.value,30);
}

function createCookiesVLDealer(){
	eraseCookiesVL();
		
	createSBTCookie("sbtSearchDealerName",document.SearchByDealer.SearchByVendorVendorName.value,30);
	createSBTCookie("sbtCityNameDealer",document.SearchByDealer.SearchByVendorCityName.value,30);
	createSBTCookie("sbtCityStateDealer",document.SearchByDealer.SearchByVendorStateName.options[document.SearchByDealer.SearchByVendorStateName.selectedIndex].value,30);
	if (multiBrandEnabled)createSBTCookie("mySelectedBrand",document.SearchByDealer.SLBrand.value,30);
}

//Function to load, erase and create cookie for the current year
function loadCurrentYearCookie(){
	readAllSBToolsCookies();

	if (sbToolsCookies['myselectedyear'] != ""){
		initialYear = sbToolsCookies['myselectedyear'];
		}else{
			initialYear = "2008";
	}
}

function eraseCookiesCY(){
	eraseSBTCookie("mySelectedYear");
}

function createCookieCurrentYear(){
	eraseCookiesCY();

	createSBTCookie("mySelectedYear",document.imageSelected.yearSelect.options[document.imageSelected.yearSelect.selectedIndex].value,30);
}

//Function to load, erase and create cookie for the seleted vehicle
function loadVehicleSelectedCookies(InputsCookies){
	readAllSBToolsCookies();
	var inputsHtml = "";
	var formHtml = "";
	var searchCookies = false;
	var dealerSearchCookies = false;
	
	if (!isEmptyString(sbToolsCookies['sbtzipcode']) && !isEmptyString(sbToolsCookies['sbtzipproximity'])) {
		updateSBTCookie('sbtSearchType', '0', 30);
		inputsHtml += "<input type='hidden' name='postalCode' value='"+sbToolsCookies['sbtzipcode']+"'>";
		inputsHtml += "<input type='hidden' name='proximity' value='"+sbToolsCookies['sbtzipproximity']+"'>";
		inputsHtml += "<input type='hidden' name='SLBrand' value='"+sbToolsCookies['myselectedbrand']+"'>";
		searchCookies = true;
	}
	if ((!isEmptyString(sbToolsCookies['sbtcityname']) && !isEmptyString(sbToolsCookies['sbtcitystate']) && !isEmptyString(sbToolsCookies['sbtcityproximity']))) {
		updateSBTCookie('sbtSearchType', '1', 30);
		inputsHtml += "<input type='hidden' name='cityState' value='"+sbToolsCookies['sbtcityname']+"'>";
		inputsHtml += "<input type='hidden' name='selectState' value='"+sbToolsCookies['sbtcitystate']+"'>";
		inputsHtml += "<input type='hidden' name='mileSelectState' value='"+sbToolsCookies['sbtcityproximity']+"'>";
		inputsHtml += "<input type='hidden' name='searchCity' value='"+sbToolsCookies['sbtcityname']+"'>";
		inputsHtml += "<input type='hidden' name='searchState' value='"+sbToolsCookies['sbtcitystate']+"'>";
		inputsHtml += "<input type='hidden' name='proximity' value='"+sbToolsCookies['sbtcityproximity']+"'>";
		inputsHtml += "<input type='hidden' name='SLBrand' value='"+sbToolsCookies['myselectedbrand']+"'>";
		searchCookies = true;
	}
	if ((!isEmptyString(sbToolsCookies['sbtsearchdealername']) && !isEmptyString(sbToolsCookies['sbtcitynamedealer']) && !isEmptyString(sbToolsCookies['sbtcitystatedealer']))) {
		updateSBTCookie('sbtSearchType', '4', 30);
		inputsHtml += "<input type='hidden' name='SearchByVendorVendorName' value='"+sbToolsCookies['sbtsearchdealername']+"'>";
		inputsHtml += "<input type='hidden' name='SearchByVendorCityName' value='"+sbToolsCookies['sbtcitynamedealer']+"'>";
		inputsHtml += "<input type='hidden' name='SearchByVendorStateName' value='"+sbToolsCookies['sbtcitystatedealer']+"'>";
		inputsHtml += "<input type='hidden' name='SLBrand' value='"+sbToolsCookies['myselectedbrand']+"'>";
		dealerSearchCookies = true;
	}
	
	if (searchCookies || dealerSearchCookies) {
		inputsHtml += "<input type='hidden' name='maximumCount' value='30'>";
		if (dealerSearchCookies) {
			if (dealerCookiesPresent()) {
				inputsHtml += "<input type='hidden' name='vendorId' value='"+sbToolsCookies['sbtbac']+"'>";
				inputsHtml += "<input type='hidden' name='BAC' value='"+sbToolsCookies['sbtbac']+"'>";
				inputsHtml += "<input type='hidden' name='dealerName' value='"+sbToolsCookies['sbtdealername']+"'>";
				inputsHtml += "<input type='hidden' name='dealerDistance' value='"+sbToolsCookies['sbtdealerdistance']+"'>";
				inputsHtml += "<input type='hidden' name='gmbpManagerName' value='"+sbToolsCookies['sbtgmbpmanager']+"'>";
				inputsHtml += "<input type='hidden' name='dealerAddress' value='"+sbToolsCookies['sbtdealerstreet']+"'>";
				inputsHtml += "<input type='hidden' name='dealerCity' value='"+sbToolsCookies['sbtdealercity']+"'>";
				inputsHtml += "<input type='hidden' name='dealerState' value='"+sbToolsCookies['sbtdealerstate']+"'>";
				inputsHtml += "<input type='hidden' name='dealerZipCode' value='"+sbToolsCookies['sbtdealerpostalcode']+"'>";
				inputsHtml += "<input type='hidden' name='dealerPhoneNumber' value='"+sbToolsCookies['sbtdealerphone']+"'>";
				inputsHtml += "<input type='hidden' name='dealerFax' value='"+sbToolsCookies['sbtdealerfax']+"'>";
				inputsHtml += "<input type='hidden' name='dealerWebSite' value='"+sbToolsCookies['sbtdealerwebsite']+"'>";
				inputsHtml += "<input type='hidden' name='dealerMap' value='"+sbToolsCookies['sbtdealermap']+"'>";
				inputsHtml += "<input type='hidden' name='dealerDirections' value='"+sbToolsCookies['sbtdealerdirections']+"'>";
			}
		}
		
		if(carCookiesPresent()){
			<!-- /Modify for divisional implementation -->
			inputsHtml += "<input type='hidden' name='MMC' value='"+sbToolsCookies['myselectedtype']+"'>";
			inputsHtml += "<input type='hidden' name='modelDesignator' value='"+sbToolsCookies['sbtmodeldesignator']+"'>";
			inputsHtml += "<input type='hidden' name='searchCountry' value='"+sbToolsCookies['sbtsearchcountry']+"'>";
			inputsHtml += "<input type='hidden' name='searchType' value='"+sbToolsCookies['sbtsearchtype']+"'>";
			inputsHtml += "<input type='hidden' name='year' value='"+sbToolsCookies['myselectedyear']+"'>";
			inputsHtml += "<input type='hidden' name='make' value='"+sbToolsCookies['myselectedbrand']+"'>";
			inputsHtml += "<input type='hidden' name='makeDesc' value='"+sbToolsCookies['myselectedbrand']+"'>";
			inputsHtml += "<input type='hidden' name='brand' value='"+sbToolsCookies['sbtbrand']+"'>";
			inputsHtml += "<input type='hidden' name='modelCode' value='"+sbToolsCookies['sbtmodelcode']+"'>";
			inputsHtml += "<input type='hidden' name='subModelCode' value='"+sbToolsCookies['sbtsubmodelcode']+"'>";
			inputsHtml += "<input type='hidden' name='cabType' value='"+sbToolsCookies['sbtcabtype']+"'>";
			inputsHtml += "<input type='hidden' name='driveType' value='"+sbToolsCookies['sbtdrivetype']+"'>";
			inputsHtml += "<input type='hidden' name='model' value='"+sbToolsCookies['sbtmodel']+"'>";
			inputsHtml += "<input type='hidden' name='price' value='"+formatPrice(sbToolsCookies['sbtprice'])+"'>";
			inputsHtml += "<input type='hidden' name='image' value='"+sbToolsCookies['sbtimage']+"'>";
			inputsHtml += "<input type='hidden' name='proximityUnitOfMeasure' value='miles'>";
			inputsHtml += "<input type='hidden' name='isbp' value='true'>";
			<!---->
			if (trimCookiesPresent()) {	
				inputsHtml += "<input type='hidden' name='pvc' value='"+sbToolsCookies['myselectedstyle']+"'>";
				inputsHtml += "<input type='hidden' name='msrPrice' value='"+formatPrice(sbToolsCookies['sbtmsrprice'])+"'>";
				inputsHtml += "<input type='hidden' name='trimName' value='"+sbToolsCookies['sbttrimname']+"'>";
				if (dealerSearchCookies) {
					if (dealerCookiesPresent()) {
						formHtml = "<form id='VLCookiesForm' method='GET' name='VLCookiesForm' action='/vehiclelocator/locateVehicle.do'>";
					}
					else {
						formHtml = "<form id='VLCookiesForm' method='GET' name='VLCookiesForm' action='/locatedealer/cntrl'>";
					}
				}
				else {
					formHtml = "<form id='VLCookiesForm' method='GET' name='VLCookiesForm' action='/vehiclelocator/locateVehicle.do'>";
				}				
			}
			else {
				if (dealerSearchCookies) {
					if (dealerCookiesPresent()) {
						formHtml = "<form id='VLCookiesForm' method='GET' name='VLCookiesForm' action='vl_SelectTrim_result.jsp'>";
					}
					else {
						formHtml = "<form id='VLCookiesForm' method='GET' name='VLCookiesForm' action='/locatedealer/cntrl'>";
					}
				}
				else {
					formHtml = "<form id='VLCookiesForm' method='GET' name='VLCookiesForm' action='vl_SelectTrim_result.jsp'>";
				}
			}		
		}
		else{			
			if (dealerSearchCookies) {
				if (dealerCookiesPresent()) {
					formHtml = "<form id='VLCookiesForm' method='GET' name='VLCookiesForm' action='vl_SelectV_result.jsp'>";
				}
				else {
					<!-- /Modify for divisional implementation -->
					inputsHtml += "<input type='hidden' name='desiredCount' value='30'>";
					inputsHtml += "<input type='hidden' name='WEBSITEID' value='SB'>"; 	// Modify for every brand
					inputsHtml += "<input type='hidden' name='BRANDID' value='SB'>"; 	// Modify for every brand
					inputsHtml += "<input type='hidden' name='FILEPATH' value='/saab/'>"; 	// Modify for every brand
					inputsHtml += "<input type='hidden' name='BPLINK' value='Y'>";
					inputsHtml += "<input type='hidden' name='SearchType' value='ByVendor'>";
					inputsHtml += "<input type='hidden' name='request' value='dealerlocate'>";
					inputsHtml += "<input type='hidden' name='mapLink' value='true'>";
					inputsHtml += "<input type='hidden' name='drivingLink' value='true'>";
					inputsHtml += "<input type='hidden' name='urlForward' value='true'>";
					inputsHtml += "<input type='hidden' name='toolName' value='VL'>";
					<!---->
					formHtml = "<form id='VLCookiesForm' method='GET' name='VLCookiesForm' action='/locatedealer/cntrl'>";
				}
			}
			else {
				formHtml = "<form id='VLCookiesForm' method='GET' name='VLCookiesForm' action='vl_SelectV_result.jsp'>";
			}
		}	
		
		document.getElementById(InputsCookies).innerHTML = formHtml + inputsHtml + "</form>";
		document.getElementById('VLCookiesForm').submit();
	}
}

function createVehicleSelectedCookies(MMC,searchCountry,searchType,make,modelCode,subModelCode,cabType,driveType,model,price,image){
	
	updateSBTCookie("mySelectedType",MMC,30);
	updateSBTCookie("sbtModelDesignator",MMC,30);
	updateSBTCookie("sbtSearchCountry",searchCountry,30);
	updateSBTCookie("sbtSearchType",searchType,30);
	updateSBTCookie("mySelectedBrand",make,30);
	updateSBTCookie("sbtBrand",brandId,30);
	updateSBTCookie("sbtModelCode",modelCode,30);
	updateSBTCookie("sbtSubModelCode",subModelCode,30);
	updateSBTCookie("sbtCabType",cabType,30);
	updateSBTCookie("sbtDriveType",driveType,30);
	updateSBTCookie("sbtModel",model,30);
	updateSBTCookie("sbtPrice",price,30);
	updateSBTCookie("sbtImage",image,30);
	updateSBTCookie("mySelectedModel",modelCode+","+subModelCode,30);
	eraseTrimCookies();
}

// Manages the Omniture Clicks
function clickTrackVL(oArgs){
	for (var q in oArgs){
		if (window[q] != undefined) {
			window[q] = oArgs[q];
		}
	}
	// s_gs(s_account);
}

//Omniture when customer clicks on locate a vehicle
function manageClickVLIndex(brandId){
	clickTrackVL({s_pageName: brandId + ' | DIVISIONAL | LOCATE VEHICLE | INDEX', s_prop1: 'DIVISIONAL', s_prop2: 'LOCATE VEHICLE', s_prop3: 'DIVISIONAL | LOCATE VEHICLE', s_prop11: 'INDEX', s_prop12: 'LOCATE VEHICLE | INDEX', s_prop13: 'DIVISIONAL | LOCATE VEHICLE | INDEX', s_prop25: brandId  });	
}

//Omniture when customer click on locate (zip, city/state or dealer)
function manageClickVL(brandId){
	clickTrackVL({s_pageName: brandId + ' | DIVISIONAL | LOCATE VEHICLE | DEALER LIST', s_prop1: 'DIVISIONAL', s_prop2: 'LOCATE VEHICLE', s_prop3: 'DIVISIONAL | LOCATE VEHICLE', s_prop11: 'DEALER LIST', s_prop12: 'LOCATE VEHICLE | DEALER LIST', s_prop13: 'DIVISIONAL | LOCATE VEHICLE | DEALER LIST', s_prop25: brandId  });	
}

//Omniture when customer select a vehicle which takes them to Select a Trim
function manageClickSelectVL(brandId, vehicle){
	clickTrackVL({s_pageName: brandId + ' | DIVISIONAL | LOCATE VEHICLE | SELECT TRIM', s_prop1: 'DIVISIONAL', s_prop2: 'LOCATE VEHICLE', s_prop3: 'DIVISIONAL | LOCATE VEHICLE', s_prop6: 'LOCATE VEHICLE | ' + vehicle + ' ', s_prop11: 'SELECT TRIM', s_prop12: 'LOCATE VEHICLE | SELECT TRIM', s_prop13: 'DIVISIONAL | LOCATE VEHICLE | SELECT TRIM', s_prop25: brandId  });	
}

//Omniture when customer select a trim package which takes them to the Inventory Results
function manageClickSelectTrim(brandId, vehicle, trim){
	clickTrackVL({s_pageName: brandId + ' | DIVISIONAL | LOCATE VEHICLE | INVENTORY RESULTS', s_prop1: 'DIVISIONAL', s_prop2: 'LOCATE VEHICLE', s_prop3: 'DIVISIONAL | LOCATE VEHICLE', s_prop6: 'LOCATE VEHICLE | ' + vehicle + ' ', s_prop7: 'LOCATE VEHICLE | ' + vehicle + ' | ' + trim + ' ', s_prop11: 'INVENTORY RESULTS', s_prop12: 'LOCATE VEHICLE | INVENTORY RESULTS', s_prop13: 'DIVISIONAL | LOCATE VEHICLE | INVENTORY RESULTS', s_prop25: brandId  });	
}

//Omniture when customer clicks View Window Sticker from the inventory results
function manageClickVWS(brandId, vehicle, trim){
	clickTrackVL({s_pageName: brandId + ' | DIVISIONAL | LOCATE VEHICLE | WINDOW STICKER', s_prop1: 'DIVISIONAL', s_prop2: 'LOCATE VEHICLE', s_prop3: 'DIVISIONAL | LOCATE VEHICLE', s_prop6: 'LOCATE VEHICLE | ' + vehicle + ' ', s_prop7: 'LOCATE VEHICLE | ' + vehicle + ' | ' + trim + ' ', s_prop11: 'WINDOW STICKER', s_prop12: 'LOCATE VEHICLE | WINDOW STICKER', s_prop13: 'DIVISIONAL | LOCATE VEHICLE | WINDOW STICKER', s_prop25: brandId  });	
}

//Omniture when customer clicks Request a Quote from the inventory results
function manageClickRAQInventoryR(brandId, vehicle, trim){
	clickTrackVL({s_pageName: brandId + ' | DIVISIONAL | RAQ | RAQ FORM', s_prop1: 'DIVISIONAL', s_prop2: 'RAQ', s_prop3: 'DIVISIONAL | RAQ', s_prop6: 'RAQ | ' + vehicle + ' ', s_prop7: 'RAQ | ' + vehicle + ' | ' + trim + ' ', s_prop11: 'RAQ FORM', s_prop12: 'RAQ | RAQ FORM', s_prop13: 'DIVISIONAL | RAQ | RAQ FORM', s_prop25: brandId  });
}

//Omniture when customer clicks Request a Quote from the inventory results
function manageClickSATDInventoryR(brandId, vehicle, trim){
	clickTrackVL({s_pageName: brandId + ' | DIVISIONAL | RAT | RAT FORM', s_prop1: 'DIVISIONAL', s_prop2: 'RAT', s_prop3: 'DIVISIONAL | RAT', s_prop6: 'RAT | ' + vehicle + ' ', s_prop7: 'RAT | ' + vehicle + ' | ' + trim + ' ', s_prop11: 'RAT FORM', s_prop12: 'RAT | RAT FORM', s_prop13: 'DIVISIONAL | RAT | RAT FORM', s_prop25: brandId  });	
}