var isDesgin = false;
var editObj = null;
var OldWidth = 0;
var OldHight = 0;
var nowleft = 0;
var nowtop = 0;
var oldleft = 0;
var oldtop = 0;
var startxx = 0;
var startyy = 0;
var isShowMoveObj = false;

document.onmousedown = function() {
    if (isDesgin) {
        var obj = event.srcElement;
        if (obj.id == "divToolsTab") {
            isShowMoveObj = true;
        }
        else if (obj.parentElement != undefined && obj.parentElement != null && (obj.parentElement.className == "editable" || obj.parentElement.className == "myui-form-div-datepicker")) {
            var objTabName = obj.tagName.toLowerCase();
            editObj = obj;
            var saveMove = document.getElementById('saveMove');
            if (obj.parentElement.className == "myui-form-div-datepicker") {
                obj = obj.parentElement;
            }
            saveMove.source = obj.parentElement.source;
            saveMove.removeAttribute("selSource");
            obj.parentElement.style.border = "5px";
            obj.parentElement.style.borderColor = "blue";
            var control = document.getElementById('btnDelControl');
            control.disabled = false;
            control = null;
            control = document.getElementById('btnEditControl');
            control.disabled = false;
            control = null;
            if (objTabName == "span") {

            }
            //                    else if (objTabName == "input") {
            //                        switch (obj.type) {
            //                            case "radio": { saveMove.source = "{type:'radio',title:'',title2:'',title3:'',datasource:'',identity:''}"; } break;
            //                            case "checkbox": { saveMove.source = "{type:'radio',title:'',title2:'',title3:'',datasource:'',identity:''}"; } break;
            //                            case "text": { saveMove.source = "{type:'radio',title:'',datasource:'',identity:''}"; } break;
            //                        }
            //                        CollectGarbage();
            //                    }
            else if (objTabName == "select") {
                saveMove.selSource = obj.parentElement.selSource;

            }
            //                    else if (objTabName == "textarea") {

            //                        CollectGarbage();
            //                    }

            saveMove = null;
            obj = null;
            CollectGarbage();
        }
    }
}

document.onmouseup = function() {
    isShowMoveObj = false;
    document.body.style.cursor = "";
}

function bodyDesgin(obj) {
    if (isDesgin) {
        isDesgin = false;
        obj.value = "设 计";
        var control = document.getElementById('btnDelControl');
        control.disabled = true;
        control = null;
        control = document.getElementById('btnEditControl');
        control.disabled = true;
        control = null;
    } else {
        isDesgin = true;
        obj.value = "非设计";
    }
    obj = null;
    CollectGarbage();
}

function getLine() {
    App.SetNullHref(0);
    parent.document.getElementById('myIframe').src = "testpage.htm";
    setTimeout("getDom()", 5000);
    var btnTest = document.getElementById("testChild");
    btnTest.disabled = "";
    App.SetNullHref(1);
}

