/**
 * @author lk
 */
mobileall = false;
Event.observe(window,'load', function(){
	$('getvalidate').observe('click',function(){
		ChkForm();
	});
	
	$('account').observe('keydown',function(event){
		event = event?event:window.event;
		if (event.keyCode == 13){
			ChkForm();
		}
	});
	
	$('getaccount').observe('click',function(){
		ChkVal();
	});
	
	$('validate').observe('keydown',function(event){
		event = event?event:window.event;
		if (event.keyCode == 13){
			ChkVal();
		}
	});	
});

String.prototype.trim = function(){
    return this.replace(/(^[\s]*)|([\s]*$)/g, "");
}

function fourthnum(obj){
	var reg = /^\d\d\d\d$/;
    return reg.test(obj);
}

/**
 * 判断是否手机号码
 * @param {Object} account
 */
function isMobile(account){
	if(!(account.length==11 || account.length==12 || account.length==8 || account.length==10)) return false;
    var reg0 = /^13\d{9}$/; //130--139。至少7位
    var reg1 = /^15\d{9}$/; //联通153。至少7位
    var reg2 = /^18\d{9}$/; //电信189。至少7位
    var reg3 = /\d{12}$/;   //小灵通
    var reg4 = /(09)?\d{8}$/; //本地固话/台湾手机号
    return reg0.test(account) || reg1.test(account)|| reg2.test(account)|| reg3.test(account)|| reg4.test(account);
}

function show_masinfo(){
	var obj = $('getmasinfo');
	if(obj.style.display == 'none'){
		obj.style.display = '';
	} else if(obj.style.display == '')
		obj.style.display = 'none';
}

//检测手机号
var sendContent = '';
var countSec = 60;				//一分钟时间(秒)
var secinterval;		//定义变量
var sectimeout;
function ChkForm(){
	sendContent = $('account').value.trim();
	if(!$('tips').hasClassName('errTips')){
		$('tips').addClassName('errTips');
	}
	
	if(sendContent == ""){
		$('tips').show();
		$('tips').update('请填写您的手机号！');
		$('account').focus();
		return false;
	}
	if(!isMobile(sendContent)){
		$('tips').show();
        $('tips').update('请输入您的真实手机号');
        $('account').focus();
        return false;
	}
	mobilecheck(sendContent);
	if(!mobileall)
		return false;
	
	//$('tips').removeClassName('errTips');
	//$('tips').hide();
	$('getvalidate').disabled = true;
	secinterval = window.setInterval("divSec()",1000);			//每秒getvalidate按钮值的改变
	sectimeout = window.setTimeout("afterSixty()",1000*60);  	//60秒后getvalidate按钮的值

	url = "/Register/getvalidate";
	var pars = "sendContent="+encodeURIComponent(sendContent);
	var searchAjax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (e) { ajax_show(e); } });	
}

function ajax_show(e){
	var r = e.responseText;
	eval('r='+r);
	$('tips').show();
	if(!$('tips').hasClassName('errTips')){
		$('tips').addClassName('errTips');
	}
	if(e.status == 200){
		switch (r){
			case 1:
				$('tips').hide();
				$('getmasinfo').style.display = '';
				break;
			case -1:
				$('tips').update('您没有填写手机号码,请联系客服!');
				break;
			case -999:	
				$('tips').show();
				$('getvalidate').disabled = true;
				window.clearInterval(secinterval);
				window.clearTimeout(sectimeout);
				$('getvalidate').value = '获取验证码';
				$('tips').update('短信发送次数过多请稍后再试!');
				break;			
			default:	
				window.clearTimeout(sectimeout);
				afterSixty()
				$('tips').update('参数丢失');
				break;
		}
	}
}

//倒计时60秒
function divSec(){
	countSec--;
	if(countSec>0) $('getvalidate').value = '获取验证码('+countSec+')';
}

function afterSixty(){
	window.clearInterval(secinterval);
	$('getvalidate').value = '重新获取验证码';	
	$('getvalidate').disabled = false;
	countSec = 60;
}

//检测验证码
function ChkVal(){
	var sendval;
	sendval = $('validate').value.trim();
	sendContent = $('account').value.trim();

	$('valtips').removeClassName('corrTips');
	$('valtips').addClassName('errTips');
	
	if(sendval == ""){
		$('valtips').show();
		$('valtips').update('请填写您收到四位验证码！');
		$('validate').focus();
		return false;
	}
	if(!fourthnum(sendval)){
		$('valtips').show();
		$('valtips').update('验证码必须是四位数字！');
		$('validate').focus();
		return false;
	}	
	if(sendContent == ""){
		$('valtips').show();
		$('valtips').update('请填写您的手机号！');
		$('account').focus();
		return false;
	}
	if(!isMobile(sendContent)){
		$('valtips').show();
        $('valtips').update('请输入您的真实手机号');
        $('account').focus();
        return false;
	}else{
		$('valtips').hide();
	}
	if($('isAccept').checked == false){
		$('valtips').show();
		$('valtips').update('您必须接受协议所有条款！');
		return false;
	}
	
	url = "/forgetpwd/checkval";
	var pars = "sendval="+encodeURIComponent(sendval)+"&account="+sendContent;
	var val_Ajax = new Ajax.Request(url, {method: "post", parameters: pars, onComplete: function (e) { ajax_val(e); } });
		
}

function ajax_val(req){
	r = req.responseText;
	eval('r='+r);
	$('valtips').show();
	if(req.status == 200){
		switch (r){
			case 1:
				$('valtips').hide();
				window.location.href="/Register/step2";
				break;
			case -1:
				$('valtips').update('参数丢失');
				break;
			default:	
				$('valtips').update('验证失败');
				break;
		}
	}	
}

/**
 * 手机检测
 * @param {Object} obj
 */
function mobilecheck(mobile){
	if(!mobile)
		return;
	var url = "/register/mobilecheck";
	var text = mobile;
	var pars = "mobile=" + text;
	var myAjax = new Ajax.Request(url, {method: "post", parameters: pars, onSuccess: function (req) { mobileavailable(req);}});
}

function mobileavailable(req){
	if (req.responseText=='1'){
		$('tips').hide();
		mobileall = true;
		return mobileall;
	}else{
		$('tips').show();
		$('tips').update("手机号码已经被注册,请选择找回密码");
		mobileall = false;
		return mobileall;
	}
}