// JavaScript source code
var xmlhttp;
function post(obj) {
    var ready = false;
    if (xmlhttp != null) {
        if (xmlhttp.readyState == 4) {
            //xmlhttp.abort();
            ready = true;
            console.log("post ready");
        }
    }
    if (!ready) {
        console.log("post not ready");
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (typeof (callbackAll) != "undefined")
                callbackAll(xmlhttp);
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var obj = xmlhttp.responseText;
                var json = eval('(' + obj + ')');
                if (json.state == "ok") {
                    callback(obj);
                } else if (json.state == "ng") {
                    if ((json.msg.indexOf('{0}') < 0) && (json.cmd != "idel"))
                        showMsg(json.msg);
                    else
                        callback(obj);
                }
                else {
                    alert(json.msg);
                    window.top.location = '/';
                }
                //timercount--;
            }
        }
    }
    xmlhttp.open("POST", "none", true);
    xmlhttp.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
    xmlhttp.send(obj.cmd);

    //alert("postit");
    //restarttimer();
    //timercount++;
}

//var httpInterval;
//var timercount = 0;
function postit(cmd) {
    var obj = {};
    obj.cmd = "cmd=" + cmd;
    post(obj);
    console.log(cmd);
}
function MyPostit(cmd) {
    var selected = window.top.frames.main.frames.mainWin.frames.ifrmBottom;
    if (cmd.startWith("screencomcmd") || cmd.startWith("drag")) {
        if (selected != null)
            cmd += "&addressFrmTo=" + selected.selectedWin;
    }
    postit(cmd);
}
function postidel() {
    postit("idel");
}
function postidelpause() {
    postit("idelpause");
}

//function stoptimer() {
//    clearInterval(httpInterval);
//}
//function starttimer() {
//    httpInterval = setInterval("timeout()", 3000);
//}
//function restarttimer() {
//    stoptimer();
//    starttimer();
//}
//function timeout() {
//    clearInterval(httpInterval);
//    alert("timeout");
//}

