/* $Id : utils.js 5052 2007-02-03 10:30:13Z weberliu $ */

var Browser = new Object();

Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument != 'undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox") != - 1);
Browser.isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") != - 1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != - 1);

var Utils = new Object();

Utils.htmlEncode = function(text)
{
  return text.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

Utils.trim = function( text )
{
  return text.replace(/^\s*|\s*$/g, "");
}

Utils.isEmpty = function( val )
{
  switch (typeof(val))
  {
    case 'string':
      return Utils.trim(val).length == 0 ? true : false;
      break;
    case 'number':
      return val == 0;
      break;
    case 'object':
      return val == null;
      break;
    case 'array':
      return val.length == 0;
      break;
    default:
      return true;
  }
}

Utils.isNumber = function(val)
{
  var reg = /^[\d|\.|,]+$/;
  return reg.test(val);
}

Utils.isInt = function(val)
{
  if (val == "")
  {
    return false;
  }
  var reg = /\D+/;
  return !reg.test(val);
}

Utils.isNum = function(val) {
    if (val == "") {
        return false;
    }
    var reg = /^[0-9]/;
    return !reg.test(val);
}

Utils.isEmail = function( email )
{
  var reg1 = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;

  return reg1.test( email );
}

Utils.isUserName = function ( val )
{
    var re=/^[0-9a-z][\w-.]*[0-9a-z]$/i;
    if(re.test(val)){return true;}else{return false;}
}

Utils.isTel = function ( tel )
{
  var reg = /^[\d|\-|\s|\_]+$/; //只允许使用数字-空格等

  return reg.test( tel );
}

Utils.isMobile = function ( mobile )
{
    if (/^13\d{9}$/g.test(mobile) || (/^15[0,1,2,3,4,5,6,7,8,9]\d{8}$/g.test(mobile)) || (/^18[0,1,2,3,4,5,6,7,8,9]\d{8}$/g.test(mobile)))
    return true;
  else
    return false;
}

Utils.isIDCard = function(idcard) {
    var re = /^(\d{14}|\d{17})(\d|[xX])$/;

    if (re.test(idcard)) {
        return true;
    }
    else {
        return false;
    }
}

Utils.fixEvent = function(e)
{
  var evt = (typeof e == "undefined") ? window.event : e;
  return evt;
}

Utils.srcElement = function(e)
{
  if (typeof e == "undefined") e = window.event;
  var src = document.all ? e.srcElement : e.target;

  return src;
}

Utils.isTime = function(val)
{
  var reg = /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}$/;

  return reg.test(val);
}

















/**------------------------------------------------------------------------------------------------------------------------------------------------
* 时间对象的格式化;
*/
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;
}


//time类型相加
Date.prototype.add = function(milliseconds) {
    var m = this.getTime() + milliseconds;
    return new Date(m);
};

//秒类型相加
Date.prototype.addSeconds = function(second) {
    return this.add(second * 1000);
};

//分钟类型相加
Date.prototype.addMinutes = function(minute) {
    return this.addSeconds(minute * 60);
};
//小时类型相加
Date.prototype.addHours = function(hour) {
    return this.addMinutes(60 * hour);
};

//天数相加
Date.prototype.addDays = function(day) {
    return this.addHours(day * 24);
};
//是否闰年
Date.isLeepYear = function(year) {
    return (year % 4 == 0 && year % 100 != 0)
};

//计算当月 天数
Date.daysInMonth = function(year, month) {
    if (month == 2) {
        if (year % 4 == 0 && year % 100 != 0)
            return 29;
        else
            return 28;
    }
    else if ((month <= 7 && month % 2 == 1) || (month > 7 && month % 2 == 0))
        return 31;
    else
        return 30;
};

//月类型相加法 1个月(判断是否跨年)
Date.prototype.addMonth = function() {
    var m = this.getMonth();
    if (m == 11) return new Date(this.getFullYear() + 1, 1, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());

    var daysInNextMonth = Date.daysInMonth(this.getFullYear(), this.getMonth() + 1);
    var day = this.getDate();
    if (day > daysInNextMonth) {
        day = daysInNextMonth;
    }
    return new Date(this.getFullYear(), this.getMonth() + 1, day, this.getHours(), this.getMinutes(), this.getSeconds());
};

