// JavaScript Document
// Contries of the world

WorldCombo.countries;
WorldCombo.combo;

function WorldCombo(doc, parent, thisid, countryName) {
	
	// initialize XMLHttpRequest object
	var xmlobj=null;
	var data=new Array();
	this.countries = new Array();
	this.combo = document.createElement('SELECT');
	var me = this;
	
	// check for existing requests
    if(xmlobj!=null&&xmlobj.readyState!=0&&xmlobj.readyState!=4){
        xmlobj.abort();
    }
    try{
        // instantiate object for Mozilla, Nestcape, etc.
        xmlobj=new XMLHttpRequest();
    }
    catch(e){
        try{
            // instantiate object for Internet Explorer
            xmlobj=new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch(e){
            // Ajax is not supported by the browser
            xmlobj=null;
            return false;
        }
    }
    // assign state handler
    xmlobj.onreadystatechange= function() {
		// if request is completed
		if(xmlobj.readyState==4){
			// if status == 200 display text file
			if(xmlobj.status==200){
				// read XML data
				//data=xmlobj.responseXML.getElementsByTagName('Country');
				me.populateCombo(xmlobj.responseXML);
			}
			else{
				alert('Failed to get response :'+ xmlobj.statusText);
			}
		}
	}
    // open socket connection
    xmlobj.open('GET',doc,true);
    // send GET request
    xmlobj.send(null);
	
	this.populateCombo = function  (xmlobj) {
		data=xmlobj.getElementsByTagName('Country');
		if(data.length > 0) {
			for (var i=0;i<data.length;i++) {
				var option = document.createElement("OPTION");
				option.text = data[i].getElementsByTagName('Name')[0].firstChild.nodeValue;
				option.value = i;
				var wc = new Array();
				wc.Name = option.text;
				if(data[i].getElementsByTagName('Latt')[0].firstChild!=null)
					wc.Latt = data[i].getElementsByTagName('Latt')[0].firstChild.nodeValue;
				else
					wc.Latt = 0;
				if(data[i].getElementsByTagName('Long')[0].firstChild!=null)
					wc.Long = data[i].getElementsByTagName('Long')[0].firstChild.nodeValue;
				else
					wc.Long = 0;
				me.countries.push(wc);
				me.combo.options.add(option);
			}
			me.showCombo(parent);
		}
	}
	
	this.showCombo = function  (parent) {
		var parent = document.getElementById(parent);
		me.combo.id = thisid;
		if(countryName!=null){
			for(var i=0;i< me.combo.options.length;i++) {
				if(me.combo.options[i].text == countryName){
					me.combo.selectedIndex = i;
					break;
				}
			}
		}
		parent.appendChild(me.combo);
	}
	
	this.getLattitude = function (index) {
		if(me.countries!=null && me.countries.length >0)
			return me.countries[index].Latt;
		else
			return null;
	}

	this.getLongitude = function (index) {
		if(me.countries!=null && me.countries.length >0)
			return me.countries[index].Long;
		else
			return null;
	}
	
	this.getCombo = function () {
		if(me.combo!=null)
			return me.combo;
		else
			return false;
	}

}
