<!--Modify for Divisional Content-->
var multiBrandEnabled = true;
<!---->

//Fuctions required to fill the states, mile and proximity combo boxes

function initLocateDealer() {
	LoadStateCityLocateDealer();
	LoadStateDealerLocator();
	LoadMileSelectionLocateDealer();
	LoadMileZipCode();
	if (multiBrandEnabled == true){LoadBrand();}

}

function initLocate() {
	LoadStateCityLocateDealer();
	LoadStateDealerLocator();
	LoadMileSelectionLocateDealer();
	LoadMileZipCode();
	LoadMileUpdate();
	if (multiBrandEnabled == true){LoadBrand();}
	LoadSelectedValues();
}
function initSearchAgain() {
	LoadStateCityLocateDealer();
	LoadStateDealerLocator();
	LoadMileSelectionLocateDealer();
	LoadMileZipCode();
	if (multiBrandEnabled == true){LoadBrand();}
	
}

//function required to display different Headers
function displayHeaders(){
	var tool = toolName.toUpperCase();
	if(tool == "VL"){
		document.getElementById('pageTitle').innerHTML += "<h2>Locate A Vehicle</h2>";
		document.getElementById('dealer').innerHTML += "<span>Step 1: Select Search Area</span>";		
		document.getElementById('vehicle').innerHTML += "<span>Step 2: Select A Vehicle</span>";
		document.getElementById('medium').innerHTML += "<span>Step 3: Inventory Results</span>";
		document.getElementById('dealer').className = "AreaOn";
		document.getElementById('vehicle').className = "VehicleOff";
		document.getElementById('medium').className = "InventoryOff";
		document.title = "Locate Vehicle";
		}else if (tool == "RAQ"){
			document.getElementById('pageTitle').innerHTML += "<h2>Request A Quote</h2>";
			document.getElementById('dealer').innerHTML += "<span>Step 1: Select A Dealer</span>";
			document.getElementById('vehicle').innerHTML += "<span>Step 2: Select A Vehicle</span>";
			document.getElementById('medium').innerHTML += "<span>Step 3: Request A Quote</span>";
			document.title = "Request A Quote";
			}else if(tool == "RAT"){
				document.getElementById('pageTitle').innerHTML += "<h2>Schedule A Test Drive</h2>";
				document.getElementById('dealer').innerHTML += "<span>Step 1: Select A Dealer</span>";
				document.getElementById('vehicle').innerHTML += "<span>Step 2: Select A Vehicle</span>";
				document.getElementById('medium').innerHTML += "<span>Step 3: Schedule A Test Drive</span>";
				document.getElementById('medium').className = "STDOff";
				document.title = "Schedule A Test Drive";
				}else if (tool == "DL"){
					document.getElementById('pageTitle').innerHTML += "<h2>Locate A Dealer</h2>";
					document.getElementById('compareMainTabs').style.display = 'none';
					document.getElementById('dealer').style.visibility = 'hidden';
					document.getElementById('dealer').style.display = 'none';
					document.getElementById('vehicle').style.visibility = 'hidden';
					document.getElementById('vehicle').style.display = 'none';
					document.getElementById('medium').style.visibility = 'hidden';
					document.getElementById('medium').style.display = 'none';
					document.title = "Locate Dealer";	
		}
}
//funtion required to display the selection drop down only for GMC
function showDealerSelection(){
	displayDealer = true;
	displayBrandSelection();
	hideMDCDLabel();
	document.getElementById('BCD').style.fontWeight = 'bold';
}

//funtion required to hide the selection drop down only for GMC
function hideDealerSelection(){
	displayDealer = false;
	displayLocateDGMC();
	hideBradD();
	hideBrandSelection();
	hideOptionalValues();
	mediumDutyDealers();
	document.getElementById('MDCM').style.fontWeight = 'bold';
	document.getElementById('BCD').style.fontWeight = 'normal';
}

