$(function () {
    $(".addScriptMaps").click(function () {
        $(".slist").append("<tr><td><input class=\"ui-input-mini\" type=\"text\" name=\"extension\" value=\"\"></td><td><input value=\"\" class=\"ui-input-small\" name=\"scriptprocessor\" type=\"text\" /></td><td><input value=\"\" class=\"ui-input-small\" name=\"incluedverbs\" type=\"text\" /></td><td><a class=\"ui-btn ui-btn-small\" href=\"javascript:void(0);\">删除</a></td></tr>");
    });
    $(".smps a").live("click", function () {
        $(this).parent().parent().remove();
    });

    var ServerName = $("#ServerName"),
            ServerIp = $("#ServerIp"),
            ServerPort = $("#ServerPort"),
            ServerPass = $("#ServerPass"),
            ServerState = $("#ServerState"),
            ServerInfo = $("#ServerInfo"),
            Server_OpenRar = $("#Server_OpenRar"),
            Server_HostRarMax = $("#Server_HostRarMax"),
            Server_HostUnRarMax = $("#Server_HostUnRarMax"),
            Server_FTPFree = $("#Server_FTPFree"),
            Server_FTPMaxUp = $("#Server_FTPMaxUp"),
            Server_FTPMaxDown = $("#Server_FTPMaxDown"),
            Server_FTPSameip = $("#Server_FTPSameip"),
            Server_SQLAdmin = $("#Server_SQLAdmin"),
            Server_SQLPwd = $("#Server_SQLPwd"),
            Server_SQLPort = $("#Server_SQLPort"),
            Server_MySQLAdmin = $("#Server_MySQLAdmin"),
            Server_MySQLPwd = $("#Server_MySQLPwd"),
            Server_MySQLPort = $("#Server_MySQLPort"),
            Server_MySQLDataPath = $("#Server_MySQLDataPath"),
            Server_phpmyadminurl = $("#Server_phpmyadminurl"),
            Server_MailUrl = $("#Server_MailUrl"),
            Server_MailSetupPath = $("#Server_MailSetupPath"),
            allFields = $([])
            .add(ServerName)
            .add(ServerIp)
            .add(ServerPort)
            .add(ServerPass)
            .add(ServerState)
            .add(ServerInfo)
            .add(Server_OpenRar)
            .add(Server_HostRarMax)
            .add(Server_HostUnRarMax)
            .add(Server_FTPFree)
            .add(Server_FTPMaxUp)
            .add(Server_FTPMaxDown)
            .add(Server_FTPSameip)
            .add(Server_SQLAdmin)
            .add(Server_SQLPwd)
            .add(Server_SQLPort)
            .add(Server_MySQLAdmin)
            .add(Server_MySQLPwd)
            .add(Server_MySQLPort)
            .add(Server_MySQLDataPath)
            .add(Server_phpmyadminurl)
            .add(Server_MailUrl)
            .add(Server_MailSetupPath)
    ;
    //判断服务器信息输入
    function ValidServer() {
        var bValid = true;
        bValid = bValid && checkLength(ServerName, "服务器名称", 3, 16);
        bValid = bValid && checkLength(ServerIp, "被控IP", 7, 15);
        bValid = bValid && checkLength(ServerPort, "被控端口", 1, 5);
        bValid = bValid && checkLength(ServerPass, "被控密码", 1, 15);
        bValid = bValid && checkLength(ServerInfo, "备注", 1, 255);
        bValid = bValid && checkRegexp(ServerIp, Regex_Ip, "IP格式错误");
        bValid = bValid && checkRegexp(ServerPort, Regex_Port, "端口号格式错误");
        return bValid;
    }
    //判断虚拟主机信息输入
    function ValidHost() {
        var bValid = true;
        bValid = bValid && checkNumlength(Server_HostRarMax, "RAR最大压缩值", 0, 2147483647);
        bValid = bValid && checkNumlength(Server_HostUnRarMax, "RAR最大解压值", 0, 2147483647);
        bValid = bValid && checkNumlength(Server_HostRarMax, "压缩值", 0, 2147483647);
        bValid = bValid && checkNumlength(Server_HostUnRarMax, "解压值", 0, 2147483647);
        bValid = bValid && checkNumlength(Server_FTPFree, "解压值", 0, 2147483647);
        bValid = bValid && checkNumlength(Server_FTPMaxUp, "解压值", 0, 2147483647);
        bValid = bValid && checkNumlength(Server_FTPMaxDown, "解压值", 0, 2147483647);
        bValid = bValid && checkNumlength(Server_FTPSameip, "解压值", 0, 2147483647);
        return bValid;
    }

    //判断SQL输入信息
    function ValidSQL() {
        var bValid = true;
        bValid = bValid && checkLength(Server_SQLAdmin, "SQL管理帐号", 1, 15);
        bValid = bValid && checkLength(Server_SQLPwd, "SQL密码", 1, 32);
        bValid = bValid && checkLength(Server_SQLPort, "SQL端口", 1, 8);
        bValid = bValid && checkRegexp(Server_SQLPort, Regex_Port, "端口格式错误");
        return bValid;
    }
    //判断MySQL输入信息
    function ValidMySQL() {
        var bValid = true;
        bValid = bValid && checkLength(Server_MySQLAdmin, "MySQL管理帐号", 1, 15);
        bValid = bValid && checkLength(Server_MySQLPwd, "MySQL密码", 1, 32);
        bValid = bValid && checkLength(Server_MySQLPort, "MySQL端口", 1, 8);
        bValid = bValid && checkLength(Server_MySQLDataPath, "MySQL路径", 1, 255);
        bValid = bValid && checkLength(Server_phpmyadminurl, "PhpMyAdmin地址", 1, 255);
        bValid = bValid && checkRegexp(Server_MySQLPort, Regex_Port, "端口格式错误");
        return bValid;
    }
    function ValidMail() {
        var bValid = true;
        bValid = bValid && checkRegexp(Server_MailUrl, Regex_HttpUrl, "WinWebMail访问地址");
        bValid = bValid && checkRegexp(Server_MailSetupPath, RegEx_WindowsPath, "WinWebMail安装地址");
        return bValid;
    }

    //测试被控连接
    $(".testServer").click(function () {
        if (ValidServer()) {
            CommonAjax(5000, "/common/ashx/global.ashx", { "Action": "testServer", "sIP": $("#ServerIp").val(), "sPort": $("#ServerPort").val(), "sPass": $("#ServerPass").val() }, "testMsg", "<img src=\"/Common/Images/loading.gif\" />连接中...", $(this), "连接成功", "连接失败");
        }
    });
    //测试SQL
    $(".testSQL").click(function () {
        if (ValidServer() && ValidSQL()) {
            CommonAjax(5000, "/common/ashx/sql.ashx", { "Action": "TestSQLServer", "Server_SQLAdmin": $("#Server_SQLAdmin").val(), "Server_SQLPwd": $("#Server_SQLPwd").val(), "Server_SQLPort": $("#Server_SQLPort").val(), "sIP": $("#ServerIp").val(), "sPort": $("#ServerPort").val(), "sPass": $("#ServerPass").val() }, "sqlTestMsg", "<img src=\"/Common/Images/loading.gif\" />连接中...", $(this), "连接成功", "连接失败");
        }
    });
    //测试MySQL
    $(".testMySQL").click(function () {
        if (ValidServer() && ValidMySQL()) {
            CommonAjax(5000, "/common/ashx/Mysql.ashx", { "Action": "TestSQLServer", "Server_MySQLAdmin": $("#Server_MySQLAdmin").val(), "Server_MySQLPwd": $("#Server_MySQLPwd").val(), "Server_MySQLPort": $("#Server_MySQLPort").val(), "Server_MySQLDataPath": $("#Server_MySQLDataPath").val(), "sIP": $("#ServerIp").val(), "sPort": $("#ServerPort").val(), "sPass": $("#ServerPass").val() }, "mysqlTestMsg", "<img src=\"/Common/Images/loading.gif\" />连接中...", $(this), "连接成功", "连接失败");
        }
    });
    //测试Mail
    $(".testMail").click(function () {
        if (ValidServer() & ValidMail()) {
            CommonAjax(5000, "/common/ashx/Mail.ashx", { "Action": "TestMail", "Server_MailSetupPath": $("#Server_MailSetupPath").val(), "Server_MailUrl": $("#Server_MailUrl").val(), "sIP": $("#ServerIp").val(), "sPort": $("#ServerPort").val(), "sPass": $("#ServerPass").val() }, "testMailMsg", "<img src=\"/Common/Images/loading.gif\" />连接中...", $(this), "连接成功", "连接失败");
        }

    });
    $("form").submit(function () {
        if (!ValidTemp()) {
            $(this).find("button[type=submit]").removeClass("loading").removeAttr("disabled");
            return false;
        }
    });

    $("input[name=extension]").live("blur", function () {
        var extName = $(this).val();
        if (extName.toLowerCase() == ".aspx") {
            if (!$(this).hasClass("oldaspx")) {
                var nextTd = $(this).parent().next();
                nextTd.attr("colspan", "2").next().remove();
                nextTd.html("<label><input type=\"checkbox\" name=\"ASPNETEdition\" value=\"1\"> 1.0~1.1 </label><label><input type=\"checkbox\" name=\"ASPNETEdition\" value=\"2\"> 2.0~3.5 </label><label><input type=\"checkbox\" name=\"ASPNETEdition\" value=\"3\"> 4.0 </label><input value=\"\" style=\"display:none;\" name=\"scriptprocessor\" type=\"text\" /><input style=\"display:none;\" value=\"\" name=\"incluedverbs\" type=\"text\" />");
                $(this).attr("readonly", "readonly");
                $(this).addClass("oldaspx");
            }
        }
        if (extName.toLowerCase() == ".asp") {
            var extPath = $(this).parent().next().find("input");
            if (extPath.val() == "") {
                extPath.val("C:\\WINDOWS\\system32\\inetsrv\\asp.dll");
            }
            var extMethod = $(this).parent().next().next().find("input");
            if (extMethod.val() == "") {
                extMethod.val("GET,HEAD,POST,TRACE");
            }
        }
        if (extName.toLowerCase() == ".php") {
            var extPath = $(this).parent().next().find("input");
            if (extPath.val() == "") {
                extPath.val("C:\\php\\php5isapi.dll");
            }
            var extMethod = $(this).parent().next().next().find("input");
            if (extMethod.val() == "") {
                extMethod.val("GET,HEAD,POST,TRACE");
            }
        }
    });
});