﻿function $drx() {
    this.tempX = 0;
    this.tempY = 0;
    this.tooltipText;
    this.tooltipWidth = '230px';
    this.tooltipId = 'tooltip';
    this.tooltipColorInner = '#E7F4DA';
    this.tooltipColorInnerBorder = 1;
    this.tooltipColorOuter = '#999';
    this.tooltipColorOuterBorder = 1;
    this.tooltipZindex = 99999;
    this.toolarrowWidth = 17;
}

$drx.prototype.resources = function(num) {
    return str[num];
}

/*
<summary>Key event</summary>
*/

$drx.prototype.keyEvent = function(event, keycode) {
    var e;
    if (document.all) {
        e = window.event;
    }
    else {
        e = event;
    }
    if (e.keyCode == keycode) {
        return true;
    }
}

/*
<summary>Short hand obj request</summary>
*/

$drx.prototype.getID = function(id) {
    if (document.getElementById(id))
        return document.getElementById(id);
    else
        return false;
}

$drx.prototype.getTag = function(tag) {
    if (document.getElementsByTagName(tag))
        return document.getElementsByTagName(tag);
    else
        return false;
}

$drx.prototype.getEl = function(obj) {
    if (this.getID(obj))
        return this.getID(obj);
}

/*
<summary>Key event</summary>
*/

$drx.prototype.removeClass = function(obj) {
    this.getEl(obj).removeAttribute('class');
    this.getEl(obj).removeAttribute('className');
}

/*
<summary>Number Validation</summary>
*/

$drx.prototype.isInteger = function(num) {
    var i;
    if (this.isEmpty(num))
        if (this.isInteger.arguments.length == 1) return 0;
    else return (this.isInteger.arguments[1] == true);
    for (i = 0; i < num.length; i++) {
        var c = num.charAt(i);
        if (!this.isDigit(c)) return false;
    }
    return true;
}

$drx.prototype.isEmpty = function(num) {
    return ((num == null) || (num.length == 0))
}

$drx.prototype.isDigit = function(num) {
    return ((num >= "0") && (num <= "9"))
}

$drx.prototype.isValidZip = function(zip) {
    if (this.isInteger(zip) && zip.length == 5) {
        return true
    }
    else {
        return false
    }
}
/*
<summary>JSON request script</summary>
*/

function $drx_request(fullUrl) {
    this.fullUrl = fullUrl;
    if ($drx.resources(8)) {
        this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    }
    else {
        this.noCacheIE = '';
    }
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'DrxJscriptId' + this.scriptCounter++;
}

$drx_request.scriptCounter = 1;

$drx_request.prototype.buildScriptTag = function() {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}

$drx_request.prototype.removeScriptTag = function() {
    this.headLoc.removeChild(this.scriptObj);
}

$drx_request.prototype.addScriptTag = function() {
    this.headLoc.appendChild(this.scriptObj);
}

function addScript(datasource) {
    var drx = new $drx_request(datasource);
    drx.buildScriptTag();
    drx.addScriptTag();
}

Array.prototype.remove = function(from, to) {
    var rest = this.slice((to || from) + 1 || this.length);
    this.length = from < 0 ? this.length + from : from;
    return this.push.apply(this, rest);
}

Array.prototype.clear = function() {
    this.length = 0;
}

$drx.prototype.convertToMoney = function(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) {
        num = "0";
    }
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) {
        cents = "0" + cents;
    }
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    }
    if (num < 0) {
        num = "N/A";
        return num;
    }
    return ('$' + num + '.' + cents);
}

$drx.prototype.convertToMonth = function(num, abbreviation) {
    var months = new Array();
    if (abbreviation) {
        months[0] = "January";
        months[1] = "Febuary";
        months[2] = "March";
        months[3] = "April";
        months[4] = "May";
        months[5] = "June";
        months[6] = "July";
        months[7] = "August";
        months[8] = "Septemeber";
        months[9] = "October";
        months[10] = "November";
        months[11] = "December";
    }
    else {
        months[0] = "Jan";
        months[1] = "Feb";
        months[2] = "Mar";
        months[3] = "Apr";
        months[4] = "May";
        months[5] = "Jun";
        months[6] = "Jul";
        months[7] = "Aug";
        months[8] = "Sep";
        months[9] = "Oct";
        months[10] = "Nov";
        months[11] = "Dec";
    }
    return months[num];
}

