﻿/// <reference path="jquery-1.2.6.js" />
/// <reference path="AppConnection.js" />
/// <reference path="JavascriptHelper.js" />
/// <reference path="QueryStringHelper.js" />

function ShowLoggedInHeader(username) {
	$('#a_MyPhotoCake').show();
	$('#a_CreateCake').show();
	$('#a_WhyPhotocake').hide();
	$('#a_SignOut').show();
	$('#a_SignIn').hide();
	$("#navWelcomeMessage").show();
	$('.NavSigninCopy').html(username);
}

function ShowGroupAdminLoggedInHeader(username) {
	$('#a_MyPhotoCake').hide();
	$('#a_CreateCake').hide();
	$('#a_WhyPhotocake').hide();
	$('#a_SignOut').show();
	$('#a_SignIn').hide();
	$("#a_Reports").show();
	$("#navWelcomeMessage").show();
	$('.NavSigninCopy').html(username);
}

function HideLoggedInHeader() {
	$('#a_MyPhotoCake').hide();
	$('#a_CreateCake').hide();
	$('#a_WhyPhotocake').show();
	$('#a_SignOut').hide();
	$('#a_SignIn').show();
	$('.NavSigninCopy').html('');
}

function signIn() {
	$('#LoginForm').show();
	$('#ForgotPasswordForm').hide();
	TINY.box.show(document.getElementById('overlaySignin').innerHTML, 0, 557, 375, 1);
}

function signOut() {
	Logout();
	userToken = null;
}

function Login() {
	$(".errLogin").hide();

	var un, pwd;
	un = TinyBoxValue($("input[id=signinUsername]"));
	pwd = TinyBoxValue($("input[id=signinPassword]"));
	if (un.length == 0 || pwd.length == 0)
		$("#errUsernamePwdRequired").show();
	else {
		DecoPac.Authentication.Login(un, pwd, Login_callback);
	}
}

function Login_callback(res) {
	if (res.error && res.error.Message != 'Unknown')
		alert(res.error.Message)
	else {
		var errMsg = $(res.value).find("error").text();
		if (errMsg.length > 0) {
			$('#errLoginError').text(errMsg);
			$('#errLoginFailure').show();
		}
		else {
			if ($(res.value).find("login").attr("status") == 2) {
				window.location.href = $(res.value).find("login").find("GroupAdmin").attr("RedirectTo");
			}
			else {
				var sURL = unescape(window.location.pathname);
				if (sURL.indexOf('/WhyPhotocake') != -1)
				//if (sURL == '/WhyPhotocake.aspx' || sURL == '/WhyPhotoCake_EasyAs123.aspx' || sURL == '/WhyPhotoCake_SweetenSales.aspx' || sURL == '/WhyPhotoCake_WorldClassService.aspx')
					window.location.href = '/MyPhotoCake.aspx';
				else
					window.location.href = sURL;
				//            var username = '';
				//            username = $(res.value).find("login").find('Customer').attr("Name");
				//            ShowLoggedInHeader(username);
				//            TINY.box.hide();
			}
		}
	}
}

function IsLoggedIn() {
	DecoPac.Authentication.IsLoggedIn(IsLoggedIn_callback);
}

function IsGroupAdminLoggedIn() {
	DecoPac.Authentication.IsLoggedIn(IsGroupAdminLoggedIn_callback);
}

function IsLoggedIn_callback(res) {
	if (res.error && res.error.Message != 'Unknown')
		alert(res.error.Message)
	else {
		var x = res.value;
		if (x != null) {
			var username = '';
			if ($(res.value).find("login").find('Customer').length > 0) {
				username = $(res.value).find("login").find('Customer').attr("Name");
				token = $(res.value).find("login").find('Customer').attr("Token");

				ShowLoggedInHeader(username);

				initApp(token);
			}
			else {
				var groupAdmin = 0;
				if ($(res.value).find("login").find('GroupAdmin').length > 0) {
					groupAdmin = $(res.value).find("login").find('GroupAdmin').attr("Admin");
					var username = $(res.value).find("login").find('GroupAdmin').attr("Name");
					if (groupAdmin == 1) {
						ShowGroupAdminLoggedInHeader(username);
						//initApp(null);
					}
				}
				else {
					HideLoggedInHeader();
					checkAndShowLogin();
				}
			}
		}
		else {
			HideLoggedInHeader();
			checkAndShowLogin();
		}
	}
}

function IsGroupAdminLoggedIn_callback(res) {
	var groupAdminLoggedIn = false;
	var username = "";
	if (res.error && res.error.Message != 'Unknown') {
		groupAdminLoggedIn = false;
	}
	else {
		var x = res.value;
		if (x != null) {
			if ($(res.value).find("login").find('GroupAdmin').length > 0) {
				groupAdmin = $(res.value).find("login").find('GroupAdmin').attr("Admin");

				username = $(res.value).find("login").find('GroupAdmin').attr("Name");

				if (groupAdmin == 1) {
					groupAdminLoggedIn = true;
					//initApp(null);
				}
			}
		}
	}
	if (groupAdminLoggedIn) {
		ShowGroupAdminLoggedInHeader();
	}
	else {
		window.location = "Index.aspx";
	}
}

function checkAndShowLogin() {
	///	<summary>
	///		Checks the Querystring, shows the login popup if showLogin=true.
	///     Used when the app needs to redirect customer to the website.
	///     Resources/Xml/AppConfig.xml is where this value is being passed to the app.
	///	</summary>
	///	<returns type="void" />
	var showLogin = getQuerystring("showLogin", "false");

	if (showLogin == "true") {
		signIn();
	}
}

function Logout() {
	DecoPac.Authentication.Logout(Logout_callback);
}

function Logout_callback(res) {
	if (res.error && res.error.Message != 'Unknown')
		alert(res.error.Message)
	else {
		var sURL = unescape(window.location.pathname);
		window.location.href = sURL;
		//HideLoggedInHeader();
	}
}

function ForgotPassword() {
	$('#LoginForm').hide();
	$('#ForgotPasswordForm').show();
	//TINY.box.show(document.getElementById('overlaySignin').innerHTML, 0, 557, 375, 1);
}

function ForgotPasswordSend() {
	$(".errLogin").hide();

	var emailAddress = TinyBoxValue($("input[id=forgotPasswordEmail]"));
	if (emailAddress == '') {
		$("#errEnterUsername").show();
	}
	else {
		DecoPac.Authentication.ForgotPassword(emailAddress, ForgotPasswordSend_callback);
	}
}

function ForgotPasswordSend_callback(res) {
	if (res.error && res.error.Message != 'Unknown')
		alert(res.error.Message)
	else {
		TINY.box.hide();
	}
}
