﻿// JScript File

function openWindow(url)
{
    window.open(url, "popup_" + Math.random().toString().replace(".",""), "width=800,height=600,scrollbars");
}

function openSmallWindow(url)
{
    window.open(url, "popup_" + Math.random().toString().replace(".",""), "width=400,height=300,scrollbars,resizable");//,toolbar,resizable,titlebar,menubar");
}

function openSmallWindow2(url, width, height)
{
    window.open(url, "popup_" + Math.random().toString().replace(".",""), "width=" + width + ",height=" + height + ",scrollbars,resizable");//,toolbar,resizable,titlebar,menubar");
}

function ShowRadWindow(url, title, width, height)
{
 //Show new window
 //not providing a name as a second parameter will create a new window
 var oWindow = window.radopen (url, null);

 //Using the reference to the window its clientside methods can be called
 oWindow.SetSize (width, height);
 oWindow.SetTitle (title);
}

function DeselectList(lst)
{
    for(var i = 0; i < lst.options.length; i++)
        lst.options[i].selected = false;
    lst.selectedIndex = -1;
}

function SelectListItems(lst, values) {
    DeselectList(lst);
    if (!values)
        return;
    for (var i = 0; i < lst.options.length; i++) {
        for (var j = 0; j < values.length; j++) {
            if (lst.options[i].value.toUpperCase() == values[j].toUpperCase()) {
                lst.options[i].selected = true;
            }
        }
    }
    return -1;
}

function SelectListItem(lst, value) {
    DeselectList(lst);
    for (var i = 0; i < lst.options.length; i++) {
        if (lst.options[i].value.toUpperCase() == value.toUpperCase()) {
            lst.options[i].selected = true;
            return i;
        }
    }
    return -1;
}

function GetListSelection(lst) {
    if (lst.selectedIndex > -1)
        return lst.options[lst.selectedIndex].value;
    return "";
}

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];}
}


Array.prototype.findArray = function(searchStr) {
    var returnArray = false;
    for (i = 0; i < this.length; i++) {
        if (typeof (searchStr) == 'function') {
            if (searchStr.test(this[i])) {
                if (!returnArray) { returnArray = [] }
                returnArray.push(i);
            }
        } else {
            if (this[i] === searchStr) {
                if (!returnArray) { returnArray = [] }
                returnArray.push(i);
            }
        }
    }
    return returnArray;
}

Array.prototype.find = function(searchStr) {
    for (i = 0; i < this.length; i++) {
        if (this[i] === searchStr) {
            return i;
        }
    }
    return -1;
}

function centerElementOnScreen(element) {
    var scrollTop = document.body.scrollTop;
    var scrollLeft = document.body.scrollLeft;
    var viewPortHeight = document.body.clientHeight;
    var viewPortWidth = document.body.clientWidth;
    if (document.compatMode == "CSS1Compat") {
        viewPortHeight = document.documentElement.clientHeight;
        viewPortWidth = document.documentElement.clientWidth;
        scrollTop = document.documentElement.scrollTop;
        scrollLeft = document.documentElement.scrollLeft;
    }
    var topOffset = Math.ceil(viewPortHeight / 2 - element.offsetHeight / 2);
    var leftOffset = Math.ceil(viewPortWidth / 2 - element.offsetWidth / 2);
    var top = scrollTop + topOffset - 40;
    var left = scrollLeft + leftOffset - 70;
    element.style.position = "absolute";
    element.style.top = top + "px";
    element.style.left = left + "px";
}

String.prototype.trim = function() {
    var str = this.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
    while (ws.test(str.charAt(--i)));
    return str.slice(0, i + 1);
}

function vpWidth() {
    return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}
function vpHeight() {
    return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}

function OnlyNumbers (event) {
    if (event.keyCode == 46 || event.keyCode == 8) {
    } else {
        if (event.keyCode < 95) {
            if (event.keyCode < 48 || event.keyCode > 57) {
                event.preventDefault();
            }
        } else {
            if (event.keyCode < 96 || event.keyCode > 105) {
                event.preventDefault();
            }
        }
    }
}

String.repeat = function (chr, count) {
    var str = "";
    for (var x = 0; x < count; x++) { str += chr };
    return str;
}

String.prototype.padL = function (width, pad) {
    if (!width || width < 1)
        return this;

    if (!pad) pad = " ";
    var length = width - this.length
    if (length < 1) return this.substr(0, width);

    return (String.repeat(pad, length) + this).substr(0, width);
}

String.prototype.padR = function (width, pad) {
    if (!width || width < 1)
        return this;

    if (!pad) pad = " ";
    var length = width - this.length
    if (length < 1) this.substr(0, width);

    return (this + String.repeat(pad, length)).substr(0, width);
} 

Date.prototype.formatDate = function (format) {
    var date = this;
    if (!format)
        format = "MM/dd/yyyy";

    var month = date.getMonth() + 1;
    var year = date.getFullYear();

    format = format.replace("MM", month.toString().padL(2, "0"));

    if (format.indexOf("yyyy") > -1)
        format = format.replace("yyyy", year.toString());
    else if (format.indexOf("yy") > -1)
        format = format.replace("yy", year.toString().substr(2, 2));

    format = format.replace("dd", date.getDate().toString().padL(2, "0"));

    var hours = date.getHours();
    if (format.indexOf("t") > -1) {
        if (hours > 11)
            format = format.replace("t", "pm")
        else
            format = format.replace("t", "am")
    }
    if (format.indexOf("HH") > -1)
        format = format.replace("HH", hours.toString().padL(2, "0"));
    if (format.indexOf("hh") > -1) {
        if (hours > 12) hours - 12;
        if (hours == 0) hours = 12;
        format = format.replace("hh", hours.toString().padL(2, "0"));
    }
    if (format.indexOf("mm") > -1)
        format = format.replace("mm", date.getMinutes().toString().padL(2, "0"));
    if (format.indexOf("ss") > -1)
        format = format.replace("ss", date.getSeconds().toString().padL(2, "0"));
    return format;
}

function ScrubDate(d, format) {
    if (!d) return "";
    if (format)
        return d.formatDate(format);
    return d.toString();
}

function StringIsNullOrEmpty(str) {
    return (!str || str.length == 0);
}

function IsNumber(str){
    var anum=/(^\d+$)|(^\d+\.\d+$)/
    return anum.test(str);
}

function CaptureEnter(e, buttonName, evalCode) {
    //the purpose of this function is to allow the enter key to 
    //point to the correct button to click.
    var key, evt;

    if (window.event) {
        key = window.event.keyCode;     //IE
        evt = window.event;
    }
    else {
        key = e.which;      //firefox
        evt = e;
    }

    if (key == 13) {
        //Get the button the user wants to have clicked
        if (buttonName && buttonName != "") {
            var btn = document.getElementById(buttonName);
            if (btn != null) { //If we find the button click it
                btn.click();
            }
        }
        else if (evalCode && evalCode != "") {
            eval(evalCode);
        }
        StopPropagation(e);
        return false;
    }
}

function StopPropagation(e) {
    if (!e) {
        e = window.event;
    }

    e.cancelBubble = true;
    if (e.stopPropagation) 
	    e.stopPropagation();
}

function IsTextNode(node) {
    return node.nodeType == 3;
}

function UpdateNodeTextContent(node, text) {
    var set = false;
    for (var i = 0; i < node.childNodes.length; i++) {
        if (IsTextNode(node.childNodes[i])) {
            if (!set) {
                node.childNodes[i].data = text;
                set = true;
            }
            else
                node.childNodes[i].data = "";
        }
    }
    return set;
}

