﻿/*
***************************************************************
组织机构代码验证js程序
为了要求用户输入大写字母，把.toUpperCase()去掉。
***************************************************************
*/

Array.prototype.contains = function (element) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == element) {
      return true;
    }
  }
  return false;
}


function IsOrgCode(ocode){
	ocode = ocode.replace("-","");
	ocode = ocode.replace("－","");
	if (ocode.replace(/^\s*(\S*\s*\S*)\s*$/g,'$1')=="000000000")
	{
		return false;
	}
	if (ocode.length != 9)
	{
		return false;
	}
	else
	{
		var x
		var y = 0,z = 0;
		var checkcode = new Array(3,7,9,10,5,8,4,2);
		var LetterNum = new Array();
		LetterNum =[['A',10],['B',11],['C',12],['D',13],['E',14],['F',15],['G',16],['H',17],['I',18],['J',19],['K',20],['L',21],['M',22],['N',23],['O',24],['P',25],['Q',26],['E',27],['S',28],['T',29],['U',30],['V',31],['W',32],['X',33],['Y',34],['Z',35]]; 

		for(var i=0;i<8;i++){
			x = ocode.substr(i,1)
			for(var j=0;j<26;j++){
				if(x == LetterNum[j][0]){
					x = LetterNum[j][1];
				}
			}
			y = y + x*checkcode[i]
		}
		z = y % 11;
		switch (z){
			case 0:z = 0;break;
			case 1:z = "X";break;
			default:z = 11 - z;
		}
		if(z==ocode.substr(8,1)){
			//alert("你的组织机构代码正确，验证码是："+z);
			return true;	
		}else{
			//alert("你的组织机构代码错误，验证码是："+z);
			return false;	
		}
	}
}

/*
***************************************************************
身份证号码验证js程序(需要用到日期格式验证js程序【 isDateString(sDate) 】)
***************************************************************
*/
function IsIDCardNumber(icode)
{   
	icode = icode.toUpperCase();  //转换为大写字母
	if (icode.length != 18&&icode.length >= 8)
	{
		return true;
	}

	if (icode.length!= 18) {
		return false;
	}
	var x
	var y = 0,z = 0;
	var checkcode = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
	
	if (icode.length == 18)
	{
		for(var i=0;i<17;i++)
		{
			x = icode.substr(i,1);
			y = y + x*checkcode[i];
		}
		z = y % 11;
		switch (z)
		{
			case 0:z = 1;break;
			case 1:z = 0;break;
			case 2:z = "X";break;
			default:z = 12 - z;
		}
		if(z==icode.substr(17,1))
		{
			//alert("你的身份证号码正确，验证码是："+z);
			return true;
		}
		else
		{
			//alert("你的身份证号码错误！");
			return false;
		}
	 }
	return true;
}
/*
***************************************************************
日期格式验证js程序（身份证号码验证js程序【 IsIDCardNumber(num) 】需要用到此程序）
***************************************************************
*/
function isDateString(sDate)
{
	var iaMonthDays = [31,28,31,30,31,30,31,31,30,31,30,31]
	var iaDate = new Array(3)
	var year, month, day

	if (arguments.length != 1) return false;
	iaDate = sDate.toString().split("-");
	if (iaDate.length != 3) return false;
	if (iaDate[1].length > 2 || iaDate[2].length > 2) return false;

	year = parseFloat(iaDate[0]);
	month = parseFloat(iaDate[1]);
	day=parseFloat(iaDate[2]);

	if (year < 1900 || year > 2100) return false;
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1]=29;
	if (month < 1 || month > 12) return false;
	if (day < 1 || day > iaMonthDays[month - 1]) return false;
	return true;
}

/*
***************************************************************
检查输入字符串是否只由英文字母和数字和下划线组成
***************************************************************
*/
function isNumberOr_Letter(InputContent){

	var regu = "^[0-9a-zA-Z\_]+$";
	
	var re = new RegExp(regu);

	if (re.test(InputContent)) {
		return true;
	}
	else{
		return false;	
	}
}


