/**
 * @author EC Team foobar
 * @version 0.1.0001
 */
var	email_empty 	= 'The Email is required';
var	email_not_exists= 'The email is not exists';
var	email_invalid = 'Email formate is invalid';
var	email_too_long	= 	'The length of email is too long';
var	email_can_use	=	'The Email can use';
var	password_empty 	= 	'The Password is required';
var	password_shorter_min = 'The password is too short [6]';
var	password_shorter_max = 'The password is too long [40]';
var password_ok 	=	'The Password is Ok';
var	confirm_password_invalid =	'The twice input password is not same.';
var	confirm_password_ok = 'The confirm password is ok';
var	verify_code_invalid	=	'The Captcha is invalid';
var	verify_code_ok	=	'The Captcha is Ok';
var	agreement_invalid = 'Please check the agreement';
var agreement_ok = '';
var check_ok = '';
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function show_message(target , message , flag)
{	
	if(flag)
	{
		$(target).text(' ');
		$(target).removeClass('wrong').addClass('right');
		
	}
	else
	{
		//$(target).show();
		$(target).removeClass('right').addClass('wrong');
	}
	if(message== '')
	{
		//$(target).hide();
		$(target).text(' ');
		$(target).removeClass('wrong').removeClass('right');
	}
	$(target).text(message);
}
/**
 * check the email formate
 *
 */
function check_email_formate(one_email) {  
 var pattern = /^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,4}$/i;  
 if(pattern.test(one_email)) {  
  return true;  
 }  
 else {  
  return false;  
 }  
} 
function is_email_used(email)
{
	
	$.ajax({
		   	url:'index.php?c=aff_allocate',
			type:'post',
			data:'email='+email,
			dataType:'json',
			success: function(data){
				if(data.touch == 'ok')
				{
					show_message( '#email_msg', email_not_exists, false);
					change_submit_button_state( false );	
				}
				else if(data.touch == 'confilct')
				{
					show_message( '#email_msg', '' ,true);
					
					change_submit_button_state( true );
					
				}
				else if(data.touch == 'error')
				{
					show_message( '#email_msg', data.msg,false );
					change_submit_button_state( false );					
				}
			}
		   });
}

function check_email()
{		
	var target = $('#aff_login_email');
	if( '' ==  target.val().trim() )
	{
		show_message( '#email_msg', email_empty,false );
		change_submit_button_state( false );
	}
	else if( target.val().trim().length >= 40)
	{
		show_message( '#email_msg', email_too_long,false );
		change_submit_button_state( false );
	}
	else if( !check_email_formate(  target.val().trim().trim() ) )
	{		
		show_message( '#email_msg', email_invalid ,false);
		change_submit_button_state( false );	
	}else
	{
	 is_email_used(  target.val().trim() ) ;	
	}
}

function check_password()
{
	var target = $('#aff_login_pwd');
	if(  target.val().trim() == '' )
	{
		show_message( '#pwd_msg', password_empty ,false);
		change_submit_button_state( false );
	}
	else if(  target.val().trim().length > 0  &&  target.val().trim().length < 6 )
	{
		show_message( '#pwd_msg', password_shorter_min ,false);
		change_submit_button_state( false );
	}
	else if( target.val().trim().length >= 40)
	{
		show_message( '#pwd_msg', password_shorter_max ,false);
		change_submit_button_state( false );
	}
	else
	{
		show_message( '#pwd_msg', '',false);
		change_submit_button_state( true );		
	}

}
function change_submit_button_state( flag )
{
   if (flag == false)
   {
   	document.getElementById('sb_button').disabled = 'disabled';
   }
   else
   {
   	document.getElementById('sb_button').disabled  = '';
   }
}

function check_all()
{
	if( $('#aff_login_email').val() == '')
	{
		show_message( '#email_msg', email_empty ,false);
		$('#aff_email').focus();	
		return false;
	}
	else if( $('#aff_login_pwd').val() == '' )
	{
		show_message( '#pwd_msg', password_empty ,false);
		$('#aff_email').focus();	
		return false;
	}
	else
	{
		//show_message( '#pwd_msg', '',false);
		//show_message( '#email_msg', '' ,false);
		$('#pwd_msg').hide();
		$('#email_msg').hide();
	}
	return true;
}