/*
**	@desc:	PHP ajax login form using jQuery
**	@author:	programmer@chazzuka.com
**	@url:		http://www.chazzuka.com/blog
**	@date:	15 August 2008
**	@license:	Free!, but i'll be glad if i my name listed in the credits'
*/

$(document).ready(function(){ 

	var wrapperId 	=	'#registerbox';		// main container
	var waitId		=	'#wait';		// wait message container
	var formId		=	'#frmreg';	// submit button identifier
	var userId		=	'#u';			// user input identifier
	var passId		=	'#p';			// password input identifier
	var verifyId		=	'#verify';			// user input identifier
	var vcode		=	'#vcode';
	var passId2		=	'#p2';			// password input identifier

	
	var waitNote	=	'Loading...';											// loading message
	var jsErrMsg	=	'User or password is not valid';						// clientside error message
	
	var postFile	=	'/index.php?act=01&do=01';	// post handler
	
	var autoRedir	=	false;			// auto redirect on success
	
	// hide first
	$(waitId).hide(); $(wrapperId).hide();
	
	// FirstLoad
	$(waitId).html(waitNote).fadeIn('fast',function(){
		// get request to load form
		$.getJSON(postFile, function(data){
			
			if(data.status==true) {
				// status is authorized
				if(autoRedir){ 
					$(waitId).hide().html('Redirecting...').fadeIn('fast', function(){window.location=data.url;});
				} else {
					$(waitId).fadeOut('slow', function(){ $(wrapperId).html(data.message).slideDown(); }).html();
				}
			} else {
				// show form
				$(wrapperId).html(data.message).slideDown('slow',function(){
					// hide  message
					$(waitId).fadeOut('fast',function(){
						
						//*/ submit handler
						$("#frmreg").submit( function() { 
							// loading
							$(waitId).html(waitNote).fadeIn();
								
							var _u = $(userId).val();	// form user
							var _p = $(passId).val();	// form id
							var _p2 = $(passId2).val();
							var _verify = $(verifyId).val();
							var _vcode = $(vcode).val();
							//@ verify username -> email
							var emailPat=/^(.+)@(.+)$/;
							var matchArray=_u.match(emailPat);
							//@ valid user ( modify as needed )
							if (matchArray==null){$(waitId).html('Invaild email address.').fadeIn('fast',function(){$(userId).focus();});} 
							else{//@ valid password
									if(_p.length<6){$(waitId).html('password length must larger than 6.').fadeIn('fast',function(){$(passId).focus();});}
									else{//@ valid password 
											if(_p != _p2){$(waitId).html('Twice password does not match.').fadeIn('fast',function(){$(passId).focus();});}
											else{	//@ Verify code
												if(_verify.length<6){$(waitId).html('Invaild verify code.').fadeIn('fast',function(){$(verifyId).focus();});}
												else{ //@ All corrent start POST
												$.post(postFile, { u: _u, p: _p, p2: _p2, vcode: _vcode, verfiy: _verify}, function(data) {
													if(data.status==true){ 
														if(autoRedir){ 
															$(waitId).html('Redirecting...').fadeIn('fast', function(){
															window.location=data.url;
														});
													} else {
														$(waitId).fadeOut('slow', function(){ 
															$(wrapperId).slideUp('slow',function(){
																$(this).html(data.message).slideDown();
															}); 
														}).html();
													}
												} else {
													$(waitId).html(data.message).slideDown('fast', function(){ 
														$(userId).focus(); 
													}); 
												}
											}
											,'json');
										}
									  }
									}
								}
							return false;
						});				
						//*/
						$(userId).focus();
					}).html();
				});
				
			}
			
		 });
	});
});