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) {

	if (type == 'models') {
		document.getElementById("models").innerHTML = "<img src='images/loading.gif' /> Loading...";
		JsHttpRequest
				.query('query.php', // backend
						{

							'make' :document.getElementById("make").value,
							't' :document.getElementById("t").value,
							'box' :type
						},
						// 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"];
							document.getElementById("trims").innerHTML = result["trims"];
						}
					}, true // do not disable caching
				);
	}

	if (type == 'models2') {
		document.getElementById("models2").innerHTML = "<img src='images/loading.gif' /> Loading...";
		JsHttpRequest
				.query('query.php', // backend
						{

							'make' :document.getElementById("make").value,
							't' :document.getElementById("t").value,
							'box' :type
						},
						// 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("models2").innerHTML = result["models"];
							// document.getElementById("trims").innerHTML
							// =result["trims"];
						}
					}, true // do not disable caching
				);
	}

	if (type == 'trims') {
		document.getElementById("trims").innerHTML = "<img src='images/loading.gif' /> Loading...";
		JsHttpRequest
				.query('query.php', // backend
						{

							'make' :document.getElementById("make").value,
							'model' :document.getElementById("model").value,
							't' :document.getElementById("t").value,
							'box' :type
						},
						// Function is called when an answer arrives.
						function(result, errors) {
							// Write errors to the debug div.
						// document.getElementById("debug").innerHTML = errors;
						// alert(errors);
						// Write the answer.
						if (result) {
							document.getElementById("trims").innerHTML = result["trims"];
						}
					}, true // do not disable caching
				);
	}

}

function GetBoxValuesExt(type) {

	document.getElementById("loading").innerHTML = "<img src='images/loading.gif' /> Loading...";
	JsHttpRequest
			.query(
					'query_new.php', // backend
					{

						'y_from' :document.getElementById("y_from").value,
						'y_to' :document.getElementById("y_to").value,
						'price_min' :document.getElementById("price_min").value,
						'price_max' :document.getElementById("price_max").value,
						'make' :document.getElementById("make").value,
						'model' :document.getElementById("model").value,
						'trim' :document.getElementById("trim").value,
						'standardbody' :document.getElementById("standardbody").value,
						'engine' :document.getElementById("engine").value,
						'transmission' :document.getElementById("transmission").value,
						'ext_color' :document.getElementById("ext_color").value,
						'int_color' :document.getElementById("int_color").value,
						't' :document.getElementById("t").value,
						'box' :type
					},
					// 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) {

							// if(result["box"]!='make')
							// document.getElementById("make_div").innerHTML
							// =result["make_box"];
							if (result["box"] != 'model')
								document.getElementById("model_div").innerHTML = result["model_box"];
							if (result["box"] != 'trim')
								document.getElementById("trim_div").innerHTML = result["trim_box"];
							if (result["box"] != 'standardbody')
								document.getElementById("standardbody_div").innerHTML = result["standardbody_box"];
							if (result["box"] != 'engine')
								document.getElementById("engine_div").innerHTML = result["engine_box"];
							if (result["box"] != 'transmission')
								document.getElementById("transmission_div").innerHTML = result["transmission_box"];
							if (result["box"] != 'ext_color')
								document.getElementById("ext_color_div").innerHTML = result["ext_color_box"];
							if (result["box"] != 'int_color')
								document.getElementById("int_color_div").innerHTML = result["int_color_box"];
							document.getElementById("found_div").innerHTML = 'Found ' + result["found"] + ' vehicles!';
							document.getElementById("loading").innerHTML = "";
						}
					}, true // 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 = $('#rate').val();
	term = $('#term').val();
	downp = $('#downp').val();
	price = $('#price').val().replace(/$/g,"").replace(/,/g,"");
	trade = $('#trade').val();

	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) - (trade==null?0:parseInt(trade));

			mointerest = rate / 100 / 12;

			months = term * 12;

			x = Math.pow(1 + mointerest, months);

			$('#monthly').html("<h3><strong> $"

					+ ((principal * x * mointerest) / (x - 1)).toFixed(2)

					+ ' / mo</strong></h3>');

		} else {

			$('monthly').html('<h3><strong> 0 </strong></h3>');

			alert("There's no monthly payment with your down payment!");

		}

	}

}

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

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 showHide(action) {
	if (action == 0) {
		document.getElementById('overlay').style.display = 'none';

	} else {
		document.getElementById('overlay').style.display = 'block';
	}
}

var formid = "";
var divclass = "";
var query = "";
loadAJAH = function(filename) {
	if (filename == "searchbox_home_new.php") {
		formid = 'form#searchform_new';
		divclass = "#searchbox_new";
	} else if (filename == "searchbox_home_used.php") {
		formid = 'form#searchform_used';
		divclass = "#searchbox_used";
	} else {
		formid = "form#searchform";
		divclass = "#searchbox";
	}

	loaderWaitDisable();
	query = "";

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

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

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

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

$( function() {
	$('input,select', divclass).removeAttr('disabled');
})