// JavaScript Document

var ajax = new sack();
var ajax_busy=false;
var selected = new Array();

var rightArrow = document.createElement('img');
rightArrow.src = site_root_stripped+"/images/right_arrow.gif";
rightArrow.alt = "&gt;";

function changeArea(){
	get_location_details(this.value);
}

function get_location_details(area_id, not_select){
	if(ajax_busy){
		setTimeout("get_location_details("+area_id+")",100);
		return;
	}
	
	var location_select = document.getElementById('ad_location');
	if (location_select && location_select.options[location_select.selectedIndex].disabled && !not_select) {
		location_select.selectedIndex = selected[1];
		return;
	}
	
	displayWaitMessage();
	
	var details_page=site_root_stripped+'/index.php?module=ads&op=gac&';
	ajax_busy=true;
	ajax.requestFile = details_page+"ad_location="+area_id;
	ajax.onCompletion = function(){ showLocationDetails(area_id); };	// Specify function that will be executed after file has been found
	ajax.runAJAX();
}

function showLocationDetails(url){
	eval(ajax.response);
	ajax_busy=false;
	
	//forefathers
	ad_loc_placeholder.innerHTML = '';
	if(details.forefathers){
		var alink = document.createElement('a');
		alink.innerHTML = 'root';
		alink.href = "javascript: get_location_details('', true)";
		ad_loc_placeholder.appendChild(alink);
		ad_loc_placeholder.innerHTML += '&nbsp;';
		ad_loc_placeholder.appendChild(rightArrow);
		ad_loc_placeholder.innerHTML += '&nbsp;';
			
		for(var i=0;i<details.forefathers.length;i++){
			var alink = document.createElement('a');
			alink.innerHTML = details.forefathers[i].name;
			alink.href = "javascript: get_location_details("+details.forefathers[i].area_id+", true)";
			ad_loc_placeholder.appendChild(alink);
			ad_loc_placeholder.innerHTML += '&nbsp;';
			ad_loc_placeholder.appendChild(rightArrow);
			ad_loc_placeholder.innerHTML += '&nbsp;';
		}
	}
	if(details.selected){
		var alink = document.createElement('a');
		alink.innerHTML = details.selected.name;
		ad_loc_placeholder.appendChild(alink);
		current_loc=details.selected.area_id;
		
		hidden_loc.setAttribute('value', current_loc);
	}
	else{
		current_loc=0;
	}
		
	
	//childs
	ad_loc_select_placeholder.innerHTML = '';
	if(details.childs){
		current_loc=false;
		var location_select = document.createElement('select');
		location_select.setAttribute('id','ad_location');
		location_select.onchange = changeArea;
	
		var selected_opt = document.createElement('option');
		selected_opt.innerHTML='select one';
		selected_opt.setAttribute('disabled', true);
		selected_opt.setAttribute('selected', true);
		
		location_select.appendChild(selected_opt);
	
		for(var i=0; i<details.childs.length; i++){
			var child_opt = document.createElement('option');
			child_opt.innerHTML=details.childs[i].name;
			child_opt.value=details.childs[i].area_id;
			location_select.appendChild(child_opt);
		}
		ad_loc_select_placeholder.appendChild(location_select);
		selected[1] = location_select.selectedIndex;
		emulate_disabled(location_select);
	}
	
	
	hideWaitMessage();
}

var ad_loc_placeholder, ad_loc_select_placeholder, hidden_loc;

function initLocationSelect(){
	ad_loc_placeholder = document.getElementById('ad_loc_placeholder');
	ad_loc_select_placeholder = document.getElementById('ad_loc_select_placeholder');
	hidden_loc = document.getElementById('area_id');
	
	get_location_details(current_loc);
}

var oldWindowOnload = window.onload;
window.onload = function(){
	if(oldWindowOnload){
		oldWindowOnload();
	}
	initLocationSelect();
}