//表单验证脚本
//调用方式
/*
function checkForm(theForm)
{
 //1.检查是否为空
 if(!checkEmpty(theForm.departmentId,"请选择部门"))return false;
 if(!checkEmpty(theForm.accountId,"请选择过期时间"))return false;
 if(!checkEmpty(theForm.role,"请选择角色"))return false;
 if(!checkEmpty(theForm.ability,"请输入接待访客上限人数"))return false;
 if(!checkEmpty(theForm.loginName,"请输入客服ID"))return false;
 if(!checkEmpty(theForm.name,"请输入姓名"))return false;
 if(!checkEmpty(theForm.password,"请输入密码"))return false;
 if(!checkEmpty(theForm.email,"请输入邮件"))return false;
 if(!checkEmpty(theForm.address,"请输入联系地址"))return false;
 //2.检查长度
 if(!checkLengthNotLow(theForm.password,6,"密码长度至少为6"))return false;
 if(!checkLengthNotUper(theForm.email,100,"邮件字符串长度不能超过100"))return false;
 if(!checkLengthNotUper(theForm.name,100,"名字长度不能超过100"))return false;
 if(!checkLengthNotUper(theForm.address,200,"地址长度不能超过200"))return false;
 if(!checkLengthNotUper(theForm.phone1,20,"电话长度不能超过20"))return false;
 if(!checkLengthNotUper(theForm.phone2,20,"电话长度不能超过20"))return false;
 if(!checkLengthNotUper(theForm.password,20,"密码长度不能超过20"))return false;
 if(!checkLengthNotUper(theForm.nickName,100,"昵称长度不能超过100"))return false;
 if(!checkLengthNotUper(theForm.loginName,100,"客服ID长度不能超过100"))return false;
 //3.检查格式
 if(!isNumber(theForm.ability,"接待访客上限人数为数字"))return false;
 if(!isEmail(theForm.email,"邮件格式不对"))return false;
 if(!checkIntValue(theForm.ability,50,"接待访客上限人数不能超过50"))return false;

 if(!checkPassword(theForm.password,theForm.rePassword,"您输入的密码不匹配，请在密码输入框中重新输入密码"))return false;　
 return true;
}
*/
function delHTML(elm) {
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
a = elm.value.indexOf("<");
b = elm.value.indexOf(">");
len = elm.value.length;
c = elm.value.substring(0, a);
if(b == -1)
b = a;
d = elm.value.substring((b + 1), len);
elm.value = c + d;
tagCheck = elm.value.indexOf("<");
if(tagCheck != -1)
elm.value = delHTML(elm.value);
return true;
}
String.prototype.trim=function() {
	return this.replace(/^\s+|\s+$/g,"");
}
function checkEmpty(elm,msg){
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  elm.value = elm.value.trim();
  if (elm.value.length==0){
    if(msg.length!=0)alert(msg);
    elm.focus();
    return false;
  }
  return true;
}
function checkLengthNotLow(elm,length,msg){
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.value.length<length){
    if(msg.length!=0)alert(msg.replace(/%1/g,length));
    elm.focus();
    return false;
  }
  return true;
}
function checkLengthNotUper(elm,length,msg){
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.value.length>length){
    if(msg.length!=0)alert(msg.replace(/%1/g,length));
    elm.focus();
    return false;
  }
  return true;
}
function checkIntValue(elm,value,msg){
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.length==0){ return true;}
  if(elm.value>value){
    if(msg.length!=0)alert(msg.replace(/%1/g,length));
    elm.focus();
    return false;
  }
  return true;
}
function checkIntValueNotLow(elm,value,msg){
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.length==0){ return true;}
  if(elm.value<value){
    if(msg.length!=0)alert(msg.replace(/%1/g,length));
    elm.focus();
    return false;
  }
  return true;
}
function isDate(elm,msg)
{
  var date_array="";
  var get_input_date_str = elm.value;
  var seperate;

  if(get_input_date_str == "")
  {
  	return true;
  }

  if (get_input_date_str.indexOf("-") != -1)
  {
      seperate = "-";
  }
  else if (get_input_date_str.indexOf("/") != -1)
  {
      seperate = "/";
  }
  else if (get_input_date_str.indexOf(".") != -1)
  {
      seperate = ".";
  }
  date_array = get_input_date_str.split(seperate);

  if(date_array.length != 3)
  {
		if(msg.length!=0)alert(msg);
		elm.focus();
		return (false);
  }
  if(date_array[0].length < 4)
  {
		if(msg.length!=0)alert(msg);
                elm.focus();
		return (false);
  }

  date_array[1] = date_array[1] - 1;
  var get_input_date = new Date(date_array[0], date_array[1], date_array[2]);

  var get_input_year = get_input_date.getYear();
  var get_input_month = get_input_date.getMonth() + 1;
  var get_input_day = get_input_date.getDate();

  var month2 = get_input_month;
  var day2 = get_input_day;

  if(get_input_year<1000)
  {
		get_input_year=1900 + get_input_year;
  }
  if(get_input_month<10)
  {
		get_input_month="0" + get_input_month;
  }
  if(get_input_day<10)
  {
		get_input_day="0" + get_input_day;
  }

//  alert( "abc" );
  var now = new Date();
  var num1 = now.getFullYear() * 10000 + now.getMonth() * 100 + now.getDate();
  var num2 = get_input_date.getFullYear() * 10000 + get_input_date.getMonth() * 100 + get_input_date.getDate();
//  alert( num1 +"," + num2 );
  if( (get_input_year + seperate + get_input_month + seperate + get_input_day) != get_input_date_str && (get_input_year + seperate + month2 + seperate + day2) != get_input_date_str)
  {
		if(msg.length!=0)alert(msg);
                elm.focus();
		return (false);
  }
//  else if ( num2<num1 )
//  {
//		alert("日期不能早于今天!");
//		return (false);
//  }
  return (true);
}
function RebuildDate(val)
{
  var date_array="";
  var get_input_date_str = val;
  var seperate;

  if(get_input_date_str == "")
  {
		return true;
  }

  if (get_input_date_str.indexOf("-") != -1)
  {
      seperate = "-";
  }
  else if (get_input_date_str.indexOf("/") != -1)
  {
      seperate = "/";
  }
  else if (get_input_date_str.indexOf(".") != -1)
  {
      seperate = ".";
  }
  date_array = get_input_date_str.split(seperate);

  if(date_array.length != 3)
  {
		return "";
  }
  if(date_array[0].length < 4)
  {
		return "";
  }

  date_array[1] = date_array[1] - 1;
  var get_input_date = new Date(date_array[0], date_array[1], date_array[2]);
  var dt = DateToStr(get_input_date);
  return dt;
 }
