// *-------------------------------------------------------------------------*
// | Name:    validate.js													 |
// | Author:  JackieWang	QQ:47266842									     |
// | Version: "2005.07.27"  Email:wfits@126.com								 |
// | * 威龙商务网*										                     |
// | Copyright(c) 2005-2010 * http://www.vlongbiz.com   All rights reserved. |
// *-------------------------------------------------------------------------*

//显示提示信息
/* 参数说明：
 *msg  所要显示的信息
 *pos  显示信息的位置
 */
function showMsg(msg,pos)
{
	document.getElementById('WarnMsg'+pos).innerHTML="<div id='TipError'>"+msg+"</div><img id='ErrorPointer' src='images/arrow_error.gif'>";
	document.getElementById('WarnMsg'+pos).style.display="";
}
//隐藏信息
function hideMsg(pos)
{
	document.getElementById('WarnMsg'+pos).style.display="none";
	document.getElementById("TipAll").style.display="none";
}

 //判断是否含有汉字（认定键盘字符以外的为汉字）
 /*	参数说明：
  *	textValue:所要判断的表单全名用document.formname形式
  *	POS　：显示信息的位置：
  *	inputTitle:	输入表单的名称
  */
 function judgeChinese(txtValue,pos,inputTitle)
{
	var text="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var checkStr=txtValue.value;
	for(i=0;i<=checkStr.length-1;i++) 
	{
		char1=checkStr.charAt(i);
		index=text.indexOf(char1);
		if(index<0) 
		{
			msg=inputTitle+"不能包含汉字请重新填写。";
			showMsg(msg,pos);
			return false;
		}
	}
	hideMsg(pos);
	return true;
}
//判断是否含非法的名字
 /*	参数说明：
  *	textName:所要判断的表单全名用document.formname形式
  *	POS　：显示信息的位置：
  *	inputTitle:	输入表单的名称
  */
function judgeIll(txtName,pos,inputTitle)
{	var checkTLD=1;
	var feifa=/^root|bin|daemon|lp|sync|shutdown|halt|mail|news|uucp|operator|games|mysql|httpd|nobody|dummy|www|cvs|shell|ftp|irc|debian|ns|weilong|weilong369|wl369|download|admin|vlongbiz|wangjie|王杰|江泽民|胡锦涛|李宏志|管理员|法轮|表面网|操$/;
	var ffArr=txtName.value.match(feifa) ;
	if(ffArr==null)
	{
		 return true;
	}
	else
	{
	 //alert(ffArr);
	  var len=ffArr.length;
	  if ( len==null)
	  {
		return true;
	  }
	   else if ( len>=0)
	  {
		msg="您输入的名字已经被过滤！";
		showMsg(msg,pos);
		return false;
   	 }
	}
	return true;
}

 //判断是否含有非法字符
 /*	参数说明：
  *	textValue:所要判断的表单全名用document.formname形式
  *	POS　：显示信息的位置：
  *	inputTitle:	输入表单的名称
  */
function judgeCharacter(txtValue,pos,inputTitle)
{
	var text="~`!@#$%^&*|\/?<>:()+{}|?`-=[];',￥·……—（）——：“《》？，、.";
	var checkStr=txtValue.value;
	for(i=0;i<=checkStr.length-1;i++) 
	{
		char1=checkStr.charAt(i);
		index=text.indexOf(char1);
		if(index>=0 | char1=='"') 
		{
			msg=inputTitle+"不能包含“"+char1+"”！请重新填写。";
			showMsg(msg,pos);
			return false;
		}
	}
	hideMsg(pos);
	return true;
}
 //判断是否含有数字
function judgenum2(txtValue,pos,inputTitle)
{
	var text="1234567890";
	var checkStr=txtValue.value;
	for(i=0;i<=checkStr.length-1;i++) 
	{
		char1=checkStr.charAt(i);
		index=text.indexOf(char1);
		if(index>=0 | char1=='"') 
		{
			msg=inputTitle+"不能包含数字请重新填写。";
			showMsg(msg,pos);
			return false;
		}
	}
	hideMsg(pos);
	return true;
}


 //判断是否为数字
 /*	参数说明：
  *	obj:所要判断的表单全名用document.formname.textname形式
  *	POS　：显示信息的位置：
  *	
  */

