function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* This function is used to change the style class of an element */
function swapClass(obj, newStyle) {
    obj.className = newStyle;
}

function isUndefined(value) {   
    var undef;   
    return value == undef; 
}

/* Function for showing and hiding elements that use 'display:none' to hide */
function toggleDisplay(targetId)
{
    if (document.getElementById) {
        target = document.getElementById(targetId);
    	if (target.style.display == "none"){
    		target.style.display = "";
    	} else {
    		target.style.display = "none";
    	}
    }
}

// toggle visibility 
function toggleVisibility(targetId) {
    if (document.getElementById) {
        target = document.getElementById(targetId);
    	if (target.style.visibility == "hidden"){
    		target.style.visibility = "visible";
    	} else {
    		target.style.visibility = "hidden";
    	}
    }
}

function checkAll(theForm) { // check all the checkboxes in the list
  for (var i=0;i<theForm.elements.length;i++) {
    var e = theForm.elements[i];
		var eName = e.name;
    	if (eName != 'allbox' && 
            (e.type.indexOf("checkbox") == 0)) {
        	e.checked = theForm.allbox.checked;		
		}
	} 
}

/* Function to clear a form of all it's values */
function clearForm(frmObj) {
	for (var i = 0; i < frmObj.length; i++) {
        var element = frmObj.elements[i];
		if(element.type.indexOf("text") == 0 || 
				element.type.indexOf("password") == 0) {
					element.value="";
		} else if (element.type.indexOf("radio") == 0) {
			element.checked=false;
		} else if (element.type.indexOf("checkbox") == 0) {
			element.checked = false;
		} else if (element.type.indexOf("select") == 0) {
			for(var j = 0; j < element.length ; j++) {
				element.options[j].selected=false;
			}
            element.options[0].selected=true;
		}
	} 
}

/* Function to get a form's values in a string */
function getFormAsString(frmObj) {
    var query = "";
	for (var i = 0; i < frmObj.length; i++) {
        var element = frmObj.elements[i];
        if (element.type.indexOf("checkbox") == 0 || 
            element.type.indexOf("radio") == 0) { 
            if (element.checked) {
                query += element.name + '=' + escape(element.value) + "&";
            }
		} else if (element.type.indexOf("select") == 0) {
			for (var j = 0; j < element.length ; j++) {
				if (element.options[j].selected) {
                    query += element.name + '=' + escape(element.value) + "&";
                }
			}
        } else {
        	if ( element.value.length > 0 ) {
	            query += element.name + '=' 
	                  + escape(element.value) + "&"; 
            }
        }
    } 
    return query;
}

/* Function to hide form elements that show through
   the search form when it is visible */
function toggleForm(frmObj, iState) // 1 visible, 0 hidden 
{
	for(var i = 0; i < frmObj.length; i++) {
		if (frmObj.elements[i].type.indexOf("select") == 0 || frmObj.elements[i].type.indexOf("checkbox") == 0) {
            frmObj.elements[i].style.visibility = iState ? "visible" : "hidden";
		}
	} 
}

/* Helper function for re-ordering options in a select */
function opt(txt,val,sel) {
    this.txt=txt;
    this.val=val;
    this.sel=sel;
}

/* Function for re-ordering <option>'s in a <select> */
function move(list,to) {     
    var total=list.options.length;
    index = list.selectedIndex;
    if (index == -1) return false;
    if (to == +1 && index == total-1) return false;
    if (to == -1 && index == 0) return false;
    to = index+to;
    var opts = new Array();
    for (i=0; i<total; i++) {
        opts[i]=new opt(list.options[i].text,list.options[i].value,list.options[i].selected);
    }
    tempOpt = opts[to];
    opts[to] = opts[index];
    opts[index] = tempOpt
    list.options.length=0; // clear
    
    for (i=0;i<opts.length;i++) {
        list.options[i] = new Option(opts[i].txt,opts[i].val);
        list.options[i].selected = opts[i].sel;
    }
    
    list.focus();
} 

/*  This function is to select all options in a multi-valued <select> */
function selectAll(elementId) {
    var element = document.getElementById(elementId);
	len = element.length;
	if (len != 0) {
		for (i = 0; i < len; i++) {
			element.options[i].selected = true;
		}
	}
}


/* This function is used to select a checkbox by passing
 * in the checkbox id
 */