//funtion required to display the brand selection drop down
function displayBrandSelection(){
	//display the brand drop down for cross-divisional sites
	if(((brandId == "FLEET")||(brandId == "GMFLEET")||(brandId == "GM")||(brandId == "CUV"))){
		displayBSelection();
		hideLocateDGMC();
		hideOptionalValues();
		hideMDCDLabel();
		//display the dealer selection only for Chevrolet and GMC
		}else if(brandId == "CHEVROLET"){
			hideBradD();
			hideLocateDGMC();
			hideOptionalValues();
			hideMDCDLabel();
			}else if(brandId == "GMC"){
				displayLocateDGMC();
				hideBradD();
				hideBrandSelection();
				hideOptionalValues();
				hideMDCDLabel();
					if(displayDealer == true){
						hideBradD();
						displayBSelection();
						document.getElementById('optionalValues').style.visibility = 'visible';
						document.getElementById('optionalValues').style.display = 'block';
						hideMDCDLabel();
					}
				}else{
					hideBrandSelection();
					hideLocateDGMC();
					hideOptionalValues();
					hideMDCDLabel();
		}	
}

//funtion to hide the Medium Duty Commercial Dealers label
function hideMDCDLabel(){
	document.getElementById('MDCM').style.fontWeight = 'normal';
}

//function to display the brand dropdown
function displayBSelection(){
	document.getElementById('brandSelection').style.visibility = 'visible';
	document.getElementById('brandSelection').style.display = 'block';
}

//function to display the Locate Dealer for GMC
function displayLocateDGMC(){
	document.getElementById('LocateDGMC').style.visibility = 'visible';
	document.getElementById('LocateDGMC').style.display = 'block';
}

//function to hide the Locate GMC Commercial Dealers for GMC
function hideLocateDGMC(){
	document.getElementById('LocateDGMC').style.visibility = 'hidden';
	document.getElementById('LocateDGMC').style.display = 'none';
}

//function to hide the optionalValues dropdown for GMC
function hideOptionalValues(){
	document.getElementById('optionalValues').style.visibility = 'hidden';
	document.getElementById('optionalValues').style.display = 'none';
}

//function to hide the brandSelection dropdown
function hideBrandSelection(){
	document.getElementById('brandSelection').style.visibility = 'hidden';
	document.getElementById('brandSelection').style.display = 'none';
}

//funtion to hide the brand dropdown
function hideBradD(){
	document.getElementById('brandD').style.visibility = 'hidden';
	document.getElementById('brandD').style.display = 'none';
}

//Function to load the values for Retail Dealer, Medium Duty Dealer and Business Central Dealer for cross-divisional sites
function setRadioValue(){
	var RD =  document.getElementById("RetailDealer").checked;
	//var MDD = document.getElementById("MediumDutyDealer").checked;
	var BCD = document.getElementById("BusinessCDealer").checked;
	if (RD == true){
		dTypeCode = document.getElementById("RetailDealer").value;
		agreementTypeCode = "";
		document.forms["SearchByZipCode"].programCode.value = "";
		document.forms["SearchBycityState"].programCode.value = "";
		document.forms["SearchByDealer"].programCode.value = "";
	}
	/*if (MDD == true){
		dTypeCode = document.forms["brandsDL"].MediumDutyDealer.value; agreementTypeCode = "SLS";
		document.forms["SearchByZipCode"].programCode.value = "";
		document.forms["SearchBycityState"].programCode.value = "";
		document.forms["SearchByDealer"].programCode.value = "";
		}*/
	if (BCD == true){
		dTypeCode = document.forms["brandsDL"].BusinessCDealer.value; 
		agreementTypeCode = "SLS";
		document.forms["SearchByZipCode"].programCode.value = "csv";
		document.forms["SearchBycityState"].programCode.value = "csv";
		document.forms["SearchByDealer"].programCode.value = "csv";
	}
}

//Function for Medium Duty Commercial Dealers
function mediumDutyDealers(){
	document.forms["SearchByZipCode"].ProgramICON.value = "bpo";
	document.forms["SearchBycityState"].ProgramICON.value = "bpo";
	document.forms["SearchByDealer"].ProgramICON.value = "bpo";
	document.forms["SearchByZipCode"].dealerTypeCode.value = "M";
	document.forms["SearchBycityState"].dealerTypeCode.value = "M";
	document.forms["SearchByDealer"].dealerTypeCode.value = "M";
}

//function to load the brand dropdown
function LoadBrand(){
	document.forms["brandsDL"].brandD.length = 0;
	document.forms["brandsDL"].brandD.options[0] = new Option("Select a Brand","Select a Brand","defaultSelected","selected");
	if(window.location.pathname.indexOf("mediumdealersearch") > -1 || window.location.pathname.indexOf("businessdealersearch") > -1){
		document.forms["brandsDL"].brandD.options[1] = new Option("Chevrolet", "ch");
		document.forms["brandsDL"].brandD.options[2] = new Option("GMC", "gc");
	}else{
		for(var i=0, j=1; i< brandObjectArray.length; i++){
				if(brandObjectArray[i].name.indexOf("Saab") == -1){
					document.forms["brandsDL"].brandD.options[j] = new Option(brandObjectArray[i].name, brandObjectArray[i].abbreviation);
					j++;
				}
			}
	}
	
}

