//
// Ensure userTrack is loaded
//
if (typeof(userTrack) == "undefined") {
    document.write('<' + 'script');
    document.write(' language="javascript"');
    document.write(' type="text/javascript"');
    document.write(' src="/edmunds/scripts/usertrack.js">');
    document.write('</' + 'script' + '>');
}

function goUrl(formName,selectName) {
        var form_info = eval("document." + formName+"."+selectName);
        var url = form_info.options[form_info.selectedIndex].value;
        if (url != '') {
                document.location.href=url;
        }
}



function popPffCalc(pffsrcid)
{
url = "http://applications.edmunds.com/servlets/PartnerLogServlet?url=http%3A%2F%2Fwww.peoplefirst.com%2Fdefault.cfm%3FPFFSRCID%3DSRED02CALCPOPTL*TL0003***&partner=PPLFIRST&contract=People+First+Referrals&source=PF.PRICE.CALC.T";
browserVer      = parseInt(navigator.appVersion);
if ( (browserVer >= 3) && (navigator.appName.indexOf("WebTV") == -1) )
{
        this.name       = "main";
        calcWindow      = window.open(url,'calcWin','toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,copyhistory=0,height=440,width=450');
        if (window.focus && (navigator.appVersion.indexOf("MSIE 4") < 0 ))
                setTimeout("calcWindow.focus()",10);
        return(false);
}
else
{
        return(true);
}

}

function popup(popupID) {
url = "http://www.edmunds.com/partners/peoplefirst/financingpopup.html?id=" + popupID;
newWindow = window.open(url,'popup','width=550,height=480,scrollbars=yes,menubar=yes,resizable')
}

function msgUnit(url,w,h) {
messageUnit = window.open(url,'SpecialInfo','width='+w+',height='+h+',scrollbars=no,menubar=no,resizable=no');
messageUnit.focus()
}

function checkEnter(event, formName)
{
        var code = 0;
        var formObject = eval("document." + formName);

        //alert("checkEnter:" + event.keyCode);

        if (event.which)
                code = event.which;
        else
                code = event.keyCode;
        if (code==13)
        {
                //alert("true");
                return true;
        }
        else
        {
                //alert("false");
                return false;
        }
}

function isBrowserOk()
{
        var agent=navigator.userAgent.toLowerCase();
        var is_aol   = (agent.indexOf("aol") != -1);
        var is_win   = ( (agent.indexOf("win")!=-1) || (agent.indexOf("16bit")!=-1) );
        //current restriction is no AOL on PC:
        if (is_aol && is_win) return false;

        return true;
}


var ADD_ZIPCODE = true;
var POPUP_WIDTH = "660";
var POPUP_HEIGHT = "480";

function popupCookiedWindow(cookieName, cookieValue, url, title, options, giveFocus){
        if (getCookieVal(cookieName) == cookieValue)
        {
                //don't do anything
                //they have all ready had a popup
        }
        else
        {
                //set the cookie
                //setCookieVal(cookieName,cookieValue,null,"/",".edmunds.com");
                if (giveFocus == null)
                        giveFocus = false;
                popupCenteredWindow(url,title,options,giveFocus);
        }

}

function popupCenteredWindow(url,title,options, giveFocus){
  var winLeft = (screen.width) / 2;
  var winUp = (screen.height) / 2;


  var winOpened = window.open(url,title,"left=" + winLeft + ",top=" + winUp + "," + options);
  if (giveFocus)
        winOpened.focus();
  else
        self.focus();
}

function popupWindow(url,title,options){
  //var winLeft = (screen.width - POPUP_WIDTH) / 2;
  //var winUp = (screen.height - POPUP_HEIGHT) / 2;


  //var winOpened = window.open(url,title,"left=" + winLeft + ",top=" + winUp + "," + options);
  var winOpened = window.open(url,title,options);
  winOpened.focus();
}

function powershopperPopupWindow(url,title,options){
  //popDLUnder(1);
  //if (dlPopupWindow)
  //{
  //            dlPopupWindow.focus();
  //            dlPopupWindow = null;
  //}
  popupWindow(url,title,options);
}

function powershopperPopupZipWindow(url,title,options, formObject){
  //popDLUnder(1);
  //if (dlPopupWindow)
  //    dlPopupWindow.focus();
  popupZipWindow(url,title,options, formObject);
}

