﻿var vsSelectorVehicle = new Object();
var vsSelectorAdVehicle = new Object();
var SelectListContainer = new Object();
var currentPage = new Object();
var gVhxUserAgentStr = navigator.userAgent.toLowerCase();
var gisGecko = gVhxUserAgentStr.indexOf("gecko") != -1;
var gisIE = gVhxUserAgentStr.indexOf("msie") != -1;
var gIEVersion = function(){
	var rv = -1;
	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(gVhxUserAgentStr) != null)
      rv = parseFloat( RegExp.$1 );
    return rv;
}
var gvhxAds = new Array();
var gvhxCompanionAds = new Array();
var gvhxRefreshing = true;
var gvhxProducts = '';
var gvx_VehicleStockImageTop;
var jsor;
setTimeout('gvhxRefreshing=false',4000);

function doAutoAdvance(o, num, next){
	if(o && o.value && o.value.length >= num)
		next.focus();
}

function newSpan()
{
	return document.createElement("span");
}

function newBr()
{
	return document.createElement("br");
}

function newTextNode(s)
{
	return document.createTextNode(s);
}

function newDiv()
{
	return document.createElement("div");
}

function newAnchor()
{
	return document.createElement("a");
}

function newImage()
{
	return document.createElement("img");
}

function processEnterKeyPress(e,sFunction,passEvent){
	if(!e.which){
		theKey = e.keyCode;
	}else{
		theKey = e.which;
	}
	if(theKey==13){
		if (e && e.preventDefault){
			e.preventDefault();
		}else{
			e.returnValue = false;
		}
		if(passEvent){
			return sFunction(e);
		}else{
			return sFunction();
		}
	}
}

function readCookie(name)
{
	var result;
	var re = new RegExp('(?:; )?' + name + '=([^;]*);?');
	if(re.test(document.cookie))
	{
		result = decodeURIComponent(RegExp['$1']);
	}
	return result;
}

function navigateFromGlobalSearchTo(e)
{	
	if(e)
	    var o = (typeof e == 'string') ? gO(e) : e.srcElement;
	if (o){
		if(validateZip(o.value)){
			var	url = location.protocol + "//" + location.hostname + gVhxSiteRoot + "_webServices/AJAXRequests.asmx/SetZipCookie";
			var	requestData = "zip=" + o.value;
			var	callBack = hideNavZipInput;
			MakeXMLHttpRequestCall(url, requestData, true, null, null, callBack, o.id);
		}
	}
}

function hideNavZipInput(s, id){
	var bZipPassed = s.object.zipvalid;
	var separator = '?';
	if(AspNetMenu__Loc.search(/\?/) != -1) separator = '&';
	
	if(bZipPassed){
		location.href = AspNetMenu__Loc + separator + 'zip=' + gO(id).value;
	}else{
		alert('Please enter a valid Zip Code.');
		gO('VX_Menu_ZipCode_Input').select();
		gO('VX_Menu_ZipCode_Input').focus();
	}
}

function navigateFromMenuWithZip(l){
	location.href = l + "&zip=" + vsSelectorZip;
}

function validateGlobalNavZip(e){
	var target;
	if(window.event){
		target = window.event.srcElement.id;
	}else{
		target = e.target.id;
	}
	if(validateZip(gO(target).value)){
		eval(gO(target + "_a").href.replace("javascript:", ""))
	}
}

function validateZip(zip){
	if(zip.search(/^\d{5}$/)==-1){
		alert('Please enter a valid Zip Code.');
		return false;
	}
	return true;
}

function showGlobalNavZipLayer(o){
	gO('nav_new_vsZipCode_div').style.display = 'none';
	gO('nav_used_vsZipCode_div').style.display = 'none';
	gO('nav_advanced_vsZipCode_div').style.display = 'none';
	gO(o).style.display = 'inline';
}

function gO(oID){
    return document.getElementById(oID)
}

function showData(id, data){
	try{
		document.getElementById(id).innerHTML = data;
	}catch(err){
	}
}