/*
   *************************************
   靠靠靠靠靠靠靠靠?
   */
function is_number_or_letter(content){
 return  isNumberOr_Letter(content);
}

/*
***************************************************************
检查输入字符串是否只由规定长度的英文字母和数字和下划线组成--适合检验用户名和密码。
函数名(字符，长度1，长度2) 规定的字符必须在长度1到长度2内，且只能由英文字母,数字,下划线和横线组成
与IsLenLetter_Number(theText,MinLength,MaxLength)函数功能一样
***************************************************************
*/
function isw(str,len1,len2){
        if(!eval("/^[A-Za-z0-9_-]{"+len1+","+len2+"}$/").test(str)){
                return false;
        }
        return true;
}

/*
***************************************************************
检查输入字符串是否只由英文字母和数字和下划线组成(规定长度)--适合检验用户名和密码。
与isw(str,len1,len2)函数功能一样
***************************************************************
*/
function IsLenLetter_Number(theText,MinLength,MaxLength)   
{  
	if(theText.replace(/^\s*(\S*\s*\S*)\s*$/g,'$1').length>=MinLength&&theText.replace(/^\s*(\S*\s*\S*)\s*$/g,'$1').length<=MaxLength)
	{
		
		for(var i=0;i<theText.length;i++)   
		{   
			var   strTemp=theText.charAt(i)   
			if   (!(strTemp>='a'   &&   strTemp<='z')   &&   !(strTemp>='A'   &&   strTemp<='Z')&&!(strTemp>='0'   &&   strTemp<='9')&&(strTemp!='_'))   
			{   
				return false;   
			}   
		}
		
	}
    else
	{
		return false;
	}  
	return true;   
}

/*
***************************************************************
检查输入框数据是否为空
***************************************************************
*/
function ISInputNull(InputContent)
{
	if (InputContent.replace(/^\s*(\S*\s*\S*)\s*$/g,'$1')=="") return true;
	return false;
}
/*
***************************************************************
检查Readio单选框或者CheckBox复选框是否被选中
***************************************************************
*/
function ISCheckReadio_CheckBox(GetName)
{  
	var InputName = document.getElementsByName(GetName)
	var LenInput = InputName.length;
	if (LenInput<=0){   
		return false;
	}
	else
	{
		for(var i=0;i<LenInput;i++)
		{
			if(InputName[i].checked)
			{
				break;
			} 
		} 
		if (i>=LenInput){
		   return false;
		}
		else{
			return true;
		}
	}
}
/*
***************************************************************
选中Readio单选框或者CheckBox复选框第N个选项
***************************************************************
*/
function CheckReadio_CheckBox(GetName,N)
{  
	var InputName = document.getElementsByName(GetName)
	var LenInput = InputName.length;
	if (LenInput<=0 || N>LenInput){   
		return;
	}
	else
	{
		InputName[N-1].checked=true;
	}
}
/*
***************************************************************
获取Readio单选框或者CheckBox复选框被选中的值
***************************************************************
*/
function GetValueReadio_CheckBox(GetName,InputType)
{  
	var GetValue;
	GetValue = "";
	var InputName = document.getElementsByName(GetName)
	var LenInput = InputName.length;
	if (LenInput<=0){   
		return GetValue;
	}
	else
	{
		for(var i=0;i<LenInput;i++){
			if(InputName[i].checked){
				if(InputType.toLowerCase() == "radio"){
					GetValue = InputName[i].value;
					break;
				}
				else if(InputType.toLowerCase() == "checkbox"){
					GetValue = GetValue + ', ' + InputName[i].value;
				}
				else{
					GetValue = "";
				}

				
			} 
		} 
		if(GetValue.indexOf(", ")!= -1){
			GetValue = GetValue.substring(2,GetValue.length);
		}
	}
	return GetValue;
}
/*
***************************************************************
检查输入框数据是否符合标准的复杂度（用于注册信息检测）
InputContent---输入框内容
ChineseLength---必须含有中文的个数,0不要求长度
MustLength---必须达到的长度，0不要求长度
SameNum----设置相同连续字符的个数，0不要求长度
***************************************************************
*/
function Iscomplex(InputContent,MustLength,ChineseLength,SameNum)
{
	var ContentText = InputContent.replace(/^\s*(\S*\s*\S*)\s*$/g,'$1');
	if (MustLength > 0)
	{
		if (ContentText.length < MustLength) 
		{
			return false;
		}
	}
	if (ChineseLength > 0)
	{	
		var ChineseNum = 0;
		for(var c=0;c<ContentText.length;c++) {
			if ((ContentText.charCodeAt(c) < 0) || (ContentText.charCodeAt(c) > 255))
			{
				ChineseNum = ChineseNum + 1;
			}
		}
		if (ChineseNum < ChineseLength) 
		{
			return false;
		}
	}
	
	if (SameNum > 0)
	{
		var MaxSame = 1;     //存放最大的相同个数。
		var nowSame = 1;     //存放当前字符的相同个数。
		
		for(var i=0;i<ContentText.length;i++)
		{
			for(var j=1;(i+j)<ContentText.length;j++)
			{
					if (ContentText.charAt(i) == ContentText.charAt(i+j))
					{
						nowSame = nowSame + 1;
						if (nowSame>MaxSame)
						{
							MaxSame = nowSame;
						}
					}
					else
					{
						nowSame = 1;
						break;//退出j循环
					}	
					if((i+j)==ContentText.length-1)
					{
						nowSame = 1;
					}
					//alert("i+j=" + i + "+" + j + "\n" + nowSame + "\n" + MaxSame);
			}

		}
		//alert(MaxSame);
		if (MaxSame > SameNum)
		{
			return false;
		}
	}
	return true;
}