function closeMe() {
    postit("logout");
    var win = window.open("", "_self");
    win.close();
}
function myConfirm(obj) {
    var ret = true;
    postidelpause();
    if (!confirm(obj)) {
        ret = false;
    }
    postidel();
    return ret;
}
function myInputDialog(title) {
    var ret;
    postidelpause();
    ret = prompt(title, "");
    postidel();
    return ret;
}
function setCookie(name, val) {
    document.cookie = name + "=" + val;
}
function getCookie(sName) {
    var sRE = "(?:;.)?" + sName + "=([^;]*);?";
    var oRE = new RegExp(sRE);
    if (oRE.test(document.cookie)) {
        if (RegExp["$1"].indexOf(',') > 0)
            return this.getCookieNet(sName);
        return decodeURIComponent(RegExp["$1"]);
    } else {
        return null;
    }
}
String.prototype.getQuery = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = this.substr(this.indexOf("\?") + 1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}
String.prototype.getCookie = function (sName) {
    var sRE = "(?:;.)?" + sName + "=([^;]*);?";
    var oRE = new RegExp(sRE);
    if (oRE.test(document.cookie)) {
        if (RegExp["$1"].indexOf(',') > 0)
            return this.getCookieNet(sName);
        return decodeURIComponent(RegExp["$1"]);
    } else {
        return null;
    }
}
String.prototype.getCookieNet = function (sName) {
    var sRE = "(?:,.)?" + sName + "=([^,]*);?";
    var oRE = new RegExp(sRE);
    if (oRE.test(document.cookie)) {
        return decodeURIComponent(RegExp["$1"]);
    } else {
        return null;
    }
}
String.prototype.format = function (args) {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}
String.prototype.temp = function (obj) {
    return this.replace(/\$\w+\$/gi, function (matchs) {
        var returns = obj[matchs.replace(/\$/g, "")];
        return (returns + "") == "undefined" ? "" : returns;
    });
};
String.prototype.replaceAll = function (reallyDo, replaceWith, ignoreCase) {
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi" : "g")), replaceWith);
    } else {
        return this.replace(reallyDo, replaceWith);
    }
};
String.prototype.trim = function () {
    return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
String.prototype.endWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length)
        return false;
    if (this.substring(this.length - str.length) == str)
        return true;
    else
        return false;
    return true;
}
String.prototype.startWith = function (str) {
    if (str == null || str == "" || this.length == 0 || str.length > this.length)
        return false;
    if (this.substring(0, str.length) == str)
        return true;
    else
        return false;
    return true;
}
var msgTimer;
var msgObj;
var topMain = window.top.frames.main;
function showMsg(msg) {
    showMsgWithTitle(msg, null);
}
clearShowedMsg();
function clearShowedMsg() {
    if (msgTimer != null)
        topMain.clearTimeout(msgTimer);
}
function showMsgWithTitle(msg, title) {
    if (title == null)
        title = "<%=警&nbsp;&nbsp;告>";
    console.log("showme " + window.top.frames.main);
    msgObj = topMain.document.getElementById("divMsg");
    var html =
"                <div id=\"divmain\" width=\"100%\">" +
"                    <b class=\"xtop\">" +
"                        <b class=\"mainxb1\" style=\"background: #000000;\"></b>" +
"                        <b class=\"mainxb2\" style=\"background: #000000;\"></b>" +
"                        <b class=\"mainxb3\" style=\"background: #000000;\"></b>" +
"                        <b class=\"mainxb4\" style=\"background: #000000;\"></b>" +
"                    </b>" +
"                    <div class=\"maicontent\" style=\"background: #000000;\">" +
"                        <table style=\"color:white;width:100%\">" +
"                            <tr>" +
"                                <td align=\"center\">" +
"                                    <div id=\"info\" style=\"text-align: center;\">" + title + "</div>" +
"                                </td>" +
"                            </tr>" +
"                        </table>" +
"                    </div>" +
"                    <div class=\"maicontent\" style=\"background: #FF0000;\">" +
"                        <table style=\"color:white;width:100%\">" +
"                            <tr>" +
"                                <td align=\"center\">" +
"                                    <div id=\"info\" style=\"text-align: center;\"><br>" + msg + "<br><br></div>" +
"                                </td>" +
"                            </tr>" +
"                        </table>" +
"                    </div>" +
"                    <b class=\"xbottom\">" +
"                        <b class=\"mainxb4\" style=\"background: #FF0000;\"></b>" +
"                        <b class=\"mainxb3\" style=\"background: #FF0000;\"></b>" +
"                        <b class=\"mainxb2\" style=\"background: #FF0000;\"></b>" +
"                        <b class=\"mainxb1\" style=\"background: #FF0000;\"></b>" +
"                    </b>" +
"                </div>"

    if (msgObj == null) {
        var o = topMain.document.body;
        //创建DIV 
        var div = topMain.document.createElement("div");
        div.id = "divMsg";
        div.setAttribute("count", 0);
        //div.onclick = function () { setTimeout(msgTimer); };
        div.className = "msg";
        //div.innerHTML = msg;
        div.innerHTML = html;
        o.appendChild(div);
    } else {
        topMain.clearTimeout(msgTimer);
        msgObj.innerHTML = html;
        msgObj.setAttribute("count", 0);
        msgObj.style.display = "block";
    }

    var timeout = function () {
        msgObj = topMain.document.getElementById("divMsg");
        var count = msgObj.getAttribute("count");
        //console.log("hello" + msgObj);
        if (msgObj.style.display == "none") {
            msgObj.setAttribute("count", ++count);
            msgObj.style.display = "block";
        }
        else
            msgObj.style.display = "none";
        if (count >= 2) {
            topMain.clearTimeout(msgTimer);
            msgObj.style.display = "none";
        }
    }
    if (msgTimer != null)
        topMain.clearTimeout(msgTimer);
    msgTimer = topMain.setInterval(timeout, 1000);
    //msgTimer = setTimeout(timeout, 1000);
}
var watchDogTimer;
var watchDogIndex = 0;
function wow() {
    console.log("wow");
    try {
        if (watchDogIndex == 0) {
            watchDogIndex++;
            topMain.frames.mainWin.frames.ifrmBottom.postit("dragInit");
        }
        else {
            topMain.frames.mainWin.frames.ifrmTop.onInit();
            clearInterval(watchDogTimer);
        }
    }
    catch (e) {
    }
}
function watchDog() {
    console.log("watchDog");
    var screen = topMain.frames.mainWin.frameElement.src;
    var pageName = screen.split('/');
    //console.log(screen);
    //console.log(pageName);
    if (pageName.length > 4) {
        if ("ScreenFrame.html" == pageName[4]) {
            console.log("Yes " + topMain.frames.mainWin.frames.ifrmTop.frameElement.src);
            watchDogIndex = 0;
            watchDogTimer = setInterval(wow, 1000);
        }
    }
}
function checkMsg() {
    msgObj = topMain.document.getElementById("divMsg");
    if (msgObj != null) {
        console.log(msgTimer);
        if (msgTimer == null) {
            msgObj.style.display = "none";
        }
    }
}
function isYMD(val) {
    var reg = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$/;
    var reg02 = /^(?:(?!0000)[0-9]{4}\/(?:(?:0[1-9]|1[0-2])\/(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])\/(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)\/02\/29) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$/;
    return (reg.test(val) || reg02.test(val));
}
function isNum(val) {
    var reg = /^[1-9]*[1-9][0-9]*$/;
    return reg.test(val);
}
function focusAndSelect(obj) {
    obj.focus();
    obj.select();
}