/*

	Search textfield for multi-select dropdown
	clientside javascript

*/

browserName = navigator.appName ;
browserVersion = navigator.appVersion ;
browserUserAgent = navigator.userAgent ;

brow = "X" ;    

// some unknown browser

if ( (browserName.indexOf("Microsoft") != -1) || (browserName.indexOf("microsoft") != -1) )       
	brow = "I" ;
if ( (browserName.indexOf("Netscape") != -1) || (browserName.indexOf("netscape") != -1) )        
	brow = "N" ;

VERSION = 0;
ver = VERSION ;  

// assume unknown version as 0

if (browserVersion.indexOf("3.") != -1 && ver == VERSION)        
	ver = 3 ;
if (browserVersion.indexOf("4.") != -1 && ver == VERSION)        
	ver = 4 ;
if (browserVersion.indexOf("5.") != -1 && ver == VERSION)        
	ver = 5 ;
if (browserVersion.indexOf("6.") != -1 && ver == VERSION)        
	ver = 6 ;
if (browserUserAgent.indexOf("AOL") != -1 || browserUserAgent.indexOf("aol") != -1 || browserUserAgent.indexOf("Aol") != -1) 
{
    brow = "A" ;
}

if (browserUserAgent.indexOf("OPERA") != -1 || browserUserAgent.indexOf("opera") != -1 || browserUserAgent.indexOf("Opera") != -1)        
	brow = "O" ;

useNewPageFlag = 0 ;                                                                           

if ( (brow == "N" && ver >= 5) || (brow == "I" && ver >= 4) || brow == "A")        
	useNewPageFlag = 1 ;                                                                   


function binSearch(toFind, arr, low, high)
{  
	mid = Math.floor( (low+high)/2 );        
	
	if ( low == high )        
	{                
		if ( match(toFind, arr[mid].text) == 0 )                        
			return mid ;               
		else                        
			return -1 ;        
	}        
	else                 
		if ( low > high )                        
			return -1 ;                
	
	if ( match(toFind, arr[mid].text) == 0 )                
		return mid ;        
	else        
	{                
		if ( match(toFind, arr[mid].text) < 0 )                        
			return binSearch(toFind, arr, low, mid-1) ;                
		else                        
			return binSearch(toFind, arr, mid+1, high) ;        
	}
}
 
function match(lastname, arr)
{        
	len = arr.length ;        
 
	for ( i = 1 ; i <= len ; i++ ) 
	{                
		if ( lastname.toLowerCase() == (arr.substr(0, i)).toLowerCase() ) { 
			return 0;                
		}        
    }        
 
	if ( lastname.toLowerCase() < arr.toLowerCase() )                
		return -1 ;      
	
	return 1 ;
}
  
function selectFirstMatch(lastname, index, formvar)
{  
	arr = formvar;           
  
	tmpIndex = index ;        
  
	while (true) 
	{                
		if ( index == 0 ) return index ;                
		if ( match(lastname, arr[--index].text) != 0)                       
			break ;        
	}        
   
	return index+1 ;
} 
   
function hOnKeyUp(formvar, boxvar)
{  
    selectlength = formvar.length;
	selbox = formvar ;        
	arr = selbox.options ;        
	lastname = boxvar.value ;                
	
	if ( lastname == "" ) {                
	selbox.options[0].selected = true ;                
	selbox.selectedIndex = -1 ;        
	}        

	index = binSearch(lastname, arr, 0, arr.length-1) ;                

	if ( index == -1 ) 
	{      
		selbox.selectedIndex = -1 ;        
	}        
	else        
	{                          
		newIndex = selectFirstMatch(lastname, index, formvar) ;   
	    for ( i=0; i < selectlength; i++)
	    {
			arr[i].selected = false ;  
	    }
		arr[newIndex].selected = true ;        
	}

}

function hOnFocus(formvar, boxvar)
{        
	boxvar.value = "";
	return true;
}                                                                                              



function hOnBlur(formvar, boxvar)
{        

	if ( useNewPageFlag == 0 ) return ;        
	textBox1FocusFlag = 0 ;        
	selbox = formvar ;        
	arr = selbox.options ;        
	index = selbox.selectedIndex ;        

	// Let's not do this...
	//if ( index >= 0 )boxvar.value = arr[index].text ;
}



function hOnChange(formvar, boxvar)
{  
	if(formvar != null)
	{
		var iSelItem = formvar.selectedIndex;
		
		var mySearch = 'document.forms[0].' + formvar.id + '.options[' + iSelItem + '].text';
		
		if(iSelItem != -1){
		
			var myVal = eval(mySearch);
			boxvar.value = myVal;
			
			
			
		}
	}
		   
	hOnBlur(formvar, boxvar);
}

