// JavaScript Document

var ajax = new sack();
var selected = new Array();
var ajax_busy=false;
var formScript = {};
var rightArrow = document.createElement('img');
rightArrow.src = SITE_URL+"/images/right_arrow.gif";
rightArrow.alt = "&gt;";

function changeArea(){
	get_location_details(this.value);
}

function changeCategory(){
	get_category_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 get_category_details(cat_id, not_select){
	if(ajax_busy){
		setTimeout("get_category_details('"+cat_id+"')",100);
		return;
	}
	
	var category_select = document.getElementById('new_ad_category');
	if (category_select && category_select.options[category_select.selectedIndex].disabled && !not_select) {
		category_select.selectedIndex = selected[2];
		return;
	}
	
	displayWaitMessage();
	
	var details_page=site_root_stripped+'/index.php?module=ads&op=gcc&';
	ajax_busy=true;
	ajax.requestFile = details_page+"cat_id="+cat_id;
	ajax.onCompletion = function(){ showCategoryDetails(cat_id); };	// Specify function that will be executed after file has been found
	ajax.runAJAX();
}

function getForm(ad_type, current_cat){
	if(ajax_busy){
		setTimeout("get_category_details('"+cat_id+"')",100);
		return;
	}
	
	var details_page=site_root_with_cat+'.html?op=gf&for=search&';
	ajax_busy=true;
	ajax.requestFile = details_page+"type="+ad_type;
	ajax.onCompletion = function(){ showForm(current_cat); };	// Specify function that will be executed after file has been found
	ajax.runAJAX();
}

function showForm(current_cat){
	eval(ajax.response);
	ajax_busy=false;
	requiredFields = false;
	
	form_placeholder.innerHTML = details;
	if(formScriptEncoded){
		eval(formScriptEncoded);
	}
	
	var hidden_loc = document.getElementById('area_id');
	if(hidden_loc){
		hidden_loc.setAttribute('value', current_loc);
	}

	var hidden_cat = document.getElementById('cat_id');
	if(hidden_cat){
		hidden_cat.setAttribute('value', current_cat);
	}
	hideWaitMessage();
}

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;
		
	
		var hidden_loc = document.getElementById('area_id');
		if(hidden_loc){
			hidden_loc.setAttribute('value', current_loc);
		}
	}
	else{
		current_loc=0;
	}
	
	
	//childs
	ad_loc_select_placeholder.innerHTML = '';
	if(details.childs){
		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();
}

function showCategoryDetails(url){
	
	eval(ajax.response);
	ajax_busy=false;
	
	form_placeholder.innerHTML = '';
	
	//forefathers
	ad_cat_placeholder.innerHTML = '';
	if(details.forefathers){
		var alink = document.createElement('a');
		alink.innerHTML = 'root';
		alink.href = "javascript: get_category_details('', true)";
		ad_cat_placeholder.appendChild(alink);
		ad_cat_placeholder.innerHTML += '&nbsp;';
		ad_cat_placeholder.appendChild(rightArrow);
		ad_cat_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_category_details("+details.forefathers[i].cat_id+", true)";
			ad_cat_placeholder.appendChild(alink);
			ad_cat_placeholder.innerHTML += '&nbsp;';
			ad_cat_placeholder.appendChild(rightArrow);
			ad_cat_placeholder.innerHTML += '&nbsp;';
		}
	}
	if(details.selected){
		var alink = document.createElement('a');
		alink.innerHTML = details.selected.name;
		ad_cat_placeholder.appendChild(alink);
		current_cat=details.selected.cat_id;
		if(details.selected.table){
			getForm(details.selected.ad_type, current_cat);
		}
	}
	else{
		current_cat=0;
	}
	
	//childs
	ad_cat_select_placeholder.innerHTML = '';
	if(details.childs){
		var category_select = document.createElement('select');
		category_select.setAttribute('id','ad_category');
		category_select.onchange = changeCategory;
	
		var selected_opt = document.createElement('option');
		selected_opt.innerHTML='select one';
		selected_opt.setAttribute('disabled', true);
		selected_opt.setAttribute('selected', true);
		
		category_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].cat_id;
			category_select.appendChild(child_opt);
		}
		ad_cat_select_placeholder.appendChild(category_select);
		selected[1] = category_select.selectedIndex;
		emulate_disabled(category_select);
	}
	
	
	if(!(details.selected && details.selected.table)){
		hideWaitMessage();
	}
}

function emulate_disabled(e) {
	for (var i=0, option; option = e.options[i]; i++) {
		if (option.disabled) {
			option.style.color = "graytext";
		}
		else {
			option.style.color = "menutext";
		}
	}
}

function check_disabled_option(select_id, index_no){
	var select_field = document.getElementById(select_id);
	if (select_field.options[select_field.selectedIndex].disabled) {
		select_field.selectedIndex = selected[index_no]?selected[index_no]:0;
		return;
	}
	else{
		selected[index_no] = select_field.selectedIndex;
	}
	return
}

var ad_loc_placeholder = document.getElementById('ad_loc_placeholder');
var ad_loc_select_placeholder = document.getElementById('ad_loc_select_placeholder');
var ad_cat_placeholder = document.getElementById('ad_cat_placeholder');
var ad_cat_select_placeholder = document.getElementById('ad_cat_select_placeholder');
var form_placeholder = document.getElementById('form_placeholder');

get_location_details(current_loc);
get_category_details(current_cat);

function processForm(thisform){	
	if(formScript && formScript.customProcessForm){
		if(!formScript.customProcessForm(thisform)){
			return false;
		}
	}
	return true;
}

function hideWaitMessage(){
	var wait_div=document.getElementById('wait_message').style;
	wait_div.display='none';
	wait_div.visibility='hidden';
	var wait_div=document.getElementById('wait_message_bg').style;
	wait_div.display='none';
	wait_div.visibility='hidden';
}

function displayWaitMessage(){
	var wait_div=document.getElementById('wait_message').style;
	wait_div.display='block';
	wait_div.visibility='visible';
	var wait_div=document.getElementById('wait_message_bg').style;
	wait_div.display='block';
	wait_div.visibility='visible';
}
