var regionsToCodes = new Array();
regionsToCodes["Northeast"] = "40";
regionsToCodes["Southeast"] = "30";
regionsToCodes["Western"] = "10";
regionsToCodes["South Central"] = "20";
regionsToCodes["North Central - East"] = "60";
regionsToCodes["North Central - West"] = "70";

var modelYears= new Array();
modelYears[0]="2008";
modelYears[1]="2007";


function addOption(zip, year, regionName, countyName){
	var countiesSelect = document.getElementById("cmbCounties");
	var control = 0; 
	var cookieCounty= cookieValue("countyCookie");
	for(var i=0;i<countiesSelect.length;i++){
		if (countiesSelect.options[i].text == textToLowerCase(countyName)){
			control = 1;
			if ((countiesSelect.options[i].value).indexOf(regionName)==-1)
				countiesSelect.options[i].value = countiesSelect.options[i].value+","+regionName;
		}
	}
	if(control == 0){
		var y = document.createElement("option");
		y.id = countyName+" - "+regionName;
		y.text = textToLowerCase(countyName);
		y.value = regionName;
		try{
			
			document.getElementById("cmbCounties").add(y,null);
			if(cookieCounty != -1){
				for(var i=0;i<countiesSelect.length;i++){
					if (countiesSelect.options[i].text == cookieCounty){
						countiesSelect.options[i].selected = true;
					}
				}
			}
		}
		catch(ex){
			document.getElementById("cmbCounties").add(y);
			if(cookieCounty != -1){
				for(var i=0;i<countiesSelect.length;i++){
					if (countiesSelect.options[i].text == cookieCounty){
						countiesSelect.options[i].selected = true;
					}
				}
			}
		}		
	}
}
/*****************************************************************/
function addRegionToOption(newRegion,countyName){
	countiesSelect = document.getElementById("cmbCounties");
	for(var i=0;i<countiesSelect.length;i++){
		if (countiesSelect.options[i].text == countyName){
			if ((countiesSelect.options[i].value).indexOf(newRegion)==-1)
				countiesSelect.options[i].value = countiesSelect.options[i].value+","+newRegion;
		}
	}
}
/*****************************************************************/
function setAction(select){
	var selectedVal = select.value;
	var cookieZip= cookieValue("zipCookie");
	if(cookieZip!= document.getElementById("txtZipCode").value){
		validate(document.regionSelectionForm);
		}
	if(selectedVal != "Select County" && document.getElementById("txtZipCode").value != ""){
		document.regionSelectionForm.county.value = select.options[select.selectedIndex].innerHTML;
		if (selectedVal == "WESTERN" || selectedVal == "SOUTH CENTRAL" || selectedVal == "SOUTHEAST" || selectedVal == "NORTHEAST" || selectedVal == "NORTH CENTRAL - EAST" || selectedVal == "NORTH CENTRAL - WEST"){
			document.regionSelectionForm.regionM.value = selectedVal;
			document.regionSelectionForm.innerHTML=document.regionSelectionForm.innerHTML + "<input id=\"region\" type=\"hidden\" name=\"region\" value=\"\"/>";
			document.regionSelectionForm.regions.value="";
			setCookieZip("countyCookie",document.regionSelectionForm.county.value);
			document.regionSelectionForm.submit();
		}else{
			var regions =  new Array();
			regions = selectedVal.split(",");
			document.regionSelectionForm.regionM.value = regions[0];
			for(var j=0; j< regions.length; j++){
				if (document.regionSelectionForm.regions.value == ""){
					document.regionSelectionForm.regions.value = regions[j];
				}
				else{
					document.regionSelectionForm.regions.value = document.regionSelectionForm.regions.value+","+regions[j];
				}
			}
			setCookieZip("countyCookie",document.regionSelectionForm.county.value);
			document.regionSelectionForm.submit();
		}
	}
}
/*****************************************************************/
function writeURL(zip, region, regions, year, county, brand){
	var currentRegion = region;
	var passedRegions =  new Array();
	var url = "";
	var yearLink = "";
	var current = "incentives";
	passedRegions = regions.split(",");	
	var regionCode;
	var openPage=window.location.href;
	openPage=openPage.substring(0,openPage.indexOf("?"));
	for(var j=0; j < passedRegions.length; j++){
		regionCode = passedRegions[j].replace("+"," ");	
		if(j != (passedRegions.length - 1))
			passedRegions[j]= passedRegions[j] + " |";
		
		if((currentRegion == passedRegions[j]) || ((currentRegion + " |") == passedRegions[j])){
//			passedRegions[j]= passedRegions[j] + " |";
			if(j==0)
				url = url +"<span class='currentRegion'> "+passedRegions[j]+" </span>";
			else
				url = url +"<span class='currentRegion'> "+passedRegions[j]+" </span>";
			currentRegion = "";
		}	
		else{
			var regionLink = passedRegions[j].replace(" |","");
			url = url + " <a href='"+openPage+"?zip="+zip+"&modelBrand="+brand+"&modelYear="+year+"&regionM="+regionLink+"&regions="+regions+"&county="+county+"&current="+current+"'> "+passedRegions[j]+" </a>";
		}
	}
	//url= changeHeader(url);
	if(window.location.href.indexOf("pages/") == -1){
		document.getElementById("rHeader").innerHTML ="Current Offers for " + changeHeader(url);
		document.getElementById("rHeader").style.display = "block";
	}
	return url;
}
/*****************************************************************/