function setCheckVal(lBox, chkBox)
{
	if(lBox.selectedIndex == -1)
			chkBox.checked = true;
	else
		chkBox.checked = false;
}
    
function confirmSelection(selID, txtID, chkItemStatus)
{
	var selectionName;
	selectionName = document.getElementById(selID);
	

	
	if(selectionName != null)
	{
		var iSelItem = selectionName.selectedIndex;
		
		var mySearch = 'document.forms[0].' + selID + '.options[' + iSelItem + '].text';
		if(iSelItem != -1){
		
			var myText = eval(mySearch); //get text of selected item
			mySearch = 'document.forms[0].' + selID + '.options[' + iSelItem + '].value';
		
			var myVal = eval(mySearch); //get value of selected item
			
			var arrVals = myVal.split("|");
			//email is the second item in arrVals
			
			if(!chkItemStatus)
				return confirm('Are you sure you want to edit the record belonging to "' + myText + '" (' + arrVals[1] +  ')?');
			else 
				return confirm('Are you sure you want to create a new record for "' + myText + '"?');
		}
		else
		{
			var txtBox = 'document.forms[0].' + txtID + '.value';
		
			var newName = eval(txtBox);
			return confirm('Are you sure you want to create a new record for "' + newName + '"?');
		}
		
		
	}
}               


//add an option to the selectObject with the passed in text/value 
function addOption(selectObject, optionText, optionValue, bOrdered) {
	if(!bOrdered)
	{
		//check if the value ends in a ";" then a number, if it does, strip that part off
		var iLastSemi = optionValue.lastIndexOf(';');
		if(iLastSemi != -1)
		{ 
			//found the semi
			//alert('before: ' + optionValue);		
			optionValue = optionValue.substring(0,iLastSemi);
			//alert('after: ' + optionValue);			
		}
		var optionObject = new Option(optionText,optionValue)
		var optionRank = selectObject.options.length
		selectObject.options[optionRank]=optionObject
	}
	else
	{
		var optionRank = selectObject.options.length
		var optionObject = new Option(optionText,optionValue + ";" + optionRank)
		selectObject.options[optionRank]=optionObject
	}
	
}

//delete object
function deleteOption(selectObject,optionRank) {
    if (selectObject.options.length!=0) { selectObject.options[optionRank]=null }
}

//move the selected item from the selectbox whose id is "idFrom" to the one whose id is "idTo"
function moveFrom(idFrom, idTo, bOrdered)
{
	var objSel1 = document.getElementById(idFrom); //source sel box obj
	var objSel2 = document.getElementById(idTo); //destination sel box obj
	
	var arr;
	var value;
	arr = objSel1.options;        
	
	var selectedIndex = objSel1.selectedIndex;
	
	//alert(selectedIndex + ' ' + arr[selectedIndex].value + ' ' + arr[selectedIndex].text);
	addOption(objSel2, arr[selectedIndex].text, arr[selectedIndex].value, bOrdered);
	deleteOption(objSel1, selectedIndex);
	
}          

function moveUp(listFieldID) {
	var listField = document.getElementById(listFieldID); //source sel box obj
	
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               alert("The first entry in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected-1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected-1].value;
               var moveValue2 = listField[selected].value;
               
                   
               var arrayVal1 = moveValue1.split(";");
               var arrayVal2 = moveValue2.split(";");
               
               moveValue1 = arrayVal1[0] + ";" + arrayVal2[1]; //switch order vals
               moveValue2 = arrayVal2[0] + ";" + arrayVal1[1]; //switch order vals
               
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected-1].text = moveText2;
               listField[selected-1].value = moveValue2;
               listField.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}       

function moveDown(listFieldID) {
	var listField = document.getElementById(listFieldID); //source sel box obj
	
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == listField.length-1 ) {
               alert("The last entry in the list cannot be moved down.");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected+1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected+1].value;
               var moveValue2 = listField[selected].value;
               
               var arrayVal1 = moveValue1.split(";");
               var arrayVal2 = moveValue2.split(";");
               
               moveValue1 = arrayVal1[0] + ";" + arrayVal2[1]; //switch order vals
               moveValue2 = arrayVal2[0] + ";" + arrayVal1[1]; //switch order vals
               
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected+1].text = moveText2;
               listField[selected+1].value = moveValue2;
               listField.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

//given the id of a select object it will alert out all the text and values for every option.
function checkVals(listFieldID)
{
	var listField = document.getElementById(listFieldID); //source sel box obj
	var arr = listField.options;
	var strAlert = "";
	
	for(var i = 0; i < arr.length; i++)
	{
		strAlert += "text: " + arr[i].text + " val: " + arr[i].value + "\n";
	}
	
	alert(strAlert);
}
    

function setHiddenChecked(hiddenFieldID, val)
{
	var objHField = document.getElementById(hiddenFieldID);
	objHField.value = val;
}                                                
