function toggleLayer(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"none";
		}
	else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"none";
		}
	else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"none";
		}
}

/* detect browser *///////////////////////////////////////////
var ns4 = (document.layers);
var ie4 = (document.all && !document.getElementById);
var ie5 = (document.all && document.getElementById);
var ie7 = (document.all && document.getElementById);
var ie8 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);

function detectBrowser(x){
	//alert('detectBrowser');
	detectedObj="";
	if(ns6!=false){ // if NS or FIREFOX
    	detectedObj = document.getElementById(x);
    }else{
        detectedObj = eval("document.all."+x);
    }
	return detectedObj;
}

/// FROM "RESULT.JSP" ////////////////////////////////////////////////////
			var JSactionType;
			var JSactionName;
			
			function getPageResultArr(){   
				allCb = document.getElementById('userBookMarksForm').elements;
				var resultArr=new Array();
				var j=0;
			 	for (i=0 ; i< allCb.length ; i++ ){		
			      	if (allCb[i].name.indexOf('checkedResultId') != -1){
			      		resultArr[j] = allCb[i];
			      		j = j+1;     		
				   	} 
				}
				return resultArr;
			}
			// m : check box button (page/all)
			// val: check state (true/false)
			function changeTags(m, val) {
				// change page results check box state
				// to reflect the state change of the 
				// all/page check box button.
				// both for page and all, all page results
				// change their state to checked/unchecked
				// to reflect the relevant change of the 
				// page/all check box button changed state.
				var cbArr= new Array();
				cbArr= getPageResultArr();
				for (var i=0; i < cbArr.length; i++) {
					cbArr[i].checked = val; 
				}
				// if changed state was 'all' button
				// set/unset both 'all and 'page' buttons.
				if (m == 'all') {
					document.getElementById("chkPageTop").checked = val;
					document.getElementById("chkPageBottom").checked = val; 
					document.getElementById("chkAllTop").checked = val;
					document.getElementById("chkAllBottom").checked = val; 
				}
				// if changed state was 'page' button
				// set/unset the 'page' button.
				// In particular, if the button state was
				// changed to unset then also unset the 'all'
				// button.
				if (m == 'page') {
					document.getElementById("chkPageTop").checked = val;
					document.getElementById("chkPageBottom").checked = val; 
					if (val == false) {
						document.getElementById("chkAllTop").checked = val;
						document.getElementById("chkAllBottom").checked = val; 
					}
				}
			}
			
			function docCheck(val) {
				if (val == false) {
					document.getElementById("chkPageTop").checked = val;
					document.getElementById("chkPageBottom").checked = val; 
					document.getElementById("chkAllTop").checked = val;
					document.getElementById("chkAllBottom").checked = val; 
				}
			}
			
			function checkSubmit() {
				var docsChecked = resultChecked();
				if (docsChecked == true) {
		 			if (confirm(deleteResult)) {
		 				//document.getElementById("userBookMarksForm").action  = "DeleteSearchResultAction.do";
		 				document.getElementById("userBookMarksForm").action  = DeleteSearchResultActionPath;
			  			submitForm();	
					}
				} else {
					alert(selectBeforeDelete);
				}
			}		  
			
			function resortResults(url, obj) {
				urlStr = new String(url);
				rExp = '/NEWSORT/gi';
				newSortStr = new String(obj.value);
				newurl = urlStr.replace(rExp, newSortStr);
				window.location.href=newurl
			}
			
			function rechunkResults(url, obj) {
				urlStr = new String(url);
				rExp = '/NEWCHUNK/gi';
				newChunkStr = new String(obj.value);
				newurl = urlStr.replace(rExp, newChunkStr);
				window.location.href=newurl
			}
/*	function reViewResults(){ MOVED TO JSP PAGES searchResults.jsp and userBookMarkList.jsp
			 
			  
			  
			  	if(JSactionType=='Search'){
					//alert(JSactionType + 'Search');
					openFile(basicPath + 'GetFlatResultsAction.do','11')
				}else{
				   document.getElementById("userBookMarksForm").action = JSactionName;
			  		submitForm();
				}
			}
			*/
			//
			
			 /**
			  * gets the contents of the form as a URL encoded String
			  * suitable for appending to a url
			  * @param formName to encode
			  * @return string with encoded form values , beings with &
			  */ 
