var d = document;
var dg = d.getElementById;

var previewed = 0;
var srchUIratio = 0.5;
if(fetchcookieval("gsfx_srchscroll") != "blank") srchUIratio = parseFloat(fetchcookieval("gsfx_srchscroll"));
var srchheightlimit = 100;
var scaleswitch = false;
var staticheight = 0;
var min_pane_height = 20;
var previewed = false;
var notnav = true;

if(navigator.appName.indexOf("Netscape") > -1 || navigator.appVersion.indexOf("Mac") > -1) notnav = false;

top.mouse_y = -1;

function SetTab()
{
	tval = fetchcookieval("SrchPreTab");
	if(tval != 'blank' && tval != '')
	{
		gsfx_selectTab(parseInt(tval));
	}	
	else
	{
		gsfx_selectTab(3);
	}
}

function setStaticHeight(){
	
	if(dg){
		if(dg("tb_height")) staticheight += dg("tb_height").offsetHeight;
		if(dg("topbarheight")) staticheight += dg("topbarheight").offsetHeight;
		if(dg("midbarheight")) staticheight += dg("midbarheight").offsetHeight;
		if(dg("sres_previewbardiv")) staticheight += dg("sres_previewbardiv").offsetHeight/2;
	}
}

function calcscaleon(){
	top.scaleswitch = true;
//	d.body.style.cursor = "n-resize";
	if(event && event.clientY){
		top.mouse_y = event.clientY;
	}
}

function calcscaleoff(){
//	d.body.style.cursor = "";
	top.scaleswitch = false;
	
}

if(dg && notnav){
	d.onmouseup = calcscaleoff;
	d.onmousemove = scalesrch;
	d.onselectstart = cancelselect;
	window.onresize = calcsearchdivsizes;
	document.calcsearchdivsizes = calcsearchdivsizes;
}

function scalesrch()
{
	if(dg)
	{
		var goforit = false;

		if(top.scaleswitch)	{	goforit = true;	}
		else	{	goforit = false;	}
		
		if(goforit)
		{
		//	d.body.style.cursor = "n-resize";
			var yloc = event.clientY;
			if(!dg("sres_previewdiv")) yloc += 180 + top.sharedheight - top.pheight;
			if(top.mouse_y < 0) top.mouse_y = yloc;
			if(top.mouse_y != yloc){
				diffr = yloc - top.mouse_y;
				top.pheight = Math.round(top.sharedheight*(top.srchUIratio));
				top.pheight = top.pheight - diffr;
				
				if(top.pheight <= min_pane_height) top.pheight = min_pane_height;
				else if(top.pheight >= top.sharedheight-min_pane_height) top.pheight = top.sharedheight-min_pane_height;
				else top.mouse_y = yloc;

				top.srchUIratio = top.pheight/top.sharedheight;
				top.calcsearchdivsizes();
				setcookieval("gsfx_srchscroll", top.srchUIratio);
			}
		}
	}
}

function cancelselect(){
	if(top.scaleswitch) return false;
}

function gsfx_showrefine(){
	d.getElementById("sres_refinetable").style.display = "block";
}

function gsfx_hiderefine(){
	d.getElementById("sres_refinetable").style.display = "none";
}

function gsfx_refineselect(){
	if(dg){
		gsfx_showrefine();
		if(notnav && d.getElementById('sres_previewdiv')){
			if(d.sortform) gsfx_hidepreview();
		}
	}
	return true;
}

function gsfx_hidepreview(){
	if(dg('sres_previewdiv')){
		dg('sres_previewdiv').style.display = "none";
		dg('sres_previewbardiv').style.display = "none";
	}
	if(d.body.clientHeight){
		totalheight = d.body.clientHeight;
		scrollheight = d.body.scrollHeight;
	}else{
		totalheight = self.innerHeight;
		scrollheight = d.body.offsetHeight;
	}
	top.sharedheight = totalheight-159-55;
	dg('sres_resultsdiv').style.height = top.sharedheight;
	dg('sres_resultsdiv').style.overflowY = "visible";
}

