// JavaScript Document
// US cities

USCities.cities;
USCities.combo;
USCities.conchange;

function USCities(acronym, parent, thisid, aclass, cityName) {

	// initialize XMLHttpRequest object
	var xmlobj=null;
	var data=new Array();
	this.cities = new Array();
	this.combo = document.createElement('SELECT');
	var me = this;
	
	if(acronym==null) {
		//adding a header to the combo
		var opt = document.createElement("OPTION");
		opt.text = '-- Choose a state --';
		opt.value = -1;
		me.combo.options.add(opt);
		var parent = document.getElementById(parent);
		var old = document.getElementById(thisid);
		if(old != null)
			parent.removeChild(old);
		me.combo.id = thisid;
		me.combo.className = aclass;
		parent.appendChild(me.combo);
		return true;
	}
	
	var doc = 'XML/'+acronym+'.xml';
	
	// 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) {
		me.combo = document.createElement('SELECT');
		var opt = document.createElement("OPTION");
		opt.text = '-- Choose a city --';
		opt.value = -1;
		me.combo.options.add(opt);
		data=xmlobj.getElementsByTagName('City');
		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 us = new Array();
				us.Name = option.text;
				if(data[i].getElementsByTagName('Latt')[0].firstChild!=null)
					us.Latt = data[i].getElementsByTagName('Latt')[0].firstChild.nodeValue;
				else
					us.Latt = 0;
				if(data[i].getElementsByTagName('Long')[0].firstChild!=null)
					us.Long = data[i].getElementsByTagName('Long')[0].firstChild.nodeValue;
				else
					us.Long = 0;
				me.cities.push(us);
				me.combo.options.add(option);
			}
			me.showCombo(parent);
		}
	}
	
	this.showCombo = function  (parent) {
		var parent = document.getElementById(parent);
		var old = document.getElementById(thisid);
		me.combo.id = thisid;
		me.combo.className = aclass;
		if(old != null)
			parent.replaceChild(me.combo,old);
		else			
			parent.appendChild(me.combo);
		me.combo.onchange = me.conchange;
		if(cityName!=null){
			for(var i=0;i< me.combo.options.length;i++) {
//				alert(cityName +'   '+ me.combo.options[i].text)
				if(me.combo.options[i].text == cityName){
					me.combo.selectedIndex = i;
					break;
				}
			}
		}
	}
	
	this.getLattitude = function (index) {
		if(index==-1)
			return null;
		if(me.cities!=null && me.cities.length >0)
			return me.cities[index].Latt;
		else
			return null;
	}

	this.getLongitude = function (index) {
		if(index==-1)
			return null;
		if(me.cities!=null && me.cities.length >0)
			return me.cities[index].Long;
		else
			return null;
	}
	
	this.getCombo = function () {
		if(me.combo!=null)
			return me.combo;
		else
			return false;
	}
	
	this.getID = function () {
		return 'USCities';
	}
	
}