//funtion to load the brand dropdown when Business Central Dealers is selected

function BCDBrand(){
	document.forms["brandsDL"].brandD.length = 0;
	document.forms["brandsDL"].brandD.options[0] = new Option("Select a Brand","Select a Brand","defaultSelected","selected");
	for(var i=0; i< brandObjectArrayBCD.length; i++){
			document.forms["brandsDL"].brandD.options[i+1] = new Option(brandObjectArrayBCD[i].nameBCD, brandObjectArrayBCD[i].abbreviationBCD);
		}
	
}

//functions to fill the state and miles dropdowns automatically
function LoadStateCityLocateDealer(){
	document.forms["SearchBycityState"].SearchByCityStateStateName.options[0]= new Option("Select A State","0");
	for(var i=0; i< stateObjectArray.length; i++){
			document.forms["SearchBycityState"].SearchByCityStateStateName.options[i+1]= new Option(stateObjectArray[i].name, stateObjectArray[i].abbreviation);
		}
}

function LoadStateDealerLocator(){
	document.forms["SearchByDealer"].SearchByVendorStateName.options[0]= new Option("Select A State","0");
	for(var i=0; i< stateObjectArray.length; i++){
			document.forms["SearchByDealer"].SearchByVendorStateName.options[i+1]= new Option(stateObjectArray[i].name, stateObjectArray[i].abbreviation);
		}
}

function LoadMileSelectionLocateDealer(){
	document.forms["SearchBycityState"].SearchByCityStateProximity.options[0]= new Option("Within 5 Miles","5");
	for(var i=0; i< mileObjectArray.length; i++){
			document.forms["SearchBycityState"].SearchByCityStateProximity.options[i+1]= new Option(mileObjectArray[i].name, mileObjectArray[i].abbreviation);
		}
	selectMileLocateDealer();
}

function LoadMileZipCode(){
	document.forms["SearchByZipCode"].SearchByPostalCodeProximity.options[0]= new Option("Within 5 Miles","5");
	for(var i=0; i< mileObjectArray.length; i++){
			document.forms["SearchByZipCode"].SearchByPostalCodeProximity.options[i+1]= new Option(mileObjectArray[i].name, mileObjectArray[i].abbreviation);
		}
	
	selectMileZipCode();
}

function LoadMileUpdate(){
	document.forms["SearchRangeDL"].LoadMileUpdate.options[0]= new Option("Within 5 Miles","5");
	for(var i=0; i< mileObjectArray.length; i++){
			document.forms["SearchRangeDL"].LoadMileUpdate.options[i+1]= new Option(mileObjectArray[i].name, mileObjectArray[i].abbreviation);
		}
	selectMile();
}

//Load the values from the dealersearch.jsp 
function LoadSelectedValues(){
	readAllSBToolsCookies();
	
	if (!isEmptyString(sbToolsCookies['sbtzipcode']) && !isEmptyString(sbToolsCookies['sbtzipproximity'])) {
		document.forms["SearchByZipCode"].SearchByPostalCodePostalCode.value = sbToolsCookies['sbtzipcode'];
		document.forms["SearchByZipCode"].SearchByPostalCodeProximity.value = sbToolsCookies['sbtzipproximity'];
	}
	
	if ((!isEmptyString(sbToolsCookies['sbtcityname']) && !isEmptyString(sbToolsCookies['sbtcitystate']) && !isEmptyString(sbToolsCookies['sbtcityproximity']))) {
		document.forms["SearchBycityState"].SearchByCityStateCityName.value = sbToolsCookies['sbtcityname'];
		document.forms["SearchBycityState"].SearchByCityStateStateName.value = sbToolsCookies['sbtcitystate'];
		document.forms["SearchBycityState"].SearchByCityStateProximity.value = sbToolsCookies['sbtcityproximity'];
	}
	
	if ((!isEmptyString(sbToolsCookies['sbtsearchdealername']) && !isEmptyString(sbToolsCookies['sbtcitynamedealer']) && !isEmptyString(sbToolsCookies['sbtcitystatedealer']))) {
		document.forms["SearchByDealer"].SearchByVendorVendorName.value = sbToolsCookies['sbtsearchdealername'];
		document.forms["SearchByDealer"].SearchByVendorCityName.value = sbToolsCookies['sbtcitynamedealer'];
		document.forms["SearchByDealer"].SearchByVendorStateName.value = sbToolsCookies['sbtcitystatedealer'];		
	}
	document.forms['brandsDL'].brandD.value = sbToolsCookies['sbtselectedbrand'];			
}


