/**
 * Copyright (c) 2007 JiFenChuanMei, Inc. All rights reserved.
 * This software is the confidential and proprietary information of 
 * JiFenChuanMei, Inc. You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the 
 * license agreement you entered into with JiFenChuanMei.
 */
// JIFENCHUANMEI Javascript Libaray
// Copyright (c) 2007 JIFENCHUANMEI (http://www.jifenka.com/)
// @author yangxilin 会员注册页面脚本

//验证是否同意服务条款
function validateInfo(type){
	//验证会员是否同意所有协议
	/**var agreement = document.getElementById('agreement').checked;
	if(agreement == false){
		alert('请先选择 \'我已阅读且同意以上《福彩电话投注服务协议》\'');
		return false;
	}**/
	//验证用户名
	var userName = document.getElementById('userName').value;
	userName = userName.trim();
	//if(userName.replace(/^\w+$/g,"**").length < 6 || userName.replace(/^\w+$/g,"**").length > 16){
		//alert('用户名长度为6-16个英文字符或数字或下划线,请按要求输入');
		//document.getElementById('userName').focus();
		//return false;
	//}
	if(userName == ''){
		alert('请输入用户名！');
		document.getElementById('userName').focus();
		return false;
	} else if(userName.replace(/[^\x00-\xff]/g,"**").length < 6 || userName.replace(/[^\x00-\xff]/g,"**").length > 16){
		alert('用户名长度为6-16个字符,请按要求输入');
		document.getElementById('userName').focus();
		return false;
	} else {
		//var reg = /^[a-zA-Z0-9_-]+$/;
		var reg = /^[a-z0-9_]+$/;
		if(!reg.test(userName)){
			alert('用户名只能包括小写字母、数字、下划线');
			document.getElementById('userName').focus();
			return false;
		}
	}
	
	if(document.getElementById('isUserNameSuccess').value != ''){
		var isSuccess = document.getElementById('isUserNameSuccess').value;
		if(isSuccess == 'false'){
			alert('对不起，您输入的用户名不可用，请重新输入');
			return false;
		}
	}
	
	//校验密码是否一致
	var passwd = document.getElementById('passwd').value;
	var confirmPasswd = document.getElementById('confirmPasswd').value;
	//passwd = passwd.trim();
	//confirmPasswd = confirmPasswd.trim();
	if(passwd ==''){
		 alert("登陆密码为空");
		 return false;
	}
	if(confirmPasswd ==''){
		alert("确认登陆密码为空");
		return false;
	}
	
	if(passwd.replace(/[^\x00-\xff]/g,"**").length < 6 || passwd.replace(/[^\x00-\xff]/g,"**").length > 16){
		alert('登录密码长度在 6-16 位之间');
		document.getElementById('passwd').focus();
		return false;
	}
	if(confirmPasswd.replace(/[^\x00-\xff]/g,"**").length < 6 || confirmPasswd.replace(/[^\x00-\xff]/g,"**").length > 16){
		alert('确认登录密码长度在 6-16 位之间');
		document.getElementById('confirmPasswd').focus();
		return false;
	}
	if(passwd != confirmPasswd){
		alert('两次登陆密码输入不一致');
		document.getElementById('passwd').value = '';
		document.getElementById('confirmPasswd').value = '';
		return false;
	}
	
	if(type == "all"){
		/**判断真实姓名*/
		var trueName = document.getElementById('addRegTrueName');
		trueName.value = trueName.value.trim();
		var patrn=/^[a-zA-Z]{1,20}|[\u4e00-\u9fa5]{1,10}$/;
		if(trueName.value == ''){
			alert('请输入真实姓名');
			trueName.focus();
			return false;
		}
		if(!patrn.test(trueName.value)){
			alert('真实姓名只能是2-5个汉字或者英文');
			trueName.focus();
			return false;
		}
		if(trueName.value.replace(/[^\x00-\xff]/g,"**").length < 4 || trueName.value.replace(/[^\x00-\xff]/g,"**").length > 16){
			alert('真实姓名长度为4-16个字符,请按要求输入');
			trueName.focus();
			return false;
		}
		/**校验证件号码**/
		if(!checkCertType()){
			return false;
		}
		/**判断联系电话**/
		var phone = document.getElementById('addRegPhone');
		phone.value = phone.value.trim();
		if(phone.value == ''){
			alert('请输入手机号码');
			phone.focus();
			return false;
		}else{
			if (!isMobile(phone.value)) {
				alert('请输入正确的手机号码');
				phone.focus();
				return false;
			}
		}
	}
	//选择安全邮箱则验证
	var safeEmail = document.getElementById('safeEmail');
	safeEmail.value  = safeEmail.value.trim();
	if(safeEmail.value == ''){
				alert('请输入安全邮箱');
				safeEmail.focus();
				return false;
	}else{
		if(safeEmail.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1){		
			alert('安全邮箱地址不正确,请重新输入');
			safeEmail.focus();
			return false;
		}
	}
	/**判断校验码**/
	var authCode = document.getElementById('authCode');
	authCode.value = authCode.value.trim();
	if(authCode.value == ''){
		alert('请输入校验码');
		authCode.focus();
		return false;
	}
	//是否年满18周岁
	if(document.getElementById('isOpenCP333').checked){
			document.getElementById('isOpenCP333').value = 4;
	}else{
		alert('请选择您是否年满18周岁');
		return false;
	}
	/**
	if(document.getElementById('isOpenCPTag').value == 0 || document.getElementById('isOpenCPTag').checked){
		//显示提现信息填写内容
		document.getElementById('liftMoney_details_a').style.display = '';
	}**/
}

