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