var hostUrl = localhost;
var resendEmail = "";
var resendPassword = "";

$(document).ready( function()
{
	$("#loginForm").validationEngine({
		inlineValidation: false,
		success: function() { ValidationSuccess(); },
		failure: function() { return false; },
		promptPosition: "topRight"
	});
	
	$("#forgotForm").validationEngine({
		inlineValidation: false,
		success: function() { ForgotValidationSuccess(); },
		failure: function() { return false; },
		promptPosition: "topRight"
	});
	
	$("#email").focus(function() {
		$(this).css("border-color", "#FFD39F");
		$(this).css("border-width", "2px");
	});
	
	$("#pass").focus(function() {
		$(this).css("border-color", "#FFD39F");
		$(this).css("border-width", "2px");
	});
	
	$("#email").blur(function() {
		$(this).css("border-color", "#CFCFCF");
		$(this).css("border-width", "1px");
	});
	
	$("#pass").blur(function() {
		$(this).css("border-color", "#CFCFCF");
		$(this).css("border-width", "1px");
	});
	
	$("#divLayoutLoginFormError").hide();
	
	$(".login").live("click", function()
	{
		$.validationEngine.closePrompt(".formErrorContent",false);
		var type = $(this).attr('class').split(' ').slice(-2) + "";
		type = type.replace(",login","");
		$("#divLayoutLoginFormError").slideUp( "slow" , function()
		{
			$.post(localhost + "/Authentication/Login", 
			{
				type: type,
				email: $("#email").val(), 
				password: $("#pass").val()
			},
			function(data)
			{
				if( data.success ) {
					$("#divLayoutLoginFormError").html('Logging in...').removeClass().addClass('rounded2').addClass('noterror').slideToggle("slow",
					function()
					{
						window.location.reload();
					});
				}
				else
				{
					if( data.errormsg == "resend" || data.errormsg == "foundboth" )
					{
						$("#divLayoutLoginFormError").html(data.errorHtml).removeClass().addClass('rounded2').addClass("error").slideToggle("slow");
						resendEmail = data.email;
						resendPassword = data.password;
						$("#ajaxLoader").hide();
						$("#loginbutton").show();
					}
					else
					{
						$("#divLayoutLoginFormError").html(data.errormsg).removeClass().addClass('rounded2').addClass('error').slideToggle("slow");
						$("#ajaxLoader").hide();
						$("#loginbutton").show();
					}
				}
			},
			"json" );
		});
		return false;
	});
	
	$("#forget").click( function()
	{
		$("#divLayoutForgetPasswordForm").animate({opacity: "toggle"} , "slow");
		$.validationEngine.closePrompt(".formErrorContent",false);
	});
	
	$(".resend").live("click", function()
	{
		$.validationEngine.closePrompt(".formErrorContent",false);
		var type = $(this).attr('class').split(' ').slice(-2) + "";
		type = type.replace(",resend","");
		$("#divLayoutLoginFormError").slideToggle("slow",function()
		{
			$.post(localhost + "/Registration/ResendVerification", 
			{
				type: type,
				email: $("#email").val(),
				password: $("#pass").val()
			},
			function(data)
			{
				if( data.success )
				{
					var errorclass = "noterror";
				}
				else
				{
					var errorclass = "error";
				}
				$("#divLayoutLoginFormError").html(data.errormsg).removeClass().addClass('rounded2').addClass(errorclass).slideToggle("slow");
				$("#ajaxLoader").hide();
				$("#loginbutton").show();
			},
			"json" );
		});
	});
	
	var loc = document.location + "";
	loc = loc.replace(localhost + "/?", "");
	if( loc == "1" ) {
		$('#addmsg').html("Please register first to add this contact to your network.");
		$("#addmsg").fadeIn("slow");
	} else if( loc == "2" ) {
		$("#addmsg").html("Please register first to contact this user.");
		$("#addmsg").fadeIn("slow");
	} else if( loc == "3" ) {
		$("#addmsg").html("Please register first to access this page.");
		$("#addmsg").fadeIn("slow");
	}
});

function ForgotValidationSuccess() {
	$.validationEngine.closePrompt(".formErrorContent",false);
	$("#forgotAjaxLoader").show();
	$("#forgotButton").hide();
	$.ajax(
	{
		type: "POST",
		url: localhost + "/Registration/ForgetPassword",
		data: { email: $("#fldForgetEmail").val() },
		dataType: "html",
		success: function(html)
		{
		}
	});
	
	$("#divLayoutForgetPasswordForm").fadeOut("slow" , function()
	{
		$("#fldForgetEmail").val("");
		$("#divLayoutLoginFormError").html("Please check your email and follow the link to change your password").removeClass().addClass('rounded2').addClass('nochange').slideDown("slow");
		$("#forgotAjaxLoader").hide();
		$("#forgotButton").show();
	});
}

function ValidationSuccess() {
	$.validationEngine.closePrompt(".formErrorContent",false);
	$("#loginbutton").hide();
	$("#ajaxLoader").show();
	
	$("#divLayoutLoginFormError").slideUp(function(){
		$.post(localhost + "/Authentication/Login", 
		{
			email: $("#email").val(), 
			password: $("#pass").val()
		},
		function(data)
		{
			if( data.success )
			{
				
				$("#divLayoutLoginFormError").html('Logging in...').removeClass().addClass('rounded2').addClass('noterror').slideToggle("slow",
				function()
				{
					window.location.reload();
				});
			}
			else
			{
				if( data.errormsg == "resend" || data.errormsg == "foundboth" )
				{
					$("#divLayoutLoginFormError").html(data.errorHtml).removeClass().addClass('rounded2').addClass("error").slideToggle("slow", function() {
						resendEmail = data.email;
						resendPassword = data.password;
						$("#ajaxLoader").hide();
						$("#loginbutton").show();
					});
				}
				else
				{
					$("#divLayoutLoginFormError").html(data.errormsg).removeClass().addClass('rounded2').addClass('error').slideToggle("slow", function() {
						$("#ajaxLoader").hide();
						$("#loginbutton").show();
					});
				}
			}
		},
		"json" );
	});
}
