//-----------------------------------------------------------------------------
// sortTable(id, col, rev)
//
//  id  - ID of the TABLE, TBODY, THEAD or TFOOT element to be sorted.
//  col - Index of the column to sort, 0 = first column, 1 = second column,
//        etc.
//  rev - If true, the column is sorted in reverse (descending) order
//        initially.
//
// Note: the team name column (index 1) is used as a secondary sort column and
// always sorted in ascending order.
//-----------------------------------------------------------------------------

function sortTable(id, col, rev) {

  // Get the table or table section to sort.
  var tblEl = document.getElementById(id);

  // The first time this function is called for a given table, set up an
  // array of reverse sort flags.
  if (tblEl.reverseSort == null) {
    tblEl.reverseSort = new Array();
    // Also, assume the team name column is initially sorted.
    tblEl.lastColumn = 1;
  }

  // If this column has not been sorted before, set the initial sort direction.
  if (tblEl.reverseSort[col] == null)
    tblEl.reverseSort[col] = rev;

  // If this column was the last one sorted, reverse its sort direction.
  if (col == tblEl.lastColumn)
    tblEl.reverseSort[col] = !tblEl.reverseSort[col];

  // Remember this column as the last one sorted.
  tblEl.lastColumn = col;

  // Set the table display style to "none" - necessary for Netscape 6 
  // browsers.
  var oldDsply = tblEl.style.display;
  tblEl.style.display = "none";

  // Sort the rows based on the content of the specified column using a
  // selection sort.

  var tmpEl;
  var i, j;
  var minVal, minIdx;
  var testVal;
  var cmp;

  for (i = 0; i < tblEl.rows.length - 1; i++) {

    // Assume the current row has the minimum value.
    minIdx = i;
    minVal = getTextValue(tblEl.rows[i].cells[col]);

    // Search the rows that follow the current one for a smaller value.
    for (j = i + 1; j < tblEl.rows.length; j++) {
	
      testVal = getTextValue(tblEl.rows[j].cells[col]);
      cmp = compareValues(minVal, testVal);
      // Negate the comparison result if the reverse sort flag is set.
      if (tblEl.reverseSort[col])
        cmp = -cmp;
      // Sort by the second column (team name) if those values are equal.
      if (cmp == 0 && col != 1)
        cmp = compareValues(getTextValue(tblEl.rows[minIdx].cells[1]),
                            getTextValue(tblEl.rows[j].cells[1]));
      // If this row has a smaller value than the current minimum, remember its
      // position and update the current minimum value.
      if (cmp > 0) {
        minIdx = j;
        minVal = testVal;
      }
    }

    // By now, we have the row with the smallest value. Remove it from the
    // table and insert it before the current row.
    if (minIdx > i) {
      tmpEl = tblEl.removeChild(tblEl.rows[minIdx]);
      tblEl.insertBefore(tmpEl, tblEl.rows[i]);
    }
  }

  // Make it look pretty.
  makePretty(tblEl, col);

  // Set team rankings.
  //setRanks(tblEl, col, rev);

  // Restore the table's display style.
  tblEl.style.display = oldDsply;

  return false;
}

//-----------------------------------------------------------------------------
// Functions to get and compare values during a sort.
//-----------------------------------------------------------------------------

// This code is necessary for browsers that don't reflect the DOM constants
// (like IE).
if (document.ELEMENT_NODE == null) {
  document.ELEMENT_NODE = 1;
  document.TEXT_NODE = 3;
}

function getTextValue(el) {

  var i;
  var s;

  // Find and concatenate the values of all text nodes contained within the
  // element.
  s = "";
  for (i = 0; i < el.childNodes.length; i++)
    if (el.childNodes[i].nodeType == document.TEXT_NODE)
      s += el.childNodes[i].nodeValue;
    else if (el.childNodes[i].nodeType == document.ELEMENT_NODE &&
             el.childNodes[i].tagName == "BR")
      s += " ";
    else
      // Use recursion to get text within sub-elements.
      s += getTextValue(el.childNodes[i]);

  return normalizeString(s);
}

