/**
 * Copyright (c) 1998-2009 InterQuery Software, Inc. All Rights Reserved.
 *
 * This javascript is the confidential and proprietary information of
 * InterQuery Software, Inc..  You shall not use or copy this javascript
 * code without the written permission of InterQuery Software, Inc..
 *
*/

var globalCount=200 ;
var reqCount=0 ;
var updates;
var ds_number=1 ;
var req ;
var selectionWindow=null;
var savedWindow=null;
var view ;

function doUpdate(search) {
	view=search ;
	if (pageCount!="...") {
		window.status="No update needed" ;
		return ; // already updated
	}
	reqCount=0 ;
	updates=window.setInterval("getData()", 3000) ;
}

function GetXmlHttpObject() {
	var objXMLHttp=null ;
	if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	}
	return objXMLHttp;
}

function getData() {
	reqCount++ ;
	if (reqCount==7) {
		//alert("Clearing updates, max=7") ;
		window.clearInterval(updates) ;
		window.status="Max updates reached" ;
		return ;
	}
	req=GetXmlHttpObject() ;
	if (req==null) {
		alert("Javascript error: req is null") ;
		return ;
	}
	req.onreadystatechange = setData;
	req.open("GET", "/search/gr/" + view + "?submit=GetValues&valueID=resultstotal&valueID=sortlist&valueID=pagetotal&valueID=duplicatesources&id="
			+ task + "&page="+ pageNumber +"&eqformat=xml&profile=gr&v=" + globalCount++, true);
	req.send(null);
}

function setData() {
	if (req.readyState!=4) return ;
	i=1 ;
	data = req.responseText;
	// sort list
	x1=data.indexOf("<sortlist>") ;
	n=document.getElementById("sortlist") ;
	//alert("n=" + n) ;
	if (x1!=-1 && n!=undefined) {
		x2 = data.indexOf("</so", x1) ;
		optionList=data.substring(x1+10, x2);
		i=2 ; x1=0;
		do {
			x1=optionList.indexOf("<value>", x1) ;
			if (x1==-1) break ;
			x2=optionList.indexOf("</val", x1) ;
			var optionValue=optionList.substring(x1+7, x2) ;
			x1=optionList.indexOf("<label>", x1) ;
			x2=optionList.indexOf("</lab", x1) ;
			var optionText=optionList.substring(x1+7, x2) ;
			//alert("n is a " + n.type + ", length=" + n.length) ;
			if (i>=n.length) {
				//alert("Adding an option") ;
				opt = document.createElement('option') ;
				opt.text = optionText ;
				opt.value = optionValue ;
				try { n.add(opt,null) ; } catch(ex) { n.add(opt);}
			}
			else {
				//alert("Replacing an option") ;
		    	n.options[i].text=optionText ;
				n.options[i].value=optionValue ;
			}
			i++ ;
		} while(true) ;
	}
	// page count
	x1 = data.indexOf("<pagetotal>") ;
	n=document.getElementById("pagecounttext") ;
	if (x1!=-1 && n!=undefined) {
		x2 = data.indexOf("</pa", x1) ;
		pageCount=data.substring(x1+11, x2);
		if (pageCount!="...") {
			window.clearInterval(updates) ;
			window.status="Search completed" ;
			n.innerHTML=pageCount;
		}
	}
	// results total
	x1 = data.indexOf("<resultstotal>") ;
	n=document.getElementById("resultstotal") ;
	if (x1!=-1 && n!=undefined) {
		x2 = data.indexOf("</res", x1) ;
		html=data.substring(x1+14, x2);
		n.innerHTML=html;
	}
	// data sources
	i=1 ;
	do {
		n=document.getElementById("ds_"+i) ;
		if (n==undefined) break;
		x1 = data.indexOf("<column>", x1) ;
		if (x1==-1) return ;
		x2 = data.indexOf("</co", x1) ;
		n.innerHTML=data.substring(x1+8, x2);
		i++ ; // counts number of elements
		x1 += 8 ;
	}
	while (true);
}

