/**
 * 数据验证框架.增加了对id字段检查出错时,直接在对应后面添加一< span>元素来显示错误信息.
 * 
 * @author liuchen
 * @version 1.0
 * @description 2011-03-01
 */
var checkData = new function() {
    var idExt="_Span";// 生成span层的id后缀
    /**
	 * 得到中英文字符长(中文为3个字符)
	 * @param {} str
	 * @return 字符长
	 */
    this.length = function(str) {
    	var p1 = new RegExp('%u.', 'g');// UTF-8编码下一个汉字是3个字节
    	// var p1 = new RegExp('%u..', 'g')//GBK编码下一个汉字是2个字节
        var p2 = new RegExp('%.', 'g');
        return escape(str).replace(p1, '').replace(p2, '').length;
    }

    /**
	 * 删除对应id元素
	 */
    this.remove = function(id) {
        var idObject = document.getElementById(id);
        if (idObject != null)
            idObject.parentNode.removeChild(idObject);
    }

    /**
	 * 在对应id后面错误信息
	 * @param id:需要显示错误信息的id元素 str:显示错误信息
	 */
    this.appendError = function(id, str) {
        this.remove(id + idExt);// 如果span元素存在，则先删除此元素
        var spanNew = document.createElement("span");// 创建span
        spanNew.id = id + idExt;// 生成spanid
        spanNew.style.color = "red";
        spanNew.appendChild(document.createTextNode(str));// 给span添加内容
        var inputId = document.getElementById(id);
        inputId.parentNode.insertBefore(spanNew, inputId.nextSibling);// 给需要添加元素后面添加span
    }

    /**
	 * @description 过滤所有空格字符。
	 * @param str:需要去掉空间的原始字符串
	 * @return 返回已经去掉空格的字符串
	 */
    this.trimSpace = function(str) {
        str += "";
        while ((str.charAt(0) == ' ') || (str.charAt(0) == '???') || (escape(str.charAt(0)) == '%u3000')){
            str = str.substring(1, str.length);
        }
        while ((str.charAt(str.length - 1) == ' ') || (str.charAt(str.length - 1) == '???') || (escape(str.charAt(str.length - 1)) == '%u3000')){
            str = str.substring(0, str.length - 1);
        }
        return str;
    }

    /**
	 * 过滤字符串开始部分的空格\字符串结束部分的空格\将文字中间多个相连的空格变为一个空格
	 * @param {Object} inputString
	 */
    this.trim = function(inputString) {
        if (typeof inputString != "string") {
            return inputString;
        }
        var retValue = inputString;
        var ch = retValue.substring(0, 1);
        while (ch == " ") {
            // 检查字符串开始部分的空格
            retValue = retValue.substring(1, retValue.length);
            ch = retValue.substring(0, 1);
        }
        ch = retValue.substring(retValue.length - 1, retValue.length);
        while (ch == " ") {
            // 检查字符串结束部分的空格
            retValue = retValue.substring(0, retValue.length - 1);
            ch = retValue.substring(retValue.length - 1, retValue.length);
        }
        while (retValue.indexOf(" ") != -1) {
            // 将文字中间多个相连的空格变为一个空格
            retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ") + 1, retValue.length);
        }
        return retValue;
    }

    /**
	 * 过滤字符串,指定过滤内容，如果内容为空，则默认过滤 '~!@#$%^&*()-+."
	 * 
	 * @param {Object}
	 *            str
	 * @param {Object}filterStr
	 * @return 包含过滤内容，返回True,否则返回false;
	 */
    this.filterStr = function(str, filterString) {
        filterString = filterString == "" ? "'~!@#$%^&*()-+.\"" : filterString
        var ch;
        var i;
        var temp;
        var error = false;// 当包含非法字符时，返回True
        for (i = 0; i <= (filterString.length - 1); i++) {
            ch = filterString.charAt(i);
            temp = str.indexOf(ch);
            if (temp != -1) {
                error = true;
                break;
            }
        }
        return error;
    }

    this.filterStrSpan = function(id, filterString) {
        filterString = filterString == "" ? "'~!@#$%^&*()-+.\"" : filterString
        var val = document.getElementById(id);
        if (this.filterStr(val.value, filterString)) {
            var str = "不能包含非法字符" + filterString; 
            this.appendError(id, str);
            return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }

    /**
	 * 检查是否为网址
	 * 
	 * @param {}
	 *            str_url
	 * @return {Boolean} true：是网址，false:<b>不是</b>网址;
	 */
    this.isURL = function(str_url) {// 验证url
        var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
                + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" // ftp的user@
                + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
                + "|" // 允许IP和DOMAIN（域名）
                + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
                + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
                + "[a-z]{2,6})" // first level domain- .com or .museum
                + "(:[0-9]{1,4})?" // 端口- :80
                + "((/?)|" // a slash isn't required if there is no file name
                + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
        var re = new RegExp(strRegex);
        return re.test(str_url);
    }

    this.isURLSpan = function(id) {
        var val = document.getElementById(id);
        if (!this.isURL(val.value)) {
            var str = "链接不符合格式;";
            this.appendError(id, str);
            return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }

    /**
	 * 检查是否为电子邮件
	 * 
	 * @param {}
	 *            str
	 * @return {Boolean} true：电子邮件，false:<b>不是</b>电子邮件;
	 */
    this.isEmail = function(str) {
        var re = /^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
        return re.test(str);
    }

    this.isEmailSpan = function(id) {
        var val = document.getElementById(id);
        if (!this.isEmail(val.value)) {
            var str = "邮件不符合格式;";
            this.appendError(id, str);
            return false;
        } else {alert("ddd");
            this.remove(id + idExt);
            return true;
        }
    }

    /**
	 * 检查是否为数字
	 * 
	 * @param {}
	 *            str
	 * @return {Boolean} true：数字，false:<b>不是</b>数字;
	 */
    this.isNum = function(str) {
        var re = /^[\d]+$/;
        return re.test(str);
    }

    this.isNumSpan = function(id) {
        var val = document.getElementById(id);
        if (!this.isNum(val.value)) {
            var str = "必须是数字;";
            this.appendError(id, str);
            return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }

    /**
	 * 检查数值是否在给定范围以内,为空,不做检查<br>
	 * @param {}str_num
	 * @param {}small 应该大于或者等于的数值（此值为空时，只检查不能大于最大值）
	 * @param {}big 应该小于或者等于的数值（此值为空时，只检查不能小于最小值）
	 * @return {Boolean} <b>小于最小数值或者大于最大数值</b>数字返回false 否则返回true;
	 */
    this.isRangeNum = function(str_num, small, big) {
        if (!this.isNum(str_num)){ // 检查是否为数字
            return false
        }
        if (small == "" && big == ""){
            throw str_num + "没有定义最大，最小值数字！";
        }
        if (small != "") {
            if (str_num < small){
                return false;
            }
        }
        if (big != "") {
            if (str_num > big){
                return false;
            }
        }
        return true;

    }

    this.isRangeNumSpan = function(id, small, big) {
        var val = document.getElementById(id);
        if (!this.isRangeNum(val.value, small, big)) {
            var str = "";
            if (small != "") {
                str = "应该大于或者等于 " + small;
            }
            if (big != "") {
                str += " 应该小于或者等于 " + big;
            }
            this.appendError(id, str);
            return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }

    /**
	 * 检查是否为合格字符串(不区分大小写)<br>
	 * 是由a-z0-9_组成的字符串
	 * @param {} str 检查的字符串
	 * @param {}idStr 光标定位的字段ID<b>只能接收ID</b>
	 * @return {Boolean} <b>不是</b>"a-z0-9_"组成返回false,否则返回true;
	 */
    this.isLicit = function(str) {
        var re = /^[_0-9a-zA-Z]*$/;
        return re.test(str);
    }

    this.isLicitSpan = function(id) {
        var val = document.getElementById(id);
        if (!this.isLicit(val.value)) {           
            var str = "是由a-z0-9_组成的字符串(不区分大小写);";
            this.appendError(id, str);
            return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }

    /**
	 * 检查二个字符串是否相等
	 * 
	 * @param {}str1 第一个字符串
	 * @param {}str2 第二个字符串
	 * @return {Boolean} 字符串不相等返回false,否则返回true;
	 */
    this.isEquals = function(str1, str2) {
        return str1 == str2;
    }

    this.isEqualsSpan = function(id, id1) {
        var val = document.getElementById(id);
        var val1 = document.getElementById(id1);
        if (!this.isEquals(val.value, val1.value)) {
            var str = "两次输入内容必须一样;";
            this.appendError(id, str);
            return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }

    /**
	 * 检查字符串是否在给定长度范围以内(中文字符以2个字节计算),字符为空,不做检查<br>
	 * @param {} str 检查的字符
	 * @param {} lessLen 应该大于或者等于的长度
	 * @param {} moreLen 应该小于或者等于的长度
	 * @return {Boolean} <b>小于最小长度或者大于最大长度</b>数字返回false;
	 */
    this.isRange = function(str, lessLen, moreLen) {
        var strLen = this.length(str);
        if (lessLen != "") {
            if (strLen < lessLen){
                return false;
            }
        }
        if (moreLen != "") {
            if (strLen > moreLen){
                return false;
            }
        }
        if (lessLen == "" && moreLen == ""){
	        throw "没有定义最大最小长度!";
			return true;
        }
    }

    this.isRangeSpan = function(id, lessLen, moreLen) {
        var val = document.getElementById(id);
        if (!this.isRange(val.value, lessLen, moreLen)) {
              return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }

    /**
	 * 检查字符串是否小于给定长度范围(中文字符以2个字节计算)<br>
	 * @param {}  str 字符串
	 * @param {}lessLen 小于或等于长度
	 * @return {Boolean} <b>小于给定长度</b>数字返回false;
	 */
    this.isLess = function(str, lessLen) {
        return this.isRange(str, lessLen, "");
    }

    this.isLessSpan = function(id, lessLen) {
        var val = document.getElementById(id);
        if (!this.isLess(val.value, lessLen)) {
            var str = "长度大于或者等于 " + lessLen;
            this.appendError(id, str);
            return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }

    /**
	 * 检查字符串是否大于给定长度范围(中文字符以2个字节计算)<br>
	 * @param {} str 字符串
	 * @param {} moreLen 小于或等于长度
	 * @return {Boolean} <b>大于给定长度</b>数字返回false;
	 */
    this.isMore = function(str, moreLen) {
        return this.isRange(str, "", moreLen);
    }

    this.isMoreSpan = function(id, moreLen) {
        var val = document.getElementById(id);
        if (!this.isMore(val.value, moreLen)) {
            var str = "长度应该小于或者等于 " + moreLen;
            this.appendError(id, str);
            return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }

	this.funcChina = function(id) {
		var element = document.getElementById(id);
		if (element.value.length>0 && element.value.match(/[\x01-\xFF]*/)==false){
			var str = "不能含有汉字;";
	        this.appendError(id, str);
			return false;
		}
		return true;
	}
	this.checkChinese = function(id) {
		var element = document.getElementById(id);
		if(element.value.length != 0 && element.value.search(/[^a-zA-Z_0-9\u4E00-\u9FA5\s]/g) != -1) {
			var str = "只能输入汉字、字母和数字;";
	        this.appendError(id, str);
			return false;
		}
		return true;
	}
	this.checkChineseA = function(id) {
		var element = document.getElementById(id);
		if(element.value.length != 0 && element.value.search(/[^a-zA-Z_\u4E00-\u9FA5_\s]/g) != -1) {
			var str = "只能输入汉字和字母;";
	        this.appendError(id, str);
			return false;
		}
		return true;　　 
	}
	this.checkChineseB = function(id) {
		var element = document.getElementById(id);
		if(element.value.length != 0 && element.value.search(/[^\u4E00-\u9FA5_\s]/g) != -1) {
			var str = "只能输入汉字;";
	        this.appendError(id, str);　　 
			return false;　　 
		}　　 
		return true;　　 
	}
	this.checkChineseC = function(id) {
		var element = document.getElementById(id);
		if(element.value.length != 0 && element.value.search(/[^a-zA-Z_\s]/g) != -1) {
			var str = "只能输入字母;";
	        this.appendError(id, str);　　 
			return false;　　 
		}　　 
		return true;　　 
	}
	// 只能输入汉字、字母和数字，并且没有红字提示
	this.checkChineseD = function(id) {
		var element = document.getElementById(id);
		if(element.value.length != 0 && element.value.search(/[^a-zA-Z_0-9\u4E00-\u9FA5\s]/g) != -1) {
			return false;
		}
		return true;
	}

    /**
	 * 检查字符不为空
	 * @param {} str
	 * @return {Boolean} <b>字符为空</b>返回true,否则为false;
	 */
    this.isEmpty = function(str) {
        return str == "";
    }

    this.isEmptySpan = function(id) {
    	var val = document.getElementById(id);
        if (this.isEmpty(val.value)) {
            var str = "不允许为空;";
            this.appendError(id, str);
            return false;
        } else {
            this.remove(id + idExt);
            return true;
        }
    }
    // 判断是否为身份证
	this.isIdCard = function(id) {
		var idCard = document.getElementById(id);
			var v_idCard = killSpace(idCard);
			if (v_idCard == "") {
				return false;
			}
			if (v_idCard.length != 15 && v_idCard.length != 18) {
				var str = "身份证号码必须是15位或者18位;";
				this.appendError(id, str);
				return false;
			}
			if (!isInteger(v_idCard)) {
				var str = "请输入正确身份证号码;";
				this.appendError(id, str);
				return false;
			} else {
					this.remove(id + idExt);
					return true;
			}
	}
	this.isDate = function(id) {
		var s = document.getElementById(id);
			if (s.length != 10 || s.charAt(4) != "-" || s.charAt(7) != "-") {
				var str = "请输入正确的日期;";
				this.appendError(id, str);
				return false;
			}
			if (!isInteger(s.substring(0, 4)) || !isInteger(s.substring(5, 7)) || !isInteger(s.substring(8, 10)) || eval(s.substring(0, 4)) < 1 || eval(s.substring(5, 7)) < 1 || eval(s.substring(8, 10)) < 1) {
				var str = "请输入正确的日期;";            
				this.appendError(id, str);
				return false;
			}
			if (checkDate(s.substring(0, 4), s.substring(5, 7), s.substring(8, 10)) == false) {
				var str = "请输入正确的日期;";
				this.appendError(id, str);
				return false;
			}
			this.remove(id + idExt);
			return true;
	}
	// mobilePhone的校验
	this.isMobilePhone = function(id){
		var s = document.getElementById(id);
		var flag;
		var mobilePhoneRegS=/^(013|015|018|13|15|18)\d{9}$/;
		if (!mobilePhoneRegS.exec(s.value)) {
			// var str = "请输入正确的手机号码;";
    		// this.appendError(id, str);
    		return false;
		}
		return true;
	}
	// phone的校验
	this.isPhone = function(id){
		var s = document.getElementById(id);
		var flag;
		var phoneRegS=/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/;
		if (!phoneRegS.exec(s.value)) {
		// var str = "请输入正确的电话号码;";
		// this.appendError(id, str);
			return false;
		}
		return true;
	}
}

// 检验日期是否合法
function checkDate(year, month, day) {
	var iyear;
	var imonth;
	var iday;
	if (year.length != 4 || month.length != 2 || day.length != 2) {
		return false;
	}
	if (!isInteger(year) || !isInteger(month) || !isInteger(day)) {
		return false;
	}
	iyear = getValueOfInt(year);
	imonth = getValueOfInt(month);
	iday = getValueOfInt(day);
	if (imonth < 1 || imonth > 12) {
		return false;
	}
	switch (imonth) {
	  case 1:
	  case 3:
	  case 5:
	  case 7:
	  case 8:
	  case 10:
	  case 12:
		if (iday > 31) {
			return false;
		}
		break;
	  case 4:
	  case 6:
	  case 9:
	  case 11:
		if (iday > 30) {
			return false;
		}
		break;
	  default:
		if (mod(iyear, 4) == 0 && (mod(iyear, 100) != 0 || mod(iyear, 400) == 0)) {// 判断是否润年
			if (iday > 29) {
				return false;
			}
		} else {
			if (iday > 28) {
				return false;
			}
		}
	}
	return true;
}

function mod(var1, var2) {
	return (var1 % var2);
}

// 判断是否整数
function isInteger(integer) {
	var count;
	var numchar;
	var numvalue;
	for(count = 0; count < integer.length; count++) {
		numchar = integer.charAt(count);
		numvalue = numchar - "0";
		if (!(numvalue >= 0 && numvalue <= 9)) {
			return false;
		}
	}
	return true;
}

// 只输入数字
$.fn.numeral = function() {   
            $(this).css("ime-mode", "disabled");   
            this.bind("keypress",function(e) {   
            var code = (e.keyCode ? e.keyCode : e.which);  // 兼容火狐 IE
                if(!$.browser.msie&&(e.keyCode==0x8))  // 火狐下不能使用退格键
                {   
                     return ;   
                    }   
                    return code >= 48 && code<= 57;   
            });   
            this.bind("blur", function() {   
                if (this.value.lastIndexOf(".") == (this.value.length - 1)) {   
                    this.value = this.value.substr(0, this.value.length - 1);   
                } else if (isNaN(this.value)) {   
                    this.value = "";   
                }   
            });   
            this.bind("paste", function() {   
                var s = clipboardData.getData('text');   
                if (!/\D/.test(s));   
                value = s.replace(/^0*/, '');   
                return false;   
            });   
            this.bind("dragenter", function() {   
                return false;   
            });   
            this.bind("keyup", function() {   
            if (/(^0+)/.test(this.value)) {   
                this.value = this.value.replace(/^0*/, '');   
                }   
            });   
 };  

function isPicture(fileName){
	 if(fileName!=null && fileName !=""){
	  	 if(fileName.lastIndexOf(".")!=-1) {
		   var fileType = (fileName.substring(fileName.lastIndexOf(".")+1,fileName.length)).toLowerCase();
		   var suppotFile = new Array();
		   suppotFile[0] = "jpg";
		   suppotFile[1] = "gif";
		   suppotFile[2] = "bmp";
		   suppotFile[3] = "png";
		   suppotFile[4] = "jpeg";
		   for (var i =0;i<suppotFile.length;i++) {
			    if (suppotFile[i]==fileType) {
			     return true;
			    } else{
			     continue;
			    }
		   }
		   		return false;
		   } else{
			   	return false;
		   }
      }
}
function widthCheck(s, n){ 
	var w = 0; 
	for (var i=0; i<s.length; i++) { 
	   var c = s.charCodeAt(i); 
	   // 单字节加1
	   if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) { 
	    w++; 
	   } 
	   else { 
	    w+=2; 
	   } 
	} 
	if (w > n) { 
	   return false; 
	} 
	return true; 
}
	
function  clearPic(id,hid){
	var Browser_Agent=navigator.userAgent; 
	if(Browser_Agent.indexOf("Firefox")!=-1) {
		$("#"+id).attr("value","");
	}else{
		var file = $("#"+id);   
		file.replaceWith(file.clone()); 
	}
	 $("#"+hid).attr("value","");
	 return true;
}
	
// 显示图片
function preview(imgFile,num,previewId,width,height){ 		
	// 预览代码，支持 IE6、IE7。
	var newPreview = document.getElementById(previewId); 
	var t ; 
	if(document.all){ // IE
		t = imgFile.value;
	}else{
		t = imgFile.files[0].getAsDataURL(); // FF
		newPreview.style.backgroundImage = "url(" + t + ")";// imgFile.value;
		newPreview.style.width = width; 
		newPreview.style.height = height; 
		newPreview.style.display = "block"; 
	}
} 
			
// 在file 上onchange 调用
function showImg(o,imgId,previewId,divId,width,height) { 
	 var Browser_Agent=navigator.userAgent; 
	 if(Browser_Agent.indexOf("Firefox")!=-1) {
		 document.getElementById(divId).style.display = ""; 
		 // 火狐使用
		 document.getElementById(imgId).src=o.files[0].getAsDataURL(); 
		 document.getElementById(imgId).style.display = "block"; 
		 document.getElementById(previewId).style.display = "none"; 
	 }else{  
	     document.getElementById(divId).style.display = ""; 
		 preview(o,1,previewId,width,height); 
		 document.getElementById(imgId).style.display = "none"; 
	 } 
} 
		

