function SaveVehicleClassifieds(id){
  GetURL('/myHq/savedVehicle_save.cfm?id='+id,'processClassifiedsSaveVehicle');
}

function processClassifiedsSaveVehicle(){
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
          response=req.responseXML.documentElement;
		  if(response.getElementsByTagName('errors')[0].firstChild.data==0){
            var image_id = response.getElementsByTagName('image_id')[0].firstChild.data;
			eval("document.SaveVehicleImage_"+image_id+".src=response.getElementsByTagName('SaveVehicleImage')[0].firstChild.data");
			eval("document.SaveVehicleImage_"+image_id+".title=response.getElementsByTagName('action')[0].firstChild.data");
		  }
		  else{
		    alert(response.getElementsByTagName('errors')[0].firstChild.data);
		  }
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function SaveVehicleSearch(id){
  GetURL('/myHq/savedVehicle_search.cfm?id='+id,'processReqSaveVehicleSearch');
}

function processReqSaveVehicleSearch(){
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
          response=req.responseXML.documentElement;
		  if(response.getElementsByTagName('errors')[0].firstChild.data==0){
            var image_id = response.getElementsByTagName('image_id')[0].firstChild.data;
 			eval("document.SaveVehicleImage_"+image_id+".src=response.getElementsByTagName('SaveVehicleImage')[0].firstChild.data");
			eval("document.SaveVehicleImage_"+image_id+".title=response.getElementsByTagName('action')[0].firstChild.data");
			eval("document.getElementById('imgResponse_"+image_id+"').childNodes[0].data=response.getElementsByTagName('action')[0].firstChild.data");
		  }
		  else{
		    alert(response.getElementsByTagName('errors')[0].firstChild.data);
		  }
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function SaveVehicle(id){
  GetURL('/myHq/savedVehicle_save.cfm?id='+id,'processReqSaveVehicle');
}

function processReqSaveVehicle(){
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
          response=req.responseXML.documentElement;
		  if(response.getElementsByTagName('errors')[0].firstChild.data==0){
            var image_id = response.getElementsByTagName('image_id')[0].firstChild.data;
			eval("document.SaveVehicleImage_"+image_id+".src=response.getElementsByTagName('SaveVehicleImage')[0].firstChild.data");
			eval("document.SaveVehicleImage_"+image_id+".title=response.getElementsByTagName('action')[0].firstChild.data");
		  }
		  else{
		    alert(response.getElementsByTagName('errors')[0].firstChild.data);
		  }
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function updateview(status,value){
  if(document.search_resubmit.length==2){
	form=document.search_resubmit[0];
  }
  else{
	form=document.search_resubmit;
  }
  currentview=form.view.value;

  if(status==true){
	currentview=currentview+','+value;
  }
  else{
	newview="";
	currentviwsplit=currentview.split(",");

	for(i=0;i<currentviwsplit.length;i++){
		if(currentviwsplit[i]!=value){
			newview=newview+','+currentviwsplit[i];
		}
	}
	currentview=newview;
  }

  if(document.viewselected.length==2){
	document.viewselected[0].view.value=currentview;
  }
  else{
	document.viewselected.view.value=currentview;
  }

  form.view.value=currentview;
}

//checks for numbers only
	function CheckNumber(ctrl, type)
	{
		
		if(type=="dbl")
		{
			var checkOK = "0123456789";
		}
		else
		{
			var checkOK = "0123456789";
		}
		
		var checkStr = ctrl.value;
		var allValid = true;
		var decPoints = 0;
		var allNum = "";
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			if (ch == "."){
				decPoints++ ;
				}
			if (decPoints > 1){
				allValid = false;
				}
				
			for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
			allValid = false;
			break;
			}
			allNum += ch;
		}
		
		return allValid;
	}

// declare variables to work in IE
var result='';
var response='';
var button='';

function popUp(URL,w,h,name) {
	newWindow = window.open(URL,name, "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,height="+h+",width="+w+",left=10,top=10");
	newWindow.focus();
}

function popUpResize(URL,w,h,name) {
	newWindow = window.open(URL,name, "toolbar=1,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,height="+h+",width="+w+",left=350,top=200");
	newWindow.focus();
}

function printPage() {
	window.print();  
}

function showPic (x) {
	if (document.getElementById) {
		document.getElementById('largeImage').src = x.href;
			if (x.title) {
				document.getElementById('desc').childNodes[0].nodeValue = x.title;
			} 
			else {
				document.getElementById('desc').childNodes[0].nodeValue = x.childNodes[0].nodeValue;
			}
		return false;
	} else {
		return true;
	}
}

function toggleDiv(x) {
	var myelement = document.getElementById(x);
	//var myicon = document.getElementById(el + "-icon");
	if( !myelement.style.display || myelement.style.display == "none" ) {
		myelement.style.display = "block";
		//myicon.src = "return.gif";
	} else {
		myelement.style.display = "none";
		//myicon.src = "search.gif";
	}
}

function show(x) {
	var myelement = document.getElementById(x);
	myelement.style.display = "block";
}

function hide(x) {
	var myelement = document.getElementById(x);
	myelement.style.display = "none";
}

function checkIt(x) { 
	if (document.getElementById(x).checked == true)  {
		document.getElementById(x).checked = false;
	}
	else if (document.getElementById(x).checked == false) { 
		document.getElementById(x).checked = true; 
	} 
}

function changeClass(x, y) {
	result=document.getElementById(x);
	result.className=y;
}

/* function toggle for showing and hiding options */

function Toggle(item) {
	obj=document.getElementById(item);
	visible=(obj.style.display!="none")
	key=document.getElementById("x" + item);
	if (visible) {
		obj.style.display="none";
		key.innerHTML="<img src='http://www.carshq.com/gfx/plus.gif' border='0' WIDTH='12' HEIGHT='12' title='Click to [+] to show hidden contents or [-] to hide contents'>";
	} 
	else {
		obj.style.display="block";
		key.innerHTML="<img src='http://www.carshq.com/gfx/minus.gif' border='0' WIDTH='12' HEIGHT='12' title='Click to [+] to show hidden contents or [-] to hide contents'>";
	}
}

/* end function toggle for showing and hiding options */

/*
function modifyText(id, text) {
	if (document.getElementById) {
		obj = document.getElementById(id);
		obj.childNodes[0].data = text;
	}
}
*/

var req;
function GetURL(url,handler){
    if(handler==undefined){handler='processReqChange';}
    url=url+'&rand='+Math.random();
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange=eval(handler);
        req.open("GET",url,true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange=eval(handler);
            req.open("GET",url,true);
            req.send();
        }
    }
}