/// 比较date1和date2: date1大于date2返回1,相等返回0,小于返回-1
function CompareDate(date1, date2)
{
	var dt1 = RebuildDate(date1);
	var dt2 = RebuildDate(date2);

	if ( dt1 > dt2 ) return (1);
	else if ( dt1 == dt2 ) return (0);
	else return (-1);
}
function CompareToday(date1)
{
	var dt1 = RebuildDate(date1);

	var dt2 = TodayToStr();

	var todayObj = document.getElementById("servertoday");
	if ( todayObj == null )
	{
	}
	else
	{
		dt2 = todayObj.value;
	}
//	alert('\u00A1\u00BE\u00B2\u00E2\u00CA\u00D4\u00A1\u00BFToday:' + dt2);
	if ( dt1 > dt2 ) return (1);
	else if ( dt1 == dt2 ) return (0);
	else return (-1);
}
function isTime(elm,msg)
{
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.length==0){ return true;}
 if(elm.value.match(/[0-2][0-9]:[0-6][0-9]/)!=elm.value){
   if(msg.length!=0)alert(msg);
   elm.focus();
   return false;
 }
 else
  return true;
}
function isEmail(elm,msg)
{
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)!= -1)
  return true;
   else
     {
     alert(msg);
     elm.focus();
     return false;
     }
}
function ixxxsEmail(elm,msg)
{
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.length==0){ return true;}
 if(elm.value.match(/[\w-.]+@{1}[\w-]+\.{1}\w{2,4}(\.{0,1}\w{2}){0,1}/ig)!=elm.value){
   if(msg.length!=0)alert(msg);
   elm.focus();
   return false;
 }
 else
  return true;
}
function isNumber(elm,msg)
{
  if (elm == null) {
    if(msg.length!=0)alert("指定的元素不存在");
    return false;
  }
  if(elm.length==0){ return true;}
 var patrn=/^[-,+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$/;
 if (!patrn.exec(elm.value)){
   if(msg.length!=0)alert(msg);
   elm.focus();
   return false;

 }
 else  return true;
}
function isInt(elm,msg)
{
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.length==0){ return true;}
 var patrn=/^[-,+]{0,1}[0-9]{0,}$/;
 if (!patrn.exec(elm.value)){
     if(msg.length!=0)alert(msg);
     elm.focus();
     return false;
   }
 else  return true;
}
function isPhoneOrMobile(elm,msg){
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.length==0){return true;}
  var numberPatrn=/^[-,+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$/;
  var phonePatrn=/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;
  var mobilePatrn=/^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/;
  if(numberPatrn.exec(elm.value)||mobilePatrn.exec(elm.value)||phonePatrn.exec(elm.value)){
   return true;
  }
  else{
    if(msg.length!=0)alert(msg);
    elm.focus();
    return false;
  }
}
function isPhone(elm,msg)
{
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.length==0){ return true;}
 var patrn=/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;
 if(!patrn.exec(elm.value)){
   if(msg.length!=0)alert(msg);
   elm.focus();
   return false;
 }
 else
  return true;
}
function isMobile(elm,msg)
{
  if (elm == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm.length==0){ return true;}
 var patrn=/^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/;
 if(!patrn.exec(elm.value)){
   if(msg.length!=0)alert(msg);
   elm.focus();
   return false;
 }
 else
  return true;
}
function checkPassword(elm1,elm2,msg)
{
  if (elm1 == null) {
    alert("指定的元素不存在");
    return false;
  }
    if (elm2 == null) {
    alert("指定的元素不存在");
    return false;
  }
  if(elm1.length==0&&elm2.length==0){ return true;}
  if(elm1.value!=elm2.value){
  	if(msg.length!=0)alert(msg);
	elm1.focus();
	return false;
  }
  else
  	return true;
}
//只能输入msg中的字符
function onlyThisMsg(elm,sR,msg)
{
	 if (elm == null) {
		alert("指定的元素不存在");
		return false;
	}
	var sTmp;
	var sV = elm.value;
	if(sV.length==0){ return (true);}
	for (var i=0; i < sV.length; i++)
	{
		sTmp= sV.substring (i, i+1);
		if (sR.indexOf (sTmp, 0)==-1)
		{
			if(msg.length!=0)alert(msg);
			elm.focus();
			return (false);
		}
	}
	return (true);
}
//只能输入msg中字符开头的字符
function onlyThisMsgFirst(elm,sR,msg)
{
	if (elm == null) {
		alert("指定的元素不存在");
		return false;
	}
	var sTmp;
	var sV = elm.value.charAt(0);
	if(sV.length==0){ return (true);}
	for (var i=0; i < sV.length; i++)
	{
		sTmp= sV.substring (i, i+1);
		if (sR.indexOf (sTmp, 0)==-1)
		{
			if(msg.length!=0)alert(msg);
			elm.focus();
			return (false);
		}
	}
	return (true);
}
//check operatorLoginName
function checkOperatorLoginName(elm)
{
	if(!onlyThisMsg(elm,"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_","您的客服ID应该是数字、字母、下划线,不允许出现汉字等其他字符!"))return false;
	if(!onlyThisMsgFirst(elm,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_","您的客服ID只能以字母或下划线开头!"))return false;
	if(!checkLengthNotUper(elm,20,"客服ID长度不能超过20个字符"))return false;
	return true;
}
function checkCompanyLoginName(elm)
{

}
//是否全位数字
function isAllDigits(argvalue) {
                argvalue = argvalue.toString();
                var validChars = "0123456789";
                var startFrom = 0;
                if (argvalue.substring(0, 2) == "0x") {
                   validChars = "0123456789abcdefABCDEF";
                   startFrom = 2;
                } else if (argvalue.charAt(0) == "0") {
                   validChars = "01234567";
                   startFrom = 1;
                } else if (argvalue.charAt(0) == "-") {
                    startFrom = 1;
                }
                for (var n = startFrom; n < argvalue.length; n++) {
                    if (validChars.indexOf(argvalue.substring(n, n+1)) == -1) return false;
                }
                return true;
}