function toggleChoice(elementId) {
    var element = document.getElementById(elementId);
    if (element.checked) {
        element.checked = false;
    } else {
        element.checked = true;
    }
}

/* This function is used to select a radio button by passing
 * in the radio button id and index you want to select
 */
function toggleRadio(elementId, index) {
    var element = document.getElementsByName(elementId)[index];
    element.checked = true;
}


/* This function is used to open a pop-up window */
function openWindow(url, winTitle, winParams) {
	winName = window.open(url, winTitle, winParams);
    winName.focus();
}


/* This function is to open search results in a pop-up window */
function openSearch(url, winTitle) {
    var screenWidth = parseInt(screen.availWidth);
    var screenHeight = parseInt(screen.availHeight);

    var winParams = "width=" + screenWidth + ",height=" + screenHeight;
        winParams += ",left=0,top=0,toolbar,scrollbars,resizable,status=yes";

    openWindow(url, winTitle, winParams);
}

/* This function is used to set cookies */
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

/* This function is used to get cookies */
function getCookie(name) {
	var prefix = name + "=" 
	var start = document.cookie.indexOf(prefix) 

	if (start==-1) {
		return null;
	}
	
	var end = document.cookie.indexOf(";", start+prefix.length) 
	if (end==-1) {
		end=document.cookie.length;
	}

	var value=document.cookie.substring(start+prefix.length, end) 
	return unescape(value);
}

/* This function is used to delete cookies */
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// Remove leading and trailing whitespace from a string
function trimWhitespace(str) {
	 //alert(str);
   var newString  = '';
   var substring  = '';
   beginningFound = false;
   
   // copy characters over to a new string
   // retain whitespace characters if they are between other characters
   for (var i = 0; i < str.length; i++) {
      
      // copy non-whitespace characters
      if (str.charAt(i) != ' ' && str.charCodeAt(i) != 9) {
         
         // if the temporary string contains some whitespace characters, copy them first
         if (substring != '') {
            newString += substring;
            substring = '';
         }
         newString += str.charAt(i);
         if (beginningFound == false) beginningFound = true;
      }
      
      // hold whitespace characters in a temporary string if they follow a non-whitespace character
      else if (beginningFound == true) substring += str.charAt(i);
   }
   //alert(newString);
   return newString;
}

/**
 *  String convenience method for checking if the
 *  end of this string equals a given string.
 *
 *  @returns boolean
 *  @throws IllegalArgumentException for parameters
 *                          not of type String
 */
String.prototype.endsWith = function (s) {
    if ('string' != typeof s) {
        throw('IllegalArgumentException: Must pass a ' +
            ' string to String.prototype.endsWith()');
    }
    var start = this.length - s.length;
    return this.substring(start) == s;
};

// This function is for stripping leading and trailing spaces
function trim(str) { 
    if (str != null) {
        var i; 
        for (i=0; i<str.length; i++) {
            if (str.charAt(i)!=" ") {
                str=str.substring(i,str.length); 
                break;
            } 
        } 
    
        for (i=str.length-1; i>=0; i--) {
            if (str.charAt(i)!=" ") {
                str=str.substring(0,i+1); 
                break;
            } 
        } 
        
        if (str.charAt(0)==" ") {
            return ""; 
        } else {
            return str; 
        }
    }
} 

// This function is used by the login screen to validate user/pass
// are entered. 
function validateRequired(form) {                                    
    var bValid = true;
    var focusField = null;
    var i = 0;                                                                                          
    var fields = new Array();                                                                           
    oRequired = new required();                                                                         
                                                                                                        
    for (x in oRequired) {                                                                              
        if ((form[oRequired[x][0]].type == 'text' || form[oRequired[x][0]].type == 'textarea' || form[oRequired[x][0]].type == 'select-one' || form[oRequired[x][0]].type == 'radio' || form[oRequired[x][0]].type == 'password') && form[oRequired[x][0]].value == '') {
           if (i == 0)
              focusField = form[oRequired[x][0]]; 
              
           fields[i++] = oRequired[x][1];
            
           bValid = false;                                                                             
        }                                                                                               
    }                                                                                                   
                                                                                                       
    if (fields.length > 0) {
       focusField.focus();
       alert(fields.join('\n'));                                                                      
    }                                                                                                   
                                                                                                       
    return bValid;                                                                                      
}