function popupZipWindow(url,title,options, formObject){
 //get the form object's zip value

 var formObj = eval("document." + formObject);


 if (formObj)
 {
    if (formObj.zip)
   {


     if (!(url.lastIndexOf("&") == url.length))
     {
        popupWindow(url + "&zip="+formObj.zip.value,title,options);
    }
    else
    {
      popupWindow(url + "zip="+formObj.zip.value,title,options);
    }
  }
  else
  if (formObj.zipcode)
   {

     if (!(url.lastIndexOf("&") == url.length))
     {
        popupWindow(url + "&zipcode="+formObj.zipcode.value,title,options);
    }
    else
    {
      popupWindow(url + "zipcode="+formObj.zipcode.value,title,options);
    }
  }
 }
}



//
// Tries to set zip code field from cookie value if possible. This will set the
// values of any input fields on the html by the name of zip or zipcode.
// selects, hiddens, dropdowns, radios, checkboxes, and textareas are exlcuded.
//
/* Added global flag for whether or not a valid zip cookie exists */
var hasCookie = false;
function setZipFromCookie() {
  var fs = document.forms;
  var cookie = document.cookie;
  var cookieName = "zip";
  var cookieVal;
  // return false if cookie does not exist.
  if ( cookie == null || cookie.length < 1) return false;

  var len = cookie.length;
  // try to grab cookie val from cookie.
  for ( var i = 0; i < len ; i++ ) {
    var nameEndIndex = i + cookieName.length;
    // if cookie field with appropriate name is found, try to grab value.
    if ( cookie.substring( i, nameEndIndex ) == cookieName ) {
      var cookieEndIndex = cookie.indexOf(";", nameEndIndex );
      if ( cookieEndIndex == -1 ) cookieEndIndex = len;
      cookieVal = unescape(cookie.substring( nameEndIndex+1, cookieEndIndex ));
    }
  }
  // return false if cookie exists, but does not have zip value.
  if ( cookieVal == null || cookieVal.length < 1 ) {
    return false; // exit function
  }
else {
// else a valid cookie exists, so set the hasCookie flag
hasCookie = true;
}
  // iterate through all forms on this page.
  for ( var i = 0; i < fs.length; i++ ) {
    var f = fs[i];
    var es = f.elements;
    // look a for field named zip or zipcode. It must be either a text or number input.
    for ( var j = 0; j < es.length; j++ ) {
      var e = es[j];
      if ( e != null && (e.name.toLowerCase() == "zip" || e.name.toLowerCase() == "zipcode") ) {
      // set value, return true.
      e.value = cookieVal;
      }
    }
  }
  return true;
}


function getCookieVal(name) {
var fs = document.forms;
var cookie = document.cookie;
// default is "" if cookie does not exist.
var cookieVal = "";
if ( cookie == null || cookie.length < name ) return cookieVal;
var len = cookie.length;
// try to grab cookie val from cookie.
for ( var i = 0; i < len ; i++ ) {
var nameEndIndex = i + name.length;
// if cookie field with appropriate name is found, try to grab value.
if ( cookie.substring( i, nameEndIndex ) == name ) {
var cookieEndIndex = cookie.indexOf(";", nameEndIndex );
if ( cookieEndIndex == -1 ) cookieEndIndex = len;
cookieVal = unescape(cookie.substring( nameEndIndex+1, cookieEndIndex ));
}
}
// return false if cookie exists, but does not have zip value.
return cookieVal;
}

function setCookieVal(name, value, expires, path, domain) {

        var secure = false;
        document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");

}


<!--
var time = new Date();
var randnum= (time.getTime());
//-->


//for used
var buySell = 0;
function checkBuySell() {
if (buySell == 0)
  {
        //alert("Please click on the buy or sell button");
        return true;
  } else {
        return true;
  }
  return false;
}

/* Checks cookie now before setting upsAction */
function clickBuySell(val) {
  buySell = 1;

  //alert("buy and sell upsaction:" + document.UPS.upsAction);
// if a valid zip code cookie exists then post the upsaction field
  if (hasCookie) document.UPS.upsAction.value = val;
  document.UPS.submit();
}
/* Checks cookie now before setting upsAction */
function clickBuySell2(val) {
        buySell2 = 1;
        // if a valid zip code cookie exists then post the upsaction field
        if (hasCookie) document.UPS2.upsAction.value = val;
        document.UPS2.submit();
}

