var ccities=new Array();
var state_options=new Object();
var states_sel_index=0;
var states_cur=null;
var max_cities=-1;
var states_last="";

function init_states(obj)
{
		state_options=obj;

		state_options["rcity"].onkeydown=function (event) {
			var obj=null;
			if(max_cities==-1) return;
			var code;
			if(!event) event=window.event;
			if(event.keyCode) code=event.keyCode;
			else if(event.watch) code=event.watch;
			switch(code)
			{
				case 38://up
						if(states_sel_index>0)
						{
							obj=document.getElementById('dropStates_'+states_sel_index);
							if(obj) obj.style.backgroundColor='white';
							states_sel_index--; 
							obj=document.getElementById('dropStates_'+states_sel_index);
							if(obj) obj.style.backgroundColor='#E6E6E6';
						}
				break;
				case 40://down	  
						if(states_sel_index<max_cities-1)
						{
							obj=document.getElementById('dropStates_'+states_sel_index);
							if(obj) obj.style.backgroundColor='white';
							states_sel_index++; 
							obj=document.getElementById('dropStates_'+states_sel_index);
							if(obj) obj.style.backgroundColor='#E6E6E6';
						}
				break;
			}
	};	
	
	state_options["rcity"].onkeyup=function (event) {
		if(max_cities==-1) return;
		var code;
		if(!event) event=window.event;
		if(event.keyCode) code=event.keyCode;
		else if(event.watch) code=event.watch;
		switch(code)
		{
			case 13:
				obj=document.getElementById('dropStates_'+states_sel_index);
				if(obj)
				{
					state_options["city"].value=obj.innerHTML;
					hide_menu(false);
				}
				break;
		}
	};
}

function get_states(obj,id)
{

	if(obj.selectedIndex >=0)
	{
		id=obj.options[obj.selectedIndex].value;
	}
    makePOSTRequest('/geo_ajax.php',"getstates="+id+"&sid="+state_options["sid"],get_states_done);
}
		
function get_states_done(data)
{
	if(data.length>0)
	{
	 	//IE7 not support table-row, it's sucks
		if(navigator.userAgent.search(/MSIE/i)==-1)
		{
			state_options["statecontainer"].innerHTML=data;
            state_options["state"] = document.getElementById('state');
			if(state_options["rstate"]) state_options["rstate"].style.display="table-row";
			if(state_options["rcity"]) state_options["rcity"].style.display="table-row";
		}else{					
			//add_options(state_options["state"],data);
            state_options["statecontainer"].innerHTML=data;
			if(state_options["rstate"]) state_options["rstate"].style.display="block";
			if(state_options["rcity"]) state_options["rcity"].style.display="block";
		}
	}else{
        state_options["statecontainer"].innerHTML = '';
        state_options["state"] = false;
		if(state_options["rstate"]) state_options["rstate"].style.display="none";
		if(state_options["rcity"]) state_options["rcity"].style.display="none";
	}
}
	
function get_cities(obj)
{
    state_options["state"] = document.getElementById('state');    
	if(state_options["state"] && state_options["state"].selectedIndex>=0)
	{
		if(obj.value.length>0)
		{
			if(typeof ccities[state_options["state"].options[state_options["state"].selectedIndex].value]=="undefined")
			{
				makePOSTRequest('/geo_ajax.php',"getcities="+state_options["state"].options[state_options["state"].selectedIndex].value,get_cities_done);	
			}else{
				sort_cities();
			}
		}else{
			hide_menu(true);
		}
	}
}
		
function get_cities_done(data)
{
	data=data.replace("&#039;","'");
	ccities[state_options["state"].options[state_options["state"].selectedIndex].value]=data.split("#");
	sort_cities();
}
		
var html;
function sort_cities()
{
	if(state_options["city"] && state_options["state"])
	{
		if(states_last!=state_options["city"].value) 
		{
			states_last=state_options["city"].value;
			states_sel_index=0;
		}
		id=state_options["state"].options[state_options["state"].selectedIndex].value;
		state_options["dcity"].innerHTML='';v=state_options["city"].value;
		html='';
		for(i=0,j=0;i<ccities[id].length && j<10;i++)
		{
			if(ccities[id][i].search(new RegExp('^'+v,'i'))!=-1)
			{
				html+='<div id="dropStates_'+j+'"';
				if(j==states_sel_index) 
				{
					state_cur=document.getElementById('dropStates_'+j);
					html+=' style="background-color:#E6E6E6;"';
				}
				html+=' " onclick="document.getElementById(\''+ state_options["city"].id+'\').value=\''+ccities[id][i]+'\';hide_menu(false);" onmouseover="document.getElementById(\'dropStates_\'+states_sel_index).style.backgroundColor=\'white\';this.style.background=\'#E6E6E6\';states_sel_index='+j+'" onmouseout="this.style.background=\'white\';document.getElementById(\'dropStates_\'+states_sel_index).style.backgroundColor=\'#E6E6E6\';">'+ccities[id][i]+"</div>";
				j++;
			}
		}
		if(j>0) 
		{
			state_options["dcity"].innerHTML+=html;
			max_cities=j;
			state_options["dcity"].style.display='block'; 
		}else{
			max_cities=-1;
		  hide_menu(false);
		}
	}
}
		
function hide_menu(del_city)
{
	if(state_options["city"] && state_options["dcity"])
	{
		state_options["dcity"].innerHTML='';
		if(del_city) 
		{
			state_options["city"].value='';
		}
		state_options["dcity"].style.display='none';
	}
}

function change_states(country,left_state)
{
   if(country.selectedIndex!=-1)
   {
   	 switch(parseInt(country.options[country.selectedIndex].value))
   	 {
   	 	 case 38://canada
   	 	 	document.getElementById(left_state).innerHTML="Province/Territory";
   	 	 	break;
   	 	 case 19://australia
   	 	 	document.getElementById(left_state).innerHTML="State/Territory";
   	 	 	break;
   	 	 case 201://united kindom
   	 	 	document.getElementById(left_state).innerHTML="Counties and Regions";
   	 	 	break;
   	 	 default:
   	 	 	document.getElementById(left_state).innerHTML="State/Province";
   	 	 	break;
   	 } 
   }
}