function compareValues(v1, v2) {
	var txtmonths=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	var nummonths=new Array("01","02","03","04","05","06","07","08","09","10","11","12");
	daytxt1 = v1.substring(0, 2);
	daytxt2 = v2.substring(0, 2);
	montxt1 = v1.substring(3, 6);
	montxt2 = v2.substring(3, 6);
	yrtxt1 = v1.substring(7, 12);
	yrtxt2 = v2.substring(7, 12);

	for (var i=0; i < 12; i++) {
		if(montxt1 == txtmonths[i]){
			v1= yrtxt1 + nummonths[i] + daytxt1;
		}
	}	
	
	for (var j=0; j < 12; j++) {
		if(montxt2 == txtmonths[j]){
			v2= yrtxt2 + nummonths[j] + daytxt2;
		}
	}

  var f1, f2;

  // If the values are numeric, convert them to floats.

  f1 = parseFloat(v1);
  f2 = parseFloat(v2);

  if (!isNaN(f1) && !isNaN(f2)) {
    v1 = f1;
    v2 = f2;
  }

  // Compare the two values.
  if (v1 == v2)
    return 0;
  if (v1 > v2)
    return 1
  return -1;
}


// Regular expressions for normalizing white space.
var whtSpEnds = new RegExp("^\\s*|\\s*$", "g");
var whtSpMult = new RegExp("\\s\\s+", "g");

function normalizeString(s) {

  s = s.replace(whtSpMult, " ");  // Collapse any multiple whites space.
  s = s.replace(whtSpEnds, "");   // Remove leading or trailing white space.

  return s;
}

//-----------------------------------------------------------------------------
// Functions to update the table appearance after a sort.
//-----------------------------------------------------------------------------

// Style class names.
var rowClsNm = "browseListOffWhite";
var colClsNm = "sortedColumn";
var colClsHdNm = "sortedheadColumn";

// Regular expressions for setting class names.
var rowTest = new RegExp(rowClsNm, "gi");
var colTest = new RegExp(colClsNm, "gi");
var colHTest = new RegExp(colClsHdNm, "gi");

function makePretty(tblEl, col) {

  var i, j;
  var rowEl, cellEl;

  // Set style classes on each row to alternate their appearance.
  for (i = 0; i < tblEl.rows.length; i++) {
   rowEl = tblEl.rows[i];
   rowEl.className = rowEl.className.replace(rowTest, "");
    if (i % 2 != 0)
      rowEl.className += " " + rowClsNm;
    rowEl.className = normalizeString(rowEl.className);
    // Set style classes on each column (other than the name column) to
    // highlight the one that was sorted.

    for (j = 0; j < tblEl.rows[i].cells.length; j++) {
      cellEl = rowEl.cells[j];
      cellEl.className = cellEl.className.replace(colTest, "");
//alert(cellEl.innerHTML);
      if (j == col)
        cellEl.className += " " + colClsNm;
      cellEl.className = normalizeString(cellEl.className);
    }
  }

  // Find the table header and highlight the column that was sorted.
  var el = tblEl.parentNode.tHead;
  rowEl = el.rows[el.rows.length - 1];
  // Set style classes for each column as above.
  for (i = 0; i < rowEl.cells.length; i++) {
    cellEl = rowEl.cells[i];
    cellEl.className = cellEl.className.replace(colHTest, "");
    // Highlight the header of the sorted column.
    if (i == col)
      cellEl.className += " " + colClsHdNm;
      cellEl.className = normalizeString(cellEl.className);
  }
}