function  processReqChange() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
          response=req.responseXML.documentElement;
		  if(response.getElementsByTagName('errors')[0].firstChild.data==0){
		    if(button!=''){document.getElementById(button).disabled=true;}
			if(response.getElementsByTagName('id')[0]&&document.getElementById('id')){
				document.getElementById('id').value=response.getElementsByTagName('id')[0].firstChild.data;}
			//document.getElementById('info_sent').childNodes[0].data='Your update was successful.';
			//document.getElementById('info_sent').style.display = "block";
		  }
		  else{
		    alert(response.getElementsByTagName('errors')[0].firstChild.data);
		  }
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function LoadModels(path,secure){
	if(path==undefined) path='';
  document.getElementById("model").options.length=0;

    GetURL('/includes/xml/models.cfm?make_id='+document.getElementById("make").options[document.getElementById("make").selectedIndex].value,'processReqChangeLoadModels');

}
function processReqChangeLoadModels(){   
  // only if req shows "complete"
  if (req.readyState == 4){
      // only if "OK"
      if (req.status == 200){
        response=req.responseXML.documentElement;
        if(response.getElementsByTagName('errors')[0].firstChild.data==0){
  		  document.getElementById("model").options[0]=new Option('','');
		  if(response.getElementsByTagName('id')){
  		    for(i=0; i<response.getElementsByTagName('id').length; i++){
		      document.getElementById("model").options[document.getElementById("model").options.length]=new Option(unescape(response.getElementsByTagName('model')[i].firstChild.data),response.getElementsByTagName('id')[i].firstChild.data);
		    }
		  }
		}
	    else{
	      alert(response.getElementsByTagName('errors')[0].firstChild.data);
	    }
      } 
	  else{
        alert("There was a problem retrieving the XML data:\n" + req.statusText);
      }
  }
}

// the following is for displaying the dealers that are within radius search of the zipcode given 

function LoadDealers(path,secure){
	if(path==undefined) path='';
  document.getElementById("dealer_id").options.length=0;
  if(secure==1){
    GetURL('https://secure.carshq.com/includes/xml/dealers.cfm?radius='+document.getElementById("radius_dealer").value+'&zip='+document.getElementById("zip_dealer").value,'processReqChangeLoadDealers');
  }
  else{
    GetURL('http://www.carshq.com/includes/xml/dealers.cfm?radius='+document.getElementById("radius_dealer").value+'&zip='+document.getElementById("zip_dealer").value,'processReqChangeLoadDealers');
	  
  }
}
function processReqChangeLoadDealers(){   
  // only if req shows "complete"
  if (req.readyState == 4){
      // only if "OK"
      if (req.status == 200){
        response=req.responseXML.documentElement;
        if(response.getElementsByTagName('errors')[0].firstChild.data==0){
  		  document.getElementById("dealer_id").options[0]=new Option('','');
		  if(response.getElementsByTagName('id')){
  		    for(i=0; i<response.getElementsByTagName('id').length; i++){
		      document.getElementById("dealer_id").options[document.getElementById("dealer_id").options.length]=new Option(unescape(response.getElementsByTagName('dealer_id')[i].firstChild.data),response.getElementsByTagName('id')[i].firstChild.data);
		    }
		  }
		}
	    else{
	      alert(response.getElementsByTagName('errors')[0].firstChild.data);
	    }
      } 
	  else{
        alert("There was a problem retrieving the XML data:\n" + req.statusText);
      }
  }
}