function judgeNum(obj,pos){
	if(event.keyCode==13){
		try{obj.value=eval(obj.value)}
		catch(e)
		{
				alert(obj.value);
		msg="请输入半角数字";
		showMsg(msg,pos);
		return false;
		//alert('请输入数字')
		}
	}
}
 //判断是字符串长度
 /*	参数说明：
  *	textValue:所要判断的表单全名用document.formname形式
  *	POS　：显示信息的位置：
  *	magnitude:要判断的字符串长度
  *	inputTitle:	输入表单的名称
  *	idName：　id名称：
  */
function judgeLength(txtValue,pos,magnitude,inputTitle,idName)
{
	var checkStr=txtValue.value;
	var msg="请重新填写。";
	//alert(magnitude);
	if (checkStr.length==magnitude && magnitude==0)
	{
		msg=inputTitle+"为空！ 请填写您的"+inputTitle;
		//alert(msg);
		if (inputTitle=="使用必须填写")
		{
			msg="使用必须填写";
		}
		if (idName)
		{
			document.getElementById(idName).className = "BgCr";
		}

		showMsg(msg,pos);
		return false;
	}
	else if (checkStr.length<magnitude && magnitude==2)
	{
		msg="用户名至少要由4位字符组成! "+msg;
		showMsg(msg,pos);
		return false;
	}
	else if (checkStr.length<magnitude && magnitude<10 && magnitude!=2)
	{
		msg=inputTitle+"至少要由"+magnitude+"位字符组成! "+msg;
		showMsg(msg,pos);
		return false;
	}
	else if(checkStr.length>magnitude && magnitude>10)
	{
		msg=inputTitle+"最多"+magnitude+"位字符组成! "+msg;
		showMsg(msg,pos);
		return false;
	}
	else
	{
		document.getElementById('WarnMsg'+pos).style.display="none";
		return true;
	}
}


var multiChar=new Array("０","１","２","３","４","５","６","７","８","９","Ｑ","Ｗ","Ｅ","Ｒ","Ｔ","Ｙ","Ｕ","Ｉ","Ｏ","Ｐ","Ａ","Ｓ","Ｄ","Ｆ","Ｇ","Ｈ","Ｊ","Ｋ","Ｌ","Ｚ","Ｘ","Ｃ","Ｖ","Ｂ","Ｎ","Ｍ","ｑ","ｗ","ｅ","ｒ","ｔ","ｙ","ｕ","ｉ","ｏ","ｐ","ａ","ｓ","ｄ","ｆ","ｇ","ｈ","ｊ","ｋ","ｌ","ｚ","ｘ","ｃ","ｖ","ｂ","ｎ","ｍ","．","－","＿","＠"," ","　","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
var asciiChar=new Array("0","1","2","3","4","5","6","7","8","9","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",".","-","_","@","","","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
var f=document.forms[0];
function replaceAll(str, strFrom, strTo)
{
	return str.replace(new RegExp(strFrom, "g"), strTo);
}
//大小写不敏感
function multi2asciisecword(TxtValue)
{
	for (var i=0;i<multiChar.length;++i)
	TxtValue=replaceAll(TxtValue, multiChar[i], asciiChar[i]);
	eval(document.theform.secword).value=TxtValue;
	return true;
}
function multi2asciinew(inputName)
{
	for (var i=0;i<multiChar.length;++i)
	inputName.value=replaceAll(inputName.value, multiChar[i], asciiChar[i]);
	return true;
}
 //判断email格式：
function checkEmailFormat(txtName,pos,selName,theForm)
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|xxx)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var emailPat=/^(.+)@(.+)$/;

	var matchArray=txtName.value.match(emailPat);	

		if(matchArray==null)
		{
			msg="请您输入的完整电子邮箱";
			showMsg(msg,pos);
			return false;
		}
	
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++)
	{
		if (user.charCodeAt(i)>127)
		{
			msg="您输入的电子邮箱中用户名包含非法字符";
			showMsg(msg,pos);
			return false;
		}
	}
	for (i=0; i<domain.length; i++)
	{
		if (domain.charCodeAt(i)>127)
		{
			msg="您输入的电子邮箱中包含非法字符";
			showMsg(msg,pos);
			return false;
		}
	}
	if (user.match(userPat)==null)
	{
		msg="请您输入正确的电子邮箱";
		showMsg(msg,pos);
		return false;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++)
	{
		if (domArr[i].search(atomPat)==-1)
		{
			msg="您输入的电子邮箱不正确";
			showMsg(msg,pos);
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1)
	{
		msg="您输入的邮箱域名必须是已知域名";
		showMsg(msg,pos);
		return false;
	}
	if (len<2)
	{
		msg="这个电子邮箱没有主机名";
		showMsg(msg,pos);
		return false;
	}
	
	
	 document.getElementById('WarnMsg'+pos).innerHTML="";
	 document.getElementById('WarnMsg'+pos).style.display="none";
	return true;
}

 //判断是否为数字
 /*	参数说明：
  *	inputtitle:所要判断的输入表单名用textname形式
  *	POS　：显示信息的位置：
  *	theForm 表单名称：
  */
