var gRight = '<img src="/images/tip_1.gif"> ';
var gWrong = '<img src="/images/tip_2.gif"> ';
var gTipCodeOn = gTipCodeOn;
var gProStatus = 0;
var gInputEmail = '';
var gInputUname = '';

$(document).ready(function(){
	$('#email').focus(showEmailTip);
	$('#email').blur(showEmailTipBlur);

	$('#uname').focus(showUnameTip);
	$('#uname').blur(showUnameTipBlur);

	$('#pass').focus(showPassTip);
	$('#pass').blur(showPassTipBlur);
	$('#pass').keyup(passLevel);

	$('#cpass').blur(showCpassTipBlur);
	$('#cpass').focus(showCpassTip);

	$('#code').focus(showCodeTip);
	$('#code').blur(showCodeTipBlur);

	$('#submit').click(checkForm);
	$('#qtime').val(new Date().getTime());
});
function checkForm(){
	//email
	var iEmail = checkEmail(1);
	var iUname = checkUname(1);
	var iPass = checkPass();
	var iCpass = checkCpass();
	var iCode = checkCode();
    
	if(gProStatus != 1 || iEmail || iUname || iPass || iCpass || iCode){
		if(gProStatus != 1 && gProStatus != 2) checkUname();
		return false;
	}
	else{
		if(checkIagree()) return false;
		//post step1
		var sEmail = $.trim($('#email').val());
		var sUname = $.trim($('#uname').val());
		var sPass = $.trim($('#pass').val());
		var sCode = $.trim($('#code').val());
		var sTrueName = $.trim($('#true_name').val());
		var sIdc = $.trim($('#idc').val());
		var sContact = $.trim($('#contact').val());
		var sAddress = $.trim($('#address').val());
		var sSec = hex_sha1(sEmail + sUname + sPass);
		var sPid = $('#pid').val();
		$('#submit').attr('disabled', true);
		jQuery.post(Regindex_js_ajax,
		{'email':sEmail, 'uname':sUname, 'pass':sPass, 'code':sCode, 'sec':sSec,'true_name':sTrueName,'idc':sIdc,'contact':sContact,'address':sAddress,'pid':sPid},
		function(sData){
			var $tipCode = $('#tip_code');
			var aItem = sData.split("\n");
			sValue = aItem[0];
			$('#submit').attr('disabled', false);
			if(sValue == -1){
				$tipCode.html(gWrong + Code_js_tip_error);
				tipWrong('tip_code');
				reCode();
			}
			else if(sValue == 1){
				$tipCode.html('');
				var usNme = '';
				//sBkurl = $('#bkurl').val();
				name= $('#uname').val();
				if(name != '') {
					usNme = '?name=' + name;
					window.location.href = 'http://v3uc.prayaya.com/regok.php';
					return;
				}
				window.location.href = 'http://v3uc.prayaya.com/regok.php';
			}
			else if(sValue == -20){
				tipRight('tip_uname');
				$('#tip_uname').html(gWrong + Uname_js_tip_used);
			}
			else if(sValue == -21){
				tipRight('tip_email');
				$('#tip_email').html(gWrong + Email_js_tip_used);
			}
			else{
				alert(Connect_js_busy);
			}
		});
	}

}
function checkIagree(){
	error = 0;
	if($('#iagree').attr('checked') != true){
		alert(V3agree_js_tip);
		error = 1;
	}
	return error;
}
function checkCode(){
	var sCode = $.trim($('#code').val());
	var error = 0;
	var sTip;
	var sId = 'tip_code';
	if(sCode.length < 4){
		error = 1;
		sTip = gWrong + gTipCodeOn;
		tipWrong(sId);
	}
	else{
		sTip = '';
		//tipRight(sId);
	}
	$('#' + sId).html(sTip);
	return error;
}
function reCode(){
	$('#pcode').attr('src', '/user/code.php?t=' + new Date().getTime());
}
function passLevel(){
	var sP = $('#pass').val();
	var iLength = sP.length;
	var iLevel = 0;
	if(sP.match(/[0-9]+/))
		iLevel++;
	if(sP.match(/[a-z]+/))
		iLevel++;
	if(sP.match(/[A-Z]+/))
		iLevel++;
	if(sP.match(/[^a-z0-9]+/i))
		iLevel++;
	//if(sP && iLength < 4)
	//	iLevel = 1;
	$('.c-visible').css({'visibility':'visible'});
	tipNormal('tip_pass');
	$('#tip_pass').html(Uname_js_rule);
	$('#pass_level_bar').attr('class', 'bg c-plv-' + iLevel);
}
function checkCpass(){
	var $tipCpass = $('#tip_cpass');
	var sPass = $('#pass').val();
	var error = 0;
	var sTip;
	var sId = 'tip_cpass';
	if(sPass != $('#cpass').val()){
		error = 1;
		if($('#cpass').val() == ''){
			sTip = gWrong + Passconfirm_js_rule;
		}
		else{
		sTip = gWrong + Passconfirm_js_notmatched;
		}
		tipWrong(sId);
	}
	else if(sPass == ''){
		sTip = '';
	}
	else{
		sTip = gRight;
		tipRight(sId);
	}
	$tipCpass.html(sTip);
	return error;
}
function checkPass(){
	var $pass = $('#pass');
	var pass = $('#pass').val();
	var $tipPass = $('#tip_pass');
	var error = 0;
	var ok = 1;
	var sTip = '';
	if(pass == '') {
		error = 1;
		$('.c-visible:first').css({'visibility':'visible'});
		sTip = gWrong + Password_js_notnull;
		tipWrong('tip_pass');
		$tipPass.html(sTip);
		return error;
	}
	if(pass.indexOf(' ') != -1){
		error = 1;
		$('.c-visible:first').css({'visibility':'visible'});
		sTip = gWrong + Password_js_nokongge;
		tipWrong('tip_pass');
		$tipPass.html(sTip);
		return error;
	}
	//uname != pass
	var sUname = $.trim($('#uname').val());
	if(sUname != '' && sUname == pass){
		error = 3;
		$('.c-visible:first').css({'visibility':'visible'});
		sTip = gWrong + password_js_not_equname;
		tipWrong('tip_pass');
		$tipPass.html(sTip);
		return error;
	}

	var passLength = pass.length;
	for(var i = 0; i < passLength; i++){
		if(pass.charCodeAt(i) < 33 || pass.charCodeAt(i) > 125) ok = 0;
	}
	if(!ok || passLength < 6 || passLength > 20){
		error = 2;
		$('.c-visible').css({'visibility':'visible'});
	}
	if(ok == 0){
		error = 4;
		sTip = gWrong + password_js_rules;
		tipWrong('tip_pass');
		$tipPass.html(sTip);
	}
	if(!error) {
		tipRight('tip_pass');
		$tipPass.html(gRight);
		$('.c-visible:last').css({'visibility':'hidden'});
	}
	return error;
}
function checkUname(){
	var $uname = $('#uname');
	var uname = $.trim($uname.val());
	var $tipUname = $('#tip_uname');
	var pattern = /^[a-z0-9\-_.\u4E00-\u9FA5]{2,24}$/i;
	var error = 0;
	var sTip = '';
	if(uname == ''){
		error = 1;
		sTip = Uname_js_notnull;
	}
	else if(uname.length < 2){
		error = 2;
		sTip = Uname_js_length;
	}
	else if(!pattern.test(uname)){
		error = 3;
		sTip = Uname_js_patten;
	}
	else if(uname.match(/^\d/)){
		error = 4;
		sTip = Uname_js_pattenhead;
	}
	else{
		if(arguments[0] == 1) return 0;
		if(gInputUname == uname){
			if(gProStatus == 1) return 0;
			return 1;
		}
		jQuery.get('/user/check-uname.php',
		{'u':uname,'tm':new Date().getTime()},
		function(sData){
			if(sData == 1){
				sTip = gRight;
				tipRight('tip_uname');
				gProStatus = 1;
			}
			else{
				sTip = gWrong + Uname_js_ajaxused;
				tipWrong('tip_uname');
				gProStatus = 2;
			}
			$tipUname.html(sTip);
			gInputUname = uname;
		});
	}
	if(error) {
		sTip = gWrong + sTip;
		tipWrong('tip_uname');
	}

	$tipUname.html(sTip);
	return error;

}
function checkEmail(){
	var $email = $('#email');
	var email = $.trim($email.val());
	var $tipEmail = $('#tip_email');
	var error = 0;
	var pattern = /^[a-z0-9][a-z0-9_\-\.]*[a-z0-9]*\@[a-z0-9_\-]+(\.[a-z0-9]{2,5}){1,}$/i;
	var sTip = '';

	if(email == ''){
		error = 1;
		sTip = Email_js_required;
	}
	else if(!pattern.test(email)){
		error = 2;
		sTip = email_js_pattenwrong;
	}
	else{
		sTip = gRight;
		tipRight('tip_email');
	}
	if(error){
		sTip = gWrong + sTip;
		tipWrong('tip_email');
	}
	$tipEmail.html(sTip);
	//if(error) $email.focus();
	return error;
}
function showCodeTip(){
	if($(this).val() == ''){
		$('#tip_code').html(gTipCodeOn);
		tipNormal('tip_code');
	}
}
function showCodeTipBlur(){
	if($(this).val() == ''){
		$('#tip_code').html('');
	}
	else checkCode();
}
function showAddressTip(){
	if($(this).val() == ''){
		$('#tip_address').html(gTipAddressOn);
		tipNormal('tip_address');
	}
}
function showAddressTipBlur(){
	if($(this).val() == ''){
		$('#tip_address').html('');
	}
	else checkAddress();
}
function showContactTip(){
	if($(this).val() == ''){
		$('#tip_contact').html(gTipContactOn);
		tipNormal('tip_contact');
	}
}
function showContactTipBlur(){
	if($(this).val() == ''){
		$('#tip_contact').html('');
	}
	else checkContact();
}
function showIdcTip(){
	if($(this).val() == ''){
		$('#tip_idc').html(gTipIdcOn);
		tipNormal('tip_idc');
	}
}
function showIdcTipBlur(){
	if($(this).val() == ''){
		$('#tip_idc').html('');
	}
	else checkIdc();
}
function showTrueNameTip(){
	if($(this).val() == ''){
		$('#tip_true_name').html(gTipTrueNameOn);
		tipNormal('tip_true_name');
	}
}
function showTrueNameTipBlur(){
	if($.trim($(this).val()) == ''){
		$('#tip_true_name').html('');
	}
	else checkTrueName();
}
function showCpassTipBlur(){
	if($('#pass').val() != '' && $(this).val() == '') $('#tip_cpass').html('');
	else checkCpass();
}
function showCpassTip(){
	if($('#cpass').val() == '' && $('#pass').val() != ''){
		$('#tip_cpass').html(password_js_confirmed);
		tipNormal('tip_cpass');
	}
}
function showPassTip(){
	if($('#pass').val() == ''){
		$('.c-visible').css({'visibility':'visible'});
		$('#tip_pass').html(Password_js_required);
		tipNormal('tip_pass');
	}
}
function showPassTipBlur(){
	if($(this).val() == '') {
		$('.c-visible').css({'visibility':'hidden'});
	}
	else checkPass();
}
function showEmailTip(){
	if($('#email').val() == ''){
		var sTip = Email_js_required;
		$('#tip_email').html(sTip);
		tipNormal('tip_email');
	}
}
function showEmailTipBlur(){
	if($(this).val() == '') $('#tip_email').html('');
	else checkEmail();
}
function showUnameTip(){
	var uname = $.trim($('#uname').val());
	var $tipUname = $('#tip_uname');
	if(uname == ''){
		$tipUname.html(Uname_js_tipcon);
		tipNormal('tip_uname');
	}
}
function showUnameTipBlur(){
	if($(this).val() == '') $('#tip_uname').html('');
	else checkUname();
}
function tipRight(id){
	$('#' + id).css({'background-image':'none'});
}
function tipWrong(id){
	$('#' + id).css({'background-image':'none'});
}
function tipNormal(id){
	$('#' + id).css({'background-image':'url(/images/icon_s.gif)'});
}