function keywords() {
	var str = document.topForm.keyword.value;
	var str1 = str.replace(/\+/gi," AND ");
	var str2 = str1.replace(/\-/gi," AND NOT ");
	document.topForm.kw2.value = str2;

	str = document.topForm.keyword.value;
	str1 = str.replace(/\+/gi,"+ ");
	//str2 = str1.replace(/\-/gi,"- ");
	document.topForm.kw3.value = str1;
}

function checkItem(id) {
	//alert("CheckItem=" + id) ;
	cbox = document.getElementById(id) ;
	//alert("cbox.value=" + cbox.value) ;
	if (cbox==null) return ;
	if (who=="resultsPage") {
		uck=document.getElementById("uck_" + cbox.value) ;
		uck.value="" ;
		uck.checked=false ;
		if (!cbox.checked) {
			uck.value=cbox.value;
			uck.checked=true ;
		}
	    if (document.cookie.indexOf("sw_userDisabled=true")!=-1) {
			//alert("checkItem(): sw_userDisabled is true") ;
			return ;
		}
		openSelectionWindow() ;
		uck.value="" ;
		uck.checked=false ;
		return ;
	}
	if (who=="selectionPage") {
	    if (opener==null) {
			alert("Can't uncheck results in tabbed window") ;
			cbox.checked=true ;
			return ;
		}
		document.f.Submit.value="Update Selection" ;
		document.f.target="_self" ;
		validateNotes(document.f) ;
		document.f.submit() ;
		if (window.opener!=null && opener.turnOffCheckbox!=undefined)
			window.opener.turnOffCheckbox(id) ;
	}
}

function turnOffCheckbox(id) {
	x = document.getElementById(id) ;
	if (x!=null) x.checked=false ;
}

function openSelectionWindow() {
	if (selectionWindow==undefined || selectionWindow.closed) {
		selectionWindow=window.open("", "selectionPage",
			"top=1000,left=1000,scrollbars=yes,resizable=yes,width=645,height=350");
	}
	document.f.target="selectionPage";
	document.getElementById("SubmitID").value="Display Selection";
	document.f.submit() ;
	selectionToggleHide() ;
	selectionWindow.focus() ;
}

/* user clicks on the Show/Hide link*/
function toggleSelectionWindow() {
	try {
		if (document.cookie.indexOf("sw_visible=true")!=-1) {
			// selection window is visible: close it
			document.cookie="sw_visible=false" ;
			if (selectionWindow==undefined || selectionWindow.closed) {
				// new opener does not have selectionWindow defined
				// re-acquire window object
				selectionWindow=window.open("", "selectionPage");
			}
			selectionWindow.close() ;
			selectionToggleShow() ;
			document.cookie="sw_userDisabled=true" ; // user wants sw hidden
		}
		else {
			// selection window is not visible: open it
			document.cookie="sw_visible=true" ;
			openSelectionWindow() ;
			selectionWindow.focus() ;
			document.cookie="sw_userDisabled=false" ;
		}
	}
	catch (ex) {
		alert("Exception=" + ex) ;
	}
}

function selectionToggleShow() {
	document.getElementById("showhideselectionwindow").innerHTML="Show" ;
	document.cookie="sw_visible=false" ;
}

function selectionToggleHide() {
	document.getElementById("showhideselectionwindow").innerHTML="Hide" ;
	document.cookie="sw_visible=true" ;
}

function loadSelection() {
	if (window.opener!=null)
		window.opener.selectionToggleHide() ;
	else
		document.cookie="sw_userDisabled=true" ; // don't open sw window when checkbox is clicked on
	document.cookie="sw_visible=true" ;
}

/* when user closes the selection window, or window reloaded after change of selection*/
function unloadSelection() {
	if (window.opener!=null) {
		window.opener.selectionToggleShow() ;
	}
	else {
		document.cookie="sw_userDisabled=true" ;
	}
	document.cookie="sw_visible=false" ;

}

function submitCached() {
	try {
		var x = document.fcached.id.selectedIndex ;
		var selectedText = document.fcached.id.options[x].text ;
		var action = selectedText.substring(0, selectedText.indexOf(", ")) ;
		var oldAction = document.fcached.action;
		document.fcached.action = oldAction + action ;
		document.fcached.submit() ;
		document.fcached.action = oldAction ;
	}
	catch (ex) {alert("javascript error"); return false;}
}