function checkNum(inputTitle,pos,theForm)
{
	var num="1234567890";
	var checkStr=theForm.value;
	for(i=0;i<=checkStr.length-1;i++) 
	{
		char1=checkStr.charAt(i);
		index=num.indexOf(char1);
		if(index<0) 
		{
			msg=inputTitle+"必须为半角数字。";
			showMsg(msg,pos);
			return false;
		}
	}
	hideMsg(pos);
	return true;
}	 
//********************************判断密码部分**************************************************//
function judgeSamePwd(pos)
{
	if(eval(document.theform.password).value!=eval(document.theform.conpassword).value)
	{
		msg="您两次输入的密码不同，请重新填写";
		showMsg(msg,pos);
		return false;
	}
	else
	{
		hideMsg(pos);
		return true;
	}
}

function checkSame(pwd)
{
	for(i=0;i<pwd.length;i++)
	{	
		if(pwd.charAt(0)!=pwd.charAt(i))
		{			
			
			return true;
		}
	}
}	

function judgeSame(pwd,pos)
{
	if(!checkSame(pwd))
	{
		msg="请您不要使用"+pwd+"(同一个字符)表示密码！请重新填写密码";
		showMsg(msg,pos);
		return false;
	}
	else
	{
		hideMsg(pos);
		return true;
	}
}

number = "01234567890";
lower = "abcdefghijklmnopqrstuvwxyz";
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function checkSeq(seq,pwd)
{
	var rt = true;
	for(j=0;j<seq.length;j++)
	{
		if(pwd.charAt(0)==seq.charAt(j))
		{
			for(i=0;i<pwd.length;i++)
			{
				if(seq.length-j>=pwd.length)
				{
					if(pwd.charAt(i)==seq.charAt(j+i))
					{
						rt= false
					}
					else
					{
						rt = true;
						break;
					}
				}
				else
				{
					rt = true;
					break;
				}
			}
			if(rt==false)
			{
				return false;
			}
			for(i=0;i<pwd.length;i++)
			{
				if(j+1>=pwd.length)
				{
					if(pwd.charAt(i)==seq.charAt(j-i))
					{
						rt= false;
					}
					else
					{
						rt = true;
						break;
					}
				}
				else
				{	
					rt = true;
					break;
				}
			}
			return rt;
		}
	}
	return true;
}

