// global variables
var s5_szActiveLayer = "";
var s5_arHints = new Array( "", "", "", "", "" );
var s5_isBatchMode = false;
/* common functions */

// Redirect to the Search Results page with query string that
// does not contain parameters with empty values
function s5_search()
{
	var reBlank = new RegExp("^ *$");
	var szQuery = "/store/searchresults.asp?";
	var blnFirstParam = true;

	with( isNN4 
		? document.layers[ "paneMainLayer" ].document.forms[ 0 ] 
		: document.forms[ "fmas" ] )
  {
		for( var i=0; i<elements.length; i++ )
		{
			if ( (elements[i].type != 'checkbox' && !reBlank.test(elements[i].value)) ||
					 (elements[i].type == 'checkbox' && elements[i].checked) ) {
				switch ( elements[i].type ) {
				case 'checkbox':
					if ( !blnFirstParam ) szQuery += '&';  else blnFirstParam = false;
					szQuery += elements[i].name + '=' + 'on';
					break;
				case 'select-one':
				case 'select-multiple':
					for ( var j=0; j<elements[i].options.length; j++ ) {
						if ( elements[i].options[j].selected ) {
							if ( !blnFirstParam ) szQuery += '&';  else blnFirstParam = false;
							szQuery += elements[i].name + '=' + escape(elements[i].options[j].value);
						}
					}
					break;
        default:
					if ( !blnFirstParam ) szQuery += '&';  else blnFirstParam = false;
		  		szQuery += elements[i].name + '=' + escape(elements[i].value);
					break;
				}
			}
		} 
  }

	window.location = szQuery;
}

// changes image source
function s5_setImageSource( szImgName, szNewSource, szLayerName /*for NN4 only*/ )
{
	if( isNN4 && (szLayerName != null) )
	{
		if( document.layers[ szLayerName ].document.images.length > 0 )
			document.layers[ szLayerName ].document.images[ szImgName ].src = szNewSource;  
	}
	else
		document.images[ szImgName ].src = szNewSource;
}

// get X coordinate of element's upper-left corner
function s5_getDivX( szDivName )
{
	if( isIE4 )
	{
		var nPos = 0;
		objCurrent = document.all[ szDivName ];
		while( objCurrent.tagName != "body" && objCurrent.offsetParent != null )
		{
			nPos += objCurrent.offsetLeft;
			objCurrent = objCurrent.offsetParent;
		}
		return nPos;
	}
	if( isNN4 )
		return document.layers[ szDivName ].pageX; 
	if( isNN6 )
		return document.getElementById( szDivName ).offsetLeft;
	return 0;
}		

// get Y coordinate of element's upper-left corner
function s5_getDivY( szDivName )
{
	if( isIE4 )
	{
		var nPos = 0;
		objCurrent = document.all[ szDivName ];
		while( objCurrent.tagName != "body" && objCurrent.offsetParent != null )
		{
			nPos += objCurrent.offsetTop;
			objCurrent = objCurrent.offsetParent;
		}
		return nPos;
	}
	if( isNN4 )
		return document.layers[ szDivName ].pageY; 
	if( isNN6 )
		return document.getElementById( szDivName ).offsetTop;
	return 0;
}		

// set position of the element
function s5_setDivXY( szDivName, nX, nY )
{
	if( isIE4 )
	{
		document.all[ szDivName ].style.left = nX;
		document.all[ szDivName ].style.top = nY;
	}
	if( isNN4 )
	{
		document.layers[ szDivName ].pageX = nX; 
		document.layers[ szDivName ].pageY = nY; 
	}
	if( isNN6 )
	{
		document.getElementById( szDivName ).style.left = nX;
		document.getElementById( szDivName ).style.top = nY;
	}
}

// set visibility of the element
function s5_setDivVisibility( szDivName, isVisible )
{
	if( isIE4 )
		document.all[ szDivName ].style.visibility = isVisible ? "visible" : "hidden";
	if( isNN4 ) 
		document.layers[ szDivName ].visibility = isVisible ? "show" : "hide";  
	
	if( isNN6 )
		document.getElementById( szDivName ).style.visibility = isVisible ? "visible" : "hidden"
}