function getFormAsString(formName){
 	
 	//Setup the return String
 	returnString ="";
 	
  	//Get the form values
 	formElements=document.forms[formName].elements;
 	
 	//loop through the array , building up the url
 	//in the form /strutsaction.do&name=value
 	
 	for ( var i=formElements.length-1; i>=0; --i ){
 		//we escape (encode) each value
 		//TODO a hack added, since the url is too long when using 100 results
 		if (formElements[i].name == "resToDisplay" || formElements[i].name == "sortBy" ||( formElements[i].name == "chkAll" && formElements[i].checked) ){
 			returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
 		}
 	}
 	
 	//return the values
 	return returnString; 
 }
 
function pagerSubmit(offset){
	offsetStr = new String(offset);	
	if(JSactionType=='Search'){
		//alert(JSactionType + 'Search');
		var temp = getFormAsString('userBookMarksForm');
		if(saveOpenSub!=''){ 
			//openFile(''+basicPath + 'GetFilterResultsAction.do?pager.offset=' + offsetStr+ '&topicID=' + saveOpenSub + '&groupID='+saveGID+ temp,'22');
			openFile(''+GetFilterResultsActionPath+'?pager.offset=' + offsetStr+ '&topicID=' + saveOpenSub + '&groupID='+saveGID+ temp,'22');
		}else{
		  	//openFile(basicPath + 'GetFlatResultsAction.do?pager.offset=' + offsetStr +temp,'22')
		  	openFile(GetFlatResultsActionPath+'?pager.offset=' + offsetStr +temp,'22')
		}
	}else{
	  	document.getElementById("userBookMarksForm").action = JSactionName + "?pager.offset=" + offsetStr;
	  	submitForm();
	}
}
function submitForm(){
  document.getElementById("userBookMarksForm").submit();
}

function sendEmail(){
	var docsChecked = resultChecked();
	if (docsChecked == true) {
		//document.getElementById("userBookMarksForm").action = "GetEmailAction.do";
		document.getElementById("userBookMarksForm").action = GetEmailActionPath;
  		submitForm();	
	} else {
		alert(selectBeforeEMail);
	}
}

function performPrint(){
 	var checked =  resultChecked();
 	if (checked == true){
 		//document.getElementById("userBookMarksForm").action = "GetPrintViewAction.do?showMode=brief";
 		document.getElementById("userBookMarksForm").action = GetPrintViewActionPath + "?showMode=brief";
  		submitForm();	
	}else{
		alert (selectBeforePrint)
	}
}

function performExport(){
 	var checked =  resultChecked();
 	if (checked == true){
 		document.getElementById("userBookMarksForm").action = GetExportActionPath;
  		submitForm();	
 	}else{
 		alert(selectBeforeExport);
 	}
}

function resultChecked(){
	var cbArr= new Array();
	cbArr= getPageResultArr();
	var docsChecked = false;
	for (var i=0; i < cbArr.length; i++) {
		if (cbArr[i].checked == true) {
			docsChecked = true;
		}
	}
	return docsChecked; 
}
///////////////////////////////////////////////////////////////////////////////
function formatNumber(val){
	if(val.length>3){
		var tempVal="";
		var k=1;
		for(i=val.length-1;i>=0;i--){
			tempVal = val.charAt(i) + tempVal;
			if(k%3==0 && i!=0){
				tempVal = "," + tempVal;
			}
			k++
		}
		return tempVal;
	}else{
		return val;
	}
}

function openCloseResult(){
	if(detectBrowser("AllResults").style.display=='none'){
		detectBrowser("AllResults").style.display='';
		detectBrowser("AllResultsHREF").innerHTML = hideDatabaseDetails;
		showHideResultsText = hideDatabaseDetails;
		showHideResultsStat = show;
	}else{
		detectBrowser("AllResults").style.display='none';	
		detectBrowser("AllResultsHREF").innerHTML = showDatabaseDetails;
		showHideResultsText = showDatabaseDetails;
		showHideResultsStat = hide;
	}
}