function gsfx_showpreview(){
	dg('sres_previewbardiv').style.display = "block";
	dg('sres_previewdiv').style.display = "block";
	dg('sres_previewdiv').style.height = Math.ceil(top.sharedheight*top.srchUIratio); 
	dg('sres_resultsdiv').style.height = Math.floor(top.sharedheight*(1-top.srchUIratio));
	dg('sres_resultsdiv').style.overflowY = "scroll";
}

function gsfx_previewselect(){
	var maxscrollheightforpreview = screen.height - 104; // as an adjustment	// added by fengj for bug 7620
	if(dg && notnav){
		if(dg("sres_previewdiv")){

			tds = d.getElementsByTagName("td");
			for(i=0; i<tds.length; i++){
				if(tds[i].className == "sres_srchlinkclicked") tds[i].className = "sres_srchlink";
			}

			if(d.body.clientHeight){
				totalheight = d.body.clientHeight;
				scrollheight = d.body.scrollHeight;
			}else{
				totalheight = self.innerHeight;
				scrollheight = d.body.offsetHeight;
			}
			
			//the following "if" is add by fengj for bug 7620
			if (scrollheight > maxscrollheightforpreview)
				top.sharedheight = (maxscrollheightforpreview - staticheight);
			else if (scrollheight > totalheight)
				top.sharedheight = (scrollheight-staticheight);			
			
			if(previewed){
				d.getElementById("src_newwinhref").style.color ="white";
				dg('previewavail').style.display = "block";
				dg('previewnull').style.display = "none";
			}else{
				dg('previewnull').style.display = "block";
				dg('previewavail').style.display = "none";
			}

			gsfx_showpreview();
			gsfx_hiderefine();
		}
	}
	return true;
}

function gsfx_srchviewmode(i){
	if(dg){
		var targ = d.topform.srchviewmode;
		switch(i){
			case 1:
				gsfx_refineselect();
				break;
			case 2:
				gsfx_previewselect();
				break;
			case 3:
				if(notnav && d.getElementById('sres_previewdiv')) gsfx_hidepreview();
				gsfx_hiderefine();
				break;
		}
		calcsearchdivsizes();
	}
}

var selectedTab = 0;
function gsfx_selectTab(i){
	//alert("selectTab");
	srch_setcookieval("SrchPreTab" , i);
	if(i != selectedTab){
		selectedTab = i;
		if(i == 1) d.getElementById("tab_start").src = finstart_blue.src;
		else d.getElementById("tab_start").src = finstart_white.src;
		if(d.getElementById){
			for(j=1; j<4; j++){
				if(j == i){
					d.getElementById("tab_"+j+"_left").src = finleft_blue.src;
					d.getElementById("tab_"+j+"_bg").style.backgroundImage = "url('"+blueBG+"')";
					d.getElementById("tab_"+j+"_right").src = finright_blue.src;
					d.getElementById("tab_"+j+"_text").className = "sres_frontTab";
					
					var rightedge = 1;
					if(!notnav) rightedge = 2;
					if(i > 1) d.getElementById("tab_"+(j-rightedge)+"_right").src = finright_white_overlap.src;

					gsfx_srchviewmode(j);
				}else{
					d.getElementById("tab_"+j+"_left").src = finleft_white.src;
					d.getElementById("tab_"+j+"_bg").style.backgroundImage = "url('"+whiteBG+"')";
					d.getElementById("tab_"+j+"_right").src = finright_white.src;
					d.getElementById("tab_"+j+"_text").className = "sres_backTab";
				}
			}
			if(i != 1){
				document.images["curve_left"].src = bluecurveleft.src;
				document.images["curve_right"].src = bluecurveright.src;
			}else{
				document.images["curve_left"].src = cleargif.src;
				document.images["curve_right"].src = cleargif.src;
			}
		}
	}
}

function showdesc(){
	if(dg){
		//alert("showdesc");
		checkobj = d.sortform;
		descs = d.getElementsByName("srchdesc");
		for(i=0; i<descs.length; i++){
			if(checkobj.desccheck.checked){
				descs[i].style.display = "block";
				setcookieval("srchext","1");
			}else{
				descs[i].style.display = "none";
				setcookieval("srchext","0");
			}
		}
	}
}

