
//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 swapfunction
function rollover(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;}}

//Universal image swapfunction
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;}}


///////////////////////MODEL FUNCTIONS/////////////////////////////////////////////////////

//function to get MPG for model
function getModelMPG(aMI){for( i=0; i<models.length; i++ ){if(models[i].id == aMI){if(models[i].mpg_city == ""){return '<i>See dealer for MPG.</i>';}else{return 'MPG: ' + models[i].mpg_city + ' city ' + '/ ' +  models[i].mpg_hwy +' hwy';}}}return "";}

//function to get HP for model
function getModelHp(aMI){for( i=0; i<models.length; i++ ){if(models[i].id == aMI){if(models[i].hp == ""){return '<i>See dealer for model horsepower.</i>';}else{return models[i].hp +' horsepower';}}}return "";}

//function to get HP for model
function getModelTorque(aMI){for( i=0; i<models.length; i++ ){if(models[i].id == aMI){if(models[i].torque == ""){return '<i>See dealer for model torque.</i>';}else{return models[i].torque +' lb�ft torque';}}}return "";}

//function to get msrp for model
function getModelMSRP(aMI){for( i=0; i<models.length; i++ ){if(models[i].id == aMI){if(models[i].msrp == ""){return '<i>Pricing available soon.</i>';}else{return 'MSRP* starting at: $' + 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;}}}}

//function to get starting @ price for  homepage family
function getStartingAtPriceHome(aV){for(i=0; i<models.length; i++){if(models[i].vehicle == aV){if(models[i].msrp == ""){return "";}else{return '$' + models[i].msrp;}}}}

			
///////////////////////OTHER FUNCTIONS/////////////////////////////////////////////////////

		
//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.
  //check if argPN is the same as the hbx.pn to avoid duplication
  var check_pn = "";
  var check_result;
  if(argPN != ""){
    for(i = 0; i < 6; i++){
      check_pn += argPN.charAt(i);
    }
    check_result = hbx.pn.indexOf(check_pn); 
    if(check_result >= 0){
      var curPN = hbx.pn;
    }else{
      var curPN = hbx.pn + "+-+" + argPN;
    }
  }else{
     var curPN = hbx.pn;
  }
  //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
// 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 on 8.25.05)
  //added vehiclespecs as temporary PDF redirection
  if( argApp == "vehiclespecs" || argApp == "dealer" || argApp == "edmunds" || argApp == "gap" || argApp == "preownedinventory" ) {
    sizedWin = true;
    var winWidth = 792, winHeight = 578;
    if( argApp == "edmunds" || argApp == "gap" || argApp == "preownedinventory" ) {
      var winWidth = 792, 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;
    }
  }
  //temporary logic for vehicle specs to PDF redirection
  if (argApp == "vehiclespecs") {
    if (argFamily == "dakota") {
		launchUrl = getDomain() + "/pdf/" + argFamily + "/model_info.pdf";
	} else {
	    sizedWin = false;
	}
  }
  //end temporary logic for PDF Specs redirection
  
  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;
        }
    }
}

var o1=0,o2=0,o3=0,o4=0,o5=0,o6=0,o7=0,o8=0,o9=0;

function vehicleDropOver( i, imgSrc ) { if( navOn ) {showE( "vehicleDrop" + i ); eval( "o" + i + " = 1" );}rollover( "top" + i, imgSrc );}

function vehicleDropOut( i, imgSrc ) { if( navOn ) {eval( "o" + i + " = 0" ); setTimeout( "if( !o" + i + " ) { hideE( 'vehicleDrop" + i + "' ); rollover( 'top" + i + "', '" + imgSrc + "' ); }", 10 );} else {rollover( "top" + i,  imgSrc ); } }

var dropClosed = 1, dropOver = 0;

function shopDrop() {if( shopOn ) { if( dropClosed ) { showE( "shopToolsDrop" ); dropClosed = 0;} else {hideE( "shopToolsDrop" ); dropClosed = 1;} } else { window.location = "/purchase/index.html"; } }

function shopDropOver() { if( shopOn ) {dropOver = 0; setTimeout( "if( !dropOver ) { hideE( 'shopToolsDrop' ); dropClosed = 1; }", 10 ); } }

// img tag creator - need for Route 2003
function imgTag( i, m, map ) {var iTag = '<img src="' + i + '"'; if( m != "" ) iTag += ' border="0" usemap="#' + m  + '"'; iTag += ' />'; if( m != "" ) iTag += map; return iTag;}

function isNumeric( d ) {
  var c;
  for( var i = 0; i < d.length; i++ ) {
    c = d.charAt( i );
    if(! ( c >= "0" && c <= "9" ) ) return false;
  }
  return true;

}

// exit window
function confirmNewWindow(newUrl){ window.open(newUrl, TARGET='_blank'); }

/*
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 "Pricing available soon.";}} if( start != end ) { return "MSRP* starting at: $" + models[start].msrp; } else {	if (argVehicle == "nitro"){ return "" } else {return "Pricing available soon.";} } } }
*/

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; break;
                }
            }
        }

        if (models[start].is_reveal)
            return "Pricing Available Later This Year";
            
        if (models[start].msrp == ""){ 
            if (argVehicle == "challenger") { 
                return "";
            } else {
                return "Pricing available soon.";
            }
        } 
        if( start != end ) { 
            return "MSRP* starting at: $" + models[start].msrp; 
        } else {
            if (argVehicle == "challenger") { 
                return "";
            } else {
                return "Pricing available soon."; 
            } 
        } 
    } 
}

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;
}

// Please note these list of arguments (except the 'tabName') are for the signature of current function  wrap (so, if you modify the wrap function, please modify the signation of 
 // callVehicleCompare function) accordingly 
 function callVehicleCompare(tabName, argApp, argTracking, argFamily, argModel) { 
     if((typeof(trilIsCompareSpec) == 'undefined') || (trilIsCompareSpec == null) || (typeof(changeTab) == 'undefined')) { 
             wrap(argApp, argTracking, argFamily, argModel); 
      } else { 
              changeTab(tabName); 
     } 
} 

