/*  javascript框架   */
function $(){
	var elements = new Array();
	for( var i=0; i<arguments.length; i++){
		var element = arguments[i];
		if( typeof(element) == "string"){
			element = document.getElementById(element);
		}
		
		if( arguments.length = 1){
			return element;
		}
		else{
			elements.push(element);
		}		
	}
	
	return elements;
}


//删除
function Delete(){ return confirm('确认删除吗？'); }

if(typeof(HTMLElement)!="undefined")   //给firefox定义contains()方法，ie下不起作用
  {   
      HTMLElement.prototype.contains=function(obj)   
      {   
          while(obj!=null&&typeof(obj.tagName)!="undefind"){ //通过循环对比来判断是不是obj的父元素
  　　　　if(obj==this)  return true;   
  　　　　obj=obj.parentNode;
   　　}   
          return false;   
      };   
  }   


function SP(){  }
SP.Event=function(e) {
  var e = e || event;
  return e;
}
SP.srcElement=function(e) {
        var e = SP.Event(e);
        var obj = (e.srcElement) ? e.srcElement : e.target;
        return obj;
}
SP.toElement=function(e) {
        var e = SP.Event(e);
        var obj = (e.toElement) ? e.toElement : e.relatedTarget;
        return obj;
}
/*
替换图层效果
    obj : 自身对象
    NormalClassName : 普通样式  如果为0则是读取CssName的值
    OnClassName : 被选中的样式
    objects : 所有的对象(id中间用,隔开)
    OnID : 需要显示的对象
    同一级容器下不需要替换css的加上 NoSwitch="True"
使用方法
    OnMouseOver="(new SP()).Switch(this,null,'On','test1,test2','test1')";
*/
SP.prototype.Switch = function( obj , NormalClassName , OnClassName , objects , OnID){
    var parentObj = obj.parentNode;
    
    for(i=0; i<parentObj.childNodes.length; i++){ 
        if(parentObj.childNodes[i].tagName != undefined){
            if(NormalClassName != null)
            {  
            
                parentObj.childNodes[i].className = parentObj.childNodes[i].getAttribute("CssName");
              
                
                }
            else{
                if(parentObj.childNodes[i].getAttribute("NoSwitch") == null)
                     parentObj.childNodes[i].className = NormalClassName; 
                 else
                 parentObj.childNodes[i].className =parentObj.childNodes[i].getAttribute("NoSwitch");
            }
        }
    }
    var ids = objects.split(",");  
    for(i=0; i<ids.length; i++){ 
        if($(ids[i]) == null) {
              // alert(ids[i] + '对象不存在！')
        }
        else  $(ids[i]).style.display = 'none'; 
    }
    obj.className = OnClassName;
    $(OnID).style.display = 'block';
}

/*
重设图片大小 
    obj : 图片所在的容器
    width : 需要设置的宽度(可省略，默认为容器大小)
使用方法 (代码写在容器被加载之后): 
    (new SP()).ImageResize(obj,500);
*/
SP.prototype.ImageResize = function(obj , width){
    if(obj == null) return;
    var imgs = obj.getElementsByTagName("img");
    if(width == undefined){
        width = obj.offsetWidth - 20;
    }
    for(i=0; i<imgs.length; i++){
        imgs[i].style.cursor = "hand";
        imgs[i].onload = function(){    if(this.width > width) this.width=width;  }
        imgs[i].onclick = function(){   window.open(this.src);  }
    } 
}