function trim(s) {
   	// Remove leading spaces and carriage returns
   	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) { 
   		s = s.substring(1,s.length); 
   	}
     
   	// Remove trailing spaces and carriage returns
 	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) { 
 		s = s.substring(0,s.length-1); 
 	}
     
   	return s;
}

function unicodeEncode(str) {	
    str = replaceAll(str,"%","*~");
    return str;
}

function replaceAll(str_,src_,target_) {
    return replaceSubstring(str_,src_,target_);
}

function replaceSubstring(inputString, fromString, toString) {
    // taken from http://www.breakingpar.com/bkp/home.nsf/Doc?OpenNavigator&U=45A5696524D222C387256AFB0013D850
    // Goes through the inputString and replaces every occurrence of fromString with toString
    var temp = inputString;
    if (fromString == "") {
	return inputString;
    }
    if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
	while (temp.indexOf(fromString) != -1) {
	    var toTheLeft = temp.substring(0, temp.indexOf(fromString));
	    var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
	    temp = toTheLeft + toString + toTheRight;
	}
    } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
	var midStrings = new Array("~", "`", "_", "^", "#");
	var midStringLen = 1;
	var midString = "";
	// Find a string that doesn't exist in the inputString to be used
	// as an "inbetween" string
	while (midString == "") {
	    for (var i=0; i < midStrings.length; i++) {
		var tempMidString = "";
		for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
		if (fromString.indexOf(tempMidString) == -1) {
		    midString = tempMidString;
		    i = midStrings.length + 1;
		}
	    }
	} // Keep on going until we build an "inbetween" string that doesn't exist
	// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
	while (temp.indexOf(fromString) != -1) {
	    var toTheLeft = temp.substring(0, temp.indexOf(fromString));
	    var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
	    temp = toTheLeft + midString + toTheRight;
	}
	// Next, replace the "inbetween" string with the "toString"
	while (temp.indexOf(midString) != -1) {
	    var toTheLeft = temp.substring(0, temp.indexOf(midString));
	    var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
	    temp = toTheLeft + toString + toTheRight;
	}
    } // Ends the check to see if the string being replaced is part of the replacement string or not
    return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

// global request and XML document objects
var BookMarkReq;	
function hrefOnClick( aTag )
{
	newHref = aTag.href;
	uc = newHref.match( /[?|&]ucode=([^&]+)/ );

	if( !uc || typeof uc == 'undefined' )
	{
		return;
	}
		
	// set new href
	newHref = newHref.replace( /[?|&]ucode=[^&]+/, "" );
	aTag.href = newHref;
		
	if( !uc[ 1 ] || typeof uc[ 1 ] == 'undefined' )
	{
		return;
	}
		
	//mark = "GetMarkResultAction.do?checkedResultId(" + uc[ 1 ] + ")=on";
	mark = ""+GetMarkResultActionPath+"?checkedResultId(" + uc[ 1 ] + ")=on";
		
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) 
	{
		BookMarkReq = new XMLHttpRequest();
		if (BookMarkReq) 
		{
			BookMarkReq.onreadystatechange = processReqChange;
			BookMarkReq.open("GET", mark, true);
			BookMarkReq.send(null);
		}
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) 
	{		
		BookMarkReq = new ActiveXObject("Microsoft.XMLHTTP");
		if (BookMarkReq) {
			BookMarkReq.onreadystatechange = processReqChange;
			BookMarkReq.open("GET", mark, true);
			BookMarkReq.send();
		}
	}

	return false;
}	

// handle onreadystatechange event of req object
function processReqChange() 
{
	return;
    // only if BookMarkReq shows "loaded"
	/*
    if (BookMarkReq.readyState == 4) {
        // only if "OK"
        if (BookMarkReq.status == 200) {
            alert("OK");
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
	*/
}