document.onmousemove = function() {
    var CurrentMoveObj = event.srcElement;
    if (isShowMoveObj) {
        document.body.style.cursor = "move";
        CurrentNodeItemObj = CurrentMoveObj;
        nowleft = parseInt(oldleft) + (event.clientX - startxx);
        nowtop = parseInt(oldtop) + (event.clientY - startyy);
        var tmpheight = parseInt(CurrentMoveObj.clientHeight) + nowtop;
        var tmpwidth = parseInt(CurrentMoveObj.clientWidth) + nowleft;
        if (tmpwidth > 1) {
            if (nowleft < 0) {
                nowleft = 0;
            }
            if (nowleft > (parseInt(CurrentMoveObj.parentElement.clientWidth) - parseInt(CurrentMoveObj.clientWidth))) {
                nowleft = parseInt(CurrentMoveObj.parentElement.clientWidth) - parseInt(CurrentMoveObj.clientWidth) - 2;
            }
            CurrentMoveObj.style.left = nowleft;
        }
        if (tmpheight > 1) {
            if (nowtop < 0) {
                nowtop = 0;
            }
            if (nowtop > (parseInt(CurrentMoveObj.parentElement.clientHeight) - parseInt(CurrentMoveObj.clientHeight))) {
                nowtop = parseInt(CurrentMoveObj.parentElement.clientHeight) - parseInt(CurrentMoveObj.clientHeight) - 2;
            }
            CurrentMoveObj.style.top = nowtop;

        }
        if (CurrentMoveObj.parentElement != null) {
            CurrentMoveObj.parentElement.scrollTop = nowtop;
            CurrentMoveObj.parentElement.scrollLeft = nowleft;
        }
    }
}
//增加控件
function addControl() {
    var getIndexForm = ShowWindow.getFormIdx();
    ShowWindow.ShowMoledDialog("Application/CMB/BDesign/BillSetting/eOA_EditControl/eOA_EditInputOrModifyControl.htm?formIdx=" + getIndexForm + "&type=AddControl", "增加控件", 600, 600, 1);
    getIndexForm = null;
    CollectGarbage();

}
//删除控件
function delControl() {
    try {
        if (editObj != null) {
            var parentControl = document.getElementById('inControl');
            if (editObj.parentElement.className == "myui-form-div-datepicker") {
                parentControl.removeChild(editObj.parentElement.parentElement);
            } else {
                parentControl.removeChild(editObj.parentElement);
            }
            editObj = null;
            parentControl = null;
            var control = document.getElementById('btnDelControl');
            control.disabled = true;
            control = null;
            control = document.getElementById('btnEditControl');
            control.disabled = true;
            control = null;
            CollectGarbage();
        }
    } catch (e) {
        alert("Error for delControl:" + e.message);
    }
}
//修改控件
function editControl() {
    var getIndexForm = ShowWindow.getFormIdx();
    ShowWindow.ShowMoledDialog("Application/CMB/BDesign/BillSetting/eOA_EditControl/eOA_EditInputOrModifyControl.htm?formIdx=" + getIndexForm + "&type=EditControl", "修改控件", 600, 600, 1);
    getIndexForm = null;
    CollectGarbage();
}

