﻿var timetoclose;

var tmpMsg = null;
function MsgBox(title, text) {
    if (tmpMsg != null){
        MsgBoxClose();
    };
    window.amLoadMask();
    var height = 150;
    var width = 225;
    var msg = document.createElement('div');
    msg.setAttribute('id', 'asmsgbox');
    msg.style.display = 'block';
    msg.style.position = 'absolute';
    var msgHd = document.createElement('div');
    msgHd.setAttribute('id','asmsgboxhd');
    var msgHdTxt = document.createElement('div');
    msgHdTxt.setAttribute('id','asmsgboxhdtxt');
    msgHdTxt.setAttribute('style','float:left');
    msgHdTxt.innerHTML = title;
    var msgClose = document.createElement('div');
    msgClose.setAttribute('style', 'float:left;');
        if (asImgClose != '') {
        var cImg = new Image();
        cImg.src = asImgClose;
        cImg.setAttribute('style', 'float:right;margin-top:3px;margin-right:3px;cursor:pointer;');
        cImg.style.cursor = 'pointer';
        cImg.onclick = function() { MsgBoxClose(); };
        cImg.setAttribute('align', 'right');
        msgClose.appendChild(cImg);
    };
    msg.appendChild(msgHd);
    msgHd.appendChild(msgClose);
    msgHd.appendChild(msgHdTxt);



    var msgTxt = document.createElement('div');
    msgTxt.setAttribute('id', 'asmsgboxtxt');
    msgTxt.innerHTML = text;
    msg.appendChild(msgTxt);
    var dimensions = getDimensions()
    var winWidth = dimensions.w;
    var divTop = 0;
    
        divTop = (asGetViewportHeight() / 2) - (height / 2);
        if (document.all) {
            divTop += top.document.documentElement.scrollTop;
        } else {
            divTop += top.pageYOffset;
        };
   
    var divLeft = (winWidth / 2) - (width / 2);
    if (msg == null) {
        alert(title);
        return false;
    };
    msg.style.height = height + "px";
    msg.style.width = width + "px";
    msg.style.zIndex = 300000;
    msg.style.display = 'block';
    msg.style.left = divLeft + 'px';
    msg.style.top = divTop + 'px';
    document.body.appendChild(msg);
    tmpMsg = msg;
    
    //timetoclose = setTimeout('MsgBoxClose()', 3000);
};
function MsgBoxClose() {
    if (tmpMsg != null){
        document.body.removeChild(tmpMsg);
        tmpMsg = null;
        window.amCloseMask()
        clearTimeout(timetoclose);
    };
    
};

function asgroupsubsave(div) {
    var cnt = document.getElementById('asgroupsubs');
    var obj = cnt.getElementsByTagName('input');
    var props = '';
    for (var i = 0; i < obj.length; i++) {
        var el = obj[i];
        if (el.type == 'checkbox') {
            if (el.checked) {
                var tmp = el.id;
                tmp = tmp.substring(el.id.indexOf('chkSub_') + 7);
                props += tmp + ';';
            };
        };

    };

    window.asDoCallback('groupsubsave', props);

    window[div].Toggle();
};
function asGetTipPosition(e) {
    var left = 0;
    var top = 0;
    while (e.offsetParent) {
        left += e.offsetLeft;
        top += e.offsetTop;
        e = e.offsetParent;

    };
    left += e.offsetLeft;
    top += e.offsetTop;
    return { x: left, y: top };
};
function asBuildThumbViewer() {
    document.getElementById('txtLink').disabled = false;
    document.getElementById('btnPreview').disabled = false;
    document.getElementById('btnPreview').value = 'Preview';
    if (window.asImg == null) { return false; };
    var divcmt = document.getElementById("divWebComment");
    divcmt.style.display = '';
    if (window.asImg != null) {
        var thumb = document.getElementById("asThumbImg");
        if (thumb != null) {
            var imgDisplay = document.getElementById("asthumbcount");
            if (asImg.length > 0) {
                imgDisplay.innerHTML = '1 of ' + (asImg.length);
                var tmpImg = new Image()
                var img = eval('asImg0');
                tmpImg.src = eval('asImg0').src; //[0]; //asThumbs[0];
                if (img.width >= 120) {
                    img.width = 120;
                } else {
                    thumb.width = img.width;
                };
                thumb.src = img.src;  //asThumbs[0];
                var hidImg = document.getElementById("hidThumb");
                hidImg.value = thumb.src;
            } else {
                thumb.src = '';
            };
        };

    };
};

