function loadImg(filename, key)
{

	document.getElementById('carImage').src = filename;
	document.getElementById('lightboxImage').src=filename;
	currentImage=key;
}

function toggleCertified()
{
	if (document.getElementById('used').checked)
	{
		document.getElementById('certified').disabled = false;
		//document.getElementById('location').disabled = false;
	}
	else
	{
		document.getElementById('certified').disabled = true;
		//document.getElementById('location').value = '';
		//document.getElementById('location').disabled = true;
	}
}

function changeLimit(url, queryStr)
{
	var arr = splitString(queryStr, "&");

	var limit = document.getElementById('limitDD').value;

	document.getElementById('limit').value = limit;

	var str = '';

	for (var i=0; i<arr.length; i++)
	{
		if (arr[i].substring(0, 6)!="limit=")
		{
			str += '&'+arr[i];
		}
	}
	str += '&limit='+limit;
	str = str.substring(1, str.length);

	str = 'http://'+url+'?'+str;

	window.location=str;
}

function changeSort(url, queryStr)
{
	var arr = splitString(queryStr, "&");

	var sort = document.getElementById('sortDD').value;

	document.getElementById('sort').value = sort;

	var str = '';

	for (var i=0; i<arr.length; i++)
	{
		if (arr[i].substring(0, 5)!="sort=")
		{
			str += '&'+arr[i];
		}
	}
	str += '&sort='+sort;
	str = str.substring(1, str.length);

	str = 'http://'+url+'?'+str;

	window.location=str;
}

function splitString(str, patern)
{
	var arr = str.split(patern);

	return arr;
}

function clearAll()
{
	//document.getElementById('new').checked=true;
	//document.getElementById('certified').checked=false;
	//document.getElementById('certified').disabled=true;
	document.getElementById('location').value='';
	document.getElementById('model').value='';
	document.getElementById('make').value='';
	document.getElementById('trim').value='';
	document.getElementById('stock').value='';
	document.getElementById('trans').value='';
	document.getElementById('fuel').value='';
	document.getElementById('drivetrain').value='';
	document.getElementById('body').value='';
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function calculatePayment(price)
{
	var downpayment = document.getElementById('downpayment').value;
	var term = document.getElementById('term').value;
	var rate = document.getElementById('rate').value;
	var payment = 0;
	var factor = 0;
	//alert (term+"=>"+rate);
	if (IsNumeric(downpayment) && IsNumeric(term) && IsNumeric(rate) && IsNumeric(price) )
	{
		rate = rate/1200;

		if (rate == 0)
		{
			payment = (price-downpayment) / term;
		}
		else
		{
			factor = Math.pow(1 + rate, term);
			payment = (price-downpayment) * ( rate / (1 - 1/factor) );
		}

		document.getElementById('estimatedPayment').innerHTML = '$'+Math.round(payment);

	}
	else
	{
		document.getElementById('estimatedPayment').innerHTML = '$'+0;
	}
}

function IsNumeric(inputVal)
{
	if (isNaN(parseFloat(inputVal)) || inputVal<0) {
        return false;
     }
     return true
}


function menuHideFields(onoff) {
	if( onoff == 1 ) {
		fieldVisibility = 'hidden';
	}
	else {
		fieldVisibility = '';
	}
	var selects = document.getElementsByTagName('select');
	for (var i = 0; i < selects.length; i++) {
		//selects[i].className=menuClass;
		selects[i].style.visibility=fieldVisibility;
	}
}

function liveChat(obj)
{
	chatAval = true;

	chat = new Image();
	chat.src = "http://admin.instantservice.com/resources/smartbutton/6152/"+departmentid+"/available.gif?"+Math.floor(Math.random()*10001);

	chat.onload = function () {
		window.open('https://admin.instantservice.com/links/6152/'+departmentid, 'custclient', 'width=600,height=160,scrollbars=0');
	}
	chat.onerror = function() {

		if (window.departmentid2)
		{
			chat2 = new Image();
			chat2.src = "http://admin.instantservice.com/resources/smartbutton/6152/"+departmentid2+"/available.gif?"+Math.floor(Math.random()*10001);
		chat2.onload = function () {
			window.open('https://admin.instantservice.com/links/6152/'+departmentid, 'custclient', 'width=600,height=160,scrollbars=0');
		}
			chat2.onerror = function() {

				if (unemail.indexOf('@',unemail) >= 0) {
					var mt = 'mailto:';
				} else {
					var mt = '';
				}

				window.location = mt + unemail;
			}
		}
		else
		{
			if (unemail.indexOf('@',unemail) >= 0)
			{
				var mt = 'mailto:';
			} else {
				var mt = '';
			}

			window.location = mt + unemail;
      	}


	}

}