//月类型减法 1个月(判断是否跨年)
Date.prototype.subMonth = function() {
    var m = this.getMonth();
    if (m == 0) return new Date(this.getFullYear() - 1, 12, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());

    var day = this.getDate();
    var daysInPreviousMonth = Date.daysInMonth(this.getFullYear(), this.getMonth());
    if (day > daysInPreviousMonth) {
        day = daysInPreviousMonth;
    }
    return new Date(this.getFullYear(), this.getMonth() - 1, day, this.getHours(), this.getMinutes(), this.getSeconds());
};

//月类型相加 多个月
Date.prototype.addMonths = function(addMonth) {
    var result = false;
    if (addMonth > 0) {
        while (addMonth > 0) {
            result = this.addMonth();
            addMonth--;
        }
    } else if (addMonth < 0) {
        while (addMonth < 0) {
            result = this.subMonth();
            addMonth++;
        }
    } else {
        result = this;
    }
    return result;
};

//年份加法
Date.prototype.addYears = function(year) {
    return new Date(this.getFullYear() + year, this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());
};









//正则替换
function Rec(allchar, val) {
    var s = allchar;
    var regS = new RegExp(val, "gi");
    return s.replace(regS, "")
}


String.prototype.template = function() {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g, function(m, i) {
        return args[i];
    });
};


function SetTime(obg, now) {
    var x = 0;
    if (now!=undefined && now != "") {
        if (now > new Date()) {
        
        } else {
            x = new Date().addHours(1).Format("hh")
        }
    }
    $(obg).empty();
    for (i = x; i <= 23; i++) {
        $(obg).append('<option value="' + (FormatTime(i)) + ':00">' + (FormatTime(i)) + ':00</option>');
        $(obg).append('<option value="' + (FormatTime(i)) + ':30">' + (FormatTime(i)) + ':30</option>');
    }
}


function SetTime2(obg, now) {
    var x = 0;
    x = new Date(now).addHours(1).Format("hh")
    $(obg).empty();
    for (i = x; i <= 23; i++) {
        $(obg).append('<option value="' + (FormatTime(i)) + ':00">' + (FormatTime(i)) + ':00</option>');
        $(obg).append('<option value="' + (FormatTime(i)) + ':30">' + (FormatTime(i)) + ':30</option>');
    }
}

function FormatTime(val) {
    if (val.toString().length == 1) return '0' + val;
    return val;
}



function GoBack(message, url) {
    $("#a1xx").hide();
    
    if (url == undefined || url == "") {
        url = window.history.go(-1);
    }
    
    $('.nowalert').html("<p>" + message + ",<b id='mmback'>3</b>秒钟后自动返回主页</p>").slideDown();
    $('.nowalert').after('<div class="clear"></div><div class=" m320"><a href="' + url + '" class="button accept">立即返回</a></div>');
 
    setTimeout(function() { window.location.href = url }, 3000);
}





//时间区域计算公式
function SetSelfBoxTime(obg, time, datetime) {
    var hoursA = (time.split("-")[0] == "00:00" ? "23:30" : time.split("-")[0]).split(":");
    var hoursB = (time.split("-")[1] == "00:00" ? "23:30" : time.split("-")[1]).split(":");
    var x = 0;
    $(obg).empty();
    for (i = hoursA[0]; i <= hoursB[0]; i++) {
        if (x == 0 && hoursA[1] == "00") {
            $(obg).append('<option value="' + (FormatTime(i)) + ':00">' + (FormatTime(i)) + ':00</option>');
            $(obg).append('<option value="' + (FormatTime(i)) + ':30">' + (FormatTime(i)) + ':30</option>');
        } else if (x == 0 && hoursA[1] == "30") {
            $(obg).append('<option value="' + (FormatTime(i)) + ':30">' + (FormatTime(i)) + ':30</option>');
        } else {
            if (i == hoursB[0] && hoursB[1] == "00") {
                $(obg).append('<option value="' + (FormatTime(i)) + ':00">' + (FormatTime(i)) + ':00</option>');
            } else {
                $(obg).append('<option value="' + (FormatTime(i)) + ':00">' + (FormatTime(i)) + ':00</option>');
                $(obg).append('<option value="' + (FormatTime(i)) + ':30">' + (FormatTime(i)) + ':30</option>');
            }
        }
        x++;
    }



    $(obg).find("option").each(function() {
        if (new Date().Format("hh:mm") > $(this).text() && datetime.Format("yyyy-MM-dd") == new Date().Format("yyyy-MM-dd")) {
            $(this).remove();
        }
    })

}
