<!-- Modify for divisional implementation -->
var multiBrandEnabled = false;

//Fuctions required to fill the states, mile and proximity combo boxes
function initLocateVehicle(){
	LoadVLStateValues();
	LoadVLMilesValues();
	if (multiBrandEnabled){
		LoadBrand();
		}else{
			document.getElementById('brandSelection').style.display = 'none';
			document.forms["brandsDL"].brandD.style.display = 'none';
	}
	LoadVLSelectedValues();
}

//function that load the selected VL values if the "Change" button is clicked.
function LoadVLSelectedValues(){
	readAllSBToolsCookies();
	if (!isEmptyString(sbToolsCookies['sbtzipcode']) && !isEmptyString(sbToolsCookies['sbtzipproximity'])) {
		document.forms["SearchByZipCode"].postalCode.value = sbToolsCookies['sbtzipcode'];
		document.forms["SearchByZipCode"].proximity.value = sbToolsCookies['sbtzipproximity'];
	}
	
	if ((!isEmptyString(sbToolsCookies['sbtcityname']) && !isEmptyString(sbToolsCookies['sbtcitystate']) && !isEmptyString(sbToolsCookies['sbtcityproximity']))) {
		document.forms["SearchBycityState"].cityState.value = sbToolsCookies['sbtcityname'];
		document.forms["SearchBycityState"].statesSCity.value = sbToolsCookies['sbtcitystate'];
		document.forms["SearchBycityState"].mileSelectState.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'];		
	}
}

function LoadVLStateValues(){
	document.getElementById('statesSCity').options[0]= new Option("Select A State","0");
	document.getElementById('SearchByVendorStateName').options[0]= new Option("Select A State","0");
	for(var i=0; i< stateObjectArray.length; i++){
			document.getElementById('statesSCity').options[i+1]= new Option(stateObjectArray[i].name, stateObjectArray[i].abbreviation);
			document.getElementById('SearchByVendorStateName').options[i+1]= new Option(stateObjectArray[i].name, stateObjectArray[i].abbreviation);
		}
}

function LoadVLMilesValues(){
	document.getElementById('proximity').options[0]= new Option("Within 5 Miles","5");
	document.getElementById('mileSelectState').options[0]= new Option("Within 5 Miles","5");
	for(var i=0; i< mileObjectArray.length; i++){
			document.getElementById('proximity').options[i+1]= new Option(mileObjectArray[i].name, mileObjectArray[i].abbreviation);
			document.getElementById('mileSelectState').options[i+1]= new Option(mileObjectArray[i].name, mileObjectArray[i].abbreviation);
		}
	selectMileZipCode();
		selectMileCityState();
}

//Functions to select the default mile
function selectMileZipCode(){
	if(document.forms["SearchByZipCode"].proximity.options[3].value == "50"){
		document.forms["SearchByZipCode"].proximity.options[3].selected = true;
	}
}

function selectMileCityState(){
	if(document.forms["SearchBycityState"].mileSelectState.options[3].value == "50"){
		document.forms["SearchBycityState"].mileSelectState.options[3].selected = true;
	}
}

//Function to load the different brands only for FLEET.
function LoadBrand(){
	document.getElementById('brandSelection').style.display = '';
	document.forms["brandsDL"].brandD.options[0] = new Option("Select a Brand","Select a Brand");
	for(var i=0; i< brandObjectArray.length; i++){
			document.forms["brandsDL"].brandD.options[i+1] = new Option(brandObjectArray[i].name, brandObjectArray[i].abbreviation);
		}
}

//Function to validate the field selection for first search
function validateZipCode(form){
	var SelectZip = form.postalCode.value;
	var errorMessage = "";
	ret = true;
	
	var regZip = new RegExp("^[0-9]{5}$")	
	if((SelectZip != " Enter Zip Code")){
		if(!SelectZip.match(regZip)) { 
			errorMessage += "Please enter a correct ZIP Code.<br />" 
			ret = false;
		}
	}
	
	if (multiBrandEnabled){
		if(SelectBrand=="Select a Brand"){
			errorMessage += "Please select a brand.<br />";
		}
		document.forms["SearchByZipCode"].SLBrand.value = SelectBrand;
	}
	
	var errorDiv = document.getElementById("ErrorMessage");
	if(errorMessage != ""){		
		errorDiv.innerHTML = errorMessage + "<br />";
		errorDiv.style.display = "block";
		ret = false;
	} else{
		errorDiv.innerHTML = "";
		errorDiv.style.display = "none";
		createCookiesVLZipCode();
		loadVehicleSelectedCookies('InputsCookies');
		//form.submit();
	}
	return ret;
}