// This function is a generic function to create form elements
function createFormElement(element, type, name, id, value, parent) {
    var e = document.createElement(element);
    e.setAttribute("name", name);
    e.setAttribute("type", type);
    e.setAttribute("id", id);
    e.setAttribute("value", value);
    parent.appendChild(e);
}

function confirmDelete(obj) {   
    var msg = "Are you sure you want to delete this " + obj + "?";
    ans = confirm(msg);
    if (ans) {
        return true;
    } else {
        return false;
    }
}

function highlightTableRows(tableId) {
    var previousClass = null;
    var table = document.getElementById(tableId); 
    var tbody = table.getElementsByTagName("tbody")[0];
    if (tbody == null) {
        var rows = table.getElementsByTagName("tr");
    } else {
        var rows = tbody.getElementsByTagName("tr");
    }
    // add event handlers so rows light up and are clickable
    for (i=0; i < rows.length; i++) {
        rows[i].onmouseover = function() { previousClass=this.className;this.className+=' over' };
        rows[i].onmouseout = function() { this.className=previousClass };
        rows[i].onclick = function() {
            var cell = this.getElementsByTagName("td")[0];
            var link = cell.getElementsByTagName("a")[0];
            location.href = link.getAttribute("href");
            this.style.cursor="wait";
        }
    }
}

function highlightFormElements() {
    // add input box highlighting 
    addFocusHandlers(document.getElementsByTagName("input"));
    addFocusHandlers(document.getElementsByTagName("textarea"));
}

function addFocusHandlers(elements) {
    for (i=0; i < elements.length; i++) {
        if (elements[i].type != "button" && elements[i].type != "submit" &&
            elements[i].type != "reset" && elements[i].type != "checkbox") {
            elements[i].onfocus=function() {this.className='focus';this.select()};
            elements[i].onclick=function() {this.select()};
            elements[i].onblur=function() {this.className=''};
        }
    }
}

function radio(clicked){
    var form = clicked.form;
    var checkboxes = form.elements[clicked.name];
    if (!clicked.checked || !checkboxes.length) {
        clicked.parentNode.parentNode.className="";
        return false;
    }

    for (i=0; i<checkboxes.length; i++) {
        if (checkboxes[i] != clicked) {
            checkboxes[i].checked=false;
            checkboxes[i].parentNode.parentNode.className="";
        }
    }

    // highlight the row    
    clicked.parentNode.parentNode.className="over";
}

// DW added
function doSearch(url, frm) {
	var frmString = getFormAsString(frm);
	var h = url + "?method=Search&" + frmString;
	location.href=h;
}
// =========

function uppercase(elemId) {

	var e = document.getElementById(elemId);
	if ( e != null ) {
		e.value = e.value.toUpperCase();
	}

}

function screenkey(e) {
   var whichCode;
   whichCode = (window.Event) ? e.which : e.keyCode;
   if ( whichCode == "13" ) {
   		// trap/ignore
      whichCode = null;
   }
}


function hideAllOfClass(classname) {
	divs = document.getElementsByTagName("div");
	
	for( var i = 0; i < divs.length; i++ ) {
		if ( divs[i].className == classname ) {
			divs[i].style.display = "none";
		}
	}
}

//----- String (taken from stdext.js, which was causing a Stack Overflow in IE)

if (!String.prototype.startsWith) {
  String.prototype.startsWith = function(prefix) {
//    return (this.substr(0, prefix.length) == prefix);
    return (this.indexOf(prefix) === 0);
  };
}

if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(suffix) {
    var startPos = this.length - suffix.length;
    if (startPos < 0) {
      return false;
    }
    return (this.lastIndexOf(suffix, startPos) == startPos);
  };
}

if (!String.prototype.withoutPrefix) {
  String.prototype.withoutPrefix = function(prefix) {
    if (this.startsWith(prefix)) {
      return this.substr(prefix.length);
    } else {
      return this;
    }
  };
}

if (!String.prototype.withoutSuffix) {
  String.prototype.withoutSuffix = function(suffix) {
    if (this.endsWith(suffix)) {
      return this.substr(0, this.length - suffix.length);
    } else {
      return this;
    }
  };
}

if (!String.prototype.strip) {
  String.prototype.strip = function() {
    return this.replace(/^\s*(.*?)\s*$/, "$1");
  };
}

