//手机号码检验函数
function isMobile(_mobile)
{
	var reg0=/^13\d{9}$/;   //130--139。
	var reg1=/^15\d{9}$/;   //130--139。
	var r=false;
	if (reg0.test(_mobile)) r=true;
	if (reg1.test(_mobile)) r=true;
 
	return r;
}


//身份证检查函数
function isPersonalNO(StrNo)
{
	this.isInteger=function(str)
	{
		if (/[^\d]+$/.test(str)) return false;
		return true;
	}

	this.isValidDate=function(iY, iM, iD)
	{ 
		sD=iY+"-"+iM+"-"+iD;
		if (!FormIsdate(sD))
		{
			alert("身份证号码内日期错误!");
			return false;
		}
		else
		{
			//如果身份证生日超过90岁，也返回FALSE
			if((new Date()).getFullYear()-iY>90)
			{
				alert("你输入的身份证的出生年份不在一个合理的范围之内!");
				return false;
			}
		}
		return true;
	}

	StrNo = StrNo.toString();
	if((StrNo.length!=18)&&(StrNo.length!=15))	
	{
		alert("输入的身份证号码必须为15位或者18位!");
		return false;
	}

	if (StrNo.length==18)//18位身份证号
	{
		var a,b,c;
		if (!isInteger(StrNo.substr(0,17))) return false;

		a=parseInt(StrNo.substr(0,1))*7+parseInt(StrNo.substr(1,1))*9+parseInt(StrNo.substr(2,1))*10;
		a=a+parseInt(StrNo.substr(3,1))*5+parseInt(StrNo.substr(4,1))*8+parseInt(StrNo.substr(5,1))*4;
		a=a+parseInt(StrNo.substr(6,1))*2+parseInt(StrNo.substr(7,1))*1+parseInt(StrNo.substr(8,1))*6; 
		a=a+parseInt(StrNo.substr(9,1))*3+parseInt(StrNo.substr(10,1))*7+parseInt(StrNo.substr(11,1))*9; 
		a=a+parseInt(StrNo.substr(12,1))*10+parseInt(StrNo.substr(13,1))*5+parseInt(StrNo.substr(14,1))*8; 
		a=a+parseInt(StrNo.substr(15,1))*4+parseInt(StrNo.substr(16,1))*2;
		b=a%11;

		if (b==2) //最后一位为校验位
		{
			c=StrNo.substr(17,1).toUpperCase(); //转为大写X
		}
		else
		{
			c=parseInt(StrNo.substr(17,1));
		}

		switch(b)
		{
			case 0: if ( c!=1 ) {alert("身份证号码校验位错误:最后一位应该为:1");return false;}break;
			case 1: if ( c!=0 ) {alert("身份证号码校验位错误:最后一位应该为:0");return false;}break;
			case 2: if ( c!="X") {alert("身份证号码校验位错误:最后一位应该为:X");return false;}break;
			case 3: if ( c!=9 ) {alert("身份证号码校验位错误:最后一位应该为:9");return false;}break;
			case 4: if ( c!=8 ) {alert("身份证号码校验位错误:最后一位应该为:8");return false;}break;
			case 5: if ( c!=7 ) {alert("身份证号码校验位错误:最后一位应该为:7");return false;}break;
			case 6: if ( c!=6 ) {alert("身份证号码校验位错误:最后一位应该为:6");return false;}break;
			case 7: if ( c!=5 ) {alert("身份证号码校验位错误:最后一位应该为:5");return false;}break;
			case 8: if ( c!=4 ) {alert("身份证号码校验位错误:最后一位应该为:4");return false;}break;
			case 9: if ( c!=3 ) {alert("身份证号码校验位错误:最后一位应该为:3");return false;}break;
			case 10: if ( c!=2 ){alert("身份证号码校验位错误:最后一位应该为:2");return false;}
		}
		return isValidDate(StrNo.substr(6,4),StrNo.substr(10,2),StrNo.substr(12,2));
	}
	else //15位身份证号
	{
		if (!isInteger(StrNo)) {alert("身份证号码错误,前15位不能含有英文字母!");return false;} 
		return isValidDate("19"+StrNo.substr(6,2),StrNo.substr(8,2),StrNo.substr(10,2));
	}
}

