var ceilPhoneReadyFirst = false;
var ceilPhoneReady = false;
var validCodeReady = false;
var mobilePhoneError = "";
$(document).ready(function(){
	if (typeof window['DWRUtil'] == 'undefined') 
        window.DWRUtil = dwr.util; 
    if (typeof window['DWREngine'] == 'undefined') 
        window.DWREngine = dwr.engine;
    $('#mobilePhone').focus();
    $('#changeValid').click(function(){
   	 	validCodeReady	= false;
    	$('#validCode').val('');
    	document.getElementById('validCode').focus();
    	$('#validTip').html('');
		$('#geneValidCode').attr('src', "../valid_code.jsp?id="+ Math.random());
	});  	
//	 $('#ceilTip').html('<img src="http://png.gc73.com.cn/website_img/icon_arrow_s.png"/>仅支持移动用户');
})
function mobileRegisterIndexOk(){
	if (!validCodeReady) {
		var validCode = $('#validCode').val();
		validCodeInfo(validCode);
	}
	if (!ceilPhoneReady) {
		var remobilePhone = $('#remobilePhone').val();
		ceilPhoneReCheck(remobilePhone);
		if (!ceilPhoneReady) {
			if (mobilePhoneError != "" || mobilePhoneError.length == 0) {
				alert(mobilePhoneError);
				return;
			} else {
				alert("您要注册的手机号码存在问题，请您确认！");
				return;
			}
		}
	}
	//在此处进行3分钟内只能发送3次验证码判断 
	var cookieVar = getCookie($('#remobilePhone').val());
	var curDate = new Date();
	if(cookieVar == null){
		setCookie($('#remobilePhone').val(),curDate.getYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+"-"+curDate.getHours()+"-"+curDate.getMinutes()+"-"+1,1);
	}else{
		var tempCookieVar = new Array();
		tempCookieVar = cookieVar.split("-");
		if(parseInt(curDate.getYear()) == parseInt(tempCookieVar[0])){
			if(curDate.getMonth()+1 == tempCookieVar[1]){
				if(parseInt(curDate.getDate()) == parseInt(tempCookieVar[2])){
					if(curDate.getHours() == tempCookieVar[3]){
						if((parseInt(curDate.getMinutes()) - parseInt(tempCookieVar[4])) > 3){
							delCookie($('#remobilePhone').val());
							setCookie($('#remobilePhone').val(),curDate.getYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+"-"+curDate.getHours()+"-"+curDate.getMinutes()+"-"+1,1);
						}else{
							if(	parseInt(tempCookieVar[5]) >=3 ){
								alert("对不起，操作过于频繁，请一分钟后再试！");
								return;
							}else{
								tempCookieVar[5] = parseInt(tempCookieVar[5]) +1;
								setCookie($('#remobilePhone').val(),curDate.getYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+"-"+curDate.getHours()+"-"+curDate.getMinutes()+"-"+tempCookieVar[5],1);
							}
						}
					}else{
						delCookie($('#remobilePhone').val());
						setCookie($('#remobilePhone').val(),curDate.getYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+"-"+curDate.getHours()+"-"+curDate.getMinutes()+"-"+1,1);	
					}
				}else{
					delCookie($('#remobilePhone').val());
					setCookie($('#remobilePhone').val(),curDate.getYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+"-"+curDate.getHours()+"-"+curDate.getMinutes()+"-"+1,1);	
				}
			}else{
				delCookie($('#remobilePhone').val());
				setCookie($('#remobilePhone').val(),curDate.getYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+"-"+curDate.getHours()+"-"+curDate.getMinutes()+"-"+1,1);	
			}
		}else{
			delCookie($('#remobilePhone').val());
			setCookie($('#remobilePhone').val(),curDate.getYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()+"-"+curDate.getHours()+"-"+curDate.getMinutes()+"-"+1,1);	
		}
		
	}
	setTimeout('phoneRegisterStepOneOk()',500);
}
//手机注册第一步提交
function phoneRegisterStepOneOk(){
	if (!validCodeReady) {
		alert("您填写的验证码错误，请重新输入");
		return;
	}
	$('#submitShow').html('正在注册中，请耐心等候。。');
	$('#mobile_register_form').ajaxSubmit({
		url : 'mRegRequest.do',
		type : 'post',
		dataType : 'json',
		cache : false,
		success : function(data) {
			if (data.error == 'S') {
				alert("尊敬的用户：\n    您好！手机验证码已经发送到您的手机，请注意查收。");
				window.location.href = "mRegPage.do";
			} else {
				alert(data.message);
				$('#submitShow').html('<a href="javascript:mobileRegisterIndexOk();" id="mobileRegBtn">获取手机激活码</a>');
				return;
			}
		}
	});
}
function ceilPhoneCheck(mobilePhone) {
	if (mobilePhone == null || mobilePhone.length == 0) {
		 ceilPhoneReadyFirst = false;
 		 $('#ceilTip').attr('style', 'color:#FF0000;font-size:12px;');
		 $('#ceilTip').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />'
						+ '请输入您要注册的手机号码');
		 mobilePhoneError = "请输入您要注册的手机号码";
		 return;
	}
	MobileRegisterService.mobilePhoneCheck(mobilePhone, {
		callback : function(str) {
			if (str == 'S') {
				$('#ceilTip').attr('style', 'color:#009900;font-size:12px;');
				$('#ceilTip').html('<img src="http://png.gc73.com.cn/website_img/right_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />'
								+ "恭喜！帐号可用");
				ceilPhoneReadyFirst = true;
				mobilePhoneError = "";
				return;
			} else {
				$('#ceilTip').attr('style', 'color:#FF0000;font-size:12px;');
				$('#ceilTip').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />'
								+ str);
				ceilPhoneReadyFirst = false;
				mobilePhoneError = str;
				return;
			}
		}
	});
}

