var quickSearch = true;

// this function is needed to ensure that variables are set before processing/using them
function isUndefined(value)
{	
	return ( ((""+value) == "undefined") || ((""+value) == "null") || ((""+value) == ""));	
}

/*	Get Db Path
 * get the url to the current database
 * returns a string
 */
function getDbPath() 
{
	// next line is a computed text (cannot be copied into js editors)
	//alert("in getdbpath");
//	var compPath = "";
//	var jsLocation = ""+self.location.href;
	var url = "";
//	if(compPath == "")
//	{
//		url = jsLocation.substring(0, jsLocation.indexOf(".nsf"))+".nsf";  	
//	}
//	else
//	{
//		url = compPath;
//	}
	//alert(url);
	url = "/fsims_pw";
	return url;
}

function doSideSearch(f) {
    var search;
	var qFld = f.srchQuery2;
	var qVal = qFld.value;
	var kw_1 = "";
	var kw_2 = "";
	var kw_3 = "";
	var sOrder = "0";
	var pc = "";
	var sPrefs = "0";
	var S_1 = "";
	var S_2 = "";
	var sStatus = "A";
	var keys;
	var sSyns = "";
	var sVariants = "";
	var sFuzzy = "";
	var msg = "";
		
     if( qVal == '') {
     	alert('You must enter at least one word in the Search box.');
     	qFld.focus();
     	return true;
	} 
	//alert(qVal);
	//if(SearchErrorPopup(qFld)) return true;
	msg = ReserveWordsParseResults(qVal,false);
	if(msg != "") {
	    alert(msg);
	    qFld.focus();	    
	    return true;
	}
	
  	// Construct the search query
  	
	if(qVal == '') {
		search = "";
	} else {
		//search =  '\"'+qVal+'\"';
		search =  URLEncode(qVal);		
		search = 'q=' + search + '';
 	}	 
	
	//  Specialties - new for 1.5.1 (can be turned off -- as default)
    	sPrefs = '&sprefs=' + sPrefs;
	
	//  Keywords
	keys = qVal.toLowerCase();		
	var key = "";
	//alert(keys);
	if( keys != '') { 
		//keys = '"' + keys + '"'; 
		keys = replaceSubstring(keys, '  ', ' ');
		//keys = replaceSubstring(keys, ' ', ',');
		keys = replaceSubstring(keys, ';', ',');
		//keys = replaceSubstring(keys, ' ', '');
		keys = replaceSubstring(keys, ',,', ',');
		//alert(keys);
		keys = "&kw=" + URLEncode(keys);
		//alert(keys);
	} else {
		keys = '';
	}
		
	sOrder = "&sort=0";
	sStatus = "&status=A"
	sSyns = "&syn=1";
	sVariants = "&searchwv=0";
	sFuzzy = "&searchfuzzy=0";
	
	//queryString = search + keys + S_1 + S_2 + sPrefs + sStatus + sOrder;
	queryString = search + keys + sStatus + sSyns + sOrder + sVariants + sFuzzy;
	//queryString = URLEncode(search) + URLEncode(keys) + sStatus + sSyns + sOrder + sVariants + sFuzzy;
	//queryString = URLEncode(queryString);
	//alert(queryString);
	// new!!! set cookie to remember query, no longer caching page because of prefs error
	//q= location.search;
	//setDocCookie('SearchQuery', queryString);
	window.location.href='PICResults.aspx?mode=Search&' + queryString.toLowerCase() + '&' ;
	
    return true;
}