/*
***************************************************************
检查输入手机号码(含小灵通)是否合法
***************************************************************
*/

function IsMobileNum(MobileNum)
{   
	if(isNaN(MobileNum)){
		return false;
	}
	if(MobileNum.length!=11&&MobileNum.length!=8&&MobileNum.length!=7){
		return false;
	}
	return true;

}

/*
***************************************************************
检查输入Email地址是否合法
***************************************************************
*/
function IsEmailAdd(EmailAdd)
{
	i=EmailAdd.indexOf('@','-1');
	j=EmailAdd.lastIndexOf('@'); 
	k=EmailAdd.indexOf('.','-1');
	if(i==j&&i!=0&&i!=-1&&i!=EmailAdd.value.length-1&&k!=0&&k!=-1)
	{
		return true;
	}
	else
	{
		return false; 
	}
}

/*
***************************************************************
检查输入邮政编码是否合法
***************************************************************
*/
function IsPostNum(PostNum)
{
	if(isNaN(PostNum)||PostNum.replace(/^\s*(\S*\s*\S*)\s*$/g,'$1').length!=6)
		return false;
	else
		return true;
}

/*
***************************************************************
清空输入框内容
***************************************************************
*/
function ClearInputValue(InputName,ClearValue)
{
	if (InputName.value == ClearValue)
	{
		InputName.value = "";
	}
}
/*
***************************************************************
限制Text框最大输入字符数
***************************************************************
*/
function textCounter(field, maxlimit)
{
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
//	else
//		document.data_form.remLen.value = maxlimit - field.value.length;
} 

/*
***************************************************************
限制Text框最大输入字符数,并提示剩余字数
***************************************************************
*/
function textCounter_Warning(field,maxlimit,DivID)
{
	if (field.value.length > maxlimit){
		alert("您输入的字符数已超过最大限制（" + maxlimit + "字）。");
		field.value = field.value.substring(0, maxlimit);
	}
	else{
		var LeftLength = maxlimit - field.value.length;
		document.getElementById(DivID).innerHTML = "<font color=\"#FF0000\">" + LeftLength + "</font>"
	}
} 

