/**
 * This script file is used to fill the details and data of the detailView Page. 
 * This file will parse the paymentEstimator.xml and populate the values.
 * 
 * @author Prabesh Bhaskaran
 */

//Global Variable array that contain the XML tags.
var detailNodeTags= [ "vehiclepayments", "vehicleinfo","netdownpayments","program"]
var netDownTags=[ "cashdown" , "tradein" ]
var inboundBrand="";

/**
 * This function is used to get the value of a particular node.
 * The parameter for this method is the node name.
 * @param {Object} obj
 */
function getValueOfNode(obj) {
	if(obj != null) {
		return obj.childNodes[0].nodeValue;
	} else {
		return null;
	}
}

/**
 * This function is used to get the values of the attribute. 
 * Here node and attribute are passed and return is the value of the attribute in that node. 
 * @param {Object} node
 * @param {Object} attr
 */
function getValueOfAttribute(node, attr) {
	if(node != null) {
		return node.getAttribute(attr);
	} else {
		return null;
	}
}

/**
 * This function hold all the variables of the programs in the detailVeiw page.
 */
function ProgramTree(){
	this.parent=null;
	this.name=null;
	this.program=null;
	this.typ=null;
	this.baseprice=null;
	this.tdpIncentives=null;
	this.iD=null;
	this.detil=null;
	this.valu=null;
	this.disclosure=null;
	this.payment=null;
	this.term=null;
	this.totaldownpayment=null;
	this.totalofpayments=null;
	this.apr=null;
	this.aprIncentives=null;
	this.monthlypayment=null;
	this.maxcashallowed=null;
	this.amtfinanced=null;
	this.annualdistallowed=null;
	this.residual=null;
	this.amtduesigning=null;
	this.securitydeposit=null;
	this.acquisitionfee=null;
	this.netcapcost=null;
	this.mesage=null;
	this.balloonpayment=null;
	this.singlepayleaseamt=null;
	this.def=null;
	this.incentiveChildren= new Array();
	this.netDownPaymentTree=new NetDownPaymentTree();
	this.paymentChildren= new Array();
	 
	this.populatePrograms= function(p_parent, p_nodeName, p_type, p_baseprice, p_annualdistallowed) {
		this.parent = p_parent;
		this.name=p_nodeName;
		this.typ=p_type;
		this.baseprice=p_baseprice;
		this.annualdistallowed=p_annualdistallowed;
		this.incentiveChildren=new Array();
		this.paymentChildren=new Array();
		};
		
	this.populateIncentives= function(p_parent, p_nodeName, p_id, p_type, p_detail, p_value, p_term, p_disclosure) {
		this.parent = p_parent;
		this.name=p_nodeName;
		this.iD=p_id;
		this.typ=p_type;
		this.detil=p_detail;
		this.valu=p_value;
		this.term=p_term;
		this.disclosure=p_disclosure;
		this.incentiveChildren=new Array();
		this.paymentChildren=new Array();
		};

	this.populatePurchasePayments= function(p_parent, p_nodeName, p_term, p_default, p_totaldownpayment, p_totalofpayments, p_tdpIncentives, p_apr, p_aprIncentives, p_monthlypayment, p_maxcashallowed, p_amtfinanced) {
		this.parent = p_parent;
		this.name=p_nodeName;
		this.term=p_term;
		this.def=p_default;
		this.totaldownpayment=p_totaldownpayment;
		this.totalofpayments=p_totalofpayments;
		this.tdpIncentives=p_tdpIncentives;
		this.apr=p_apr;
		this.aprIncentives=p_aprIncentives;
		this.monthlypayment=p_monthlypayment;
		this.maxcashallowed=p_maxcashallowed;
		this.amtfinanced=p_amtfinanced;
		this.incentiveChildren=new Array();
		this.paymentChildren=new Array();
		};
		
	this.populatePayments= function(p_parent, p_nodeName, p_term, p_default, p_totaldownpayment, p_totalofpayments, p_tdpIncentives, p_apr, p_aprIncentives, p_monthlypayment, p_maxcashallowed, p_amtfinanced, p_residual, p_amtduesigning, p_securitydeposit, p_acquisitionfee, p_netcapcost, p_message, p_id, p_type, p_balloonpayment, p_singlepayleaseamt) {
		this.parent = p_parent;
		this.name=p_nodeName;
		this.term=p_term;
		this.def=p_default;
		this.totaldownpayment=p_totaldownpayment;
		this.totalofpayments=p_totalofpayments;
		this.tdpIncentives=p_tdpIncentives;
		this.apr=p_apr;
		this.aprIncentives=p_aprIncentives;
		this.monthlypayment=p_monthlypayment;
		this.maxcashallowed=p_maxcashallowed;
		this.amtfinanced=p_amtfinanced;
		this.residual=p_residual;
		this.amtduesigning=p_amtduesigning;
		this.securitydeposit=p_securitydeposit;
		this.acquisitionfee=p_acquisitionfee;
		this.netcapcost=p_netcapcost;
		this.mesage=p_message;
		this.iD=p_id;
		this.typ=p_type;
		this.balloonpayment=p_balloonpayment;
		this.singlepayleaseamt=p_singlepayleaseamt;
		this.incentiveChildren=new Array();
		this.paymentChildren=new Array();
		};
		
		this.printData= function() {
		var str= "{"+ this.name + ":[\n";
		var tmp=this.printChildren(this.paymentChildren);
		str= str + tmp+"]}";
		return str;
	};
	
    this.printChildren= function(child) {
		  var str="";
		  for(var count=0; count < child.length; count++) {
			  str = str +"{" + child[count].name + ": {\n" + " program:" +child[count].program +",typ:" +child[count].typ +",baseprice:" +child[count].baseprice +",incentives:" +child[count].incentives
			 +",iD:" +child[count].iD +",detil:" +child[count].detil +",valu:" +child[count].valu
			 +",disclosure:" +child[count].disclosure +",payment:" +child[count].payment +",term:" +child[count].term
			 +",totaldownpayment:" +child[count].totaldownpayment+",totalofpayments:"+child[count].totalofpayments +",apr:" +child[count].apr +",monthlypayment:" +child[count].monthlypayment
			 +",maxcashallowed:" +child[count].maxcashallowed +",amtfinanced:" +child[count].amtfinanced +",annualdistallowed:" +child[count].annualdistallowed
			 +",residual:" +child[count].residual +",amtduesigning:" +child[count].amtduesigning +",securitydeposit:" +child[count].securitydeposit
			 +",acquisitionfee:" +child[count].acquisitionfee +",netcapcost:" +child[count].netcapcost +",mesage:" +child[count].mesage
			 +",balloonpayment:" +child[count].balloonpayment +",singlepayleaseamt:"+child[count].singlepayleaseamt + ",def:" +child[count].def;
			  str=str + "\n";
			  var val=this.printChildren(child[count].paymentChildren, str);
			  str= str + val+ "}\n";
		  }
	  return str;
	};
	
	//This function is used to build the Net Down Payment.
	this.buildNetDownPayment= function(nodeName, parent, xmlDocument) {
		var list= xmlDocument.getElementsByTagName(nodeName)[0];
		if (list.childNodes.length>0) {
			var cashdown = list.getElementsByTagName("cashdown")[0];
			var tradein = list.getElementsByTagName("tradein")[0];
			var amtowed = tradein.getAttribute("amtowed");
			this.netDownPaymentTree.populateNode(parent, getValueOfNode(cashdown), getValueOfNode(tradein), amtowed, nodeName);
		}
	};
}
/**
 * This function is used to set all the variables of the netDownPayment.
 * It contain values like cashdown, tradein, amout owed, name etc.
 */
