﻿var selectedBound = new Object();
var selectedCatBound = new Object();

var selectedCat = "";

var lastAllIds = "";

var setCategoriesTimer = "";

var dontResizeOnCat = 0;

function setCategories()
{
	clearTimeout(setCategoriesTimer);
	setCategoriesTimer = "";	
	catSelect();
}

function setBoundFromFlash(rez)
{	
	//var rez2 = rez.split(",");	
	catSelectStart = 1
	selectedCatBound.xMin = rez[0];
	selectedCatBound.xMax = rez[1];
	selectedCatBound.yMin = rez[2];
	selectedCatBound.yMax = rez[3];	
	
	
	if (setCategoriesTimer)
	
		clearTimeout(setCategoriesTimer);
		
	setCategoriesTimer = window.setTimeout(setCategories,300);
		
}

function clearCat()
{	
	selectedCat = "";
	var obj = document.getElementById("cat_box");
	if (obj)
	{
		obj.selectedIndex = -1;
		if (map) map.clear();
		var obj = document.getElementById("cat_rez");
		if (obj)
		obj.innerHTML = "";						
		document.getElementById("cat_rez_box").style.display = "none";
		lastRez = "";
	}
}	

var catSelectStart = 1;

function catSelect(num)
{
	document.getElementById("loader1").style.display = "inline";
	var obj = document.getElementById("cat_box");
	selectedCat = obj;
	if (num)
		catSelectStart = num;
		
	if (obj.selectedIndex >= 0)
	{
	if (!dontResizeOnCat)
		mapMaximize();
	dontResizeOnCat = 0;	
	var cat = obj.options[obj.selectedIndex].value;	
	var q = "cat="+cat+"&extraparams=onlyshow";
	if (selectedBound)
	{
	q += "&xMin="+selectedCatBound.xMin;
	q += "&xMax="+selectedCatBound.xMax;
	q += "&yMin="+selectedCatBound.yMin;
	q += "&yMax="+selectedCatBound.yMax;
	}
	var l = document.getElementById("cat_num_items");	
	q += "&num="+l.options[l.selectedIndex].value;
	q += "&start="+(catSelectStart-1);
	xmlhttpPost("/forms/search/cat.php",q,catSelectCB);
	}
}

function listSelectCatItem(obj,params)
{				
	if (obj.parentNode.selectedItem != obj)
	{
	
	if (obj.parentNode.selectedItem)
			obj.parentNode.selectedItem.className = "searchItem";
	
	obj.parentNode.selectedItem  = obj;		
	obj.className = "searchItemSelected";		
	obj.params = params;
	}	
	if (obj.loc)
	{								
		if (params && map)
			map.showSubject(params,"onlyshow");		
	}	
}

var lastRez = "";

