function jsSendTime() {
}

window.onload = function() {
    jsSendTime.setSelectValue();
    jsSendTime.getSource();
    jsSendTime.changeMode();
}

/*读取数据*/
jsSendTime.getSource = function() {
    if (App.query("insertSource") != "" && App.query("insertSource") != null) {
        var obj = eval(unescape(App.query("insertSource")));
        var rdoItem = $("input[name='Cycle']");
        for (var i = 0; i < rdoItem.length; i++) {
            if (rdoItem[i].value == obj.SendOpportunity) {
                rdoItem[i].checked = true;
                break;
            }
        }
        var month = document.getElementById("sendMonth");
        var date = document.getElementById("sendDate");
        var hour = document.getElementById("sendHour");
        var minute = document.getElementById("sendMinute");
        var second = document.getElementById("sendSecond");
        var week = document.getElementById("sendWeekDay");
        var sendTime = obj.SendTime;
        if (obj.SendOpportunity == "0") {
            sendTime = sendTime.split("月");
            month.value = sendTime[0];
            sendTime = sendTime[1].split("日");
            date.value = sendTime[0];
            sendTime = sendTime[1].split(":");
            hour.value = sendTime[0];
            minute.value = sendTime[1];
            second.value = sendTime[2];
        } else if (obj.SendOpportunity == "1") {
            sendTime = sendTime.split("日");
            date.value = sendTime[0];
            sendTime = sendTime[1].split(":");
            hour.value = sendTime[0];
            minute.value = sendTime[1];
            second.value = sendTime[2];
        } else if (obj.SendOpportunity == "2") {
            sendTime = sendTime.split(":");
            hour.value = sendTime[0];
            minute.value = sendTime[1];
            second.value = sendTime[2];
        } else {
            week.value = sendTime == "" ? "星期一" : sendTime;
        }
        var selDom = document.getElementById("selSendTime");
        if (obj.SendTypeSign != "") {
            selDom.value = obj.SendTypeSign;
        }
        document.getElementById("SendTermin").value = obj.SendTermin;
        month = null;
        date = null;
        hour = null;
        minute = null;
        second = null;
        week = null;
        selDom = null;
        CollectGarbage();
    }

}


/*单选按钮操作*/
jsSendTime.changeMode = function() {
    var obj = $("input[name='Cycle'][checked]");
    var _value = obj[0].value;
    var domYear = document.getElementById("spanYear");
    var domMonth = document.getElementById("spanMonth");
    var domDate = document.getElementById("spanDate");
    var domWeek = document.getElementById("spanWeek");
    var domName = document.getElementById("spanName");
    domYear.style.display = "none";
    domMonth.style.display = "none";
    domDate.style.display = "none";
    domWeek.style.display = "none";
    if (_value == "0") {
        domYear.style.display = "";
        domMonth.style.display = "";
        domDate.style.display = "";
        domName.innerText = "年";
    }
    else if (_value == "1") {
        domMonth.style.display = "";
        domDate.style.display = "";
        domName.innerText = "月";
    }
    else if (_value == "2") {
        domDate.style.display = "";
        domName.innerText = "日";
    }
    else if (_value == "3") {
        domWeek.style.display = "";
        domName.innerText = "周";
    }
    domYear = null;
    domMonth = null;
    domDate = null;
    domWeek = null;
    domName = null;
    dom = null;
    CollectGarbage();
}

jsSendTime.changeSelectValue=function(obj){
    if($(obj).val()!=0){
        $("#fTimeSel").hide();
    }else{
        $("#fTimeSel").show();
    }
}

/*绑定下拉框*/
jsSendTime.setSelectValue = function() {
    var objStr = DBReader.selectSqlForJson("select * from T_SMSSendType");
    var obj = $.parseJSON("[{" + objStr + "}]");
    var selDom = document.getElementById("selSendTime");
    selDom.style.width = "70px";
    if (obj[0].table != undefined) {
        for (var i = 0; i < obj[0].table.length; i++) {
            var newItem = new Option(obj[0].table[i].TypeName, obj[0].table[i].SMSSendTypeSign);
            if (i == 0) {
                newItem.selected = true;
            }
            selDom.options.add(newItem);
            newItem = null;
        }
    }
}

/*验证*/
jsSendTime.dataValidate = function(str, obj) {
    if ($.trim(obj.value) != "") {
        var _reTimeReg = /^$|^[]{0,1}[0-9]{1,}$/;
        var _val = obj.value;
        //      var alertTxt = "";        
        //      _reTimeReg = /^$|^[]{0,1}[0-9]{1,}$/;
        //      alertTxt = "错误的格式，请重新输入正整数格式如：1或27";        
        if (_reTimeReg != "" && (_val != null || $.trim(_val) != "")) {
            if (!(_reTimeReg.test(_val))) {
                alert("错误的格式，请重新输入！");
                obj.focus();
            } else if (str == "month") {
                if (parseInt(_val) > 12) {
                    alert("错误的月份，请重新输入！");
                    obj.focus();
                }
            } else if (str == "date") {
                if (document.getElementById("spanYear").style.display == "") {
                    var monthObj = document.getElementById("sendMonth");
                    var month = 1;
                    if ($.trim(monthObj.value) != "") {
                        month = monthObj.value;
                    } else {
                        monthObj.value = month;
                    }
                    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                        if (parseInt(_val) > 31) {
                            alert("错误的日期，请重新输入！");
                            obj.focus();
                        }
                    }
                    else if (month == 2) {
                        if (parseInt(_val) > 29) {
                            alert("错误的日期，请重新输入！");
                            obj.focus();
                        }
                    }
                    else {
                        if (parseInt(_val) > 30) {
                            alert("错误的日期，请重新输入！");
                            obj.focus();
                        }
                    }

                } else {
                    if (parseInt(_val) > 31) {
                        alert("错误的日期，请重新输入！");
                        obj.focus();
                    }
                }

            } else if (str == "hour") {
                if (parseInt(_val) > 23) {
                    alert("错误的小时，请重新输入！");
                    obj.focus();
                }
            } else if (str == "minute") {
                if (parseInt(_val) > 59) {
                    alert("错误的分钟，请重新输入！");
                    obj.focus();
                }
            } else if (str == "second") {
                if (parseInt(_val) > 59) {
                    alert("错误的秒，请重新输入！");
                    obj.focus();
                }
            } else if (str == "week") {
                if (parseInt(_val) > 6) {
                    alert("错误的周，请重新输入！");
                    obj.focus();
                }
            }
        }
        _reTimeReg = null;
        _val = null;
    }
    obj = null
    CollectGarbage();
}

