﻿/// <reference path="MicrosoftAjax.js" />
/// <reference path="MicrosoftMvcAjax.js" />
/// <reference path="jquery-1.3.2.min-vsdoc.js" />
/// <reference path="jquery.boxy.js" />
/// <reference path="jquery.jhistory.js" />

var app_root = "/";
var regPhone=/^([0-9]{3,4}-)?[0-9]{6,8}(-[0-9]{3,4})?$/i;
var regMobile=/^(\+86)?[0-9]{11}$/i;
var regEmail = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;

function watermarktextbox_focus(text) {
    if (text.input == null ||
                text.input == undefined ||
                text.input == "") {
        text.mark = text.value;
        text.value = text.input || "";
    }
    text.focused = true;
}

function watermarktextbox_blur(text) {
    text.input = text.value;
    if (text.value == null || text.value == "") {
        text.value = text.mark;
    }
    text.focused = false;
}

function get_form(form, obj) {
    obj = obj || {};
    for (var j = 0, c = form.elements.length; j < c; j++) {
        var el = form.elements[j];
        if (el.type == "hidden" && obj.hasOwnProperty(el.name))
            continue;
        getInputValue(el, obj, el.name);
    }
    return obj;
}

function validate_form(arr, obj, result) {
    result = result || { msg: "", valid: true };
    var valid = true;
    for (var i = 0, c = arr.length; i < c; i++) {
        var dom = arr[i];
        if (dom.className == "form-validation") {
            var field = dom.getAttribute("field");
            if (!obj.hasOwnProperty(field)) {
                throw ("找不到字段[" + field + "]");
            }
            var callback = eval(dom.getAttribute("valid"));
            var correct = callback(dom, obj[field]);
            if (!correct) {
                result.valid = false;
                if (dom.getAttribute("mode") == "popup") {
                    result.msg += dom.innerHTML + "<br/>";
                } else {
                    dom.style.display = "inline";
                }
            } else {
                dom.style.display = "none";
            }
        }
    }
    return result;
}

function post_form(form,url,title,width,height) {
    var obj = get_form(form);
    var arr = form.getElementsByTagName("SPAN");
    var result = validate_form(arr, obj);
    if (result.msg != "") {
        try{divFrame(url,title,width,height,false);}catch(ex){}
    }
    return result.valid;
}

function getInputValue(input, obj, name) {
    if (input.tagName == "SELECT") {
        obj[name] = input.selectedIndex > -1 ? input.options[input.selectedIndex].value : null;
    } else if (input.type == "checkbox") {
        obj[name] = input.checked;
    } else if (input.type == "radio") {
        if (input.checked) {
            obj[name] = input.value;
        }
    } else if (input.getAttribute("watermark")) {
        if (input.focused) {
            input.input = input.value;
        }
        obj[name] = input.input;
    } else {
        obj[name] = input.value;
    }
}

function required_validate(dom, value) {
    if (value == null || value == "") {
        return false;
    }
    return true;
}

function email_validate(dom, value) {
    if (required_validate(dom, value)) {
        return regEmail.test(value);
    }
    return false;
}

function tab_select(dom) {
    var $tab = $("#tab_set .selected").removeClass("selected");
    $tab.attr("src", $tab.attr("src").replace("_sel.jpg", ".jpg"));
    $tab.next("div").removeClass("hide_splitter");
    $tab.prev("div").removeClass("hide_splitter");
    $tab = $(dom).addClass("selected");
    $tab.attr("src", $tab.attr("src").replace(".jpg", "_sel.jpg"));
    $tab.next("div").addClass("hide_splitter");
    $tab.prev("div").addClass("hide_splitter");
}

function loading_show(selector, img, mode) {
    if (mode == 'inplace') {
        $(selector).html("<img src=\"" + img + "\"/>");
    } else if (mode == 'block') {
        $("<div class=\"loading\" style=\"background-image: url(" + img + ");\"></div>")
            .insertAfter(selector).fadeIn('fast');
    }
}

function loading_hide(selector, mode) {
    if (mode == 'inplace') {
        $(selector).empty();
    } else if (mode == 'block') {
        $(selector).next().fadeOut('fast', function() {
            $(this).remove();
        });
    }
}

function ajax(url, option) {
    option = option || {};
    var selector = option.target || "#main_content";
    var dataType = option.dataType || "html";
    var mode = option.mode || 'block';
    if (dataType == "html") {
        loading_show(selector, option.img || "/Content/ajax-loader.gif", mode);
    }
    $.ajax({
        "url": app_root + url,
        type: option.type || "POST",
        success: function(data) {
            if (dataType == "html") {
                if (mode == 'inplace') {
                    loading_hide(selector, mode);
                    $(selector).html(data);
                } else {
                    $(selector).html(data);
                    loading_hide(selector, mode);
                }
            }
            if (option.callback) {
                option.callback(data);
            }
        },
        error: function() {
            $(selector).html("<span class=\"error\">无法打开页面</span>");
            loading_hide(option.target || "#main_content", mode);
        },
        "dataType": dataType,
        data: option.data
    });
}

