/*
*
*/
var myAcc;
function onVSLoad(id){	
		var showTab =-1
		if 	(id == "vsModel") showTab = 0;
		if 	(id == "style") showTab = 1;
		if 	(id == "app") showTab = 2;
		myAcc = new Fx.Accordion($$('#gExtras .titles'), $$('#gExtras .gVSNavContent'), {show: showTab, display: showTab,
			onActive: function(toggler, i){
				//console.log(toggler.id + " " + i.id);
				//if (!hasClassName(toggler, "on")) {
				//console.log("adding on to "+ i.id);
					addClassName(toggler, "on");
					//alert(toggler.id + " ("+ toggler.className +")");
				//}
			},
		
			onBackground: function(toggler, i){
				//console.log(toggler.id + " " + i.id);
				//if (hasClassName(toggler, "on")) {
				//console.log("removing on from "+ toggler.id + " ("+ toggler.className +")");
					removeClassName(toggler, "on");
				//}
				//toggler.setStyle('background-color', toggler.defaultColor);
				//toggler.getFirst().setStyle('color', '#222');
			}});

}

function changeDisplay(obj){
	if (obj.className.indexOf("on") > -1){
		myAcc.options.alwaysHide=true;
	}else{
		myAcc.previous = -1;
		myAcc.options.alwaysHide=false;
	}
}

var urllink = window.location.href;
var params = window.location.search;
var menuOption = "", objId="", optionSelected=false, menuItemSelected="", firstHeight="", firstMarginTop="";

