var loading = true;

var NETSCAPE_4_TEXT_HEIGHT= 65;
var NETSCAPE_6_TEXT_HEIGHT= 65;

// return array of selected EAN numbers
function getEanArray() {
	
	var result = new Array(NumberOfImages); //CJNS
	var frm = null;
		
	for (var i=0; i < result.length; i++) { 
		
		if ( isnav4 ) {
			frm = document.layers['picture' + i] ? 
				  document.layers['picture' + i].document.forms[0] : null;
		} else {
			frm = document.forms['frm' + i] ? 
				  document.forms['frm' + i] : null;
		}

		var chkbox = eval('frm.check' + i);
		var eanbox = frm.ean_number;

		result[i] = chkbox.checked ? eanbox.value : "";
	}

	return result;
}

// return array of selected Id numbers
function getIdArray() { 

	var result = new Array(NumberOfImages); //CJNS
	var frm = null;
	
	for (var i=0; i < result.length; i++) { 
		
		if ( isnav4 ) {
			frm = document.layers['picture' + i] ? 
				  document.layers['picture' + i].document.forms[0] : null;
		} else {
			frm = document.forms['frm' + i] ? 
				  document.forms['frm' + i] : null;
		}

		var chkbox = eval('frm.check' + i);
		var hlbox = frm.hl_number;

		result[i] = chkbox.checked ? hlbox.value : "";
	}
	
	return result;
}


// checks all checkboxes displayed
function checkAllCheckboxes() { 

	var frm = null;
	
	for (var i=0; i < NumberOfImages; i++) { 
		
		if ( isnav4 ) {
			frm = document.layers['picture' + i] ? 
				  document.layers['picture' + i].document.forms[0] : null;
		} else {
			frm = document.forms['frm' + i] ? 
				  document.forms['frm' + i] : null;
		}

		var chkbox = eval('frm.check' + i);

		chkbox.checked =true ;
		if ( isnav4 ) saveCheckBoxState(chkbox);

	}
	
}







//CJNS
function init()
{
	parent.search_simple_contents.focus(); 
	show_caption('null',0)
}

// THIS IS THE MOST IMPORTANT FUNCTION WHICH HANDLES THE IMAGE POSITIONING WHEN
// THE WINDOW IS RESIZED.
// IF THE BROWSER IS NAVIGATOR THEN AN DUMMY FORM IS SUBMITED FROM THE PARENT FRAME[frame_search_results_thumb.asp]
// BECAUSE NAVIGATOR DOES HANDLE LAYERS PROPERLY WHEN WINDOW IS RESIZED.
// IF THE BROWSER IS EXPLORER THEN THE SCREEN WIDTH IS CALCULATED AND THE DIVISION ARE
// DYNAMICALLY PLACED
function handleresize()
{
	if ( isnav4 ) {	
		document.location.reload();
	}

	if (isie || isnav6)  {
		doc_width = GetInsideWindowWidth()-260;
		nCols = Math.floor(doc_width / (ImageWidth + ImageBorderWidth));
/*		nRows = Math.ceil(NumberOfImages / nCols);
		x_Border = (doc_width - (nCols * (ImageWidth + ImageBorderWidth))) / 2;
		y_Border = TopMargin;
*/
		for (var Image=0; Image < ActualNumberOfImages; Image++)
			elmtpos(Image);	
		var nRow = Math.floor(ActualNumberOfImages / nCols)+1;
		if ((nRow-1) == (ActualNumberOfImages / nCols)) nRow--;
		leftHeight = y_Border + (ImageHeight + ImageBorderHeight + ImageLabelHeight) * nRow+22;
//		document.all.left.style.height = leftHeight;
//		document.all.table_left.style.height = leftHeight;
		document.all.table_right.style.height = leftHeight;
 	}
}

