
// Change models based on make selection
// Ajax function. This is a re-write from the one
// before. This one works in all browsers.
var xmlHttp

function showModels(str) {
	if (str.length==0) { 
  		document.getElementById("txtHint").innerHTML="";
  		return;
  	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="makemodel.php";
	url=url+"?make="+str;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function stateChanged() { 
	if (xmlHttp.readyState==4) { 
		document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}catch (e){
 	 	// Internet Explorer
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}catch (e){
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp;
}

// A little less java and ajax to
// show images from thumbnail view
function showImage(url, imgstr) { 
	var newImg = new Image();
	newImg.src = url+'/'+imgstr;
	var height = newImg.height;
	var width = newImg.width;
	if(width >= 350) {
		var newwidth = 'width="350"';
	}
	document.getElementById("loadarea2").innerHTML = '<img '+newwidth+' src="'+url+'">';
}
// A little less java and ajax to
// show images from thumbnail view
function showImage2(url, imgstr) { 
//	var newImg = new Image();
//	newImg.src = url+'/'+imgstr;
//	var height = newImg.height;
//	var width = newImg.width;
//	if(width >= 310) {
		var newwidth = 'width="310"';
//	}
	document.getElementById("loadarea2").innerHTML = '<a href="javascript:openpic(\''+url+'/'+imgstr+'\')"> <img '+newwidth+' src="'+url+'/'+imgstr+'" style="border-style: none"></a>';
}
// Little function to show and hide
// divs as needed
function toggleDiv(divid){
   if(document.getElementById(divid).style.display == 'none'){
     document.getElementById(divid).style.display = 'block';
   }else{
     document.getElementById(divid).style.display = 'none';
   }
}

// Monthly payment calculator
function CalculatePayments(principal, down_payment, interest, years) {
	var x = ((principal - down_payment) * interest / (12 * (1 - Math.pow(1 + (interest / 12), (-years * 12)))));
	return Math.floor(x * 100) / 100
}
		
function ShowPayments() {
	var x = CalculatePayments(document.jsForm.jsPrincipal.value, document.jsForm.jsDownPayment.value, document.jsForm.jsInterest.value / 100, document.jsForm.jsYears.value);
	if (isNaN(x)) {
		document.jsForm.jsResult.value = 'Could not compute';
	}else{
		document.jsForm.jsAmtFin.value = document.jsForm.jsPrincipal.value - document.jsForm.jsDownPayment.value;
		document.jsForm.jsResult.value = x;
	}
}

// Function to open new window
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos) {
	if(pos=="random"){
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center"){
		LeftPosition=(screen.width)?(screen.width-w)/2:100;
		TopPosition=(screen.height)?(screen.height-h)/2:100;
	}else if((pos!="center" && pos!="random") || pos==null){
		LeftPosition=0;TopPosition=20;
	}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
}
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function PopupPic(sPicURL,w,h) { 

 window.open( sPicURL, "", "resizable=1,HEIGHT=" + h + ",WIDTH=" + w); 

}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		nav_welcome_over = newImage("{$templateurl}/images/nav_welcome_over.gif");
		nav_listinventory_over = newImage("{$templateurl}/images/nav_listinventory_over.gif");
		nav_searchinventory_over = newImage("{$templateurl}/images/nav_searchinventory_over.gif");
		nav_contactus_over = newImage("{$templateurl}/images/nav_contactus_over.gif");
		findus_over = newImage("{$templateurl}/images/findus_over.gif");
		nav_specials_over = newImage("{$templateurl}/images/nav_specials_over.gif");
		moreinfo_over = newImage("{$templateurl}/images/moreinfo_over.gif");
		searchbutton_over = newImage("{$templateurl}/images/searchbutton_over.gif");
		submit_on = newImage("{$templateurl}/images/submit_on.gif");
		reset_on = newImage("{$templateurl}/images/reset_on.gif");
		getdirections_over = newImage("{$templateurl}/images/getdirections_over.gif");
		preloadFlag = true;
	}
}