function submitSort() {
	if (document.sort.sortby.value==22)
		document.sort.sortorder.value=1;
	l=document.f.uncheck.length;
	for (i=0, j=0; i<l; i++, j++)
		if (document.f.uncheck[j].value!=null && document.f.uncheck[j].value!="") {
			//alert("j=" + i + ", document.f.uncheck[j].value=" + document.f.uncheck[j].value) ;
			document.sort.appendChild(document.f.uncheck[j]) ;
			j-- ; //  document.f.uncheck[i] is removed each time
		}
	document.sort.submit() ;
	document.sort.sortorder.value=0;
}

function checkSaved(newsaved) {
	try {
		savedList=document.f.savedName.options ; // list of saved selection pages
		if (savedList==null) {
			//alert("saveList is null") ;
			return ;
		}
		for (i=0; i<savedList.length; i++) {
			if (savedList[i].value==newsaved.value) {
				x=confirm(newsaved.value + " already exists. Press OK to replace it.") ;
				return x ;
			}
		}
	}
	catch (ex) {
		//alert("checkSaved exception") ;
		return ;
	}
}

function openSavedWindow() {
	// should really open a new tab in the opener!
	if (savedWindow==undefined || savedWindow.closed)
		savedWindow=window.open("SavedPage", "savedPage","menubar=yes,toolbar=yes,titlebar=yes,resizable=yes,location=yes,status=yes,scrollbars=yes,directories=yes");
	else savedWindow.focus() ;
	return savedWindow ;
}

function saveSelection() {
	if (checkSaved(document.f.newSaved)==false) {
		document.f.Submit.value="Update Selection";
		document.f.target="_self" ;
		return false ;
	}
	savedWindow=openSavedWindow() ;
	document.f.Submit.value="Save Selection";
	document.f.target="savedPage" ;
	return true ;
}

function retrieveSelection() {
	savedWindow=openSavedWindow() ;
	document.f.Submit.value="Get Selection";
	document.f.target="savedPage" ;
}

function editSelection() {
	//alert("selectionWindow=" + selectionWindow) ;
	savedWindow=openSavedWindow() ;
	document.f.Submit.value="Edit Saved";
	document.f.target="savedPage" ;
}

function addSelection() {
	savedWindow=openSavedWindow() ;
	document.f.Submit.value="Add Selection";
	document.f.target="savedPage" ;
}

function deleteSelection() {
	document.f.Submit.value="Delete Selection" ;
	if (who=="selectionPage") {
		deletedWindow=window.open("", "deletedPage",
			"toolbar=no, menubar=no, width=430, height=120, top=900, left=1000");
		document.f.target="deletedPage" ;
		setTimeout("reloadSelection()", 1500) ;
	}
}

function reloadSelection() {
	document.f.Submit.value="Update Selection" ;
	document.f.target="_self" ;
	validateNotes(document.f) ;
	document.f.submit() ;
}

function removeSelection() {
	document.f.Submit.value="Update Selection" ;
	document.f.target="_self" ;
	// uncheck all "item" checkboxes
	var items = document.f.item ;
	if (items.length==undefined) {
		items.checked=false ; // only one item
		if (window.opener!=null && opener.turnOffCheckbox!=undefined)
			window.opener.turnOffCheckbox(items.id) ;
	}
	else // multiple items
		for (i=0; i<items.length; i++) {
			items[i].checked=false;
			if (window.opener!=null && opener.turnOffCheckbox!=undefined)
				window.opener.turnOffCheckbox(items[i].id) ;
	}
	document.f.submit() ;
}

function show(obj) {
  if (obj) {
    obj.style.display="inline";
    obj.style.visibility="visible";
  }
}

function hide(obj) {
  if (obj) {
    obj.style.visibility="hidden";
    obj.style.display="none";
  }
}

var help="" ;
function selectionHelp() {
	items=document.getElementsByName("item") ;
	items[0].checked=true ;
	openSelectionWindow() ;
}


/**
 * Copyright (c) 1998-2009 InterQuery Software, Inc. All Rights Reserved.
 *
 * This javascript is the confidential and proprietary information of
 * InterQuery Software, Inc..  You shall not use or copy this javascript
 * code without the written permission of InterQuery Software, Inc..
 *
*/