//Functions to select the default mile
function selectMile(){
	
	if(document.forms["SearchRangeDL"].LoadMileUpdate.options[3].value == "50"){
			document.forms["SearchRangeDL"].LoadMileUpdate.options[3].selected = true;
		}
	var index = 0;
	if(mileRange != ""){
		index = milesRangeValues[mileRange];
	}else{
		if(searchProximity != "null" && searchProximity != "")
			index = milesRangeValues[searchProximity];					
	}
	document.forms["SearchRangeDL"].LoadMileUpdate.options[index].selected = true;		
}

function selectMileLocateDealer(){
	if(document.forms["SearchBycityState"].SearchByCityStateProximity.options[4].value == "100"){
			document.forms["SearchBycityState"].SearchByCityStateProximity.options[4].selected = true;
		}
}

function selectMileZipCode(){
	if(document.forms["SearchByZipCode"].SearchByPostalCodeProximity.options[4].value == "100"){
			document.forms["SearchByZipCode"].SearchByPostalCodeProximity.options[4].selected = true;
		}
	}

// Submit the form with the current position "recordBegin , recordEnd" of the paging
function submitForm(recordBegin, recordEnd, PCProx, CSProx, result){
	document.forms["SearchRangeDL"].recordBegin.value = recordBegin;
	document.forms["SearchRangeDL"].recordEnd.value   = recordEnd;
	AssignProximityPaging(PCProx, CSProx);
	if(result == 'viewall'){
		document.forms["SearchRangeDL"].ShowAllresults.value = 'yes';
		}else{
			document.forms["SearchRangeDL"].ShowAllresults.value = 'no';
		}
	document.forms["SearchRangeDL"].submit();
}

function AssignProximityPaging(PCProx, CSProx){
	if(PCProx != null){
		document.forms["SearchRangeDL"].PCProximity.value = PCProx;
		document.forms["SearchRangeDL"].CSProximity.value = "null";
	}else if (CSProx != null){
		document.forms["SearchRangeDL"].PCProximity.value = "null";
		document.forms["SearchRangeDL"].CSProximity.value = CSProx;
		}else if((PCProx == null)&&(CSProx == null)){
			document.forms["SearchRangeDL"].PCProximity.value = "null";
			document.forms["SearchRangeDL"].CSProximity.value = "null";
			}
}

//function required to assign the SLbrand value
function AssignBrandValue(formName){
	document.forms[formName].SLBrand.value = brandAbbreviation;
}

function optionState(){
	var selectObj = document.forms["brandsDL"].brandD;
	var selection = selectObj.options[selectObj.selectedIndex].value;
	
	if(source != "OLA"){		
			document.getElementById("RetailDealer").checked = true;	
		if(selection == "ch" || selection == "gc"){
			document.getElementById("BusinessCDealer").disabled = false;
			document.getElementById("MediumDutyDealer").disabled = false;
			document.getElementById("BusinessDiv").style.color = "#000000";
			document.getElementById("MediumDiv").style.color = "#000000";
		}else{
			document.getElementById("BusinessCDealer").disabled = true;
			document.getElementById("MediumDutyDealer").disabled = true;	
			document.getElementById("BusinessDiv").style.color = "#CCCCCC";
			document.getElementById("MediumDiv").style.color = "#CCCCCC";
		}
	}
}

function setBrand(id){
	document.forms["brandsDL"].brandD.length = 0;
	document.forms["brandsDL"].brandD.options[0] = new Option("Select a Brand","Select a Brand","defaultSelected","selected");
	if(id.indexOf("BusinessCDealer") > -1 || id.indexOf("MediumDutyDealer") > -1){
		document.forms["brandsDL"].brandD.options[1] = new Option("Chevrolet", "ch");
		document.forms["brandsDL"].brandD.options[2] = new Option("GMC", "gc");
	}else{
		for(var i=0; i< brandObjectArray.length; i++){
				document.forms["brandsDL"].brandD.options[i+1] = new Option(brandObjectArray[i].name, brandObjectArray[i].abbreviation);
			}
	}		
}