// THIS IS ALSO AN IMPORTANT FUNCTION WHICH CALCULATES THE WINDOW WIDTH AND POSITIONS THE DIV TAGS
// ACCORDINGLY AND MAKE THEM VISIBLE.
function elmtpos(ImageNumber)
{  

	var nRow = Math.floor(ImageNumber / nCols);
	var x_Offset = x_Border + ((ImageNumber - (nRow * nCols)) * (ImageWidth + ImageBorderWidth));
	var y_Offset = y_Border + (ImageHeight + ImageBorderHeight + ImageLabelHeight) * nRow;
	/*if (ImageNumber==0) {
		alert(x_Border + '+((' + ImageNumber + '-(' +  nRow + '*' +  nCols + '))*(' +  ImageWidth + '+' +  ImageBorderWidth+"))="+x_Offset);
	}	*/

	if (x_Offset > 2)
	{	
		shifttoshow('border' + ImageNumber, x_Offset, y_Offset);
		shifttoshow('picture' + ImageNumber, x_Offset, y_Offset);
	}
}
// BASED ON THE BROWSER TYPE THE DIV TAGS ARE MOVED TO X AND Y POSITIONS BASED ON
// THE PARAMETER
function shiftto(obj,x,y)
{
	var theobj = GetObject(obj)
	if (theobj != null)
	{
		if (isnav4)
		{	
			theobj.moveTo(x,y);
		}
		if (isnav6)
		{
			var theobj = document.getElementById(obj)
			theobj.style.left=x
			theobj.style.top =y
		}
		else
		{
			theobj.pixelLeft=x;
			theobj.pixelTop=y;
		}
	}	
}
function shifttoBottom(obj,x,y)
{
	var theobj = GetObject(obj)
	
	if (theobj != null)
	{
		if (isnav4)
		{
			theobj.moveTo(x,y);//CJTODO
		}
		if (isnav6)
		{
			var theobj = document.getElementById(obj)
			theobj.style.left=x
			theobj.style.bottom =y
		}
		else
		{
			theobj.pixelLeft=x;
			theobj.pixelBottom=y;
		}
	}	
}
// BASED ON THE BROWSER TYPE THE DIV TAGS ARE MOVED TO X AND Y POSITIONS BASED ON
// THE PARAMETERS AND MAKES THE DIV TAGS VISIBLE
function shifttoshow(obj,x,y)
{

	if (isnav4)
	{   var theobj = GetObject(obj)
		theobj.moveTo(x,y);
		theobj.visibility ='visible';
	}
	if (isnav6)
	{
		var theobj = document.getElementById(obj)
		theobj.style.left=x
		theobj.style.top =y
		theobj.style.visibility='visible'
	
	} else {
	
	   var theobj = GetObject(obj); 
		theobj.pixelLeft=x;
		theobj.pixelTop=y;
		theobj.visibility ='visible';
	}

}
// BASED ON THE BROWSERS THIS FUNCTION IS EXECUTED TO DISPLAY THE
// DIV TAGS WHICH POPULATES THE CAPTION ON THE MOUSEOVER OF THE IMAGES
function showlayer(obj){
	if (isnav4)
	{
	var theobj = GetObject(obj)
//	if (theobj != null)
		theobj.visibility ='visible';
	}
	if (isie)
	{
	var theobj = GetObject(obj)
	theobj.visibility ='visible';
	}
	if (isnav6)
	{
		var theobj = document.getElementById(obj)
		theobj.style.width = GetInsideWindowWidth()-20
		theobj.style.left=0
		theobj.style.visibility='visible';
	}
}
// BASED ON THE BROWSERS THIS FUNCTION IS EXECUTED TO HIDE THE
// DIV TAGS WHICH POPULATES THE CAPTION ON THE MOUSEOVER OF THE IMAGES
function hidelayer(obj)
{
	if (isie || isnav4) {
		var theobj = GetObject(obj)
		if (theobj != null) {
			theobj.visibility ='hidden';
		}
	}

	if(isnav6) {	
		var theobj = document.getElementById(obj)
		
		theobj.style.visibility='hidden';
	}
}