function asGetThumb(idx) {
    if (asImg != null) {
        if (asImg.length > 0) {
            var thumb = document.getElementById("asThumbImg");
            var imgDisplay = document.getElementById("asthumbcount");
            for (var i = 0; i <= asImg.length; i++) {
                var tmpImg = eval(asImg[i]);
                if (thumb.src == tmpImg.src) {
                    idx = i + idx
                    if (idx > asImg.length - 1) {
                        idx = 0
                    } else if (idx < 0) {
                        idx = asImg.length - 1
                    };
                    imgDisplay.innerHTML = (idx + 1) + ' of ' + (asImg.length);
                    tmpImg = eval(asImg[idx]);
                    if (tmpImg.width >= 120) {
                        thumb.width = 120;
                    } else {
                        thumb.width = tmpImg.width;
                    };
                    thumb.src = tmpImg.src;
                    var hidImg = document.getElementById("hidThumb");
                    hidImg.value = thumb.src;

                    break;
                };
            };

        };
    };
};
function asCheckTabs(params) {
    var i = 0;
    for (i = 0; i <= params.length; i++) {

        if (params[i] != '' && params[i] != undefined) {
            if (params[i].split('=')[0] == 'showtab') {
                var tab = document.getElementById('div' + params[i].split('=')[1]);
                window.am_toggleTab(tab)
                break;
            };
        };

    };

};

function asJournalToggleCtl(ctl) {
    var div = document.getElementById(ctl);
    var cnt = document.getElementById('asjournalctl');
    var obj = cnt.getElementsByTagName('div');
    for (var i = 0; i < obj.length; i++) {
        var el = obj[i];
        if (el.id.indexOf('ctlas') >= 0 && el.id != div.id) {
            el.style.display = 'none';
        };
    };
    if (div.style.display == 'none') {
        div.style.display = '';
    } else {
        div.style.display = 'none';
    };
};

function toggleActionEnd(modal) {
    var pnlStart = document.getElementById("pnlStart");
    var pnlEnd = document.getElementById("pnlEnd");
    var pnlButtons = document.getElementById("pnlButtons");
    pnlStart.style.display = 'none';
    pnlEnd.style.display = '';
    pnlButtons.style.display = 'none';
    if (window.asCurrentModal != null) {
        if (window.asModalTemp != null) {
            window.asModalTemp.closeTimer();
        };
        //window.closeOnTimer();

    };
};

function toggleSendMessage(tmpElem) {
    if (window.asCurrentModal != null) {
        var modal = document.getElementById(window.asCurrentModal);
        modal.style.height = "80px";
        var pnlSendMessage = document.getElementById("pnlSendMessage");
        var pnlMessageSent = document.getElementById("pnlMessageSent");
        pnlSendMessage.style.display = 'none';
        pnlMessageSent.style.display = 'block';
        if (window.asModalTemp != null) {
            window.asModalTemp.closeTimer();
        } else {
        window.closeOnTimer();
        };
        
    };
};


function getScrollWidth() {
    var w = document.body.scrollWidth ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;

    return w ? w : 0;
};

function getScrollHeight() {
    var h = document.body.scrollHeight ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;

    return h ? h : 0;
};
function asMsgBox(type, msg, auto) {
    alert(msg);
};
function asReqClass(obj) {
    obj.className = obj.className + '_asreq';
};



////////////////////////////////////////////
//Private Message
////////////////////////////////////////////

function IsChecked() {


    var ret = false;
    var inputElements = document.getElementsByTagName('input');
    for (var i = 0; i < inputElements.length; i++) {
        var myElement = inputElements[i];

        // Filter through the input types looking for checkboxes
        if (myElement.type == "checkbox") {
            var id = myElement.id;

            if (myElement.checked == true && id.indexOf('chkInboxChkSelect') > 0) return true;

        }
    }
    return ret;
}