$drx.prototype.returnEmpty = function(num) {
    if (num == 0) {
        return 'display:none';
    }
}

function sb(value) {
    this.strings = new Array("");
    this.append(value);
}


sb.prototype.append = function(value) {
    if (value) {
        this.strings.push(value);
    }
}

sb.prototype.clear = function() {
    this.strings.length = 1;
}


sb.prototype.toString = function() {
    return this.strings.join("");
}

$drx.prototype.lightBox = function(string, id, parentid, width) {
    var lb = document.createElement('div');
    lb.setAttribute('class', 'drx_lightbox');
    lb.setAttribute('className', 'drx_lightbox');
    lb.id = id;
    if (document.all) {
        lb.style.height = document.body.offsetHeight + 'px';
        lb.style.width = document.body.offsetWidth + 'px';
    }
    else {
        if (document.body.offsetHeight > $drx.getEl('pageContainer').offsetHeight) {
            lb.style.height = document.body.offsetHeight + 'px';
        }
        else {
            lb.style.height = $drx.getEl('pageContainer').offsetHeight + 'px';
        }
        lb.style.width = document.body.offsetWidth + 'px';
    }
    document.body.appendChild(lb);

    var content = document.createElement('div');
    content.setAttribute('class', 'drx_lightbox_content');
    content.setAttribute('className', 'drx_lightbox_content');
    content.id = id + '_content';
    content.innerHTML = string;
    content.style.width = width + 'px';
    var parent;
    if ($drx.getEl('pageWrapper') != null)
        parent = $drx.getEl('pageWrapper');
    else
        parent = document.body;
        
    parent.appendChild(content);

    $drx.getEl(id + '_content').style.marginLeft = parseFloat(-content.offsetWidth / 2) + 'px';
    self.scrollTo(0, 0);

}


$drx.prototype.focuschange = function(event, obj, tag) {
    if ($drx.keyEvent(event, 40)) {
        if (obj.nextSibling) {
            obj.nextSibling.focus();
        }
        else {
            obj.parentNode.getElementsByTagName(tag)[0].focus();
        }
    }
    if ($drx.keyEvent(event, 38)) {
        if (obj.previousSibling) {
            obj.previousSibling.focus();
        }
        else {
            obj.parentNode.getElementsByTagName(tag)[obj.parentNode.getElementsByTagName(tag).length - 1].focus();
        }
    }

}

