
var MapleCountry = {
		loaded:false,
		doLoad:function()
		{
			if(!this.loaded)
			{
				 $A(document.getElementsByTagName("script")).findAll( function(s) {
				      return (s.src && s.src.match(/country\.js(\?.*)?$/))
				    }).each( function(s) {
				      var path = s.src.replace(/country\.js(\?.*)?$/,'');
				      var includes = s.src.match(/\?.*load=([a-z,]*)/);
				      var datapath = path + "generated/countryData.js";
						new Ajax.Request(datapath, { method: 'get',force:true, onSuccess:function(t) {
							}
						});	
		
				    });
			}
		},
		onloaded: function()
		{
			this.loaded = true;
			if(this.onCountryParams)
			{
				this.doOnCountry(this.onCountryParams[0],
							this.onCountryParams[1],
							this.onCountryParams[2],
							this.onCountryParams[3]);
			}
		},
		initCountries: function(list,cSel)
			{
				for(var i=0;i<this.worldInfo.length;i++)
				{
					str = this.worldInfo[i];
					if(str)
					{
						v = str.substring(0,str.indexOf("|"));
						t = str.substring(str.indexOf("|")+1);
						
						opt = new Option(t,v);
						opt.value= v;
						opt.text = t;
						if(v == cSel) opt.selected = "true";
						
						// First try the DOM2 method ...
						try {
						list.add(opt,null);
						}
						// ... And if that doesn't work use the IE-only method
						catch (e) {
						list.add(opt,list.length);
						}
					}
				}
		},
		doOnCountry:function(cntrylist,statelist,cSel,sSel)
		{
		var states = $(statelist);
		var countries = $(cntrylist);
		if(!cSel)
			cSel = 'USA';
		if(countries != null && countries.options.length < 5)
		{
			this.initCountries(countries,cSel);
		}
		
		var sinfo = null;	
		if(countries != null) sinfo = this.countryInfo[countries.value];
		if(sinfo == null) sinfo = this.countryInfo[cSel];
		if(sinfo == null) return;
		if(states == null) return;
		
		var	ct;
		ct = states.options.length;
		for(var i=0;i<=ct;i++)
		{
			states.options[1] = null;
		}
		if(sinfo.length == 2)
		{
			sSel = sinfo[0].substring(0,sinfo[0].indexOf("|"));
		}
			for(i=0;i<sinfo.length;i++)
	{
		str = sinfo[i];
		if(str != null)
		{
			var v = str.substring(0,str.indexOf("|"));
			var t = str.substring(str.indexOf("|")+1);
			
			var opt = new Option();
			opt.value= v;
			opt.text = t;
			if(v == sSel) opt.selected = "true";
			
			// First try the DOM2 method ...
			try {
			states.add(opt,null);
			}
			// ... And if that doesn't work use the IE-only method
			catch (e) {
			states.add(opt,states.length);
			}
		}
	} 
			if(states.options.length == 2)
	{
		//states.style.visibility = "hidden";
		//states.disabled = true;
		
		//the 'states' cannot be disabled - it needs to be submitted
		var lab = $(statelist+"Label");
		if(lab != null)
			lab.style.visibility = "hidden";
	}
	else
	{
		//states.style.visibility = "visible";
		states.disabled=null;
		var lab = $(statelist+"Label");
		if(lab != null)
			lab.style.visibility = "visible";
	}
	
	Event.fire(document.body, "maple:onCountryLoaded", {});
			
  }
		
		
	};

onCountry = function(cntrylist,statelist,cSel,sSel)
{
	if(!MapleCountry.loaded)
	{
		MapleCountry.onCountryParams = [cntrylist,statelist,cSel,sSel];
		MapleCountry.doLoad();
	}
	else
	{
		MapleCountry.doOnCountry(cntrylist,statelist,cSel,sSel);
	}
}
initCountries = function(l,c){onCountry(l,null,c,null);}



