// Benefit Resource, Inc. 
// www.benefitresourceinc.com 
// Copyright 2004

function openURL(url) {
    window.location.href=url;
}

function newURL(url) {	
    newURL(url,"","");
}

function newURL(url,name,features) {
    if(name == null) name = "";
    if(features == null) features = "";
    var w = window.open(url,name,features);
    if(!w) {
        alert("A popup blocker may be preventing the window from opening.\nIf so, allow this website access or try disabling the blocker to open the window.");
    } else {
        w.focus();
    }
    return w;
}

function correctURL(url) {
    if(url == null || url.length == 0) return "";
    if(url.startsWith("http"))   return url;
    if(url.startsWith("https"))  return url;
    if(url.startsWith("ftp"))    return url;
    if(url.startsWith("mailto")) return url;
    return "http://" + url;
}

function colorBG(e,color) {
    e.style.backgroundColor = color; 
}

// SHOW ELEMENT
function show(id) {
    var e = document.getElementById(id);
    if(e == null) return;
    e.style.display = "";
}

// HIDE ELEMENT
function hide(id) {
    var e = document.getElementById(id);
    if(e == null) return;
    e.style.display = "none";
}

// TOGGLE (SHOW / HIDE) ELEMENT
function toggle(id) {
    var e = document.getElementById(id);
    if(e == null) return;
    e.style.display = e.style.display == "none" ? "" : "none";
}

// SHOW OR HIDE ELEMENT
function display(id,bool) {
    var e = document.getElementById(id);
    if(e == null) return;
    e.style.display = bool ? "" : "none";
}

function autoTab(input,e,len) {
    var isNN = (navigator.appName.indexOf("Netscape")!=-1);
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
    }
    function containsElement(arr, ele) {
        var found = false, index = 0;
        while(!found && index < arr.length) {
            if(arr[index] == ele) found = true;
            else index++;
        }
        return found;
    }
    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1) {
            if (input.form[i] == input) index = i;
            else i++;
        }
        return index;
    }
    return true;
}

function buttonOn(id, control, newColor, save) {
    if (save) document.getElementById(control).name="1";
    document.getElementById(id).bgColor=newColor;
}

function buttonOff(id, control, newColor, save) {
    if ((save==false) && (document.getElementById(control).name=="1")) return;
    document.getElementById(control).name="0";
    document.getElementById(id).bgColor=newColor;
}

function formatCurrency(num) {
    if(isNaN(num)) return "$0.00";
    // ACCOUNTING RULES: ROUND DOWN IF LAST DIGIT IS 5 AND SECOND TO LAST DIGIT IS EVEN
    num = new String(parseFloat(num).toFixed(3));
    if(num.charAt(num.length-1) == '5' && parseInt(num.charAt(num.length-2)) % 2 == 0) {
	num = num.substring(0,num.length-1);
    }   
    var cents = Math.abs(Math.round((num * 100) % 100));
    if(cents < 10) {
        cents = "0" + cents;
    }    
    var n = ""; var count = 0; var neg = num < 0;
    num = Math.floor(Math.abs(num)).toString();
    for(var i = num.length - 1; i >= 0; i--) {
        if(count > 0 && count % 3 == 0) n = "," + n;
        n = num.charAt(i) + n;
        count++;
    }    
    return (neg ? "-" : "") + ("$" + n + "." + cents);
}

function numericValue(input) {
    var chars = "0123456789.";
    var output = "";
    for (var i=0; i < input.length; i++)
      if (chars.indexOf(input.charAt(i)) != -1)
        if (!(output.length == 0 && input.charAt(i) == "0"))
          output += input.charAt(i);
    if (output == "") return 0;
    return output;
}

function setTime(d,h,m,date) {
    var min = date.getMinutes();
    if(min < 8) {
        min = "00";
    } else if(min < 23) {
        min = "15";
    } else if(min < 38) {
        min = "30";
    } else if(min < 53) {
        min = "45";
    } else {
        min = "00";
	date.setHours(date.getHours() + 1);
    }
    var hr = date.getHours();
    hr = hr >= 12 ? (hr > 12 ? hr - 12 : hr) + " pm" : hr + " am"
    d.value = formatDate(date,"MM/dd/yyyy");
    setSelectedByValue(h,hr);
    setSelectedByValue(m,min);
}

// PIPE CLASS

function Pipe(p) {
    this.value = p;
}

Pipe.prototype.contains = function(p) {
    return this.value.indexOf("|" + p + "|") > -1;
}

Pipe.prototype.add = function(p) {
    if(this.value.length == 0) {
        this.value = "|" + p + "|";
    } else {
        if(!this.contains(p)) {
            this.value += p + "|";
        }
    }
    return this.value;
}

Pipe.prototype.remove = function(p) {
    if(this.value == "|" + p + "|") {
        this.value = "";
    } else {
	this.value = this.value.replace(p + "|","");
    }	
    return this.value;
}

// STRING CLASS

String.prototype.trim = function() {
   return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.after = function(delim) {
   if(delim == null || delim == "") return this;
   var index = this.indexOf(delim);
   if(index > -1){
       index += delim.length;
       return this.substring(index,this.length);
   }
   return this;
}

String.prototype.before = function(delim) {
   if(delim == null || delim == "") return this;
   var index = this.indexOf(delim);
   if(index > 0) return this.substring(0,index);
   return this;
}

String.prototype.startsWith = function(str) {
    if(str.length == 0) return true;
    if(str.length > this.length) return false;
    return this.indexOf(str) == 0;
}

String.prototype.endsWith = function(str) {	
    if(str.length == 0) return true;	
    if(str.length > this.length) return false;
    return this.slice(this.length - str.length) == str;	
}