﻿
function ContactAdmin() {
    window.open("ContactAdmin.aspx", "_blank", "resizable=no,menubar=no,status=no,toolbar=no,scrollbars=false,width=500,height=300");
}

//

function PreviewEmail(tag, addText) {
    var url = 'PreviewEmail.aspx?t=' + tag + "&a=" + addText;
    window.open(url, "_ResendEmail", "width=800,height=600,resizable=yes,status=no,toolbar=no,scrollbars=yes");
}

//

function DoPrint() {
    if (window.print)
        window.print();
}

function ConfirmDelete(sType) {
    return confirm("Are you sure you wish to DELETE this " + sType + "?");
}

//

function getObjectPos(obj, sPos) {
    var iPos = 0;
    while (obj != null) {
        iPos += obj["offset" + sPos];
        obj = obj.offsetParent;
    }
    return iPos;
}

//

function PopupDBImage(objImg, sPopupImgId, sTable, sField, sID, sScale) {
    var imgUrl = "GetDBImage.aspx?t=" + sTable + "&f=" + sField + "&id=" + sID + "&scale=" + sScale;

    var popupImage = document.getElementById(sPopupImgId);
    popupImage.src = imgUrl;
    popupImage.style.left = getObjectPos(objImg, "Left") - 400 + "px";
    popupImage.style.top = getObjectPos(objImg, "Top") - 50 + "px";
    popupImage.style.visibility = 'visible';
}

function HideDBImage(sImgObj) {
    var popupImage = document.getElementById(sImgObj);
    popupImage.style.visibility = 'hidden';
}

//

function onSigLoad() {

    var sDivId;
    var browseWidth, browseHeight;

    if (document.layers || (document.getElementById && !document.all)) {
        browseWidth = window.outerWidth;
        browseHeight = window.outerHeight;
    } else if (document.all) {
        browseWidth = document.body.clientWidth;
        browseHeight = document.body.clientHeight;
    }

    var posY = $("#PopupSignatures").position().top;
    var divHeight = $("#PopupSignatures").height();

    var min = document.body.scrollTop;
    var max = document.body.scrollTop + document.body.clientHeight - divHeight;

    if (posY < min)
        posY = min + 10;
    else if (posY > max)
        posY = max - 10;

    var div = document.getElementById("PopupSignatures");
    div.style.top = posY + "px";
}

function PopupSignatureImages(objImg, sDivId, sID) {
    var empImg = document.getElementById('imgEmployeeSignature');
    $("#imgEmployeeSignature").load(onSigLoad);
    empImg.src = "GetDBImage.aspx?t=Faxes&f=EmployeeSignatureImage&id=" + sID;

    var mgrImg = document.getElementById('imgManagerSignature');
    $("#imgManagerSignature").load(onSigLoad);
    mgrImg.src = "GetDBImage.aspx?t=Faxes&f=ManagerSignatureImage&id=" + sID;

    var browseWidth, browseHeight;

    if (document.layers || (document.getElementById && !document.all)) {
        browseWidth = window.outerWidth;
        browseHeight = window.outerHeight;
    } else if (document.all) {
        browseWidth = document.body.clientWidth;
        browseHeight = document.body.clientHeight;
    }

    var y_offset = 120;

    var div = document.getElementById(sDivId);
    div.style.left = getObjectPos(objImg, "Left") + 24 + "px";

    var posY = getObjectPos(objImg, "Top");   //- y_offset;
    div.style.top = posY + "px";

    div.style.visibility = 'visible';

    // shim
    ShowShim(div);
}


function HideSignatureImages(sDivId) {
    var empImg = document.getElementById('imgEmployeeSignature');
    empImg.src = "images/loading.gif";

    var mgrImg = document.getElementById('imgManagerSignature');
    mgrImg.src = "images/loading.gif";

    var div = document.getElementById(sDivId);
    div.style.visibility = 'hidden';

    // shim
    HideShim();
}

//