// update hints on the main panel
function s5_UpdateHints( szLayerName )
{
	var szUl = isNN4 ? "" : "<ul class=\"hint\">";
	var szUlz = isNN4 ? "" : "</ul>";
	var szLi = isNN4 ? "- " : "<li>";
	var szLiz = isNN4 ? "<br>" : "</li>";
	
	var szHintLayer = szLayerName + "HelpResult";
	var szAllBox = szLayerName + "All";
	var szInner = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td>" + szUl;
	var nHints = 0;
	
	// walk hints
	with( isNN4 
		? document.layers[ szLayerName ].document.forms[ 0 ] 
		: document.forms[ s5_Layer2FormName( szLayerName ) ] )
	{
		for( var i=0; i<elements.length; i++ )
		{
			if( elements[ i ].type == "checkbox" )
			{
				if( elements[ i ].checked )
				{
					nHints++;
					if( nHints >= 5 )
					{
						szInner += szLi + "<span class=\"blk1a\"><a href=\"javascript://\" onclick=\"s5_handlerButtonClick('" + szLayerName + "')\">more...</a></span>" + szLiz;
						break;
					}
					szInner += szLi + "<span class=\"blk1a\">" + elements[ i ].value + "</span>" + szLiz;
				}
			}
		} 
	}
	
	szInner += szUlz + "</td></tr></table>";

	// empty the hint text if nothing is selected
	if( nHints == 0 )
		szInner = "&nbsp;";

	// store hints in the array (for NN4)
	s5_arHints[ s5_LayerName2No( szLayerName ) ] = szInner;
	
	// check/uncheck "all" checkbox
	with( isNN4 
		? document.layers[ "paneMainLayer" ].document.forms[ 0 ] 
		: document.forms[ "fmas" ] )
		if( nHints == 0 )
			elements[ szAllBox ].checked = true;
		else
			elements[ szAllBox ].checked = false;
		
	// write hints
	if( isIE4 )
		document.all[ szHintLayer ].innerHTML = szInner;
	if( isNN6 )
		document.getElementById( szHintLayer ).innerHTML = szInner;
	if( isNN4 && !s5_isBatchMode )
		s5_updateNN4HelpLayer();
}	

function s5_updateNN4HelpLayer()
{
	if( !isNN4 )
		return;
	var szCellWidth = ( window.innerWidth - 300 ) / 5;
		
	szLayerHTML = "<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\"><tr>";

	// alignment row
	for( var i=0; i<5; i++ )
	{
		szLayerHTML += "<td width=\"" + szCellWidth + "\" height=\"1\">";
		szLayerHTML += "<img src=\"/images/store/empty1x1.gif\" width=\"" + szCellWidth + "\" height=\"1\" border=\"0\" alt=\"\">";
		szLayerHTML += "</td>";
	}

	// data row
	szLayerHTML += "</tr><tr>";
	for( var i=0; i<5; i++ )
	{
		szLayerHTML += "<td align=\"center\" valign=\"top\" width=\"" + szCellWidth + "\">";
		szLayerHTML += s5_arHints[ i ];
		szLayerHTML += "</td>";
	}
	szLayerHTML += "</tr></table>";
	// write actions
	document.layers[ 'NN4TextHelpLayer' ].document.write( szLayerHTML );
	document.layers[ 'NN4TextHelpLayer' ].document.close();
}

// clear form on given layer
function s5_ClearForm( szLayerName )
{
	with( isNN4 
		? document.layers[ szLayerName ].document.forms[ s5_Layer2FormName( szLayerName ) ] 
		: document.forms[ s5_Layer2FormName( szLayerName ) ] )
	{
		for( var i=0; i<elements.length; i++ )
		{
			if( elements[ i ].type = "checkbox" )
			{
				elements[ i ].checked = false;
				s5_reg(elements[ i ]);
			}
		} 
	}
}