//初始化异步基本信息
var xmlHttp;

function createXMLHttpRequest() { 
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}
//异步验证用户名是否可用
function checkUserName(){
	var userName = document.getElementById('userName').value;
	userName = userName.trim();
	//验证昵称格式是否正确
	if(userName == ''){
		showIdMsg('userName','请输入用户名！',false);
		return;
	} else if(userName.replace(/[^\x00-\xff]/g,"**").length < 6 || userName.replace(/[^\x00-\xff]/g,"**").length > 16){
		showIdMsg('userName','用户名长度为6-16个字符,请按要求输入',false);
		document.getElementById('userName').focus();
		return;
	} else {
		//var reg = /^[a-zA-Z0-9_-]+$/;
		var reg = /^[a-z0-9_]+$/;
		if(!reg.test(userName)){
			showIdMsg('userName','用户名只能包括小写字母、数字、下划线',false);
			return;
		}
	}
	//清空消息
	document.getElementById('userNameInfo').innerHTML = '';
	//设置隐藏域值
	document.getElementById('isUserNameSuccess').value = '';
	if(userName != ''){
		//打开连接异步提交
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleStateChange2;
		var url = "/include/members/validateMembersUserName.htm?userName=" + encodeURIComponent(userName);
		xmlHttp.open("POST", url, true);
		xmlHttp.send('');
	}else{
		showIdMsg('userName','请输入用户名！',false);
		return;
	}
}

//结果处理程序
function handleStateChange2() {
	//进度logo
	if(xmlHttp.readyState == 1){
		document.getElementById('userState').style.display = '';
	}
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200){
			document.getElementById('userState').style.display = 'none';
			//得到数据
			var xmlData = xmlHttp.responseXML; 
			//alert('xmlData = ' + xmlData);
			// 取得XML文档的根
            var root = xmlData.documentElement;      
            //alert('root = ' + root);
            // 取得<isSuccess>结果
			var isSuccess = root.getElementsByTagName('isSuccess')[0].firstChild.nodeValue;
			//alert(isSuccess);
			// 取得<msg>结果
			var msg = root.getElementsByTagName('msg')[0].firstChild.nodeValue;
			//显示消息
			if(isSuccess == 'true'){
				msg = "<img src='/images/main/true.gif' alt='输入正确' /> "+msg;
			}else{
				msg = "<img src='/images/main/false.gif' alt='输入错误' /> "+msg;
			}
			document.getElementById('userNameInfo').innerHTML =  msg ;
			//设置隐藏域值
			document.getElementById('isUserNameSuccess').value = isSuccess;
		}
	}
}