//function SearchErrorPopup(qFld)
//{
//	var match=false;
//	var qVal = qFld.value.toUpperCase();
//	
//	if( !(qVal.charAt(0) == '"' && qVal.charAt(qVal.length-1) == '"'))
//	{	
//        if( /\bACCRUE\b/.test(qVal)) match=true;
//	    if( /\bPARAGRAPH\b/.test(qVal)) match=true;
//	    if( /\bSENTENCE\b/.test(qVal)) match=true;
//	    if( /\bTERMWEIGHT\b/.test(qVal)) match=true;
//	    if( /\bFIELD\b/.test(qVal)) match=true;
//	    if( /\bEXACTCASE\b/.test(qVal)) match=true;
//	    if( /\bCONTAINS\b/.test(qVal)) match=true;
//	    if( qVal.indexOf('&') != -1) match=true;
//	    if( qVal.indexOf(',') != -1) match=true;
//	    if( qVal.indexOf('!') != -1) match=true;
//	    if( qVal.indexOf('|') != -1) match=true;
//	    if( qVal.indexOf('+') != -1) match=true;
//	    if( qVal.indexOf('-') != -1) match=true;
//	    if( qVal.indexOf(']') != -1) match=true;
//	    if( qVal.indexOf('[') != -1) match=true;
//	    if( qVal.indexOf('>') != -1) match=true;
//	    if( qVal.indexOf('<') != -1) match=true;
//	    if( qVal.indexOf('=') != -1) match=true;
//	    if( qVal.indexOf('+') != -1) match=true;
//	    if( qVal.indexOf('-') != -1) match=true;
//	    if( /\b*\b/.test(qVal)) match=true;
//	}
//	if(match)
//	{
//     	alert('The search phrase contains one or more reserve words or special characters.  The search cannot be executed.  Please place your entire search phrase in quotation marks before executing the search.');
//     	qFld.focus();
//     	return true;
//	} 
//	else
//	    return false;
//	
//}

//// code added from Fran's ElecHandbook.js in Intranet site
//function ReserveWordsParseResults(query,full) {
//    var msg = "";
//    var bacChar = "";
//    var hasQuotes = (query.charAt(0)=='"') && (query.charAt(query.length-1)=='"');
//    var parse =  (full || (!hasQuotes)); 
//    qVal = URLDecode(query.toUpperCase());
//    if (parse) {
//        var match = false;        
//        if (full) {                    
//            if( /\bAND\b/.test(qVal)) match=true;
//            if( /\bOR\b/.test(qVal)) match=true;
//            if( /\bNOT\b/.test(qVal)) match=true;
//            //if( query.indexOf('*') != -1) match=true;        
//            //if( query.indexOf('?') != -1) match=true;     
//            //if( query.indexOf('+') != -1) match=true;
//           	//if( query.indexOf('-') != -1) match=true;       			     
//        }
//        if( /\bACCRUE\b/.test(qVal)) match=true;
//        if( /\bPARAGRAPH\b/.test(qVal)) match=true;
//        if( /\bSENTENCE\b/.test(qVal)) match=true;
//        if( /\bTERMWEIGHT\b/.test(qVal)) match=true;
//        if( /\bFIELD\b/.test(qVal)) match=true;
//        if( /\bEXACTCASE\b/.test(qVal)) match=true;
//        if( /\bCONTAINS\b/.test(qVal)) match=true;
//        if( query.indexOf('&') != -1) match = true;            
//        if( query.indexOf(',') != -1) match=true;
//        if( query.indexOf('!') != -1) match=true;
//        if( query.indexOf('|') != -1) match=true;
//        if( query.indexOf(']') != -1) match=true;
//        if( query.indexOf('[') != -1) match=true;
//       // if( query.indexOf('+') != -1) match=true;
//        if( query.indexOf('-') != -1) match=true;
//        if( query.indexOf('>') != -1) match=true;
//        if( query.indexOf('<') != -1) match=true;
//        if( query.indexOf('=') != -1) match=true;
//        if( query.indexOf('(') != -1) match=true;
//        if( query.indexOf(')') != -1) match=true;  
//        
//        if(match) {
//	        msg= 'The search phrase contains one or more reserve words or special characters.  The search cannot be executed.  Please place your entire search phrase in quotation marks before executing the search.';
//        }
//    }
//    else {
//        thisquery = trimEnds(query,'" ');
//        if (thisquery=="-") {
//           msg="A valid search cannot be executed for just '-'.";
//        }
//         //else if (thisquery=="+") msg="A valid search cannot be executed for just '+'.";                  
//    }
//                 
//    return msg;  
//}