var __SrchLastHeight = 0;
var __SrchLastScrollHeight = 0;

function calcsearchdivsizes(){
	var maxscrollheightforpreview = screen.height - 104; // as an adjustment //added by fengj for 7620
	if(dg && notnav){
		var doWork = false;
		
		if(d.body.clientHeight){
			totalheight = d.body.clientHeight;
			scrollheight = d.body.scrollHeight;
		}else{
			totalheight = self.innerHeight;
			scrollheight = d.body.offsetHeight;
		}
		//alert(__SrchLastHeight + "," + totalheight + "/" + __SrchLastScrollHeight + "," + scrollheight);
		if(totalheight != __SrchLastHeight) { 
			__SrchLastHeight = totalheight; 
			doWork = true;
		}
		if(scrollheight != __SrchLastScrollHeight) {
			__SrchLastScrollHeight = scrollheight;
			doWork = true;
		}
		//if(!doWork) { return; }		
		if(dg('sres_previewdiv')){
			if(dg('sres_previewdiv').style.display != "none"){
				// the following is modified by fengj for 7620
				if (scrollheight > maxscrollheightforpreview)
					top.sharedheight = (maxscrollheightforpreview - staticheight);
				else if (scrollheight > totalheight)
					top.sharedheight = (scrollheight-staticheight);
				if(top.sharedheight > srchheightlimit){
					dg('sres_resultsdiv').style.height = Math.floor(top.sharedheight*(1 - top.srchUIratio));
					dg('sres_previewdiv').style.height = Math.ceil(top.sharedheight*top.srchUIratio); 
					showdesc();
				}
			}else{
				if(dg('sres_refinetable')){
					dg('sres_resultsdiv').style.height = top.sharedheight;
				}else{
					dg('sres_resultsdiv').style.height = top.sharedheight;
				}
			}
		}
		if(dg('xnewsborder')){
			if(d.body.clientHeight) totalheight = d.body.clientHeight;
			else totalheight = self.innerHeight;
			if(d.body.clientHeight) totalwidth = d.body.clientWidth;
			else totalwidth = self.innerWidth;
			dg('xnewsborder').style.height = totalheight-44;
			dg('xnewsborder').style.width = totalwidth-4;
		}
	}
}

function scrolled(){
	calcsearchdivsizes();
}

function gsfx_verifykanisa(textval){
	var srcform = d.wizform;
	goforsubmit = 1;
	var radiochecked;
	for(i=0;i<srcform.elements.length;i++){
		var inputname = srcform.elements[i].name;
		if(inputname != "refinecheck"){
			if(srcform.elements[i].type.toLowerCase() == "text"){
				if(srcform.elements[i].value == '' || srcform.elements[i].value == textval) goforsubmit = 0;
			}
		}
	}
	if(!goforsubmit) alert("Please fill in the entire Search Wizard.");
	return goforsubmit;
}

function gsfx_srchwindow(url){
	url = url.replace("&tpl=preview.tpl", "");
	if(d.body.clientHeight){
		winheight = Math.round(window.document.body.clientHeight * 0.9);
		winwidth = Math.round(window.document.body.clientWidth * 0.9);
		leftmargin = Math.round(window.document.body.clientWidth * 0.05);
		topmargin = Math.round(window.document.body.clientWidth * 0.02);
	}else{
		winheight = Math.round(d.body.offsetHeight * 0.9);
		winwidth = Math.round(d.body.offsetWidth * 0.9);
		leftmargin = Math.round(d.body.offsetWidth * 0.05);
		topmargin = Math.round(d.body.offsetWidth * 0.02);
	}
	var win = window.open(url, "_top", "resizable=yes,scrollbars=yes,menubar=yes,location=yes,toolbar=yes,status=yes,height="+winheight+",width="+winwidth+",left="+leftmargin+",top="+topmargin);
	win.focus();
}

function CaptureState(gHTMLElementStateInfo){

}

