/******************************** REVERSE ESTIMATOR ********************************/
/**
 * This function uncheck all the radio buttons and the select the radio buttonfrom which this 
 * function is called. And assigns the value of the vehicle to the hidden variable vehicle.
**/
function radiobuttonClick(thisObj, thisEvent) {
// In the form the vehicle entry have id like: revForm:data:0:items:0:line
    var form = document.getElementById("revForm");
   var flag = false;
   var rowId = 0;
   var upperRowId = 0;
   for(i=0; i<form.elements.length; i++){     
     radioButtonName = "revForm:data:"+upperRowId+":items:"+rowId+":line";
     flag = false;
     //Deselect all radio buttons first(if this element is a radio button inside your datatable)
     while(form.elements[i].name == radioButtonName) { 
        form.elements[i].checked = false;
        rowId = rowId+1;
        radioButtonName = "revForm:data:"+upperRowId+":items:"+rowId+":line";
        i = i+1;
        //getRow(form.elements[i]).className = "";
        flag = true;
     } 
        upperRowId = upperRowId+1;
        rowId = 0;
        if(flag == true) i = i-1;
   }
   thisObj.checked = true;
   var vehicle = document.getElementById("revForm:vehiclId");
   vehicle.value = thisObj.value;
}

//Setting default term value for different Payment Modes. 
var paymentMode="Buy";
function PaymentModeChanged(thisobj) {
    if(thisobj.value == "Buy"){
        paymentMode = "Buy";
// CHANGE START - Remove 72 months option for lease vehicle - 08/07/2007 
        populateRetail();
// CHANGE END - Remove 72 months option for lease vehicle - 08/07/2007 
        document.getElementById("revIndexForm:desiredTerm").value="60";
    } else if(thisobj.value == "Lease") {
        paymentMode = "Lease";
// CHANGE START - Remove 72 months option for lease vehicle - 08/07/2007 
        populateLease();
// CHANGE END - Remove 72 months option for lease vehicle - 08/07/2007 
        document.getElementById("revIndexForm:desiredTerm").value="48";
    }
}

// CHANGE START - Remove 72 months option for lease vehicle - 08/07/2007 

function populateRetail() {
    if(document.getElementById("revIndexForm:desiredTerm").length != 0) {
        document.getElementById("revIndexForm:desiredTerm").length = 0;
    }
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("24 months", "24");
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("36 months", "36");
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("48 months", "48");
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("60 months", "60");
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("72 months", "72");
    // START-NTIER Low Mileage Changes,05 December,2007
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("84 months", "84");
    // END-NTIER Low Mileage Changes,05 December,2007    
    
}

function populateLease() {
    if(document.getElementById("revIndexForm:desiredTerm").length != 0) {
        document.getElementById("revIndexForm:desiredTerm").length = 0;
    }
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("24 months", "24");
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("36 months", "36");
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("48 months", "48");
    document.getElementById("revIndexForm:desiredTerm").options[document.getElementById("revIndexForm:desiredTerm").length] = new Option("60 months", "60");
}

// CHANGE END - Remove 72 months option for lease vehicle - 08/07/2007 

/***************************************** OCA *****************************/

function AssignSSN(){
   document.getElementById("appForm:ssn").value = document.getElementById("appForm:ssn1").value+document.getElementById("appForm:ssn2").value+document.getElementById("appForm:ssn3").value; 
}
function AssignDOB(){
    var dobmon = document.getElementById("appForm:birthMon").value;
    var dobdt = document.getElementById("appForm:birthDay").value;
    var dobyr = document.getElementById("appForm:birthYear").value;
    
    if(""==dobmon || ""==dobdt || ""==dobyr)
        document.getElementById("appForm:dob").value = "";
    else document.getElementById("appForm:dob").value = dobmon+'/'+dobdt+'/'+dobyr;
}
function AssignHomePhone(){
    document.getElementById("appForm:homephone").value = document.getElementById("appForm:homephone1").value+document.getElementById("appForm:homephone2").value+document.getElementById("appForm:homephone3").value;
}
function AssignWorkPhone(){
    document.getElementById("appForm:workphone").value = document.getElementById("appForm:workphone1").value+document.getElementById("appForm:workphone2").value+document.getElementById("appForm:workphone3").value;
}
/*
 * Before continuing sometimes it is necessary to assign all composite variables.
*/ 
function BeforeConinue(){
    AssignSSN();
    AssignDOB();
    AssignHomePhone();
    AssignWorkPhone();
}
/*
 * These functions are for ssn edit boxes.
 * If one edit boxes full or deleted then it will move to the next or previous edit box.
*/