//// code added from Fran's ElecHandbook.js in Intranet site
//function URLEncode(query)
//{
//	// The Javascript escape and unescape functions do not correspond
//	// with what browsers actually do...
//	var SAFECHARS = "0123456789" +					// Numeric
//					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
//					"abcdefghijklmnopqrstuvwxyz" +
//					'_.!~*()" ';					// RFC2396 Mark characters
//	var HEX = "0123456789ABCDEF";

//	var plaintext = query;
//	var encoded = "";
//	for (var i = 0; i < plaintext.length; i++ ) {
//		var ch = plaintext.charAt(i);
//	    if (ch == " ") {
//		    encoded += " ";				// x-www-urlencoded, rather than %20
//		} else if (SAFECHARS.indexOf(ch) != -1) {
//		    encoded += ch;
//		} else {
//		    var charCode = ch.charCodeAt(0);
//			if (charCode > 255) {
//			    //alert( "Unicode Character '" 
//                   //     + ch 
//                   //     + "' cannot be encoded using standard URL encoding.\n" +
//			    //          "(URL encoding only supports 8-bit characters.)\n" +
//				//		  "A space (+) will be substituted." );
//				alert("A non-standard keyboard character was encountered. It will be removed from the search.")
//				encoded += "+";
//			} else {
//				encoded += "%";
//				encoded += HEX.charAt((charCode >> 4) & 0xF);
//				encoded += HEX.charAt(charCode & 0xF);
//			}
//		}
//	} // for
//	return encoded;
//}

// code added from Fran's ElecHandbook.js in Intranet site
function URLDecode(query)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = query;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == " ") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				plaintext += ch;
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
}

function getCheckboxValue( chkObj ) {
   // return: array
   var arr = new Array();
   var xx = 0;

   if ( chkObj[0] ) {
      for (var i=0; i < chkObj.length; i++) {
         if ( chkObj[i].checked )
            arr[xx++] = chkObj[i].value;
      }
   }
   else {
      if ( chkObj.checked )
         arr[xx++] = chkObj.value;
   }

   arr.length = xx;  
   return ( arr );
}


function jsval_IsNullCheckbox( chkObj ) {
   // return: boolean
   return ( getCheckboxValue(chkObj).length == 0 );
}

function getRadioValue( radioObj ) {
   // return: string
   for (var i=0; i < radioObj.length; i++) {
      if ( radioObj[i].checked )
         return ( radioObj[i].value );
   }
   return ( "" );
}

function getSelectValue( selObj ) {
	// return single item -- doesn't work for multi select groups
	val = "";
	if( selObj.selectedIndex != -1 ) {
		val = selObj.options[selObj.selectedIndex].value;
		if(val == "") {
			val = selObj.options[selObj.selectedIndex].text;
		}
	}	
	return val;
}

function trim( str ) {
	while( str.indexOf("  ") >= 0 ) {
		str = str.replace("  ", " ");
	}
	if ( str.charAt(0) == " " ) {
		str = str.substring( 1, str.length );
	}
	if ( str.charAt( str.length - 1 ) == " " ) {
		str = str.substring( 0, str.length -1 );
	}	
	while ( str.charAt( str.length - 1 ) == "," ) {
		str = str.substring( 0, str.length -1 );
	}	
	while ( str.charAt( 0 ) == "," ) {
		str = str.substring( 1, str.length );
	}	
	return str;
}