/* Checks cookie now before setting upsAction */
function clickBuySell3(val) {
  buySell3 = 1;
// if a valid zip code cookie exists then post the upsaction field
  if (hasCookie) document.UPS3.upsAction.value = val;
  document.UPS3.submit();
}
/* Checks cookie now before setting upsAction */
function clickBuySell4(val) {
        buySell4 = 1;
        // if a valid zip code cookie exists then post the upsaction field
        if (hasCookie) document.UPS4.upsAction.value = val;
        document.UPS4.submit();
}

function clickBuySellGeneric(val, f) {
buySell = 1;
  var dform=f;
  //alert("form:" + dform);
  //alert("form ups:" + dform.upsAction.value);

// if a valid zip code cookie exists then post the upsaction field
  if (hasCookie) dform.upsAction.value = val;
  //alert("got here");
  dform.submit();
}


 function isZipValid(zip, zipFormElement, alternateErrorMessage)
 {
         var errorMessage="Please enter a valid 5-digit Zip Code";
         if(alternateErrorMessage!=null) errorMessage= alternateErrorMessage;
         if(zip==null || isNaN(zip)|| zip.length!=5)
         {
                alert(errorMessage);
                if(zipFormElement!=null) zipFormElement.focus();
                return false;
         }
         return true;
 }

// the window will popup 1/[multiplier] times
// ie if multiplier=20, the window will popup 1/20th of the time:

function DLhandler(mult, styleid, zip, tid)
{
  if ( !isBrowserOk() )
    return;
  if ( getCookieVal("vpudlclose") == "true" )
    return;
  var dlParam = styleid + "_" + zip;
  if ( dlParam == getCookieVal("dlparam") )
    return;
  if ( Math.ceil(Math.random()*mult) != 1 )
    return;
  setCookieVal("dlparam", dlParam, null, "/", ".edmunds.com");
  var dlURL = "/apps/dl/dealerlocator.xct?dldisplay=stdalone&styleid=" + styleid + "&zip=" + zip + "&usein=n&setbcode=t&" + tid;
  var winLeft = (screen.width/2) - 350;;
  var winUp = (screen.height/2) - 250;
  var win = window.open(dlURL, "dlvdppopup", "width=610,height=625,location=no,toolbar=no,menubar=yes,scrollbars=yes,status=yes,left=" + winLeft + ",top=" + winUp+",resizable=yes");
  self.focus();
}

function dlPopUnder(page, styleid, zip, tid) {
    DLhandler(1,styleid, zip, tid);
    window.location = page;
}

function setTID(value){
//document.cookie="tid=" + escape(value);

var today = new Date();
var expdate = new Date(today.getTime() +  30000); //expired in 30 seconds

if (value.indexOf('nytimes') == 0){
document.cookie="tid=" + escape(value) + "; expires=" + expdate.toGMTString() + ";path=/;domain=.nytimes.com";
}else{
document.cookie="tid=" + escape(value) + "; expires=" + expdate.toGMTString() + ";path=/;domain=.edmunds.com";
}
 }

function getTID(){

var url = window.location.href;
var TIDfromQueryString =  getParameter(url, 'tid');

if(TIDfromQueryString == "null"){

var dc = document.cookie;
var prefix = "tid=";
var begin = dc.indexOf("; " + prefix);

    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return "";
    }
    else
    {
        begin += 2;
    }
var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }

    var TIDfromCookie = unescape(dc.substring(begin + prefix.length, end));

    // Delete cookie immediately after retrieving for purpose of avoiding receiving incorrect
    // TID value in case of calling previous page from browser’s address field.
    //document.cookie= "tid=" + escape("");
    //DeleteCookie("tid", TIDfromCookie);

    return TIDfromCookie;

    }else{
      return TIDfromQueryString;
    }
 }



function getParameter ( queryString, parameterName ) {

var parameterName = parameterName + "=";
if ( queryString.length > 0 ) {

begin = queryString.indexOf ( parameterName );

if ( begin != -1 ) {

begin += parameterName.length;

end = queryString.indexOf ( "&" , begin );
if ( end == -1 ) {
end = queryString.length
}

var cookieValue = queryString.substring ( begin, end );
//document.cookie= "tid=" + escape(cookieValue);
return unescape (cookieValue);
}

return "null";
}
}

function DeleteCookie (name, value) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);

if(name != null){
document.cookie = name + "=" + value + "; expires=" + exp.toGMTString();
 }
}
