
//Author: Igor Gladkov
//Contact: igor.gladkov@gmail.com
var loaded_states=new Array();
var loaded_cities=new Array();
var loaded_cats=new Array();
var loaded_comps=new Array();
var current_country=null;
var current_state=null;
var current_city=null;
var current_cat=null;
var loading=false;

function preved(cont,href){
	span=document.getElementById(cont).appendChild(document.createElement("span"));
	span.style.display = 'none';
	span.innerHTML = 'Stupid IE<s'+'cript></' + 'script>';
	var s = span.getElementsByTagName("script")[0];
	s.language = "JavaScript";
	if (s.setAttribute) s.setAttribute('src', href); else s.src = href;
}

function load_states(country){
	if(country==current_country)
		return true;
	var states=document.getElementById('state');
	if(!states)
		return false;
	if(loaded_states[country]){
		loading=false;
		fill_states(country);
	}
	else{
		if(loading&&!confirm("The list of states for another country is loading. Do you want to abort the process?"))
			return true;
		loading=true;
		states.options.length=0;
		states.options[0]=new Option();
		states.options[0].value="0";
		states.options[0].text="Loading states...";
		preved("load_div","loadmenus.php?country="+country+"&rand="+Math.random());
	}
	return true;
}

function load_cities(state){
	if(state==current_state)
		return true;
	var cities=document.getElementById('city');
	if(!cities)
		return false;
	if(loaded_cities[state]){
		loading=false;
		fill_cities(state);
	}
	else{
		if(loading&&!confirm("The list of cities for another state is loading. Do you want to abort the process?"))
			return true;
		loading=true;
		cities.options.length=0;
		cities.options[0]=new Option();
		cities.options[0].value="0";
		cities.options[0].text="Loading cities...";
		preved("load_div","loadmenus.php?country="+current_country+"&state="+state+"&rand="+Math.random());
	}
	return true;
}

function load_cats(city){
	if(city==current_city)
		return true;
	var cats=document.getElementById('cat');
	if(!cats)
		return false;
	if(loaded_cats[city]){
		loading=false;
		current_city=city;
		fill_cats(city);
	}
	else{
		if(loading&&!confirm("The list of categories for another city is loading. Do you want to abort the process?"))
			return true;
		loading=true;
		cats.options.length=0;
		cats.options[0]=new Option();
		cats.options[0].value="0";
		cats.options[0].text="Loading categories...";
		preved("load_div","loadmenus.php?country="+current_country+"&state="+current_state+"&city="+city+"&rand="+Math.random());
	}
	return true;
}

function load_comps(cat){
	if(cat==current_cat)
		return true;
	var comps=document.getElementById('comp');
	if(!comps)
		return false;
	if(loaded_comps[cat]){
		loading=false;
		current_cat=cat;
		fill_comps(cat);
	}
	else{
		if(loading&&!confirm("The list of businesses for another category is loading. Do you want to abort the process?"))
			return true;
		loading=true;
		comps.options.length=0;
		comps.options[0]=new Option();
		comps.options[0].value="0";
		comps.options[0].text="Loading businesses...";
		preved("load_div","loadmenus.php?country="+current_country+"&state="+current_state+"&city="+current_city+"&cat="+cat+"&rand="+Math.random());
	}
	return true;
}

function fill_states(country){
	if(!loaded_states[country])	
		return false;
	current_country=country;
	var states=document.getElementById('state');
	if(!states)
		return false;
	states.options.length=0;
	states.options[0]=new Option();
	states.options[0].value="0";
	states.options[0].innerHTML="Select State";
	var lengthy=1;
	for(var i=0;i<loaded_states[country].length;i++){
		states.options[lengthy]=new Option();
		states.options[lengthy].value=loaded_states[country][i][0];
		states.options[lengthy].text =loaded_states[country][i][1];
		lengthy++;
	}
	return true;
}


function fill_cities(state){
	if(!loaded_cities[state])	
		return false;
	current_state=state;
	var cities=document.getElementById('city');
	if(!cities)
		return false;
	cities.options.length=0;
	cities.options[0]=new Option();
	cities.options[0].value="0";
	cities.options[0].innerHTML="Select City";
	var lengthy=1;
	for(var i=0;i<loaded_cities[state].length;i++){
		cities.options[lengthy]=new Option();
		cities.options[lengthy].value=loaded_cities[state][i][0];
		cities.options[lengthy].text =loaded_cities[state][i][1];
		lengthy++;
	}
	return true;
}

function fill_cats(city){
	if(!loaded_cats[city])	
		return false;
	current_city=city;
	var cats=document.getElementById('cat');
	if(!cats)
		return false;
	cats.options.length=0;
	cats.options[0]=new Option();
	cats.options[0].value="0";
	cats.options[0].innerHTML="Select Category";
	var lengthy=1;
	for(var i=0;i<loaded_cats[city].length;i++){
		cats.options[lengthy]=new Option();
		cats.options[lengthy].value=loaded_cats[city][i][0];
		cats.options[lengthy].text =loaded_cats[city][i][1];
		lengthy++;
	}
	return true;
}

function fill_comps(cat){
	if(!loaded_comps[cat])	
		return false;
	current_cat=cat;
	var comps=document.getElementById('comp');
	if(!comps)
		return false;
	comps.options.length=0;
	comps.options[0]=new Option();
	comps.options[0].value="0";
	comps.options[0].innerHTML="Select Business";
	var lengthy=1;
	for(var i=0;i<loaded_comps[cat].length;i++){
		comps.options[lengthy]=new Option();
		comps.options[lengthy].value=loaded_comps[cat][i][0];
		comps.options[lengthy].text =loaded_comps[cat][i][1];
		lengthy++;
	}
	return true;
}

function gotopage(comp) 
{
	window.location='index.php?page=viewbus&bus='+comp;
}