//Function to validate the field selection for first search
function validateCityState(form){
	var SelectCity = form.cityState.value;
	var SelectState = form.statesSCity.value;
	var SelectMile = form.mileSelectState.value;
	var errorMessage = "";
	ret = true;
	
	if(((SelectCity==" Enter City") || (SelectCity == "") || (SelectCity == " "))){
		errorMessage += "Please enter the city.<br />";
		ret = false;
	}
	
	if (SelectState==0) { errorMessage += "Please select a state.<br />"; ret = false;}
	
	if (multiBrandEnabled){
		if(SelectBrand=="Select a Brand"){
			errorMessage += "Please select a brand.<br />";
		}
		document.forms["SearchBycityState"].SLBrand.value = SelectBrand;
	}
	
	var errorDiv = document.getElementById("ErrorMessage");
	if(errorMessage != ""){		
		errorDiv.innerHTML = errorMessage + "<br />";
		errorDiv.style.display = "block";
		ret = false;
	} else{
		errorDiv.innerHTML = "";
		errorDiv.style.display = "none";
		createCookiesVLCityState();
		loadVehicleSelectedCookies('InputsCookies');
		//form.submit();
	}
	return ret;
}

//Function if the request is from Vehicle Locator Result page
function validateZipCodeVL(form){
	var SelectZip = form.postalCode.value;
	var errorMessage = "";	
	ret = true;
	
	var regZip = new RegExp("^[0-9]{5}$")	
	if((SelectZip != " Enter Zip Code")){
		if(!SelectZip.match(regZip)) { 
			errorMessage += "Please enter a correct ZIP Code.<br />" 
			ret = false;
		}
	}
	
	if (multiBrandEnabled){
		if(SelectBrand=="Select a Brand"){
			errorMessage += "Please select a brand.<br />";
		}
		document.forms["SearchByZipCode"].SLBrand.value = SelectBrand;
	}
	
	var errorDiv = document.getElementById("ErrorMessage");
	if(errorMessage != ""){		
		errorDiv.innerHTML = errorMessage + "<br />";
		errorDiv.style.display = "block";
		ret = false;
	} else{
		errorDiv.innerHTML = "";
		errorDiv.style.display = "none";
		document.forms["SearchByZipCode"].searchType.value = 0;
		createCookiesVLZipCode();
		form.submit();
	}
	return ret;
}

//Function if the request is from Vehicle Locator Result page
function validateCityStateVL(form){
	var SelectCity = form.cityState.value;
	var SelectState = form.statesSCity.value;
	var SelectMile = form.mileSelectState.value;
	var errorMessage = "";
	ret = true;
	
	if(((SelectCity==" Enter City") || (SelectCity == "") || (SelectCity == " "))){
		errorMessage += "Please enter the city.<br />";
		ret = false;
	}
	
	if (SelectState==0) { errorMessage += "Please select a state.<br />"; ret = false;}
	
	if (multiBrandEnabled){
		if(SelectBrand=="Select a Brand"){
			errorMessage += "Please select a brand.<br />";
		}
		document.forms["SearchBycityState"].SLBrand.value = SelectBrand;
	}
	
	var errorDiv = document.getElementById("ErrorMessage");
	if(errorMessage != ""){		
		errorDiv.innerHTML = errorMessage + "<br />";
		errorDiv.style.display = "block";
		ret = false;
	} else{
		errorDiv.innerHTML = "";
		errorDiv.style.display = "none";
		document.forms["SearchBycityState"].searchCity.value = SelectCity;
		document.forms["SearchBycityState"].searchState.value = SelectState;
		document.forms["SearchBycityState"].proximity.value = SelectMile;
		document.forms["SearchBycityState"].searchType.value = 1;
		createCookiesVLCityState();
		form.submit();
	}
	return ret;
}

function validateDealer(form){
	var SelectDealer = form.SearchByVendorVendorName.value;
	var SelectCity = form.SearchByVendorCityName.value;
	var SelectState = form.SearchByVendorStateName.value;
	var errorMessage = "";
	ret = true;
	
	if(((SelectDealer==" Enter Dealer Name") || (SelectDealer == "") || (SelectDealer == " "))){
		errorMessage += "Please enter the dealer name.<br />";
		ret = false;
	}
	if(((SelectCity==" Enter City") || (SelectCity=="") || (SelectCity == " "))){
		errorMessage += "Please enter the city.<br />";
		ret = false;
	}
	if (SelectState==0) { errorMessage += "Please select a state.<br />"; ret = false;}
	
	if (multiBrandEnabled){
		if(SelectBrand=="Select a Brand"){
			errorMessage += "Please select a brand.<br />";
		}
		document.forms["SearchByDealer"].SLBrand.value = SelectBrand;
	}
	
	var errorDiv = document.getElementById("ErrorMessage");
	if(errorMessage != ""){
		errorDiv.innerHTML = errorMessage + "<br />";
		errorDiv.style.display = "block";
		ret = false;
	} else {
		errorDiv.innerHTML = "";
		errorDiv.style.display = "none";
		createCookiesVLDealer();
		form.submit();
	}
	return ret;
}

function validateRangeDL(form){
	var SelectMile = form.mileSelect.value;
	var errorMessage = "";
	
	if (SelectMile==0){ errorMessage += "Please select a valid range.<br />"; }
	
	var errorDiv = document.getElementById("ErrorMessage");
	if(errorMessage != ""){
		errorDiv.innerHTML = errorMessage + "<br />";
		errorDiv.style.display = "block";
	} else {
		errorDiv.innerHTML = "";
		errorDiv.style.display = "none";
		form.submit();
	}
}