function changeHeader(stringText){	
 var finalContent,findTag,until,longTag,tagIni,tagEnd,len,strFirst; 
 if(stringText.indexOf("<a") != -1)
 {
   tagIni = "<a";
   tagEnd = "</a>";
 }
 else
 {
    tagIni = "<A";
	tagEnd = "</A>";
 }  
	while(stringText.indexOf(tagIni) != -1){
		finalContent="";
		len=stringText.length;
		findTag = stringText.indexOf(tagIni);	
		strFirst= stringText.substring(findTag,len);	
		finalContent= finalContent+stringText.substring(0,findTag);		
		until = strFirst.indexOf(">");	
		longTag=findTag+until+1;
		finalContent= finalContent+stringText.substring(longTag,len);
		finalContent = finalContent.replace(tagEnd,"");	
		stringText=finalContent;		
	}	
	return stringText;
	
}
/*****************************************************************/
function showSelectedOption(optionName, selectName){
	cmbSelect = document.getElementById(selectName);
	for(var i=0;i<cmbSelect.length;i++){
		if (cmbSelect.options[i].text == optionName){
			cmbSelect.options[i].selected = true;
		}
	}
}
/*****************************************************************/
function validate(frm){
	var msgError = "";
	if (frm.zip.value == ""){
		msgError = "Please enter your ZIP Code.";
	}
	else if (frm.zip.value.length < 5){
		msgError = "The ZIP Code is invalid.";
	}
	if (msgError == ""){
		window.location.search="";
		setCookieZip("zipCookie",frm.zip.value);
		frm.submit();
	}
	else{
		alert(msgError);
	}
	msgError = "";	
}
/*****************************************************************/
function setYear(year){
	document.regionSelectionForm.modelYear.value = year;
	document.regionSelectionForm.submit();
}
/*****************************************************************/
function changeYear(select){
	var year = select.value;
	if (year =="2008"){
	document.getElementById("year2007").style.display='none';
	document.getElementById("models2007").innerHTML=document.getElementById("models2008").innerHTML;
	}else{
		document.getElementById("year2008").style.display='none';
		document.getElementById("models2007").innerHTML="<tr><th class=\'contentText\'><h4>2007 Models</h4></th></tr>";
	}
	document.getElementById("year"+year).style.display='block';
}
/*****************************************************************/

function changeFrom(f){
	from = f;	
}
/*****************************************************************/
function showSelected(year, brand){
	if(year == ""){
		year = "2008";
	}
	if(brand == ""){
		brand = "0";
	}
	yearSelect = document.getElementById("cmbYear");
	brandSelect = document.getElementById("cmbBrand");
	for(var i=0;i<yearSelect.length;i++){
		if (yearSelect.options[i].text == year){
			yearSelect.options[i].selected = true;
		}
	}
	for(var i=0;i<brandSelect.length;i++){
		if (brandSelect.options[i].text == brand){
			brandSelect.options[i].selected = true;
		}
	}
	
}
/*****************************************************************/
function showCurrentDiv(year, brandSelect){
		document.getElementById("models2007").innerHTML= year + " Models";
	for(var i=0; i < modelYears.length; i++){
		if(modelYears[i]!=year ){
			document.getElementById("year"+modelYears[i]).style.display='none';
		}else{
		document.getElementById("year"+modelYears[i]).style.display='block';
		}
	}
	changeBrand(brandSelect,year);
	
	}