function NetDownPaymentTree(){
		this.children = new Array();
		this.parent=null;
		this.cashdown = null;
		this.tradein=null;
		this.amtowed=null;
		this.name= null;
		
		this.populateNode= function(p_parent, p_cashdown, p_tradein, p_amtowed, p_nodeName) {
		this.parent=p_parent;
		this.cashdown = p_cashdown;
		this.tradein = p_tradein;
		this.amtowed = p_amtowed;
		this.name = p_nodeName;
		this.children=new Array();
 	};

	this.populateRoot= function(nodeTagsArr, attributeTagsArr) {
		this.children=new Array();
		this.name=nodeTagsArr[0];
	};

	this.printData= function() {
		var str= "{"+ this.name + ":[\n" + " tradein : " + this.tradein + ", cashdown : " + this.cashdown + ", amtowed : " + this.amtowed + "] ";
		//var tmp=this.printChildren(this.children);
		//str= str + tmp+"]}";
		return str;
	};
	
    this.printChildren= function(child) {
		  var str="";
		  for(var count=0; count < child.length; count++) {
			  str = str +"{" + child[count].name + ": {\n" + " tradein : " + child[count].tradein + ", cashdown : " + child[count].cashdown + ", amtowed : " + child[count].amtowed + ", ";
				  str=str + "\n";
				  var val=this.printChildren(child[count].children, str);
				  str= str + val+ "}\n";
			  }
		  return str;
	};
	}
	
	/**
	 * This function holds the vehicle information selected by the user. 
	 */
	function VehicleInfoTree(){
		this.children = new Array();
		this.parent=null;
		this.msrp = null;
		this.msrpact = null;
		this.type=null;
		this.vin=null;
		this.pkgdiscount=null;
		this.name= null;
		
		this.brandId=null;
		this.brand=null;
		this.modelFamilyId=null;
		this.modelFamily=null;
		this.modelYearId=null;
		this.modelYear=null;
		this.model=null;
		this.modelId=null;
		this.dealerId=null;
		this.delaer=null;

		
		this.populateNode= function(p_parent, p_msrp,p_msrpact, p_type, p_vin, p_pkgdiscount, p_nodeName,
			p_brandId,p_brand,p_modelFamilyId,p_modelFamily,p_model,p_modelId,p_modelYearId,p_modelYear,p_dealerId,
			p_delaer) {
		this.parent=p_parent;
		this.msrp = p_msrp;
		this.msrpact=p_msrpact;
		this.type = p_type;
		this.vin = p_vin;
		this.pkgdiscount = p_pkgdiscount;
		this.name = p_nodeName;
		this.brandId=p_brandId;
		this.brand=p_brand;
		this.modelFamilyId=p_modelFamilyId;
		this.modelFamily=p_modelFamily;
		this.modelYearId=p_modelYearId;
		this.modelYear=p_modelYear;
		this.model=p_model;
		this.modelId=p_modelId;
		this.dealerId=p_dealerId;
		this.delaer=p_delaer;
		this.children=new Array();
 	};

	this.populateRoot= function(nodeTagsArr, attributeTagsArr) {
		this.children=new Array();
		this.name=nodeTagsArr[0];
	};

	this.printData= function() {
		var str= "{"+ this.name + ":[\n" + " msrp : " + this.msrp + " msrpact : " + this.msrpact + ", type : " + this.type + ", vin : " + this.vin + ", pkgdiscount :"+this.pkgdiscount +"] ";
		//var tmp=this.printChildren(this.children);
		//str= str + tmp+"]}";
		return str;
	};
	
    this.printChildren= function(child) {
		  var str="";
		  for(var count=0; count < child.length; count++) {
			  str = str +"{" + child[count].name + ": {\n" + " msrp : " + child[count].tradein + ", type : " + child[count].cashdown + ", vin : " + child[count].amtowed + ", pkgdiscount :" +child[count].pkgdiscount;
				  str=str + "\n";
				  var val=this.printChildren(child[count].children, str);
				  str= str + val+ "}\n";
			  }
		  return str;
	};
	}
	
