// 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,stryear) {
	if (str.length==0) { 
  		document.getElementById("txtModels").innerHTML="";
  		return;
  	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="model.php";
	url=url+"?make="+str+"&year="+stryear;
	xmlHttp.onreadystatechange=modelsChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function modelsChanged() { 
	if (xmlHttp.readyState==4) { 
		document.getElementById("txtModels").innerHTML=xmlHttp.responseText;
	}
}

function showMakes(str) {
	if (str.length==0) { 
  		document.getElementById("txtMakes").innerHTML="";
  		return;
  	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="make.php";
	url=url+"?year="+str;
	xmlHttp.onreadystatechange=makesChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function makesChanged() { 
	if (xmlHttp.readyState==4) { 
		document.getElementById("txtMakes").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;
}

// 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.getElementById('vehicleprice').innerHTML = 'Could not compute';
	}else{
		document.jsForm.jsAmtFin.value = document.jsForm.jsPrincipal.value - document.jsForm.jsDownPayment.value;
		document.getElementById('vehicleprice').innerHTML = "$" + Math.round(x);
        
	}
}

function moveOnMax(field,nextFieldID){
  if(field.value.length >= field.maxLength){
    document.getElementById(nextFieldID).focus();
  }
}
function numbersonly(myfield, e, dec){
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
function data_copy() {
	if(document.form1.copy[0].checked){
		document.form1.coxstreetnumber.value=document.form1.streetnumber.value;
		document.form1.coxstreetname.value=document.form1.streetname.value;
		document.form1.coxstreettype.value=document.form1.streettype.value;
		document.form1.coxaptsuite.value=document.form1.aptsuite.value;
		document.form1.coxcity.value=document.form1.city.value;
		document.form1.coxstate.value=document.form1.state.value;
		document.form1.coxpostal.value=document.form1.postal.value;
		document.form1.coxhmphone1.value=document.form1.hmphone1.value;
		document.form1.coxhmphone2.value=document.form1.hmphone2.value;
		document.form1.coxhmphone3.value=document.form1.hmphone3.value;
		document.form1.coxaddressyears.value=document.form1.addressyears.value;
		document.form1.coxaddressmonths.value=document.form1.addressmonths.value;
		document.form1.coxhousestatus.value=document.form1.housestatus.value;
	}else{
		document.form1.coxstreetnumber.value="";
		document.form1.coxstreetname.value="";
		document.form1.coxstreettype.value="";
		document.form1.coxaptsuite.value="";
		document.form1.coxcity.value="";
		document.form1.coxstate.value="";
		document.form1.coxpostal.value="";
		document.form1.coxhmphone1.value="";
		document.form1.coxhmphone2.value="";
		document.form1.coxhmphone3.value="";
		document.form1.coxaddressyears.value="";
		document.form1.coxaddressmonths.value="";
		document.form1.coxhousestatus.value="";
	
	}

}
<!--

/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj){
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	if(document.getElementById('cox').checked) {
		// Enter name of mandatory fields
		var fieldRequired = Array("firstname", "lastname", "emailaddress", "ssn1", "ssn2", "ssn3", "birthdate1", "birthdate2", "birthdate3"
		, "streetnumber", "streetname", "city", "state", "postal", "hmphone1", "hmphone2", "hmphone3", "addressyears", "housestatus", "housepay"
		, "employedstatus", "employername", "workphone1", "workphone2", "workphone3", "employertimeyears", "income", "coxrelationship"		
		,"coxfirstname", "coxlastname", "coxssn1", "coxssn2", "coxssn3", "coxbirthdate1", "coxbirthdate2", "coxbirthdate3"
		, "coxstreetnumber", "coxstreetname", "coxcity", "coxstate", "coxpostal", "coxhmphone1", "coxhmphone2", "coxhmphone3", "coxaddressyears", "coxaddressmonths", "coxhousestatus", "coxhousepay"
		, "coxemployedstatus", "coxemployername", "coxworkphone1", "coxworkphone2", "coxworkphone3", "coxemployertimeyears", "coxincome", "methodid");
		// Enter field description to appear in the dialog box
		var fieldDescription = Array("First Name", "Last Name", "Email Address", "Social Security 1", "Social Security 2", "Social Security 3"
		, "Birth Date Month", "Birth Date Day", "Birth Date Year", "Street Number", "Street Name", "City", "State", "Postal/Zip", "Home Phone Area Code", "Home Phone Prefix", "Home Phone Suffix"
		, "Years at address", "Housing Status", "Mtg Payment or Rent", "Employment Status", "Employers Name", "Work Phone Area Code", "Work Phone Prefix", "Work Phone Suffix"
		, "Years employed", "Income", "Co-Applicants Relationship"		
		,"Co-Applicants First Name", "Co-Applicants Last Name", "Co-Applicants Social Security 1", "Co-Applicants Social Security 2", "Co-Applicants Social Security 3"
		, "Co-Applicants Birth Date Month", "Co-Applicants Birth Date Day", "Co-Applicants Birth Date Year", "Co-Applicants Street Number", "Co-Applicants Street Name", "Co-Applicants City", "Co-Applicants State", "Co-Applicants Postal/Zip", "Co-Applicants Home Phone Area Code", "Co-Applicants Home Phone Prefix", "Co-Applicants Home Phone Suffix"
		, "Co-Applicants Years at address", "Co-Applicants Months atr address", "Co-Applicants Housing Status", "Co-Applicants Mtg Payment or Rent", "Co-Applicants Employment Status", "Co-Applicants Employers Name", "Co-Applicants Work Phone Area Code", "Co-Applicants Work Phone Prefix", "Co-Applicants Work Phone Suffix"
		, "Co-Applicants Years employed", "Co-Applicants Income", "Contact Method");
	}else{
		// Enter name of mandatory fields
		var fieldRequired = Array("firstname", "lastname", "emailaddress", "ssn1", "ssn2", "ssn3", "birthdate1", "birthdate2", "birthdate3"
		, "streetnumber", "streetname", "city", "state", "postal", "hmphone1", "hmphone2", "hmphone3", "addressyears", "housestatus", "housepay"
		, "employedstatus", "employername", "workphone1", "workphone2", "workphone3", "employertimeyears", "income", "methodid");
		// Enter field description to appear in the dialog box
		var fieldDescription = Array("First Name", "Last Name", "Email Address", "Social Security 1", "Social Security 2", "Social Security 3"
		, "Birth Date Month", "Birth Date Day", "Birth Date Year", "Street Number", "Street Name", "City", "State", "Postal/Zip", "Home Phone Area Code", "Home Phone Prefix", "Home Phone Suffix"
		, "Years at address", "Housing Status", "Mtg Payment or Rent", "Employment Status", "Employers Name", "Work Phone Area Code", "Work Phone Prefix", "Work Phone Suffix"
		, "Years employed", "Income", "Contact Method");
		
	}
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == 0){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}
// -->

// 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 setItems(checkObj) {

var checkCount = 0;
var maxChecks = 4;

var itemNum = 1;

//Determine the number of checked boxes
while (document.getElementById('item'+itemNum))
{
fieldObj = document.getElementById('item'+itemNum);
if (fieldObj.checked)
{
checkCount++;
}
itemNum++;
}

var itemNum = 1;
//Enable/disable unchecked boxes
while (document.getElementById('item'+itemNum))
{
fieldObj = document.getElementById('item'+itemNum);
if (!fieldObj.checked)
{
fieldObj.disabled = (checkCount==maxChecks);
}
itemNum++;
}
}
