$.ajaxSetup({ cache: false }); 

function showLoginPOP(Warning,Action) {
	if (Warning=='Yes')
	{ 	$("#LoginWarning").show();
	}

 
if (Action=='AddRecipeReview')
	{ 	destURL=this.location.href + "&action=addrecipereview";
	}
	else if (Action=='AddRecipePhoto')
	{
	destURL=this.location.href + "&action=addrecipephoto";
	}
	else if (Action=='AddGroupPhoto')
	{
	destURL=this.location.href + "&action=addgroupphoto";
	}
	else if (Action=='AddRecipeGroup')
	{
	destURL=this.location.href + "&action=addrecipegroup";
	}
	else if (Action=='AddRecipe')
	{	
	destURL="/addrecipe.asp";
	}
	else if (Action=='AddGroup')
	{
	destURL="/addgroup.asp";
	}
	else if (Action=='AddAnswer')
	{
	destURL=this.location.href + "&action=addanswer";
	}
	else if (Action=='AddRecipeRequest')
	{
	destURL="/addrecipe.asp?requestid=" + $('#requestid').val();
	}
	else if (Action=='EditRecipe')
	{
	destURL="/addrecipe.asp?recipeid=" + $('#recipeid').val();
	}
	else if (Action=='MyProfile')
	{
	destURL="/member.asp";
	}

	else
	{
	destURL=this.location.href;
	}

	var popID = 'LoginPOP'
    var popWidth = '500'

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });
    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 

return false;
};

//Close Popups and Fade Layer
$('a.close, #fade, #close').live('click', function() { //When clicking on the close or fade layer...
    $('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove();  //fade them both out
    });
    return false;
});


function IsEmail(email) {   var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;   return regex.test(email); } 


function logout()
{	
var url = "/logout.asp";
 
	$.get(url, function(data) { 
     if (data=="1")
    {
	 // now update logged in status on top of page
	$("#loggedin_top").hide();
	$("#notloggedin_top").show();
	}
	
   })
   	 .error(function() { alert("Error occured while talking to the server. Please try again.");})

return false;
}

function login()
{	
 
	if ($('#loginemail').val() == "" || !IsEmail($('#loginemail').val())) 		
	{
		alert("Please enter a valid email");
		$("loginemail").focus();
		return false;
	}
	
	$("#resendsuccess").hide();
	$("#resendsuccess2").hide();
	$("#resenderror").hide();
	$("#resenderror2").hide();
	$("#loginerror2").hide();
	$("#loginerror3").hide();
	$("#loginerror4").hide(); 
	$("#loginerror5").hide();
	$("#loginloading").show();

 
	var url = "/site_login.asp";
 
	$.post(url, {email: $('#loginemail').val(), password: $('#loginpsw').val()}, function(data) { 

     if (data=="1")
     {
	// alert(data);
	 showResponse(data);}
	else 
	{
	// alert(data);
	showException(data);}
   })
	 .error(function() { alert("Error occured while talking to the server. Please try again.");
		$("#logonloading").hide();
		$("#loginerror").hide();
		$("#login").show(); });
 
	function showResponse(req)
	{	
		if (req=="1")	
		{
			$("#loginloading").hide();
			$("#loginsuccess").show();
			$("#loginemail").val("");
			$("#loginpsw").val("");
			$("#resendsuccess").hide();
			$('#fade , .popup_block').fadeOut(function() {
			$('#fade, a.close').remove();  //fade them both out
			 });
			 // now update logged in status on top of page
			$("#loggedin_top").show();
			$("#notloggedin_top").hide();
			$("#loginsuccess").hide();
			window.location.href=destURL;
		}
	}
	function showException(req)
	{
	if (req=="2")	
		{
			$("#loginloading").hide();
			$("#loginerror2").show();
			$("#loginpsw").val("");
		}
		

	if (req=="3")	
		{
			$("#loginloading").hide();
			$("#loginerror3").show();
			$("#loginpsw").val("");
		}

	if (req=="4")	
		{
			$("#loginloading").hide();
			$("#loginerror4").show();
			$("#loginpsw").val("");

		}
	if (req=="5")	
		{
			$("#loginloading").hide();
			$("#loginerror5").show();
			$("#loginpsw").val("");

		}

	}
		return false;
}

function showlogin()
{	

$("#loginpsw").show();
$("#submitlogin").show();
$("#resendpassword").hide();
$("#backtologin").hide();
$("#forgotpassword").show();
$("#ResendPswHeader").hide();
$("#ResendConfHeader").hide();
$("#LoginHeader").show();
$("#resenderror").hide();
$("#loginerror3").hide();
$("#loginerror4").hide();
$("#loginerror5").hide();
$("#resendconfirmation").hide();

return false;
}

function showresendpassword()
{	
$("#loginerror2").hide();
$("#loginpsw").hide();
$("#submitlogin").hide();
$("#resendpassword").show();
$("#backtologin").show();
$("#forgotpassword").hide();
$("#ResendPswHeader").show();
$("#LoginHeader").hide();
$("#ResendConfHeader").hide();	
$("#resendsuccess").hide();
$("#resendsuccess2").hide();
$("#resenderror").hide();
$("#resenderror2").hide();
$("#resendconfirmation").hide();
return false;
}

function showresendconfirmation()
{	
$("#loginerror3").hide();
$("#loginpsw").hide();
$("#submitlogin").hide();
$("#resendconfirmation").show(); 
$("#backtologin").show();
$("#forgotpassword").hide();
$("#ResendConfHeader").show();
$("#LoginHeader").hide();
$("#ResendPswHeader").hide();
$("#resendsuccess").hide();
$("#resendsuccess2").hide();
$("#resenderror").hide();
$("#resenderror2").hide();
return false;
}

function resendpassword()
{
if ($('#loginemail').val() == "" || !IsEmail($('#loginemail').val())) 		
	{
		alert("Please enter a valid email");
		$("loginemail").focus();
		return false;
	}

	$("#loginloading").show();
	$("#resenderror").hide();
 
	var url = "/resendpassword.asp";
 
	$.post(url, {email: $('#loginemail').val()}, function(data) { 
     if (data=="1")
     {
	 // alert(data);
	// show success green bar	
	// then return to login screen

	showlogin();
	$("#loginloading").hide();
	$("#resendsuccess").show();
	}
	else 
	{
	// alert(data);
	// show errorbar email not found
		$("#loginloading").hide();
		$("#resenderror").show();

}
   })
	 .error(function() { alert("Error occured while talking to the server. Please try again.");
		$("#logonloading").hide();
		$("#loginerror").hide();
		$("#login").show(); });

}

function resendconfirmation()
{
if ($('#loginemail').val() == "" || !IsEmail($('#loginemail').val())) 		
	{
		alert("Please enter a valid email");
		$("loginemail").focus();
		return false;
	}

	$("#loginloading").show();
	$("#resenderror").hide();
 
	var url = "/resendconfirmation.asp";
 
	$.post(url, {email: $('#loginemail').val()}, function(data) { 
     if (data=="1")
     {
	 // alert(data);
	// show success green bar	
	// then return to login screen

	showlogin();
	$("#loginloading").hide();
	$("#resendsuccess2").show();
	}
	else 
	{
	// show errorbar email not found
		$("#loginloading").hide();
		$("#resenderror").show();
}
   })
	 .error(function() { alert("Error occured while talking to the server. Please try again.");
		$("#logonloading").hide();
		$("#loginerror").hide();
		$("#login").show(); });

}