function SSNChanged1(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("appForm:ssn2").focus();
    AssignSSN();
}
function SSNChanged2(thisobj){
    var val = thisobj.value;
    if(val.length == 2)
        document.getElementById("appForm:ssn3").focus();
    if(val.length == 0){
        document.getElementById("appForm:ssn1").focus();
        document.getElementById("appForm:ssn1").value = document.getElementById("appForm:ssn1").value;
    }
    
    AssignSSN();
}
function SSNChanged3(thisobj){
    var val = thisobj.value;
    if(val.length == 0){
        document.getElementById("appForm:ssn2").focus();
        document.getElementById("appForm:ssn2").value = document.getElementById("appForm:ssn2").value;
    }
    
    AssignSSN();
}


/*
 * These functions are for date of birth edit boxes.
 * If one edit boxes full or deleted then it will move to the next or previous edit box.
*/

function DOBChanged1(thisobj){
    var val = thisobj.value;
    if(val.length == 2)
        document.getElementById("appForm:birthDay").focus();
    
    AssignDOB();
}
function DOBChanged2(thisobj){
    var val = thisobj.value;
    if(val.length == 2)
        document.getElementById("appForm:birthYear").focus();
    if(val.length == 0){
        document.getElementById("appForm:birthMon").focus();
        document.getElementById("appForm:birthMon").value = document.getElementById("appForm:birthMon").value;
    }
    
    AssignDOB();
}
function DOBChanged3(thisobj){
    var val = thisobj.value;
    if(val.length == 0){
        document.getElementById("appForm:birthDay").focus();
        document.getElementById("appForm:birthDay").value = document.getElementById("appForm:birthDay").value;
    }
    
    AssignDOB();
}


/*
 * These functions are for homephone edit boxes.
 * If one edit boxes full or deleted then it will move to the next or previous edit box.
*/
function HomephoneChanged1(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("appForm:homephone2").focus();
   AssignHomePhone();
}
function HomephoneChanged2(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("appForm:homephone3").focus();
    if(val.length == 0){
        document.getElementById("appForm:homephone1").focus();
        document.getElementById("appForm:homephone1").value = document.getElementById("appForm:homephone1").value;
    }
    AssignHomePhone();
}
function HomephoneChanged3(thisobj){
    var val = thisobj.value;
    if(val.length == 0){
        document.getElementById("appForm:homephone2").focus();
        document.getElementById("appForm:homephone2").value = document.getElementById("appForm:homephone2").value;
    }
    AssignHomePhone();
}


/*
 * These functions are for workphone edit boxes.
 * If one edit boxes full or deleted then it will move to the next or previous edit box.
*/
function WorkphoneChanged1(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("appForm:workphone2").focus();
   AssignWorkPhone();
}
function WorkphoneChanged2(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("appForm:workphone3").focus();
    if(val.length == 0){
        document.getElementById("appForm:workphone1").focus();
        document.getElementById("appForm:workphone1").value = document.getElementById("appForm:workphone1").value;
    }
    AssignWorkPhone();
}
function WorkphoneChanged3(thisobj){
    var val = thisobj.value;
    if(val.length == 0){
        document.getElementById("appForm:workphone2").focus();
        document.getElementById("appForm:workphone2").value = document.getElementById("appForm:workphone2").value;
    }
    AssignWorkPhone();
}


function populateAddress() {
    document.getElementById("coAppForm:coAppAddress").value = document.getElementById("coAppForm:streetAddress").value;
    document.getElementById("coAppForm:coAppCity").value = document.getElementById("coAppForm:city").value;
    document.getElementById("coAppForm:coAppState").value = document.getElementById("coAppForm:state").value;
    document.getElementById("coAppForm:coAppZipCode").value = document.getElementById("coAppForm:zipCode").value;
    document.getElementById("coAppForm:homephone1").value = document.getElementById("coAppForm:apphomephone1").value;
    document.getElementById("coAppForm:homephone2").value = document.getElementById("coAppForm:apphomephone2").value;
    document.getElementById("coAppForm:homephone3").value = document.getElementById("coAppForm:apphomephone3").value;
    AssignCoAppHomePhone();
}