/**
 * This function is used to populate the purchase prgram data. 
 * This function gets the node of the xml, parent, baseprice etc. 
 * and create the incentive Object and payment Object. 
 * Later this payment Object and incentive object will be used to populate the values.
 *   
 * @param {Object} array
 * @param {Object} count
 * @param {Object} list
 * @param {Object} type
 * @param {Object} basePrice
 * @param {Object} annualdistallowed
 * @param {Object} parent
 * @param {Object} nodeName
 */	
function populatePurchaseData(array, count, list, type, basePrice, annualdistallowed, parent, nodeName) {
	var program = new ProgramTree();
	
	//populatePrograms= function(p_parent, p_nodeName, p_type, p_baseprice, p_annualdistallowed)
	program.populatePrograms(parent, nodeName, type, basePrice, annualdistallowed);
	//fill in incentives data
	var incentivesList = list.getElementsByTagName("incentive");
	for(var i=0; i < incentivesList.length; i++) {
		var incentiveObj = new ProgramTree();
		var incentive = incentivesList[i];
		var id = getValueOfAttribute(incentive,"id");
		var type = getValueOfAttribute(incentive, "type");
		var det = incentive.getElementsByTagName("detail")[0];
		var disclosure = incentive.getElementsByTagName("disclosure")[0];
//		var valu = getValueOfAttribute(det, "value");
		var valu = getValueOfAttribute(det, "amount");
		var term = getValueOfAttribute(det, "term");
		incentiveObj.populateIncentives(program, nodeName, id, type, getValueOfNode(det), valu, term, getValueOfNode(disclosure));
		program.incentiveChildren[i] = incentiveObj;
	}

	program.buildNetDownPayment("netdownpayments", program, list);

	//fill in payment data
	var paymentList = list.getElementsByTagName("payment");
	for(var i=0; i < paymentList.length; i++) {
		var paymentObj = new ProgramTree();
		
		var payment = paymentList[i];
		var term = getValueOfAttribute(payment, "term");
		var def = getValueOfAttribute(payment, "default");
		
		var totaldownpayment = payment.getElementsByTagName("totaldownpayment")[0];
		var totalofpayments = payment.getElementsByTagName("totalOfPayments")[0];
		var tdpIncentives = getValueOfAttribute(totaldownpayment, "incentives");
		
		var apr = payment.getElementsByTagName("apr")[0];
		var aprIncentives = getValueOfAttribute(apr, "incentives");
		
		var monthlypayment = payment.getElementsByTagName("monthlypayment")[0];
		var maxcashallowed = payment.getElementsByTagName("maxcashallowed")[0];
		var amtfinanced = payment.getElementsByTagName("amtfinanced")[0];
		
		var residual = payment.getElementsByTagName("residual")[0];
		var amtduesigning = payment.getElementsByTagName("amtduesigning")[0];
		var securitydeposit = getValueOfAttribute(amtduesigning, "securitydeposit");
		var acquisitionfee = getValueOfAttribute(amtduesigning, "acquisitionfee");
		var netcapcost = payment.getElementsByTagName("netcapcost")[0];
		var mesage = payment.getElementsByTagName("message")[0];
		var iD = getValueOfAttribute(mesage, "id");
		var typ = getValueOfAttribute(mesage, "type");
		var balloonpayment = payment.getElementsByTagName("balloonPayment")[0];
		var singlepayleaseamt = payment.getElementsByTagName("singlePayLeaseAmt")[0];
		
		paymentObj.populatePayments(program, nodeName, term, def, getValueOfNode(totaldownpayment), getValueOfNode(totalofpayments),tdpIncentives, getValueOfNode(apr), aprIncentives, getValueOfNode(monthlypayment), getValueOfNode(maxcashallowed), getValueOfNode(amtfinanced),
		getValueOfNode(residual), getValueOfNode(amtduesigning), securitydeposit, acquisitionfee, getValueOfNode(netcapcost), 
		getValueOfNode(mesage), iD, typ, getValueOfNode(balloonpayment), getValueOfNode(singlepayleaseamt));
		
		program.paymentChildren[i] = paymentObj;
	}
	
	array[count] = program;
}

