//校验类
//校验字符串中是否仅为数字,返回true表示为一实数(首尾不为小数点).

function checkNumber(str)
{
	return /^[-|+]?\d+(\.\d+)?$/g.test(str);
}

//校验整数
function checkInteger(str)
{
	return /^[-\+]?\d+$/g.test(str);
}

//校验Email
function checkEmail(str)
{
	return /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/g.test(str);
}

//校验IP地址
function checkIp(str)    
{    
    var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; //匹配IP地址的正则表达式   
    if(re.test( str.value ))   
    {   
        if( RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256) return true;   
    }   
    return false;    
}

//校验邮政编码
function checkPostCode(str)
{
	return /^[a-zA-Z0-9 ]{3,12}$/g.test(str);
}

//校验电话号码
function checkPhone(str)
{
	return /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/g.test(str);
}

//校验手机号
function checkMobilePhone(str)
{
	return /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/g.test(str);
}

//校验Url地址
function checkUrl(str)
{
	return  /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/g.test(str);
}

//校验身份证号码
function checkIDcard(str)
{
	return  /^\d{15}(\d{2}[A-Za-z0-9])?$/g.test(str);	
}

//校验货币
function checkCurrency(str)
{
	return /^\d+(\.\d+)?$/g.test(str);
}

//校验QQ号
function checkQQ(str)
{
	return /^\d+(\.\d+)?$/g.test(str);
}

//返回字符串中的中、英文字符总长度
function getStrLength(str)
{
  var digit = str.length;
  var arr = str.match(/[^\x00-\x80]/ig);
  if(arr != null)
  {
	 digit += arr.length;
  }
  return digit;
}
//检测英文(包含字母和数字)
function checkEnglish(str)
{
	if(!checkPureEnglish(str))
	{
		if(str.trim().length != getStrLength(str))
		{
			return false;
		}
	}
	return true;
}

//校验纯英文
function checkPureEnglish(str)
{
	return /^[A-Za-z]+$/g.test(str);
}

//校验纯汉字
function checkPureChinese(str)
{
	return /^[\u0391-\uFFE5]+$/g.test(str);
}

//校验上传文件的格式
function checkFileType(obj,formatArray)
{
	var source = eval("document.all."+obj);
	var eXtendName = source.value.match(/^(.*)(\.)(.{1,8})$/)[3].toLowerCase();
	var Value = false;
	var strFormat = "";
	
	for(i=0;i<formatArray.length;i++)
	{
		if(formatArray[i].toLowerCase() == eXtendName)
			return true;
		else
		{
			if(i != formatArray.length-1)
				strFormat += "."+formatArray[i].toString()+"、";
			else
				strFormat += "."+formatArray[i].toString(); 
			Value = false;
			continue;
		}
	}
	if(!Value)
	{
		alert("您只能上传"+strFormat.toString()+"格式的文件！");
		return false;
	}
}

//增加String对象的Trim()方法,功能[去掉空格]
String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