function $v(oID) {
  return document.getElementById(oID)
}

function getElementsByClass(searchClass,node,tag) {
    var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");	
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// Collapsable Article Container code.
function toggleArticle(link) {
    articleHead = link.parentNode;
    articleIntro = articleHead.nextSibling;
    articleBody = articleIntro.nextSibling;
                          
    switch (link.className) {
        case "btnShowArticle":
            link.className = 'btnHideArticle';
            articleHead.className = 'articleHeadOpen';
            articleIntro.style.display = 'none';
            articleBody.style.display = 'block';
            break;
        default:
            link.className = 'btnShowArticle';
            articleHead.className = 'articleHeadClosed';
            articleIntro.style.display = 'block';
            articleBody.style.display = 'none';
            break;
    }
}

// Popup Window
var aOpenWindows = new Array();

function OpenWindow(sURL, sWindowName, bToolbar, bScrollBars, lWidth, lHeight, lTop, lLeft) {
    if(typeof lTop == 'undefined') {
	    lTop = (screen.height - lHeight) / 2
    }
    if(typeof lLeft == 'undefined') {
	    lLeft = (screen.width - lWidth) / 2
    }
    if(bToolbar == true){
	    bToolbar = 'yes'
	    bMenubar = 'yes'
    }else{
	    bToolbar = 'no'
	    bMenubar = 'no'
    }
    if(bScrollBars == true){
	    bScrollBars = 'yes'
    }else{
	    bScrollBars = 'no'
    }
    var lsFeatures = 'toolbar='+bToolbar+',scrollbars='+bScrollBars+',resizable=yes,status=yes';	
    lsFeatures += ',top='+ lTop
    lsFeatures += ',left='+ lLeft
    if(lWidth > 0){lsFeatures += ',width='+lWidth};
    if(lHeight > 0){lsFeatures += ',height='+lHeight};
    lsFeatures += ',menubar=' + bMenubar;
    popupWin = window.open(sURL, sWindowName, lsFeatures);
    popupWin.focus();
}

function convertSingleQuote(s)
{
	if(s != undefined && s.length > 0)
		return s.replace("'", "\'");
}

function redirectVSsearchinventory(sicondition, apppath, pattern, token){
	var mycondition = (sicondition ? sicondition : vsSelectorVehicle.condition);
	var sloc = '';
	var rows = readCookie("vhxRowsPerPage");
	switch(mycondition){
		case "1":
			mycondition = "New";
			break;
		case "2":
			mycondition = "Used";
			break;
		case "4":
			mycondition = "CPO";
			break;
	}
	
	var aPattern = pattern.split("/");
	for(i = 0; i < aPattern.length; i++){
		sloc += "/";
		switch(aPattern[i]){
			case "make":
				sloc += encodeURIComponent(vsSelectorVehicle.makeName);
				break;
			case "smodel":
				sloc += encodeURIComponent(vsSelectorVehicle.modelName);
				break;
			case "model":
				sloc += encodeURIComponent(vsSelectorVehicle.modelName);
				break;
			case "condition":
				sloc += mycondition;
				break;
			case "zip":
				sloc += vsSelectorVehicle.zip;
				break;
			case "token":
				sloc += token;
				break;
			case "radius":
				sloc += vsSelectorVehicle.radius;
				break;
			case "rows":
				typeof(rows) == "undefined" ? sloc += "25" : sloc += rows;
				break;
			case "page":
				sloc += "1";
				break;
		}
	}
	sloc += "/";
	location.href= (apppath ? apppath : "") + sloc;
	return false;
}


function SYCOnClick_Click()
{
    if(gO('enterZipSYC'))
    {
        gO('enterZipSYC').style.display = 'block';
        if(gO('freeSellYourCarDiv'))
            gO('freeSellYourCarDiv').style.display = 'none';
        if(gO('sellYourCarDiv'))
            gO('sellYourCarDiv').style.display = 'none';
        if(gO('sellYourCarRegLink'))
            gO('sellYourCarRegLink').style.display = 'none';
    }
}