if (!String.prototype.format) {
  // This function substitutes placeholders -- {0}, {1}, ... -- with
  // its arguments. The substituted value can be formatted by a function
  // like this
  // 'The list is {0, formatList}'.format(['a', 'b', 'c', 'd'])
  // with
  // function formatList(l) {
  //   if (l.length == 0) return 'empty';
  //   if (l.length == 1) return l[0];
  //   if (l.length == 2) return l[0] + ' and ' + l[1];
  //   var s = '';
  //   for (var i = 0; i < l.length - 1; i++)
  //     s += l[i] + ', ';
  //   return s + 'and ' + l[i];
  // }
  String.prototype.format = function() {
    var args = arguments;
    return this.replace(
      /\{\{[^{}]*\}\}|\{(\d+)(,\s*([\w.]+))?\}/g,
      function(m, a1, a2, a3) {
        if (m.chatAt == '{') {
          return m.slice(1, -1);
        }
        var rpl = args[a1];
        if (a3) {
          var f = eval(a3);
          rpl = f(rpl);
        }
        return rpl ? rpl : '';
      });
  };
}

if (!String.isBlank) {
  String.isBlank = function(s) {
    return (!s || (/^\s*$/).test(s));
  };
}

if (!String.compare) {
  String.compare = function(s1, s2) {
    if (s1 == s2) {
      return 0;
    }
    if (s1 > s2) {
      return 1;
    }
    return -1;
  };
}

// ------------
//----- Array


if (!Array.prototype.difference) {
  Array.prototype.difference = function(minusArray, compareFunc) {
    if (!minusArray) {
      return this;
    }
    if (!compareFunc) {
      compareFunc = Function.equality;
    }
    var diff = new Array();
    var len = this.length;
    for (var i = 0; i < len; i++) {
      var el = this[i];
      if (!minusArray.some(function(other) { return compareFunc(el, other); })) {
        diff.push(el);
      }
    }
    return diff;
  };
}

if (!Array.prototype.equals) {
  Array.prototype.equals = function(other) {
    if (!other) {
      return false;
    }
    var len = this.length;
    if (len != other.length) {
      return false;
    }
    for (var i = 0; i < len; i++) {
      if (this[i] != other[i]) {
        return false;
      }
    }
    return true;
  };
}


Array.flatten = function(array, excludeUndefined) {
  if (excludeUndefined === undefined) {
    excludeUndefined = false;
  }
  var result = [];
  var len = array.length;
  for (var i = 0; i < len; i++) {
    var el = array[i];
    if (el instanceof Array) {
      var flat = el.flatten(excludeUndefined);
      result = result.concat(flat);
    } else if (!excludeUndefined || el != undefined) {
      result.push(el);
    }
  }
  return result;
};

if (!Array.prototype.flatten) {
  Array.prototype.flatten = function(excludeUndefined) {
    return Array.flatten(this, excludeUndefined);
  }
}

if (!Array.prototype.moveElement) {
  Array.prototype.moveElement = function(fromPos, toPos) {
    if (fromPos < 0 || fromPos >= this.length) {
      throw new Error('Array.moveElement: fromPos must be < length and >= 0: ' + fromPos);
    }
    if (toPos < 0 || toPos >= this.length) {
      throw new Error('Array.moveElement: toPos must be < length and >= 0: ' + toPos);
    }
    if (fromPos === toPos) {
      return this;
    }
    var el = this.splice(fromPos, 1);
    if (el instanceof Array) {
      el = el[0];
    }
    this.splice(toPos, 0, el);
    return this;
  };
}

if (!Array.prototype.pushUnlessNull) {
  Array.prototype.pushUnlessNull = function(element) {
    if (element != undefined) {
      return this.push(element);
    } else {
      return this.length;
    }
  };
}

if (!Array.prototype.pushUnlessContains) {
  Array.prototype.pushUnlessContains = function(element) {
    if (!this.contains(element)) {
      return this.push(element);
    } else {
      return this.length;
    }
  };
}


// The array function below are from
// http://erik.eae.net/playground/arrayextras/
// Guards added to some, in order not to override existing functions