function catSelectCB(rez)
{				
	rez = rez.split("#$#");
	str = rez[0];
	num = rez[1];
	count = rez[2];
		
	if (str && lastRez != str)
	{		
		var obj = document.getElementById("cat_rez");
		var rez = '<div id="cat_searchList" class="searchList" style="background-color:#ffffff; border:1px solid #999999;height:320px;">'+str+'</div>';
		obj.innerHTML = rez;
		
		var l = document.getElementById("cat_num_items");	
		var k = l.options[l.selectedIndex].value*1;
		
		var obj = document.getElementById("cat_other");
		var rez = "Showing: "+(catSelectStart*1)+" to "+(catSelectStart*1+num*1-1)+" of "+count;
		rez += "<br/>";
		pitem = catSelectStart*1-k;
		nitem = catSelectStart*1+k;
		if (pitem < 1) pitem = 1;
		if (nitem > count) nitem = count;
		if (catSelectStart*1 > 1)
			rez += '<a href="javascript:catSelect('+pitem+')">&lt;&lt; previous '+k+'</a>';
			
		if (catSelectStart*1+num*1-1 < count)
		{
			if (catSelectStart*1 > 1)
				rez += ' | ';
			rez += '<a href="javascript:catSelect('+nitem+')"> next '+k+' &gt;&gt;';
		}
		obj.innerHTML = rez;
				
		
		document.getElementById("cat_rez_box").style.display = "block";
		
	
		
		var srch = document.getElementById("cat_searchList");	  	   		
		var allids = "";
		var allidsNum = 0;				
		if (map) map.clear();	
			for (i=0; i < srch.childNodes.length; i++)
			{				
				var tmpid = srch.childNodes[i].id;				
				if (tmpid && tmpid.substring(0,7) == "itmlok_")		
				{		
					srch.childNodes[i].loc = true;
					allids += tmpid.substring(7)+",";
					allidsNum++;				
				}
			}					
			if (allids)		
			{
				allids = allids.substring(0,allids.length-1);								
				
				if (map)	
				map.showSubject(allids,"minimized,onlyshow");
				
				//if (allidsNum == 1)
				//listSelect(srch.childNodes[0],"");				
			}
			else
			if (srch.childNodes.length > 0)
			{
				var fobj = srch.childNodes[0].id.substring(7);
				listSelect(srch.childNodes[0],fobj,"onlyshow");
			}		
		srch.style.display = "block";			
	}
	else
	{
		if (!str)
		{
			var obj = document.getElementById("cat_rez");
			obj.innerHTML = "";			
			document.getElementById("cat_rez_box").style.display = "none";
			if (map) map.clear();
		}
	}
	lastRez = str;
	document.getElementById("loader1").style.display = "none";
}

function showLoader()
{
	document.getElementById("loading").style.display = "inline";
}

function hideLoader()
{
	document.getElementById("loading").style.display = "none";
}

function disableDropDowns(start,end,cur)
{	
	if (!end) end = 6;
	for (var i=start; i <= end; i++)
	{
		obj = document.getElementById("searchSel"+i);	
		if (obj)
		{
			if (i != cur)
			obj.selectedIndex = 0;
			obj.disabled = "disabled";
		}
	}	
}

function enableDropDowns(start,end)
{
	dropDownsCount = 0;
	dropDownsLeft = 0;
	if (!end) end = 6;	
	//alert(end);
	for (var i=start; i <= end; i++)
	{
		obj = document.getElementById("searchSel"+i);		
		if (obj) obj.disabled = "";
	}	
}

var dropDownsLeft = 0;
var dropDownsCount = 0;

function setObjBound(obj)
{
	var opt = obj.options[obj.selectedIndex];
	var val = opt.value.split(";");	
	if (map && updateMapBound)
	{
		//map.modifyObjectsColor("cntry"+val[2]);
		map.getBound(val[0]);	
	}
	var b = val[0].split(",");
	selectedBound.xMin = b[0];
	selectedBound.xMax = b[1];
	selectedBound.yMin = b[2];
	selectedBound.yMax = b[3];	
	
}

function searchSelect1(obj,noupdate)
{
	var q = "";	
	if (obj.selectedIndex > 0)
	{	
	document.getElementById("searchText").disabled = "";
	document.getElementById("txt_trazi").disabled = "";
		
			
	var opt = obj.options[obj.selectedIndex];
	var val = opt.value.split(";");	
	var hid = document.getElementById("fn_prov");	
	if (hid) hid.value = val[1];		
	
	setObjBound(obj);
	
	selectedProv = val[1];
	
	q = "prov="+val[1];
		
	}
	else
	{		
		map.zoomToFull();
		selectedProv = "";
		selectedBound = new Object();
		document.getElementById("searchText").disabled = "disabled";
		document.getElementById("searchText").value = "";
		document.getElementById("txt_trazi").disabled = "disabled";
	}
			
	if (!noupdate)
	{				
		hideSearchRez();		
		
		if (obj.selectedIndex)
		{
			showLoader();			
			if (dropDownsCount < 5)
			dropDownsCount = 5;
			dropDownsLeft = 3;			
			disableDropDowns(1,6,1);
		}
		else
			disableDropDowns(2);
			
		
		xmlhttpPost("/forms/search/sel1.php",q,searchSelect1CB);		
		if (!populating)
		{
			xmlhttpPost("/forms/search/sel2.php",q,searchSelect2CB);
			//xmlhttpPost("/forms/search/sel3.php",q,searchSelect3CB);
			xmlhttpPost("/forms/search/sel4.php",q,searchSelect4CB);			
		}
	}
}

