// Preload Images
function showLogin()
{
	$.get("layout/en/popup/login.php", function(data)
		{
					    $("#container").html(data);
						$("#loadingDiv").hide();
						ajaxLoginCalls();
					  
						$("#forgot_link").click(function()
							  {
  								  $("#loadingDiv").show();
								  $('#login_response').html("");
								  $("#container").html("")

								  $.get("layout/en/popup/forgot.php", function(data2)
										{ 
											$("#container").html(data2)
											$("#loadingDiv").hide();
								  			ajaxForgotCalls()
										})
								  return false;
							  }
						);
					}
		);

	$('#login_form').modal({
					   close:false
	    });
}

// When DOM is ready
$(document).ready(function()
{
		
	//showLogin();
	// When the form is submitted
	$("#loginBtn").click(function()
	{
		showLogin();
		return false;
	});
});

function reloadPage()
{
	location.reload(); 
}

function ajaxLoginCalls()
{
	$("#container > form").submit(function()
							{  

								// 'this' refers to the current submitted form  
								$('#login_response').html("Processing...");
								var str = $(this).serialize();  

								// -- Start AJAX Call --
		
								$.ajax(
								{  
									type: "POST",
									url: "doLogin.php",  // Send the login info to this page
									data: str,  
									success: function(msg)
										{  
											$("#container").ajaxComplete(function(event, request, settings)
												{  
							 
													 if(msg == 'OK') // LOGIN OK?
													 {  
														 var login_response = '<div id="logged_in">' +
														 '<div style="width: 350px; float: left; margin-left: 70px;">' + 
														 '<div style="margin: 10px 0px 0px 10px; float: right; text-align: left; width: 300px;">'+ 
														 "You are successfully logged in! <br /> Please wait while you're redirected...</div></div>";  
							
														$('a.modalCloseImg').hide();  
													
														 $(this).html(login_response); // Refers to 'status'
							
														// After 3 seconds redirect the 
														setTimeout('reloadPage()', 1000); 
													}else // ERROR?
										 			{  
											 			var login_response = msg;
														$('#login_response').html(login_response);
													 }  
											 });  
											document.forms[0].reset();
									   }  
							});  
								
							// -- End AJAX Call --
					
							return false;
					
						}); // end submit event
}

function ajaxForgotCalls()
{
	$("#container > form").submit(function()
							{  

								// 'this' refers to the current submitted form  
								var str = $(this).serialize();  

								// -- Start AJAX Call --
								$.ajax(
								{  
									type: "POST",
									url: "doForgot.php",  // Send the login info to this page
									data: str,  
									success: function(msg)
										{  
											$("#container").ajaxComplete(function(event, request, settings)
												{  
							 
													 if(msg == 'OK') // LOGIN OK?
													 {  
														 var login_response = '<div style="margin: 10px 0px 0px 10px; text-align: center; width: 450px;">'+ 
														 "Your password has been sent to your email address!</div>";  
							
														$('a.modalCloseImg').hide();  
													
														 $(this).html(login_response); // Refers to 'status'
							
														// After 3 seconds redirect the 
													}else // ERROR?
										 			{  
											 			var login_response = msg;
														$('#login_response2').html(login_response);
													 }  
											 });  
											document.forms[0].reset();
									   }  
							});  
								
							// -- End AJAX Call --
					
							return false;
					
						}); // end submit event
}

