//Updated 6/5 for Testing
//Universal Window Function Type "yes" or "no" for scrollBar
  function flexWin(aU,aS,aW,aH,aN,aO,aX,aY,aC){
   var wN = aN ? aN : "myWin" + nocacheRandom();
   var oV = aO == "all" ? 1 : 0;
   var oS = "history="+oV+",toolbar="+oV+",location="+oV+",directories="+oV+",status="+oV+",menubar="+oV+",resizable="+oV;
   var wO = aO != "all" ? aO : oS;
   var wW = aW ? aW : 800;wO += ",width=" + wW;
   var wH = aH ? aH : 600;wO += ",height=" + wH;
   var sB = ",scrollbars="+aS;wO+=sB;
   var wX = aX ? aX :(screen.availWidth-wW)/2;wO+=",left="+wX+",screenX="+wX;
   var wY = aY ? aY :((screen.availHeight-wH)/2)-40;wO+=",top="+wY+",screenY="+wY;
   window.open(aU,wN,wO);
   aC ? window.close():"";
  }

//Universal image swap function
function swapImg(aIN, aIS, aDA){var dA = "";if(document.layers && aDA){for( var i = 0; i < aDA.length; i++ ){dA += 'document.' + aDA[i] + '.';}eval(dA + 'document.' + aIN + '.src ="' + aIS + '";');return true;}else{document[aIN].src = aIS;}}

//function to get msrp for model
/*
function getMSRP( argVehicle, argModel) {if( argModel != "" ) {for( i=0; i<models.length; i++ ) {if( models[i].vehicle == argVehicle && models[i].model == argModel && models[i].msrp != "" ) {return 'MSRP* starting at: $' + models[i].msrp; // do any additional formatting here.
} }return "Please see your dealer for pricing."; } else { var start; for( i=0; i<models.length; i++ ) {if( models[i].vehicle == argVehicle ) {start = i; break;} }var end;    for( i=models.length-1; i>=0; i-- ) { if( models[i].vehicle == argVehicle ) { end = i; break; } }if (models[i].msrp == ""){ if (argVehicle == "nitro"){ return "" } else {return "Please see your<br>dealer for pricing.";}} if( start != end ) { return "MSRP* starting at: $" + models[start].msrp;	} else { return "Please see your dealer for pricing."; } } }

//function getMSRP(aMI){for( i=0; i<models.length; i++ ){if(models[i].id == aMI){return 'Starting at $' + models[i].msrp + ' MSRP*';}}return "";}
*/

function getMSRP(argVehicle, argModel) {

    if( argModel!="") {
        for( i=0; i<models.length; i++ ) {
            if( models[i].vehicle == argVehicle && models[i].model == argModel && models[i].msrp != "" ) {

                if (models[i].is_reveal)
                    return "Pricing Available Later This Year";

                return 'MSRP* starting at: $' + models[i].msrp; // do any additional formatting here.
            }
        }
        return "Please see your dealer for pricing.";
    } else {
        var start=null, end=null, i=0;

        for( i=0; i<models.length; i++ ) {
            if( models[i].vehicle == argVehicle ) {
                if (start==null) {
                    start = i;
                } else {
                    end = i;
                }
            }
        }
        if (models[start].is_reveal)
            return "Pricing Available Later This Year";

        if (models[start].msrp == ""){
            return "Pricing available soon.";
        }
        if( start != end ) {
            return "MSRP* starting at: $" + models[start].msrp;
        } else {
            return "Pricing available soon.";
        }
    }
}

//function to get msrp ONLY for model
function getMSRPonly(aMI){for( i=0; i<models.length; i++ ){if(models[i].id == aMI){return  models[i].msrp;}}return "";}

//function to get starting @ price for family
function getStartingAtPrice(aV){for(i=0; i<models.length; i++){if(models[i].vehicle == aV){if(models[i].msrp == ""){return "";}else{return 'MSRP* starting at: $' + models[i].msrp;}}}}

//Universal function to take user to a page in main window
function targetMainWindow(aU){if(window.blur){self.blur();}if (window.opener.closed){window.open(aU,"newWin");}else{window.opener.location = aU;}}

//Universal function for no cache
function nocacheRandom(){var axel = Math.random() + "";var ord = axel * 1000000000000000000;return ord;}

//Generate random number within range passed in
function generateRandomNumber( argRange ) { return 1 + Math.round( Math.random() * ( argRange - 1 ) );}

//Universal function to get parameter from url srting
function getParameter(aP){var qS = new String(location.search.substring(1,location.search.length));var p = qS.split("&");var val = "";if(aP){for(i=0;i<p.length;i++){if(p[i].split( "=" )[0] == aP){val = p[i].split( "=" )[1];}}return val;}}

//Universal function to Print Page in all browsers including IE 4
var da = (document.all) ? 1 : 0;var pr = (window.print) ? 1 : 0;var mac = (navigator.userAgent.indexOf("Mac") != -1);