//增加控件
function getSource(strObj) {
    try {
        str = $.parseJSON(strObj);
        var _format = str.format;
        var _type = str.type;
        var _title = str.title;
        var _dataSource = str.datasource;
        var _identity = str.identity;
        var obj = "";
        var parent = document.createElement("div");
        var today = new Date();
        today = today.getYear() + ((today.getMonth() + 1) > 9 ? (today.getMonth() + 1) : ("0" + (today.getMonth() + 1).toString())) + (today.getDate() > 9 ? today.getDate() : "0" + (today.getDate().toString())) + parseInt(Math.random() * 1000);
        if (_format == "text") {
            switch (_type) {
                case "text": { obj = _title + "：<input type=" + _type + " size='15'/>"; parent.source = strObj; } break;
                case "textarea": { obj = _title + "：<textarea rows='3' cols='20'></textarea>"; parent.source = strObj; } break;
                case "select": { obj = _title + "：<select style='width:120px;'>"; var i = str.selSource.length - 1; for (i; i >= 0; i--) { obj += "<option value='" + str.selSource[i].value + "'> " + str.selSource[i].name + " </option>"; } obj += "</select>"; parent.source = strObj; } break;
                case "radio": { var i = str.selSource.length - 1; for (i; i >= 0; i--) { obj += "<input type=" + _type + " name='" + today + "'value='" + str.selSource[i].value + "'/>" + str.selSource[i].name; } parent.source = strObj; } break;
                case "checkbox": { var i = str.selSource.length - 1; for (i; i >= 0; i--) { obj += "<input type=" + _type + " name='" + today + "'value='" + str.selSource[i].value + "'/>" + str.selSource[i].name; } parent.source = strObj; } break;
            }
            //                if (_type != "radio" && _type != "checkbox") {
            //                    obj = document.createElement(_type);
            //                } else {
            //                    obj = document.createElement("input");
            //                    obj.type = _type;
            //                }
        } else if (_format == "dateTime") {
            obj = _title + "：<input type=" + _type + " size='15'/>";
            switch (_type) {
                case "yyyy-MM-dd": { obj = _title + "：<input type='text' size='15' id='" + today + "' onblur='var _reTimeReg = /^$|^(?:(?!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)$/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：1999-11-01\");value=\"\";}}' />"; parent.source = strObj; } break;
                case "yyyy": { obj = _title + "：<input type='text' size='15'onblur='var _reTimeReg =/^(?:19|20|21|22)[0-9][0-9]/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：1999(输入范围为(1900-2299))\");value=\"\";}}'/>"; parent.source = strObj; } break;
                case "MM": { obj = _title + "：<input type='text' size='15'onblur='var _reTimeReg =/^(?:(?:0[1-9])|(?:1[0-2]))/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：01或11(输入范围为(01-12))\");value=\"\";}}'/>"; parent.source = strObj; } break;
                case "dd": { obj = _title + "：<input type='text' size='15'onblur='var _reTimeReg =/^(?:(?:[0-2][1-9])|(?:[1-3][0-1]))/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：01或27(输入范围为(01-31))\");value=\"\";}}'/>"; parent.source = strObj; } break;
                case "yyyy-MM-dd hh:mm:ss": { obj = _title + "：<input type='text' size='15' id='" + today + "' onblur='var _reTimeReg = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：1999-01-10 10:26:07\");value=\"\";}}' />"; parent.source = strObj; } break;
            }
        } else if (_format == "decimal") {
            switch (_type) {
                case "integer": { obj = _title + "：<input type='text' size='15' onblur=\"var _reTimeReg =/^-?\\d+$/g;if(value!=''){if(!(_reTimeReg.test(value))){alert('错误的格式，请重新输入整数格式如：-1或27');value='';}}\"/>"; parent.source = strObj; } break;
                case "decimal": { obj = _title + "：<input type='text' size='15' onblur='var _reTimeReg = /^(-?\\d+)(\\.\\d+)?$/g;if(value!=\"\"){if(!(_reTimeReg.test(value))){alert(\"错误的格式，请重新输入小数格式如：-1.14或27.95\");value=\"\";}else if(value.indexOf(\".\")==-1){value=value+\".00\"}}' />"; parent.source = strObj; } break;
            }
        } else {
            obj = _title + "：<input type=" + _type + " size='15'/>";
        }
        parent.className = "editable";
        //            parent.source = "{type:'radio',title:'',title2:'',title3:'',datasource:'',identity:''}";
        parent.format = _format;
        parent.type = _type;
        parent.title = _title;
        parent.dataSource = _dataSource;
        parent.identity = _identity;
        parent.innerHTML = obj;
        var inControl = document.getElementById('inControl');
        inControl.appendChild(parent);
        var divType = "";
        if (_format == "text") { divType = "varchar"; }
        else if (_format == "decimal") { divType = "decimal"; }
        else if (_format == "dateTime") { divType = "dateTime"; }
        else if (_format == "attachment") { divType = "varchar"; }
        else if (_format == "table") { divType = "varchar"; }
        DBReader.execSql("exec eOA_CMB_ModifyDatatableField '" + _dataSource + "','" + _identity + "','" + divType + "'");
        if (_format == "dateTime") {
            switch (_type) {
                case "yyyy-MM-dd":
                    {
                        showRemindTime(today, false);
                    } break;
                case "yyyy-MM-dd hh:mm:ss":
                    {
                        showRemindTime(today, true);

                    } break;
            }
        }
        today = null;
        str = null;
        strObj = null;
        parent = null;
        inControl = null;
        obj = null;
        _format = _type = divType = _title = _dataSource = _identity = null;
        CollectGarbage();
    } catch (e) {
        alert("Error for getSource:" + e.message);
    }
}
//保存修改
function saveControl(str) {
    try {
        var strObj = str;
        //                var selObj = sel;
        str = $.parseJSON(str);
        //                if (sel.length > 5) {
        //                    sel = $.parseJSON(sel);
        //                } else {
        //                    sel = null;
        //                }
        var obj = "";
        var parent = editObj.parentElement;
        var saveMove = document.getElementById('saveMove');
        saveMove.source = strObj;
        var today = new Date();
        today = today.getYear() + ((today.getMonth() + 1) > 9 ? (today.getMonth() + 1) : ("0" + (today.getMonth() + 1).toString())) + (today.getDate() > 9 ? today.getDate() : "0" + (today.getDate().toString())) + parseInt(Math.random() * 1000);

        if (str.format == "text") {
            switch (str.type) {
                case "text": { obj = str.title + "：<input type=" + str.type + " size='15' value='" + editObj.value + "' />"; parent.source = strObj; } break;
                case "textarea": { obj = str.title + "：<textarea rows='3' cols='20'>" + editObj.value + "</textarea>"; parent.source = strObj; } break;
                case "select": { obj = str.title + "：<select style='width:120px;'>"; var i = str.selSource.length - 1; for (i; i >= 0; i--) { obj += "<option value='" + str.selSource[i].value + "'> " + str.selSource[i].name + " </option>"; } obj += "</select>"; parent.source = strObj; } break;
                case "radio": { var i = str.selSource.length - 1; for (i; i >= 0; i--) { obj += "<input type=" + str.type + " name='" + today + "'value='" + str.selSource[i].value + "'/>" + str.selSource[i].name; } parent.source = strObj; } break;
                case "checkbox": { var i = str.selSource.length - 1; for (i; i >= 0; i--) { obj += "<input type=" + str.type + " name='" + today + "'value='" + str.selSource[i].value + "'/>" + str.selSource[i].name; } parent.source = strObj; } break;
            }
        } else if (str.format == "dateTime") {
            //                    obj = _title + "：<input type=" + _type + " size='15'/>";
            if (parent.className == "myui-form-div-datepicker") {
                parent = parent.parentElement;
            }
            switch (str.type) {
                case "yyyy-MM-dd": { obj = str.title + "：<input type='text' size='15' id='" + today + "' onblur='var _reTimeReg = /^$|^(?:(?!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)$/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：1999-11-10\");value=\"\";}}'  />"; parent.source = strObj; } break;
                case "yyyy": { obj = str.title + "：<input type='text' size='15'onblur='var _reTimeReg =/^(?:19|20|21|22)[0-9][0-9]/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：1999(输入范围为(1900-2299))\");value=\"\";}}'/>"; parent.source = strObj; } break;
                case "MM": { obj = str.title + "：<input type='text' size='15'onblur='var _reTimeReg =/^(?:(?:0[1-9])|(?:1[0-2]))/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：01或11(输入范围为(01-12))\");value=\"\";}}'/>"; parent.source = strObj; } break;
                case "dd": { obj = str.title + "：<input type='text' size='15'onblur='var _reTimeReg =/^(?:(?:[0-2][1-9])|(?:[1-3][0-1]))/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：01或27(输入范围为(01-31))\");value=\"\";}}'/>"; parent.source = strObj; } break;
                case "yyyy-MM-dd hh:mm:ss": { obj = str.title + "：<input type='text' size='15' id='" + today + "' onblur='var _reTimeReg = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;if(value!=\"\"){if(!_reTimeReg.test(value)){alert(\"错误的日期格式，请重新输入如：1999-11-10 10:26:07\");value=\"\";}}' />"; parent.source = strObj; } break;
            }
        } else if (str.format == "decimal") {
            switch (str.type) {
                case "integer": { obj = str.title + "：<input type='text' size='15' value='" + editObj.value + "' onblur=\"var _reTimeReg =/^-?\\d+$/g;if(value!=''){if(!(_reTimeReg.test(value))){alert('错误的格式，请重新输入整数格式如：-1或27');value='';}}\"/>"; parent.source = strObj; } break;
                case "decimal": { obj = str.title + "：<input type='text' value='" + editObj.value + "' size='15' onblur='var _reTimeReg = /^(-?\\d+)(\\.\\d+)?$/g;if(value!=\"\"){if(!(_reTimeReg.test(value))){alert(\"错误的格式，请重新输入小数格式如：-1.14或27.95\");value=\"\";}else if(value.indexOf(\".\")==-1){value=value+\".00\"}}' />"; parent.source = strObj; } break;
            }
        } else {
            obj = str.title + "：<input type=" + str.type + " value=" + editObj.value + " size='15'/>";
        }
        parent.type = str.type;
        parent.title = str.title;
        parent.dataSource = str.datasource;
        parent.identity = str.identity;
        parent.innerHTML = obj;
        editObj = parent.children[0];
        var divType = "";
        var _format = parent.format;
        if (_format == "text") { divType = "varchar"; }
        else if (_format == "decimal") { divType = "decimal"; }
        else if (_format == "dateTime") { divType = "dateTime"; }
        else if (_format == "attachment") { divType = "varchar"; }
        else if (_format == "table") { divType = "varchar"; }
        DBReader.execSql("exec eOA_CMB_ModifyDatatableField '" + str.datasource + "','" + str.identity + "','" + divType + "'");
        if (str.format == "dateTime") {
            switch (str.type) {
                case "yyyy-MM-dd":
                    {
                        showRemindTime(today, false);
                    } break;
                case "yyyy-MM-dd hh:mm:ss":
                    {
                        showRemindTime(today, true);

                    } break;
            }
        }
        alert("保存成功！");
        today = null;
        obj = null;
        saveMove = null;
        divType = null;
        str = null;
        _format = null;
        CollectGarbage();
    } catch (e) {
        alert("Error for saveControl:" + e.message);
    }
}