function AreaSelClick( aoEvent )
{
	if( aoEvent == null )
		aoEvent = event;
	if( aoEvent != null )
	{
		var loSrc = aoEvent.srcElement;
		if( loSrc == null )
			loSrc = aoEvent.target;
			
		if( loSrc != null && loSrc.tagName.toLowerCase( ) === "select" )
		{
			var loAreaSearch = CHUFindParentByTagAndClass( loSrc, "div", "areaSearch" );
			if( loAreaSearch != null )
				$( ".areaSearchWarn", loAreaSearch ).hide( );
		}
	}

	return true;
}

function AreaList( aoEvent )
{
	if( aoEvent == null )
		aoEvent = event;
	if( aoEvent != null )
	{
		var loSrc = aoEvent.srcElement;
		if( loSrc == null )
			loSrc = aoEvent.target;

		if( loSrc != null && loSrc.tagName.toLowerCase( ) === "select" && loSrc.value != "0" )
		{
			var loCurrListItem = loSrc.parentNode;
			if( loCurrListItem != null && loCurrListItem.tagName.toLowerCase( ) === "li" )
			{
				var loList = loCurrListItem.parentNode;
				if( loList != null && loList.tagName.toLowerCase( ) === "ul" )
				{
					var loDiv = loList.parentNode;
					if( loDiv != null && loDiv.tagName.toLowerCase( ) === "div" )
					{
						var loSpans = $( "span", loDiv );
						if( loSpans.length > 5 )
						{
							var lsUrl = loSpans.eq( 0 ).text( );
							var lsIOF = loSpans.eq( 2 ).text( );
							var lsScM = loSpans.eq( 3 ).text( );
							var lsScId = loSpans.eq( 4 ).text( );
							var lsPrefixes = loSpans.eq( 5 ).text( );
							var lsPreSel = loSpans.eq( 6 ).text( );
							if( lsUrl != null && lsUrl !== "" && lsScM != null && lsScM !== ""  &&
								lsScId != null && lsScId !== "" )
							{
								var loSel;
								var loListItems = $( "li", loList );
								var liIdx, liFnd = -1, liDiv, loListDiv;
								for( liIdx = 0; liIdx < loListItems.length; liIdx++ )
								{
									if( liFnd >= 0 )
									{
										loSel = $( "select", loListItems[liIdx] );
										if( loSel.length > 0 )
										{
											while( loSel[0].options.length > 0 )
												loSel[0].remove( 0 );
											loSel[0].value = "";
										}
										loSel = $( "div", loListItems[liIdx] );
										for( liDiv = 0; liDiv < loSel.length; liDiv++ )
											if( loSel[liDiv].id.indexOf( "slc" ) >= 0 )
												$( loSel[liDiv] ).html( "" );
									}
									if( loListItems[liIdx] === loCurrListItem )
										liFnd = liIdx;
								}

								if( liFnd >= 0 && liFnd < loListItems.length - 1 )
								{
									var loCtrlToUpdate = null;
									var loNextListItem = loListItems[liFnd + 1];
									loSel = $( "select", loNextListItem );
									if( loSel[0] == null )
									{
										loSel = $( "div", loNextListItem );
										for( liDiv = 0; liDiv < loSel.length && loCtrlToUpdate == null; liDiv++ )
											if( loSel[liDiv].id.indexOf( "slc" ) >= 0 )
											{
												loCtrlToUpdate = loSel[liDiv];
												lsScM = "2";
											}
									}
									else
									{
										loCtrlToUpdate = loSel[0];
										lsScM = "1";
									}

									var loImg = $( "img", loNextListItem );
									if( loCtrlToUpdate != null && loImg[0] != null )
									{
										//L loCtrlToUpdate.show( );
										loImg.show( );
										$.ajax( {
											cache: false,
											complete: AreaListFinal,
											data: "iof=" + lsIOF + "&pid=" + loSrc.value + "&scm=" + lsScM +
												"&tgtid=" + loCtrlToUpdate.id + "&imgid=" + loImg[0].id +
												"&scid=" + lsScId + "&pfx=" + lsPrefixes + "&prs=" + lsPreSel +
												"&lid=" + loList.id, //L
											error: AreaListErr,
											success: AreaListOk,
											url: lsUrl
											} );
									}
								}
							}
						}
					}
				}
			}
		}

		aoEvent.returnValue = false;
	}

	return false;
}

