/*
Program Purpose
**********************************************************************
Used in conjuction with paging.asp for viewing query results on a html 
page and being able to sort and view results per page
**********************************************************************

Bug Fix notes or code changes
**********************************************************************
Original Programmer: Anu
1) WIGe030002
programmer: Priority Support
date: 03/07/2003
lineNumber:
purpose: Fixes for Netscape 7.0

' Change Id	: WI2.1_ENC_AT_6
' Date	: 11-27-2003
' Author	: Priority Support
' Comments	: 1. 'Find It' should find subsequent matches. 
'		  2. Implement the Arizona version of putting help text within Find It box. WIAZ030002

' Change ID 	: WI_2.3_WIKB040006
' Date		: 9/20/2004
' Author	: Priority Support
' Comments	: No match Found Message should be displayed in a Message Box

******************************************************************************
*/
	/*
		PROTOTYPING SECTION. PUT ALL PROTOTYPES HERE FOR EASY LOCATION
		The problem with this is that prototypes can only be used in this file, and on a page "including" this
		script. We can't use them on other files because they can't have include files and we don't want inter
		javascript file dependency. Redefining them is OK though, so could have these prototypes in more than one
		javascript file and if you included both of them, the last one's definitions would apply and there isn't a
		runtime error associated with the redefinition. The redefinitions just shouldn't be different change from 
		file to file, otherwise the changed version would break code depending on another version. dan kahle
	*/
	
	//adds a left trim function to strings. e.g.: stfips.ltrim()
	String.prototype.ltrim = function () 
	{
		return this.replace(/^\s+/,"");
	}

	//adds a right trim function to strings. e.g.: stfips.rtrim()
	String.prototype.rtrim = function () 
	{
		return this.replace(/\s+$/g,"");
	}

	//adds a trim function to strings. e.g.: stfips.trim()
	String.prototype.trim = function () 
	{
		return this.replace(/^\s+|\s+$/g,"");
	}

	//WI_2.3_WIKB040006 Starts Here
	//A older version of the that's deprecated, but needs to be supported as it's still being used.
	//Use selectItemInSelect or selectItemInSelectAndSet from now on.
	function selectItem(notUsedBooleanString, textboxName, selectName, formName) 
	{
		selectItemInSelect(formName, textboxName, selectName);
	}
	
	/*A short form of the selectItemInSelectAndSet function below
		examples:
		onclick="selectItemInSelect('formName', 'textboxName', 'selectName')"
		calling a function if it's found:
		onclick="if(selectItemInSelect('formName', 'textboxName', 'selectName')) test()"
	*/
	function selectItemInSelect(formName, textboxName, selectName)
	{
		//just call the generic function with setElement set to null
		return selectItemInSelectAndSet(formName, textboxName, selectName, null);
	}
	
	/* This function selects an item in a select which matches a textbox value. It also sets the value of an element
		if the setElementName is not null. It starts the search after the current selection if it exists or 
		at the beginning, therefore, repeated calls will get new values if more than one text match exists
		in the select. 
		The function returns true if it finds a match, which can be used by the caller to call the infamous "test" 
		function associated with this in the past. Here's a couple
		examples:
		onclick="selectItemInSelectAndSet('formName', 'textboxName', 'selectName', 'setElementName')"
		calling a function if it's found:
		onclick="if(selectItemInSelectAndSet('formName', 'textboxName', 'selectName', 'setElementName')) test()"
	*/
	function selectItemInSelectAndSet(formName, textboxName, selectName, setElementName)
	{
		var str, i, j, pos, start;
		var found = false;
		var textbox, select, setElement
		
		if(document.forms[formName] == null)
		{
			alert("Form: " + formName + " doesn't exist.")
			return false;
		}
		
		textbox = document.forms[formName].elements[textboxName];
		if(textbox == null)
		{
			alert("Textbox: " + textboxName + " doesn't exist.")
			return false;
		}
		
		select = document.forms[formName].elements[selectName];
		if(select == null)
		{
			alert("Select: " + selectName + " doesn't exist.")
			return false;
		}
		
		if(setElementName == null)
			setElement = null;
		else
		{
			setElement = document.forms[formName].elements[setElementName];
			if(setElement == null)
			{
				alert("setElement: " + setElementName + " doesn't exist.")
				return false;
			}
		}
		
		str = textbox.value.toUpperCase();

		if(str.trim().length == 0)
		{
			window.alert("Please enter search text.");
			textbox.focus();
			return false;
		}
		
		//see if a match exists in list
		for(i=0; i<= select.length-1; i++) 
		{
			pos = select[i].text.toUpperCase().indexOf(str);
			if(pos >= 0)
			{
				found = true;
				break;
			}
		}

		if(!found)
		{
			window.alert("No match found. Please try another keyword.");
			//can't select here because of the onfocus/onblur changes in the textboxes, so we'll just focus
			textbox.focus();
			return false;
		}

		//we'll start looking for it after the current selection if it exists, otherwise at the beginning
		if (select.selectedIndex <= 0 || select.selectedIndex == select.length-1)
			start = 0;
		else
			start = select.selectedIndex + 1;

		for(i=start; i<select.length; i++) 
		{
			pos = select[i].text.toUpperCase().indexOf(str);

			if(pos >= 0) 
			{
				//well deselect any selected values. This isn't needed for the single item selects, but is for multiple
				for(j=0; j<select.length; j++)
					select[j].selected = false;
				select[i].selected = true;
				if(setElement != null)
					setElement.value = select.value;
				return true;
			}

			//if we know we have one and haven't found it by the end of the loop, we'll start over
			if(i == select.length-1 && found == true)
				i = -1;
		}

	}
	//WI_2.3_WIKB040006 Ends Here
	// function is redundant as of now.but might be used later.do not delete it.
	function test(){
		var index1;
		var str,len,selectText,pos;		
		index1 = document.frmDataAnalysis.occupn.selectedIndex;			
		
		if (index1 != -1) {
			//WIGe030002
			//document.frmDataAnalysis.careerID.value = document.frmDataAnalysis.occ1[index1].value;
			
				/************************ WI2.1_ENC_AT_6 ***********************/
			if (document.frmDataAnalysis.findIt1.value == 'Enter an Occupation')
				document.frmDataAnalysis.findIt1.value = '';
			/*************************************************************/
		
			
		}
	}
				
				
	//this is used when the select/clear all check boxes are clicked in data Analysis pages.
	function selectAll_click(checkBoxName,selectBoxName,frm, functionCall) {
	
	/*
		functionCall:- 
		Y - call refillcombos function
		P - call refillcombos function with Paremeter
		N = do not call refillcombos function
	
	*/
		if (checkBoxName.checked == true ) {
			for(var i=0; i< selectBoxName.options.length;i++) { 				
				selectBoxName.options[i].selected = true;
				if (functionCall == 'Y') {
					Refillcombos();
				}
				else {
					if (functionCall == 'P') {
						Refillcombos(2);
					}
					else {
						// do not call any functions
					
					}
				
				}	
			}
			
			//in refillcombos function u would have unselected the select box,that is why u need to reset the value here
			checkBoxName.checked = true ;		
		}
		else {
			
			selectBoxName.selectedIndex = -1
		}		
		
	}
	
	
	//this is used when the select/clear all check boxes are clicked in data Analysis pages.
	function selectMax_click(checkBoxName,selectBoxName,frm, functionCall,maxLength) 
	{
	
	/*
		functionCall:- 
		Y - call refillcombos function
		P - call refillcombos function with Paremeter
		N = do not call refillcombos function
	
	*/
	    var length=selectBoxName.options.length;
	    if(length<maxLength)
	     maxLength=length;
	     
	    // find first selected checkbox - Jason added this
	    var intSelectedIndex = selectBoxName.selectedIndex;
	    // if no selected index then set selected as the first option - Jason added this
	    if(intSelectedIndex < 0) 
	    { 
			intSelectedIndex = 0
		}
		else
		{
			maxLength = (maxLength + intSelectedIndex <= length) ? maxLength + intSelectedIndex: length;
		}
	    
	    for(var i=0; i< selectBoxName.options.length;i++) 
	    { 	    	 
		 selectBoxName.options[i].selected = false;
		}
	    //alert(intSelectedIndex);     
	    if (checkBoxName.checked == true ) {
			for(var i=intSelectedIndex; i< maxLength;i++) { 
				selectBoxName.options[i].selected = true;
				if (functionCall == 'Y') {
					Refillcombos();
				}
				else {
					if (functionCall == 'P') {
						Refillcombos(2);
					}
					else {
						// do not call any functions
					
					}
				
				}	
			}
			
			
			//in refillcombos function u would have unselected the select box,that is why u need to reset the value here
			checkBoxName.checked = true ;		
		}
		else {
			
			selectBoxName.selectedIndex = -1
		}		
		
	}
	
	
	
	function setCheckBoxStatus(checkBoxName,selectBoxName,frm) {
		var blnSelectAll;
		
		blnSelectAll = false;
		for (var i = 0 ;i < selectBoxName.options.length;i++) {
			if ( selectBoxName.options[i].selected == true ) {
				blnSelectAll = true;
			
			}
			else {
				blnSelectAll = false;
				break;
			}
		}
			
		if (blnSelectAll == true ) 
			checkBoxName.checked = true;
		else
			checkBoxName.checked = false;	
	


	}	
	
	
	function setSelectedValues(hiddenField,listBox)
	{
	 var arrAreaIndex;
	 if(hiddenField.value!="") 
	 {
		hidSelected=hiddenField.value;
		arrAreaIndex=hidSelected.split(",");
		for(i=0;i<=arrAreaIndex.length;i++) 
		{
		//WIGe030002
		if(listBox[i] !=null) 
			//WIGe030002
			listBox[i].selected=false;  		 
	    }
	
		for(i=0;i<=arrAreaIndex.length;i++) 
		{
		//WIGe030002
		if(listBox[arrAreaIndex[i]] !=null) 
			//WIGe030002
			listBox[arrAreaIndex[i]].selected=true;  		 
	    }
	  }  
	
	}
	
	function setSelectedValuetoHidden(hiddenField,listBox)
	{
	 var areaIndex="";
	 for(i=0;i<=listBox.options.length;i++) 
	 {
	  //WIGe030002
	  if(listBox[i]!=null)
	   {
	    //WIGe030002	
	    if(listBox[i].selected)
	    { 
	     areaIndex+=i+",";
	     } 
	    } 
	  }
	 hiddenField.value=areaIndex
	}
	
	
	function validateListBox(listBox,size)
    {
     var itemCount=0;
	 for(i=0;i<=listBox.options.length;i++) 
	 {
	   //WIGe030002 	
	   if(listBox[i]!=null)
	   {
	    //WIGe030002
	    if(listBox[i].selected)
	     itemCount+=1;
	   } 
	  }
	  if (itemCount> size)  
	   return true;
	  else
	   return false; 
	}
	
	
	
 function Browser() {

  var ua, s, i;

  this.isIE    = false;  // Internet Explorer
  this.isNS    = false;  // Netscape
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}
