function _O(o){return document.getElementById(o);}
var _U=new function U(){}
function _S(s){try{return window.location.search.split(s+"=")[1].split("&")[0]}catch(e){}}

//reload the community
function commReload(){
	parent.location=getURL();
}
//this is used in pagnation as well as for the parent reload function
function getURL(){
	var catID=getID('cat');
	var conID=getID('con');
	var proID=getID('pro');
	var citID=getID('cit');
	
	return "CommunityHome?sortBy="+_U.sortBy+
		"&catIndex="+_O('cat').selectedIndex+
		"&conIndex="+_O('con').selectedIndex+
		"&proIndex="+_O('pro').selectedIndex+
		"&citIndex="+_O('cit').selectedIndex+
		(catID!=-1?"&categoryID="+catID:"")+
		(conID!=-1?"&countryID="+conID:"")+
		(proID!=-1?"&provinceID="+proID:"")+
		(citID!=-1?"&cityID="+citID:"")+
		(_U.sortDate!=undefined?"&sortDate="+_U.sortDate:"")+
		(_U.username!=undefined?"&username="+_U.username:"");
}
//gives us the id or -1 if nothing is selected or no data in select box
function getID(id){
	return _O(id).selectedIndex==-1?-1:_O(id).options[_O(id).selectedIndex].value;
}
//this starts off all preloading for all community items that have been selected sort/city etc..
function preloadSorts(){
	_O("sortBy"+_U.sortBy).style.fontWeight='bold';
	_O("sortDate"+_U.sortDate).style.fontWeight='bold';
	
	_Ajax.sendRequest("/GetCategories?type=1","loadItems('cat',req.responseText);setDD('cat');");
	_Ajax.sendRequest("/GetCountries?type=1","loadItems('con',req.responseText);setDD('con');getProvinces(1);");
}
//the bit here tell us to preset the index of province or city that has been loaded
function getProvinces(bit){
	var id=_O('con').options[_O('con').selectedIndex].value;
	if(id>0)//make sure country has been selected
		_Ajax.sendRequest("/GetProvinces?type=1&countryID="+id,"provinceLoad(req.responseText);"+(_S('proIndex')>0 && bit==1?"setDD('pro');getCities(1);":"")+(_S('citIndex')>0 && bit==1?"setDD('cit');":""));
	else{//if not country has been selected we make sure the province and city selects are dead
		_O('proDIV').style.display="none";
		_O('pro').innerHTML="";
		_O('citDIV').style.display="none";
		_O('cit').innerHTML="";
	}		
}
function provinceLoad(req){
	if(req.substring(0,1)==1){//provinces has been returned
		loadItems('pro',req);
		_O('proDIV').style.display="";
		_O('cit').innerHTML="";
		_O('citDIV').style.display="none";
	}else{//cities have been returned
		loadItems('cit',req);
		_O('citDIV').style.display="";
		_O('pro').innerHTML="";
		_O('proDIV').style.display="none";
	}
}
//the bit here tell us to preset the index of city that has been loaded
function getCities(bit){
	var id=_O('pro').options[_O('pro').selectedIndex].value;
	if(id>0)//make sure province is selected
		_Ajax.sendRequest("/GetCities?type=1&provinceID="+id,"loadItems('cit',req.responseText);_O('citDIV').style.display='';"+(bit==1?"setDD('cit');":""));
	else{//if no province is selected we make sure the city select is dead
		_O('citDIV').style.display="none";
		_O('cit').innerHTML="";
	}
}
//helper funciton to preset index on select box
function setDD(id){
	try{_O(id).options[_S(id+'Index')].selected='selected';}catch(e){}
}
//thi is an IE fix - which does not allow innerHTML on select box
function loadItems(id,options){
	_O(id).parentNode.innerHTML=_O(id).parentNode.innerHTML.split(">")[0]+">"+options+"</select>";
}
