// JavaScript Document
// JavaScript Document

/******************************************************************
WriteLayer: common function used to write all data to the page
*******************************************************************/
UtilObject = new Object();

/******************************************************************
ImportXML: common function used call all xml
*******************************************************************/
UtilObject.importXML = function(obj,xml,xmlLoc)
{
	var http_request = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
	  
		http_request = new XMLHttpRequest();
		
		if (http_request.overrideMimeType) {
		http_request.overrideMimeType('text/xml');
		}
		
		http_request.onreadystatechange = function () {
			if (http_request.readyState == 4) {
				var xmldoc = http_request.responseXML;
				//once finished call the appropriate function to store the xml
				obj.storeXML(xmldoc)
			}
		};
	  //pass in time stamp sp safari doesn't cache		
		  http_request.open('GET', xmlLoc+xml+"?"+ new Date().getTime(), true);
		  http_request.send(null);
		  
	} else if (window.ActiveXObject) { // IE
	 	http_request = new ActiveXObject("Microsoft.XMLDOM");
		http_request.onreadystatechange = function () {
			//once finished call the appropriate function to store the xml
			if (http_request.readyState == 4) 
				obj.storeXML(http_request);
		};		
	var xmlName = xml;
	http_request.load(xmlLoc+xmlName);		
  }
  if (!http_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }
}


pageData = new Object();
/******************************************************************
SetData: Called from the html page to get the ball rolling
*******************************************************************/
callXMLData = function(lang){
	//find the current language

	
	if(lang== undefined){
		lang = "en";	
	}
	//set properties
	pageData.lang = lang;
	pageData.xmlLoc = "/"+lang+"/xml/";
	pageData.xmlFile = "countryRates.xml";
	
	pageData.getData();
	//call to get the features		
}



pageData.getData = function(){
	var xml=this.xmlFile;
	UtilObject.importXML(pageData,xml,this.xmlLoc);
}


pageData.storeXML = function(xml,preLoad){
	
	var nodes = xml.getElementsByTagName('item');

	var total_nodes = nodes.length;

	var countries = [];

	for(var i=0; i<total_nodes; i++){
		var tempObj = [];
		
			var textNodes = nodes[i];
		
			if(textNodes.firstChild != undefined){
				tempObj.name = 	 textNodes.firstChild.nodeValue;	
			}
			
			tempObj.rate = textNodes.getAttribute("rate"); 
	
		
		countries.push(tempObj)
	}	
	//countries.sort(sortByName);
	this.showCountries(countries);
	
}

pageData.showCountries = function(countryArr){
				
	var displayData = '<select onchange="javascript: showRate(this);"><option value="">';
	if(this.lang == "fr"){
	 	displayData += 'Choisir un pays';
	}else{
		displayData += 'Choose a Country';
	}
	displayData +='</option>';
		
	
	for(var i=0;i<countryArr.length;i++){		
		displayData +='<option value="'+countryArr[i].rate+'">'+countryArr[i].name+'</option>';	
	}
	displayData+='</select>';
	getElement('countries').innerHTML = displayData;
}


function showRate(obj){
	getElement('rate').value = obj.value;
}
/*
function myArraySort(f,array) {
  var i, j;
  var currentValue;
  var currentObj;
  var compareObj;
  var compareValue;
  
  for(i=1; i<array.length;i++) {
    currentObj = array[i];
    currentValue = currentObj[f];
    
    j= i-1;
    compareObj = array[j];
    compareValue = compareObj[f];
    
   // while(j >=0 && escape(compareValue) > escape(currentValue)) {
	  
	 while(j >=0 && abc.indexOf(compareValue.charAt(0)) > abc.indexOf(currentValue.charAt(0))){
      array[j+1] = array[j];
      j--;
      if (j >=0) {
        compareObj = array[j];
        compareValue = compareObj[f];
	  break;
      }        
    }  
    array[j+1] = currentObj;
  }
}


var abc = 'AÀÁÂÃÄÅBßCÇDÐEÈÉÊËFGHIÌÍÎÏJ' + 'KLMNÑOÒÓÔÕÖØPQRSTÙÚÛÜUVWXYÝYZ';

function sortByName(a, b) {
	var x = escape(a.name);
	var y = escape(b.name);
    //var x = a.FirstName.toLowerCase();
    //var y = b.FirstName.toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
*/