/*****************************************************************/
function changeParam(zip, region, regionM, regions, county, year, brand){
	document.regionSelectionForm.txtZipCode.value=zip;
	document.regionSelectionForm.modelYear.value=year.value;
	document.regionSelectionForm.modelBrand.value=brand;
	document.regionSelectionForm.regionM.value=regionM;
	document.regionSelectionForm.regions.value=regions;
	document.regionSelectionForm.county.value=county;
	ele = document.createElement('input'); // 5
	ele.type = 'hidden'; // 6
  	ele.value = region; // 8
  	ele.name = 'region' // 8
 	document.regionSelectionForm.appendChild(ele); // 7
	//document.regionSelectionForm.innerHTML=document.regionSelectionForm.innerHTML + "<input id=\"region\" type=\"hidden\" name=\"region\" value=\""+region+"\" />";
	}
/*****************************************************************/
function setYearSelectValues(zip, region, regionM, regions, county, year, brand){
	changeParam(zip, region, regionM, regions, county, year, brand);
	document.regionSelectionForm.submit();
}
/*****************************************************************/
function changeBrand(select, year){
	var brandSelected = select.value;
	var joinOption = brandSelected + year;
	for(i= 1; i<=8; i++){
		if(brandSelected=="0"){
			document.getElementById("car"+i+year).style.display="block";
		}else{
			document.getElementById("car"+i+year).style.display="none";
			}
	}
	if(brandSelected!="0"){
		document.getElementById("car"+joinOption).style.display="block";
	}
	document.getElementById("modelBrand").value= select.options[brandSelected].text;
	if(document.getElementById("regionLink")){
		var textLink = document.getElementById("regionLink").innerHTML;
		var len=textLink.length;
		firtPosition = textLink.indexOf("&");
		strFirst= textLink.substring(0,firtPosition);
		strSecond = textLink.substring(firtPosition+1,len);
		strSecond = strSecond.substring(strSecond.indexOf("&"),len);
		strFinal= strFirst + "&modelBrand=" + document.regionSelectionForm.modelBrand.value + strSecond;
		document.getElementById("regionLink").innerHTML = strFinal;
		}
	}
/*****************************************************************/
function addRegionOption(zip, year, region){
	var regionsSelect = document.getElementById("cmbRegions");
	var control = 0; 
	for(var i=0;i<regionsSelect.length;i++){
		if (textToLowerCase(regionsSelect.options[i].text) == textToLowerCase(region)){
			control = 1;
			if ((regionsSelect.options[i].value).indexOf(region)==-1)
				regionsSelect.options[i].value = regionsSelect.options[i].value+","+region;
		}
	}
	if(control == 0){
		var y = document.createElement("option");
		y.id = region;
		y.text = textToLowerCase(region);
		y.value = textToLowerCase(region);
		regionsSelect.disabled = false;
		try{
			document.getElementById("cmbRegions").add(y,null);
		}
		catch(ex){
			document.getElementById("cmbRegions").add(y);
		}
		
	}
}

