/*********************************************************************************************************
** Default Javascript for Client sites
*********************************************************************************************************/
var SelectedNav = null;
var Models = new Array();
var InvPhoto = new Array();
var PhotoWindow = null;
var ia = new Array();
var ia_on = new Array();
var bVer  = parseInt(navigator.appVersion);
var bNS = navigator.appName=="Netscape";
var bIE = navigator.appName=="Microsoft Internet Explorer";

function Overload(img, src) {
	if (img.name=="") {
		index = ia.length;
		img.name = index;
		ia[index] = new Image;
		ia[index].src = img.src;
		ia_on[index] = new Image;
		ia_on[index].src = src;
	}
}

function ImgSwap(img, over) {
	img.src = (over==1)?ia_on[img.name].src:ia[img.name].src;
}

function HighlightRow(row, num, vid) {
	for (i=0; i<row.cells.length; i++) {
	  if (row.cells[i].className.indexOf("Row_Sel") == -1) row.cells[i].className += " Row_Sel";
	}
	if (div = getId("divPhotoPreview")) {
		div.style.top = row.offsetTop - 10;
		getId("imgPhotoPreview").src = InvPhoto[num].src;
		getId("PreviewLink").href = "VD.html?VID="+vid;
		div.style.display = "inline";
	}
}
function RestoreRow(row) {
  for (i=0; i<row.cells.length; i++) {
	  if ((pos = row.cells[i].className.indexOf("Row_Sel")) > -1) {
	    row.cells[i].className = row.cells[i].className.substr(0,pos);
	  }
  }
}
function PageLoad(pageid) {
  try { parent.NavFrame.SelectNav(pageid); } catch(e) { }
  if (pageid=="CS") {
    try { SelectMake(); } catch(e) { }  
  }
}
function SelectNav(pageid) {
	ClearNav(SelectedNav);
	obj = getId("Nav_"+pageid);
	obj.className = obj.className + "_Sel";
	SelectedNav = pageid;
}
function ClearNav(pageid) {
	if (SelectedNav==null) { SelectedNav = "HM"; }
	obj = getId("Nav_"+SelectedNav);
	cn = obj.className;
	if (cn.indexOf("_Sel")>0) cn = cn.substr(0,cn.indexOf("_Sel"));
	obj.className = cn;
}
function SelectInvLot(val) {
  if (val=="") {
    document.location.href = Homepage+InvPage+".html"+((Framed)?"?F=C":"");
  } else {
    document.location.href = Homepage+"L"+val+".html"+((Framed)?"?F=C":"");
  }
}
function SelectInvYear(ind) {
  getId("listYear").selectedIndex = ind;
  getId("ChangeDisplay").value="1";
  getId("MainForm").submit();
}
function SelectInvMake(ind) {
  getId("listMake").selectedIndex = ind;
	if (getId("listMake").selectedIndex > 0) { getId("listModel").selectedIndex = 0; }
  getId("ChangeDisplay").value="1";
	getId("MainForm").submit();
}
function SelectInvModel(ind) {
  getId("listModel").selectedIndex = ind;
	if (getId("listModel").selectedIndex > 0) { getId("listMake").selectedIndex = 0; }
  getId("ChangeDisplay").value="1";
	getId("MainForm").submit();
}
function SetInvInd(ind) {
  getId("ListIndex").value=ind;
	getId("MainForm").submit();
}
function SelectSortBy() { getId("MainForm").submit(); }
function ViewPhotos(vid) { PhotoWindow = window.open(AM_ClientURL + "Scripts/VehiclePhotos.aspx?VID="+vid+"&PID=1","AM_PhotoForm","height=562, width=662, location=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no"); }
function SelectMinYear() {
	minyear = getId("listMinYear"); maxyear = getId("listMaxYear");
	if (minyear.selectedIndex > 0 && maxyear.selectedIndex > 0) { if (minyear.selectedIndex < maxyear.selectedIndex) { maxyear.selectedIndex = minyear.selectedIndex; } }
}
function SelectMaxYear() {
	minyear = getId("listMinYear"); maxyear = getId("listMaxYear");
	if (minyear.selectedIndex > 0 && maxyear.selectedIndex > 0) { if (maxyear.selectedIndex > minyear.selectedIndex) { minyear.selectedIndex = maxyear.selectedIndex; } }
}
function SelectMake() {
	make = getId("listSearchMake").value;
	list = getId("listSearchModel");
	while (list.length > 1) { list.remove(1); }
	for (i=1, valid=true; valid; i++) {
		if (Models[make+i]) {
			opt = document.createElement("OPTION");
			list.add(opt);
			opt.text = Models[make+i];
			opt.value = Models[make+i];
		} else { valid = false; }
	}
  if (getId("SelectedModel").value!="") list.value = getId("SelectedModel").value;
}
function SelectMinPrice() {
	minprice = getId("listMinPrice"); maxprice = getId("listMaxPrice");
	if (minprice.selectedIndex > 0 && maxprice.selectedIndex > 0) { if (minprice.selectedIndex > maxprice.selectedIndex) { maxprice.selectedIndex = minprice.selectedIndex + 1; if (maxprice.selectedIndex == -1) maxprice.selectedIndex = 0; } }
}
function SelectMaxPrice() {
	minprice = getId("listMinPrice"); maxprice = getId("listMaxPrice");
	if (minprice.selectedIndex > 0 && maxprice.selectedIndex > 0) { if (maxprice.selectedIndex < minprice.selectedIndex) { minprice.selectedIndex = maxprice.selectedIndex - 1; } }
}
function SelectMinMiles() {
	minmiles = getId("listMinMiles"); maxmiles = getId("listMaxMiles");
	if (minmiles.selectedIndex > 0 && maxmiles.selectedIndex > 0) { if (minmiles.selectedIndex > maxmiles.selectedIndex) { maxmiles.selectedIndex = minmiles.selectedIndex + 1; if (maxmiles.selectedIndex == -1) maxmiles.selectedIndex = 0; } }
}
function SelectMaxMiles() {
	minmiles = getId("listMinMiles"); maxmiles = getId("listMaxMiles");
	if (minmiles.selectedIndex > 0 && maxmiles.selectedIndex > 0) { if (maxmiles.selectedIndex < minmiles.selectedIndex) { minmiles.selectedIndex = maxmiles.selectedIndex - 1; } }
}
function CheckContactUs() {
	if (document.forms[0].txtFirstName.value=="") {
		alert("Please enter your First Name.");
		document.forms[0].txtFirstName.focus();
		return false;
	}
	if (document.forms[0].txtLastName.value=="") {
		alert("Please enter your Last Name.");
		document.forms[0].txtLastName.focus();
		return false;
	}
	if (document.forms[0].txtPhone1.value=="" && document.forms[0].txtPhone2.value=="" && document.forms[0].txtEmailAddress.value=="") {
		alert("Please enter a valid Phone Number or E-mail Address.");
		document.forms[0].txtEmailAddress.focus();
		return false;
	}
	return true;
}
function LaunchCreditApp(mode) {
  window.open(AM_ClientURL.replace(/http:/g, "https:") + "Scripts/CreditApplication.aspx?ClientID=" + AM_ClientID + "&Form=" + mode);
}