function searchSelect1CB(str)
{		
	var obj = document.getElementById("searchSel2");		
	if (!populating)
	{
		var hid = document.getElementById("fn_dist");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_local");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_mainp");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_suburb");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_street");	if (hid) hid.value = "";
	}
	if (!str) 
	{
		obj.selectedIndex = 0;
		obj.disabled = "disabled";		
		selectedBound = new Object();
		populating = 0;
		updateMapBound = 1;
		selectedDist = "";		
		selectedMainp = "";
		selectedMunic = "";
		selectedSub = "";
	}
	else
	{			
		var obj_cont = document.getElementById("searchSel2_cont");
		var rez = '<select style="width:178px;height:20px;" id="searchSel2" onchange="searchSelect2(this)" disabled="disabled">';		
		rez += "<option >-- District Municipality --</option>"+str;		
		rez += "</select>"
		obj_cont.innerHTML = rez;					
		if (populating)
		setDropDown("searchSel2","fn_dist",searchSelect2);				
	}	
	dropDownsLeft--;
	if (dropDownsLeft == 0)
		enableDropDowns(1,dropDownsCount);		
}

function searchSelect2(obj,noupdate,w)
{	
	var q = "";
	var hid = document.getElementById("fn_dist");	
	if (w) hid.value = w;
	
	if (obj.selectedIndex > 0)
	{		
		var opt = obj.options[obj.selectedIndex];
		var val = opt.value.split(";");												
		if (hid) hid.value = val[1];			
		
		selectedDist = val[1];
		
		if (!noupdate)
		{				
		setObjBound(obj);		
		q = "dist="+val[1];		
		}
	}
	else
	{	
		setObjBound(document.getElementById("searchSel1"));					
		selectedDist = "";
	}		
			
	if (!noupdate)
	{		
		hideSearchRez();		
		
		if (obj.selectedIndex)
		{
			showLoader();	
			if (dropDownsCount < 5)
			dropDownsCount = 5;
			dropDownsLeft = 2;			
			disableDropDowns(2,6,2);		
		}
		else
			disableDropDowns(3);		
		
		xmlhttpPost("/forms/search/sel2.php",q,searchSelect2CB);	
		if (!populating)
		{
			//xmlhttpPost("/forms/search/sel3.php",q,searchSelect3CB);
			xmlhttpPost("/forms/search/sel4.php",q,searchSelect4CB);
		}
	}
}

function searchSelect2CB(str)
{	

	var obj = document.getElementById("searchSel3");
	if (!populating)
	{
		var hid = document.getElementById("fn_local");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_mainp");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_suburb");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_street");	if (hid) hid.value = "";
	}
	if (!str) 
	{
		obj.selectedIndex = 0;
		obj.disabled = "disabled";				
		selectedBound = new Object();		
		populating = 0;
		updateMapBound = 1;
		selectedMainp = "";
		selectedMunic = "";
		selectedSub = "";					
	}
	else
	{		
		var obj_cont = document.getElementById("searchSel3_cont");
		var rez = '<select style="width:178px;height:20px;" id="searchSel3" onchange="searchSelect3(this)" disabled="disabled">';		
		rez += "<option >-- Local Municipality --</option>"+str;		
		rez += "</select>"
		obj_cont.innerHTML = rez;		
		if (populating)
		setDropDown("searchSel3","fn_local",searchSelect3);		
	}
	dropDownsLeft--;
	if (dropDownsLeft == 0)
	{
		enableDropDowns(1,dropDownsCount);	
		hideLoader();	
	}
	
}