function s5_Layer2FormName( szLayerName )
{
	switch( szLayerName )
	{
		case "Topic": return "fmto";
		case "Jurisdiction": return "fmju";
		case "Publication": return "fmpu";
		case "Format": return "fmfo";
		case "Brand": return "fmbr";
	}
}

function s5_LayerName2No( szLayerName )
{
	switch( szLayerName )
	{
		case "Topic": return 0;
		case "Jurisdiction": return 1;
		case "Publication": return 2;
		case "Format": return 3;
		case "Brand": return 4;
	}
}

function s5_aa2LayerName( szAA )
{
	switch( szAA )
	{
		case "to": return "Topic";
		case "ju": return "Jurisdiction";
		case "pu": return "Publication";
		case "fo": return "Format";
		case "br": return "Brand";
	}
}

/* shortcuts */

function s5_hideMain()
{	
	if( isNN4 )
	{
		s5_setDivVisibility( 'paneMainLayer', false );
   		s5_setDivVisibility( 'NN4TextHelpLayer', false );
	}
	else
		s5_setDivVisibility( 'paneMainDiv', false );
}

function s5_showMain()
{
	if( isNN4 )
	{
		s5_setDivVisibility( 'paneMainLayer', true );
   		s5_setDivVisibility( 'NN4TextHelpLayer', true );
	}
	else
		s5_setDivVisibility( 'paneMainDiv', true );
}

function s5_clearMainForm()
{
	with( isNN4 
		? document.layers[ "paneMainLayer" ].document.forms[ 0 ] 
		: document.forms[ "fmas" ] )
	{
		elements["Keyword"].value="";
		elements["Title"].value="";
		elements["ISBN"].value="";
		elements["Author"].value="";
		elements["TopicAll"].checked="true";
		elements["JurisdictionAll"].checked="true";
		elements["PublicationAll"].checked="true";
		elements["FormatAll"].checked="true";
		elements["BrandAll"].checked="true";
		if (elements["Display"])
			elements["Display"].options["3"].selected="true";

		for (var i=1;i<=58;i++) elements[ 'hcbto'+i ].value="";
		for (var i=1;i<=57;i++) elements[ 'hcbju'+i ].value="";
		for (var i=1;i<=36;i++) elements[ 'hcbpu'+i ].value="";
		for (var i=1;i<=10;i++) elements[ 'hcbfo'+i ].value="";
		for (var i=1;i<=18;i++) elements[ 'hcbbr'+i ].value="";
	}
}

function s5_resetAllForms()
{
	if( isNN4 )
	{
		with(document.layers[ "Topic" ].document.forms[ "fmto" ])
		{
			for (var i=1;i<=58;i++) elements[ 'cbto'+i ].checked=false;
		}	
		with(document.layers[ "Jurisdiction" ].document.forms[ "fmju" ])
		{
			for (var i=1;i<=57;i++) elements[ 'cbju'+i ].checked=false;
		}	
		with(document.layers[ "Publication" ].document.forms[ "fmpu" ])
		{
			for (var i=1;i<=36;i++) elements[ 'cbpu'+i ].checked=false;
		}
		with(document.layers[ "Format" ].document.forms[ "fmfo" ])
		{
			for (var i=1;i<=10;i++) elements[ 'cbfo'+i ].checked=false;
		}
		with(document.layers[ "Brand" ].document.forms[ "fmbr" ])
		{
			for (var i=1;i<=18;i++) elements[ 'cbbr'+i ].checked=false;
		}
	}
	else
	{
		with(document.forms[ "fmto" ])
		{
			for (var i=1;i<=58;i++) elements[ 'cbto'+i ].checked=false;
		}
		with(document.forms[ "fmju" ])
		{
			for (var i=1;i<=57;i++) elements[ 'cbju'+i ].checked=false;
		}
		with(document.forms[ "fmpu" ])
		{
			for (var i=1;i<=36;i++) elements[ 'cbpu'+i ].checked=false;
		}
		with(document.forms[ "fmfo" ])
		{
			for (var i=1;i<=10;i++) elements[ 'cbfo'+i ].checked=false;
		}
		with(document.forms[ "fmbr" ])
		{
			for (var i=1;i<=18;i++) elements[ 'cbbr'+i ].checked=false;
		}
	}
	
	s5_clearMainForm();
}

