// ---------------------------------------------------------------------------------
/* Set focus to id */
function f(id){

  document.getElementById(id).focus();
}
// ---------------------------------------------------------------------------------
/* This script will take an enter keystroke and change it to a tab. */
function entertotab() {
  if(window.event && window.event.keyCode==13)
  		window.event.keyCode=9;
}
/* This will take an enter keystroke and kill it. */
function noenter() {
  return !(window.event && window.event.keyCode == 13); }

/* This script will resize a window such that all of the elements exist
   within the width and height.                                              */

var OFFSET_WIDTH = 45;  // How far to offset the right most element
var OFFSET_HEIGHT = 85; // How far to offset the bottom most element

  function getMaxWidth(objElement, intWidth) {

   var objChild;

    objChild = objElement.firstChild;
     while(!(objChild == null)) {
      intWidth = getMaxWidth(objChild, intWidth);
      objChild = objChild.nextSibling;
     }

    return ((objElement.offsetLeft + objElement.offsetWidth > intWidth) ? objElement.offsetLeft + objElement.offsetWidth : intWidth);
  }

  function getMaxHeight(objElement, intHeight) {

   var  objChild;

    objChild = objElement.firstChild;
     while(objChild != null) {
      intHeight = getMaxHeight(objChild, intHeight);
      objChild = objChild.nextSibling;
     }

    return ((objElement.offsetTop + objElement.offsetHeight > intHeight) ? objElement.offsetTop + objElement.offsetHeight : intHeight);
  }

  function reSize(intMaxHeight, intMaxWidth) {

   var intWidth;
   var intHeight;

   startNode = document;
    self.resizeTo(1,1);
    intWidth = getMaxWidth(startNode, 0) + OFFSET_WIDTH;
    self.resizeTo(intWidth, 1);
    intHeight = getMaxHeight(startNode, 0) + OFFSET_HEIGHT;

    //self.moveTo(((screen.width/2)-(intWidth /2)),((screen.height/2)-(intHeight/2)));
    if(eval(intMaxHeight))
     if(intHeight>intMaxHeight)
      intHeight=intMaxHeight;

    //self.moveTo(((screen.width/2)-(intWidth /2)),((screen.height/2)-(intHeight/2)));
    if(eval(intMaxWidth))
     if(intWidth>intMaxWidth)
      intWidth=intMaxWidth;

    self.resizeTo(intWidth, intHeight);

  }

  function closeSelf() {

   window.opener.location = window.opener.location;
   window.close()
  }
// ---------------------------------------------------------------------------------
function send2clipboard(txt){

  if (window.clipboardData){
    window.clipboardData.setData("Text", txt);
  }else if (window.netscape){
    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
    var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
    if (!clip) return;
    var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
    if (!trans) return;
    trans.addDataFlavor('text/unicode');
    var str = new Object();
    var len = new Object();
    var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
    var copytext=txt;
    str.data=copytext;
    trans.setTransferData("text/unicode",str,copytext.length*2);
    var clipid=Components.interfaces.nsIClipboard;
    if (!clip) return false;
    clip.setData(trans,null,clipid.kGlobalClipboard);
  }
   return false;
}
// ---------------------------------------------------------------------------------
function getAbsTop(o) {
	oTop = o.offsetTop;
	while(o.offsetParent!=null) {
	  oParent = o.offsetParent;
	  oTop += oParent.offsetTop;
	  o = oParent;
	}
	return oTop;
}
function getAbsLeft(o) {
	oLeft = o.offsetLeft;
	while(o.offsetParent!=null) {
	  oParent = o.offsetParent;
	  oLeft += oParent.offsetLeft;
	  o = oParent;
	}
	return oLeft;
}
// ---------------------------------------------------------------------------------
/* This script will popup a window with the expectation of it being modal         */

  function modalPopup(strURL,x,y,w,h) {
   var x = (eval(x) ? x : 100);
   var y = (eval(y) ? y : 100);
   var w = (eval(w) ? w : 300);
   var h = (eval(h) ? h : 200);

   winModalWindow = window.open(strURL,"","width=1,height=1,dependent=yes,resizable=yes,scrollbars=yes,status=yes,top="+y+",left="+y+",width="+w+",height="+h);
   winModalWindow.focus();
  }
