﻿function padLeft(str, lenght) {
    if (str.length >= lenght)
        return str;
    else
        return padLeft("0" + str, lenght);
}

String.prototype.Trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
function $$(obj) {
    return document.getElementById(obj);
}


var msgTSpan = 3; //默认跳转时间
var locationHref = "http://tourzj.com"; //默认跳转地址

//创建遮罩层
//flag://错误,//成功,//跳转,//询问,//等待
//content_text:显示内容
//title_text:标题文本,默认为"信息确认",参考"提示信息"
//functionName:点"确认"按钮时,需要调用的函数名称
//parmList:点"确认"按钮时,调用函数的参数列表,多个参数用","隔开,如果是跳转,参数格式{地址,时间}
function CreateWHBox(flag, content_text, title_text, functionName, parmList) {
    //alert(parmList);
    ClearWHBox();
    if (flag == '跳转') {
        locationHref = parmList.split(',')[0];
        msgTSpan = parmList.split(',')[1];
    }
    //如果背景层遮罩不存在，并且设置为遮罩效果，则创建
    if ($$('webhint_box') == null) {
        alert('遮罩层未创建!');
        //        var webhint_box = document.createElement('div');
        //        webhint_box.id = 'webhint_box';
        //        webhint_box.style.backgroundColor = '#DDDDDD';
        //        webhint_box.style.filter = 'Alpha(opacity=50)'; //filter: Alpha(opacity=10);
        //        webhint_box.style.MozOpacity = '0.50';
        //        webhint_box.className = 'cdiv';
        //        webhint_box.style.position = 'absolute';
        //        webhint_box.style.left = '0px';
        //        webhint_box.style.top = '0px';
        //        webhint_box.style.width = Math.max(document.body.scrollWidth, document.documentElement.clientWidth) + 'px';
        //        webhint_box.style.height = Math.max(document.body.scrollHeight, document.documentElement.clientHeight) + 'px';
        //        document.body.appendChild(webhint_box);
        //        $$('webhint_box').style.zIndex = 100;
    }
    else {
        //如果遮罩层存在，并且设置为遮罩效果，则直接显示。
        //        $$('webhint_box').style.display = 'block';
        $$('webhint_box').style.backgroundColor = '#DDDDDD';
        $$('webhint_box').style.filter = 'Alpha(opacity=50)'; //filter: Alpha(opacity=10);
        $$('webhint_box').style.MozOpacity = '0.50';
        $$('webhint_box').className = 'cdiv';
        $$('webhint_box').style.position = 'absolute';
        $$('webhint_box').style.left = '0px';
        $$('webhint_box').style.top = '0px';
        $$('webhint_box').style.width = Math.max(document.body.scrollWidth, document.documentElement.clientWidth) + 'px';
        $$('webhint_box').style.height = Math.max(document.body.scrollHeight, document.documentElement.clientHeight) + 'px';
        //document.body.appendChild(webhint_box);
        $$('webhint_box').style.zIndex = 9999;
        $$('webhint_box').style.display = 'block';
    }
    //设置容器层(显示层,设置之前,必须保证在页面上已经存在该容器)
    CreateWH_Box();
    //创建标题层
    CreateWH_TitleBox(flag, title_text);
    //创建内容层
    CreateWH_ContentBox(flag, content_text);
    //创建等待层
    CreateWH_WaitingBox(flag, content_text);
    //创建按钮层
    CreateWH_BottomBox(flag, functionName, parmList);
    if (flag == '跳转') {
        MsgTimeCount();
    }
}
//创建之前先情况遮罩层
function ClearWHBox() {
    if ($$('webhint_box') != null) {
        //document.body.removeChild($$('webhint_box')); //.style.display = 'none';
        $$('webhint_box').style.display = 'none';
    }
    if ($$('wh_box') != null) {
        //        var divs = $$("wh_box").getElementsByTagName("div");
        //        for (var i = 0; i < divs.length; i++) {
        //            $$("wh_box").removeChild($$(divs[i].id));
        //        }        
        $$("wh_box").innerHTML = '';
        $$("wh_box").style.display = 'none';
    }
}
//删除遮罩层
function DeleteWHBox() {
    if ($$('webhint_box') != null) {
        //document.body.removeChild($$('webhint_box')); //.style.display = 'none';
        $$('webhint_box').style.display = 'none';
    }
    if ($$('wh_box') != null) {
        //        var divs = $$("wh_box").getElementsByTagName("div");
        //        for (var i = 0; i < divs.length; i++) {
        //            $$("wh_box").removeChild($$(divs[i].id));
        //        }        
        $$("wh_box").innerHTML = '';
        $$("wh_box").style.display = 'none';
    }
    if (msgT != null) {
        clearTimeout(msgT);
    }
}

//创建wh_box层
function CreateWH_Box() {
    if ($$('wh_box') == null) {
        alert('显示层未创建!');
        //        var _wh_box = document.createElement('div');
        //        _wh_box.id = 'wh_box';
        //        _wh_box.className = 'wh_box';
        //        _wh_box.style.position = "absolute";
        //        _wh_box.style.left = (document.documentElement.clientWidth / 2 + document.body.scrollLeft - 180) + "px";
        //        var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
        //        _wh_box.style.top = (document.documentElement.clientHeight / 2 + scrollTop - 80) + "px";
        //        _wh_box.style.zIndex = 99999;
        //        document.body.appendChild(_wh_box);
        //        _wh_box.style.display = "block";
    }
    else {
        var _wh_box = document.createElement('div');
        $$('wh_box').className = 'wh_box';
        $$('wh_box').style.position = "absolute";
        $$('wh_box').style.left = (document.documentElement.clientWidth / 2 + document.body.scrollLeft - 180) + "px";
        var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
        $$('wh_box').style.top = (document.documentElement.clientHeight / 2 + scrollTop - 80) + "px";
        $$('wh_box').style.zIndex = 99999;
        //document.body.appendChild(_wh_box);
        $$('wh_box').style.display = "";
    }
}