function trimEnds(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function replaceSubstring(srcStr, findStr, repStr) {
	while(srcStr.indexOf(findStr) >= 0) {
		srcStr = srcStr.replace(findStr, repStr);
	}	
	return srcStr;
} 

function buildKeywordValues(f) {
	// get the title, text or conditions and build the keyword search
	var titleVal = "";
	var fullTxtVal = "";
	var keywords = "";
	if(getRadioValue( f.SearchType ) == "1" ) {
		fullTxtVal = f.Search.value;
		if( f.Cond_1 ) { 
			cond_1 = f.Cond_1.value == '' ||  f.Op_1.options[f.Op_1.selectedIndex].value == 'AND NOT' ? '' :  ',' +  f.Cond_1.value; 
		}
		if( f.Cond_2 ) {
			cond_2 = f.Cond_2.value == '' ||  f.Op_2.options[f.Op_2.selectedIndex].value == 'AND NOT' ? '' :  ',' +  f.Cond_2.value ; 
		}	
		if( f.Cond_3 ) {		
			cond_3 = f.Cond_3.value == '' ||  f.Op_3.options[f.Op_3.selectedIndex].value == 'AND NOT' ? '' :  ',' +  f.Cond_3.value; 
		}				
	     fullTxtVal = fullTxtVal + cond_1 + cond_2 + cond_3;
	}
	
	keywords = titleVal + fullTxtVal;
	f.Keywords_1.value = keywords;
}

//function getSSID() {
//	var id = "";
//	listBox = document.forms[0].SearchLookup;
//	if(isUndefined(listBox) || listBox.selectedIndex == -1) {
//		//id = "";
//		//leave id blank
//	} else {
//		id = listBox.options[listBox.selectedIndex].value;
//		if(id == "0") {
//			id = "";
//		}
//	}	
//	return id;
//}

function doSearch(f) {
	if ( f.Keywords_1 ) {
		buildKeywordValues(f);
	}
	
	//aler('x');
	var isTitleSearch = false;
	
	var msg = "";
	
	var search;
	var cond_1 = "";
	var cond_2 = "";
	var cond_3 = "";
	var kw_1 = "";
	var kw_2 = "";
	var kw_3 = "";
	var sOrder = "0";
	var synSrch = "0";
	var pc = "";
	var ssid = "";
	var sPrefs = "0";
	var S_1 = "";
	var S_2 = "";
	var sVariants = "";
	var sFuzzy = "";

	// Search fields
	var fld_1 = "";   
	var fld_2 = "";
	var fld_3 = "";
	var fld_4 = "";
	var fld_5 = "";

//    if( f.Search.value == '' && f.POC.selectedIndex <1 && f.Owner.selectedIndex <1 && f.AFS_Owners.selectedIndex <1 && f.srch_date_1.value == '' && f.srch_date_2.value == '') {
//     	alert('You must enter at least one word in the Search box.');
//     	f.Search.focus();
//     	return true;
//	} 
	
	// loop the fields performing the check on each
	if( f.Search.value == '') {
     	alert('You must enter at least one word in the Search box.');
     	f.Search.focus();
     	return true;
	} 
	
	// must check all the query fields
	var fields = [f.Search, f.Cond_1, f.Cond_2, f.Cond_3];
	
	for(i = 0; i < 3; i++) {		
		//if(SearchErrorPopup(fields[i])) {
		msg = ReserveWordsParseResults(fields[i].value, false);
		if(msg != "") {
		    alert(msg);
		    fields[i].focus();
		    return true;
		}
	}

  	// Construct the search query
	if(f.Search.value == '') {
		search = 'q=';
	} else {
		if( getRadioValue( f.SearchType ) == "0" ) {
			search = "ti=" + URLEncode(f.Search.value);
			isTitleSearch = true;
		} else {
			//search =  '\"' + f.Search.value + '\"';		
			search =  "" + URLEncode(f.Search.value);		
//			if( f.Cond_1 ) { cond_1 = f.Cond_1.value == '' ? '' :  ' ' + f.Op_1.options[f.Op_1.selectedIndex].value + ' ' +  '\"' +f.Cond_1.value + '\"'; }
//			if( f.Cond_2 ) { cond_2 = f.Cond_2.value == '' ? '' :  ' ' + f.Op_2.options[f.Op_2.selectedIndex].value + ' ' + '\"' +f.Cond_2.value + '\"'; }
//			if( f.Cond_3 ) { cond_3 = f.Cond_3.value == '' ? '' :  ' ' + f.Op_3.options[f.Op_3.selectedIndex].value + ' ' + '\"' +f.Cond_3.value + '\"'; }
//			search = 'q=(' + search + cond_1 + cond_2 + cond_3 + ')';
			if( f.Cond_1 ) { 
			    //cond_1 = f.Cond_1.value == '' ? '' : f.Op_1.options[f.Op_1.selectedIndex].value == 'AND NOT' ? ' AND NOT ' +  f.Cond_1.value : ',' +  f.Cond_1.value; 
			    cond_1 = f.Cond_1.value == '' ? '' :  ' ' + f.Op_1.options[f.Op_1.selectedIndex].value + ' ' + URLEncode(f.Cond_1.value);
	        }
	        if( f.Cond_2 ) {
	            //cond_2 = f.Cond_2.value == '' ? '' : f.Op_2.options[f.Op_2.selectedIndex].value == 'AND NOT' ? ' AND NOT ' +  f.Cond_2.value : ',' +  f.Cond_2.value ; 
	            cond_2 = f.Cond_2.value == '' ? '' :  ' ' + f.Op_2.options[f.Op_2.selectedIndex].value + ' ' + URLEncode(f.Cond_2.value);
	        }	
	        if( f.Cond_3 ) {		
	            //cond_3 = f.Cond_3.value == '' ? '' : f.Op_3.options[f.Op_3.selectedIndex].value == 'AND NOT' ? ' AND NOT ' +  f.Cond_3.value : ',' +  f.Cond_3.value; 
	            cond_3 = f.Cond_3.value == '' ? '' :  ' ' + f.Op_3.options[f.Op_3.selectedIndex].value + ' ' + URLEncode(f.Cond_3.value);
	        }				
	        search = "q=" + search + cond_1 + cond_2 + cond_3;
 	   	}
	}	 

	//  Field Searches
//	if(f.srch_date_1 && f.srch_date_1.value != '') {
//		fld_1 = '&f1=' + getSelectValue( f.srch_oper_1 ) + f.srch_date_1.value;
//	}
//	
//	if(f.srch_date_2 && f.srch_date_2.value != '') {
//		fld_2 = '&f2=' + getSelectValue( f.srch_oper_2 ) + f.srch_date_2.value;
//	}
//	
//	if(f.Owner.selectedIndex > 0 ) {
//		fld_3= '&f3=' + getSelectValue( f.Owner );
//	}
//	
//	if(f.POC.selectedIndex > 0 ) {
//		fld_4= '&f4=' + getSelectValue( f.POC );
//	}
//	
//	if(f.AFS_Owners.selectedIndex > 0 ) {
//		fld_5= '&f5=' + getSelectValue( f.AFS_Owners );
//	}
//	

    // Publication Document Types
//	var docTypes = getCheckboxValue( f.DocType ) + "";
//	var docTypes2 = "";
//	var dts = new Array();
//	var item = "";
//	var sep = "_";
//	var dt = "";
//	
//	if( docTypes != "") { 
//		if(docTypes + "" != "All") {
//			dts = docTypes.split(",");
//			for(i = 0; i < dts.length; i++) {
//				item = dts[i]+"";
//				dt = item.substr(0, item.indexOf(sep))+"";
//				if(dt != "") {
//					if(docTypes2 != "") {
//						docTypes2 += "," + item;
//					} else {
//						docTypes2 += item + "";
//					} 
//					docTypes = replaceSubstring(docTypes, item,"");
//				}
//			}
//			docTypes = replaceSubstring(docTypes, ",,", ",");
//			docTypes = trim(docTypes);
//			if( docTypes != "") { 
//				docTypes = '&dt=' + docTypes; 
//			}
//			if( docTypes2 != "") { 
//				docTypes2 = '&dt2=' + docTypes2; 
//			}
//		}
//	}
	

//    	//  Specialties - new for 1.5.1 (can be turned off -- as default)
//    	sPrefs = getCheckboxValue( f.SearchPrefsOnOff );
//    	if( sPrefs == '1' ) {
//		S_1 =  (typeof S1 == 'string') ? '&s1=' + S1 :  ""  ; 
//		S_2 =  (typeof S2 == 'string') ? '&s2=' + S2 :  ""  ; 
//	}
//	if( sPrefs == '') { sPrefs = '0' }	// this will include them
//	sPrefs = '&sprefs=' + sPrefs;
//	// Validation Date Range
//    	var valDates = "";
//   	
//   	// Expiration Date Range  
//    	var expDates = "";
//   
	//  Areas Of Interest
	var aois = getCheckboxValue( f.aoiOption ) + '';
	if( aois != '' && aois != 'All' ) { 
		aois = '&aoi=' + aois; 
	} else {
		aois = '';
	}
	//  Subjects
	var subjs = getCheckboxValue( f.subjOption ) + '';
	if( subjs != '' && subjs != 'All' ) { 
		subjs = '&sub=' + subjs; 
	} else {
		subjs = '';
	}
	//  DocTypes
	var docTypes = getCheckboxValue( f.ptypeOption ) + '';
	if( docTypes != '' && docTypes != 'All' ) { 
		docTypes = '&dt=' + docTypes; 
	} else {
		docTypes = '';
	}
		
	//  Keywords
	var sKeys = "";
	
	var keys = "";	
	if(!isTitleSearch) {	   
		if( f.Cond_1 ) { 
		    cond_1 = f.Cond_1.value == '' ||  f.Op_1.options[f.Op_1.selectedIndex].value == 'AND NOT' ? '' :  ',' +  URLEncode(f.Cond_1.value); 
	    }
	    if( f.Cond_2 ) {
		    cond_2 = f.Cond_2.value == '' ||  f.Op_2.options[f.Op_2.selectedIndex].value == 'AND NOT' ? '' :  ',' +  URLEncode(f.Cond_2.value); 
	    }	
	    if( f.Cond_3 ) {		
		    cond_3 = f.Cond_3.value == '' ||  f.Op_3.options[f.Op_3.selectedIndex].value == 'AND NOT' ? '' :  ',' +  URLEncode(f.Cond_3.value); 
	    }				
	    keys = "&kw=" + URLEncode(f.Search.value) + cond_1 + cond_2 + cond_3;
	}
	
	if(getRadioValue( f.SortOrder ) == "1" ) {
		sOrder = "&sort=1";
	} else {
		sOrder = "&sort=0";
	}
	
	if(getCheckboxValue( f.UseSynonyms ) == "1" ) {
		synSrch = "&syn=1";
	} else {
		synSrch = "&syn=";
		synSrch = "";
	}	
	
	if(getCheckboxValue( f.UseVariants ) == "1" ) {
		sVariants = "&searchwv=1";
	} else {
		sVariants = "";
	}	
	
	if(getCheckboxValue( f.UseFuzzy ) == "1" ) {
		sFuzzy = "&searchfuzzy=1";
	} else {
		sFuzzy = "";
	}	
	
//	if(getRadioValue( f.ActiveStatus ) == "I" ) {
//		activeStatus = "&status=I";
//	} else {
//		activeStatus = "&status=A";
//	}
    activeStatus = "&status=A";
	
//	if(getSSID() != "") {
//		ssid = "&ssid=" + getSSID();
//	}
	
	// get page count (pc) per results page
//	if(getSelectValue( f.SearchPageCount ) != "" ) {
//		pc = "&pc="+getSelectValue( f.SearchPageCount );
//	} else {
		pc = "&pc=200";
//	}

	//queryValues = search  + keys + S_1 + S_2 + sPrefs + valDates + expDates + AOI + docTypes + docTypes2 + ssid + activeStatus + fld_1 + fld_2 + fld_3+ fld_4  + fld_5 + sOrder + synSrch;
	queryValues =  search + keys + activeStatus + aois + subjs + docTypes + fld_1 + fld_2 + fld_3 + fld_4  + fld_5 + sOrder + synSrch + sVariants + sFuzzy;
	//queryValues = URLEncode(queryValues);
    //alert(queryValues);
	// new!!! set cookie to remember query, no longer caching page because of prefs error
	setDocCookie('SearchQuery', queryValues);
	showWaitDiv(true);
	window.location.href='PICResults.aspx?mode=Search&' + queryValues.toLowerCase() + '&' ;  //  'path' is defined in calendarDateInput.js 
    return true;
}


function getCheckboxValue( chkObj ) {
   // return: array
   var arr = new Array();
   var xx = 0;

   if ( chkObj[0] ) {
      for (var i=0; i < chkObj.length; i++) {
         if ( chkObj[i].checked )
            arr[xx++] = chkObj[i].value;
      }
   }
   else {
      if ( chkObj.checked )
         arr[xx++] = chkObj.value;
   }

   arr.length = xx;  
   return ( arr );
}


function jsval_IsNullCheckbox( chkObj ) {
   // return: boolean
   return ( getCheckboxValue(chkObj).length == 0 );
}

function getRadioValue( radioObj ) {
   // return: string
   for (var i=0; i < radioObj.length; i++) {
      if ( radioObj[i].checked )
         return ( radioObj[i].value );
   }
   return ( "" );
}

function getSelectValue( selObj ) {
	// return single item -- doesn't work for multi select groups
	val = "";
	if( selObj.selectedIndex != -1 ) {
		val = selObj.options[selObj.selectedIndex].value;
		if(val == "") {
			val = selObj.options[selObj.selectedIndex].text;
		}
	}	
	return val;
}

// functions for clicking on notes actions ********************************

function getAnchorObj(anchorName)
{	
	alert("getAnchor");
	var anchor;
	if (navigator.appName == "Netscape")
	{	
		anchor = document.anchors[anchorName];	
	}
	else
	{	
		anchor = document.anchors(anchorName);	
	}
	return anchor;
}

function getAnchorOnClick(anchorName)
{	
	//alert("getAnchorOnClick");
	var oc;
	if (navigator.appName == "Netscape")
	{	
		oc = document.anchors[anchorName].getAttribute("onClick");	
	}
	else
	{	
		oc = document.anchors(anchorName).getAttribute("onClick");	
	}
	return oc;
}

function getAnchorHref(anchorName)
{	var anchor;
	if (g_isNS)
	{	anchor = document.anchors[anchorName].href;	}
	else
	{	anchor = document.anchors(anchorName).href;	}

	return anchor;
}

function doClickAction(anchorName)
{	// determine page destination by locating the document's hidden anchor's herf values
		
	if (g_isNS)
	{	var action = document.anchors[anchorName].getAttribute("onclick");

		if ((isUndefined(action)) || (action == ""))
		{	window.location.href = getAnchorHref(anchorName);	}
		else
		{	// remove "return " function
			action = action.substring(action.indexOf(" "));
			test = eval(action);
		}
	}
	else
	{	document.anchors(anchorName).click();	}
}

// new code to replace caching of last search
// gets values from cookie that are set everytime user clicks GO button
function setPreviousSearch() {
	var cookieSQ = getDocCookie("SearchQuery");
	if(isUndefined(cookieSQ) || cookieSQ == "") {
		return;
	}
	// note these fields must be editable now to use in js
	// thus, page must be refreshed to update fields (they are Notes fields)
	var sid = document.forms[0].SSID.value;
	var ssq = document.forms[0].SSSearchQuery.value;

	if(sid == "" && ssq == "") {
		// get cookie value
		if(isUndefined(cookieSQ) || cookieSQ == "") {
			// leave new form alone
			return;
		} else {
			// rebuild search form url to include query of last search performed
			url = location.href;
			if(url.indexOf("&q=") == -1) {
				url = url + cookieSQ;
			} else {
				url = url.substring(0, url.indexOf("&q=")) + cookieSQ;
			}

			// reload page with last search values for initialization
			location.href = url;
			// delete the cookie, assuming last values already loaded
			delDocCookie("SearchQuery");
		}
	}	
}

function showWaitDiv(showIt) {
	div = document.getElementById("waitDiv");
	if(isUndefined(div)) {
		//alert("boom");
		return;
	} 
	if(showIt) {
		div.style.display = "inline";
		obj = document.getElementById('msg');
		blink();
	} else {
		div.style.display = "none";	
	}
	return;	
}

var onOff = 0;
var obj;

function blink() {
    if ( onOff == 0 ) {
	obj.innerText = ".";
	onOff = 1;
    }  else  if ( onOff == 1 ) {
	obj.innerText = ". .";
	onOff = 2;
    }  else  if ( onOff == 2 ) {
	obj.innerText = ". . .";
	onOff = 3;
    }  else {
	obj.innerText = " ";
	onOff = 0;
    }
    setTimeout("blink()",500);
}

function checkEnterOld(e) { 
 if ((navigator.appName == "Microsoft Internet Explorer" )){	
	if (window.event.keyCode==13) { doSearch(document.forms[0] ); event.returnValue=false;}
    }  else {
	if (e.which==13) {doSearch(document.forms[0]);}
   }
}

function checkEnter(e) { 

    var KeyCode = (e.keyCode) ? e.keyCode : e.which;
    if ( KeyCode==13 ){
    	if ( e.srcElement.id == 'srchQuery2' ) {
    		doSideSearch(document.forms[0]);
    		e.returnValue=false;
    	} else if (e.srcElement.id == 'srchQuery' ) {
    	    //if (location.href.indexOf('?search.aspx') > 0) {
    	  	    doSearch(document.forms[0]);
    		    e.returnValue=false;
    		//}    
    	} else {
    		e.returnValue=false;
    	}
    }
}

function expandCollapseSection(divID, obj ) {
    var divObj = document.getElementById(divID);
    if ( divObj.style.display == 'none' ) {
	divObj.style.display = 'block'
	obj.src='images/collapse.gif'
    } else {
	divObj.style.display = 'none'
	obj.src='images/expand.gif'
    }
}

function handleCheckboxChange( checkedItem, chkObj ) {
      if(checkedItem.value == "All") {
            uncheckAllCheckboxes( chkObj, 1 );
      } else {
		uncheckFirstCheckbox( chkObj, 1 );
      } 
      return;
}

function uncheckAllCheckboxes( chkObj, startIndx ) { 
     var s = 0;
     if(!isUndefined(startIndx)) {
		s = startIndx;
     } 
     if( chkObj.length == undefined ) {
	chkObj.checked = false;
     } else {
         for( i = s; i < chkObj.length; i++) {
		 chkObj[i].checked = false;
         }
     }
     return;
}

function checkAllCheckboxes( chkObj, startIndx ) { 
     var s = 0;
    if(!isUndefined(startIndx)) {
		s = startIndx;
     } 
     for( i = 0; i < chkObj.length; i++) {
		chkObj[i].checked = true;
     }
     return;
}

function uncheckFirstCheckbox( chkObj ) {
	chkObj[0].checked = false;
	return;
}

function uncheckCheckboxValue( chkObj, chkValue ) {
     for( i = 0; i < chkObj.length; i++) {
	 if(chkObj[i].value == chkValue) {
		chkObj[i].checked = false;
	}
     }
     return;
}

function checkCheckboxValue( chkObj, chkValue ) {
     for( i = 0; i < chkObj.length; i++) {
	 if(chkObj[i].value == chkValue) {
		chkObj[i].checked = true;
	}
     }
     return;
}

function clearSelectValue( selObj ) {
	// clears the selected value
	selObj.selectedIndex = -1;
}

function showDocType2(checkedItem, type) {
	var sGrpDiv;
	var sGrpChkObj;
	if(type == "Bulletins") {
		sGrpDiv = document.getElementById("cbSubBulletins");
		sGrpChkObj = document.forms[0].DocType2;
	}
	// if checked display sub options and check them
	// if unchecked hide the sub optons and uncheck them	
	
	if(checkedItem.checked) {
		sGrpDiv.style.display = "";
	} else {
		uncheckAllCheckboxes(sGrpChkObj);
		sGrpDiv.style.display = "none";	
	}
	return;
} 

function handleSubCheckboxChange( parentChkValue, chkObj ) {
      var parentChkObj;
      var chkParentOn = false;
      var indexesToTest = new Array();
      if(parentChkValue == "Bulletins") {
		parentChkObj = document.forms[0].DocType;
		indexesToTest[0] = 0;
		indexesToTest[1] = 1;
      } 
      for(i = 0; i < indexesToTest.length; i++) {
	indx = indexesToTest[i];
	if(chkObj[indx].checked) {
		chkParentOn = true;
	}
      }
      if(chkParentOn) {
	checkCheckboxValue(parentChkObj, parentChkValue);
      } else {
            uncheckCheckboxValue(parentChkObj, parentChkValue);
      }
      return;
}