function onStateLeftNav(){
		if (urllink.indexOf("/business-central-dealers") > -1){			
				menuOption = "smallBusiness";
				objId = document.getElementById("iSmallBusiness");
				objId.style.background  = "url('/images/nav/btn_small_on.jpg') no-repeat";
				optionSelected = true;
			if(params.indexOf("source=workReady") > -1){
				toggle('vehicleInfoOff', 'vehicleInfoContent');
				var element = document.getElementById("findDealerContent").getElementsByTagName("li");
				element[1].childNodes[0].className = "";
			}else{
				toggle('findDealerOff', 'findDealerContent');
				var element = document.getElementById("vehicleInfoContent").getElementsByTagName("li");
				element[2].childNodes[0].className = "";
			}	
		}
		//tools div
		if (urllink.indexOf("/tools") > -1){
				menuOption = "tools";
				objId = document.getElementById("iTools");
				objId.style.background  = "url('/images/nav/btn_tools_on.jpg') no-repeat";
				optionSelected = true;
				
				if (urllink.indexOf("/help") > -1){
						toggle('tHelpOff', 'tHelpContent');
				}
				
		}
		//small business div
		else if ( urllink.indexOf("/smallbusiness") > -1){
				menuOption = "smallBusiness";
				objId = document.getElementById("iSmallBusiness");
				objId.style.background  = "url('/images/nav/btn_small_on.jpg') no-repeat";
				optionSelected = true;
				if (urllink.indexOf("/incentives") > -1){
						toggle('incentivesPricingOff', 'incentivesPricingContent');
				}
				if (urllink.indexOf("/maintenance") > -1){
						toggle('maintenanceOff', 'maintenanceContent');
				}
				if (urllink.indexOf("/finddealer") > -1){
						toggle('findDealerOff', 'findDealerContent');
				}				
				if (urllink.indexOf("/pages") > -1 ){
						toggle('vehicleInfoOff', 'vehicleInfoContent');
						if (urllink.indexOf("/shop") > -1 ){
						
						}
				}
				if (urllink.indexOf("/specialtyvehicles") >-1){
					toggle('specialtyVehiclesOff', 'specialtyVehiclesContent');
				}
				if (urllink.indexOf("/vehicles") >-1){
					toggle('vehicleInfoOff', 'vehicleInfoContent');
				}
		}		
				//fleet div
		else if (urllink.indexOf("/fleet") > -1){
				menuOption = "fleet";
				objId = document.getElementById("iFleet");
				objId.style.background  = "url('/images/nav/btn_fleet_on.jpg') no-repeat";
				optionSelected = true;
				
				if (urllink.indexOf("/maintenance") > -1){
						toggle('fleetServiceMaintenanceContentOff', 'fleetServiceMaintenanceContent');
				}
				if (urllink.indexOf("/vehicles") >-1){
					toggle('fleetVehicleInfoOff', 'fleetVehicleInfoContent');
				}
				if (urllink.indexOf("/incentives") > -1){
					toggle('fleetIncentivesPricingOff', 'fleetIncentivesPricingContent');
				}
				if (urllink.indexOf("/currentoffers") > -1){
						toggle('fleetIncentivesPricingOff', 'fleetIncentivesPricingContent');
				}
				if (urllink.indexOf("/finddealer") > -1){
						toggle('fleetFindDealerOff', 'fleetFindDealerContent');
				}
				if (urllink.indexOf("/specialtyvehicles") > -1){
						toggle('fleetSpecialtyVehiclesOff', 'fleetSpecialtyVehiclesContent');
				}
		}
		//government dev
		else if (urllink.indexOf("/government") > -1){
				menuOption = "government";
				objId = document.getElementById("iGovernment");
				objId.style.background  = "url('/images/nav/btn_government_on.jpg') no-repeat";
				optionSelected = true;
				
				//Ordering
				if (urllink.indexOf("/ordering/") > -1 && urllink.indexOf("?navSec=ordering") >-1){
					toggle('gordering2InfoOff', 'gordering2InfoContent');
				}else if (urllink.indexOf("/ordering/") > -1){
					toggle('gorderingInfoOff', 'gorderingInfoContent');
				
				}
				
				//Products
				if (urllink.indexOf("/products/") > -1 && urllink.indexOf("?navSec=products") > -1){
						toggle('gproducts2InfoOff', 'gproducts2InfoContent');
				}else if (urllink.indexOf("/products/") > -1){
					toggle('gproductsInfoOff', 'gproductsInfoContent');
				}
				
				
				//Maintenance
				if (urllink.indexOf("/maintenance-info") > -1){
						toggle('gserviceInfoOff', 'gserviceInfoContent');
				}
				if (urllink.indexOf("/maintenance-tools") > -1){
						toggle('gserviceToolsOff', 'gserviceToolsContent');
				}
				

		}	
				
		if ((urllink.indexOf("/finddealer/svmlocator") > -1) && (urllink.indexOf("section=fleet") > -1)){
			menuOption = "fleet";
			objId = document.getElementById("iFleet");
			objId.style.background  = "url('/images/nav/btn_fleet_on.jpg') no-repeat";
			optionSelected = true;
			toggle('fleetFindDealerOff', 'fleetFindDealerContent');
		}
		if ((urllink.indexOf("/finddealer/svmlocator") > -1) && (urllink.indexOf("section=smallbusines") > -1)){
			menuOption = "smallBusiness";
			objId = document.getElementById("iSmallBusiness");
			objId.style.background  = "url('/images/nav/btn_small_on.jpg') no-repeat";
			optionSelected = true;
			toggle('findDealerOff', 'findDealerContent');
		}
		if (optionSelected){
				objId.style.width  = "204px";
				document.getElementById(menuOption).className="itemShown";
		}
		menuItemSelected=itemList;
		//showMenuSelected(itemList, "down");
		//alert(document.getElementById(itemList).offsetHeight);
}		

var  cont=0;

function showDiv(divId, direction, height, actualHeight){
	var ready="";
	if (direction == "down"){
		  if (cont + 10 > height ){
		  		cont =cont + ( height - cont);
		  		ready="ok";
		  }else		
		   		cont = cont + 10;
		  document.getElementById(divId).style.height = cont + "px";
		  document.getElementById(divId).style.display="block";
		 document.getElementById(divId).style.visibility="visible";
		  if (ready == "") setTimeout("showDiv('"+divId+"','down'," + height +")",60); 
		  else {cont=0; }
	}		
	if (direction == "up"){
		  if (0 > height - 10){
		  		ready="ok";
		 		height = 0;
		  }else
		 		height = height - 10;
		  if (height == 0) {
			  document.getElementById(divId).style.height=(actualHeight) +"px";
			  document.getElementById(divId).style.display="none";
		 }else{
		  		document.getElementById(divId).style.height = height+ "px";
		 }  
		  
		  if (ready == "") 
		  		setTimeout("showDiv('"+divId+"','up'," + (height) +"," +actualHeight + ")",60); 
	}			
}