function AreaListOk( asData, asStatus )
{
	//L
	var lsListId = null, lsSelId = null, lsImgId = null, lsPreSel = null, lsResType = null, lsRes = null;
	var liCurrIdx, liPrevIdx, liNum;
	var lbContinue = true;

	//L
	liNum = 0;
	liCurrIdx = -1;
	do
	{
		liPrevIdx = liCurrIdx;
		liCurrIdx = asData.indexOf( "/", liPrevIdx + 1 );
		if( liCurrIdx > 0 )
		{
			if( liNum == 0 )
				lsListId = asData.substring( liPrevIdx + 1, liCurrIdx );
			else if( liNum == 1 )
				lsSelId = asData.substring( liPrevIdx + 1, liCurrIdx );
			else if( liNum == 2 )
				lsImgId = asData.substring( liPrevIdx + 1, liCurrIdx );
			else if( liNum == 3 )
				lsPreSel = asData.substring( liPrevIdx + 1, liCurrIdx );
			else if( liNum == 4 )
			{
				lsResType = asData.substring( liPrevIdx + 1, liCurrIdx );
				lsRes = asData.substring( liCurrIdx + 1 );
				lbContinue = false;
			}

			liNum++;
		}
		else
			lbContinue = false;
	}
	while (lbContinue);
	
	//L
	if( liNum == 5 )
	{
		/*L if( lsResType === "H" )
			$( "#" + lsSelId ).html( lsRes );
		else*/ if( lsResType === "J" )
			UpdateSelectors( lsListId, lsSelId, lsPreSel, lsRes );
	}

	//L
	if( lsImgId != null && lsImgId != "" )
		$( "#" + lsImgId ).hide( );
}

//L
function UpdateSelectors( asListId, asSelId, asPreSel, asRes )
{
	var loSelect = $( "#" + asSelId );

	if( loSelect[0] != null )
	{
		var lbErr = false;
		var loOptionsList;

		if( asRes == null || asRes === "" )
			loOptionsList = null;
		else
		{
			loOptionsList = eval( asRes );
			lbErr = ( typeof loOptionsList !== "object" || loOptionsList.length == 0 )
		}

		if( !lbErr )
		{
			if( asPreSel == "Y" )
				UpdateSelectorsSet( asListId, loSelect, loOptionsList );
			else if( asPreSel == "N" )
				UpdateSingleSelector( asListId, loSelect, loOptionsList );
		}
	}
}

//L
function UpdateSelectorsSet( asListId, aoSelect, aoOptionsList )
{
	if( asListId != null && asListId != "" )
	{
		var liState, liIdx, liCurrOptions;
		var loListItems = $( "#" + asListId + " li" ), loSelect;

		liState = 0;
		liCurrOptions = 0;
		for( liIdx = 0; liIdx < loListItems.length; liIdx++ )
		{
			loSelect = $( "select", loListItems.eq( liIdx ) );
			if( loSelect[0] != null )
			{
				if( liState == 0 )
				{
					loListItems.eq( liIdx ).show( );
					if( loSelect[0].id == aoSelect[0].id )
					{
						if( aoOptionsList == null )
							loListItems.eq( liIdx ).hide( );
						else
							UpdateSingleSelectorOptions( loSelect[0], aoOptionsList[liCurrOptions++] )
						liState = 1;
					}
				}
				else if( liState == 1 )
				{
					if( aoOptionsList == null )
					{
						loListItems.eq( liIdx ).hide( );
						liState = 2;
					}
					else if( liCurrOptions < aoOptionsList.length )
					{
						loListItems.eq( liIdx ).show( );
						UpdateSingleSelectorOptions( loSelect[0], aoOptionsList[liCurrOptions++] )
					}
					else
					{
						loListItems.eq( liIdx ).hide( );
						liState = 2;
					}
				}
				else if( liState == 2 )
					loListItems.eq( liIdx ).hide( );
			}
		}
	}
}

//L
function UpdateSingleSelector( asListId, aoSelect, aoOptionsList )
{
	if( asListId != null && asListId != "" )
	{
		var loListItem = $( "#" + asListId + " li:has(select#" + aoSelect[0].id + ")" );

		if( aoOptionsList != null )
		{
			var loSelect = $( "select", loListItem );
			if( loSelect.length > 0 )
			{
				UpdateSingleSelectorOptions( loSelect[0], aoOptionsList[0] );
				loListItem.show( );
			}
		}
		else
			loListItem.hide( );

		loListItem.nextAll( ).hide( );
	}
}

//L
function UpdateSingleSelectorOptions( aoSelect, aoOptions )
{
	var liOpt;
	var loOption;
	for( liOpt = 0; liOpt < aoOptions.length; liOpt++ )
	{
		loOption = document.createElement( "option" );
		loOption.text = aoOptions[liOpt].name;
		loOption.value = aoOptions[liOpt].value;
		try
		{
			aoSelect.add( loOption, null ); // standards compliant
		}
		catch (loEx)
		{
			aoSelect.options.add( loOption ); // IE only
		}
	}
}

function AreaListErr( aoReq, asErr, aoEx )
{
}

function AreaListFinal( aoReq, asType )
{
}