//*****检验输入框内容是否符合要求*****//
//obj:  name	 
//type: datetime/number/string/integer/email/phone/mobile/url/idcard/currency/qq/english/chinese
//len : 字符长度
//isnull : 是否必须输入内容（true/false）
//************************************//
function checkObjValue(obj,type,len,isnull,strAlert)
{
	var source = eval("document.all."+obj);
	source.value = source.value.trim();
	if(type=="datetime")
	{
		if(source.value.trim()!="")
			Vdate(source,strAlert);
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();	
				return false;
			}
			else
				return true;
		}
	}
	else if(type=="number")
	{
		if(source.value.trim()!="")
		{
			if(checkNumber(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是数字！");
				source.focus();
				source.select();
				return false;
			}
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type=="integer")
	{
		if(source.value.trim() != "")
		{
			if(checkInteger(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是整数！");
				source.focus();
				source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type == "email")
	{
		if(source.value.trim() != "")
		{
			if(checkEmail(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是合法的邮箱地址！");
				source.focus();
				source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type == "phone")
	{
		if(source.value.trim() != "")
		{
			if(checkPhone(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是合法的固定电话号码！");
				source.focus();
				source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type == "mobile")
	{
		if(source.value.trim() != "")
		{
			if(checkMobilePhone(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是合法的移动手机号码！");
				source.focus();
				source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type == "url")
	{
		if(source.value.trim() != "")
		{
			if(checkUrl(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是合法的Url地址！");
				source.focus();
				source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type == "currency")
	{
		if(source.value.trim() != "")
		{
			if(checkCurrency(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是合法的货币金额！");
				source.focus();
				source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type == "qq")
	{
		if(source.value.trim() != "")
		{
			if(checkQQ(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是合法的qq号码！");
				source.focus();
				source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type == "english")
	{
		if(source.value.trim() != "")
		{
			if(checkPureEnglish(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是全英文字母字串！");
				source.focus();
				source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type == "chinese")
	{
		if(source.value.trim() != "")
		{
			if(checkPureChinese(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是全中文汉字字串！");
				source.focus();
				//source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type == "idcard")
	{
		if(source.value.trim() != "")
		{
			if(checkIDcard(source.value.trim()))
				return true;
			else
			{
				alert(strAlert +"必须是合法的身份证号码！");
				source.focus();
				source.select();
				return false;
			}	
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}
	else if(type=="string")
	{
		if(source.value!="")
		{
			if(checkDigit(source.value.trim())>len)
			{
				var a1,a2,a3;
				a1 = (len/2);
				a2 = a1.toString();

				if(a2.indexOf(".")>=0)
					a3 = a2.substring(0,a2.indexOf("."));
				else
					a3 = a2;
				alert(strAlert+"的长度超出了范围，最多" + a3 + "个汉字，" + len + "个英文！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;			
		}
		else
		{
			if(!isnull)
			{
				alert("必须输入" + strAlert + "！");
				source.focus();
				source.select();
				return false;
			}
			else
				return true;
		}
	}		
	return true;
}

//input框日期校验
function Vdate(obj)
{
	if(obj.value.trim() == "")
	{
		alert("查询时间字段不能为空值。");
		return false;
	}
	var now = new Date(); 
	var yy = now.getYear()+"-"; 
	var mm = now.getMonth()+1;
	var dd = now.getDate();
	if(mm<10)
		mm="0"+mm+"-";
	else
		mm=mm+"-";
	if(dd<10)
		dd="0"+dd;
	else
		dd=dd+"";
	var date = yy+mm+dd;
	var tempdate=obj.value.trim();
	if(tempdate=="") return false;
	if(tempdate.length>10||tempdate.length<8)
	{
		alert("请输入合法的日期，格式为 YYYY-MM-DD  例如 2000-5-12");
		//alert("\u8bf7\u6309\u7167\u6807\u51c6\u65e5\u671f\u683c\u5f0f\u8f93\u5165\u683c\u5f0f\uff01\n\u6807\u51c6\u65e5\u671f\u683c\u5f0f\uff1a2003-01-30");
		obj.select();
		obj.focus();
		return false;
	}
	var last = tempdate.lastIndexOf("-");
	var nian=parseInt(tempdate.substring(0,4),10);
	var yue=parseInt(tempdate.substring(5,last),10)-1;
	var ri=parseInt(tempdate.substring(last+1,10),10);
	var d=new Date(nian,yue,ri);

	if(tempdate.substring(4,5)=="-"&&tempdate.substring(last,last+1)=="-")
	{
		if(d.getMonth()==yue&&d.getDate()==ri)
			return true;
		else
		{
			alert("请输入合法的月份和天数！");
			//alert("\u8bf7\u8f93\u5165\u6709\u6548\u65e5\u671f\uff01");
			obj.focus();
			obj.select();
			return false;
		}
	}
	else
	{
		alert("请输入合法的日期，格式为 YYYY-MM-DD  例如 2000-5-12");
		//alert("\u8bf7\u6309\u7167\u6807\u51c6\u65e5\u671f\u683c\u5f0f\u8f93\u5165\u683c\u5f0f\uff01\n\u6807\u51c6\u65e5\u671f\u683c\u5f0f\uff1a2003-01-30");
		obj.focus();
		return false;
	}
	
	
	
}

