//declare variables
var xmlDocument; //used as general container
var filename;
var isLoaded; //flag to indicate if xml file is loaded successful
//end declare
/////////////////////////To load the XML file////////////////////////////
/*
usage: to load a xml file
requeirement: declare 'xmlDocument' before calling 'loadXML(filename)'
*/
function loadXML(filename){
	// IE5+
	if (window.ActiveXObject)
	{
		xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
		xmlDocument.async=false;
		xmlDocument.resolveExternals = false;		
		xmlDocument.load(filename);
		if (xmlDocument.parseError.errorCode != 0 ) {
		    var myErr = xmlDocument.parseError;
	        alert("You have error " + myErr.reason);
			return false;
		}
	} 
	// NN6+ and Mozilla
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDocument = document.implementation.createDocument("","",null);
		xmlDocument.async=false;
		xmlDocument.resolveExternals = false;
		xmlDocument.ignoreWhite = true;
		xmlDocument.load(filename);
	}
	return true;
}
/////////////////////////////////////////////////////////////////////
//0-IE5 1-NS6 or Mozilla
function browserDetect(){
	if (window.ActiveXObject) return 0;
	else if (document.implementation && document.implementation.createDocument) return 1;
}
/////////////////////////////////////////////////////////////////////
/*
function XPathEvaluation(XPathString){
	var result;
	if(browserDetect() == 1){//mozilla
		result = xmlDocument.evaluate(XPathString, xmlDocument, null, 0 , null);
	}
	else if (browserDetect() == 0) {//IE 5+
		result = xmlDocument.documentElement.selectNodes(XPathString);		
	}	
	return result;
}
*/
/////////////////////////To get interest rate////////////////////////////
/*
function getInterestRate(year){
	var theRate;
	var xpath = "//InterestList/Interest[Year=" + year + "]/Rate";
	theRate = XPathEvaluation(xpath);
	if(browserDetect() == 1){//mozilla
		var theNode = theRate.iterateNext();
		if(theNode == null){
			return null;
		}
		theRate = theNode.firstChild.nodeValue;
	}
	else if(browserDetect() == 0){//IE 5+
		if(theRate[0] == null){
			return null;
		}
		theRate = theRate[0].firstChild.nodeValue;
	}
	return theRate;
}
*/
/////////////////////////////////////////////////////////////////////
/*
function getMotorPrice(ID){
	var thePrice;
	var xpath = "//MobileList/Mobile[@ID='" + ID + "']/Price";
	thePrice = XPathEvaluation(xpath);
	if(browserDetect() == 1){//mozilla
		var theNode = thePrice.iterateNext();
		if(theNode == null){
			return null;
		}
			thePrice = theNode.firstChild.nodeValue;		
	}
	else if(browserDetect() == 0){//IE 5+
		if(thePrice[0] == null){
			return null;
		}
		thePrice = thePrice[0].firstChild.nodeValue;
	}
	return thePrice;
}
*/
/////////////////////////////////////////////////////////////////////
function getModelList(carID){
	filename = 'data/mobiledetails_all.xml';
	isLoaded = loadXML(filename);
	var theModelAry = new Array();//two dimension array	
	var theModelDesc;
	var theModelPrice;
	var theModelList;
	var theModelNode;
	var tempAryEntry;
	theCarList = xmlDocument.getElementsByTagName('Mobile');	
	for(var i = 0;i<theCarList.length;i++){	
		theCarNode = theCarList[i];
		if(theCarNode.getElementsByTagName('carID')[0].firstChild.nodeValue == carID){ 
			for (var x = 0; x<theCarNode.getElementsByTagName('carModel').length;x++){				
				theModelDesc = theCarNode.getElementsByTagName('Model')[x].firstChild.nodeValue;
				theModelPrice = theCarNode.getElementsByTagName('Price')[x].firstChild.nodeValue;
				tempAryEntry = new Array(2);
				tempAryEntry[0] = theModelPrice;
				tempAryEntry[1] = theModelDesc;
				theModelAry.push(tempAryEntry);	
			}	
		}
	}
	return theModelAry;
}
/////////////////////////////////////////////////////////////////////
function getCarList(carID){
	filename = 'data/mobiledetails_all.xml';
	isLoaded = loadXML(filename);
	var theCarAry = new Array();//two dimension array	
	var theCarID;
	var theCarName;
	var theCarList;
	var theCarNode;
	var tempAryEntry;
	theCarList = xmlDocument.getElementsByTagName('Mobile');	
	for(var i = 0;i<theCarList.length;i++){
		theCarNode = theCarList[i];
		theCarID = theCarNode.getElementsByTagName('carID')[0].firstChild.nodeValue;
		theCarName = theCarNode.getElementsByTagName('carName')[0].firstChild.nodeValue;
		if(carID == 0){
		tempAryEntry = new Array(2);
		tempAryEntry[0] = theCarID;
		tempAryEntry[1] = theCarName;
		theCarAry.push(tempAryEntry);
		}else if (carID == theCarID){
		tempAryEntry = new Array(2);
		tempAryEntry[0] = theCarID;
		tempAryEntry[1] = theCarName;
		theCarAry.push(tempAryEntry);
		}
	}
	return theCarAry;
}
/////////////////////////////////////////////////////////////////////

function getTenureList(){
	filename = 'data/interestdetails.xml';
	isLoaded = loadXML(filename);
	var theTenureAry = new Array();
	var theTenureYear;
	var theTenureRate;
	var theTenureList;
	var theTenureNode;
	var tempAryEntry;
	theTenureList = xmlDocument.getElementsByTagName('Interest');
	for(var i = 0;i<theTenureList.length;i++){
		theTenureNode = theTenureList[i];
		theTenureYear = theTenureNode.getElementsByTagName('Year')[0].firstChild.nodeValue;
		theTenureRate = theTenureNode.getElementsByTagName('Rate')[0].firstChild.nodeValue;
		tempAryEntry = new Array(2);
		tempAryEntry[0] = theTenureRate;
		tempAryEntry[1] = theTenureYear;
		theTenureAry.push(tempAryEntry);
	}
	return theTenureAry;
}