function ceilPhoneReCheck(remobilePhone) {
	var mobilePhone = $('#mobilePhone').val();
	if (remobilePhone == null || remobilePhone.length == 0) {
		 $('#reCeilTip').attr('style', 'color:#FF0000;font-size:12px;');
		 $('#reCeilTip').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />'
						+ '请再次输入您要注册的手机号码');
		mobilePhoneError = "请再次输入您要注册的手机号码";
		ceilPhoneReady = false;
		return;
	}
	if (!ceilPhoneReadyFirst) {
		ceilPhoneCheck(mobilePhone);
		if (!ceilPhoneReadyFirst) {
			ceilPhoneReady = false;
			return;
		}
	}
	if (mobilePhone != remobilePhone) {
		$('#reCeilTip').attr('style', 'color:#FF0000;font-size:12px;');
		$('#reCeilTip').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />'
						+ '您两次输入的手机号码不一致，请重新输入');
		mobilePhoneError = "您两次输入的手机号码不一致，请重新输入";
		ceilPhoneReady = false;
		return;
	}
	ceilPhoneReady = true;
	mobilePhoneError = "";
	$('#reCeilTip').attr('style', 'color:#009900;font-size:12px;');
	$('#reCeilTip').html('<img src="http://png.gc73.com.cn/website_img/right_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-3px;" />');
	return;
}

function validCodeInfo(validCode) {
	if (validCode == null || validCode.length == 0) {
		validCodeReady = false;
		$('#validTip').attr('style', 'color:#FF0000;font-size:12px;font-weight:normal;');
		$('#validTip').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-7px;" />'
						+ '请输入验证码');
		return;
	}
	RegisterInfoService.checkValidateCode(validCode, {
		callback : function(str) {
			if (str == 'S') {
				validCodeReady = true;
				$('#validTip').attr('style', 'color:#009900;font-size:12px;');
				$('#validTip').html('<img src="http://png.gc73.com.cn/website_img/right_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-7px;" />');
				return;
			} else {
				validCodeReady = false;
				$('#validTip').attr('style', 'color:#FF0000;font-size:12px;font-weight:normal;');
				$('#validTip').html('<img src="http://png.gc73.com.cn/website_img/error_icon.gif" width="15" height="14" style="vertical-align:bottom;display:inline;position:relative;padding-right:5px;top:-7px;" />'
						+ str);
				return;
			}
		}
	});
}

function reCeilTipRemainder(mobilePhone) {
	if (mobilePhone == null || mobilePhone.length == 0) {
		$('#reCeilTip').html('<img src="http://png.gc73.com.cn/website_img/icon_arrow_s.png"/>请再次输入您要注册的手机号码');
	}
}

function ceilTipRemainder(mobilePhone) {
	if (mobilePhone == null || mobilePhone.length == 0) {
//		$('#ceilTip').html('<img src="http://png.gc73.com.cn/website_img/icon_arrow_s.png"/>仅支持移动用户');
	}
}