var MyTag = null;
/*----------------------------------------*\ 
 * 使用 js 标记高亮关键词 
 * 参数说明: 
 * obj: 对象, 要进行高亮显示的html标签节点. 
 * hlWords: 字符串, 要进行高亮的关键词词, 使用 竖杠(|)或空格 分隔多个词 . 
 * cssClass: 字符串, 定义关键词突出显示风格的css伪类. 
 * 参考资料: javascript HTML DOM 高亮显示页面特定字词 By shawl.qiu
\*----------------------------------------*/ 
SP.prototype.MarkHighLight = function(obj ,  hlWords , cssClass,linktype){
    MyTag = null;
    hlWords=AnalyzeHighLightWords(hlWords);
    var linkType=linktype;
    if(obj==null || hlWords.length==0)
        return;
    if(cssClass==null)
        cssClass="highlight1";
    MarkHighLightCore(obj,hlWords,linktype);
    
    //------------执行高亮标记的核心方法----------------------------
    function MarkHighLightCore(obj,keyWords){
        var re=new RegExp(keyWords, "i");         
        for(var i=0; i<obj.childNodes.length; i++){        
            var childObj=obj.childNodes[i];
            if(childObj.nodeType==3){
                if(childObj.data.search(re)==-1)continue; 
                var reResult=new RegExp("("+keyWords+")", "gi"); 
                var objResult=document.createElement("span");
                objResult.innerHTML = childObj.data.replace(reResult,"<font class='"+cssClass+"'  onclick=\"Link('"+linkType+"','$1');\"  title='搜索更多$1相关信息' style=\"cursor:pointer\">$1</font>");
                
                var arrMactches = childObj.data.match(reResult);
                MyTag = Filter(arrMactches);
                if(childObj.data==objResult.childNodes[0].innerHTML) continue; 
                obj.replaceChild(objResult,childObj);                                      
            }else if(childObj.nodeType==1){
                MarkHighLightCore(childObj , keyWords);
            }
        }
    }   
    


    //----------分析关键词----------------------
    function AnalyzeHighLightWords(hlWords)
    {
        if(hlWords==null) return "";
        hlWords=hlWords.replace(/\s+/g,"|").replace(/\|+/g,"|");            
        hlWords=hlWords.replace(/(^\|*)|(\|*$)/g, "");
        
        if(hlWords.length==0) return "";
        var wordsArr=hlWords.split("|"); 
        
        if(wordsArr.length>1){
            var resultArr=BubbleSort(wordsArr);
            var result="";
            for(var i=0;i<resultArr.length;i++){
                result=result+"|"+resultArr[i];
            }                
            return result.replace(/(^\|*)|(\|*$)/g, "");

        }else{
            return hlWords;
        } 
    }    
    
    //-----利用冒泡排序法把长的关键词放前面-----    
    function BubbleSort(arr){        
        var temp, exchange;    
        for(var i=0;i<arr.length;i++){            
            exchange=false;                
            for(var j=arr.length-2;j>=i;j--){                
                if((arr[j+1].length)>(arr[j]).length){                    
                    temp=arr[j+1]; arr[j+1]=arr[j]; arr[j]=temp;
                    exchange=true;
                }
            }                
            if(!exchange)break;
        }
        return arr;            
    }
    
    //----过滤重复元素
    function Filter(array){
        var MyArray = new Array();
        for(i=0; i<array.length; i++){
            var isRepeat = false;
            for(n=0; n<MyArray.length; n++){
                if(MyArray[n] == array[i])
                   isRepeat = true; 
            }
            if(!isRepeat){ MyArray[MyArray.length] = array[i];  }
        }
        return MyArray;        
    }

}
/**********************************************数组********************************************************/
SP.Array = {
    match: function(text, array) {
        var arrResult = new Array();
        if (text != "") {
            for (var i = 0; i < array.length; i++) {
                if (array[i].toUpperCase().indexOf(text.toUpperCase()) > -1) {
                    arrResult.push(array[i]);
                }
            }
        }
        return arrResult;
    },
    toString: function(array) {
        var string = "";
        var i = 0;
        for (var key in array) {
            string += key + "=" + array[key] + "&";
            i++;
        }
        if (i > 0)
            string = string.substring(0, string.length - 1);
        return string;
    }
}
/**********************************数字*******************************************/
Number.prototype.toMoney =function(){
   var v = this+"";
   if(v.indexOf(".")<0)
      v = v+".00";
   else{
      var s = v.split('.');
      if(s[1].length==1)
         v+="0";
      if(s[1].length>2)
      {
          v = s[0] + "." +  s[1].substring(0,2);
      }
   }
   return v;
}
/**********************************************字符串********************************************************/