// go to dealership via options
function getDealership() {
	var d = document.forms.getDealer;
    var uri = "/search/index.cfm?page=results&tab=1&dealer_id="+d.dealer_id.options[d.dealer_id.selectedIndex].value+"&zip="+document.getElementById("zip_dealer").value+"&radius="+document.getElementById("radius_dealer").value;
    newPage = window.open(uri,"_self","");
}

// open newspaperCategories options

function changePage() {
	var f = document.forms.newspaperCategoryNav;
    var uri = f.newspaperCategories.options[f.newspaperCategories.selectedIndex].value;
    newPage =window.open(uri,"_self","");
}

// open help window
function info(page,w,h) {
	newPage = window.open("http://www.carshq.com/admin/help.cfm?helpid=" + page,"info","width=" + w + ",height=" + h + ",scrollbars");}
	
// open help window
function recall(year,make,model,w,h) {
	newPage = window.open("http://www.carshq.com/includes/recall.cfm?recall_year=" + year + "&recall_make=" + make + "&recall_model=" + model,"recall","width=" + w + ",height=" + h + ",scrollbars");}
	
// opens export excel inventory in admin
function exportInventory(id){
window.location="excel/vehiclestocksheet.cfm?id="+id;
}
	
// opens export excel inventory in admin
function exportInventoryFeatured(id){
window.location="excel/vehiclestocksheetFeatured.cfm?id="+id;
}
	
// opens export excel inventory in admin
function exportInventoryTop(id){
window.location="excel/vehiclestocksheetTop.cfm?id="+id;
}

// opens export excel inventory in admin
function exportrepReport(property_ids,start_date,end_date){
window.location="excel/repReport_export.cfm?property_ids="+property_ids+"&start_date="+start_date+"&end_date="+end_date;
}

// opens upSell window
function upSell(url){
window.location=url;
}

// upSell/Promotion
function calcTotal() {
  var q, total_price, price;
  
  q=document.order.quantity.value;
  if(q>71){
    price=10;
  }
  else if (q>35) {
    price=15;
  }
  else if (q>11) {
    price=20;
  }
  else if (q>0) {
    price=25;
  }
  else{
    price=0;
  }
  
  
  total_price=Currency(q*price);
  document.order.total.value=total_price;
  price=Currency(price);
  document.order.price.value=price;
  
}  
  
function Currency(anynum) {
        //returns number as string in $xxx,xxx.xx format.
        anynum = "" + eval(anynum)  //evaluate (in case an expression sent)
        intnum = parseInt(anynum)  //isolate integer portion
        intnum = Math.abs(intnum)
        intstr = ""+intnum
        //add comma in thousands place.
        if (intnum >= 1000) {
                intlen = intstr.length
                temp1=parseInt(""+(intnum/1000))
                temp2=intstr.substring(intlen-3,intlen)
                intstr = temp1+","+temp2

        }
        if (intnum >= 1000000) {
                intlen = intstr.length
                temp1=parseInt(""+(intnum/1000000))
                temp2=intstr.substring(intlen-7,intlen)
                intstr = temp1+","+temp2

        }

        decnum = Math.abs(parseFloat(anynum)-parseInt(anynum)) //isolate decimal portion
        decnum = decnum * 100 // multiply decimal portion by 100.
        decstr = "" + Math.abs(Math.round(decnum))
        if (decstr.length>2) {decstr=decstr.substring(0,2)}
        while (decstr.length < 2) {decstr="0"+decstr}
        retval = intstr + "." + decstr 
        if (anynum < 0) {
                retval="("+retval+")"
        }
        //return "$"+retval
		return retval
}  
/***********************************************
* Show Hint script- � Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
		
var horizontal_offset="9px" //horizontal offset of hint box from anchor link

/////No further editting needed

var vertical_offset="0" //horizontal offset of hint box from anchor link. No need to change.
var ie=document.all
var ns6=document.getElementById&&!document.all

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
}
else{
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
}
return edgeoffset
}

function showhint(menucontents, obj, e, tipwidth){
if ((ie||ns6) && document.getElementById("hintbox")){
dropmenuobj=document.getElementById("hintbox")
dropmenuobj.innerHTML=menucontents
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
dropmenuobj.style.visibility="visible"
obj.onmouseout=hidetip
}
}

function hidetip(e){
dropmenuobj.style.visibility="hidden"
dropmenuobj.style.left="-500px"
}

function createhintbox(){
var divblock=document.createElement("div")
divblock.setAttribute("id", "hintbox")
document.body.appendChild(divblock)
}

if (window.addEventListener)
window.addEventListener("load", createhintbox, false)
else if (window.attachEvent)
window.attachEvent("onload", createhintbox)
else if (document.getElementById)
window.onload=createhintbox