/*****************************************************************/
function regionChange(regionName) {
	var url = window.opener.location.href;
	//window.opener.location.href = url +"?region="+regionsToCodes[regionName]; current offers in the overview's pages
	window.close();
}
/*****************************************************************/
/*function setRegionAction(select){
	var selectedUrl = select.value;
	window.location= selectedUrl;
}*/
/*****************************************************************/
function textToLowerCase(text){
	var s1 = text;
	var arr = new Array();
	if(s1.split(" - ").length > 1){
		arr = s1.split(" - ");
		switch(s1.split(" - ").length){
			case 2:
				if(arr[0].split(" ").length > 1){
					var arrSub = new Array();
					arrSub = arr[0].split(" ");
					arr[0] = arrSub[0].substring(0,1)+""+arrSub[0].substring(1,arrSub[0].length).toLowerCase()+" "+arrSub[1].substring(0,1)+""+arrSub[1].substring(1,arr[0].length).toLowerCase();
				}else{
					arr[0] = arr[0].substring(0,1)+""+arr[0].substring(1,arr[0].length).toLowerCase();				
				}
				if(arr[1].split(" ").length > 1){
					var arrSub = new Array();
					arrSub = arr[1].split(" ");
					arr[1] = arrSub[0].substring(0,1)+""+arrSub[0].substring(1,arrSub[0].length).toLowerCase()+" "+arrSub[1].substring(0,1)+""+arrSub[1].substring(1,arr[1].length).toLowerCase();
				}else{
					arr[1] = arr[1].substring(0,1)+""+arr[1].substring(1,arr[1].length).toLowerCase();
				}
				return(arr[0]+" - "+arr[1]);
				break;
			case 3:
				arr[0] = arr[0].substring(0,1)+""+arr[0].substring(1,arr[0].length).toLowerCase();
				if(arr[1].split(" ").length > 1){
					var arrSub = new Array();
					arrSub = arr[1].split(" ");
					arr[1] = arrSub[0].substring(0,1)+""+arrSub[0].substring(1,arrSub[0].length).toLowerCase()+" "+arrSub[1].substring(0,1)+""+arrSub[1].substring(1,arr[1].length).toLowerCase();
				}else{
					arr[1] = arr[1].substring(0,1)+""+arr[1].substring(1,arr[1].length).toLowerCase();	 
				}
				arr[2] = arr[2].substring(0,1)+""+arr[2].substring(1,arr[1].length).toLowerCase();
				return(arr[0]+" - "+arr[1]+" "+arr[2]);
				break;
		}
	}
	else if(s1.split(" ").length > 1) {
		arr = s1.split(" ");
		arr[0] = arr[0].substring(0,1)+""+arr[0].substring(1,arr[0].length).toLowerCase();
		arr[1] = arr[1].substring(0,1)+""+arr[1].substring(1,arr[1].length).toLowerCase();
		return(arr[0]+" "+arr[1]);
	}else{
		s1 = s1.substring(0,1)+""+s1.substring(1,s1.length).toLowerCase();
		return(s1);
	}
}

function changeUpperCapital(name){
 var chars;
 chars= name[0];
 for (i=1 ; i < name.length; i++){
	 chars = chars + name[i].toLowerCase();
 }
 return chars;
}

	/********************Zip Code Cookie************************************************/
function setCookieZip(name,value){
/*		var date = new Date();
		date.setTime(date.getTime()+(30*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
//		document.cookie = name+"="+value+expires+"; path=/";*/
		document.cookie= name+"="+value+";expires=2 Dec 9999 23:59:59 GMT";
	}
	
function getCookieZip(cookiename, passedZip) {
	var cookieZip=cookieValue(cookiename);
 if (cookieZip==-1 || cookiename==""){
	 if(passedZip!=0){
	 setCookieZip(cookiename,passedZip);
	 }
 return 0;
 }else{
	 if (cookieZip==-1){
	 cookieZip=cookiestring.length;
	 }
	 if(document.getElementById("txtZipCode").value ==0 || document.getElementById("txtZipCode").value==""){
	 document.getElementById("txtZipCode").value= cookieZip;
	 validate(document.regionSelectionForm);
	 }
 }
}
function cookieValue(cookieName){
	var cookiestring=""+document.cookie;
	var firstIndex = cookiestring.indexOf(cookieName);
	cookiestring=cookiestring.substring(firstIndex,cookiestring.length);
	if(cookiestring.indexOf(';') != -1)
		cookiestring=cookiestring.substring(0,cookiestring.indexOf(';'));
	var valueFound;
	if(firstIndex == -1)
		return firstIndex;
	else{
		valueFound = unescape(cookiestring=cookiestring.substring(cookiestring.indexOf('=')+1,cookiestring.length));
		return valueFound;
	}
}

function  verifyCookie(modelID){
	var cookiestring=""+document.cookie;
	var cookiesNames= new Array();
	var cookieActual;
	var i =0;
	while(i<cookiestring.length){
		cookieActual=cookiestring.substring(0,cookiestring.indexOf('='));
		if (cookieActual.indexOf(modelID) ==-1 && cookieActual !="zipCookie" && cookieActual !="countyCookie" && cookieActual !="s_cc" && cookieActual !="s_sq" && cookieActual !="JSESSIONID"){
			cookiesNames[i]=cookiestring.substring(0,cookiestring.indexOf('='));
			i++;
		}
		if(cookiestring.indexOf(';') != -1)
			cookiestring = cookiestring.substring(cookiestring.indexOf(';')+2,cookiestring.length);
		else i=cookiestring.length;
	}
	if(cookiesNames.length!=0){
		for(i=0; i<cookiesNames.length; i++){
			if(cookiesNames[i]!=" "){
				eraseCookie(cookiesNames[i]);
				}
			}
		}
	}