﻿function $(id)
{
    return document.getElementById(id);
}

var loginLib = 
{
    // --- GIC web service URL:
    base_url: "//gic.corrigo.com/GIC/LoginLib.asmx",

    // --- param specifying function to call back from GIC.GetApplicationHost web method:
    callback_param: "callback=loginLib.onResultRecieved",

    // --- compose parametrized URL for GIC.GetApplicationHost web method:
    getWebMethodUrl : function (companyName) {
        return document.location.protocol + this.base_url + 
            "?" + this.callback_param +
            "&companyName=" + encodeURIComponent(companyName);
    },

    // --- set controls to be used with loginLib
    bindToControls : function (loginFormId, warningLblId, companyTextBoxId, usernameTextBoxId)
    {
        this.loginFormId = loginFormId;
        this.warningLblId = warningLblId;
        this.companyTextBoxId = companyTextBoxId;
        this.usernameTextBoxId = companyTextBoxId;
    },

    populateControls : function ()
    {
        var args = getPageArguments();

        if (args.userName != null){
            $("userName").value = args.userName;
        }

        if (args.company != null){
            $("company").value = args.company;
        }

        if (args.errorString != null){
            $("lblWarning").innerHTML = args.errorString;
        }
    },

    // --- 
    authenticate : function ()
    {
        var html_doc = document.getElementsByTagName('head').item(0);

        // remove the last included script, we don't need it anymore
        if (this.last_include)
        {
            html_doc.removeChild(this.last_include);
        }

        var script_filename = this.getWebMethodUrl($(this.companyTextBoxId).value);
        var js = document.createElement('script');

        js.setAttribute('language', 'javascript');
        js.setAttribute('type', 'text/javascript');
        js.setAttribute('src', script_filename);

        html_doc.appendChild(js);

        this.last_include = js; // set the pointer to the last include

        return false;
    },

    // --- is called back when result from GIC.GetApplicationHost was recieved
    onResultRecieved : function (result)
    {
        if (null == result.errorString)
        {
            if (this.loginFormId)
            {
                document.forms[this.loginFormId].action = 
                    document.location.protocol + "//" + result.appLoginURL + "/Corp/Login.aspx";

                document.forms[this.loginFormId].submit();
            }
        }
        else
        {
            if (this.warningLblId)
                document.getElementById(this.warningLblId).innerHTML = result.errorString;
        }
    }
}

function LoginPageArguments()
{
    this.userName = null;
    this.company = null;
    this.errorString = null;
}

function getPageArguments()
{
    var sURL = window.document.URL.toString();
    var result = new LoginPageArguments();

    if (sURL.indexOf("?") > 0)
    {
        var arrURLParams = sURL.split("?")[1].split("&");
        var param;

        for (var i = 0; i < arrURLParams.length; i++)
        {
            param = arrURLParams[i].split("=");

            if (param != null && param[1] != "")
            {
                switch (param[0].toLowerCase())
                {
                    case "username":
                        result.userName = decodeURIComponentInclPluses(param[1]);
                    break;
                    case "company":
                        result.company = decodeURIComponentInclPluses(param[1]);
                    break;
                    case "error":
                        result.errorString = decodeURIComponentInclPluses(param[1]);
                    break;
                }
            }
        }
    }

    return result;
}

function decodeURIComponentInclPluses(str)
{
    return decodeURIComponent(str.replace(/\+/g, " "));
}
