﻿//日期时间格式化
Date.prototype.format = function (format) {
    /*
    * eg:format="YYYY-MM-dd hh:mm:ss";
    */
    var o = {
        "M+": this.getMonth() + 1,  //month
        "d+": this.getDate(),     //day
        "h+": this.getHours(),    //hour
        "m+": this.getMinutes(),  //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

Date.prototype.dateDiff = function (interval, objDate) {
    //若參數不足或 objDate 不是日期物件則回傳 undefined
    if (arguments.length < 2 || objDate.constructor != Date) return undefined;
    switch (interval) {
        //計算秒差 
        case "s": return parseInt((objDate - this) / 1000);
            //計算分差
        case "n": return parseInt((objDate - this) / 60000);
            //計算時差
        case "h": return parseInt((objDate - this) / 3600000);
            //計算日差
        case "d": return parseInt((objDate - this) / 86400000);
            //計算週差
        case "w": return parseInt((objDate - this) / (86400000 * 7));
            //計算月差
        case "m": return (objDate.getMonth() + 1) + ((objDate.getFullYear() - this.getFullYear()) * 12) - (this.getMonth() + 1);
            //計算年差
        case "y": return objDate.getFullYear() - this.getFullYear();
            //輸入有誤
        default: return undefined;
    }
}

/***********加载页面************/
function loadPage(id, url, callback) {
    $("#" + id).addClass("loader");
    $("#" + id).append("Loading......");
    $.ajax({
        type: "get",
        url: url,
        cache: false,
        error: function () { alert('加载页面' + url + '时出错！'); },
        success: function (msg) {
            $("#" + id).empty().append(msg);
            $("#" + id).removeClass("loader");
            callback();
        }
    });
}

/*********检查表单是否为空********/
function veriform(obj) {
    if ($("#" + obj).val().length < 1) {
        alert($("#" + obj).attr("title") + " 不能为空");
        $("#" + obj).focus();
        return false;
    }
    else
    { return true; }
}

//格式化手机，区号，电话的显示
function formatMpAndTel(mp, areacode, tel) {
    var telrs = checkLinkStr(areacode, tel, '-');
    return checkLinkStr(mp, telrs, '，');
}

/*验证手机号码是否合法*/
function myCheckIsMphone(mphone) {
    var regMobile = /^(?:13\d|15\d|18\d)-?\d{5}(\d{3}|\*{3})$/; 	//13 15 18 开头的手机号码
    if (!regMobile.test(mphone))
    { return false; }
    else
    { return true; }
}

/*验证电话号码是否合法*/
function myCheckIsTelPhone(tel) {
    var regTel = /^[0-9]{7,8}$/;
    if (!regTel.test(tel))
    { return false; }
    else
    { return true; }
}

/*检查邮政编码是否合法*/
function myCheckIsPostCode(postcode) {
    var regPostCode = /^[0-9]{6}$/;
    if (!regPostCode.test(postcode))
    { return false; }
    else { return true; }
}

/*检查用户名是否合法*/
function myCheckIsUserName(username) {
    var regUserName = /^[A-Za-z0-9]+$/;
    if (!regUserName.test(username)) { return false; }
    else { return true; }
}

/*检查Email格式是否合法*/
function isEmail(email)
{ return (new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(email)); }
function noHTML(str) {
    str = str.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
    str.value = str.replace(/[ | ]*\n/g, '\n'); //去除行尾空白
    //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
    return str;
}

/*加入收藏夹*/
function addFavorite()
{
    try
    {
        window.external.addFavorite(window.location.href, document.title);
    }
    catch (e)
    {
        try
        {
            window.sidebar.addPanel(document.title, window.location.href, "");
        }
        catch (e)
        {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

/*设为首页*/
function setHomePage(obj)
{
    try
    {
        obj.style.behavior='url(#default#homepage)';obj.setHomePage(window.location.href);
    }
    catch(e)
    {
        if(window.netscape)
        {
            try
            {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e)
            {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage',window.location.href);
        }
    }
}

/*重置*/
function ReSetForm(Area)
{
    $("" + Area + " input[type != 'button'][type != 'submit'][type != 'reset'][type != 'hidden'], textarea").each(function () {
        $(this).val("");
    });
    var selects = $(""+Area+" select");
    for (i = 0; i < selects.length; i++)
    {
        selects[i].options[0].selected = true;
    }
}