var selectedProv = "";
var selectedDist = "";
var selectedMunic = "";
var selectedMainp = "";
var selectedSub = "";

function searchSelect3(obj,noupdate,w)
{
	var q = "";
	var hid = document.getElementById("fn_local");	
	if (w) hid.value = w;
	if (obj.selectedIndex > 0)
	{		
		document.getElementById("searchText").disabled = "";
		document.getElementById("txt_trazi").disabled = "";
		var opt = obj.options[obj.selectedIndex];
		var val = opt.value.split(";");										
			
		if (hid) hid.value = val[1];
		
		if (!noupdate)
		{							
			hideSearchRez();
			setObjBound(obj);					
	
			q = "id="+val[1];		
			selectedMainp = "";
			selectedSub = "";
		}

		selectedMunic = val[2];
		
		if (!populating)
		{					
			setDropDown2("searchSel2",val[3],searchSelect2);
		}
				
	}
	else
	{
		setObjBound(document.getElementById("searchSel2"));					
		selectedMunic = "";				
	}		
	
	if (!noupdate)	
	{
		hideSearchRez();
				
		if (obj.selectedIndex)
		{
		showLoader();	
		if (dropDownsCount < 5)
		dropDownsCount = 5;
		dropDownsLeft = 1;			
		disableDropDowns(3,6,3);
		}
		else
		disableDropDowns(4);
		
		//xmlhttpPost("/forms/search/sel3.php",q,searchSelect3CB);
		//if (!populating)		
		xmlhttpPost("/forms/search/sel4.php","local="+val[1],searchSelect4CB);
	}
	
}

function searchSelect3CB(str)
{			
	var obj = document.getElementById("searchSel4");
	if (!obj) return;
	if (!populating)
	{	
		var hid = document.getElementById("fn_mainp");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_suburb");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_street");	if (hid) hid.value = "";
	}
	if (!str) 
	{		
		obj.selectedIndex = 0;
		obj.disabled = "disabled";
		selectedSub = "";		
		selectedBound = new Object();		
		populating = 0;
		updateMapBound = 1;			
	}
	else
	{		
		var obj_cont = document.getElementById("searchSel4_cont");
		if (obj_cont)
		{
			var rez = '<select style="width:178px;height:20px;" id="searchSel4" onchange="searchSelect4(this)" disabled="disabled">';		
			rez += "<option >-- Main Place --</option>"+str;		
			rez += "</select>"
			obj_cont.innerHTML = rez;		
			if (populating)
			setDropDown("searchSel4","fn_mainp",searchSelect4);
		}
	}	
	dropDownsLeft--;
	if (dropDownsLeft == 0)
	{
		enableDropDowns(1,dropDownsCount);	
		hideLoader();	
	}
}


function searchSelect4(obj,noupdate,w)
{
	var q = "";
	var hid = document.getElementById("fn_mainp");	
	if (w) hid.value = w;
	if (obj.selectedIndex > 0)
	{
		
		document.getElementById("searchText").disabled = "";
		document.getElementById("txt_trazi").disabled = "";
		
		var opt = obj.options[obj.selectedIndex];
		var val = opt.value.split(";");										
			var hid = document.getElementById("fn_mainp");	
	if (hid) hid.value = val[1];
		
				
		if (!noupdate)
		{				
			showLoader();	
			if (map && updateMapBound)	
			map.getBound(val[0]);		
		
			var b = val[0].split(",");
			selectedBound.xMin = b[0];
			selectedBound.xMax = b[1];
			selectedBound.yMin = b[2];
			selectedBound.yMax = b[3];
	
			q = "id="+val[1];		
			selectedMunic = "";
			selectedSub = "";
		}
		
		selectedMainp = val[2];
		
		if (!populating)
		{
		setDropDown2("searchSel3",val[4],searchSelect3);
		setDropDown2("searchSel3",val[3],searchSelect3);
		setDropDown2("searchSel2",val[4],searchSelect2);
		}
				
	}
	else
	{
		selectedMainp = "";		
		selectedBound = new Object();
	}			
	if (!noupdate)	
	{
		hideSearchRez();
		disableDropDowns(4,6,4);
		xmlhttpPost("/forms/search/sel4.php",q,searchSelect4CB);		
	}
}

