// JavaScript Document
var req = null;

function reloadAccountDetails(option) {
	form = document.getElementById("registrationForm");
	form.action="register.php";

	option.checked=true;
	
	pass = document.getElementsByName("password")[0];
	pass.value="";
	
	file = document.getElementsByName("photo")[0];
	file.value="";
	
	form.submit();
	return true;
}

function clear_combo_cities() {
	selCities = document.getElementById("city");
	while( selCities.options.length > 0 ) {
		selCities.remove(0);
	}
	optSelectState = document.createElement("option");
	optSelectState.value=null;
	optSelectState.innerHTML="You must select a state first";
	selCities.appendChild(optSelectState);
	selCities.selectedIndex = 0;
}
	
function load_states(country_id) {
	clear_combo_cities();
	createReqElement();
	if( country_id ) {
		req.onreadystatechange = load_states_callback;
		req.open("GET", "get_states.php?country_id="+country_id, true);
		req.send(null);
		loading(true);
	}
}
	
function load_cities(state_id) {
	createReqElement();
	if( state_id ) {
		req.onreadystatechange = load_cities_callback;
		req.open("GET", "get_cities.php?state_id="+state_id, true);
		req.send(null);
		loading(true);
	}
}
	
function fill_combo( combo, array )	{
	while( combo.options.length > 0 ) {
		combo.removeChild(combo.options[0]);
	}
	
	opt = document.createElement("option");
	opt.value="";
	opt.innerHTML="&nbsp;&nbsp;&nbsp;&nbsp;";
	combo.appendChild(opt);
	for( i = 0; i < array.length; i++ ) {
		values = array[i].split("=");
		opt = document.createElement("option");
		opt.value=values[0];
		opt.innerHTML=values[1];
		combo.appendChild(opt);
	}
}
	
function open_new_city_window( ) {
	window.open("new_city.html", "_blank", "height=130, width=350, directories=0, location=0, menubar=0, status=0, toolbar=0");
}
	
function add_new_city( city_name ) {
	states = document.getElementById("state");
	if( !states.selectedIndex )
		return;
	
	createReqElement();
	state_id = states.options[states.selectedIndex].value;
	
	req.open("GET", "add_city.php?city_name="+city_name+"&state_id="+ state_id, false);
	req.send(null);
	
	load_cities(state_id);
}
	
function createReqElement() {
	if( window.XMLHttpRequest ) {
		req = new window.XMLHttpRequest();
	} else if( window.ActiveXObject ) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		form = document.getElementById("registrationForm");
		form.action = "prototipo_register.php";
		form.submit();
	}
}

function load_states_callback() {
	// Terminou de carregar
	if( req.readyState == 4 ) {
		// Status é "OK"
		if( req.status == 200 ) {
			var content = req.responseText;
			states = content.split("\n");
			cmbStates = document.getElementById("state");
			fill_combo( cmbStates, states );
			loading(false);
		} else {
			window.alert("Problem retrieving country states.\nPlease alert the site admin.");
		}
	}
}

function load_cities_callback() {
	// Terminou de carregar
	if( req.readyState == 4 ) {
		// Status é "OK"
		if( req.status == 200 ) {
			var content = req.responseText;
			cities = content.split("\n");
			cmbCities = document.getElementById("city");
			fill_combo( cmbCities, cities );
			loading(false);
		} else {
			window.alert("Problem retrieving state cities.\nPlease alert the site admin.");
		}
	}
}

function loading(turnOn) {
	loading_display = document.getElementById("loading");
	if( turnOn ) {
		loading_display.style.visibility = 'visible';
	} else {
		loading_display.style.visibility = 'hidden';
	}
}