//创建内容层wh_content
//flag:
//错误 = 0,
//成功 = 1,
//跳转 = 2,
//询问 = 3,
//等待 = 4
function CreateWH_ContentBox(flag, content_text) {
    if ($$('wh_content') == null) {
        var _wh_content = document.createElement('div');
        _wh_content.id = 'wh_content';
        switch (flag) {
            case '错误':
                _wh_content.className = 'wh_content_wrong';
                _wh_content.innerHTML = content_text;
                break;
            case '成功':
                _wh_content.className = 'wh_content_right';
                _wh_content.innerHTML = content_text;
                break;
            case '询问':
                _wh_content.className = 'wh_content_question';
                _wh_content.innerHTML = content_text;
                break;
            case '跳转':
                _wh_content.className = 'wh_content_right';
                _wh_content.innerHTML = content_text;
                _wh_content.innerHTML += '<br/><span id="msgSecond" style="color: Blue;">' + msgTSpan.toString() + '</span>秒后页面将会自动跳转。';
                _wh_content.innerHTML += '<br/><a href="' + locationHref + '">如果没有自动跳转,请点这里。</a>';
                break;
            default:
                _wh_content.className = 'wh_content';
                break;
        }
        $$('wh_box').appendChild(_wh_content);
    }
}

//定时器
function MsgTimeCount() {
    msgT = setTimeout('MsgTimeCount()', 1000);
    msgTSpan--;
    if (msgTSpan <= 0) {
        clearTimeout(msgT);
        location.href = locationHref;
    }
    $$('msgSecond').innerHTML = msgTSpan;
}

//创建等待层wh_waiting
//flag:
//错误 = 0,
//成功 = 1,
//跳转 = 2,
//询问 = 3,
//等待 = 4
function CreateWH_WaitingBox(flag, waiting_text) {
    switch (flag) {
        case '等待':
            if ($$('wh_waiting') == null) {
                var _wh_waiting = document.createElement('div');
                _wh_waiting.id = 'wh_waiting';
                _wh_waiting.className = 'wh_waiting';
                _wh_waiting.innerHTML = waiting_text;
                $$('wh_content').appendChild(_wh_waiting);
            }
            break;
        default:
            break;
    }
}

//创建标题层
//<div class="wh_title">
//    <div class="wh_left">
//        提示信息</div>
//    <div class="wh_right">
//        <img src="http://www.tourzj.com/images/gclose.gif" title="关闭" alt="关闭" onclick="CloseFloatBox('wh_ConfirmBox')"
//            class="wh_close" /></div>
//</div>
//flag:
//错误 = 0,
//成功 = 1,
//跳转 = 2,
//询问 = 3,
//等待 = 4
function CreateWH_TitleBox(flag, title_text) {
    switch (flag) {
        case '等待':
            break;
        default:
            if ($$('wh_title') == null) {
                var _wh_title = document.createElement('div');
                _wh_title.id = '_wh_title';
                _wh_title.className = 'wh_title';
                _wh_title.innerHTML = '<div id="wh_title" class="wh_left">' + (title_text == '' ? '信息确认' : title_text) + '</div>';
                _wh_title.innerHTML += '<div class="wh_right"><img src="http://www.tourzj.com/images/gclose.gif" title="关闭" alt="关闭" onclick="DeleteWHBox()" class="wh_close" /></div>';
                $$('wh_box').appendChild(_wh_title);
            }
            break;
    }
}

//创建按钮层
//<div id="conbox" class="wh_bottom" style="text-align: center;">
//    <input id="Button1" type="button" value="确定" class="cnewbutton" onclick="ConfirmOK()" />
//    <input id="Button2" type="button" value="取消" class="cnewbutton" onclick="CloseFloatBox('confirmBox')" />
//</div>
//flag:
//错误 = 0,
//成功 = 1,
//跳转 = 2,
//询问 = 3,
//等待 = 4
function CreateWH_BottomBox(flag, functionName, parmList) {
    switch (flag) {
        case '等待':
            break;
        case '跳转':
            break;
        default:
            if ($$('wh_bottom') == null) {
                var _wh_bottom = document.createElement('div');
                _wh_bottom.id = '_wh_bottom';
                _wh_bottom.className = 'wh_bottom';
                if (flag == '询问') {
                    _wh_bottom.innerHTML = '<input id="btnCancel" type="button" value="确定" class="cnewbutton" onclick="' + functionName + '(' + parmList + ')" />';
                    _wh_bottom.innerHTML += '<input id="btnCancel" type="button" value="取消" class="cnewbutton" onclick="DeleteWHBox()" />';
                }
                else {
                    _wh_bottom.innerHTML = '<input id="btnCancel" type="button" value="关闭" class="cnewbutton" onclick="DeleteWHBox()" />';
                }
                $$('wh_box').appendChild(_wh_bottom);
            }
    }
}