// ---------------------------------------------------------------------------------
function fixNumber(strNum) {
  strNum = "0" + strNum;
  return strNum.substring(strNum.length -2);
}
// ---------------------------------------------------------------------------------
function newAction(strDate, userID, timeID, extra){

  var strExtra = "";
  if(eval(extra))
   strExtra += "&"+extra;
  modalPopup("mod_schedule.php?userID="+userID+"&strDate="+strDate+"&timeID="+timeID+strExtra);
}
// ---------------------------------------------------------------------------------
function in_array(my_array,my_value){
	for(var i=0;i<my_array.length;i++){
		if(my_array[i] == my_value){
			return true;
		}
	}
	return false;
}
// ---------------------------------------------------------------------------------
function toUpper(id, val){
	document.getElementById(id).value = val.toUpperCase();
}
// ---------------------------------------------------------------------------------
function checkall(id, num){
	for(var x=0;x<num;x++)
		document.getElementById(id+x).checked = "checked";
}
function uncheckall(id, num){
	for(var x=0;x<num;x++)
		document.getElementById(id+x).checked = "";
}
// ---------------------------------------------------------------------------------
function js_addslashes(string){

  return replace( replace(string, "'", "&#039;"), '"', '&quot;');
}
// ---------------------------------------------------------------------------------
function js_stripslashes(string){

  return replace( replace(string, "\'", "'"), '\"', '"');
}
// ---------------------------------------------------------------------------------
function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
// ---------------------------------------------------------------------------------
function select_ByValue(id, val){

  var opts = document.getElementById(id).options;
  for(var x=0;x<opts.length;x++)
	if(document.getElementById(id).options[x].value == val){
	  document.getElementById(id).selectedIndex = x;
	  return true;
	}
  return false;
}
// ---------------------------------------------------------------------------------
function selectAllText(id) {
  var tempval=eval("document.getElementById('"+id+"')");
  tempval.focus();
  tempval.select();
}
// ---------------------------------------------------------------------------------
function selectCustomerPopup(urlbase, insurance, department){

  var strInsurance = "";
  if(eval(insurance))
    strInsurance = "?insurance=1";

  var strDepartment = "";
  if(eval(department))
    strDepartment = (strInsurance.length>0 ? "&" : "?")+"dept=1";

  var win = window.open(urlbase+"popupSelectCustomer.php"+strInsurance+strDepartment, 'selectCustomer', 'toolbar=0,scrollbars=1,location=0,status=yes,menubar=0,resizable=1,width=735,height=500,left=50,top=50');
  win.focus();
}
// ---------------------------------------------------------------------------------
function selectContactPopup(urlbase){

  var win = window.open(urlbase+"popupSelectContact.php", 'selectContact', 'toolbar=0,scrollbars=1,location=0,status=yes,menubar=0,resizable=1,width=735,height=500,left=50,top=50');
  win.focus();
}
// ---------------------------------------------------------------------------------
function selectVendorPopup(urlbase){

 var win = window.open(urlbase+"popupSelectVendor.php", 'selectVendor', 'toolbar=0,scrollbars=1,location=0,status=yes,menubar=0,resizable=1,width=735,height=500,left=50,top=50');
 win.focus();
}
// ---------------------------------------------------------------------------------
function insertbbcode(id, myValue) {

 var myField = document.getElementById(id);

 if (document.selection) {
  //IE support
  myField.focus();
  sel = document.selection.createRange();
  sel.text = myValue;
 }else if (myField.selectionStart || myField.selectionStart == '0') {
  //MOZILLA/NETSCAPE support
  var startPos = myField.selectionStart;
  var endPos = myField.selectionEnd;
  myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
 } else {
  myField.value += myValue;
 }
}
// ---------------------------------------------------------------------------------
function pause(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}
// ---------------------------------------------------------------------------------
// http://www.jsfromhell.com/geral/event-listener
/*
Functions
addEvent(object: Object, event: String, handler: Function(e: Event): Boolean, [scope: Object = object]): void
Adds an event listener to an object.
-object: object that will receive the listener
-event: event name without the "on" prefix (click, mouseover, ...)
-handler: function that will be called when the event occur, the event object will be sent as argument to this function, and besides the normal properties, it will *always* have:
--target: object that generated the event
--key: keeps the character key code on keyboard events
--stopPropagation: method to avoid the event propagation
--preventDefault: method to avoid the default action the preventDefault method can be emulated by returning "false" in the function
-scope: scope (who the "this" inside the callback will refer to) that will be used when the function get invoked, the default value is the object on the first argument

removeEvent(object: Object, event: String, handler: function(e: Event): Boolean, [scope: Object = object]): Boolean
Removes a previously added listener from the object and returns true in case of success.
-object: object that received the listener
-event: event name without the "on" prefix (click, mouseover, ...)
-handler: same function that was assigned on the addEvent
-scope: same scope that was assigned on the addEvent (if you provided a scope, it's necessary to send the same object as argument, otherwise the listener won't be removed), the default value is the object on the first argument
*/
// ---------------------------------------------------------------------------------
addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};
// ---------------------------------------------------------------------------------
/* quick getElement reference */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
// ---------------------------------------------------------------------------------
/** Detects whether any version of Mozilla (Netscape, firefox, etc) is being used.  Returns
 * true if it is, false otherwise. */
function isMozilla() {
    return navigator.appName.toLowerCase().indexOf("netscape") >= 0;
}
// ---------------------------------------------------------------------------------
/** Detects whether any version of Internet Explorer is being used.  Returns true if
 * it is, false otherwise. */
function isIE() {
    return navigator.appName.toLowerCase().indexOf("internet explorer") >= 0;
}
// ---------------------------------------------------------------------------------
/** Returns the name of the current browser.  Simply a convenience method, calls a
 * normal browser function.  */
function getBrowser() {
    return navigator.appName;
}
// ---------------------------------------------------------------------------------
/* Count the number of selected items in a combo that allows multiple
 * options to be selected.  */
function countSelected(id) {
    var options = document.getElementById(id).options;
    var selected = 0;

    for(var i = 0; i < options.length; i++)
        if(options[i].selected)
            selected++;

    return selected;
}

// ---------------------------------------------------------------------------------
/* Prevents a form from being submitted more than once.  Put a call to this in the
 * onSubmit event of the form tag:
 * <form action='...' onSubmit='return submitOnce()'>
 */
var submitOnceVar = false;
function submitOnce() {
	if(submitOnceVar == false) {
		submitOnceVar = true;
		return true;
	}
	return false;
}
// ----------------------------------------------------------------------------------
function phoneMask(format,id,val){

  var returnStr = "";
	if(val.length==10){
		if(format == "1")
			returnStr = "("+val.substring(0,3)+") "+val.substring(3,6)+"-"+val.substring(6,10);
		else if(format == 2)
			returnStr = val.substring(0,3)+"."+val.substring(3,6)+"."+val.substring(6,10);
		document.getElementById(id).value = returnStr;
		return true;
	}else if(val.length==7){
	    if(format == "1")
			returnStr = val.substring(0,3)+"-"+val.substring(3);
		else if(format == "2")
			returnStr = val.substring(0,3)+"."+val.substring(3);
		document.getElementById(id).value = returnStr;
		return true;
	}
	return true;
}