//Code for Flash tracking on sites prior to 3.7.05
function flashTracking(lid, pos, argPN){
  var curPN = hbx.pn +"+-+"+argPN;
  var newPN = curPN.replace(/\s/g,"+");
  if (argPN){
    _hbSet('n',newPN);
  }
  trackNamedHit(lid, pos);
}

// Flash link tracking code
function flashLinkTracking(argPN, argMLC, argLid, argLpos, argHEC){
  // Sets current page name.  if argPN has a value it will be added to hbx.pn which is set in the .html page.
  var curPN = (argPN=="")?hbx.pn:hbx.pn + "+-+" + argPN;
  // variables curPN | argLid | newLpos - allow only alphanumeric, spaces, "+" and hypens and then it strips out spaces and makes them "+".
  var newPN = curPN.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");
  var newLid = argLid.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");
  var newLpos = argLpos.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");
  var newMLC = (argMLC=="" || argMLC=="undefined")?hbx.mlc:argMLC;

  // Gateway variables are being set for WSS and then sent
    _hbSet('n', newPN);
    _hbSet('vcon', newMLC);
    _hbSet('lid', newLid);
    _hbSet('lpos', newLpos);
    _hbSet('hec', argHEC);
    _hbSend();
}

// Flash page view tracking
function flashPageTracking(argPN, argMLC, argGP){
  // Sets current page name.
  var curPN = hbx.pn +"+-+" + argPN;
 // curPN - allows only alphanumeric, spaces, "+" and hypens and then it strips out spaces and makes them "+".
  var newPN = curPN.replace(/[^a-zA-Z0-9,\s,+,-]/g,'').replace(/\s+/g,"+");
  var stem = location.protocol + '\/\/' + location.host + '\/' + argPN.toLowerCase();
  var newGP = argGP.toLowerCase();
  var newMLC = (argMLC=="" || argMLC=="undefined")?hbx.mlc:argMLC;

    // Gateway variables are being set for WSS and then sent
    _hbSet('n', newPN);
    _hbSet('vcon', newMLC);
   // custom variables
    _hbSet('cv.c21', stem);
    _hbSet('cv.c22', hbx.hc22);
  // The page is set as a goal page if "y" is passed to argGP
  if (newGP == "y"){
    _hbSet('gp','LAST');
    _hbSet('gn',argPN);
  }
  _hbSend();
}

//universal popup

var wrapMsg = "";
wrapMsg += 'The contents in the current "Build Your Own" window will be erased   \n';
wrapMsg += 'and a new session will be started. Click OK to continue and restart,\n';
wrapMsg += 'or Cancel to retain the contents of the window.';
var pandeWindow = null;
var universalWindow = null;

// universal popup
function wrap(argApp, argTracking, argFamily, argModel) {

  argApp = argApp.toLowerCase();
  var sizedWin = false;

  //if( argApp == "cdl" ) argApp = "dealer";

  var launchUrl = getDomain()+"/bridge/index.html?";
  if( argTracking != '' ) launchUrl += argTracking + "&";
  launchUrl += "app=" + argApp + "&family=" + argFamily + "&model=" + argModel;

  // argApp == "fulfillment" || (removed 9.1.05)
  if( argApp == "dealer" || argApp == "edmunds" || argApp == "gap" || argApp == "preownedinventory" ) {
    sizedWin = true;
    var winWidth = 618, winHeight = 452;
    if( argApp == "edmunds" || argApp == "gap" || argApp == "preownedinventory" ) {
      var winWidth = 790, winHeight = ( window.screen.availHeight - 130 );
    }
    var options = "resizable,scrollbars,toolbar,"
    options += "width=" + winWidth + ",height=" + winHeight;
    if( window.screen ) {
      var xPos = ( screen.availWidth - winWidth ) / 2;
      var yPos = ( ( screen.availHeight - winHeight ) / 2 ) - 40;
      options += ",left=" + xPos + ",screenX=" + xPos;
      options += ",top=" + yPos + ",screenY=" + yPos;
    }
  }
  if(sizedWin) {
    window.open( launchUrl, "universal", options );
  } else {
    window.location = launchUrl;
  }
}

function askWin(f) {
    if( f ) {
        f.target = "_self";
        if( f.ask.value == "Type question or keyword"   || 
		    f.ask.value == "Type a question or keyword" ||
		    f.ask.value == "Enter question or keyword"  || 
			f.ask.value == "Enter a question or keyword"   )
		{
			f.ask.value = ""; 
		} 
        if( f.ask.value.replace(/^\s+|\s+$/g,'') == "" ) {
            alert( "You must type a keyword in the space provided and then click \"Go\" to begin your search." );
            return false;
        } else {
            flashLinkTracking('', '', 'Search+Success', 'Search+Validation', 1);
            return true;
        }
    }
}

function validateZip( argF ) {
	var z = argF.zipcode;
	var re = new RegExp('^[0-9]{5}$');

	if( z = z.value )
		if( z.length == 5 )
			if( z.match( re ))
				return true;
	if( z == "Zip Code" || z == "ZIP Code" || z == "Enter Zip" || z == "Enter ZIP" || !z || z == "" || z.length == 0 )
		return true;
	alert( "Please check your zipcode for accuracy and try again." );
	return false;
}