function CheckPhone(field) {
	num = field.value;
  num = num.replace(/[^0-9\(\) -x]+/g, "");
  field.value = num;
	num = num.replace(/[^0-9]+/g, "");
  if (num.length==10) FormatPhone(field);
}
function FormatPhone(field) {
	num = field.value;
	num = num.replace(/[^0-9\(\) -]+/g, "");
	if (field.value!= num) { field.value = num; return; }
	if (num.length>14) { field.value = num.substring(0,14); return; }
	
	if (num.match(/\(\d{3}\) \d{3}-\d{4}/) != null) return;
	num = num.replace(/[^0-9]+/g, "");
	if (num.length==11 && num.charAt(0)=="1") num = num.substring(1,11);
	if (num.length==10) {
	  num = "(" + num.substring(0,3) + ") " + num.substring(3,6) + "-" + num.substring(6,10);
		field.value = num;
	}
}

function ValidEmail(address) {
  return address.match(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/g);
}

function NRC(e) {
  if (bNS && e.which > 1){
    alert("Right-click is disabled.");
    return false;
  } else if (bIE && (event.button >1)) {
    alert("Right-click is disabled.");
    return false;
  }
}

if (AM_Env!="Development") {
  if (document.layers) {
    document.captureEvents(Event.MOUSEDOWN);
    document.onmousedown = NRC;
  } else if (bIE) {
    document.onmousedown = NRC;
  }
  document.oncontextmenu = NRC;
}

function getId(id) {
  C1 = document.getElementById(id);
  if (C1!=null) return C1;
  C2 = document.getElementById(ControlID+id);
  if (C2!=null) return C2;
  return null;
}
