function pageLoad() {    
    InitJQueryUI();
    $.event.trigger("pageLoaded");
}

/* Setup JQuery UI */
function InitJQueryUI() {
    $(".button").button();
    $("#messageBox").dialog({ autoOpen: false, buttons: { "Ok": function () { $(this).dialog("close"); } } });    
   /* $(".buttonAddNew").button({ icons: { primary: "ui-icon-document"} });
    $(".buttonPopupClose").button({ text: false, icons: { primary: "ui-icon-closethick"} });
    $(".buttonSave").button({ icons: { primary: "ui-icon-disk"} });
    $(".buttonCancel").button({ icons: { primary: "ui-icon-cancel"} });
    $(".buttonBack").button({ icons: { primary: "ui-icon-arrowreturnthick-1-w"} });
    $(".buttonNumericUp").button({ text: false, icons: { primary: "ui-icon-triangle-1-n"} });
    $(".buttonNumericDown").button({ text: false, icons: { primary: "ui-icon-triangle-1-s"} });
    $(".buttonYes").button({ icons: { primary: "ui-icon-check"} });
    $(".buttonNo").button({ icons: { primary: "ui-icon-cancel"} });
    $(".buttonUpload").button({ text: false, icons: { primary: "ui-icon-folder-open"} });
    $(".buttonUnlock").button({ icons: { primary: "ui-icon-unlocked"} });
    $(".buttonPrevious").button({ icons: { primary: "ui-icon-arrowthick-1-w"} });
    $(".buttonNext").button({ icons: { primary: "ui-icon-arrowthick-1-e"} });
    $(".buttonLogin").button({ icons: { primary: "ui-icon-key"} });
    $(".buttonHelp").button({ icons: { primary: "ui-icon-help"} });*/
}

function onAuthenticate(data) {
    try {
        $('#divAuth').dialog('close');
	//alert(data.token);
        if (data.token != '') {
            //alert(location.protocol + '//' + location.host + '/Content/Public/Pages/login.aspx?token=' + data.token + '&productCode=' + $('#Apps').val() + '&callback=' + encodeURIComponent(window.location.href));
            //window.location.assign(location.protocol + '//' + location.host + '/Content/Public/Pages/login.aspx?token=' + encodeURIComponent(data.token) + '&productCode=' + $('#Apps').val());
		
	    var loc = location.protocol + '//' + location.host + '/Content/Public/Pages/login.aspx?token=' + encodeURIComponent(data.token) + '&productCode=' + $('#Apps').val();
            var productCode = $('#Apps').val();
            switch (productCode.toLowerCase()) {
                case "metrixstream":
                    loc = location.protocol + "//" + location.host + "/vigilstream/login.aspx?id=" + encodeURIComponent(data.token) + "&app=PortalLogin";
                    break;
		case "metrixmail":
		    loc = location.protocol + "//" + location.host + "/vigilmail/login.aspx?id=" + encodeURIComponent(data.token) + "&app=PortalLogin";
		    break;
		case "metrixkiosk":
		    loc = location.protocol + "//" + location.host + "/vigilkiosk/login.aspx?id=" + encodeURIComponent(data.token) + "&app=PortalLogin";
		    break;
		case "metrixintegration":
    		    loc = location.protocol + "//" + location.host + "/vigilmetrixintegration/login.aspx?id=" + encodeURIComponent(data.token) + "&app=PortalLogin";
		    break;
		case "sapphireelite":
		    loc = location.protocol + "//" + location.host + "/vigilmetrixelite/autologin.aspx?token=" + encodeURIComponent(data.token);
		    break;
		case "clickstream":
		    loc = location.protocol + "//www.reporting.co.za/autologin.asp?id=" + encodeURIComponent(data.token);
		    break;
		case "loglizard":
		    loc = location.protocol + "//"  + location.host + "/loglizard/autologin.aspx?id=" + encodeURIComponent(data.token);
		    break;
            }
            //alert(loc);
            //window.location.assign(location.protocol + '//' + location.host + '/Content/Public/Pages/login.aspx?token=' + encodeURIComponent(data.token) + '&productCode=' + $('#Apps').val());
            window.location.assign(loc);

         }
        else {
            $('#divAuthError').dialog('open');
        }
    }
    catch (e) { }
}