function PopupComments(objImg, sDivId, sComments) {
    var commentsText = document.getElementById('CommentsSpan');
    SetHtml(commentsText, sComments);

    var browseWidth, browseHeight;

    if (document.layers || (document.getElementById && !document.all)) {
        browseWidth = window.outerWidth;
        browseHeight = window.outerHeight;
    } else if (document.all) {
        browseWidth = document.body.clientWidth;
        browseHeight = document.body.clientHeight;
    }

    var y_offset = 0;

    var div = document.getElementById(sDivId);
    div.style.left = getObjectPos(objImg, "Left") + 24 + "px";

    var posY = getObjectPos(objImg, "Top") - y_offset;
    var min = document.body.scrollTop;
    var divHeight = 100;
    var max = document.body.scrollTop + document.body.clientHeight - divHeight;



    if (posY < min)
        posY = min + 10;
    else if (posY > max)
        posY = max - 10;
    div.style.top = posY + "px";

    div.style.visibility = 'visible';

    // shim
    ShowShim(div);
}

function HideComments(sDivId) {
    var div = document.getElementById(sDivId);
    div.style.visibility = 'hidden';

    // shim
    HideShim();
}

function ShowShim(div) {
    if (div == null)
        return;

    var browser = navigator.appName;
    var b_version = navigator.appVersion;
    var version = parseFloat(b_version);
    if ((browser == "Microsoft Internet Explorer") && (version < 7)) {
        var shim = document.getElementById('iFrameShim');
        if (shim != null) {
            shim.style.left = div.style.left;
            shim.style.top = div.style.top;
            shim.style.width = 400;
            shim.style.height = 600;
            shim.style.display = 'block';
        }
    }
}

function HideShim() {
    var shim = document.getElementById('iFrameShim');
    if (shim != null) {
        shim.style.display = 'none';
    }
}

//

function loadImage(objImg, sTable, sField, sId) {
    //objImg.src = "Images/loading.gif";
    objImg.src = "GetDBImage.aspx?t=" + sTable + "&f=" + sField + "&id=" + sId;
}

//

function SetText(obj, s) {
    if (obj == null)
        return;
    obj.innerText = obj.textContent = s;
}

function SetHtml(obj, s) {
    if (obj == null)
        return;
    obj.innerHTML = s;
}

function TrimString(sString) {
    if (sString == '')
        return '';

    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}

function IsEmpty(strVal) {
    if (strVal == null)
        return true;

    strVal = TrimString(strVal);

    if (strVal == '')
        return true;

    return false;
}

function ASP_getElement(strId) {
    var obj = $('[id$=_' + strId + ']')

    if (obj == null) {
        alert('ERROR: could not find control: [' + strId + ']');
        return null;
    }

    return $(obj)[0];
}

function getElement(strId) {
    var obj = document.getElementById(strId);
    /*
    if( obj == null )
    alert('ERROR: object ' + strId + ' was not found!');
    */
    return obj;
}

function StripWhitespace(s) {
    if (s == null)
        return null;
    s = s.replace(/(\s)+/gi, '');
    return s;
}

function StringToInt(str) {
    while (str.length > 1 && str.substring(0, 1) == '0')
        str = str.substring(1, str.length);
    var i = parseInt(str);
    if (isNaN(i))
        i = 0;
    return i;
}

function IsTimeValue(s) {
    return (TimeToMinutes(s, false) != -1);
}

function IsHourValue(s) {
    return (HoursToMinutes(s) != -1);
}

//

function TimeToMinutes(s, defaultPM) {
    s = StripWhitespace(s);
    s = s.toLowerCase();

    if (!s.match(/^[0-9:apm]+$/, 'gi'))
        return -1;

    var isPM = defaultPM;
    var h = 0;
    var m = 0;

    // get rid of trailing 'm'
    var len = s.length;
    if (len <= 0)
        return -1;
    if (s.substr(len - 1, 1) == 'm' || s.substr(len - 1, 1) == 'M')
        s = s.substr(0, len - 1);

    // check am/pm
    len = s.length;
    if (len <= 0)
        return -1;
    if (s.substr(len - 1, 1) == 'p' || s.substr(len - 1, 1) == 'P') {
        s = s.substr(0, len - 1);
        isPM = true;
    }
    else if (s.substr(len - 1, 1) == 'a' || s.substr(len - 1, 1) == 'A') {
        s = s.substr(0, len - 1);
        isPM = false;
    }

    if (s.indexOf(':') >= 0) {
        var cols = s.split(':');
        h = StringToInt(cols[0]);
        m = StringToInt(cols[1]);
    }
    else {
        h = StringToInt(s);
    }

    if (isNaN(h) || isNaN(m))
        return -1;

    if (h < 0 || h > 23 || m < 0 || m > 59)
        return -1;

    if (h == 12) {
        if (!isPM)
            h = 0;
    }

    if (isPM) {
        if (h < 12)
            h = h + 12;
    }

    m = (h * 60) + m;

    return m;
}