String.prototype.removeCode = function() {
    var str = this;
    str = str.replace(/<[^>]+>/g, "");
    str = str.replace(/\[[^>]+\]/g, "");
    str = str.replace(/&nbsp;|&gt;|&lt;/g, "");
    str = str.replace(/<|>|\r|\n/g, "");
    return str;
}
String.prototype.removeHtml = function() {
    var str = this;
    str = str.replace(/<[^>]+>/g, "");
    return str;
}
String.prototype.removeBR=function(){
        var str = this;
        if(str.length==0) return;
        str = str.replace(/\n/g,"");
        str = str.replace(/\n\r|\r\n/g,"     ");
        return str;
}
String.prototype.toBR=function(){
        var str = this;
        if(str.length==0) return;
        str = str.replace(/\n|\n\r|\r\n/g,"<br />");
        return str;
}
String.prototype.unUBB=function(){
        var str = this;
        if(str.length==0) return;
        str = str.replace("&lt;", "<");
        str = str.replace("&gt;", ">");
        str = str.replace("&nbsp;", " "); 
        return str;
}

String.prototype.trim = function() {
     return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.checkStrong = function() {
    var password = this;
    if (password.length <= 6)
        return 0; 
    var Modes = 0;
    for (i = 0; i < password.length; i++) {
        Modes |= CharMode(password.charCodeAt(i));
    }
    return bitTotal(Modes);
}
String.prototype.isDate = function() {
    var str = this;
    var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
    var r = str.match(reg);
    if (r == null) return false;
    var d = new Date(r[1], r[3] - 1, r[4]);
    var newstr = d.getFullYear() + "" + (d.getMonth() + 1) + "" + d.getDate();
    str = str.replace(/(-|\/)0{0,1}/g, "");
    return str == newstr;
}
String.prototype.isAccount = function() {
    var reg = /^[A-Za-z0-9|_|-]{3,20}$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isDomain = function() {
    var reg = /^[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isEmail = function() {
    var reg = /^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isPhone = function() {
    var reg = /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isMobile = function() {
    var reg = /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isUrl = function() {
    var reg = /^http:\/\/[A-Za-z0-9\-]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isIdCard = function() {
    var reg = /^\d{15}(\d{2}[A-Za-z0-9])?$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isCurrency = function() {
    var reg = /^\d+(\.\d+)?$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isNumber = function() {
    var reg = /^\d+$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isZip = function() {
    var reg = /^[1-9]\d{5}$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isQQ = function() {
    var reg = /^[1-9]\d{4,8}$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isInt = function() {
    var reg = /^[-\+]?\d+$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isDouble = function() {
    var reg = /^[-\+]?\d+(\.\d+)?$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isEnglish = function() {
    var reg = /^[A-Za-z]+$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.isChinese = function() {
    var reg = /^[\u0391-\uFFE5]+$/;
    return this.match(reg) == null ? false : true;
}
String.prototype.getBytes = function() {　　
　　var cArr = this.match(/[^x00-xff]/ig);　　
　　return this.length + (cArr == null ? 0 : cArr.length);　　
}
String.prototype.toTime = function() {　　
　　var str = this;
　　var a = str.split(':');
　  return　new Date(0,0,0,a[0],a[1],a[2]);
}
String.prototype.getTotalSeconds = function() {　　

　  var str = this;
　  var t = str.toTime();
　  var h = t.getHours()*3600 + t.getMinutes()*60 + t.getSeconds();
　　return h;            
}
//测试某个字符是属于哪一类.  
function CharMode(iN){  
    if (iN>=48 && iN <=57) //数字  
    return 1;  
    if (iN>=65 && iN <=90) //大写字母  
    return 2;  
    if (iN>=97 && iN <=122) //小写  
    return 4;  
    else  
    return 8; //特殊字符  
}  
//计算出当前密码当中一共有多少种模式  
function bitTotal(num){  
    modes=0;  
    for (i=0;i<4;i++){  
      if (num & 1) modes++;  
      num>>>=1;  
    }  
    return modes;  
}  