function setRanks(tblEl, col, rev) {

  // Determine whether to start at the top row of the table and go down or
  // at the bottom row and work up. This is based on the current sort
  // direction of the column and its reversed flag.

  var i    = 0;
  var incr = 1;
  if (tblEl.reverseSort[col])
    rev = !rev;
  if (rev) {
    incr = -1;
    i = tblEl.rows.length - 1;
  }

  // Now go through each row in that direction and assign it a rank by
  // counting 1, 2, 3...

  var count   = 1;
  var rank    = count;
  var curVal;
  var lastVal = null;

  // Note that this loop is skipped if the table was sorted on the name
  // column.
  while (col > 1 && i >= 0 && i < tblEl.rows.length) {

    // Get the value of the sort column in this row.
    curVal = getTextValue(tblEl.rows[i].cells[col]);

    // On rows after the first, compare the sort value of this row to the
    // previous one. If they differ, update the rank to match the current row
    // count. (If they are the same, this row will get the same rank as the
    // previous one.)
    if (lastVal != null && compareValues(curVal, lastVal) != 0)
        rank = count;
    // Set the rank for this row.
    tblEl.rows[i].rank = rank;

    // Save the sort value of the current row for the next time around and bump
    // the row counter and index.
    lastVal = curVal;
    count++;
    i += incr;
  }

  // Now go through each row (from top to bottom) and display its rank. Note
  // that when two or more rows are tied, the rank is shown on the first of
  // those rows only.

  var rowEl, cellEl;
  var lastRank = 0;

  // Go through the rows from top to bottom.
  for (i = 0; i < tblEl.rows.length; i++) {
    rowEl = tblEl.rows[i];
    cellEl = rowEl.cells[0];
    // Delete anything currently in the rank column.
    while (cellEl.lastChild != null)
      cellEl.removeChild(cellEl.lastChild);
    // If this row's rank is different from the previous one, Insert a new text
    // node with that rank.
    if (col > 1 && rowEl.rank != lastRank) {
      cellEl.appendChild(document.createTextNode(rowEl.rank));
      lastRank = rowEl.rank;
    }
  }
}

			function movetomouse(targetId){
	      

		    target = document.getElementById(targetId);
			var x = tempX;
			target.style.left = x;
		target.style.top = tempY + 'px';
		if(eval(target.style.width.replace(new RegExp("px"), ""))+tempX < screen.width) target.style.left = tempX + 'px';
		else target.style.left = tempX - eval(target.style.width.replace(new RegExp("px"), ""))  + 'px';			
			
			
			}

			function toggle(targetId){
			var both = getMouseXY();
			var xyarray = both.split(",");
			var x = xyarray[0];
			var y = xyarray[1];
			
			  if (document.getElementById){
			        target = document.getElementById(targetId);
			           if (target.style.display == "none") target.style.display = "";
					   else target.style.display = "none";
			     }
			target.style.left = x;
			target.style.top = y;
			}
// -------------------------------------------------------------------
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  This function moves options between select boxes. Works best with
//  multi-select boxes to create the common Windows control effect.
//  Passes all selected values from the first object to the second
//  object and re-sorts each box.
//  If a third argument of 'false' is passed, then the lists are not
//  sorted after the move.
//  If a fourth string argument is passed, this will function as a
//  Regular Expression to match against the TEXT or the options. If 
//  the text of an option matches the pattern, it will NOT be moved.
//  It will be treated as an unmoveable option.
//  You can also put this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the 
//  onDblClick() event handler).
// -------------------------------------------------------------------
function moveSelectedOptions(from,to,strform) {
	// Unselect matching options, if required
	if (arguments.length>3) {
		var regex = arguments[3];
		if (regex != "") {
			unSelectMatchingOptions(from,regex);
			}
		}
	// Move them over
	for (var i=0; i<from.options.length; i++) {
		var o = from.options[i];
		if (o.selected) {
			//var getasc_desc = document.forms[0].asc_desc.value;
			to.options[to.options.length] = new Option( o.text, o.value, false, false);
			}
		}
	// Delete them from original
	for (var i=(from.options.length-1); i>=0; i--) {
		var o = from.options[i];
		if (o.selected) {
			from.options[i] = null;
			}
		}
	if ((arguments.length<3) || (arguments[2]==true)) {
		sortSelect(from);
		sortSelect(to);
		}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
	}
	
	
// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a 
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before 
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions(obj) {
	for (var i=0; i<obj.options.length; i++) {
		obj.options[i].selected = true;
		}
	}
		
