

var _isIE, _isNS, _isDOM;

_isIE = (navigator.appName == "Microsoft Internet Explorer");
_isNS = (navigator.appName == "Mozilla" || navigator.appName == "Netscape");
_isDOM = _isNS && (navigator.appVersion.indexOf("4.") == -1);

var allObjs = new Array();


function dom_object(obj) {
  this.docObj = obj;
  this.name = obj.id;
  this.hide = domHide;
  this.show = domShow;
  this.getHeight = domGetHeight;
  this.setTop = domSetTop;
  this.setLeft = domSetLeft;
  this.setInnerHTML = domSetInnerHTML;
  this.unDisplay = domUnDisplay;
  this.displayOff = domDisplayOff;
}


function ie_object(obj) {
  this.docObj = obj;
  this.name = obj.id;
  this.hide = domHide;
  this.show = domShow;
  this.getHeight = ieGetHeight;
  this.setTop = domSetTop;
  this.setLeft = domSetLeft;
  this.setInnerHTML = domSetInnerHTML;
  this.unDisplay = domUnDisplay;
  this.displayOff = domDisplayOff;
}

function ns_object(obj) {
  this.docObj = obj;
  this.name = obj.name;
  this.hide = nsHide;
  this.show = nsShow;
  this.setInnerHTML = nsSetInnerHTML;
  this.getHeight = nsGetHeight;
  this.setTop = nsSetTop;
  this.setLeft = nsSetLeft;
  this.unDisplay = nsHide;
  this.displayOff = nsDisplayOff;
}

function ie_image(obj) {
  this.imgObj = obj;
  this.getTop = ieGetImgTop;
  this.getLeft = ieGetImgLeft;
}

function ns_image(obj) {
  this.imgObj = obj;
  this.getTop = nsGetImgTop;
  this.getLeft = nsGetImgLeft;
}

function ieGetImgTop() { return parseInt(this.imgObj.offsetTop); }
function ieGetImgLeft() { return parseInt(this.imgObj.offsetLeft); }
function ieGetHeight() { return parseInt(this.docObj.clientHeight); }
function domHide() { this.docObj.style.visibility = "hidden"; }
function domShow() { this.docObj.style.visibility = "visible"; }
function domSetInnerHTML(s) { this.docObj.innerHTML = s; }
function domGetHeight() { return parseInt(document.defaultView.getComputedStyle(this.docObj, "").getPropertyValue("height")); }
function domUnDisplay() { this.docObj.style.display = 'none'; }
function domSetLeft(x) { this.docObj.style.left = x + "px"; }
function domSetTop (y) { this.docObj.style.top = y + "px"; }
function domDisplayOff() { this.docObj.style.display = "none"; }

function nsGetImgTop() { return parseInt(this.imgObj.y); }
function nsGetImgLeft() { return parseInt(this.imgObj.x); }
function nsHide() { this.docObj.visibility = "hidden"; }
function nsShow() { this.docObj.visibility = "inherit"; }
function nsGetHeight() { return this.docObj.document.height; }
function nsSetTop(y) { this.docObj.top = y; }
function nsSetLeft(x) { this.docObj.left = x; }
function nsUnDisplay() { this.docObj.clip.left = 0; this.docObj.clip.right = 0; this.docObj.clip.bottom = 0; this.docObj.clip.top = 0; }
function nsDisplayOff() { this.docObj.display = "none"; }

function nsSetInnerHTML(s) {
  this.docObj.document.clear();
  this.docObj.document.write(s);
  this.docObj.document.close();
}

function findNSLayer(objName) {
   for (i = 0; i < document.layers.length; i++) {
     if (document.layers[i].name == objName) return document.layers[i];
   }
}

// Note: images not in the same DIV/doc are not
// currently supported under Netscape
function getImgObj(objName) {
  if (allObjs[objName]) return allObjs[objName];
  var theObj = null;
  if (_isIE || _isDOM) {
    if (!document.images[objName]) {
      alert("IE cannot find image " + objName);
      return null;
    }
    theObj = new ie_image(document.images[objName]);
  } else if (_isNS) {
    if (!document.images[objName]) {
      alert("NS cannot find image " + objName);
      // If required, add sub-doc search here
    }
    theObj = new ns_image(document.images[objName]);
  }
  allObjs[objName] = theObj;
  return theObj;
}