function showMenuSelected(itemList, direction){
		if (document.getElementById(itemList)){
			if (direction == "down"){
				var height = document.getElementById(itemList).offsetHeight;
				document.getElementById(itemList).style.visibility="hidden";
				document.getElementById(itemList).style.display="none";
				document.getElementById(itemList).style.overflow="hidden";
				setTimeout("showDiv('"+ itemList +"','down'," + height+","+ height+")",60);
			}else{
			
				var heightString = document.getElementById(itemList).style.height;
				heightString = heightString.substring(0,heightString.indexOf("px"));
				var height = parseInt(heightString);
/*				document.getElementById(itemList).style.visibility="hidden";
				document.getElementById(itemList).style.display="none";
				document.getElementById(itemList).style.overflow="hidden";*/
				setTimeout("showDiv('"+ itemList +"','up'," + height+","+ height+")",60);
			}	
		}	

}

function returnMaxHeight(){
		if ( document.getElementById("gContent").offsetHeight > document.getElementById("gExtras").offsetHeight && 
			   document.getElementById("gContent").offsetHeight > document.getElementById("gNavigation").offsetHeight	)
			   document.getElementById("gNavigation").style.height = document.getElementById("gContent").offsetHeight + "px";
		else
			if ( document.getElementById("gNavigation").offsetHeight > document.getElementById("gExtras").offsetHeight)	
 				document.getElementById("gNavigation").style.height = document.getElementById("gNavigation").offsetHeight + "px"; 
 			else	
 				document.getElementById("gNavigation").style.height = document.getElementById("gExtras").offsetHeight + "px"; 

 		if (document.getElementById("gNavigation").offsetHeight > document.getElementById("navBg").offsetHeight){		
 			document.getElementById("gNavigation").style.border = "1px #878787 solid"; 		
 			document.getElementById("navBg").style.borderBottom = "1px"; 		
 		}else{
 			document.getElementById("navBg").style.border = "1px #878787 solid"; 		
 			document.getElementById("gNavigation").style.borderBottom = "1px "; 		
 		}
 		
 		document.getElementById("gPageWrapper").style.height = document.getElementById("gNavigation").style.height; 
 		if (firstHeight == ""){
 			firstHeight = document.getElementById("gPageWrapper").offsetHeight;
 			//firstMarginTop = document.getElementById("gFooter").style.marginTop;
 		}else{
				if (document.getElementById("gNavigation").offsetHeight >  firstHeight){
				    var rest = document.getElementById("gPageWrapper").offsetHeight -  firstHeight;
 					document.getElementById("gFooter").style.marginTop = rest + "px"; 
 				}else{
 				   document.getElementById("gFooter").style.marginTop = "0px";
 				}
 		}	
}


function currentSubmit(form){
	document.forms.f_selectRegion.action = document.forms.f_selectRegion.action + '#currentoffers';	
	document.forms.f_selectRegion.submit();
}

// JavaScript Document

//http://www.biasecurities.com/blogs/jim/archive/2005/04/28/1794.aspx
function AttachEvent(elementObj, eventName, eventHandlerFunctionName)
{
	if (elementObj.addEventListener) { // Non-IE browsers
		elementObj.addEventListener(eventName, eventHandlerFunctionName, false);
	} else if (elementObj.attachEvent) { // IE 6+
		elementObj.attachEvent('on' + eventName, eventHandlerFunctionName);
	} else { // Older browsers
		var currentEventHandler = elementObj['on' + eventName];
		if (currentEventHandler == null) {
			elementObj['on' + eventName] = eventHandlerFunctionName;
		} else {
			elementObj['on' + eventName] = function(e) { currentEventHandler(e); eventHandlerFunctionName(e); }
		}
	}
}

function validateEmail(fieldID, errorField, thkField){
	var emailRegExp = new RegExp("^\[^\\n]+@[^\\n]+\\.[^\\n]+$");
	var field= document.getElementById(fieldID).value;
	
	if (!field.match(emailRegExp)){
		document.getElementById(thkField).style.display= "none";
		document.getElementById(errorField).style.display= "block";
		return false;
	}
	document.getElementById(errorField).style.display= "none";
	document.getElementById(thkField).style.display= "block";
	document.getElementById(fieldID).value= " Enter your email";
	
	return true;
}