function searchSelect4CB(str)
{			
	var obj = document.getElementById("searchSel5");
	if (!populating)
	{		
		var hid = document.getElementById("fn_suburb");	if (hid) hid.value = "";
		var hid = document.getElementById("fn_street");	if (hid) hid.value = "";
	}
	if (!str) 
	{		
		obj.selectedIndex = 0;
		obj.disabled = "disabled";
		selectedSub = "";		
		selectedBound = new Object();		
		populating = 0;
		updateMapBound = 1;

	}
	else
	{		
		var obj_cont = document.getElementById("searchSel5_cont");
		var rez = '<select style="width:178px;height:20px;" id="searchSel5" onchange="searchSelect5(this)" disabled="disabled">';		
		rez += "<option >-- Suburb --</option>"+str;		
		rez += "</select>"
		obj_cont.innerHTML = rez;		
		if (populating)
		setDropDown("searchSel5","fn_suburb",searchSelect5);
	}		
	
	dropDownsLeft--;
	//alert(dropDownsLeft);
	if (dropDownsLeft == 0)
	{
		enableDropDowns(1,dropDownsCount);	
		hideLoader();	
	}
		
}



function searchSelect5(obj,noupdate,w)
{
	var q = "";	
	var hid = document.getElementById("fn_suburb");	
	if (w) hid.value = w;
	if (obj.selectedIndex > 0)
	{		
		document.getElementById("searchText").disabled = "";
		document.getElementById("txt_trazi").disabled = "";
		var opt = obj.options[obj.selectedIndex];
		var val = opt.value.split(";");													
		if (hid) hid.value = val[1];
		
		selectedSub = val[1];		
		if (!noupdate)
		{						
		hideSearchRez();
		selectedMainp = "";
		selectedMunic = "";
		
		setObjBound(obj);							
		
		q = "id="+val[1];	
		}		
		if (!populating)
		{
			setDropDown2("searchSel4",val[3],searchSelect4);			
			setDropDown2("searchSel3",val[2],searchSelect3);
		}
		
	}
	else
	{
		setObjBound(document.getElementById("searchSel3"));					
		selectedSub = "";				
		
	}		
	if (!noupdate)	
	{
		
		hideSearchRez();				
		if (obj.selectedIndex)
		{
		showLoader();		
		if (dropDownsCount < 6)
		dropDownsCount = 6;
		dropDownsLeft = 1;			
		disableDropDowns(5,6,5);		
		}
		else
			disableDropDowns(6);		
		xmlhttpPost("/forms/search/sel5.php",q,searchSelect5CB);		
	}
}

function searchSelect5CB(str)
{				
	var obj = document.getElementById("searchSel6");
	if (obj)
	{	
	if (!populating)
	{			
		var hid = document.getElementById("fn_street");	if (hid) hid.value = "";
	}
	if (!str) 
	{		
		obj.selectedIndex = 0;
		obj.disabled = "disabled";
		selectedSub = "";		
		selectedBound = new Object();	
		populating = 0;
		updateMapBound = 1;	
	}
	else
	{		
		var obj_cont = document.getElementById("searchSel6_cont");
		var rez = '<select style="width:178px;height:20px;" id="searchSel6" onchange="searchSelect6(this)" disabled="disabled">';		
		rez += "<option >-- Street --</option>"+str;		
		rez += "</select>"
		obj_cont.innerHTML = rez;		
		if (populating)
		setDropDownLookInArray("searchSel6","fn_street",searchSelect6);
	}
	}
	dropDownsLeft--;
	if (dropDownsLeft == 0)
	{
		enableDropDowns(1,dropDownsCount);	
		hideLoader();	
	}	
}