function getElemById(id) {
	
	var isnav4 = false;
	var isnav6 = false;
	var isie = false;
	
	if (navigator.appName == "Netscape") {
		isnav4 = parseInt(navigator.appVersion) >= 4 && parseInt(navigator.appVersion) < 5;
		isnav6 = parseInt(navigator.appVersion) == 5;
	}

	// this is to assign value to some variables if IE browser
	isie = navigator.appName == "Microsoft Internet Explorer";


	if ( isie ) {
		return eval('document.all.' + id);
	} 

	if ( isnav4 ) {
		return eval('document.' + id);
	} 
	
	if ( isnav6 ) {
		return document.getElementById(id);
	} 
	
	return null;
}

// BASED ON THE BROWSER THIS FUNCTION IS EXECUTED TO DISPLAY THE DIV TAGS WITH THE CAPTION
function showlegend(x,n)
{
	if ( loading ) return;

	if (n == -1)
	{
		n = NumberOfImages;
		x = 'l' + NumberOfImages;
	}

	var theobj = getElemById(x);

	if (!theobj) return false;

	var textWidth = (GetInsideWindowWidth() - 2 * (x_Border * 2 + ImageBorderWidth));

	// set text
	sText = "";
	/*
	if (this)
	{
	    if (this.getPageStory(n))
		{
			sText = this.getPageStory(n);
			
			if (isie || isnav6) {

			   	var theobj = getElemById(x);

				if ( sText && theobj )	{

					var str = "<TABLE bgcolor='#CCCCCC' CELLSPACING='0' CELLPADDING='0' BORDER='0' WIDTH='100%'>";
					str += "<TR><TD><TABLE WIDTH='" + textWidth + "' BORDER='0' CELLSPACING='0' CELLPADDING='0' align='center'>";
					str += "<TR><TD>";
					str += "<SPAN class='b2text'>";
					str += sText;
					str += "</SPAN></TD>";
					str += "</TR>";
					str += "</TABLE></TD></TR></TABLE>";
					if ((navigator.userAgent.indexOf("MSIE 5.0;") != -1) &&
						(navigator.userAgent.indexOf("Mac") != -1))
						str += "&nbsp;";
					theobj.innerHTML = str;
				} else {

					// this is for case when no search results are returned but still want summary layer to display
					if ( theobj ) {
						
						var str = "<TABLE bgcolor='#CCCCCC' CELLSPACING='0' CELLPADDING='0' BORDER='0' WIDTH='100%'>";
						str += "<TR><TD><TABLE WIDTH='" + textWidth + "' BORDER='0' CELLSPACING='0' CELLPADDING='0' align='center'>";
						str += "<TR><TD>";
						str += "<SPAN class='b2text'>";
						str += sText;
						str += "</SPAN></TD>";
						str += "</TR>";
						str += "</TABLE></TD></TR></TABLE>";
						
						theobj.innerHTML = str;
					}
				}

			}
			
			if (isnav4) {
				if ( sText != null && sText != "" && 
					 document.layers[x] != null  && 
					 document.layers[x].document != null )	{
					
					var str = "<TABLE bgcolor='#CCCCCC' CELLSPACING='0' CELLPADDING='0' BORDER='0' WIDTH='100%'>";
					str += "<TR><TD><TABLE WIDTH='" + textWidth + "' BORDER='0' CELLSPACING='0' CELLPADDING='0'>";
					str += "<TR><TD>";
					str += "<SPAN class='b2text'>";
					str += sText;
					str += "</SPAN></TD>";
					str += "</TR>";
					str += "</TABLE></TD></TR></TABLE>";
					
					document.layers[x].document.writeln(str);
					document.layers[x].document.close();
					
				} else {

					// this is for case when no search results are returned but still want summary layer to display
					if ( document.layers && document.layers[x] && 
						 document.layers[x].document ) {
						
						document.layers[x].document.open();
						document.layers[x].document.writeln("<TABLE CELLSPACING='0' CELLPADDING='0' BORDER='0' WIDTH='100%'>");
						document.layers[x].document.writeln("<TR><TD><TABLE WIDTH='" + textWidth + "' BORDER='0' CELLSPACING='0' CELLPADDING='0' ALIGN='left'>");
						document.layers[x].document.writeln("<TR><TD>");
						document.layers[x].document.writeln("<SPAN class='b2text'>");
						document.layers[x].document.writeln(sText);
						document.layers[x].document.writeln("</SPAN></TD>");
						document.layers[x].document.writeln("</TR>");
						document.layers[x].document.writeln("</TABLE></TD></TR> </TABLE>");
						document.layers[x].document.close();
					}
				}
			}
		}
	}*/

	// position text box

//	if (!loading)
	{
		if (isie)
		{	
		// if document.body.scrollBottom == undefined then it's IE6 on MAC
			var bottom = navigator.appVersion.indexOf("Macintosh") == -1 ? 0 :
			document.body.scrollHeight - 
			( document.body.scrollTop + 
			document.body.clientHeight );
			shifttoBottom(x, 0, bottom);
		}
		if (isnav4)
		{
			var topposition = (innerHeight + pageYOffset - theobj.clip.height);
			var doc_width = GetInsideWindowWidth();
			var leftposition = x_Border * 2 + ImageBorderWidth;
			if (leftposition < 0 ) leftposition = 0;
			textWidth = GetInsideWindowWidth() - 2 * leftposition;
//			var numberOfLines = (sText.length / (textWidth / 8)) + 1
//			topposition = topposition - numberOfLines * 10.5;
			shiftto(x,leftposition,topposition);
			shiftto('cccccc',0,topposition);
			GetObject('cccccc').resizeTo(GetInsideWindowWidth()+20,1000);
			showlayer('cccccc');
		}
		
		if (isnav6)
		{	
			shifttoBottom(x, 0, 0);
//			var topposition = (innerHeight + pageYOffset - theobj.offsetHeight);
//			var doc_width = GetInsideWindowWidth();
//			var leftposition = ((doc_width-660)/2);
//			if (leftposition < 0 ) leftposition = 0;
//			shiftto(x,leftposition,topposition);
		}
		
		showlayer(x);
	}

}