//异步验证证件号是否可用
function checkCertCode(){
	var addRegCertCode = document.getElementById('addRegCertCode').value;
	var certType = document.getElementById('certType').value;
	addRegCertCode =addRegCertCode.trim();
	//校验证件号
	if(!checkCertType()){
		return false;
	}
	//清空消息
	document.getElementById('addRegCertCodeInfo').innerHTML = '';
	//设置隐藏域值
	//document.getElementById('isCertCodeSuccess').value = '';
	if(addRegCertCode != ''){
		//打开连接异步提交
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleStateChange3;
		var url = "/include/members/validateMembersCertCode.htm?certCode=" + encodeURIComponent(addRegCertCode)+"&certType="+encodeURIComponent(certType);
		//alert(url);
		xmlHttp.open("POST", url, true);
		xmlHttp.send('');
	}else{
		document.getElementById('addRegCertCode').focus();
		return;
	}
}
//结果处理程序
function handleStateChange3() {
	//进度logo
	if(xmlHttp.readyState == 1){
		document.getElementById('state').style.display = '';
	}
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200){
			document.getElementById('state').style.display = 'none';
			//得到数据
			var xmlData = xmlHttp.responseXML; 
			//alert('xmlData = ' + xmlData);
			// 取得XML文档的根
            var root = xmlData.documentElement;      
            //alert('root = ' + root);
            // 取得<isSuccess>结果
			var isSuccess = root.getElementsByTagName("isSuccess")[0].firstChild.nodeValue;
			// 取得<msg>结果
			var msg2 = root.getElementsByTagName("msg2")[0].firstChild.nodeValue;
			//alert('msg = ' + msg);
			//显示消息
			if(isSuccess == 'true'){
				msg2 = "<img src='/images/main/true.gif' alt='输入正确' /> "+msg2;
			}else{
				msg2 = "<img src='/images/main/false.gif' alt='输入错误' /> "+msg2;
			}
			document.getElementById('addRegCertCodeInfo').innerHTML =  msg2 ;
			 //设置隐藏域值
			//document.getElementById('isCertCodeSuccess').value = isSuccess;
		}
	}
}