function IsCheckedRead() {


    var ret = false;
    var inputElements = document.getElementsByTagName('input');
    for (var i = 0; i < inputElements.length; i++) {
        var myElement = inputElements[i];

        // Filter through the input types looking for checkboxes
        if (myElement.type == "checkbox") {
            var id = myElement.id;

            if (myElement.checked == true && id.indexOf('chkInboxChkSelect') > 0 && ReadStatus(id) == "True") return true;

        }
    }
    return ret;
}
function IsCheckedUnRead() {


    var ret = false;
    var inputElements = document.getElementsByTagName('input');
    for (var i = 0; i < inputElements.length; i++) {
        var myElement = inputElements[i];

        // Filter through the input types looking for checkboxes
        if (myElement.type == "checkbox") {
            var id = myElement.id;

            if (myElement.checked == true && id.indexOf('chkInboxChkSelect') > 0 && ReadStatus(id) == "Fals") return true;

        }
    }
    return ret;
}
function ControlButton(btnID) {

    var chkValue = IsChecked();

    var btn = document.getElementById(btnID);
    if (btn != null) {

        btn.disabled = !chkValue;
    }
}

function ControlMarkAsReadButton(btnID1) {

    var chkValue = IsCheckedUnRead();

    var btn1 = document.getElementById(btnID1);
    if (btn1 != null) {
        btn1.disabled = !chkValue;
    }
}
function ControlMarkAsUnReadButton(btnID1) {

    var chkValue = IsCheckedRead();

    var btn1 = document.getElementById(btnID1);
    if (btn1 != null) {
        btn1.disabled = !chkValue;
    }
}
function ReadStatus(id) {

    var val = "False";
    if (msgTable != 'undefined') {
        var pos = msgTable.indexOf(id)
        if (pos >= 0) {
            val = msgTable.substr(msgTable.indexOf(":", pos + 1) + 1, 4);
            return val;

        }
    }

    return val;
}


function CheckAllCheckBoxes(drdID) {

    var IndexValue = drdID.selectedIndex;
    var inputElements = document.getElementsByTagName('input');

    for (var i = 0; i < inputElements.length; i++) {
        var myElement = inputElements[i];

        // Filter through the input types looking for checkboxes
        if (myElement.type == "checkbox" && myElement.id.indexOf('chkInboxChkSelect') > 0) {
            var id = myElement.id;
            switch (IndexValue) {
                case 0: break;
                case 1: myElement.checked = false; break; //None
                case 2: myElement.checked = (ReadStatus(id) == "True"); break;  //Read
                case 3: myElement.checked = (ReadStatus(id) == "Fals"); break; //UnRead - Fals=False
                case 4: myElement.checked = true; break;

            }
        }
    }
}

//function clearDefault(el) {
//    if (el.defaultValue == el.value) el.value = "";
//};

function SetDefault(el) {
    el.value = el.defaultValue;
};
function onlyNumbers(evt) {
    var charCode = (evt.which != undefined) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode != 46) && (charCode != 44))
        return false;
    return true;
};

function asSetCurPosition(elemId, curPos) {
    //curPos = 0: Zero will put the cursor at the beginning
    //curPos = 1:  will put the cursor at the end of the text
    var elem = document.getElementById(elemId);

    if (elem != null) {
        if (elem.createTextRange) {
            var range = elem.createTextRange();
            if (curPos == "0") {
                range.move('character', 0);
            }
            else
            { range.move('character', elem.innerHTML.length); }
            range.select();
        }
        else {
            if (elem.selectionStart) {
                elem.focus();
                if (curPos == "0") {
                    elem.setSelectionRange(0, 0);
                }

            }
            else
                elem.focus();
        }
    }
}