function addbookmark(title) {
    if (window.external) {
        window.external.AddFavorite(window.location.href, title);
    } else {
        Boxy.alert("您的浏览器不支持该操作，请使用IE浏览器", null, { title: "提示", modal: true, unloadOnHide: true });
    }
}

function dropdown_search(data) {
    if ($("#search_dropdown").css("display") == "none") {
        $("#search_dropdown").slideDown("fast");
    } else {
        $("#search_dropdown").slideUp("fast");
    }
}

function search_dropdown_focus(src) {
    $("#search_dropdown .selected").each(function() {
        if (this != src) {
            $(this).removeClass("selected");
        }
    });
    $(src).addClass("selected");
}

function search_dropdown_sel(src, id) {
    $get("search_catalogID").value = src.innerHTML.trim();
    $("#search_box input").each(function() {
        if (this.name == "catalogID") {
            this.value = id;
        }
    });
    $("#search_dropdown").slideUp("fast");
}

function copy() {
    if (window.clipboardData) {
        window.clipboardData.setData("Text", window.location.href);
        Boxy.alert("已经将该页地址复制到剪贴板", null, { title: "提示", modal: true, unloadOnHide: true });
    }
}

function sel_file_list_head(src) {
    var img = $(".file_list_head .selected").get(0);
    if (img != src) {
        var temp = img.src;
        img.src = $(img).attr("img2");
        $(img).attr("img2", temp).removeClass("selected");
        temp = src.src;
        src.src = $(src).attr("img2");
        $(src).attr("img2", temp).addClass("selected");
    }
}

function MM_openBrWindow(theURL, winName, features) { 
    window.open(theURL, winName, features);
}

function tab(aa, bb) {
    var $ = function(i) { return document.getElementById(i); }
    for (i = 1; i < 6; i++) {
        if (aa == "tit" + i) {
            $("tit" + i).className = "lihover";
            $("cont" + i).style.display = "";
        }
        else {
            $("tit" + i).className = "lilink";
            $("cont" + i).style.display = "none";
        }
    }
}

function nav(cc, dd) {
    var $ = function(i) { return document.getElementById(i); }
    for (i = 1; i < 4; i++) {
        if (cc == "ftit" + i) {
            $("ftit" + i).className = "lihover2";
            $("cont" + i).style.display = "";
        }
        else {
            $("ftit" + i).className = "lilink2";
            $("cont" + i).style.display = "none";
        }
    }
}


function menu(ee, ff) {
    var $ = function(i) { return document.getElementById(i); }
    for (i = 1; i < 4; i++) {
        if (ee == "dtit" + i) {
            $("dtit" + i).className = "lihover3";
            $("cont" + i).style.display = "";
        }
        else {
            $("dtit" + i).className = "lilink3";
            $("cont" + i).style.display = "none";
        }
    }
}

function nnav(aa, bb) {
    var $ = function(i) { return document.getElementById(i); }
    for (i = 1; i <= 3; i++) {
        if (aa == "tit" + i) {
            $("tit" + i).className = "i_n_navon";
            $("cot" + i).style.display = "block";
        }
        else {
            $("tit" + i).className = "i_n_navoff";
            $("cot" + i).style.display = "none";
        }
    }
}

function nnav2(c, d) {
    var $ = function(i) { return document.getElementById(i); }
    for (i = 1; i <= 2; i++) {
        if (c == "menu" + i) {
            $("menu" + i).className = "g_n_navon";
            $("ct" + i).style.display = "block";
        }
        else {
            $("menu" + i).className = "g_n_navoff";
            $("ct" + i).style.display = "none";
        }
    }
}

function nnav3(f, g) {
    var $ = function(i) { return document.getElementById(i); }
    for (i = 1; i <= 3; i++) {
        if (f == "tit" + i) {
            $("tit" + i).className = "h_n_navon";
            $("cot" + i).style.display = "block";
        }
        else {
            $("tit" + i).className = "i_n_navoff";
            $("cot" + i).style.display = "none";
        }
    }
}

function nnav4(x, y) {
    var $ = function(i) { return document.getElementById(i); }
    for (i = 1; i <= 2; i++) {
        if (x == "tit" + i) {
            $("tit" + i).className = "i_n_navon";
            $("cot" + i).style.display = "block";
        }
        else {
            $("tit" + i).className = "i_n_navoff";
            $("cot" + i).style.display = "none";
        }
    }
}