//检查FORM合法性
function checkFormValues(thisForm) 
{  
	for (n=0;n<thisForm.elements.length;n++)
	{ 
		thisObj=thisForm.elements[n];

		//检查是否不能为空
		if (thisObj.noBlank)
		{
			if (thisObj.type=="radio")
			{
				
				_name="";
				o=thisObj;
				while(true)
				{
					o=o.parentElement;
					if(o.tagName=="FORM"||o.tagName=="BODY")
					{
						if(o.tagName=="FORM") _name=o.name+".";
						break;
					}

				}
				o=eval(_name+thisObj.name);
				_return=false;
				for(x=0;x<o.length;x++)
				{
					_return=o[x].checked;
					if(_return) break;
				}
				if(!_return)
				{
					alert("请选择您的["+thisObj.noBlank+"]!");
					try{
						thisObj.focus();
					}catch(e){}
					return false;
				}
			}
			else
			{
				if (FormIsempty(thisObj.value))
				{
					alert("请填写["+thisObj.noBlank+"]!");
					try{
						thisObj.focus();
					}catch(e){}
					return false
				}
			}
		}

		//检查是否应该是邮政编码
		if (thisObj.mustZipcode)
		{
			if (!FormIsempty(thisObj.value))
			{
				if (!FormIszipcode(thisObj.value))
				{
					alert("请填写正确的["+thisObj.mustZipcode+"]!");
					try{
						thisObj.focus();
					}catch(e){}
					return false
				}
			}
		}

		//检查是否应该是身份证号码
		if (thisObj.mustPersonalNO)
		{
			if (!FormIsempty(thisObj.value))
			{
				if (!isPersonalNO(thisObj.value))
				{
					try{
						thisObj.focus();
					}catch(e){}
					return false
				}
			}
		}

		//检查是否应该是EMAIL
		if (thisObj.mustEmail)
		{
			if (!FormIsempty(thisObj.value))
			{
				if (!FormIsemail(thisObj.value))
				{
					alert("请填写正确的["+thisObj.mustEmail+"]!");
					try{
						thisObj.focus();
					}catch(e){}
					return false
				}
			}
		}

		//检查是否应该是手机号码
		if (thisObj.mustMobile)
		{
			if (!FormIsempty(thisObj.value))
			{
				if (!isMobile(thisObj.value))
				{
					alert("请填写正确的["+thisObj.mustMobile+"]!");
					try{
						thisObj.focus();
					}catch(e){}
					return false
				}
			}
		}

		//检查是否应该是数字
		if (thisObj.mustNumeric)
		{
			if (!FormIsempty(thisObj.value))
			{
				if (!FormIsnumeric(thisObj.value))
				{
					alert("请填写正确的["+thisObj.mustNumeric+"]!");
					try{
						thisObj.focus();
					}catch(e){}
					return false
				}
			}
		}

		//检查是否应该是日期
		if (thisObj.mustDate)
		{
			if (!FormIsempty(thisObj.value))
			{
				if (!FormIsdate(thisObj.value))
				{
					alert("请填写正确的["+thisObj.mustDate+"]!");
					try{
						thisObj.focus();
					}catch(e){}
					return false
				}
				else
				{
					thisObj.value=JsDateValue(thisObj.value);
				}
			}
		}
	}  
	return true;
}

//在FORM中移动焦点
function ChangeFocus(thisForm) 
{  
  key=window.event.keyCode;  
  if(key!=13||event.shiftKey) return false;
  thisIndex=thisForm.elements.length;
  for (n=0;n<thisForm.elements.length;n++)  
  { 
    thisObj=thisForm.elements[n];
    if(event.srcElement==thisObj) thisIndex=n;
    try
    {
      if((thisIndex<n) && (thisObj.style.display.toLowerCase()!="none") && (!thisObj.disabled) && (thisObj.type.toLowerCase()!="hidden"))
      { 
        if(thisObj.tabStop!="0")
		{
			thisObj.focus(); //移动焦点 
		    if(thisObj.type.toLowerCase()=="text") thisObj.select(); //全选
			return true;
		}
      }
    }
    catch(e){}  
  }  
}

//显示对象函数
function ShowObject(ThisObject,ThisDisplay,ThisIndex)
{
	if (ThisIndex==null)
	{
		if (!ThisObject.length)
		{
		  ThisObject.style.display=ThisDisplay;
		  
		}
		else
		{
		  for(var i=0;i<ThisObject.length;i++)
		  {
		    ThisObject[i].style.display=ThisDisplay;
		  }
		}
	}
	else
	{
	  if(ThisObject[ThisIndex]) ThisObject[ThisIndex].style.display=ThisDisplay;
	  else ThisObject.style.display=ThisDisplay;
	}	
}

//使一个容器对象中的子对象只能浏览
function setViewOnly(Obj) 
{  
  switch (Obj.tagName)
  {
    case "BODY" :
      prntObj=document.all;
      try
      {
        document.getElementById("bSave").disabled=true;
        document.getElementById("bCancel").disabled=true;
      }
      catch(e)
      {}    
      break;
    case "FORM" :
      prntObj=Obj.elements;
      break;
    default :
      return;
  }

  inputStr="/BUTTON/INPUT/SELECT/TEXTAREA/A"; 
  for (n=0;n<prntObj.length;n++)  
  { 
    thisObj=prntObj[n];
    if(inputStr.indexOf(thisObj.tagName)>0)
    {
      if(thisObj.style.display.toLowerCase()!="none")
      {
        showValue="";
        switch (thisObj.tagName)
        {
          case "BUTTON" :
            break;
          case "SELECT" :
            for(i=0;i<thisObj.length;i++)
            {
              if(!thisObj.multiple)
              {
                if(thisObj.options[i].selected) showValue+=thisObj.options[i].text;
              }
              else  showValue+=thisObj.options[i].text+"\n";
            }
            break;
          case "TEXTAREA" :
            showValue=thisObj.value;
            break;
          case "A" :
            showValue=thisObj.innerText;
            break;
          default :
            switch (thisObj.type.toLowerCase())
            {
              case "text" :
                showValue=thisObj.value;
                break;
              case "password" :
                showValue="******";
                break;
              case "checkbox" :
                if(!thisObj.checked) thisObj.nextSibling.nodeValue="";
                break;
              case "radio" :
                if(!thisObj.checked) thisObj.nextSibling.nodeValue="";
                break;
            }
        }
        
        thisObj.insertAdjacentHTML("AfterEnd",showValue.replace(/\n/g,"<br>"));
        thisObj.style.display="none";
      }
    }
  }  
}
