﻿String.prototype.IsMobile = function()
{
    return /^1[0-9]{10}$/.test(this);
}
String.prototype.IsEMail = function()
{
    return /^[a-zA-Z0-9_\.-]+@([\w]+)(\.[a-zA-Z0-9]+){1}(\.[a-zA-Z0-9]+)?$/.test(this);
}
String.prototype.IsPostCode = function()
{
    return /^[0-9]{6}$/.test(this);
}
String.prototype.Trim = function()
{
    return this.replace(/^\s*|\s*$/g,"");
}
function IsIE()
{
    return navigator.userAgent.indexOf("MSIE") != -1;
}
function $(source)
{
    return IsString(source) ? document.getElementById(source) : source;
}
function $$(source)
{
    return IsString(source) ? document.getElementsByName(source) : source;
}
function IsString(source)
{
    return typeof(source) == "string";
}
function IsNumber(source)
{
    return typeof(source) == "number";
}
function IsObject(source)
{
    return typeof(source) == "object";
}
/*******************
为一个控件添加方法
source:源控件
method:方法名
fun:方法体或函数指针
*******************/
function AddMethod(source , method , fun)
{
    if(source.attachEvent)
    {
        source.attachEvent("on" + method , new Function(fun));
    }
    else if(source.addEventListener)
    {
        source.addEventListener(method , new Function(fun) , false);
    }
}
/*******************************
根据Value或者Text值选择Seclect
*******************************/
function Select(source , vt)
{//alert(source.options.length);
    if(source == null || source.options.length <= 0) return;
    for(var i = 0 ; i < source.options.length ; i++)
    {
        if(source.options[i].value == vt || source.options[i].text == vt)
        {
            source.selectedIndex = i;
            break;
        }
    }
}
/********************************
为Checkbox提供反选及全选操作
source:源控件CheckBox
type:true为全选,false或不写为反选
********************************/
function CheckTurn(source , type)
{
    if(source == null) return;
    for(var i = 0 ; i < source.length ; i++)
    {
        if(type)
        {
            source[i].checked = true;
        }
        else
        {
            if(source[i].checked)
                source[i].checked = false;
            else
                source[i].checked = true;
        }
    }
}
/******************************************************
关于查询字符串
qsHref:URL
qsHost:不包括查询字符串在内的主机地址加文件名
qsSearch:查询字符串
arrSearch:查询字符串数组
arrKey:查询字符串Key数组
arrValue:查询字符串Value数组
SplitKeyAndValue:分解参数到arrKey和arrValue
GetValueByKey:根据查询参数获得值
SetValueByKey:设置某个参数的值,如果没有则添加这个参数
RemoveByKey:删除一个参数和值
AppendParam:增加参数
CombiParam:组合参数,返回不带"?"的参数字符串
******************************************************/
function QueryString()
{
    this.qsHref = location.href;
    this.qsPathName = location.pathname;
    this.qsHost = this.qsHref.replace(location.search,"");
    this.qsSearch = location.search.replace("?","");
    this.arrSearch = this.qsSearch.split("&");
    this.arrKey = new Array();
    this.arrValue = new Array();
    this.SplitKeyAndValue = SplitKeyAndValue(this.arrSearch,this.arrKey,this.arrValue);
    this.GetValueByKey = GetValueByKey;
    this.SetValueByKey = SetValueByKey;
    this.RemoveByKey = RemoveByKey;
    this.AppendParam = AppendParam;
    this.CombiParam = CombiParam;
    this.ToPathAndQuery = ToPathAndQuery;
}
function SplitKeyAndValue(arrSearch,arrKey,arrValue)
{
    for(var i = 0;i<arrSearch.length;i++)
    {
        var temp = arrSearch[i].split("=");
        if(temp.length == 2)
        {
            arrKey.push(temp[0].toLowerCase());
            arrValue.push(temp[1]);
        }
    }
}
function GetValueByKey(key)
{
    for(var i = 0;i<this.arrKey.length ; i++)
    {
        if(this.arrKey[i] == key.toLowerCase())
            return this.arrValue[i];
    }
}
function SetValueByKey(key,value)
{
    var have = false;
    for(var i = 0; i<this.arrKey.length; i++)
    {
        if(this.arrKey[i] == key.toLowerCase())
        {
            this.arrValue[i] = value;
            have = true;    
        }
    }
    if(!have)
        this.AppendParam(key,value);
}
function RemoveByKey(key)
{
    for(var i = 0; i<this.arrKey.length; i++)
    {
        if(this.arrKey[i] == key.toLowerCase())
        {
            this.arrKey.splice(i,1);
            this.arrValue.splice(i,1);
        }
    }
}
function AppendParam(key,value)
{
    this.arrKey.push(key);
    this.arrValue.push(value);
}
function CombiParam()
{
    var effect = "";
    for(var i = 0; i < this.arrKey.length; i++)
    {
        effect += this.arrKey[i] + "=" + this.arrValue[i] + "&";
    }
    return effect.substring(0,effect.length-1);
}
function ToPathAndQuery()
{
    return this.qsPathName + "?" + this.CombiParam();
}

function Post(form , page , method)
{
    var frm = $(form);
    if(frm != null)
    {
        frm.method = method;
        frm.action = page;
        RemoveChild("__VIEWSTATE");
        frm.submit();
    }
}
function RemoveChild(id)
{
    var child = $(id);
    if(child != null && child.parentNode != null)
    {
        child.parentNode.removeChild(child);
    }
}
function fill(id,s)
{
    var o = $(id);
    o.length = 0;
    var op = document.createElement("option");
    op.text = "=请选择=";
    op.value = "0";
    o.options.add(op);
    var dom = createDom(s)
    var s = dom.getElementsByTagName(id);
    for(var i=0; i< s.length; i++)
    {
        var id = s[i].getElementsByTagName("id")[0].firstChild.nodeValue;
        var name = s[i].getElementsByTagName("name")[0].firstChild.nodeValue;
        var op = document.createElement("option");
        op.text = name;
        op.value = id;
        o.options.add(op);
    }
}

//验证码
function rc()
{
    var r = $("txtRandomCode");
    if(r.value == "")
    {
        di("txtRandomCode" , "验证码不能为空");
    }
    else if(!/^[a-zA-Z0-9]{4,5}$/.test(r.value))
    {
        di("txtRandomCode" , "请输入正确的验证码");
    }
}
function di(id , msg)
{
    var aw = $(id + "Warn");
    if(aw.firstChild != null)
        aw.removeChild(aw.firstChild);
    aw.appendChild(document.createTextNode(msg));
}
function cr(id)
{
    var w = $(id + "Warn");
    if(w.firstChild != null)
        w.removeChild(w.firstChild); 
}