/*实现设置*/
jsSendTime.sendTimeSetting = function() {
    var str = '';
    var obj = $("input[name='Cycle'][checked]");
    var _value = obj[0].value;
    var sendTermin = document.getElementById("SendTermin");
    var month = document.getElementById("sendMonth");
    var date = document.getElementById("sendDate");
    var hour = document.getElementById("sendHour");
    var minute = document.getElementById("sendMinute");
    var second = document.getElementById("sendSecond");
    var week = document.getElementById("sendWeekDay");
    var selDom = document.getElementById("selSendTime");
    if (_value == "0") {
        str = '{"SendOpportunity":"' + _value + '","SendTypeSign":"' + selDom.value + '","SendTermin":"' + ($.trim(sendTermin.value) == '' ? '1' : $.trim(sendTermin.value)) + '","SendTime":"' + (($.trim(month.value) == '' ? '01' : parseInt($.trim(month.value)) > 9 ? $.trim(month.value) : '0' + parseInt($.trim(month.value))) + '月' + ($.trim(date.value) == '' ? '01' : parseInt($.trim(date.value)) > 9 ? $.trim(date.value) : '0' + parseInt($.trim(date.value))) + '日' + ($.trim(hour.value) == '' ? '00' : parseInt($.trim(hour.value)) > 9 ? $.trim(hour.value) : '0' + parseInt($.trim(hour.value))) + ':' + ($.trim(minute.value) == '' ? '00' : parseInt($.trim(minute.value)) > 9 ? $.trim(minute.value) : '0' + parseInt($.trim(minute.value))) + ':' + ($.trim(second.value) == '' ? '00' : parseInt($.trim(second.value)) > 9 ? $.trim(second.value) : '0' + parseInt($.trim(second.value)))) + '"}';
    }
    else if (_value == "1") {
        str = '{"SendOpportunity":"' + _value + '","SendTypeSign":"' + selDom.value + '","SendTermin":"' + ($.trim(sendTermin.value) == '' ? '1' : $.trim(sendTermin.value)) + '","SendTime":"' + (($.trim(date.value) == '' ? '01' : parseInt($.trim(date.value)) > 9 ? $.trim(date.value) : '0' + parseInt($.trim(date.value))) + '日' + ($.trim(hour.value) == '' ? '00' : parseInt($.trim(hour.value)) > 9 ? $.trim(hour.value) : '0' + parseInt($.trim(hour.value))) + ':' + ($.trim(minute.value) == '' ? '00' : parseInt($.trim(minute.value)) > 9 ? $.trim(minute.value) : '0' + parseInt($.trim(minute.value))) + ':' + ($.trim(second.value) == '' ? '00' : parseInt($.trim(second.value)) > 9 ? $.trim(second.value) : '0' + parseInt($.trim(second.value)))) + '"}';
    }
    else if (_value == "2") {
        str = '{"SendOpportunity":"' + _value + '","SendTypeSign":"' + selDom.value + '","SendTermin":"' + ($.trim(sendTermin.value) == '' ? '1' : $.trim(sendTermin.value)) + '","SendTime":"' + (($.trim(hour.value) == '' ? '00' : parseInt($.trim(hour.value)) > 9 ? $.trim(hour.value) : '0' + parseInt($.trim(hour.value))) + ':' + ($.trim(minute.value) == '' ? '00' : parseInt($.trim(minute.value)) > 9 ? $.trim(minute.value) : '0' + parseInt($.trim(minute.value))) + ':' + ($.trim(second.value) == '' ? '00' : parseInt($.trim(second.value)) > 9 ? $.trim(second.value) : '0' + parseInt($.trim(second.value)))) + '"}';
    }
    else if (_value == "3") {
        str = '{"SendOpportunity":"' + _value + '","SendTypeSign":"' + selDom.value + '","SendTermin":"' + ($.trim(sendTermin.value) == '' ? '1' : $.trim(sendTermin.value)) + '","SendTime":"' + week.value + '"}';
    }
    ShowWindow.opener.execScript(App.query("formIdx"), App.query("action").replace('@str', escape(str)));
    month = null;
    date = null;
    hour = null;
    minute = null;
    second = null;
    week = null;
    selDom = null;
    obj = null;
    _value = null;
    ShowWindow.close();
    CollectGarbage();
}