// Mozilla 1.8 has support for indexOf, lastIndexOf, forEach, filter, map, some, every
// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:lastIndexOf
if (!Array.prototype.indexOf) {
  Array.prototype.indexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
      fromIndex = 0;
    } else if (fromIndex < 0) {
      fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex; i < this.length; i++) {
      if (this[i] === obj)
        return i;
    }
    return -1;
  };
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:lastIndexOf
if (!Array.prototype.lastIndexOf) {
  Array.prototype.lastIndexOf = function (obj, fromIndex) {
    if (fromIndex == null) {
      fromIndex = this.length - 1;
    } else if (fromIndex < 0) {
      fromIndex = Math.max(0, this.length + fromIndex);
    }
    for (var i = fromIndex; i >= 0; i--) {
      if (this[i] === obj)
        return i;
    }
    return -1;
  };
}


// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:forEach
if (!Array.prototype.forEach) {
  Array.prototype.forEach = function (f, obj) {
    var l = this.length;  // must be fixed during loop... see docs
    for (var i = 0; i < l; i++) {
      f.call(obj, this[i], i, this);
    }
  };
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:filter
if (!Array.prototype.filter) {
  Array.prototype.filter = function (f, obj) {
    var l = this.length;  // must be fixed during loop... see docs
    var res = [];
    for (var i = 0; i < l; i++) {
      if (f.call(obj, this[i], i, this)) {
        res.push(this[i]);
      }
    }
    return res;
  };
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:map
if (!Array.prototype.map) {
  Array.prototype.map = function (f, obj) {
    var l = this.length;  // must be fixed during loop... see docs
    var res = [];
    for (var i = 0; i < l; i++) {
      res.push(f.call(obj, this[i], i, this));
    }
    return res;
  };
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:some
if (!Array.prototype.some) {
  Array.prototype.some = function (f, obj) {
    var l = this.length;  // must be fixed during loop... see docs
    for (var i = 0; i < l; i++) {
      if (f.call(obj, this[i], i, this)) {
        return true;
      }
    }
    return false;
  };
}

// http://developer-test.mozilla.org/docs/Core_JavaScript_1.5_Reference:Objects:Array:every
if (!Array.prototype.every) {
  Array.prototype.every = function (f, obj) {
    var l = this.length;  // must be fixed during loop... see docs
    for (var i = 0; i < l; i++) {
      if (!f.call(obj, this[i], i, this)) {
        return false;
      }
    }
    return true;
  };
}

if (!Array.prototype.contains) {
  Array.prototype.contains = function (obj) {
    return this.indexOf(obj) != -1;
  };
}

if (!Array.prototype.copy) {
  Array.prototype.copy = function (obj) {
    return this.concat();
  };
}

if (!Array.prototype.insertAt) {
  Array.prototype.insertAt = function (obj, i) {
    this.splice(i, 0, obj);
  };
}

if (!Array.prototype.insertBefore) {
  Array.prototype.insertBefore = function (obj, obj2) {
    var i = this.indexOf(obj2);
    if (i == -1)
      this.push(obj);
    else
      this.splice(i, 0, obj);
  };
}

if (!Array.prototype.removeAt) {
  Array.prototype.removeAt = function (i) {
    this.splice(i, 1);
  };
}

if (!Array.prototype.remove) {
  Array.prototype.remove = function (obj) {
    var i = this.indexOf(obj);
    if (i != -1)
     this.splice(i, 1);
  };
}

// ------------------

function addWindowOnload(func) {
	//  attach window onload event
	if (window.addEventListener)
	{
	    window.addEventListener("load", func, false);
	}
	else if (window.attachEvent)
	{
	    window.attachEvent("onload", func);
	}
	else
	{
	    window.onload = func;
	}

}

function setReadOnly(fieldId, readOnly) {
	var fld = $(fieldId);
		
	if (readOnly) {
		Element.hide(fieldId);
	}
	else {
		Element.show(fieldId);
	}
}


/**
 * Toolbar
 */
var Toolbar = Class.create();

Toolbar.prototype = {
	
	/**
	  * initialize
	  * @param toolbarDiv
	  */
	initialize: function(toolbarDiv) {
		this.toolbarDiv = toolbarDiv;
	},
	
	/**
	  * maximize
	  */
	maximize: function() {
		// hide left and top
		$("left").hide();
		$("left").addClassName("hidden");
		$("head").hide();
		$("head").addClassName("hidden");
		$("content").addClassName("maximize");

	},
	
	/**
	  * restore
	  */
	restore: function() {
		// hide left and top
		$("left").show();
		$("left").removeClassName("hidden");
		$("head").show();
		$("head").removeClassName("hidden");
		$("content").removeClassName("maximize");
	}


}

// Show the document's title on the status bar
window.defaultStatus=document.title;