function AreaLink( asListId, asDlgId, asDlgEPContId, aoEvent ) //L
{
	if( aoEvent == null )
		aoEvent = event;
	if( aoEvent != null )
	{
		var loSrc = aoEvent.srcElement;
		if( loSrc == null )
			loSrc = aoEvent.target;

		if( loSrc != null )
		{
			var loLastSel = $( "#" + asListId + " li:visible:last select" )[0]; //L document.getElementById( asSelId );
			if( loLastSel != null )
			{
				var loAreaSearchContLink = CHUFindParentByTagAndClass( loSrc, "div", "areaSearchContLink" );
				if( loAreaSearchContLink != null )
				{
					if( loLastSel.value == null || loLastSel.value === "" || loLastSel.value === "0" )
						$( ".areaSearchWarn", loAreaSearchContLink ).show( );
					else
					{
						//L
						var lsLastChar;
						if( loLastSel.value.length == 1 )
							lsLastChar = loLastSel.value;
						else
							lsLastChar = loLastSel.value.substring( loLastSel.value.length - 1 );

						//L
						if( lsLastChar === "N" || lsLastChar === "S" )
							$( ".areaSearchWarn", loAreaSearchContLink ).show( );

						else
						{
							$( ".areaSearchWarn", loAreaSearchContLink ).hide( );
							var lsTgtUrl = location.protocol + "//" + location.host +
								$( "span", loAreaSearchContLink ).text( ) + loLastSel.value; //L
							if( asDlgId == null || asDlgEPContId == null )
								window.location.href = lsTgtUrl;
							else
							{
								$("#" + asDlgId).dialog({
									bgiframe: ( $.browser.msie && parseInt($.browser.version) < 9 ),// necessario per via di un'incompatibilità riscontrata con IE9
									width: 780,
									height: 510,
									closeOnEscape: false,
									modal: true
								});
								$( "#" + asDlgId + " iframe[name=" + asDlgEPContId + "]" ).attr( "src", lsTgtUrl );
								//popup( lsTgtUrl );
							}
						}
					}
				}
			}
		}

		aoEvent.returnValue = false;
	}

	return false;
}

function AreaLBCloseMap( asDlgId, asDlgEPContId )
{
	$( "#" + asDlgId + " iframe[name=" + asDlgEPContId + "]" ).attr( "src", "about:blank" );
	//$( "#" + asDlgId ).dialog( "close" );
	$( "#" + asDlgId ).dialog( "destroy" );
}

function AreaLBPrintMap( asDlgEPContId )
{
	top[asDlgEPContId].window.focus( );
	top[asDlgEPContId].window.print( );
}

//function popup(file)
//{
//	if(typeof file=='undefined')
//		file='about:blank';
//	var screenWidth = screen.availWidth||screen.width||1024;
//	var screenHeight = screen.availHeight||screen.height||768;
//	var	popupWidth=770;
//	var	popupHeight=500;
//	var	popupLeft=(screenWidth-popupWidth)/2;
//	var	popupTop=(screenHeight-popupHeight)/2-20;

//	if(typeof popup.ref=='undefined' || !popup.ref || popup.ref.closed)
//	{
//		popup.ref=window.open(file,'',"width="+popupWidth+",height="+popupHeight+",top="+popupTop+",left="+popupLeft+
//			",scrollbars=yes,resizable=yes,status=no,personalbar=no,toolbar=no,location=no,menubar=no,directories=no");
//	}

//	if(typeof popup.ref=='undefined' || !popup.ref || popup.ref.closed)
//	{
////		link.target = '_blank';
//		return true;
//	}
//	if( popup.ref.location.href != 'about:blank'&& popup.ref.location.href != file )
//	{
//		popup.ref.document.body.style.block = 'none';
//		popup.ref.location.replace(file);
//	}
//	popup.ref.focus();
//}

//if( typeof $ != 'undefined' )
//{
//	$(document).ready(function() {
//		$('#container .groupage.geoarea-search .grpllist .grplli a').click( function(){
//			popup( this.href );
//			return false;
//		});
//		$('#container .groupage.geoarea-search .grpslbl').text('');
//	});
//}

//L
if( typeof $ != 'undefined' )
{
	$(document).ready(function() {
		ResetAreas("aclist", "acsel");
		ResetAreas("grplist", "grpsel");
	});
}

//L
function ResetAreas(asListCls, asSelCls)
{
	var loSels = $("." + asListCls + " > li > select." + asSelCls);
	if( loSels.length > 0 )
	{
		var loIdxCtrl;
		var liIdx;
		for( liIdx = 0; liIdx < loSels.length; liIdx++ )
		{
			loIdxCtrl = $("+ span", loSels[liIdx]);
			if( loIdxCtrl.length == 0 )
				loSels[liIdx].selectedIndex = 0;
			else
				loSels[liIdx].selectedIndex = new Number(loIdxCtrl.text());
		}
	}
}