function getDocObj(objName) {
  if (allObjs[objName]) return allObjs[objName];
  var theObj = null;
  if (_isIE) {
    if (!document.all[objName]) {
      return null;
    }
    theObj = new ie_object(document.all[objName]);
  } else if (_isDOM) {
    if (!document.getElementById(objName)) {
      return null;
    }
    theObj = new dom_object(document.getElementById(objName));
  } else if (_isNS) {
    var nsLayer = document.layers[objName];
    if (!nsLayer) {
      nsLayer = findNSLayer(objName);
      if (!nsLayer) {
        return null;
      }
    }
    theObj = new ns_object(nsLayer);
  }
  allObjs[objName] = theObj;
  return theObj;
}

function place(o,vehID,xPos,yOffset) {
  var y = 0;
  if (ns4) { y = eval("document." + vehID).pageY; }
  if (ns6) { y = document.getElementsByName(vehID).item(0).offsetTop; }
  if (ie4) {y=event.y;}
  if (ie5) {y=event.y+self.document.body.scrollTop;}
  y += yOffset;
  o.setLeft(xPos);
  o.setTop(y);
}

var firstCompareSelection = null;
function vehicleCompare(vehID, vehA, vehB) {
  // we have two vehicles specified to compare
  if(null!=vehA && null!=vehB) {
    if(vehA!=vehB) {
      showComparePopup(vehA, vehB);
    }
    hideCompareInfo();
    firstCompareSelection = null;
    return false;
  }
  // choosing first or second vehicle
  else if(null!=vehA && null==vehB) {
    if(null == firstCompareSelection) {
      // this is the first vehicle choice
      firstCompareSelection = vehA;
      showCompareInfo(vehID);
      return false;
    } else {
      return vehicleCompare(vehID, firstCompareSelection, vehA);
    }
  }
  else return false;
}

function showCompareInfo(vehID) {
  var compareBGDiv = getDocObj("comparebg");
  place(compareBGDiv,vehID,437,-117);

  var compareDiv = getDocObj("compareDiv");
  place(compareDiv,vehID,437,-117);

  compareBGDiv.show();
  compareDiv.show();
}
function hideCompareInfo() {
  var compareDiv = getDocObj("compareDiv");
  var compareBGDiv = getDocObj("comparebg");

  compareDiv.hide();
  compareBGDiv.hide();
}
function displayOff(e) {
 getDocObj(e).displayOff();
}

function show(divName) {
  getDocObj(divName).show();
}

function hide(divName) {
  getDocObj(divName).hide();
}

function formatPercentage(input) {
  var v_regexp = new RegExp("%$","");  // end in a percent sign
  if (!v_regexp.test(input)) {
      input += "%";  //append a percent symbol
  }
  return input;
}

function formatCurrency(input) {
  var v_regexp = new RegExp("^\\$","");  // begin with $ symbol
  if (!v_regexp.test(input)) {
    if ("" == input) {
      input = "0";
    }
    input = "$" + input;  // add $ symbol
  }
  return input;
}

function getOptionValue(obj) {
  return obj.options[obj.selectedIndex].text;
}

// function called from the mouseover event for the headers
function onMouseOver(obj, url) {
        obj.src = url;
}
    
// function called from the mouseout event for the headers
function onMouseOut(obj, url) {
        obj.src = url;
}

function submitLoanLeaseForm() {
//alert('in submit loanleaseform');
  if(opener && opener.document.mainform && opener.document.mainform.pr) {
    opener.document.mainform.pr.value = formatCurrency(document.mainform.pr.value);
    opener.document.mainform.ti.value = formatCurrency(document.mainform.ti.value);
    opener.document.mainform.dp.value = formatCurrency(document.mainform.dp.value);
    opener.document.mainform.tm.value = getOptionValue(document.mainform.tm);
    opener.document.mainform.ir.value = formatPercentage(document.mainform.ir.value);
    opener.document.mainform.lpr.value = formatCurrency(document.mainform.lpr.value);
    opener.document.mainform.lti.value = formatCurrency(document.mainform.lti.value);
    opener.document.mainform.ldp.value = formatCurrency(document.mainform.ldp.value);
    opener.document.mainform.ltm.value = getOptionValue(document.mainform.ltm);
    opener.document.mainform.lty.value = getOptionValue(document.mainform.lty);
    opener.configClick('clearNotable','true');
  }
}

