// Page Specific JavaScript Document
jQuery(function() {
	var chkPassErr = 1;
	var chkConfirmPassErr=1;
	var chkmail=1;
	var chuser=1;
	var chlast=1;
	var chfirst=1;
	var code =1;
	var mobile =0;

	jQuery("#UserEmail").empty();
	jQuery('#UserProfileMobile').blur(function() 
	{ 
	   var mobile_length = jQuery("#UserProfileMobile").val().length; 
	    if (mobile_length >0  ){
			if(!isInteger(jQuery("#UserProfileMobile").val()) || mobile_length > 12){
				 jQuery("#mobile_warning").html(__("Nhập số không hợp lệ (nhỏ hơn 12 số)"));
				 mobile =1;		
			}else{
				jQuery("#mobile_warning").html("");
				 mobile =0;		
			}
			
		}	
		
	
	});
	jQuery('#UserUsername').blur(function() 
	{ 
		var username_length = jQuery("#UserUsername").val().length; 
		jQuery("#username_warning").empty(); 
		 if (username_length < 6){ 
			 jQuery("#username_warning").html(__("Username is too short"));
			 chuser =1;
			 return false;
		 }
		 jQuery("#username_warning").empty(); 
		 if (username_length >= 6){
			   chkmail =0;
			 jQuery.ajax({
					type: "GET",
					url: www_root+'users/checkusename'+'/username:'+ jQuery("#UserUsername").val(),
					success: function(msg) {
					 if(msg==0){
						 jQuery("#username_warning").html(__("Username have character invalid "));
					 }
					 if(msg==1){
						 jQuery("#username_warning").html(__("Username have exits"));
						   chkmail =0;
					 }
					 	
					}
			  });
			 
		 }else{
			 jQuery("#username_warning").html(__("Username more 6 charater"));
			 return false;
		 }
		 chuser =0;
		
	});
	jQuery('#UserEmail').blur(function() 
			{ 
			 
				jQuery("#warning_email").empty(); 
				 if (!validateEmail(this.value)){ 
					 jQuery("#warning_email").html(__("Email not validate"));
					 chkmail = 1;
					 return false;
				 }
				  chkmail =0;
				 if (chkmail==0){
					 
					 jQuery.ajax({
							type: "GET",
							url: www_root+'users/checkemail'+'/email:'+ jQuery("#UserEmail").val(),
							success: function(msg) {
							
							 if(msg==1){
								 jQuery("#warning_email").html(__("Email have exits"));
								  chkmail =1;
							 }
							 	
							}
					  });
					 
				 }
				
				
			});

	
	jQuery("#UserPw").keyup( function() {
		// Update check
		
		jQuery("#sConfirm_PassWord1").val('');
		jQuery("#warning_pass").empty(); 
		if(this.value.length < 6){
			 jQuery("#pass_warning").html(__("Password is too short"));
			 chkPassErr=1;
			return false;
		}	
		jQuery("#pass_warning").empty(); 
		chkPassErr=0;
	} );

	jQuery("#UserPw").blur( function(){
		// Update check
	
		// if(this.value == ''){
		// return false;
		// }
		jQuery("#pass_warning").empty(); 
			
		var username_pss = jQuery("#UserPw").val().length; 
		 if (username_pss < 6){ 
			 chkPassErr=1;
			 jQuery("#pass_warning").html(__("Password is too short"));
			 return false;
		 }
		
		chkPassErr=0;
		return checkValid("#pass_warning");
		
		
	} );

	jQuery("#sConfirm_PassWord1").blur( function(){
		// Update check

		// End
		if(jQuery("#sConfirm_PassWord1").val()=='' || jQuery("#UserPw").val()=='' || chkPassErr==1)
			
			return false;
		if(jQuery("#UserPw").val() != jQuery("#sConfirm_PassWord1").val()){
			chkConfirmPassErr = 1;
		
			jQuery("#warning_pass").html(__("Password do not match"));
			return false;
		}
		chkConfirmPassErr = 0;
		
		jQuery("#warning_pass").empty(); 

		
	} );
	jQuery("#sConfirm_PassWord1").keyup( function() {	
		// Update check
					 
		if( this.value != "" &&  this.value.length == jQuery("#UserPw").val().length){
			if(jQuery("#UserPw").val() == jQuery("#sConfirm_PassWord1").val()){
				chkConfirmPassErr = 0;
				jQuery("#warning_pass").empty(); 
				jQuery("#pass_warning").empty(); 
				return true;				
			}
		}
		if(this.value.length >= jQuery("#UserPw").val().length){
			if(jQuery("#UserPw").val() != jQuery("#sConfirm_PassWord1").val()){
				chkConfirmPassErr = 1;
				// jQuery("#pass_warning").html(__("Password is too short"));
				jQuery("#warning_pass").html(__("Password do not match"));
				return false;
			}
		}
	} );
	jQuery('#UserProfileFirstName').blur(function() 
	{ 
				var first_length = jQuery("#UserProfileFirstName").val().length; 
				jQuery("#first_warning").empty(); 
				 if (first_length <=0){ 
					 chfirst =1;
					 jQuery("#first_warning").append(__("Last name not empty"));
					 return false;
				 }
				 chfirst =0;
	} );
	jQuery('#UserProfileLastName').blur(function() 
			{ 
						var first_length = jQuery("#UserProfileLastName").val().length; 
						jQuery("#last_warning").empty(); 
						 if (first_length <=0){ 
							 chlast =1;
							 jQuery("#last_warning").html(__("Last name not empty"));
							 return false;
						 }
						 chlast =0;
			} );
	jQuery("#code").keyup( function() {
		// Update check
		
		 jQuery.ajax({
				type: "GET",
				url: www_root+'users/checkcode'+'/code:'+ jQuery("#code").val(),
				success: function(msg) {
				 if(msg==0){
					 jQuery("#codewarning").html(__("code invalid "));
					 code =1;
				 }else
				 {   code = 0;
					 jQuery("#codewarning").html("");
				 }
				 
				 	
				}
		  });
	} );

	jQuery("#code").blur( function(){
		 
		jQuery.ajax({
				type: "GET",
				url: www_root+'users/checkcode'+'/code:'+ jQuery("#code").val(),
				success: function(msg) {
				 if(msg==0){
					 jQuery("#codewarning").html(__("code invalid "));
					 code =1;
				 }else
				 {   code = 0;
					 jQuery("#codewarning").html("");
				 }
		}
				 	
				
		  });
		
		
	} );
	jQuery('#UserProfileAddForm, #UserEditForm').submit(function(){
		if(checksubmit()){
			
			 jQuery.post(www_root+'Users/ajax_register',
		         jQuery(this).serializeArray(),
		            afterValidate,
		            "json"
		        );
			
		}	
			
			
			
        return false;
    });
	 function afterValidate(data, status)  {
		 if (data.errors) {
	            onError(data.errors);
	        }
		 if (data.success) {
			 
			 jQuery("#page_user_register").html(__("Cám ơn bạn đã đăng ký tài khoản tại YANTV.VN. Để hoàn tất việc đăng ký, xin vui lòng kích hoạt tài khoản thông qua đường link được gửi vào hộp mail của bạn. Vui lòng kiểm tra trong spam nếu bạn không thấy trong inbox") );
			
			 
			// window.setTimeout(function() {
		    // window.location.href = www_root+'';
		    // }, 2000);
	       }
	   }
	 function onError(data) {
	       // flashMessage(data.message);
	        jQuery.each(data.data, function(model, errors) {
	            for (fieldName in this) {
				
	                var element = jQuery("#" + camelize(model + '_' + fieldName));
					 if ($('#element_id:visible').length > 0)

	                var _insert = jQuery(document.createElement('div')).insertAfter(element);
					 _insert.text("");
	                _insert.addClass('error-message').text(this[fieldName]);
	            }
	         
	        });
	    };
	    function camelize(string) {
	        var a = string.split('_'), i;
	        s = [];
	        for (i=0; i<a.length; i++){
	            s.push(a[i].charAt(0).toUpperCase() + a[i].substring(1));
	        }
	        s = s.join('');
	        return s;
	    }
	function checksubmit()
	{ var flag= true;
		if( chuser == 1 && jQuery("#UserUsername").val().length==0){
			 jQuery("#username_warning").html(__("Username not empty"));
			 flag= false;
		}
		if( chkmail == 1 && jQuery("#UserEmail").val().length==0){
			 jQuery("#warning_email").html(__("Email not empty"));
			 flag= false;
		}
		if( chkPassErr == 1){
			 jQuery("#pass_warning").html(__("Password not empty"));
			 flag= false;
		}
		if( chkConfirmPassErr == 1){
			 jQuery("#warning_pass").html(__("Password not empty"));
			 flag= false;
		}
		if( chfirst == 1 && jQuery("#UserProfileFirstName").val().length ==0){
			 jQuery("#first_warning").html(__("First name not empty"));
			 flag= false;
		}
		if( chlast == 1 && jQuery("#UserProfileLastName").val().length ==0){
			 jQuery("#last_warning").html(__("Last name not empty"));
			 flag= false;
		}
		if( code == 1){
			 jQuery("#codewarning").html(__("code invalid"));
			 flag= false;
		}
		if( mobile == 1){
			 jQuery("#mobile_warning").html(__("Nhập số không hợp lệ (nhỏ hơn 12 số)"));
			 flag= false;
		}
		return flag;
	}
	function validateEmail(id)
	{
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
		return emailPattern.test(id);

	} 
	function checkValid(obj){
		jQuery(obj).show();
		jQuery(obj).fadeOut(2000);
		return true;
		
	}

	function pass_checkLen(obj){
		if((obj.length != 0 & obj.length < 6) || obj.length > 32)
			return false;
		return true;
	}
	function isInteger(s)
	{ 	var i;
		for (i = 0; i < s.length; i++)
		{
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}
	
});


