/********************/
/*		Events		*/
/********************/


var ACTION_GET_RESOURCE_STRING = "getresource";
var ACTION_GET_TOWNS = "gettowns";

function uiajx_get_resource(resource_key, helptopicid, eventhandler){
	
	var url = ''
	if(location.href.indexOf('admin') == -1)
			url = 'admin/'
			
	url += 'AjaxEvtHandler.aspx?moduledefid=-1&action=' + ACTION_GET_RESOURCE_STRING;
	url += '&key=' + encodeURIComponent(resource_key);
	url += '&topicid=' + encodeURIComponent(helptopicid);
					
	return ajx_SendRequest("POST", url, '', false, eventhandler, true);	
}

function uiajx_evtlst_set_towns(ddcountries_id, ddcountys_id, ddtowns_id, setcounties, showafter, showhidden){
	
	var oddcountry = getObject(ddcountries_id);
	var countryid = '';	
	if(oddcountry!=null)
		countryid = oddcountry.options[oddcountry.selectedIndex].value;
		
	// set county/town dds visiblity
	setElementVisibility('tr_evtlst_county_town', (countryid > 0));
	
	var oddcountys = getObject(ddcountys_id);
	var county = '';
	try {
		if(oddcountys!=null)
			county = oddcountys.options[oddcountys.selectedIndex].value;
	} catch(exc) {}
	
	var url = ''
	if(location.href.indexOf('admin') == -1)
			url = 'admin/'
			
	url += 'AjaxEvtHandler.aspx?moduledefid=' + MODULE_DEF_ID_EVENTLIST + '&action=' + ACTION_GET_TOWNS;
	url += '&ddcountries_id=' + encodeURIComponent(ddcountries_id);
	url += '&ddtowns_id=' + encodeURIComponent(ddtowns_id);
	url += '&ddcountys_id=' + encodeURIComponent(ddcountys_id);
	url += '&countryid=' + countryid;
	url += '&county=' + encodeURIComponent(county);
	url += '&showafter=' + showafter;
	url += '&showhidden=' + showhidden;
	if(setcounties)
		ajx_SendRequest("POST", url, '', true, '_uiajx_evtlst_cb_set_towns_counties', true);	
	else
		ajx_SendRequest("POST", url, '', true, '_uiajx_evtlst_cb_set_towns', true);	
		
	return false;	
}

function _uiajx_evtlst_cb_set_towns_counties(response){
	
	//try {
		var xmlDoc = response;
		
		var nodeResponse = xmlDoc.getElementsByTagName("response");
		
		_uiajx_fill_towns(nodeResponse);
		_uiajx_fill_counties(nodeResponse);
								
	/*} catch(e){
		
	}	*/
}

function _uiajx_evtlst_cb_set_towns(response){
	
	//try {
		var xmlDoc = response;
		
		var nodeResponse = xmlDoc.getElementsByTagName("response");
		
		_uiajx_fill_towns(nodeResponse);
								
	/*} catch(e){
		
	}	*/
}

function _uiajx_fill_towns(nodeResponse){
	var node_ddtowns_id = nodeResponse[0].getElementsByTagName("ddtowns_id");
		
	// get the towns dd
	var oddtown_id = node_ddtowns_id[0].firstChild.nodeValue;		
	var oddtown = getObject(oddtown_id);
	
	//clear the towns dd
	oddtown.length = 0;		
	
	// add the towns to the dd
	var xnodesTowns = nodeResponse[0].getElementsByTagName("town");
	
	for (i=0; i<xnodesTowns.length; i++){	
		var nodeTown = xnodesTowns[i];	
		var town = '';
		if(nodeTown.firstChild!=null)
			town = nodeTown.firstChild.nodeValue
		if(is_ie)
			oddtown.add(new Option(town,town));
		else
			oddtown.add(new Option(town,town),null);
	}
}

function _uiajx_fill_counties(nodeResponse){

	var node_ddcountys_id = nodeResponse[0].getElementsByTagName("ddcountys_id");
		
	// get the counties dd
	var oddcounty_id = node_ddcountys_id[0].firstChild.nodeValue;		
	var oddcounty = getObject(oddcounty_id);
	
	//clear the countys dd
	oddcounty.length = 0;		
	
	// add the counties to the dd
	var xnodesCounties = nodeResponse[0].getElementsByTagName("county");
	
	for (i=0; i<xnodesCounties.length; i++){	
		var nodeCounty = xnodesCounties[i];	
		var county = '';
		if(nodeCounty.firstChild!=null)
			county = nodeCounty.firstChild.nodeValue			
		if(is_ie)
			oddcounty.add(new Option(county,county));
		else
			oddcounty.add(new Option(county,county),null);
	}
}


function uiajx_fill_txtbox_from_dropdown(dd_id, txt_id){
	var odd = getObject(dd_id);
	var otxt = getObject(txt_id);
	
	if(odd != null && otxt != null)
		otxt.value = odd.options[odd.selectedIndex].value
}