function submitLoanLeaseInfiForm() {
//alert('in infi form');
//alert(opener.document.mainform.pr);
// if(opener && opener.document.mainform && opener.document.mainform.pr) {
    opener.document.mainform.ir.value = formatPercentage(document.mainform.ir.value);
    opener.document.mainform.tm.value = getOptionValue(document.mainform.tm);
    opener.document.mainform.dp.value = formatCurrency(document.mainform.dp.value);
    opener.document.mainform.ti.value = formatCurrency(document.mainform.ti.value);
    opener.document.mainform.pr.value = formatCurrency(document.mainform.pr.value);
//alert('done');
    opener.configClick('clearNotable','true');
 // }
}

function submitLoanLeaseInfiFormOnEnter() {
  if(window.event.keyCode == 13) {
    submitLoanLeaseInfiForm();
  }
}


function submitLoanLeaseFormOnEnter() {
  if(window.event.keyCode == 13) {
    submitLoanLeaseForm();
  }
}
function reportLoanLeasePopupOpen() {
  if(opener && opener.document && opener.document.mainform && opener.document.mainform.lc) {
    opener.document.mainform.lc.value='false';
  }
}
function reportLoanLeasePopupClose() {
  if(opener && opener.document && opener.document.mainform && opener.document.mainform.lc) {
    opener.document.mainform.lc.value='true';
  }
}

function iconLinkUrl(page, args, doScroll) {
  var link;
  link = page;

  link += "?state_token=" + escape(window.document.mainform.state_token.value);
  link += "&lc=" + escape(window.document.mainform.lc.value);
  if(args) {
     link += "&" + args;
  }
  if(doScroll) {
     var offset = getScrollPos();
     if (offset > 0) { link += ("&_scrollPos=" + offset); }
  }
  return link;
}

function iconLink(page, args, doScroll) {
	var str="var dealer=dealerInfo.dealer0";
	eval(str);
	if(dealer!=null)
	{
		args+="&sdealerName="+dealer[6].value+"&sdealerRadius="+dealer[16].value+"&sdealerState="+dealer[9].value+"&sdealerCity="+dealer[8].value+"&sdealerAddress="+dealer[7].value+"&sdealerZip="+dealer[10].value+"&sdealerTel="+dealer[12].value+"&sdealerContactable="+dealer[22].value+"&sdealerID="+dealer[3].value;
	}
  document.location.href = iconLinkUrl(page, args, doScroll);
}

function doCheckPhoneEmail()
{
    var abort = false;

    if (document.mainform.emailAnnouncements_value.checked && document.mainform.email.value == "")
    {
        abort = true;
        alert("Please enter a valid email address if you would like to receive future product news and announcements electronically.");
    }
    if (document.mainform.phoneAnnouncements_value.checked && document.mainform.phone1.value == "")
    {
        abort = true;
        alert("Please enter a valid phone number if you would like to be contacted by phone to hear about our products.");
    }

    return !abort;
}

function submitForm()
{
    if (doCheckPhoneEmail())
        doAction('submitUserInfo');
}

function submitLead()
{
    if(doCheckPhoneEmail())
        doAction('submitLead');
}


// BEGIN CSS STYLE DEFINITION CODE
// Includes the default, NS and Mac stylesheets depending on the user's browser

// TODO: set up mac and ns_mac stylesheets


var startString = '<link rel="stylesheet" href="/css/tril';
var styleString = startString;
var endString = '.css" type="text/css">';

if (ns4) styleString = startString + '_ns';
//if (is.mac) styleString = styleString + '_mac';

styleString = styleString + endString;
document.write(styleString);

// END CSS STYLE DEFINITION CODE