function clearAddress() {
    document.getElementById("coAppForm:coAppAddress").value = "";
    document.getElementById("coAppForm:coAppCity").value = "";
    document.getElementById("coAppForm:coAppState").selectedIndex = 0;
    document.getElementById("coAppForm:coAppZipCode").value = "";
    document.getElementById("coAppForm:homephone1").value = "";
    document.getElementById("coAppForm:homephone2").value = "";
    document.getElementById("coAppForm:homephone3").value = "";
    AssignCoAppHomePhone();
}

function handleAddress(make){

    if (make == "T") {
            field = document.forms[0].elements["coAppForm:sameAddress"];
        } else {
            field = document.forms[1].elements["coAppForm:sameAddress"];
        }

	  for (i = 0; i < field.length; i++)
	  { 
		if(field[i].checked == true && field[i].value == "Yes")
		{
		  populateAddress();
		  break;
		}else if(field[i].checked == true && field[i].value == "No"){
          clearAddress();
        }
	  }

}

function AssignCoAppSSN(){
    document.getElementById("coAppForm:coappssn").value = document.getElementById("coAppForm:ssn1").value+document.getElementById("coAppForm:ssn2").value+document.getElementById("coAppForm:ssn3").value;
}
function AssignCoAppDOB(){
    var dobmon = document.getElementById("coAppForm:birthMon").value;
    var dobdt = document.getElementById("coAppForm:birthDay").value;
    var dobyr = document.getElementById("coAppForm:birthYear").value;
    
    if(""==dobmon || ""==dobdt || ""==dobyr)
        document.getElementById("coAppForm:coappdob").value = "";
    else document.getElementById("coAppForm:coappdob").value = dobmon+'/'+dobdt+'/'+dobyr;
}
function AssignCoAppHomePhone(){
    document.getElementById("coAppForm:homephone").value = document.getElementById("coAppForm:homephone1").value+document.getElementById("coAppForm:homephone2").value+document.getElementById("coAppForm:homephone3").value;
}
function AssignCoAppWorkPhone(){
    document.getElementById("coAppForm:workphone").value = document.getElementById("coAppForm:workphone1").value+document.getElementById("coAppForm:workphone2").value+document.getElementById("coAppForm:workphone3").value;
}

/*
 * Before continuing sometimes it is necessary to assign all composite variables.
*/ 
function BeforeConinueCoApp(){
    AssignCoAppSSN();
    AssignCoAppDOB();
    AssignCoAppHomePhone();
    AssignCoAppWorkPhone();
}

/*
 * These functions are for ssn edit boxes.
 * If one edit boxes full or deleted then it will move to the next or previous edit box.
*/

function CoAppSSNChanged1(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("coAppForm:ssn2").focus();
    AssignCoAppSSN();
}
function CoAppSSNChanged2(thisobj){
    var val = thisobj.value;
    if(val.length == 2)
        document.getElementById("coAppForm:ssn3").focus();
    if(val.length == 0){
        document.getElementById("coAppForm:ssn1").focus();
        document.getElementById("coAppForm:ssn1").value = document.getElementById("coAppForm:ssn1").value;
    }
    
    AssignCoAppSSN();
}
function CoAppSSNChanged3(thisobj){
    var val = thisobj.value;
    if(val.length == 0){
        document.getElementById("coAppForm:ssn2").focus();
        document.getElementById("coAppForm:ssn2").value = document.getElementById("coAppForm:ssn2").value;
    }
    AssignCoAppSSN();
}


/*
 * These functions are for date of birth edit boxes.
 * If one edit boxes full or deleted then it will move to the next or previous edit box.
*/

function CoAppDOBChanged1(thisobj){
    var val = thisobj.value;
    if(val.length == 2)
        document.getElementById("coAppForm:birthDay").focus();
    AssignCoAppDOB();
}
function CoAppDOBChanged2(thisobj){
    var val = thisobj.value;
    if(val.length == 2)
        document.getElementById("coAppForm:birthYear").focus();
    if(val.length == 0){
        document.getElementById("coAppForm:birthMon").focus();
        document.getElementById("coAppForm:birthMon").value = document.getElementById("coAppForm:birthMon").value;
    }
    
    AssignCoAppDOB();
}
function CoAppDOBChanged3(thisobj){
    var val = thisobj.value;
    if(val.length == 0){
        document.getElementById("coAppForm:birthDay").focus();
        document.getElementById("coAppForm:birthDay").value = document.getElementById("coAppForm:birthDay").value;
    }
    
    AssignCoAppDOB();
}


