var obEmail=new MyFunctions();
$(document).ready(function(){
	/**************************
	***	Validation to login ***
	***************************/
	$('#bttnlogin').click(function(){
		if(jQuery.trim($('#userName').val()).length==0)
		{
			$('#divuser').html(' - Enter your Username.');
			$('#userName').val('');
			$('#userName').focus();
			return false;
		}
		else if(jQuery.trim($('#passwd').val()).length==0)
		{
			$('#divpass').html(' - Enter your password.');
			$('#passwd').val('');
			$('#passwd').focus();
			return false;
		}
		else
			$('#frmlogin').submit();
	});
	
	/***********************************
	***	Validation to create account ***
	************************************/
	$('#bttnsave').click(function(){
		$('#errorMssgUser').html('');
		$('#errorMssg').html('');
		if(jQuery.trim($('#userNameC').val()).length==0)
		{
			//$(this).append('<div style="color: #99FF00">Enter your Username.</div>');
			$('#errorMssgUser').html('- Enter your Username.');
			$('#userNameC').val('');
			$('#userNameC').focus();
			return false;
		}
		else if(jQuery.trim($('#password').val()).length==0)
		{
			$('#errorMssgUser').html('- Enter your password.');
			$('#password').val('');
			$('#password').focus();
			return false;
		}
		else if(jQuery.trim($('#confPass').val()).length==0)
		{
			$('#errorMssgUser').html('- Please retype your password.');
			$('#confPass').val('');
			$('#confPass').focus();
			return false;
		}
		else if(jQuery.trim($('#password').val())!=jQuery.trim($('#confPass').val()))
		{
			$('#errorMssgUser').html('- Retype password mismatched.');
			$('#confPass').select();
			return false;
		}
		else if(jQuery.trim($('#firstName').val()).length==0)
		{
			$('#errorMssg').html('- Enter your First Name.');
			$('#firstName').val('');
			$('#firstName').focus();
			return false;
		}
		else if(jQuery.trim($('#lastName').val()).length==0)
		{
			$('#errorMssg').html('- Enter your Last Name.');
			$('#lastName').val('');
			$('#lastName').focus();
			return false;
		}
		else if(jQuery.trim($('#email').val()).length==0)
		{
			$('#errorMssg').html('- Enter your Email.');
			$('#email').val('');
			$('#email').focus();
			return false;
		}
		else if(jQuery.trim($('#email').val()).length>0 && !obEmail.isvalidemail($('#email').val()))
		{
			$('#errorMssg').html('- Enter a valid Email Id.');
			$('#email').select();
			return false;
		}
		/*else if(jQuery.trim($('#address').val()).length==0)
		{
			$('#errorMssg').html('- Enter your Address.');
			$('#address').val('');
			$('#address').focus();
			return false;
		}*/
		else
			return true;
	});
	
	/***********************************************************
	***	Verifying the username is available or not available ***
	************************************************************/
	$('#userNameC').blur(function(){
		if(jQuery.trim($('#userNameC').val()).length>0)
		{
			$.ajax({
						type: "POST",
						url: "userAction.php",
						data: "mode=checkUsername&userName="+$(this).val(),
						success: function(msg){
							$('#shwMssg').html(msg);
					}
			});
		}
		else
			$('#shwMssg').html('');
		return false;
	});
	
	/***************************************************************
	***	Forgot Password section goes here. Open the modal window ***
	****************************************************************/
	var divImg=$("#divImg");
	$('#forgotpass').click(function(){
		$.blockUI(divImg,{ 'width':'575px','height':'220px','top':'270px','left':'420px','right':'100px','color':'#fff','background-color':'#000','font-family':'Verdana','font-weight':'normal','font-size':'12px'});
		return false;
	});
	
	/***************************************************
	***	Validation of the forgot password modal form ***
	****************************************************/
	$('#bttnSubmit').click(function(){
		$('#Divmsg').html('');
		if(jQuery.trim($('#usernameModal').val()).length==0 && jQuery.trim($('#emailModal').val()).length==0)
		{
			$('#Divmsg').html('- Either enter your Username or Email address.');
			$('#usernameModal').val('');
			$('#usernameModal').focus();
			return false;
		}
		else if(jQuery.trim($('#emailModal').val()).length>0 && !obEmail.isvalidemail($('#emailModal').val()))
		{
			$('#Divmsg').html('- Enter a valid Email Id.');
			$('#emailModal').select();
			return false;
		}
		else
		{
			/**************************************************
			***	Calling Ajax to send password to user email ***
			***************************************************/
			$('#Divmsg').html('<img src="images/gallery-loader.gif" alt="loading" width="32" height="32" />');
			$.ajax({
					type: "POST",
					url: "userActionsAjax.php",
					data: "mode=ForgotPass&username="+jQuery.trim($('#usernameModal').val())+"&email="+jQuery.trim($('#emailModal').val()),
					success: function(msg){
						if(msg)
						{
							$('#Divmsg').html(msg);
							$('#emailModal').val('');
							$('#confemailModal').val('');
						}
					}
			});
		}
		return false;
	});
	
	/*******************************
	***	Closing the modal window ***
	********************************/
	$("#bCancU").click(function(){
		$('#Divmsg').html('');
		$('#usernameModal').val('');
		$('#emailModal').val('');
		$('#confemailModal').val('');
		$.unblockUI();
	});
	
	$('#bttnPrev').click(function(){
		window.location.href='gallery-step2.php';
		return false;
	});
	
	$('#bttnNxt').click(function(){
		window.location.href='gallery-step4.php';
		return false;
	});
});