﻿/*
Copyright (c) 2010 by 5 Limes Pty Limited. All rights reserved.
This material may not be duplicated for any purpose without the
express written consent of 5 Limes Pty Limited of Sydney, Australia.
http://www.5Limes.com.au
*/

function CMyPanasonicLoginPanel() { this._initialise(); }

CMyPanasonicLoginPanel.prototype =
{
	_initialise: function () {
		jQuery("#tnUsername, #tnPassword").keypress(function (e) {
			// Redirect the "enter" key to hit the login button
			if (e.keyCode == "13") {
				e.preventDefault();
				jQuery("#btnLoginNav").click();
			}
		});
		jQuery("#btnLoginNav").click(function (e) {
			e.preventDefault();
			var userName = jQuery("#tnUsername").val();
			var pwd = jQuery("#tnPassword").val();
			if (userName.length == 0 || pwd.length == 0) {
				jQuery("#divUsername").addClass('error');
				jQuery("#divPassword").addClass('error');
				jQuery("#incorrectCredentialsMessage").text("Username or password cannot be empty");
				jQuery("#incorrectCredentialsMessage").show();
			}
			else {
				jQuery.ajax({
					type: "POST",
					contentType: "application/json; charset=utf-8",
					url: "/webservices/SitecoreAPI.asmx/LoginUser",
					data: "{'username': '" + userName + "', 'password': '" + pwd + "'}",
					dataType: "json",
					success: function (result) {
						if (result.d.LoggedIn) {

							if ((jQuery("#hdFullUrl").val()).substring(0, 12) == '/My+Panasonic') {
								window.location.reload();
							}
							jQuery("#incorrectCredentialsMessage").hide();
							jQuery("#loginControls").hide();
							jQuery("#loggedInMessage #loggedInUsername").text(result.d.Message);
							jQuery("#loggedInMessage").show();
						}
						else {
							jQuery("#divUsername").addClass('error');
							jQuery("#divPassword").addClass('error');
							jQuery("#incorrectCredentialsMessage").text(result.d.Message);
							jQuery("#incorrectCredentialsMessage").show();
						}
					}
				});
			}
		});
        jQuery("#btnLogoutNav").click(function (e) {
            e.preventDefault();
            jQuery.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/webservices/SitecoreAPI.asmx/LogoutUser",
                dataType: "json",
                success: function (result) {
                    if (result.d == true) {
                        window.location.reload();
                    }
                }
            });
        });
	}
}

var gMyPanasonicLoginPanel = null;

if (jQuery) {
    jQuery(function () {
		gMyPanasonicLoginPanel = new CMyPanasonicLoginPanel();
    });
}