function popup(url,winName,attributes){
  var newWindow = window.open(url,winName,attributes);
  if(newWindow == null){
	  alert('A popup containing important information was blocked by your browser.\n Please enable popups for this site in order to view this information.');
  }else{
    newWindow.focus();
  }
}

function superPopup(pOptions){
	
	var url, type, directories, location, menubar, resizable, scrollbars, status, toolbar, top, left, width, height, winName, clickTrackValue;
	
	// URL is the only required field.
	if (pOptions.url != null) {	url = pOptions.url;} else { alert('Error: Link URL Missing')};
	
	if (pOptions.type != null) { type = pOptions.type;};

	// type will set some basic options to make the function cleaner -- otherwise all values can be set
	if(type == "_blank"){	//used to generate a window the same size as the previous
		var windowWidth = pOptions.type;
		var windowHeight = pOptions.type;	
			
		//used to generate original "window names"
		var randomNumber = Math.floor(Math.random()*1000);
		winName="blank" + randomNumber; width=windowWidth; height=windowHeight; scrollbars="yes"; menubar="yes"; toolbar="yes"; directories="yes"; location="yes"; top="0"; left="0";
	} else if(type == "mapwin"){
		winName="mapwin"; width = "586"; height= "550"; scrollbars = "yes"; resizable = "yes"; toolbar="no"; directories="no"; location="no"; status="no"; menubar="no";
	} else if(type == "KBB"){
		winName="kbbPopup"; width = "780"; height= "500"; scrollbars = "yes"; resizable = "yes"; toolbar="no"; directories="no"; location="no"; status="no"; menubar="no";
    } else if(type == "regionDetails"){
		winName="regionDetails"; width = "750"; height= "650"; scrollbars = "yes"; resizable = "yes"; toolbar="no"; directories="no"; location="no"; status="no"; menubar="no";
    } else if(type == "otherPage"){
		winName="outsidePage"; width = "780"; height= "780"; scrollbars = "yes"; resizable = "yes"; toolbar="yes"; directories="no"; location="yes"; status="yes"; menubar="yes";
    } else if(type == "reqBrochure"){
		winName="reqBrochure"; width = "795"; height= "600"; scrollbars = "no"; resizable = "yes"; toolbar="no"; directories="no"; location="no"; status="no"; menubar="no";
    } else if(type == "bonusCash"){
		winName="bonusCash"; width = "720"; height= "610"; scrollbars = "no"; resizable = "no"; toolbar="no"; directories="no"; location="no"; status="no"; menubar="no";
    } else if(type == "infoPopup"){
		winName="infoPopup"; width = "750"; height= "500"; scrollbars = "yes"; resizable = "no"; toolbar="no"; directories="no"; location="no"; status="no"; menubar="no";
    } else if(type == "specsPopup"){
		winName="specsPopup"; width = "550"; height= "400"; scrollbars = "yes"; resizable = "no"; toolbar="no"; directories="no"; location="no"; status="no"; menubar="no";
    } else if(type == "galleryPopup"){
		winName="galleryPopup"; width = "570"; height= "500"; scrollbars = "no"; resizable = "no"; toolbar="no"; directories="no"; location="no"; status="no"; menubar="no";
    } else if(type == "safetyPopup"){
		winName="safetyPopup"; width = "405"; height= "400"; scrollbars = "no"; resizable = "no"; toolbar="no"; directories="no"; location="no"; status="no"; menubar="no";
    }
	 
					
	// test to see if we have set a window option on the function or in a profile above, if not then set the value to be a default.
	if (pOptions.directories != null) { directories = pOptions.directories;} else if (directories == null) { directories = "no" ;}
	if (pOptions.location != null) { location = pOptions.location;} else if (location == null) { location = "no" ;}
	if (pOptions.menubar != null) { menubar = pOptions.menubar;} else if (menubar == null) { menubar = "no" ;}
	if (pOptions.resizable != null) { resizable = pOptions.resizable;} else if (resizable == null) { resizable = "no" ;}
	if (pOptions.scrollbars != null) { scrollbars = pOptions.scrollbars;} else if (scrollbars == null) { scrollbars = "yes" ;}
	if (pOptions.status != null) { status = pOptions.status; } else if (status == null) { status = "yes" ;}
	if (pOptions.toolbar != null) { toolbar = pOptions.toolbar; } else if (toolbar == null) { toolbar = "no" ;}
	if (pOptions.top != null) { top = pOptions.top} else if (top == null) { top = "50";}
	if (pOptions.left != null) { left = pOptions.left;} else if (left == null) { left = "50" ;}
	if (pOptions.width != null) { width = pOptions.width;} else if (width == null) { width = "250" ;}
	if (pOptions.height != null) { height = pOptions.height;} else if (height == null) { height = "250" ;}
	if (pOptions.winName != null) { winName = pOptions.winName;} else if (winName == null) { winName = "popUp" ;}
	if (pOptions.clickTrack != null) { clickTrackValue = pOptions.clickTrack;}
	
	// build our complete window options statement
	windowOptions = "width=" + width + ", height=" + height + ", directories=" + directories + ", location=" + location + ", menubar=" + menubar + ", resizable=" + resizable + ", scrollbars=" + scrollbars + ", toolbar=" + toolbar + ", status=" + status + ", toolbar=" + toolbar + ", top=" + top + ", left=" + left;
	
//	if(clickTrackValue != null){
		//alert(clickTrackValue);
	//	clickTrack({s_linkName: clickTrackValue});
//	}*/
	
	var newWindow = window.open(url,winName,windowOptions);
	//alert(windowOptions);
	if (newWindow == null){
		//alert('A popup containing important information was blocked by your browser. Please enable popups for this site in order to view this information.');
		var errorBox = document.getElementById("blockedPopup");
		errorBox.style.display = "block";

		
	} else { 
		if (window.focus && newWindow) { newWindow.focus() }
	}
}