function s5_updateAllHints()
{
	s5_isBatchMode = true;

	s5_UpdateHints( "Topic" );
	s5_UpdateHints( "Jurisdiction" );
	s5_UpdateHints( "Publication" );
	s5_UpdateHints( "Format" );
	s5_UpdateHints( "Brand" );

	s5_isBatchMode = false;
	s5_updateNN4HelpLayer();
}

/* handlers */

function s5_handlerButtonOver( szImgName, szNewSource, szLayerName )
{
	if( !is4Plus ) 
		return;
		
	s5_setImageSource( szImgName, szNewSource, szLayerName );
}

function s5_handlerButtonOut( szImgName, szNewSource, szLayerName )
{
	if( !is4Plus ) 
		return;

	s5_setImageSource( szImgName, szNewSource, szLayerName );
}

function s5_handlerButtonClick( szLayer )
{
	if( !is4Plus ) 
		return;
		
	var nX, nY;
	
	if( isNN4 )
	{
		nX = s5_getDivX( "s5posLayer" );
		nY = s5_getDivY( "s5posLayer" );
	}
	else
	{
		nX = s5_getDivX( "s5posDiv" );
		nY = s5_getDivY( "s5posDiv" );
	}
	
	s5_setDivXY( szLayer, nX, nY );
	
	s5_szActiveLayer = szLayer;
	s5_hideMain();
	s5_setDivVisibility( szLayer, true );	
}

function s5_handlerApply()
{
	s5_UpdateHints( s5_szActiveLayer );
	s5_setDivVisibility( s5_szActiveLayer, false );	

	s5_szActiveLayer = "";
	s5_showMain();
	window.scrollTo( 0, 0 );
}

function s5_handlerCancel()
{
	s5_ClearForm( s5_szActiveLayer );
}

function s5_reg( obCb )
{
	with( isNN4 ? document.layers[ "paneMainLayer" ].document.forms[ 0 ] : document.forms[ "fmas" ] )
		elements[ "h" + obCb.name ].value = obCb.checked ? obCb.value : "";
}

function s5_regBack( szCbName, szLayerName )
{
	with( isNN4 ? document.layers[ szLayerName ].document : document )
		forms[ s5_Layer2FormName( szLayerName ) ].elements[ szCbName ].checked = true;
}

function s5_regBackAll( )
{
	with( isNN4 
		? document.layers[ "paneMainLayer" ].document.forms[ 0 ] 
		: document.forms[ "fmas" ] )
	{
		for( var i=0; i<elements.length; i++ )
		{
			if( ( elements[ i ].type != "hidden" ) 
				|| ( elements[ i ].value != "on" ) )
				continue;

			var szCbName = elements[ i ].name.substr( 1 );
			var szLayerName = s5_aa2LayerName( elements[ i ].name.substr( 3, 2 ) );
			s5_regBack( szCbName, szLayerName );
		}
	}
}

function s5_clearAll()
{
	s5_resetAllForms();
	s5_updateAllHints();
	if( isNN4 )
   		s5_setDivVisibility( 'NN4TextHelpLayer', false );
}

function s5_onResize()
{
	// for NN4 only
	if( s5_szActiveLayer != "" )
		s5_handlerButtonClick( s5_szActiveLayer );
	else
		s5_updateAllHints();
}

function s5_onLoad()
{
	if( isNN4 )
		window.onResize = s5_onResize;
		
	s5_regBackAll();
	s5_updateAllHints();
}

function rm1_ClearAll()
{
	with( document.forms['AdvRefSearch'] )
	{
		elements["Keyword"].value = "";
		elements["title"].value = "";
		elements["SAPNum"].value = "";
		elements["MaterialsAbout"][0].checked = true;
		elements["Materialsfor"][0].checked = true;
		if ( elements["Display"])
			elements["Display"].options[3].selected = true;
	}
}
