// JavaScript Document
// International cities

ICities.cities;
ICities.comboState;
ICities.comboCity;
ICities.aclass;
ICities.sonchange;
ICities.conchange;

function ICities(doc, parent, thisid, aclass, regionName) {

	// initialize XMLHttpRequest object
	var xmlobj=null;
	var data=new Array();
	this.cities = new Array();
	this.comboState = document.createElement('SELECT');
	this.comboCity = document.createElement('SELECT');
	var me = this;
	this.aclass = aclass;
	
	//adding a header to the combo
	var opt2 = document.createElement("OPTION");
	opt2.text = '-- Choose a region --';
	opt2.value = -1;
	this.comboCity.options.add(opt2);
	
	// 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.populateComboState(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.populateComboState = function (xmlobj) {
		
		var opt = document.createElement("OPTION");
		opt.text = '-- Select a region --';
		opt.value = -1;
		this.comboState.options.add(opt);
		data=xmlobj.getElementsByTagName('City');
		var states = new Array();
		if(data.length > 0) {
			for (var i=0;i<data.length;i++) {
				
				// finding different states
				var st = data[i].getElementsByTagName('State')[0].firstChild.nodeValue;
				var insert=true;
				for(var k=0;k < states.length;k++) {
					if(states[k] == st)
						insert=false;
				}
				if(insert)
					states.push(st);
					
				// populating cities array
				var city = new Array();
				city.Name = data[i].getElementsByTagName('Name')[0].firstChild.nodeValue;
				city.State = st;
				if(data[i].getElementsByTagName('Latt')[0].firstChild!=null)
					city.Latt = data[i].getElementsByTagName('Latt')[0].firstChild.nodeValue;
				else
					city.Latt = 0;
				if(data[i].getElementsByTagName('Long')[0].firstChild!=null)
					city.Long = data[i].getElementsByTagName('Long')[0].firstChild.nodeValue;
				else
					city.Long = 0;
				
				me.cities.push(city);
			}
		}
		// populating combo State
		for(var j=0;j< states.length;j++) {
			var option = document.createElement("OPTION");
			option.text = states[j];
			option.value = states[j];
			me.comboState.options.add(option);
		}
		
		me.showComboState(parent);
	}

	this.populateComboCity = function (state, parent, cid) {
		//adding a header to the combo
		me.comboCity = document.createElement('SELECT');
		var opt2 = document.createElement("OPTION");
		opt2.text = '-- Choose a city --';
		opt2.value = -1;
		me.comboCity.options.add(opt2);
		for(var i=0;i<me.cities.length;i++) {
			var st = me.cities[i].State;
			if(st==state) {
				var option = document.createElement("OPTION");
				option.text = me.cities[i].Name;
				option.value = i;
				me.comboCity.options.add(option);
			}
		}
		me.showComboCity(parent, cid);
	}

	this.showComboState = function  (parent) {
		var parent = document.getElementById(parent);
		var old = document.getElementById(thisid);
		if(old != null)
			parent.removeChild(old);
		me.comboState.id = thisid;
		me.comboState.className = me.aclass;
		me.comboState.onchange = me.sonchange;
		if(regionName!=null) {
			for(var i=0;i< me.comboState.options.length;i++) {
				if(me.comboState.options[i].text == regionName){
					me.comboState.selectedIndex = i;
					me.comboState.className = me.aclass;
					break;
				}
			}
		}
		parent.appendChild(me.comboState);
	}

	this.showComboCity = function  (parent, cid) {
		var parent = document.getElementById(parent);
		var old = document.getElementById(cid);
		me.comboCity.className = me.aclass;
		me.comboCity.id = cid;
		me.comboCity.onchange = me.conchange;
		if(old != null)
			parent.replaceChild(me.comboCity,old);
		else			
			parent.appendChild(me.comboCity);
	}

	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.getComboState = function () {
		if(me.comboState!=null)
			return me.comboState;
		else
			return false;
	}
	
	this.getComboCity = function () {
		if(me.comboCity!=null)
			return me.comboCity;
		else
			return false;
	}
	
	this.getID = function () {
		return 'ICities';
	}
	

}
