﻿// JScript 文件

function $(){
    var length = arguments.length;
    if(length>1){
        if(typeof(arguments[0])=="object"){
            return arguments[0].getElementsByTagName(arguments[1]);
        }
        for(var i = 0, elements = [];i < length; i++){
            elements.push($(arguments[i]));
        }
        return elements;
    }else{
        return document.getElementById(arguments[0]);
    }
}

String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
// 返回字符的长度，一个中文算2个
String.prototype.ChineseLength=function()
{ 
    return this.replace(/[^\x00-\xff]/g,"**").length;
}
// 判断字符串是否以指定的字符串结束
String.prototype.EndsWith = function(str) 
{
    return this.substr(this.length - str.length) == str;
}
// 去掉字符左端的的空白字符
String.prototype.LeftTrim = function()
{
    return this.replace(/(^[\\s]*)/g, "");
}
// 去掉字符右端的空白字符
String.prototype.RightTrim = function()
{
    return this.replace(/([\\s]*$)/g, "");
}
// 判断字符串是否以指定的字符串开始
String.prototype.StartsWith = function(str) 
{
    return this.substr(0, str.length) == str;
}
// 去掉字符两端的空白字符
String.prototype.Trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
   //[\u4E00-\u9FA5]为汉字﹐[\uFE30-\uFFA0]为全角符号 [\uFE30-\uFFA0\u4E00-\u9FA5\s]为全角汉字和符号  

String.prototype.ChkChinese = function()  //检测是否全部为汉字 允许空格和换行
{
   return /^[\uFE30-\uFFA0\u4E00-\u9FA5\s]+$/g.test(this);   
}

String.prototype.ChkBigCode = function()   //检测是否全部为全角字符目前未成功
{   
   return /^[\uFE30-\uFFA0\u4E00-\u9FA5\s]+$/g.test(this);   
}
String.prototype.FilterImage=function(){
return this.replace(/<img.*?>/gi,"");
}

String.prototype.IsMail=function()//E-mail值检测
{ 
    if(!this)return null;
    if(!isEnglish(this))
        return false;
    i=this.indexOf("@");
    j=this.lastIndexOf("@");
    if(i==-1)
        return false;
    if(i!=j)
        return false;
    if(i==this.length)
        return false;
    if(j==0)
        return false;
    return true;
}

String.prototype.IsPlusInt=function(empty)//数值检测
{
    if(empty){
        return /^[0-9]{0,}$/.test(this);  
    }else{
        return /^[0-9]{1,}$/.test(this);  
    }
}

Number.prototype.substring=function(index,lastindex)
{
	return this.toString().substring(index,lastindex)	
}
Number.prototype.substr=function(index,lastindex)
{
	return this.toString().substr(index,lastindex)	
}
Number.prototype.indexOf=function(Num)
{
	return this.toString().indexOf(Num.toString())	
}
Number.prototype.lastIndexOf=function(Num)
{
	return this.toString().lastIndexOf(Num.toString())	
}


function getXMLHttpRequest(){
  var XHR;
  
  if(window.ActiveXObject){
    try {
      XHR = new ActiveXObject("Msxml2.XMLHTTP") ;
    }
    catch(e){
      try{
        XHR = new ActiveXObject("Microsoft.XMLHTTP") ;
      }
      catch(e){
        XHR = false ;
      }
    }
  }
  else if(window.XMLHttpRequest)
    XHR = new XMLHttpRequest();

  return XHR ;
}

function ajax(url,fun){
 var xmlHttp = getXMLHttpRequest();
 
 if (!xmlHttp){
   return false;
  }
  
  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4){
      if (xmlHttp.status == 200){
        reply = xmlHttp.responseText;
        doOK(reply,fun);
      }
      else
      {
        doError(xmlHttp.status);
      }
    }
  }
  
  xmlHttp.open("get",url,true);
xmlHttp.setRequestHeader("If-Modified-Since","0");
//以上一句 禁止IE缓存
  xmlHttp.send(null);
}

function doOK(s,fun) {
  //返回结果处理
  eval(fun);
}

function doError(error) {
  //错误处理
  alert("意外错误！")
}