//Used for application authentication so remote login can take place
function Authenticate() {
    var productCode = $('#Apps').val();
    if (productCode == "-1")
        return false;

    //Code to stop application access
    //switch (productCode.toLowerCase()) {
    //    case "metrixstream":
    //        $('#divApplicationUnavailable').dialog('open');
    //        return false;
    //        break;
    //}

    var handler  = location.protocol + '//' + location.host + '/LoginHandler.ashx';
    //alert(productCode);

    switch (productCode.toLowerCase()) { 
        case "loglizard":
            handler = location.protocol + '//' + location.host + '/LogLizard/LoginHandler.ashx';
            break;
        case "clickstream":
            handler = location.protocol + '//www.reporting.co.za/LoginHandler.asp';
            break;
        default:
            handler = location.protocol + '//' + location.host + '/LoginHandler.ashx';
            break;
    }
    //alert(handler);
	
    $.ajax({
        url: handler,
        context: document.body,
        cache: false,
        dataType: "jsonp",
        type: "POST",
        data: 'username=' + encodeURIComponent($('#username').val()) + '&password=' + encodeURIComponent($('#password').val()) + '&productCode=' + productCode,
        jsonp: "onAuthenticated",
        jsonpCallback: "onAuthenticate",
        beforeSend: function (jqXHR, setting) {
            $('#divAuth').dialog('open');
        },
        success: function (data, textStatus, jqXHR) {
        },
        error: function (jqXHR, textStatus, errorThrown) {
	    //alert(errorThrown);
	    $('#divAuth').dialog('close');
            $('#divError').dialog('open');
        }
    });
}

/* Used for one time setup of events etc. */
$().ready(function () {
    $("#Apps").msDropDown({visibleRows:8});
    $("#btnLogin").click(Authenticate);
    $("#username").keydown(function (e) {
        if (e.which == 13)
	{
	    e.preventDefault(); 
            Authenticate(); 
	}
    });
    $("#password").keydown(function (e) {
        if (e.which == 13)
	{
	    e.preventDefault(); 
            Authenticate(); 
	} 
    });
    $("body").append('<div id="divAuth" style="display:none;"></div>');
    $('#divAuth')
        .html('<img src="' + location.protocol + '//' + location.host + '/App_Themes/Default/images/auth.gif" alt="Authenticating" style="float:right" />We are currently authenticating your credentials.')
        .dialog({
            autoOpen: false,
            modal: true,
            title: 'Authenticating',
            minHeight: "100"
        });

    $("body").append('<div id="divAuthError" style="display:none;"></div>');
    $('#divAuthError')
        .html('<img src="' + location.protocol + '//' + location.host + '/App_Themes/Default/images/authError.gif" alt="Authenticating" style="float:right" />Incorrect username or password.')
        .dialog({
            autoOpen: false,
            modal: true,
            title: 'Incorrect Credentials',
            minHeight: "100"
        });

    $("body").append('<div id="divError" style="display:none;"></div>');
    $('#divError')
    .html('<img src="' + location.protocol + '//' + location.host + '/App_Themes/Default/images/authError.gif" alt="Authenticating" style="float:right" />An unexpected error has occurred while validating your credentials. Please try again.')
    .dialog({
        autoOpen: false,
        modal: true,
        title: 'Unexpected Error',
        minHeight: "100"
    });
    $("body").append('<div id="divApplicationUnavailable" style="display:none;"></div>');
    $('#divApplicationUnavailable')
    .html('<img src="' + location.protocol + '//' + location.host + '/App_Themes/Default/images/authError.gif" alt="Application Unavailable" style="float:right" />The application you are trying to access is currenty unavailable. Please try again later.')
    .dialog({
        autoOpen: false,
        modal: true,
        title: 'Application Unavailable',
        minHeight: "100"
    });
});