function asEventToggleCtl(ctl) {
    var div = document.getElementById(ctl);
    if (div.style.display == 'none') {
        div.style.display = '';
    } else {
        div.style.display = 'none';
    };
};
function amLoadMask() {
    var mDiv = document.createElement('div');
    mDiv.setAttribute('id', 'ammodalmask');
    mDiv.style.position = 'absolute';
    mDiv.style.top = '0px';
    mDiv.style.left = '0px';
    mDiv.style.width = getScrollWidth();
    mDiv.style.height = getScrollHeight();
    mDiv.style.backgroundColor = '#f5f5f5';
    mDiv.style.filter = 'alpha(opacity=50)';
    mDiv.style.opacity = '.5';
    mDiv.style.display = '';
    mDiv.style.zIndex = '1000';
    //    if (actDiv != null) {
    //        mDiv.appendChild(actDiv);
    //    };
    document.body.appendChild(mDiv);
};
function amCloseMask() {
    var mm = document.getElementById('ammodalmask');
    if (mm != null || mm != undefined) {
        var tmp = mm.parentNode
        tmp.removeChild(mm);
    };
};
function asGetViewportHeight() {
    if (window.innerHeight != window.undefined) return window.innerHeight;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight;

    return window.undefined;
};
var tmpElem = null;
var pViewTimer;
function closePview() {
    var pv = document.getElementById("asPview");
    pv.style.display = 'none';
    pv.style.left = '0px';
    pv.style.top = '0px';
    clearTimeout(pViewTimer);
};
function hidePview() {
    pViewTimer = setTimeout('closePview()', 1000);

};
function cancelPclose() {
    clearTimeout(pViewTimer);
};
function asClearStatus(el) {
    tmpElem = el;
    cbMainAction = 'statclear';
    asDoCallback('statclear');
};
function cbMainComplete() {
    if (cbMainAction != '') {
        switch (cbMainAction) {
            case 'friendsave':
                if (tmpElem != null) {
                    var result = document.getElementById('cbMainResult');
                    if (result != null) {

                        tmpElem.innerHTML = result.innerHTML;
                    };
                    tmpElem = null;
                };
                break;
            case 'ascf':
                if (tmpElem != null) {
                    var result = document.getElementById('cbMainResult');
                    if (result != null) {
                        if (result.innerHTML == '') {
                            var p = tmpElem.parentNode;
                            p.removeChild(tmpElem);
                        } else {
                            tmpElem.innerHTML = result.innerHTML;
                        };

                    };
                    tmpElem = null;
                };
                break;
            case 'asif':
                if (tmpElem != null) {
                    var result = document.getElementById('cbMainResult');
                    if (result != null) {
                        if (result.innerHTML == '') {
                            var p = tmpElem.parentNode;
                            p.removeChild(tmpElem);
                        };

                    };
                    tmpElem = null;
                };
                break;
            case 'asrf':
                if (tmpElem != null) {
                    var result = document.getElementById('cbMainResult');
                    if (result != null) {
                        if (result.innerHTML == '') {
                            var p = tmpElem.parentNode;
                            p.removeChild(tmpElem);
                        } else {
                            tmpElem.innerHTML = result.innerHTML;
                        };

                    };
                    tmpElem = null;
                };
                break;
            case 'siteinvite':
                var sending = document.getElementById("asSendingMessage");
                sending.style.display = 'none';
                var sent = document.getElementById("asMessageSent");
                if (sent != null) {
                    sent.style.display = '';
                };
                timetoclose = setTimeout('asCloseInvite()', 2000);
                break;
            case 'asgroupinvite':
                var sending = document.getElementById("asMsgBefore");
                if (sending != null) {
                    sending.style.display = 'none';
                };

                var sent = document.getElementById("asMsgAfter");
                if (sent != null) {
                    sent.style.display = '';
                };
                timetoclose = setTimeout('asCloseGen()', 2000);
                window.location.href = window.location.href;
                break;
            case 'statclear':
                if (tmpElem != null) {
                    var p = tmpElem.parentNode;
                    tmpElem.style.display = 'none';

                    p.firstChild.nodeValue = '';

                    tmpElem = null;
                };
                break;

        };
        cbMainAction = '';
    };
};
function asToggle(id, obj) {
    var div = document.getElementById(id);
    var p = div.parentNode;
    var ul = obj.parentNode;
    var divs = p.getElementsByTagName('div');
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].id.indexOf('divAs') > 0) {
            divs[i].style.display = 'none';
        };
    };
    var lis = ul.getElementsByTagName('li');
    for (var i = 0; i < lis.length; i++) {
        lis[i].removeAttribute('class');
        lis[i].className = '';
    };
    div.style.display = 'block';
    obj.className = 'aslinavsel';
};
function amClose() {
    if (asModalTemp != null) {
        asModalTemp.Close();
    } else {
        var mm = document.getElementById('ammodalmask');
        if (mm != null || mm != undefined) {
            var tmp = mm.parentNode
            tmp.removeChild(mm);
        };
        if (asCurrentModal != null) {
            var mTable = document.getElementById(asCurrentModal + 'ammodalshell');
            mTable.style.display = 'none';
            var bb = document.getElementById('ammodalbuttonbar');
            var modalBody = document.getElementById(asCurrentModal + 'ammodalbody');
            if (bb != null || bb != undefined) {
                modalBody.removeChild(bb);
            };
            clearTimeout(timetoclose);
            asCurrentModal = null;
            asModalTemp = null;
        };
    };
};