function judgeNumOrder(number,pwd,pos)
{
	if(!checkSeq(pwd,number))
	{
		msg="请您不要使用纯数字顺序“"+number+"”表示密码！请重新填写密码";
		showMsg(msg,pos);
		return false;
	}
	else
	{
		hideMsg(pos);
		return true;
	}
}
function judgeLowerOrder(lower,pwd,pos)
{
	if(!checkSeq(pwd,lower))
	{
		msg="请您不要使用纯小写字母顺序“"+lower+"”表示密码！请重新填写密码";
		showMsg(msg,pos);
		return false;
	}
	else
	{
		hideMsg(pos);
		return true;
	}
}
function judgeUpperOrder(upper,pwd,pos)
{
	if(!checkSeq(pwd,upper))
	{
		msg="请您不要使用纯大写字母顺序“"+upper+"”表示密码！请重新填写密码";
		showMsg(msg,pos);
		return false;
	}
	else
	{
		hideMsg(pos);
		return true;
	}
}
//*****************************************综合判断部分****************************************//
//判断威龙帐号
function confirmaccounts()
{								  
	if(multi2asciinew(document.theform.accounts) && judgeLength(document.theform.accounts,0,0,"用户名") && judgeLength(document.theform.accounts,0,5,"用户名") && judgeCharacter(document.theform.accounts,0,"用户名") && judgeLength(document.theform.accounts,0,15,"表面网提醒")&& judgeIll(document.theform.accounts,0,"表面网提醒")&&judgeChinese(document.theform.accounts,0,"表面网提醒"))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//判断商友地带笔名（已经删除）

//密码
function confirmPwd()
{
	if(judgeLength(document.theform.password,15,0,"密码") && judgeLength(document.theform.password,15,6,"密码")&&judgeSame(document.theform.password.value,15)&&judgeNumOrder('123456',document.theform.password.value,15)&&judgeLowerOrder('abcdef',document.theform.password.value,15)&&judgeUpperOrder('ABCDEF',document.theform.password.value,15))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//确认密码
function confirmSecPwd()
{
	if (judgeLength(document.theform.conpassword,16,0,"确认密码") && judgeSamePwd(16))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//判断提示问题
function confirmquestion()
{
	if (judgeLength(document.theform.question,2,0,"提示问题"))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//判断提示问题答案
function confirmanswer()
{
	if ((judgeLength(document.theform.answer,3,0,"提示问题答案"))&&(judgeLength(document.theform.answer,3,40,"提示问题答案")))
	  {
		return true;
	}
	else
	{
		return false;
	}
}
//判断真实姓名
function confirmname()
{
	if ((judgeLength(document.theform.name,4,0,"真实姓名"))&&(judgeLength(document.theform.name,4,10,"真实姓名"))&&(judgeCharacter(document.theform.name,4,"真实姓名"))&&(judgenum2(document.theform.name,4,"真实姓名")))
	  {
		return true;
	}
	else
	{
		return false;
	}
}
//判断电子邮件
function confirmemail()
{
	if(multi2asciinew(document.theform.email) && judgeLength(document.theform.email,5,0,"电子邮箱")&&checkEmailFormat(document.theform.email,5,'','theform'))
	{
		if(document.getElementById('WarnMsg5').innerHTML!="<SPAN>电子邮箱为空！ 请填写您的电子邮箱</SPAN><BR>")
		{
			document.getElementById('WarnMsg5').style.display="";
		}
		return true;
	}
	else
	{
		return false;
	}
}
//判断区号电话号码
function confirmphone()
{
	if ((judgeLength(document.theform.areaphone,6,0,"区号"))&&(judgeLength(document.theform.areaphone,6,3,"区号"))&&(judgeLength(document.theform.phone,6,0,"电话号码"))&&(judgeLength(document.theform.phone,6,7,"电话号码"))&&(checkNum("区号",6,document.theform.areaphone))&&(checkNum("电话号码",6,document.theform.phone)))
	  {
		return true;
	}
	else
	{
		return false;
	}
}
//判断公司所在地
function confirmprovince()
{
	if (judgeLength(document.theform.province,7,0,"公司所在地"))
	{
		return true;
	}
	else
	{
		return false;
	}
}
//判断县区街道地址(已经删除)

//判断公司名称
 function confirmcorpname()
{
	if (judgeLength(document.theform.corpname,9,0,"公司名称")&&judgeLength(document.theform.corpname,9,60,"公司名称"))
	  {
		return true;
	}
	else
	{
		return false;
	}
}
//判断你的职位
 function confirmposition()
{
	if (judgeLength(document.theform.position,10,0,"你的职位")&&judgeLength(document.theform.position,10,60,"你的职位")&&judgenum2(document.theform.position,10,"你的职位"))
	  {
		return true;
	}
	else
	{
		return false;
	}
}
//判断公司类型
 function confirmtrade()
{
	if (judgeLength(document.theform.trade,11,0,"公司类型"))
	  {
		return true;
	}
	else
	{
		return false;
	}
}
 //判断邮编
function confirmpostcode()
{
	if (judgeLength(document.theform.postcode,12,0,"邮编"))
	  {
		return true;
	}
	else
	{
		return false;
	}
}


//检测帐号唯一
function checkaccounts() {
	//alert(checkinput);
	var accounts = document.theform.accounts.value;
	if (accounts == "") {
		return false;
	}
   if(confirmaccounts()){
	document.checkAccountsForm.TPL_Accounts.value = accounts;
	document.getElementById("WarnMsg0").innerHTML = "<div id='TipError2'>检测中，请稍等……</div><img id='ErrorPointer' src='images/arrow_error.gif'>";
	document.getElementById('WarnMsg0').style.display="";
	document.checkAccountsForm.submit();
   	return true;
   }
	//return true;
}

//检测商友地带笔名唯一(已经删除)

//检测email唯一
function checkemail() {
	//alert(checkinput);
	var email = document.theform.email.value;
	if (email == "") {
		return false;
	}
	if (confirmemail())
	{
	document.checkEmailForm.TPL_Email.value =email;
	document.getElementById("WarnMsg5").innerHTML = "<div id='TipError2'>检测中，请稍等……</div><img id='ErrorPointer' src='images/arrow_error.gif'>";
	document.getElementById('WarnMsg5').style.display="";
	document.checkEmailForm.submit();
	return true;
	}
}
//最后提交检测
function FinialSubmit(feedback)
{
	//alert (feedback);
	//var feed=feedback;
	if(confirmaccounts() && confirmemail() && confirmname()&&confirmPwd()&& confirmSecPwd() && confirmquestion() && confirmanswer() && confirmname()&&  confirmphone()&& confirmprovince()&& confirmtrade()&& confirmposition()&& confirmpostcode())
	{
		
		document.theform.Submit();
		document.getElementById("TipAll").innerHTML=feedback;
		document.getElementById("TipAll").style.display="";
		//showMsg(feedback,17);
		TipAll.style.display="none";
		return true;
	}
	else
	{
	  //alert (confirmusername());

		msg="出错啦！请根据红字提示完成输入。"+feedback;		
		document.getElementById("TipAll").innerHTML=msg;
		document.getElementById("TipAll").style.display="";
		if(!confirmaccounts())
		{
                       
			document.theform.accounts.focus();//用户名
                      
		}
		else if(!confirmname())
		{
			document.theform.name.focus();//真名
		}
		else if(!confirmPwd())
		{
			document.theform.password.focus();//密码
		}
		else if(!confirmSecPwd())
		{
			document.theform.conpassword.focus();//确认密码
		}
		else if(!confirmquestion())
		{
			document.theform.question.focus();//提示问题
		}
		else if(!confirmemail())
		{
			document.theform.email.focus();//电邮
		}
		else if(!confirmphone())
		{
			document.theform.phone.focus();//电话
		}
		else if(!confirmprovince())
		{
			document.theform.province.focus();//公司地址
		}
		
		else if(!confirmcorpname())
		{
			document.theform.corpname.focus();//公司名称
		}
		else if(!confirmposition())
		{
			document.theform.position.focus();//职务
		}
		else if(!confirmtrade())
		{
			document.theform.trade.focus();//公司类型
		}
		else if(!confirmpostcode())
		{
			document.theform.postcode.focus();//邮编
		}
		
		return false;
	}
}