function currentState(){
	if(dealerType == "RD"){
		document.getElementById("RetailDealer").checked = true;
	}else if(dealerType == "BC"){
		document.getElementById("BusinessCDealer").checked = true;
	}else if(dealerType == "MD"){
		document.getElementById("MediumDutyDealer").checked = true;
	}
	if (window.location.pathname.indexOf("messaging") == -1  && window.location.pathname.indexOf("vehiclelocator")  == -1)
		changeDealerOption("R");
}

//change the radio buttons

function changeDealerOption(activeNav){
	this.sitenav = document.getElementById("findDealerContent");
	var anchors = this.sitenav.getElementsByTagName('a');
	//alert(anchors);
/*	var a;
	for (var i = 0; i != anchors.length; i++) {
		a = anchors[i];
		if (a.firstChild.data=="Business Central Dealers" && activeNav == "B") {
			a.className="selected";
		}else{
			if (a.firstChild.data=="Retail Dealers" && activeNav == "R") {				
				a.className="selected";
			}else{
				if (a.firstChild.data=="Medium Duty Dealers" && activeNav == "M") {
					a.className="selected";
				}
				else
					a.className="";
				}
			
			}
	}	*/
	
	anchors[0].className="selected";
	
	}
	
	
	function setRadioButton(id){
		var elements = document.getElementById("brandsDL").getElementsByTagName("input");
		for(var i = 0; i < elements.length; i++){
			if(elements[i].id == id){
				document.getElementById(elements[i].id).checked = true;
			}else{
				document.getElementById(elements[i].id).checked = false;
			}
		}
		setStaticMsg();
	}

milesRangeValues = new Array();
milesRangeValues['5'] = 0;
milesRangeValues['10'] = 1;
milesRangeValues['25'] = 2;
milesRangeValues['50'] = 3;
milesRangeValues['100'] = 4;

/*********************Tool Tip***********************************/
function hideText(){
	document.getElementById("toolTip").style.display='none';
	if(window.location.pathname.indexOf("smallbusiness/finddealer/dealersearch") > -1){
		document.getElementById("whyNotDealer").firstChild.style.paddingLeft = 0 +"px";
	}
}
function openText(){
	document.getElementById("toolTip").style.display='block';
}
function displayText(current, text){
	var browser = document.body.className;
	var url = location.href;
	document.getElementById("dynamicText").innerHTML=text;
	document.getElementById("toolTip").style.display='block';
	document.getElementById("dynamicText").style.display='block';
	var top,left;
	if(browser.indexOf("FireFox3") > -1 || browser.indexOf("Safari") > -1){
		top = parseInt(getTop(current)) - 15;
	    left = parseInt(getLeft(current)) - 228;
	}else{
		if(browser.indexOf("Explorer7") > -1){
			top = parseInt(getTop(current)) + 5;
			left = parseInt(getLeft(current)) - 230;
		}else {
			top = parseInt(getTop(current));
			left = parseInt(getLeft(current));
		}
	}	
	
	if (current.id.indexOf("whyNotDealer") > -1){
		document.getElementById("toolTip").style.top = top+ 44 + "px";
		document.getElementById("dynamicText").style.width = "270px";
		if(browser.indexOf("FireFox3") > -1 || browser.indexOf("Safari") > -1){
			if(url.indexOf("locatedealer") > -1){
				document.getElementById("toolTip").style.top = top+ 64 + "px";
				document.getElementById("toolTip").style.left = 435 +"px";
			}else{
				document.getElementById("toolTip").style.left = 203 +"px";		
			}
		}else{
			document.getElementById("toolTip").style.left = 435 +"px";
		}
		current.firstChild.style.paddingLeft = 135 +"px";
	}else{
		document.getElementById("toolTip").style.top = top + "px";			
		document.getElementById("toolTip").style.left = left + "px";
		document.getElementById("toolTip").style.paddingTop = 0 +"px";
	}	
}


function getTop(element)
{
	var topValue = 0;
	while( element != null ) {
		topValue += element.offsetTop;
		element = element.offsetParent;
	}
	return topValue;
}

function getLeft(element)
{
	var leftValue = 0;
	while( element != null ) {
		leftValue += element.offsetLeft;
		element = element.offsetParent;
	}
	return leftValue;
}