function HoursToMinutes(s) {
    s = StripWhitespace(s);
    s = s.toLowerCase();

    if (!s.match(/^[0-9:.]+$/, 'gi'))
        return -1;

    var h = 0;
    var m = 0;
    var ss = 0;

    if (s.indexOf(':') >= 0) {
        var cols = s.split(':');
        h = StringToInt(cols[0]);
        m = StringToInt(cols[1]);
        if (cols.length == 3) {
            ss = StringToInt(cols[2]);
            return parseFloat((h * 60)) + parseFloat(m) + parseFloat((ss / 60).toFixed(2));
        }
        return (h * 60) + m;
    }
    else if (s.indexOf('.') >= 0) {
        return parseFloat((s * 60).toFixed(2));
    }
    else {
        m = StringToInt(s);
        if (m <= 12) {
            h = m;
            m = 0;
            ss = 0;
        }
        m = (h * 60) + m + '.' + ss;
        return parseFloat(m);
    }

    if (isNaN(h) || isNaN(m))
        return -1;
    return 0;
}

function HoursToMinutesNoLimit(s) {
    s = StripWhitespace(s);
    s = s.toLowerCase();

    if (!s.match(/^[0-9:.]+$/, 'gi'))
        return -1;

    var h = 0;
    var m = 0;
    var ss = 0;

    if (s.indexOf(':') >= 0) {
        var cols = s.split(':');
        h = StringToInt(cols[0]);
        m = StringToInt(cols[1]);
        if (cols.length == 3) {
            ss = StringToInt(cols[2]);
            return parseFloat((h * 60)) + parseFloat(m) + parseFloat((ss / 60).toFixed(2));
        }
        return (h * 60) + m;
    }
    else if (s.indexOf('.') >= 0) {
        return parseFloat((s * 60).toFixed(2));
    }
    else {
        h = StringToInt(s);
        m = 0;
        ss = 0;
        m = (h * 60) + m + '.' + ss;
        return parseFloat(m);
    }

    if (isNaN(h) || isNaN(m))
        return -1;
    return 0;
}

//

function SetButtonClass(obj, cls) {
    obj.className == cls;
}

//

function ShowObjectById(objId) {
    var obj = document.getElementById(objId);
    if (obj != null)
        obj.style.display = 'block';
}

function HideObjectById(objId) {
    var obj = document.getElementById(objId);
    if (obj != null)
        obj.style.display = 'none';
}

function CheckBoxIsChecked(chkId) {
    var chk = document.getElementById(chkId);
    if (chk == null)
        return false;
    return chk.checked;
}

function ToggleObjectByCheckBox(chkId, objId) {
    if (CheckBoxIsChecked(chkId)) {
        ShowObjectById(objId);
    }
    else {
        HideObjectById(objId);
    }
}

//

function SetButtonOver(obj, isOver) {
    if (obj.className == 'ButtonTableDisabled')
        return;

    if (isOver)
        obj.className = 'ButtonTableOver';
    else
        obj.className = 'ButtonTable';
}

function DoButtonClick(obj) {
    if (obj.className == 'ButtonTableDisabled')
        return;

    alert('click!');
}

function SetButtonEnabled(obj, isEnabled) {
    if (isEnabled)
        obj.className = 'ButtonTable';
    else
        obj.className = 'ButtonTableDisabled';
}