//清空隐藏域和消息
function clearIsSuccess(){
	document.getElementById('isSuccess').value = '';
	document.getElementById('isUserNameSuccess').value = '';
	document.getElementById('userNameInfo').innerHTML = '';
	document.getElementById('certCodeInfo').innerHTML = '';
}
function openCP(){
	if(document.getElementById('isOpenCPTag').checked){
		document.getElementById('liftMoney_details_a').style.display = '';
		//document.getElementById('isOpenCP').value = 0;
	}else{
		document.getElementById('liftMoney_details_a').style.display = 'none';
		//document.getElementById('isOpenCP').value = 1;
	}
}
function isOpenCPaa(){
	if(document.getElementById('isOpenCP333').checked){
		//document.getElementById('isOpenCP333').checked=false;
		document.getElementById('isOpenCP333').value = 4;
	}else{
		//document.getElementById('isOpenCP333').checked=true;
		document.getElementById('isOpenCP333').value = 3;
	}
}
function checkIdcard(idcard){
	var area={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"} 
	var idcard,Y,JYM;
	var S,M;
	var idcard_array = new Array();
	idcard_array = idcard.split("");
	//地区检验
	if(area[parseInt(idcard.substr(0,2))]==null) 
	{
		return 4;
	}
	//身份号码位数及格式检验
	switch(idcard.length){
	case 15:
		if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 )){
               ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$/;//测试出生日期的合法性
		} else {
		ereg=/^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$/;//测试出生日期的合法性
		}
	if(ereg.test(idcard))
	{
	   return 0;
	}else {
	   return 2;
	}
	break;
	case 18:
		//18位身份号码检测
		//出生日期的合法性检查 
		//闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
		//平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
		if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 )){
		ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
		} else {
		ereg=/^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$/;//平年出生日期的合法性正则表达式
		}
		if(ereg.test(idcard)){//测试出生日期的合法性
				//计算校验位
				S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
				+ (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
				+ (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
				+ (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
				+ (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
				+ (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
				+ (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
				+ parseInt(idcard_array[7]) * 1 
				+ parseInt(idcard_array[8]) * 6
				+ parseInt(idcard_array[9]) * 3 ;
				Y = S % 11;
				M = "F";
				JYM = "10X98765432";
				M = JYM.substr(Y,1);//判断校验位
				if(M == idcard_array[17])
				{
					return 0; //检测ID的校验位
				}
				else
				{
					return 3;
				}
		}else
		{
	   		return 2;
		}
	  	break;
	  	default:
	  	return 1;
	  	break;
	}
}

function checkIdentifyNumber(Field)
{
   if(Field.value!="" ){
   		if(Field.value.toString().length == 18){
   			var canDate = parseInt(new Date().getFullYear()) - parseInt(Field.value.toString().substr(6,4));
   			if(canDate < 18){
				showIdMsg('addRegCertCode','您还未成年，禁卡购买彩票。彩票帐户无法开通!',false);
				document.getElementById('isOpenCP333').checked = false;
				document.getElementById('isOpenCP333').value = 3;
				return false;
			}
		}
	    var Errors=new Array("验证通过!","身份证号码位数不对!","身份证号码出生日期超出范围或含有非法字符!","身份证号码校验错误!","身份证地区非法!");
        var tvalue=Field.value;
        var result=checkIdcard(tvalue);  
        if(result!=0)
        {
        	showIdMsg('addRegCertCode',Errors[result],false);
               return false;        
        }else
        {
        	return true;
        }          
    }else{
    	return false;
    }
}

//判断身份证
function checkCertType(){
	var addRegCertCode = document.getElementById('addRegCertCode');
	var certType = document.getElementById('certType');
	addRegCertCode.value = addRegCertCode.value.trim();
	if(addRegCertCode.value == ''){
		showIdMsg('addRegCertCode','证件号码不能为空,证件号码将用于您投注以及领奖，每一证件号码只能注册一次',false);
		return false;
	}else{
		if(certType.value == '身份证'){
			if(!checkIdentifyNumber(addRegCertCode)){
				return false;
			}
		}
		return true;
	}
}

//判断电话格式
function comMobile(){
	var phone = document.getElementById('phone').value;
	if(phone.value == ''){
		alert('请输入手机号码');
		phone.focus();
		return false;
	}
}

String.prototype.trim = function() 
{
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}

//判断邮箱
function checkEmail(emailName){
	var Email = document.getElementById(emailName);
	Email.value  = Email.value.trim();
	if(Email.value == ''){
		showIdMsg(emailName,'请输入安全邮箱',false);
		return false;
	}else{
		if(Email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1){		
			showIdMsg(emailName,'安全邮箱地址不正确,请重新输入',false);
			return false;
		}else{
			//正确
			showIdMsg(emailName,'',true);
		}
	}
}

//判断登陆密码
function checkPasswd(passwdId,confirmPasswdId){
	var passwd = document.getElementById(passwdId);
	if(passwd.value ==''){
		 showIdMsg(passwdId,'登陆密码不能为空',false);
		 return false;
	}
	
	var zmsz = /^[a-zA-Z0-9\u4e00-\u9fa5]+$/;
	if(!zmsz.exec(passwd.value))
	{
		showIdMsg(passwdId,'请使用长度为 6-16 个英文字母或数字',false);
		return false;
	}
	
	if(passwd.value.replace(/[^\x00-\xff]/g,"**").length < 6 || passwd.value.replace(/[^\x00-\xff]/g,"**").length > 16){
		showIdMsg(passwdId,'登陆密码长度在 6-16 位之间',false);
		passwd.focus();
		return false;
	}
	checkConfirmPasswd(confirmPasswdId,passwdId);
	showIdMsg(passwdId,'',true);
}

//判断交易密码
function checkPayPasswd(passwdId,confirmPasswdId){
	var passwd = document.getElementById(passwdId);
	if(passwd.value ==''){
		showIdMsg(passwdId,'交易密码不能为空',false);
		return false;
	}
	var patrn = /^[0-9]*$/;
	if(!patrn.exec(passwd.value))
	{
		passwd = passwd.value.substr(0,passwd.length-1);
		showIdMsg(passwdId,'交易密码必须为6位数字',false);
		return false;
	}
	if(passwd.value.length!=6){
		showIdMsg(passwdId,'交易密码必须为6位数字',false);
		return false;
	}
	checkConfirmPasswd(confirmPasswdId,passwdId);
	showIdMsg(passwdId,'',true);
}
//二次密码是否一致
function checkConfirmPasswd(confirmPasswdId,passwdId){
	var confirmPasswd = document.getElementById(confirmPasswdId);
	var passwd = document.getElementById(passwdId);
	if(confirmPasswd.value ==''){
		showIdMsg(confirmPasswdId,'确认密码不能为空',false);
		return false;
	}
	if(passwd.value != confirmPasswd.value){
		showIdMsg(confirmPasswdId,'二次输入的密码不一致',false);
		return false;
	}
	showIdMsg(confirmPasswdId,'',true);
}

//真实姓名
function checkTrueName(){
	var trueName = document.getElementById('addRegTrueName');
	trueName.value = trueName.value.trim();
	var patrn=/^[a-zA-Z]{1,20}|[\u4e00-\u9fa5]{1,10}$/;
	if(trueName.value == ''){
		showIdMsg('addRegTrueName','请输入真实姓名',false);
		return false;
	}
	if(!patrn.test(trueName.value)){
 		showIdMsg('addRegTrueName','真实姓名只能是2-5个汉字或者英文',false);
		return false;
	}
	if(trueName.value.replace(/[^\x00-\xff]/g,"**").length < 4 || trueName.value.replace(/[^\x00-\xff]/g,"**").length > 16){
		showIdMsg('addRegTrueName','真实姓名长度为4-16个字符,请按要求输入',false);
		return false;
	}
	showIdMsg('addRegTrueName','',true);
}

//联系电话格式
function checkPhone(){
	var phone = document.getElementById('addRegPhone');
	if(phone.value ==''){
		 showIdMsg('addRegPhone','联系电话不能为空',false);
		 return false;
	}
	if(!isMobile(phone.value))
    {
		showIdMsg('addRegPhone','联系电话格式不符',false);
		return false;
	}
	showIdMsg('addRegPhone','',true);
}

function isMobile(str) {
	if(str.length!=11){
		return false;
	}
	var patrn = /^(\+|-)?(0|[1-9]\d*)(\.\d*[1-9])?$/;  
	return patrn.test(str);
}

function showIdMsg(id,msg,flag){
	if(flag == false){
		document.getElementById(id+'Info').innerHTML = "<img src='/images/main/false.gif' alt='输入错误' /> "+msg;
	}else{
		document.getElementById(id+'Info').innerHTML = "<img src='/images/main/true.gif' alt='输入正确' /> "+msg;
	}
	document.getElementById(id+'Info').style.display = "inline";
}