function checkForm(frm) {
	var emailValue = frm.email.value;
    var errorMessage = "";
    
	var regExEmail = new RegExp("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");
	
    if(emailValue == "" || emailValue == "E-mail Address") {
		errorMessage += "Please enter your e-mail address";
    } else if( ! emailValue.match(regExEmail)){
		errorMessage += "Your e-mail address is not valid";
	}
	
	if(errorMessage != ""){
		alert(errorMessage); return false;
	}else{
		return true;
	}
}
function checkIdForm(frm) {
	var fname = frm.first_name.value;
	var lname = frm.last_name.value;
	var cname = frm.company_name.value;
	var emailValue = frm.email_address.value;
	var bac = frm.bac_code.value;
    var errorMessage = "";
    
	var regExEmail = new RegExp("^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");
	var regExBac = new RegExp("^[0-9]$");
	
    if(emailValue == "" || emailValue == "Email Address") {
		errorMessage += "-Please enter your email address.\n";
    } else if( ! emailValue.match(regExEmail)){
		errorMessage += "-Your email address is not valid.\n";
	}
	
	if(fname == "") {
		errorMessage += "-Please enter your First Name.\n";
    } 
	
	if(lname == "") {
		errorMessage += "-Please enter your Last Name.\n";
    } 
	
	if(cname == "") {
		errorMessage += "-Please enter your Company Name.\n";
    } 
	
	if(bac == "") {
		errorMessage += "-Please enter your BAC Code.\n";
    }
	
	if(errorMessage != ""){
		alert(errorMessage); return false;
	}
}

function replaceAnchor(pageContent){	
 var finalContent,findTag,until,longTag,tagIni,tagEnd,len,strFirst; 
 if(pageContent.indexOf("<a") != -1)
 {
   tagIni = "<a";
   tagEnd = "</a>";
 }
 else
 {
    tagIni = "<A";
	tagEnd = "</A>";
 }  
	while(pageContent.indexOf(tagIni) != -1){
		finalContent="";
		len=pageContent.length;
		findTag = pageContent.indexOf(tagIni);	
		strFirst= pageContent.substring(findTag,len);	
		finalContent= finalContent+pageContent.substring(0,findTag);		
		until = strFirst.indexOf(">");	
		longTag=findTag+until+1;
		finalContent= finalContent+pageContent.substring(longTag,len);
		finalContent = finalContent.replace(tagEnd,"");	
		pageContent=finalContent;		
	}	
	return pageContent;
	
}