function searchSelect6(obj,noupdate,w)
{
	var q = "";	
	var hid = document.getElementById("fn_street");	
	if (w) hid.value = w;
	if (obj.selectedIndex == 0 && w)
	{		
		for (var i=1; i < obj.options.length;i++)
		{
			//alert(obj.options[i].value);
			var val = obj.options[i].value.split(";");			
			var ar = val[1].split(",");
			
			for (var k=0; k < ar.length;k++)			
				if (ar[k] == w)
				{
					obj.selectedIndex	= i;
					break;
				}
				if (obj.selectedIndex > 0)
					break;	
		}
	}	
	if (obj.selectedIndex > 0)
	{		
		var opt = obj.options[obj.selectedIndex];		
		var val = opt.value.split(";");												
		if (hid) 
		{
			var ar = val[1].split(",");			
			hid.value = ar[0];		
		}		
		document.getElementById("searchText").disabled = "";
		document.getElementById("txt_trazi").disabled = "";
		var opt = obj.options[obj.selectedIndex];
		document.getElementById("searchText").value =val[0];
		
		if (!populating)
		{
			setDropDownLookInArray2("searchSel5",val[2],searchSelect5);
		}
		
		if (populating)
		{
			updateMapBound = 1;
			populating = 0;
		}	
		else
		if (!noupdate)		
			trazi();
	}
	else
	{
		//selectedSub = "";
		//document.getElementById("searchText").disabled = "disabled";
		//document.getElementById("txt_trazi").disabled = "disabled";
		selectedBound = new Object();
	}	
}

var updateMapBound = 1;
var populating = 0;

function populateDropDowns()
{
	if (document.getElementById("fn_x").value == 0 && document.getElementById("fn_y").value == 0)
		updateMapBound = 1;
	else
		updateMapBound = 0;
	populating = 1;	
	setDropDown("searchSel1","fn_prov",searchSelect1);
}

function testItemValue(opt,hid)
{
	var val = opt.value.split(";");							
	return (val[1] == hid.value);
}

function testItemValue3(opt,testval)
{
	var val = opt.value.split(";");			
	var ar = val[1].split(",");
				
	for (var k=0; k < ar.length;k++)			
	if (ar[k] == testval)
		return true; 
									
	return false;
}

function testItemValue2(opt,w)
{
	var val = opt.value.split(";");				
	return val[1] == w;
}


function setDropDown(obj_name,hid_name,func)
{
	var obj = document.getElementById(obj_name);
	var hid = document.getElementById(hid_name);
	if (obj && hid)
	{
		for (var i=1; i < obj.options.length;i++)
		if (testItemValue(obj.options[i], hid) || (obj.options[i].innerHTML == hid.value))
		{
			obj.selectedIndex = i;
			func(obj);
			break;
		}
	}
}

function setDropDownLookInArray(obj_name,hid_name,func)
{
	var obj = document.getElementById(obj_name);
	var hid = document.getElementById(hid_name);	
	if (obj && hid)
	{
		for (var i=1; i < obj.options.length;i++)
		if (testItemValue3(obj.options[i], hid.value) || (obj.options[i].innerHTML == hid.value))
		{
			obj.selectedIndex = i;
			func(obj);
			break;
		}
	}
}

function setDropDownLookInArray2(obj_name,val,func)
{
	var obj = document.getElementById(obj_name);	
	if (!obj) return;
	if (val)
	{		
		for (var i=1; i < obj.options.length;i++)
		if (testItemValue3(obj.options[i], val) || (obj.options[i].innerHTML == val))
		{
			obj.selectedIndex = i;
			func(obj,true);		
			break;
		}
	}
}

function setDropDown2(obj_name,val,func)
{
	var obj = document.getElementById(obj_name);		
	if (!obj) return;
	if (val)
	{
		for (var i=1; i < obj.options.length;i++)
		if (testItemValue2(obj.options[i], val) || (obj.options[i].innerHTML == val))
		{
			obj.selectedIndex = i;	
			func(obj,true);		
			return;
		}
	}	
	obj.selectedIndex = 0;
	func(obj,true,val);		
}
