﻿//浏览器兼容函数
if(window.HTMLElement){
	//insertAdjacentHTML
	HTMLElement.prototype.insertAdjacentHTML=function(where, html) 
	{ 
		var e=this.ownerDocument.createRange(); 
		e.setStartBefore(this); 
		e=e.createContextualFragment(html); 
		switch (where.toLowerCase()) 
		{ 
			case 'beforebegin': this.parentNode.insertBefore(e, this);break; 
			case 'afterbegin': this.insertBefore(e, this.firstChild); break; 
			case 'beforeend': this.appendChild(e); break; 
			case 'afterend': 
			if(!this.nextSibling) this.parentNode.appendChild(e); 
			else this.parentNode.insertBefore(e, this.nextSibling); break; 
		} 
	};

    //附加属性
    HTMLElement.prototype.__defineGetter__("menuindex",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("menuindex").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("menuindex",function(v){
        	var attrs=this.setAttribute("menuindex",v);
        });
        
    HTMLElement.prototype.__defineGetter__("mustdate",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("mustdate").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("mustdate",function(v){
        	var attrs=this.setAttribute("mustdate",v);
        });
        
    HTMLElement.prototype.__defineGetter__("formgroupname",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("formgroupname").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("formgroupname",function(v){
        	var attrs=this.setAttribute("formgroupname",v);
        });

    HTMLElement.prototype.__defineGetter__("noblank",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("noblank").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("noblank",function(v){
        	var attrs=this.setAttribute("noblank",v);
        });

    HTMLElement.prototype.__defineGetter__("mustupload",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("mustupload").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("mustupload",function(v){
        	var attrs=this.setAttribute("mustupload",v);
        });

    HTMLElement.prototype.__defineGetter__("mustlen",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("mustlen").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("mustlen",function(v){
        	var attrs=this.setAttribute("mustlen",v);
        });

    HTMLElement.prototype.__defineGetter__("mustlink",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("mustlink").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("mustlink",function(v){
        	var attrs=this.setAttribute("mustlink",v);
        });

    HTMLElement.prototype.__defineGetter__("mustzipcode",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("mustzipcode").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("mustzipcode",function(v){
        	var attrs=this.setAttribute("mustzipcode",v);
        });

    HTMLElement.prototype.__defineGetter__("mustpersonalno",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("mustpersonalno").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("mustpersonalno",function(v){
        	var attrs=this.setAttribute("mustpersonalno",v);
        });

    HTMLElement.prototype.__defineGetter__("mustemail",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("mustemail").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("mustemail",function(v){
        	var attrs=this.setAttribute("mustemail",v);
        });

    HTMLElement.prototype.__defineGetter__("mustmobile",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("mustmobile").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("mustmobile",function(v){
        	var attrs=this.setAttribute("mustmobile",v);
        });

    HTMLElement.prototype.__defineGetter__("mustnumeric",function(){
    		var attr;
     		var _r=null;
        	var attrs=this.attributes;
        	for(var i=0;i<attrs.length;i++){
            	if(attrs[i].name.toLowerCase()==("mustnumeric").toLowerCase())
            	{
            		_r=attrs[i].value;
            		break;
            	}
        	}
        	return _r;
        	
        });
    HTMLElement.prototype.__defineSetter__("mustnumeric",function(v){
        	var attrs=this.setAttribute("mustnumeric",v);
        });
        
	
	
	
	//ourtHTML
    HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML){
        var r=this.ownerDocument.createRange();
        r.setStartBefore(this);
        var df=r.createContextualFragment(sHTML);
        this.parentNode.replaceChild(df,this);
        return sHTML;
        });

    HTMLElement.prototype.__defineGetter__("outerHTML",function(){
     	var attr;
        var attrs=this.attributes;
        var str="<"+this.tagName.toLowerCase();
        for(var i=0;i<attrs.length;i++){
            attr=attrs[i];
            if(attr.specified)
                str+=" "+attr.name+'="'+attr.value+'"';
            }
        if(!this.canHaveChildren)
            return str+">";
        return str+">"+this.innerHTML+"</"+this.tagName.toLowerCase()+">";
        });
        
 	HTMLElement.prototype.__defineGetter__("canHaveChildren",function(){
  		switch(this.tagName.toLowerCase()){
            case "area":
            case "base":
         	case "basefont":
            case "col":
            case "frame":
            case "hr":
            case "img":
            case "br":
            case "input":
            case "isindex":
            case "link":
            case "meta":
            case "param":
            return false;
        }
        return true;
     });
}

function FormIsempty(_s)
{
	if(!_s) return true;
	if(_s.replace(/(^\s*)|(\s*$)/g, "")=="") return true;
	return false;
}

function FormIsdate(strDate)
{
    var strCheckDate = strDate+"";     //进一步确认哪来判断的肯定是一串字符串
         
    if(strCheckDate == "")        //空字符串,不是合法的日期字符串，返回false
    {
        return false;
    } 
    debugger;
    //判断传进来的数据是那种格式写成日期
    var intIndex = -1;         //利用正则表达式，查找字符串中是否包含某个字符，没找到为-1,否则为 （0 - String.length - 1）
    var arrDate;          //分别存储年月日
    var regExpInfo = /\./;        //正则表达式，匹配第一个出现 "."的位置
         
    //在这里，我之所以不使用replace函数把所有的"."和"/"换成"-",然后分别存储年月日，是因为用户有可能输入 2001/3-2,就判断不出它是不合法日期了
    intIndex = strCheckDate.search(regExpInfo);   //查找是否含有 "."
    if(intIndex == - 1)         //不包含  
    {
        regExpInfo = /-/;
        intIndex = strCheckDate.search(regExpInfo);
          
        if(intIndex == -1)
        {
            regExpInfo = /\//;       //查找是否含有 "/"
            intIndex = strCheckDate.search(regExpInfo); 
           
            if(intIndex == -1)
            {
                arrDate = new Array();  //只包含年或格式为20010307
                if(strCheckDate.length==4)
                {
                    arrDate[0]=strCheckDate;
                    //window.alert(arrDate[0]);
                }
                else if(strCheckDate.length==6)
                {
                    arrDate[0]=strCheckDate.substring(0,4);
                    arrDate[1]=strCheckDate.substring(4,6);
               
                }
                else if(strCheckDate.length==8)
                {
                    arrDate[0]=strCheckDate.substring(0,4);
                    arrDate[1]=strCheckDate.substring(4,6);
                    arrDate[2]=strCheckDate.substring(6,8);
                
                }
                else
                {
                    return false;
                }
            }
            else
            {
                arrDate = strCheckDate.split("/");  //2001/3/7 型
            }
        }
        else
        {
            arrDate = strCheckDate.split("-");   //2001-3-7 型
        }
    }
    else
    {
        arrDate = strCheckDate.split(".");    //2001.3.7 型
    }
         
    if(arrDate.length > 3)        //如果分离出来的项超过3，除了年月日还有其它的，不合法日期，返回false
    {
        return false;
    }
    else if(arrDate.length > 0) 
    {
        //判断年是否合法
        if(fnIsIntNum(arrDate[0]))   //是正整数
        {
            if(parseInt(arrDate[0]) < 1 || parseInt(arrDate[0]) > 9999)  //年范围为1 - 9999
            {
                return false;
            } 
        }
        else
        {
            return false;     //年不是正整数，错误
        }
           
        //判断月是否合法
        if(arrDate.length > 1)
        {
            if(fnIsIntNum(arrDate[1]))  //是正整数
            {
                if(parseInt(arrDate[1]) < 1 || parseInt(arrDate[1]) > 12)
                {
                    return false;
                } 
            }
            else
            {
                return false;
            }
        }
        
           
        //判断日是否合法
        if(arrDate.length > 2)
        {
            var tmpDate=Trim(arrDate[2]);
            var tmpTime="";
            if(tmpDate.indexOf(" ")>0){
                aTmpDateTime=tmpDate.split(" ");
                if(aTmpDateTime.length!=2) return false;
                tmpDate=aTmpDateTime[0];
                tmpTime=aTmpDateTime[1];
                tmpTime=tmpTime.split(":");
                if(tmpTime.length!=3) return false;
                
                if(!FormIsnumeric(tmpTime[0])) return false;
                if(parseInt(tmpTime[0])<0||parseInt(tmpTime[0])>23) return false;
                
                if(!FormIsnumeric(tmpTime[1])) return false;
                if(parseInt(tmpTime[1])<0||parseInt(tmpTime[1])>59) return false;
                
                if(!FormIsnumeric(tmpTime[2])) return false;
                if(parseInt(tmpTime[2])<0||parseInt(tmpTime[2])>59) return false;
                
            }
            
            if(fnIsIntNum(tmpDate))  //是正整数
            {
                var intDayCount = fnComputerDay(parseInt(arrDate[0]),parseInt(arrDate[1]));
                if(intDayCount < parseInt(tmpDate))
                {
                    return false;
                }   
            }
            else
            {
                return false;
            }
        }
            
    }
    return true;
}

//**********************************************************************************************************
//判断一个数是否为正整数
//参数：strNum ---- 需要判断的字符串
//返回值：true ---- 整数 false ---- 非整数
function fnIsIntNum(strNum)
{
    var strCheckNum = strNum + "";
    if(strCheckNum.length < 1)  return false;
    else{
        if(isNaN(strCheckNum)) return false;
        else{
            if(parseInt(strCheckNum) < 1) return false; 
            else{
                if(parseFloat(strCheckNum) > parseInt(strCheckNum)) return false;
            }
        }
    }
    return true;
}

//判断一个数是否为数字
function FormIsnumeric(strNum)
{
    var strCheckNum = strNum + "";
    if(strCheckNum.length < 1)  return false;
    else{
        if(isNaN(strCheckNum)) return false;
    }
    return true;
}

//**********************************************************************************************************
//功能：判断intYear年intMonth月的天数
//返回值：intYear年intMonth月的天数
function fnComputerDay(intYear,intMonth)
{
    var dtmDate = new Date(intYear,intMonth,-1);
    var intDay = dtmDate.getDate() + 1;
    
    return intDay;    
}
//验证Email是否正确
function FormIsemail(fData)
{
    if(Trim(fData).indexOf(" ")>=0) return false;
    var reEmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
    return fData.match(reEmail);
}

function Trim(s){
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}


//兼容JSDATEDIFF
function JsDateDiff(sDate1, sDate2){  //sDate1和sDate2是2002-12-18格式 目前只允许取天数
    sDate1=Trim(sDate1).split(" ")[0];
    sDate2=Trim(sDate2).split(" ")[0];
    var aDate, oDate1, oDate2, iDays;
    aDate = sDate1.split("-");
    oDate1 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]);  //转换为12-18-2002格式
    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]);

    iDays = parseInt((oDate2 - oDate1) / 1000 / 60 / 60 /24);  //把相差的毫秒数转换为天数
    return iDays;
}


//浏览器兼容函数 结束