// BASED ON THE BROWSER THIS FUNCTION IS EXECUTED TO DISPLAY THE DIV TAGS WITH THE CAPTION
function replacelegend(x,n)
{
	if ( loading ) return;

	if (n == -1)
	{
		n = NumberOfImages;
		x = 'l' + NumberOfImages;
	}

	var theobj = getElemById(x);

	if (!theobj) return false;

	var textWidth = (GetInsideWindowWidth() - 2 * (x_Border * 2 + ImageBorderWidth));

	sText = "";
	{
		if (isie)
		{	
		// if document.body.scrollBottom == undefined then it's IE6 on MAC
			var bottom = navigator.appVersion.indexOf("Macintosh") == -1 ? 0 :
			document.body.scrollHeight - 
			( document.body.scrollTop + 
			document.body.clientHeight );
			shifttoBottom(x, 0, bottom);
		}
		if (isnav4)
		{
			var topposition = (innerHeight + pageYOffset - theobj.clip.height);
			var doc_width = GetInsideWindowWidth();
			var leftposition = x_Border * 2 + ImageBorderWidth;
			if (leftposition < 0 ) leftposition = 0;
			textWidth = GetInsideWindowWidth() - 2 * leftposition;
			shiftto(x,leftposition,topposition);
			shiftto('cccccc',0,topposition);
			GetObject('cccccc').resizeTo(GetInsideWindowWidth()+20,1000);
			showlayer('cccccc');
		}
		
		if (isnav6)
		{	
			shifttoBottom(x, 0, 0);
		}
	}
}

// BASED ON THE BROWSER THIS FUNCTION IS EXECUTED TO HIDE THE DIV TAGS WITH THE CAPTION
function hidelegend(x)
{	
	if ( loading ) return;

	if (x == 'l-1')
		x = 'l' + NumberOfImages;

	if (!loading)
	{
		if (isie) {
			hidelayer(x);
		}
		
		if (isnav4 || isnav6)
		{
			hidelayer(x);
			hidelayer('cccccc');
		}
	}
}

// THIS FUNCTION IS TO HIDE THE SROLLING OF THE TEXT ON THE IMAGES
function scrollwin() { 

	for( i=0; i<ActualNumberOfImages; i++) { 
		obj = 'l' + i;
		hidelegend(obj)	
	}
}
