var flag=new Boolean(false);

//Called to populate the model content values
function populate(str,urlServlet)
{
 if(str=="1"){
	flag=true;
	var url=urlServlet+'?strQuery=initial';
	initRequest(url);	
	}
 else if(str=="2"){
	flag=false;
	initRequest(urlServlet);	
    } 
}

//Generating the XMLHttpRequest to call the servlet 
function initRequest(url) 
{		
        if (window.XMLHttpRequest) 
		{
            req = new XMLHttpRequest();
        }
		else if (window.ActiveXObject) 
		{
            isIE = true;
            req = new ActiveXObject("Microsoft.XMLHTTP");  
    	        } 	
		req.onreadystatechange =processRequest;

		req.open("GET", url, true); 
        req.send(null);
						
 }
 function processRequest()
  {
	if (req.readyState == 4) 
		{			
		if(req.status==200)	{		
			if(flag==true){											
			populateCountries();			
	        }			
			populateRates();
			}
		} 
  }
 
// Populate the country codes and names 
function populateCountries()
{
	var fieldValues=req.responseXML.getElementsByTagName("field");  
  	var varMunteenheid=fieldValues[2].firstChild.nodeValue;
    var varMunteenheidResultaat=fieldValues[3].firstChild.nodeValue;  	
    var countryList=req.responseXML.getElementsByTagName("rate");	 
	var munteenheidIndex=0;
	var munteenheidResultaatIndex=0;
        for (i=0;i<countryList.length; i++)
        {
			var code=countryList.item(i).getAttribute("isocode");
			var description=countryList[i].childNodes[0].firstChild.nodeValue;
			
document.calculateForm.munteenheid.options[i] = new Option(description+'('+code+')', description);
			document.calculateForm.munteenheidResultaat.options[i] = new Option(description+'('+code+')', description);    
		if(description==varMunteenheid)				
				munteenheidIndex=i;       
				
			if(description==varMunteenheidResultaat)
				munteenheidResultaatIndex=i;                
		}		
		document.calculateForm.munteenheid.options[munteenheidIndex].selected=true;
		document.calculateForm.munteenheidResultaat.options[munteenheidResultaatIndex].selected=true;	
}
// Populate the currency rate  
function populateRates()	
{
	 var fieldValues=req.responseXML.getElementsByTagName("field");  
  	 document.calculateForm.bedrag.value=fieldValues[0].firstChild.nodeValue;
     document.calculateForm.bedragResultaat.value=fieldValues[1].firstChild.nodeValue; 
 }