/**
 * This function contains all the required function to fill the detail View Page. 
 * All the function called will be prefixed with VehicleDetailData.. 
 * These function can be accessed only using the prefix.
 */	
var VehicleDetailData ={
	netDownPaymentTree: new NetDownPaymentTree(),
	vehicleInfoTree: new VehicleInfoTree(),
	programTree: new Array(),
	thisPurchaseTerm: null,
	thisLeaseTerm: null,
	thisFPlusTerm: null,

	checkIfXMLPresent: function(nodeName, xmlDocument) {
		 var list= xmlDocument.getElementsByTagName(nodeName);
		 if(list.length > 0 ) {
			 return true;
		 } else {
			 return false;
		 }
	},

	getInboundBrand: function(nodeName, xmlDocument) {
		 var list= xmlDocument.getElementsByTagName(nodeName);
		 var InboundBrandVal;
		 if(InboundBrandVal != null) {
			InboundBrandVal = list[0].getAttribute("InboundBrand");
			inboundBrand = InboundBrandVal;
		 }
	},

	//This function is used to build the Net Down Payment.
	buildNetDownPayment: function(array, nodeName, parent, xmlDocument) {
	var list= xmlDocument.getElementsByTagName(nodeName)[0];
	if (list.childNodes.length>0) {
		   array = new NetDownPaymentTree();
           var cashdown = list.getElementsByTagName("cashdown")[0];
		   var tradein = list.getElementsByTagName("tradein")[0];
		   var amtowed = tradein.getAttribute("amtowed");
		   VehicleDetailData.netDownPaymentTree.populateNode(parent, cashdown.childNodes[0].nodeValue, tradein.childNodes[0].nodeValue, amtowed, nodeName);
       }
	},
		
	//This function is used to build the vehicle Info.
	buildVehicleInfo: function(array, nodeName, parent, xmlDocument) {
	//alert("in buildVehicleInfo function ");
	var list= xmlDocument.getElementsByTagName(nodeName)[0];	
	if (list.childNodes.length>0) {
		   array = new VehicleInfoTree();
		   var msrp = list.getAttribute("msrp");
		   var msrpact = list.getAttribute("msrpact");
		   var type = list.getAttribute("type");
		   var vin = list.getAttribute("vin");
		   var pkgdiscount = list.getAttribute("pkgdiscount");
		   
		   var brandNode=xmlDocument.getElementsByTagName("brand")[0];
		   var brand = getValueOfNode(brandNode);
		   var brandId = getValueOfAttribute(brandNode, "id");
			
		   var modelFamilyNode=xmlDocument.getElementsByTagName("modelfamily")[0];
           var modelFamily;
		   var modelFamilyId;
		   if(modelFamilyNode.childNodes[0] != null){
		   		 modelFamily = getValueOfNode(modelFamilyNode);
				 modelFamilyId = getValueOfAttribute(modelFamilyNode, "id");
			 }else{
			    modelFamily ="";
				modelFamilyId ="";
		   }
		   
		   var modelNode=xmlDocument.getElementsByTagName("model")[0];
		   var model;
   		   var modelId
		   if(modelNode.childNodes[0] != null){
			   model= getValueOfNode(modelNode);
			   modelId = getValueOfAttribute(modelNode, "id");
		   }else{
			   model= "";
			   modelId = "";
		   }


		   var modelYearNode=xmlDocument.getElementsByTagName("modelyear")[0];
		   var modelYear;
           var modelYearId;
			if(modelYearNode.childNodes[0] != null){
			  modelYear = getValueOfNode(modelYearNode);
			  modelYearId = getValueOfAttribute(modelYearNode, "id");
			}else{
				modelYear = "";
			  modelYearId = "";
			}
				   
		   var dealerNode=xmlDocument.getElementsByTagName("dealer")[0];
		   var dealer = getValueOfNode(dealerNode);
		   var dealerId = getValueOfAttribute(dealerNode, "id");

		   VehicleDetailData.vehicleInfoTree.populateNode(parent, msrp,msrpact,type, vin, pkgdiscount,
		  nodeName,brandId,brand,modelFamilyId,modelFamily,model,modelId,modelYearId,modelYear,dealerId,dealer);
		  
       }
	},
	
	//This function is used to fill the vehicle information at the load of the page. This is called on once.
	fillVehicleInfo: function(form){
		VehicleDetailData.fillPurchaseVehicleInfo(form);
		VehicleDetailData.fillLeaseVehicleInfo(form);
		VehicleDetailData.fillFPlusVehicleInfo(form);
	},
	
	//This function is used to fill the vehicle information of purchase program. 
	//This will be called when there is a change in values of apr or cashdown in this program.
	fillPurchaseVehicleInfo: function(form){
		//alert("In fillVehicle Info ");
		form.rVehiclePrice.value = VehicleDetailData.vehicleInfoTree.msrp;
	},
	
	//This function is used to fill the vehicle information of lease Program. 
	//This will be called when there is a change in values of apr or cashdown in this program.
	fillLeaseVehicleInfo: function(form){
		//alert("In fillVehicle Info ");
		form.lCapitalizedCost.value = VehicleDetailData.vehicleInfoTree.msrp;
	},
	
	fillFPlusVehicleInfo: function(form){
		form.pVehiclePrice.value = VehicleDetailData.vehicleInfoTree.msrp;
	},
	
	//This function is used to build the programs.
	buildProgram: function(array, nodeName, parent, xmlDocument) {
		//alert("in build Program function ");
		var list= xmlDocument.getElementsByTagName(nodeName);
		for(var count=0; count < list.length; count++) {
			var type = list[count].getAttribute("type");
			var basePrice = list[count].getAttribute("baseprice");
			var annualdistallowed = list[count].getAttribute("annualdistallowed");
			parent.name =type;
			populatePurchaseData(array, count, list[count], type, basePrice, annualdistallowed, parent, nodeName);
		}
	},
	//This function is used to fill the purchase program. 
	//This is the main method that will be used later by other function to load the purchase program.
	fillPurchaseProgram: function(form){
		form.rTerm.options.length = 1;
	  	form.rTerm.options.selectedIndex = 0;
		
		i = 0;
		var defaultSelectedFlag = false;
		var fC= VehicleDetailData.programTree[0].paymentChildren;
		for (fIndex in fC) {
			form.rTerm.options[i] = new Option(fC[fIndex].term, fC[fIndex].term);
			if (fC[fIndex].def == 'true') {
				form.rTerm.selectedIndex = i;
				VehicleDetailData.thisPurchaseTerm = fC[fIndex];
				VehicleDetailData.fillPurchaseData(form, VehicleDetailData.thisPurchaseTerm);
				defaultSelectedFlag = true;
			}
			i++;
	  	}
		if(!defaultSelectedFlag) {
			VehicleDetailData.changePurchaseTerm(form);
		}
		
	},
	//This function is used to fill the lease program. 
	//This is the main method that will be used later by other function to load the lease program.
	fillLeaseProgram: function(form){
		form.lTerm.options.length = 1;
	  	form.lTerm.options.selectedIndex = 0;
		
		i=0;
		defaultSelectedFlag = false;
		var ltP= VehicleDetailData.programTree[1].paymentChildren;
		for (fIndex in ltP) {
			form.lTerm.options[i] = new Option(ltP[fIndex].term, ltP[fIndex].term);
			if (ltP[fIndex].def == 'true') {
				form.lTerm.selectedIndex = i;
				VehicleDetailData.thisLeaseTerm = ltP[fIndex];
				VehicleDetailData.fillLeaseData(form, VehicleDetailData.thisLeaseTerm);
			}
			i++;
	  	}
		if(!defaultSelectedFlag) {
			VehicleDetailData.changeLeaseTerm(form);
		}
	},
	
	fillSinglePayLease: function(form, term, singlePayLeaseAmt, apr){
		form.splTerm.options.length = 1;
		
		if(term == '24'){
			form.splTerm.options[1] = new Option(term, term);
			form.splMonthly.value = singlePayLeaseAmt;
			form.splApr.value = apr;
		} else if (term == '36'){
			form.splTerm.options[1] = new Option(term, term);
			form.splMonthly.value = singlePayLeaseAmt;
			form.splApr.value = apr;
		} else {
			form.splTerm.options[1] = new Option("N/A", "N/A");
			form.splMonthly.value = "N/A";
			form.splApr.value = "N/A";
		}
		form.splTerm.options.selectedIndex = 1;
	},

	//This function is used to fill the finance Plus program. 
	//This is the main method that will be used later by other function to load the finance Plus program.
	fillFPlusProgram: function(form){
		form.pTerm.options.length = 1;
	  	form.pTerm.options.selectedIndex = 0;
		
		i=0;
		defaultSelectedFlag = false;
		var ptP= VehicleDetailData.programTree[2].paymentChildren;
		for (fIndex in ptP) {
			form.pTerm.options[i] = new Option(ptP[fIndex].term, ptP[fIndex].term);
			if (ptP[fIndex].def == 'true') {
				form.pTerm.selectedIndex = i;
				VehicleDetailData.thisFPlusTerm = ptP[fIndex];
				VehicleDetailData.fillFPlusData(form, VehicleDetailData.thisFPlusTerm);
			}
			i++;
	  	}
		if(!defaultSelectedFlag) {
			VehicleDetailData.changeFPlusTerm(form);
		}
		
	},
	
	//This function is used to fill the all the program. 
	//This function will be called once on the page load.
	fillPrograms: function(form) {
		VehicleDetailData.fillPurchaseProgram(form);
		VehicleDetailData.fillLeaseProgram(form);
		VehicleDetailData.fillFPlusProgram(form);
	},
	
	//This function is used to fill the data when the term drop down is changed. 
	//This function will change the details of pruchase program only.
	changePurchaseTerm: function(form) {
	  VehicleDetailData.thisPurchaseTerm = VehicleDetailData.programTree[0].paymentChildren[form.rTerm.selectedIndex];
	  VehicleDetailData.fillPurchaseData(form, VehicleDetailData.thisPurchaseTerm)
	},
	
	//This function is used to fill the purchase data.
	fillPurchaseData: function(form, purchaseTree) {
		if(purchaseTree != null && purchaseTree != 'undefined') {
			if(purchaseTree.monthlypayment != null) {
				var retailMonthlyPayment = parseFloat(purchaseTree.monthlypayment);
				if(!isNaN(retailMonthlyPayment) && retailMonthlyPayment>=0) {
					form.rMonthly.value = purchaseTree.monthlypayment;
				} else {
					form.rMonthly.value = "";
				}
			} else if(purchaseTree.monthlypayment == null) {
				form.rMonthly.value = '';
			} 
			if(purchaseTree.totaldownpayment != null && parseFloat(purchaseTree.totaldownpayment)>0) {
				form.rTotalDownPayment.value = purchaseTree.totaldownpayment;
			} else {
				form.rTotalDownPayment.value = "0.00";
			}
			if(purchaseTree.amtfinanced != null && parseFloat(purchaseTree.amtfinanced)>0) {
				form.rAmountFinanced.value = purchaseTree.amtfinanced;
			} else {
				form.rAmountFinanced.value = "0.00";
			}
			
			if(purchaseTree.apr != null) {
				form.rApr.value = purchaseTree.apr;
			} else if(purchaseTree.apr == null) {
				form.rApr.value = "";
			}

			if(purchaseTree.totalofpayments != null && parseFloat(purchaseTree.totalofpayments)>0) {
				form.rTotalOfPayments.value = purchaseTree.totalofpayments;
			} else {
				form.rTotalOfPayments.value = "0.00";
			}

			var purchaseProgram = VehicleDetailData.programTree[0];
			if(purchaseProgram.netDownPaymentTree.cashdown != null) {
				form.rCashDown.value = purchaseProgram.netDownPaymentTree.cashdown;
			}
			if(purchaseProgram.netDownPaymentTree.tradein != null) {
				form.rTradeInAmt.value = purchaseProgram.netDownPaymentTree.tradein;
			}
			if(purchaseProgram.netDownPaymentTree.amtowed != null) {
				form.rAmountOwed.value = purchaseProgram.netDownPaymentTree.amtowed;
			}
			if(purchaseProgram.baseprice != null) {
				form.rVehiclePrice.value = purchaseProgram.baseprice;
			}
		}
	},

	//This function is used to fill the data when the term drop down is changed. 
	//This function will change the details of lease program only.
	changeLeaseTerm: function(form) {
	  VehicleDetailData.thisLeaseTerm = VehicleDetailData.programTree[1].paymentChildren[form.lTerm.selectedIndex];
		if(VehicleDetailData.thisLeaseTerm!=undefined){
		  VehicleDetailData.fillLeaseData(form, VehicleDetailData.thisLeaseTerm);
		  var termObj = VehicleDetailData.programTree[1].paymentChildren[form.lTerm.selectedIndex];
		  var term = form.lTerm.options[form.lTerm.selectedIndex].value;
		  VehicleDetailData.fillSinglePayLease(form, term, termObj.singlepayleaseamt, termObj.apr);
		}
	},
	
	//This function is used to fill the lease data.
	fillLeaseData: function(form, leaseTree) {
		if(leaseTree != null && leaseTree != 'undefined') {
			if(VehicleDetailData.programTree[1].annualdistallowed != null) {
				form.lannualMileage.value = VehicleDetailData.programTree[1].annualdistallowed;
			}
			if(leaseTree.monthlypayment != null) {
				var leaseMonthlyPayment = parseFloat(leaseTree.monthlypayment);
				if(!isNaN(leaseMonthlyPayment) && leaseMonthlyPayment>=0) {
					form.lMonthly.value = leaseTree.monthlypayment;
				} else {
					form.lMonthly.value = "";
				}
			}
			if(leaseTree.apr != null) {
				form.lapr.value = leaseTree.apr;
			}
			if(leaseTree.totaldownpayment != null && parseFloat(leaseTree.totaldownpayment)>0) {
				form.lTotalDownPayment.value = leaseTree.totaldownpayment;
			} else {
				form.lTotalDownPayment.value = "0.00";
			}

			if(leaseTree.amtfinanced != null) {
				form.lAmountFinanced.value = leaseTree.amtfinanced;
			}
			if(leaseTree.residual != null && parseFloat(leaseTree.residual)>0) {
				form.lResidual.value = leaseTree.residual;
			} else {
				form.lResidual.value = "0.00";
			}
			/*if(leaseTree.netcapcost != null) {
				form.lNetCapCost.value = leaseTree.netcapcost;
			}*/
			/*if(leaseTree.acquisitionfee != null && parseFloat(leaseTree.acquisitionfee)>0) {
				form.lAcquisitionFee.value = leaseTree.acquisitionfee;
			} else {
				form.lAcquisitionFee.value = "0.00";
			}		 */
			if(form.lTerm.selectedIndex == 24){
				form.splMonthly.value = leaseTree.singlepayleaseamt;
				form.splApr.value = leaseTree.apr;
			}
			if(form.lTerm.selectedIndex == 36){
				form.splMonthly.value = leaseTree.singlepayleaseamt;
				form.splApr.value = leaseTree.apr;
			}

			var leaseProgram = VehicleDetailData.programTree[1];
			if(leaseProgram.netDownPaymentTree.cashdown != null) {
				form.lCashDown.value = leaseProgram.netDownPaymentTree.cashdown;
			}
			if(leaseProgram.netDownPaymentTree.tradein != null) {
				form.lTradeInAmt.value = leaseProgram.netDownPaymentTree.tradein;
			}
			if(leaseProgram.netDownPaymentTree.amtowed != null) {
				form.lAmountOwed.value = leaseProgram.netDownPaymentTree.amtowed;
			}
			if(leaseProgram.baseprice != null) {
				form.lCapitalizedCost.value = leaseProgram.baseprice;
			}
		}
		
	},
	
	//This function is used to fill the program Incentives. 
	//During the load of the page first time this fucntion is called and all the incentives are loaded. 
	//But when a particular program is loaded only its corresponding incentives will be loaded.
	//The parameters passed are divObj: This will contain the span id in which the incentives needs to be loaded.
	//doc: The document, positionId: The position of the program in the array, pgmType:Name of the field in a particular program.
	fillPgmIncentives: function(divObj,doc,positionId, pgmType ) {
		//alert("In fillPgmIncentives");
		divObj.innerHTML="";
		var table = doc.createElement ("table")
		table.setAttribute("border", "0");
		table.setAttribute("cellPadding","0");
		table.setAttribute("cellSpacing","0");		
		var objTR = doc.createElement ("tr")
		var objTD = doc.createElement ("td");
		objTD.setAttribute("colspan","2");
		objTD.setAttribute("width","15");
		objTD.setAttribute("class","textHighlightBrightBlue");
		var oCaption = doc.createElement("span");
		oCaption.setAttribute("class","textHighlightBrightBlue");
		var keyt = document.createTextNode("Incentives");
		try
		{
			oCaption.appendChild(keyt);
		}
		catch (e)
		{
			oCaption.innerText="Incentives";
		}

		objTD.appendChild(oCaption);
		objTR.appendChild(objTD);
		table.appendChild(objTR);
		
		var incentivesArr = VehicleDetailData.programTree[positionId].incentiveChildren;
		for (i in incentivesArr) {
			objTR = doc.createElement ("tr");
			objTD = doc.createElement ("td");	
			//The behaviour is different for IE and Mozilla.
			//This check is done for radio button to be created on the Fly. 
			//Radio button creation is different for mozilla and IE.
			if (window.ActiveXObject) {
			var objInput = doc.createElement ("<input type=\"radio\" name=\""+pgmType+"\">");
			} else {
				var objInput = doc.createElement ("input");
				objInput.type="radio";
				objInput.name=pgmType;
			}
			//objInput.value=incentivesArr[i].valu;
			objInput.setAttribute("value",incentivesArr[i].valu);
			var objId = "incentive"+pgmType;
			objInput.setAttribute("id",objId);
			//alert("incentivesArr[i].type= "+incentivesArr[i].type+":incentivesArr[i].amount="+incentivesArr[i].amount+":incentivesArr[i].valu="+incentivesArr[i].valu);
			if(incentivesArr[i].typ == "APR"){
             objInput.setAttribute("onClick","javascript:parent.nestFRAME.updateAPRAndTerm(this,'"+pgmType+"','"+incentivesArr[i].valu+"','"+incentivesArr[i].term+"')");
			}
			objTD.appendChild(objInput);
			objTR.appendChild(objTD);			
			objTD = doc.createElement ("td");	
			objTD.innerHTML=incentivesArr[i].detil;			
			objTR.appendChild(objTD);
			table.appendChild(objTR);
			divObj.appendChild(table);
	  	}
		//alert(divObj.innerHTML);
	},

	
	//This function is used to change the Finance Plus details when the finance plus term drop down is changed.
	changeFPlusTerm: function(form) {
	  VehicleDetailData.thisFPlusTerm = VehicleDetailData.programTree[2].paymentChildren[form.pTerm.selectedIndex];
	  if(VehicleDetailData.thisFPlusTerm!=undefined){
		VehicleDetailData.fillFPlusData(form, VehicleDetailData.thisFPlusTerm)
	  }
	},
	
	//This function is used to fill the Finance Plus data.
	fillFPlusData: function(form, fPlusTree) {
		//alert("In FillFPlusData:");
		if(fPlusTree != null && fPlusTree != 'undefined') {
			if(fPlusTree.monthlypayment != null) {
				var plusMonthlyPayment = parseFloat(fPlusTree.monthlypayment);
				if(!isNaN(plusMonthlyPayment) && plusMonthlyPayment>=0) {
					form.pMonthly.value = fPlusTree.monthlypayment;
				} else {
					form.pMonthly.value = "";
				}
			} else if(fPlusTree.monthlypayment == null) {
				form.pMonthly.value = '';
			} 
			if(fPlusTree.totaldownpayment != null && parseFloat(fPlusTree.totaldownpayment)>0) {
				form.pTotalDownPayment.value = fPlusTree.totaldownpayment;
			} else {
				form.pTotalDownPayment.value = "0.00";
			}
			if(fPlusTree.amtfinanced != null && parseFloat(fPlusTree.amtfinanced)>0) {
				form.pAmountFinanced.value = fPlusTree.amtfinanced;
			} else {
				form.pAmountFinanced.value = "0.00";
			}
			if(fPlusTree.apr != null) {
				form.pApr.value = fPlusTree.apr;
			} else if(fPlusTree.apr == null) {
				form.pApr.value = "";
			}
			if(fPlusTree.balloonpayment != null) {
				form.pballoonPayment.value = fPlusTree.balloonpayment;
			}
			var fPlusProgram = VehicleDetailData.programTree[2];
			if(fPlusProgram.netDownPaymentTree.cashdown != null) {
				form.pCashDown.value = fPlusProgram.netDownPaymentTree.cashdown;
			}
			if(fPlusProgram.netDownPaymentTree.tradein != null) {
				form.pTradeInAmt.value = fPlusProgram.netDownPaymentTree.tradein;	
			}
			if(fPlusProgram.netDownPaymentTree.amtowed != null) {
				form.pAmountOwed.value = fPlusProgram.netDownPaymentTree.amtowed;
			}
			if(fPlusProgram.baseprice != null) {
				form.pVehiclePrice.value = fPlusProgram.baseprice;
			}
		}
	}
};

	//This function is used to populate the XML file and parse the data and set it to the array. 
	//This array will later be used by the fill programs to get the data.
	function populateDetailTree(xmlDocument, form, doc) {
		//alert("123 xmlDocument="+xmlDocument);
		if(VehicleDetailData.checkIfXMLPresent(detailNodeTags[1],xmlDocument)){
			VehicleDetailData.netDownPaymentTree.populateRoot(detailNodeTags, netDownTags);
			VehicleDetailData.getInboundBrand(detailNodeTags[0], xmlDocument);
			VehicleDetailData.buildNetDownPayment(VehicleDetailData.netDownPaymentTree, detailNodeTags[2], VehicleDetailData.netDownPaymentTree, xmlDocument);
			VehicleDetailData.buildVehicleInfo(VehicleDetailData.vehicleInfoTree, detailNodeTags[1], VehicleDetailData.vehicleInfoTree, xmlDocument);
			VehicleDetailData.buildProgram(VehicleDetailData.programTree, detailNodeTags[3], VehicleDetailData, xmlDocument);
			VehicleDetailData.fillPrograms(form);
		} else {
			parent.document.getElementById("nestFRAME").src = "SelectVehicle.html";
		}
	}

	function populateDetailTreePrintEstimator(xmlDocument) {
		if(VehicleDetailData.checkIfXMLPresent(detailNodeTags[1],xmlDocument)){
			VehicleDetailData.netDownPaymentTree.populateRoot(detailNodeTags, netDownTags);
			VehicleDetailData.getInboundBrand(detailNodeTags[0], xmlDocument);
			VehicleDetailData.buildNetDownPayment(VehicleDetailData.netDownPaymentTree, detailNodeTags[2], VehicleDetailData.netDownPaymentTree, xmlDocument);
			VehicleDetailData.buildVehicleInfo(VehicleDetailData.vehicleInfoTree, detailNodeTags[1], VehicleDetailData.vehicleInfoTree, xmlDocument);
			VehicleDetailData.buildProgram(VehicleDetailData.programTree, detailNodeTags[3], VehicleDetailData, xmlDocument);
		} else {
			parent.document.getElementById("nestFRAME").src = "SelectVehicle.html";
		}
	}