/*
 * These functions are for homephone edit boxes.
 * If one edit boxes full or deleted then it will move to the next or previous edit box.
*/
function CoAppHomePhoneChanged1(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("coAppForm:homephone2").focus();
    AssignCoAppHomePhone();
}
function CoAppHomePhoneChanged2(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("coAppForm:homephone3").focus();
    if(val.length == 0){
        document.getElementById("coAppForm:homephone1").focus();
        document.getElementById("coAppForm:homephone1").value = document.getElementById("coAppForm:homephone1").value;
    }
    AssignCoAppHomePhone();
}
function CoAppHomePhoneChanged3(thisobj){
    var val = thisobj.value;
    if(val.length == 0){
        document.getElementById("coAppForm:homephone2").focus();
        document.getElementById("coAppForm:homephone2").value = document.getElementById("coAppForm:homephone2").value;
    }
    AssignCoAppHomePhone();
}

/*
 * These functions are for workphone edit boxes.
 * If one edit boxes full or deleted then it will move to the next or previous edit box.
*/
function CoAppWorkphoneChanged1(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("coAppForm:workphone2").focus();
   AssignCoAppWorkPhone();
}
function CoAppWorkphoneChanged2(thisobj){
    var val = thisobj.value;
    if(val.length == 3)
        document.getElementById("coAppForm:workphone3").focus();
    if(val.length == 0){
        document.getElementById("coAppForm:workphone1").focus();
        document.getElementById("coAppForm:workphone1").value = document.getElementById("coAppForm:workphone1").value;
    }
    AssignCoAppWorkPhone();
}
function CoAppWorkphoneChanged3(thisobj){
    var val = thisobj.value;
    if(val.length == 0){
        document.getElementById("coAppForm:workphone2").focus();
        document.getElementById("coAppForm:workphone2").value = document.getElementById("coAppForm:workphone2").value;
    }
    AssignCoAppWorkPhone();
}
function DealerChange(thisobj){

   var form = document.getElementById("dealerForm");
   var rowId = 0;
   var radioButtonName = "dealerForm:dealerTable:"+rowId+":dealer";
   for(i=0; i<form.elements.length; i++){
     
     //Deselect all radio buttons first(if this element is a radio button inside your datatable)
     if(form.elements[i].name == radioButtonName){ 
        form.elements[i].checked = false;
        rowId = rowId+1;
        radioButtonName = "dealerForm:dealerTable:"+rowId+":dealer";
        } 
        if(rowId>4) break;
    }
    var chosendealer = document.getElementById("dealerForm:chosenDealer");
    chosendealer.value = thisobj.value;
    //alert(chosendealer.value);
    thisobj.checked = true;
}
function SelectDealer(){
    var form = document.getElementById("dealerForm");
    var chosenDealer = document.getElementById("dealerForm:chosenDealer");
    var rowId = 0;
    var radioButtonName = "dealerForm:dealerTable:"+rowId+":dealer";
    for(i=0; i<form.elements.length; i++){
        if(form.elements[i].name == radioButtonName){ 
        if(form.elements[i].value == chosenDealer.value){
            form.elements[i].checked = true;
            break;
        }
        rowId = rowId+1;
        radioButtonName = "dealerForm:dealerTable:"+rowId+":dealer";
        } 
        if(rowId>4) break;
    }
}


/**
 * The following two  methods are round about solutions of jsf frame work problem for OCA and PE.
 * The problem is if one enters after providing some data in edit fields it 
 * gets submitted to nowhere. For this problem in the zip code editing pages this 
 * method is used to focus the submit button once the 5 digit zip code is entered.
**/
function EditZipcodeOCA(thisobj, thisevent) {
    var zipcode = thisobj.value;
    var key = thisevent.keyCode;
    if(zipcode.length == 5 && !(key == 16 || key == 9 )) {
        document.getElementById("appForm:submit").focus();
    }
}

function EditZipcodePE(thisobj, thisevent) {
    var zipcode = thisobj.value;
    var key = thisevent.keyCode;
    if(zipcode.length == 5 && !(key == 16 || key == 9 )) {
        document.getElementById("peIndexform:validateZip").focus();
    }
}