$drx.prototype.moveTooltip = function() {
    if (this.getEl(this.tooltipId)) {
        this.getEl(this.tooltipId).style.top = this.tempY - (this.getEl(this.tooltipId).offsetHeight / 2) + 'px';
        this.getEl(this.tooltipId).style.left = this.tempX + 10 + 'px';
    }
}
$drx.prototype.hidetooltip = function() {
    if (this.getEl(this.tooltipId))
        document.body.removeChild(this.getEl('tooltip'));
}
$drx.prototype.tooltipUI = function(obj) {
    var sb = new Array();
    sb.push('<table style=\"border-collapse:collaspe;z-index:' + this.tooltipZindex + '\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr>');
    sb.push('<td>');
    var counter = this.toolarrowWidth;
    for (var a = 1; a < counter + 1; a++) {
        var x;
        if (a <= Math.floor(counter / 2)) {
            x = a;
        }
        else {
            x = parseFloat(counter - a + 1);
        }
        sb.push('<span style=\"background-color:' + this.tooltipColorOuter + ';clear:both;display:block;font-size:0px;float:right;height:1px;line-height:0px;overflow:hidden;width:' + x + 'px;\"><\/span>');
    }
    sb.push('<\/td>');
    sb.push('<td style=\"border:' + this.tooltipColorOuterBorder + 'px ' + this.tooltipColorOuter + ' solid\">');
    sb.push('<div style=\"border:' + this.tooltipColorInnerBorder + 'px #fff solid;background-color:' + this.tooltipColorInner + ';line-height:15px;font-family:arial;padding:10px;\">' + this.tooltipText + '<\/div>');
    sb.push('<\/td>');
    sb.push('<\/tr><\/tbody><\/table>');
    return sb.join('');
}
$drx.prototype.showtooltip = function(obj, e, text, removeselect) {
    //$mlg.tr('Mouseover', 'mouseover', obj.innerHTML);
    var selects = document.getElementsByTagName('select');
    if (removeselect) {
        for (var s = 0; s < selects.length; s++) {
            selects[s].style.visibility = 'hidden';
        }
    }
    if (text == null) {
        this.tooltipText = obj.title;
    }
    else {
        this.tooltipText = text;
    }
    obj.title = '';
    obj.onmousemove = function(e) {
        if ($drx.getEl('tooltip')) {
            $drx.getMousePosition(e);
            $drx.moveTooltip();
        }
    }
    obj.onmouseout = function() {
        if ($drx.getEl('tooltip')) {
            $drx.hidetooltip();
            this.title = $drx.tooltipText;
            var selects = document.getElementsByTagName('select');
            for (var s = 0; s < selects.length; s++) {
                selects[s].style.visibility = '';
            }
        }
    }
    this.getMousePosition(e);
    var tooltip = document.createElement('div');
    tooltip.id = this.tooltipId;
    tooltip.style.position = 'absolute';
    tooltip.style.width = this.tooltipWidth;
    tooltip.style.zIndex = this.tooltipZindex;
    var sb = new Array();
    sb.push(this.tooltipUI(obj));
    tooltip.innerHTML = sb.join('');
    document.body.appendChild(tooltip);
    this.moveTooltip();

}

$drx.prototype.setOpacity = function(value) {
    var obj = $drx.getEl('this.tooltipId');
    obj.style.opacity = value / 100;
    obj.style.filter = 'alpha(opacity=' + value * 1 + ')';
}

$drx.prototype.getMousePosition = function(e) {
    if (document.all) {
        this.tempX = event.clientX + document.body.scrollLeft;
        this.tempY = event.clientY + document.documentElement.scrollTop;
    }
    else {
        this.tempX = e.pageX;
        this.tempY = e.pageY;
    }

    if (this.tempX < 0)
        this.tempX = 0;
    if (this.tempY < 0)
        this.tempY = 0;
    return true
}
$drx.prototype.getID = function(id) {
    if (document.getElementById(id))
        return document.getElementById(id);
    else
        return false;
}
$drx.prototype.getEl = function(obj) {
    if (this.getID(obj))
        return this.getID(obj);
}

// Date Drop Down Validation
function ValidDate(day, month, year) {
    if ((month == 2) && (day > 29))
        return false;
    else if ((month == 2) && (day == 29) && (year % 4 != 0))
        return false;
    else if ((day == 31) && ((month == 4) || (month == 6) || (month == 9) || (month == 11)))
        return false;
    else if ((month > 0) && (month <= 12) && (day > 0) && (day <= 31) && (year > 0))
        return true;
    else
        return false;
}

function showSection(id, obj) {
    var getDiv = obj.parentNode.getElementsByTagName('div')[0];
    if (getDiv) {
        if (getDiv.style.display == "none")
            getDiv.style.display = "";
        else
            getDiv.style.display = "none";
    }
    else {
        window.location = "#" + id;
    }
}

function backTop() {
    window.location = "#pageWrapper";
    var lnkSection = $get('lnkSection');
    var hideDiv = lnkSection.getElementsByTagName('div');
    if (hideDiv[0]) {
        for (h = 0; h < hideDiv.length; h++) {
            hideDiv[h].style.display = "none";
        }
    }
}

function GetRadioCheckedValue(id) {
    if ($get(id)) {
        var radios = $get(id).getElementsByTagName('input');
        if (radios) {
            for (var i = 0; i < radios.length; i++) {
                if (radios[i].checked)
                    return radios[i].parentNode.lastChild.innerHTML;
            }
        }
    }

    return '';
}
