// JavaScript Document

function toggleTab(n){
	// first, turn off all tabs and windows
	for (i=1; i<4; i++){
		if (document.getElementById('div_tab' + i)){
			document.getElementById('div_win' + i).style.display = 'none'
			document.getElementById('div_tab' + i).className = 'tab_off'
		}
	}
	
	// turn on selected tab and window
	document.getElementById('div_win' + n).style.display = 'block'
	document.getElementById('div_tab' + n).className = 'tab_on'
}

var currentForm;
function fillSelect(f, action, id){
	window.setTimeout("", 50) // in case the function is called more than once very quick
	if (id == ''){ return false }
	var url = baseHref + 'ajax/searchform.php'
	currentForm = f
	if (action == 'getprovinces'){
		cms_doAjaxRequest(url, 'action=' + action + '&id=' + id, 'handlerFunc_provinces', '')
	}else if (action == 'getcities' || action == 'getcountrycities'){
		cms_doAjaxRequest(url, 'action=' + action + '&id=' + id, 'handlerFunc_cities', '')
	}else if (action == 'getareas'){
		cms_doAjaxRequest(url, 'action=' + action + '&id=' + id, 'handlerFunc_areas', '')
	}else{ alert("fillSelect() error: unknown action (" + action + ")") }
}

function handlerFunc_provinces(text){
	obj = eval("document." + currentForm + ".province_id")
	handlerFunc_fillSelect(text, obj)
}

function handlerFunc_cities(text){
	obj = eval("document." + currentForm + ".city_id")
	handlerFunc_fillSelect(text, obj)
}

function handlerFunc_areas(text){
	obj = eval("document." + currentForm + ".area_id")
	handlerFunc_fillSelect(text, obj)
}


function handlerFunc_fillSelect(text, obj){
	var a = text.split('|SEP|')
	// empty object first
	obj.options.length = 0
	// add empty option
	obj.options[0] = new Option('', '')
	// add all other options
	for (i=0; i<a.length; i+=2){
		obj.options[i/2+1] = new Option(a[i+1], a[i])
	}
	return true
}