function clearbox(val){
  var box_val=document.getElementById("message").value;
  //alert(box_val.length);
  if(box_val.length==val) { document.getElementById("message").value=''; }
}

function GetBoxValues(type,status,dealer_id,t) {
     
	 if(type=='makes') {
	   document.getElementById("makes").innerHTML ="<img src='images/loading.gif' /> Loading...";	 
	   JsHttpRequest.query(
            'query.php', // backend
            {
                
                 
                't': t,
				'box': type,
				'status': status,
				'dealer_id': dealer_id
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
                // Write errors to the debug div.
              //  document.getElementById("debug").innerHTML = errors; 
                // Write the answer.
                if (result) {
                    xGetElementById("makes").innerHTML =result["makes"];
					xGetElementById("models").innerHTML =result["models"];
					xGetElementById("ext_color_div").innerHTML =result["ext_colors"];
                    xGetElementById("standardbody_div").innerHTML =result["standardbodies"];
					 
                }
            },
            false  // do not disable caching
        );
	 }
	 
	 if(type=='models') {
	   document.getElementById("models").innerHTML ="<img src='images/loading.gif' /> Loading...";	 
	   JsHttpRequest.query(
            'query.php', // backend
            {
                
                'make': document.getElementById("make").value,  
                't': t,
				'box': type,
				'status': status,
				'dealer_id': dealer_id
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
                // Write errors to the debug div.
          //      document.getElementById("debug").innerHTML = errors; 
                // Write the answer.
                if (result) {
                    document.getElementById("models").innerHTML =result["models"];
					 
                }
            },
            false  // do not disable caching
        );
	 }
	 if(type=='home') {
	   document.getElementById("models").innerHTML ="<img src='images/loading.gif' /> Loading...";	 
	   JsHttpRequest.query(
            'query.php', // backend
            {
                
                'make': document.getElementById("make").value,  
                't': t,
				'box': type,
				'status': status,
				'dealer_id': dealer_id
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
                // Write errors to the debug div.
          //      document.getElementById("debug").innerHTML = errors; 
                // Write the answer.
                if (result) {
                    document.getElementById("models").innerHTML =result["models"];
					 
                }
            },
            false  // do not disable caching
        );
	 }
	  if(type=='body') {
	   xGetElementById("ext_color_div").innerHTML ="<img src='images/loading.gif' /> Loading...";
	   xGetElementById("standardbody_div").innerHTML ="<img src='images/loading.gif' /> Loading...";	 
	   JsHttpRequest.query(
            'query.php', // backend
            {
                
                'make': xGetElementById("make").value,  
                'model': xGetElementById("model").value,  
                't': t,
				'box': type,
				'status': status,
				'dealer_id': dealer_id
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
                // Write errors to the debug div.
                xGetElementById("debug").innerHTML = errors; 
                // Write the answer.
                if (result) {
                    xGetElementById("ext_color_div").innerHTML =result["ext_colors"];
                    xGetElementById("standardbody_div").innerHTML =result["standardbodies"];
					 
                }
            },
            false  // do not disable caching
        );
	 }
    }
 
function popitup(url) {
	newwindow=window.open(url,'name','height=200,width=150');
	if (window.focus) {newwindow.focus()}
	return false;
}
				
function popOpen(page,width,height,scrollbars){
	if(width==""){ width=457;}
	if(height==""){ height=459;}
	window.open(page,"w","width="+width+", height="+height+", menubar=0, scrollbars="+scrollbars);	
}


function loanCalculator(){
	rate  = document.load.rate[document.load.rate.selectedIndex].value;
	term = document.load.term[document.load.term.selectedIndex].value;
	downp = document.load.downp.value;
	price = document.load.price.value;
	msg="";
	
	if(isNaN(price)){ 
		msg+="\nPlease enter just numbers in the 'Vehicle Price' field";
	}
	if(downp!="" && isNaN(downp)){ 
	 	msg+="\nPlease enter just numbers in the 'Down Payment' field";
	 }else{
		if(downp==""){	downp=0; }
	 }
	if(msg!=""){
		alert(msg);
	}else{
		if(parseInt(downp)<parseInt(price)){
			
		principal = parseInt(price)-parseInt(downp);
  		mointerest = rate / 100 / 12;
		months = term * 12;
		x = Math.pow(1 + mointerest, months);
		document.getElementById('monthly').innerHTML="<h3><strong> $"+((principal*x*mointerest)/(x-1)).toFixed(2)+ ' / Month</strong></h3>';
		}else{
			document.getElementById('monthly').innerHTML='<h3><strong> 0 </strong></h3>';
			alert("There's no monthly payment with your down payment!");
		}
	}
	
}
function mpgCalculator(){
	mpg_miles  = document.mpg.mpg_miles[document.mpg.mpg_miles.selectedIndex].value;
	mpg_price = document.mpg.mpg_price[document.mpg.mpg_price.selectedIndex].value;
	mpg_city = document.mpg.mpg_city.value;
	mpg_hwy = document.mpg.mpg_hwy.value;
	if(parseInt(mpg_city)<parseInt(mpg_hwy)){
		
		cost=(0.6/mpg_city+0.4/mpg_hwy)*mpg_price*((mpg_miles/12)*12);
	}
	document.getElementById('fuel_cost').innerHTML="<h3><strong> $"+ cost.toFixed(2) + ' / Year</strong></h3>';
	 
}

function showCoupon(id){
	window.open('http://www.dealerfire.com/coupons/?DealerID='+id+'','','resizable=yes,width=766,height=620');
}


function goto(val){
	if(val!=""){
		window.open(val,"s","width=800, height=600, top=0, left=0, menubar=1, resizable=1, location=1, scrollbars=1");
	}
}

function expand_info(val){
	var pos = document.getElementById('expand_info').style.display;
	if(pos!="block"){
		document.getElementById('expand_info').style.display='block';
		document.getElementById('expand_lnk').innerHTML='Less Info';
	}else {
	    document.getElementById('expand_info').style.display='none';
		document.getElementById('expand_lnk').innerHTML='Expand Info';
	}
}




function xGetElementById(e) {
    if(typeof(e)!='string') return e;
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
    return e;
}  
/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}


var query = "";
loadAJAH = function(filename)
{
	loaderWaitDisable();
	query = "";

	$("input,select","form#searchform").each(function() {
		if($(this).attr('name')!='' && $(this).val()!='' && $(this).val()!=null)
		{
			query += $(this).attr('name')+"="+$(this).val().replace(/ /g,"%20").replace(/&/g,"%26")+"&";
		}
	})

	$("#searchbox").load("searchbox_ajax.php?"+query+'&filename='+filename, loaderDoneDisable);
}

loaderWaitShade = function()
{
	$('#blackback').css('background-color','#000000');
	$('#searchbox').fadeTo( "slow", 0.30 );
}

loaderDoneShade = function()
{
	$('#searchbox').fadeTo( "slow", 1 ,function() {
		$('#blackback').css('background-color','#ffffff')
	});
}
loaderWaitDisable = function()
{
	$('input,select',"#searchbox").attr('disabled','true');
}
loaderDoneDisable = function()
{
	$('input,select',"#searchbox").removeAttr('disabled');
}

$(function() {
	$('input,select',"#searchbox").removeAttr('disabled');
})