// Get object of visible area combox
function GetVisibleArea(areaFldName)
{
	var s;
	try {
		s = eval("divCountry"+areaFldName+".innerHTML");
		if (s == "")
			return null;
	}
	catch (e) {
		return "";	// No area combox
	}
	var arr = s.split(";");
	var found = false, rc = null, ctrlName, ctrlObj;
	for (var i = 0; i < arr.length; i++) {
		ctrlName = "form." + areaFldName + "_" + arr[i];
		ctrlObj = eval(ctrlName); 
		if (typeof(ctrlObj) != "undefined" && ctrlObj.style.display == "") {
			found = true;
			break;
		}
	}
	if (found)
		rc = ctrlObj;
	return rc;
}

// Get object of visible location combox
function GetVisibleLocation(locationFldName)
{
	var s;
	try {
		s = eval("divArea"+locationFldName+".innerHTML");
		if (s == "")
			return null;
	}
	catch (e) {
		return null;	// No location combox
	}
	var arr = s.split(";");
	var found = false, rc = null, ctrlName, ctrlObj;
	for (var i = 0; i < arr.length; i++) {
		ctrlName = "form." + locationFldName + "_" + arr[i];
		ctrlObj = eval(ctrlName); 
		if (typeof(ctrlObj) != "undefined" && ctrlObj.style.display == "") {
			found = true;
			break;
		}
	}
	if (found)
		rc = ctrlObj;
	return rc;
}

// Change visible area combox according to chosen country
function OnCountryChange(countryFldName, areaFldName, locationFldName, onInit)
{		 
	var s;
	try {
		s = eval("divCountry"+areaFldName+".innerHTML");
		if (s == "")
			return;
	}
	catch (e) {
		return;	// No area combox
	}
	
	var arr = s.split(";");
	var countryId = eval("form." + countryFldName + ".value");
	var found = false, ctrlName;
	for (var i = 0; i < arr.length; i++) {
		if (countryId == arr[i]) {
			// Display area combox for selected country
			for (var j = 0; j < arr.length; j++) {
				if (i != j) {
					ctrlName = "div" + areaFldName + "_" + arr[j];
					eval(ctrlName + ".style.display = 'none'");
					ctrlName = "form." + areaFldName + "_" + arr[j];
					eval(ctrlName + ".style.display = 'none'");
				}
			}
			ctrlName = "div" + areaFldName + "_" + countryId;
			eval(ctrlName + ".style.display = ''");
			ctrlName = "form." + areaFldName + "_" + countryId;
			eval(ctrlName + ".style.display = ''");			
			found = true;
			if (!onInit)
				eval(ctrlName + ".selectedIndex = 0; ");
			break;
		}
	}
	if (!found) {
		// Display area combox for country "0"
		for (var j = 0; j < arr.length; j++) {
			if (arr[i] != "0") {
				ctrlName = "div" + areaFldName + "_" + arr[j];
				eval(ctrlName + ".style.display = 'none'");
				ctrlName = "form." + areaFldName + "_" + arr[j];
				eval(ctrlName + ".style.display = 'none'");
			}
		}
		ctrlName = "div" + areaFldName + "_0";
		eval(ctrlName + ".style.display = ''");
		ctrlName = "form." + areaFldName + "_0";
		eval(ctrlName + ".style.display = ''");
		if (!onInit)
			eval(ctrlName + ".selectedIndex = 0; ");
	}
	if (locationFldName != "")
		OnAreaChange(areaFldName, locationFldName);
}

// Change visible location combox according to chosen area
function OnAreaChange(areaFldName, locationFldName)
{	
	try {		 
		OnAreaChange_DP();
	}
	catch (e) {
	}
	var s;
	try {
		s = eval("divArea"+locationFldName+".innerHTML");
		if (s == "")
			return;
	}
	catch (e) {
		return;	// No location combox
	}
	var arr = s.split(";");
	var areaId = GetVisibleArea(areaFldName).value;
	if (typeof(areaId)=="undefined")
		areaId = eval("form."+areaFldName+".value");
	var found = false, ctrlName;
	for (var i = 0; i < arr.length; i++) {
		if (areaId == arr[i]) {
			// Display location combox for selected area
			for (var j = 0; j < arr.length; j++) {
				if (i != j) {
					ctrlName = "div" + locationFldName + "_" + arr[j];
					eval(ctrlName + ".style.display = 'none'");
					ctrlName = "form." + locationFldName + "_" + arr[j];
					eval(ctrlName + ".style.display = 'none'");
				}
			}
			ctrlName = "div" + locationFldName + "_" + areaId;
			eval(ctrlName + ".style.display = ''");
			ctrlName = "form." + locationFldName + "_" + areaId;
			eval(ctrlName + ".style.display = ''");
			found = true;
			break;
		}
	}
	if (!found) {
		// Display location combox for area "0"
		for (var j = 0; j < arr.length; j++) {
			if (arr[i] != "0") {
				ctrlName = "form." + locationFldName + "_" + arr[j];
				eval(ctrlName + ".style.display = 'none'");
			}
		}
		ctrlName = "div" + locationFldName + "_0";	
		eval("if (" + ctrlName + ")" +  ctrlName + ".style.display = ''");		
		ctrlName = "form." + locationFldName + "_0";		
		eval("if (" + ctrlName + ")" +  ctrlName + ".style.display = ''");
	}
}

// Set area hidden fields according to chosen area
function SetHiddenArea(idFldName, descFldName)
{
	var ctrlObj = GetVisibleArea(idFldName);
	if (ctrlObj) {
		if (ctrlObj.selectedIndex > 0) {
			eval("form." + idFldName + ".value = ctrlObj.item(ctrlObj.selectedIndex).value");
			if (descFldName != "")
				eval("form." + descFldName + ".value = ctrlObj.item(ctrlObj.selectedIndex).text");
		}
		else {
			eval("form." + idFldName + ".value = ''");
			if (descFldName != "")
				eval("form." + descFldName + ".value = ''");
		}
	}
}

// Set location hidden fields according to chosen location
function SetHiddenLocation(idFldName, descFldName, valueFldName)
{     
	var ctrlObj = GetVisibleLocation(idFldName);
	if (ctrlObj) {
		if (ctrlObj.selectedIndex > 0) {
		    if (valueFldName)		    
			    eval("form." + valueFldName + ".value = ctrlObj.item(ctrlObj.selectedIndex).value");
			else
			    eval("form." + idFldName + ".value = ctrlObj.item(ctrlObj.selectedIndex).value");
			if (descFldName != "")
				eval("form." + descFldName + ".value = ctrlObj.item(ctrlObj.selectedIndex).text");
		}
		else {
			eval("form." + idFldName + ".value = ''");
			if (descFldName != "")
				eval("form." + descFldName + ".value = ''");
		}
	}
}

// Set OnChange event on all area comboxes
function SetAreaOnChange(areaFldName, func)
{
	var s;
	try {
		s = eval("divCountry"+areaFldName+".innerHTML");
	}
	catch (e) {
		return;	// No area combox
	}
	var arr = s.split(";");
	var ctrlName;
	for (var i = 0; i < arr.length; i++) {
		ctrlName = "form." + areaFldName + "_" + arr[i];
		eval(ctrlName + ".onchange = func");
	}
}