// -------------------------------------------------------------------
// sortSelect(select_object)
//   Pass this function a SELECT object and the options will be sorted
//   by their text (display) values
// -------------------------------------------------------------------
function sortSelect(obj) {
	var o = new Array();
	if (obj.options==null) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort( 
		function(a,b) { 
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			} 
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}
	
	function replaceSubstring(inputString, fromString, toString) {
   // 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
var key = 0;

function clearFilterbox()
{
  with (document.defaultFilter) {

    for (var i=0; i < elements.length; i++) { 
        if (elements[i].type == 'text'){
           elements[i].value = "";
		}
        if (elements[i].type == 'select-multiple'){
			for(var j=0; j< elements[i].options.length; j++){
				elements[i].options[j].selected=false;
			}
			elements[i].selectedIndex = 0  ;
		}		   
    }
  }
}


function moveUpList(listField, noalert) {
if(key==13) return;
	var itemselected = -1;
	for (var i = 0; i < listField.options.length; i++) {
		if (listField.options[i].selected) 
			itemselected = i;
	}
	
	if (itemselected == 0) {
		if (noalert != 1)
		alert("The first entry in the list cannot be moved up.");
		return false;
	}
	else if (listField.value.length == 0){
		if (noalert != 1)
		alert("There are no values which can be moved1!");
		return false;
	}	
	else {
		var holdtexttop = listField.options[itemselected].text;
		var holdvaluetop = listField.options[itemselected].value;
		var holdtextbottom = listField.options[itemselected-1].text;
		var holdvaluebottom = listField.options[itemselected-1].value;
		listField.options[itemselected].text = holdtextbottom;		
		listField.options[itemselected].value = holdvaluebottom;		
		listField.options[itemselected-1].text = holdtexttop;		
		listField.options[itemselected-1].value = holdvaluetop;		
		
		listField.options[itemselected-1].selected = true;
		listField.options[itemselected].selected = false;
	}
	return true;
}


function moveTop(listField) {

    if(key==13) return;

	var itemselected = -1;
	
	for (var i = 0; i < listField.options.length; i++) {
		if (listField.options[i].selected) 
			itemselected = i;
	}
	
	if (itemselected == 0) {
		alert("The first entry in the list cannot be moved up.");
	}
	else if (listField.value.length == 0) {
		alert("There are no values which can be moved2!");
	}
	else {
		var finishmove = false;
		var holder = false;

		while (!finishmove) {
			holder = moveUpList(listField, 1);
			if (holder == false) 
				finishmove = true;
			else finishmove = false;
		}
		return;
	}
}

function moveDownList(listField, noalert) {
if(key==13) return;
	var itemselected = -1;
	for (var i = 0; i < listField.options.length; i++) {
		if (listField.options[i].selected) 
			itemselected = i;
	}
	
	if (itemselected == listField.options.length - 1) {
		if (noalert != 1)
		alert("The last entry in the list cannot be moved down.");
		return false;
	}
	else if (listField.value.length == 0){
		if (noalert != 1)
		alert("There are no values which can be moved3!");
		return false;
	}	
	else {
		var holdtexttop = listField.options[itemselected].text;
		var holdvaluetop = listField.options[itemselected].value;
		var holdtextbottom = listField.options[itemselected+1].text;
		var holdvaluebottom = listField.options[itemselected+1].value;
		listField.options[itemselected].text = holdtextbottom;		
		listField.options[itemselected].value = holdvaluebottom;		
		listField.options[itemselected+1].text = holdtexttop;		
		listField.options[itemselected+1].value = holdvaluetop;		
		
		listField.options[itemselected+1].selected = true;
		listField.options[itemselected].selected = false;
	}
	return true;
}

function moveBtm(listField) {
if(key==13) return;
	var itemselected = -1;
	
	for (var i = 0; i < listField.options.length; i++) {
		if (listField.options[i].selected) 
			itemselected = i;
	}
	
	if (itemselected == listField.options.length - 1) {
		alert("The last entry in the list cannot be moved down.");
	}
	else if (listField.value.length == 0){
		alert("There are no values which can be moved4!");
	}
	else {	
		var finishmove = false;
		var holder = false;
		
		while (!finishmove) {
			holder = moveDownList(listField, 1);
			if (holder == false) 
				finishmove = true;
			else finishmove = false;
		}
		return;
	}
}

function clearFilterbox()
{
  with (document.filterform) {

    for (var i=0; i < elements.length; i++) { 
        if (elements[i].type == 'text'){
           elements[i].value = "";
		}
        if (elements[i].type == 'select-multiple' || elements[i].type == 'select-one'){
			elements[i].selectedIndex = -1;
		}		 
		if (elements[i].type == 'checkbox'){
           elements[i].checked = false;
		}  
    }
  }
}
function checkit(num)
{
for (var i=0; i < document.filterform.elements.length; i++) { 
if (document.filterform.elements[i].type == 'checkbox')
 document.filterform.elements[i].checked=num;
}
}

function freezeform() {
var field = document.filterform.savedfile.options;
var allelem = document.filterform.elements;
	for (var i = 0; i < field.length; i++) {
		if (field.options[i].selected) {
			if(field.options[i].value == '') {
				for (var i = 0; i < allelem.length ; i++) 
				if(allelem[i] != document.filterform.savedfile && allelem[i].type != 'button')	allelem[i].disabled = false;				
			}
			else {
				for (var i = 0; i < allelem.length ; i++) 
				if(allelem[i] != document.filterform.savedfile && allelem[i].type != 'button') allelem[i].disabled = true;			
			}
			}
	}


}
			function saveit()
			{
			  var myWidth = 0, myHeight = 0;
			  if( typeof( window.innerWidth ) == 'number' ) {
			    //Non-IE
			    myWidth = window.innerWidth;
			    myHeight = window.innerHeight;
			  } else if( document.documentElement &&
			      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			    //IE 6+ in 'standards compliant mode'
			    myWidth = document.documentElement.clientWidth;
			    myHeight = document.documentElement.clientHeight;
			  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			    //IE 4 compatible
			    myWidth = document.body.clientWidth;
			    myHeight = document.body.clientHeight;
			  }
				var horiz = parseInt((myWidth-450)/2);
				var vert = parseInt((myHeight-150)/2)-50; //Popup intentionally popped up slightly above center
				document.getElementById('savedialogue').style.left = horiz;
				document.getElementById('savedialogue').style.top = vert;		
				document.getElementById('savedialogue').style.display = "";			
				return false;			
		//	document.filterform.save.value = prompt('File Name:','');
		//	document.filterform.submit();
			}
			function savecancel(){
				document.filterform.jedi_save = '';
				document.filterform.jedi_author = '';
				document.filterform.jedi_notes = '';
				toggle('savedialogue');
				return false;			
			}
			function pleasewait(){
var allelem = document.filterform.elements;
for (var i = 0; i < allelem.length ; i++) allelem[i].style.display = 'none';
//alert(allelem[i].name);
//allelem[i].style.display = "none";
			  var myWidth = 0, myHeight = 0;
			  if( typeof( window.innerWidth ) == 'number' ) {
			    //Non-IE
			    myWidth = window.innerWidth;
			    myHeight = window.innerHeight;
			  } else if( document.documentElement &&
			      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			    //IE 6+ in 'standards compliant mode'
			    myWidth = document.documentElement.clientWidth;
			    myHeight = document.documentElement.clientHeight;
			  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			    //IE 4 compatible
			    myWidth = document.body.clientWidth;
			    myHeight = document.body.clientHeight;
			  }
				var horiz = parseInt((myWidth-450)/2);
				var vert = parseInt((myHeight-150)/2)-50; //Popup intentionally popped up slightly above center
				document.getElementById('pleasewait').style.left = horiz;
				document.getElementById('pleasewait').style.top = vert;		
				document.getElementById('pleasewait').style.display = "";			
				return false;
			}
			
			var IE = document.all?true:false;
			if (!IE) document.captureEvents(Event.MOUSEMOVE);
			document.onmousemove = getMouseXY;
			var tempX = 0;
			var tempY = 0;
			var opendiv = "";
			
			function getMouseXY(e) {
			if (IE) { // grab the x-y pos.s if browser is IE
			tempX = event.clientX + document.body.scrollLeft;
			tempY = event.clientY + document.body.scrollTop;
			}
			else {  // grab the x-y pos.s if browser is NS
			tempX = e.pageX;
			tempY = e.pageY;
			}  
			if (tempX < 0){tempX = 0;}
			if (tempY < 0){tempY = 0;}  
			//document.filterform.xy.value = tempX +',' + tempY;
			return true;
			
			//  End -->
			}
	/*==================================================*
 $Id: util.js,v 1.4 2006/12/21 15:54:11 jbradley Exp $
 Copyright 2003 Patrick Fitzgerald
 http://www.barelyfitz.com/webdesign/articles/filterlist/

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *==================================================*/

function filterlist(selectobj) {

  // VARIABLES

  // HTML SELECT object
  this.selectobj = selectobj;

  // Flags for regexp matching.
  // "i" = ignore case; "" = do not ignore case
  this.flags = "i";

  // Make a copy of the options array
  this.optionscopy = new Array();
  for (var i=0; i < selectobj.options.length; i++) {
    this.optionscopy[i] = new Option();
    this.optionscopy[i].text = selectobj.options[i].text;
    this.optionscopy[i].value = selectobj.options[i].value;
  }

  //==================================================
  // METHODS
  //==================================================

  //--------------------------------------------------
  this.reset = function() {
  // This method resets the select list to the original state.
  // It also unselects all of the options.

    this.set("");
  }

  //--------------------------------------------------
  this.set = function(pattern) {
  // This method removes all of the options from the select list,
  // then adds only the options that match the pattern regexp.
  // It also unselects all of the options.
  // In case of a regexp error, returns false

    var loop=0, index=0, regexp, e;

    // Clear the select list so nothing is displayed
    this.selectobj.options.length = 0;

    // Set up the regular expression
    try {
      regexp = new RegExp(pattern, this.flags);
    } catch(e) {
      return;
    }

    // Loop through the entire select list
    for (loop=0; loop < this.optionscopy.length; loop++) {

      // Check if we have a match
      if (regexp.test(this.optionscopy[loop].text)) {

        // We have a match, so add this option to the select list
        this.selectobj.options.length = index + 1;
        this.selectobj.options[index].text = this.optionscopy[loop].text;
        this.selectobj.options[index].value = this.optionscopy[loop].value;
        this.selectobj.options[index].selected = false;

        // Increment the index
        index++;
      }
    }
  }

  //--------------------------------------------------
  this.set_ignore_case = function(value) {
  // This method sets the regexp flags.
  // If value is true, sets the flags to "i".
  // If value is false, sets the flags to "".

    if (value) {
      this.flags = "i";
    } else {
      this.flags = "";
    }
  }

}
		


function addContact(myValue, myText, mySelect, myhidden)
{
	var sep;
	var addedSuccessfully = true;
	//var mySelect = window.opener.document.#attributes.formname#.#attributes.displayListName#show;
	//var myhidden = window.opener.document.#attributes.formname#.#attributes.displayListName#;	

	// insert it only if its value is not found in the options
	if (getOptionIndexInSelect(mySelect, myValue) == -1)
		{
			sep = ',';
			if(myhidden.value ==0) sep = '';
			myhidden.value += sep + myValue;
			
			insertNewOption(mySelect, myValue, myText);
		}
		
	else
		addedSuccessfully = false;

	return addedSuccessfully;
}
function addThisContact(actual, show, show_filter)
{
	var selectedContactIndex = show_filter.selectedIndex;

	if (show_filter.options[selectedContactIndex].value == 0)
		alert("Please choose a Contact.");
	else
	{
		var selectedContactName = show_filter.options[selectedContactIndex].text;

		var selectedContactID = show_filter.options[selectedContactIndex].value;

		var success = addContact(selectedContactID, selectedContactName, show, actual);
		if (!success)
			alert("This contact is already in the selected contact list.");
	}
}


function getOptionIndexInSelect(mySelect, myValue)
{
	var found = false;
	var i=0;
	while (i<mySelect.length && !found)
	{
		if (mySelect.options[i].value == myValue)
			found = true;
		else
			i++;
	}
	
	if (found)
		return i;
	else
		return -1;
}
function SelectAllOptions(mySelect)
{
	var i=0;
	while (i<mySelect.length)
	{
	 mySelect.options[i].selected = true;
	 i++
	}
}

function insertNewOption(mySelect, myValue, myText)
{
	var currLength = mySelect.length;
	
	var newOption = new Option();
	newOption.value = myValue;
	newOption.text = myText;
	
	mySelect.options[currLength] = newOption;
}

function removeOptionAtIndex(mySelect, i)
{
	if (i >= 0)
		mySelect.options[i] = null;	
}

function clearOptions(mySelect)
{
	mySelect.options.length = 0;
}

//----------------------------------------------------------
function getNumCheckboxesInForm(formName)
{
	var numCheckboxes = 0;

	for (var i=0; i<formName.elements.length; i++)
		if (formName.elements[i].type == "checkbox")
			numCheckboxes++;
	
	return numCheckboxes;
}

// return the number of elements of the given name in the form
function getNumElementsInForm(formName, elementName)
{
	var numElements = 0;

	for (var i=0; i<formName.elements.length; i++)
		if (formName.elements[i].name == elementName)
			numElements++;
	
	return numElements;
}

// assuming relavant checkboxes have the same name
// pass "true" to check all, "false" to uncheck all
function CheckAllCheckboxesInForm(formName, checkboxName, checkAll)
{
	for (var i=0; i<formName.elements.length; i++)
		if (formName.elements[i].name == checkboxName &&
			formName.elements[i].type == "checkbox")
		{
			formName.elements[i].checked = checkAll;
		}
}

// assuming relavant checkboxes have the same name
function getNumCheckedCheckboxesInForm(formName, checkboxName)
{
	var numChecked = 0;

	for (var i=0; i<formName.elements.length; i++)
		if (formName.elements[i].name == checkboxName &&
			formName.elements[i].type == "checkbox")
		{
			if (formName.elements[i].checked) numChecked++;
		}

	return numChecked;
}


function deleteContact(actual, show)
{
	removeOptionAtIndex(show, show.selectedIndex);
	createContactList(actual, show);
}

function clearContacts(actual, show)
{
	clearOptions(show);
	createContactList(actual, show);
}

function createContactList(actual, show)
{
	var updatedList = "";
	for(var i=0; i<show.length; i++)
	{
		if (i>0) updatedList += ",";
		updatedList += show.options[i].value;	
		
	}

	actual.value = updatedList;
}

function togglevis(targetId, showhide){
  if (document.getElementById){
        target = document.getElementById(targetId);
           if (showhide) target.style.display = "";
		   else target.style.display = "none";
    }
}
function toggleTreeImg(img){
	myimg = img;
	
	// navy arrow 
	if(myimg.src.indexOf('navy_arrow_down.gif')>1) {
		 myimg.src='images/navy_arrow_right.gif';
		 return 0;
	} else if(myimg.src.indexOf('navy_arrow_right.gif')>1)	 {
		 myimg.src='images/navy_arrow_down.gif';
		 return 1; 
	}

	// white arrow
	if(myimg.src.indexOf('white_arrow_down.gif')>1) {
		 myimg.src='images/white_arrow_right.gif';
		 return 0;
	} else if(myimg.src.indexOf('white_arrow_right.gif')>1)	 {
		 myimg.src='images/white_arrow_down.gif';
		 return 1; 
	}
}

function setTreeImgState(img,state){
	// state value of 0 is arrow right, 1 is down
	myimg = img;
	
	// navy arrow 
	if(myimg.src.indexOf('navy_arrow')>1) {
		switch(state) {
			case 0:
				myimg.src='images/navy_arrow_right.gif';
				break;
			case 1:
				myimg.src='images/navy_arrow_down.gif';
				break;
		}
	}

	// white arrow
	if(myimg.src.indexOf('white_arrow')>1) {
		switch(state) {
			case 0:
				myimg.src='images/white_arrow_right.gif';
				break;
			case 1:
				myimg.src='images/white_arrow_down.gif';
				break;
		}
	}
}


function toggleTreeImg2(img){
	myimg = img;
	if(myimg.src.indexOf('img-folder-open-0.gif')>1)
	 {
	 myimg.src='images/img-folder-closed-0.gif';
	 return 0;
	 }
	 else
	 {
	 myimg.src='images/img-folder-open-0.gif';
	 return 1;	 
	 }
}

function getthedata( url, writediv, write_execute) {
	var obj_data;
    if ( window.XMLHttpRequest ){
	   obj_data = new XMLHttpRequest();
    } else {
	   obj_data = new ActiveXObject("MSXML2.XMLHTTP");
    }	
	if(write_execute == 'write') {
		document.getElementById(writediv).innerHTML ='<div class="loadingData"><img src="images/wait.gif">Loading data. Please wait...</div>';		
		obj_data.onreadystatechange = function() {writedata(writediv,obj_data);};		
	}
	else obj_data.onreadystatechange = function() {executethedata(obj_data);};
    	obj_data.open( "get", url, true);
		obj_data.send(null);
}	

function executethedata(obj_data) {
	if ( obj_data.readyState == 4 ) {
		eval(obj_data.responseText);
	}
}	

function writedata(writediv,obj_data) {
	if ( obj_data.readyState == 4 ) {
		document.getElementById(writediv).innerHTML = obj_data.responseText;
	}
}	

function ajax_show_hide(url, writediv, showhide, overwrite){
	togglevis(writediv, showhide);
	if((showhide==1 && document.getElementById(writediv).innerHTML.length) == 0 || (showhide==1 && overwrite==1))
		getthedata(url, writediv, 'write');
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
		}
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curtop += obj.offsetTop;
		}
	}
	return curtop;
}

function toggleOverlay() {
	if (document.getElementById('bodyOverlay')) {
		var overlay = document.getElementById('bodyOverlay');
		
		if (overlay.style.display == 'none')
			overlay.style.display = '';
		else
			overlay.style.display = 'none';
	}
}
//]]>