function isMemberOf(divid,arrayVisibleDIV){
	var i;
	var boolFound;
	boolFound=false
	for (i=0;i<arrayVisibleDIV.length;i++) {if (arrayVisibleDIV[i]==divid){boolFound=true;}}
	return boolFound;
}

function show(id){
	if(d.getElementById) d.getElementById(id).style.display = "block";
	else if(d.layers) d.layers[id].visibility = "show";
	else if(d.all) d.all[id].style.display = "block";
}

function hide(id){
	if(d.getElementById) d.getElementById(id).style.display = "none";
	else if(d.layers) d.layers[id].visibility = "hide";
	else if(d.all) d.all[id].style.display = "none";
}

function RestoreState(gHTMLElementStateInfo){

	var strInVisibleDIV,arrayInVisibleDIV;
	var i,divid,collDIV;
	var VisibleDIVLookupTable;


	//get the invisible divs from the form field and make an array of it.
	strInVisibleDIV=gHTMLElementStateInfo.value
	  
	  
	if (!isblank(strInVisibleDIV)){ 
  		arrayInVisibleDIV=strInVisibleDIV.split("|");
  		collDIV=d.all.tags("DIV")
		
  		for (i=0;i<collDIV.length;i++){	
  			divid=collDIV(i).id;
  			if(!isblank(divid)){
  				if (isMemberOf(divid,arrayInVisibleDIV)){hide(divid);}
	  			else{show(divid);}
	  		}
  		}
	}
	  
	if(d.all.invisiblespans){
		currentVisible = d.all.invisiblespans.value;
		if(currentVisible != "") d.all[currentVisible].style.display = "inline";
	}
}

function fillInDialogState(tf){if(tf){tf.dialogState.value = dialogstate;}}

function answerLinksQuestion(theLinksForm,theCaptureStateElement, questionId, answerValue){
	fillInDialogState(theLinksForm);
	CaptureState(theCaptureStateElement);
	theLinksForm.id.value= questionId;
	theLinksForm.answerValue.value=answerValue;
	return theLinksForm.submit();
}

function gsfx_srchlink(prevURL, linkURL, srctag){
	if(dg){	if(dg('sres_previewdiv'))
		if(dg('sres_previewdiv').style.display != 'none'){
			previewed = 1; 
			gsfx_previewselect(); 
			if(srctag.parentElement) srctag.parentElement.className='sres_srchlinkclicked'; 
			dg('sres_previewdiv').src = prevURL + "&pr=1"; 
			if(prevURL.toLowerCase().indexOf("viewdoc.aspx") < 0) dg('src_newwinhref').href = linkURL;
			else dg('src_newwinhref').href = prevURL;
			return false;
		}else{
			if(prevURL.toLowerCase().indexOf("viewdoc.aspx") < 0) gsfx_srchwindow(linkURL); 
			else gsfx_srchwindow(prevURL);
			return false;
		}
	}else return true;
}

function gsfx_srchredir(url1, url2, sdval){
	pc = fetchcookieval("Params").toLowerCase();
	if(pc != "blank"){
		cva = new Array();
		nva = new Array();
		cva = pc.split("&");
		SD = "";
		for(i=0;i<cva.length;i++){
			nva = cva[i].split("=");
			if(nva[0] == "sd"){
				SD = nva[1];
				break;
			}
		}
		if(SD != sdval) d.location = url2.replace("||SDVAL||", SD);
		else d.location = url1;
	}else d.location = url1;
}

var produtil;

function writeKanisaProdStr(prodval){
	var writeme = "All products";
	var strval = new Array();
	for(i=0;i<10;i++){
		eval("top.produtil = top.PRD"+i);
		if(!top.produtil) eval("top.produtil = top.Product"+i);
		if(top.produtil){
			for(j=0;j<top.produtil.length;j++){
				strval = top.produtil[j].split("\",\"");
				if(strval[1].toLowerCase() == prodval.toLowerCase()){
					writeme = strval[0];
					i = 10;
					j = top.produtil.length;
				}
			}
		}else{
			i=10;
		}
	}
	d.write(writeme);
}

function __fireTrigger()
{	}
