﻿//--------
//- These are the function for getting the SSO token for aerial
function GenerateAerialToken()
{
    var loginBox = document.getElementById("AerialLogin");
    var passwordBox = document.getElementById("AerialPassword");
    
    CreateRequestForToken(loginBox, passwordBox, "Aerial");   
}

//-------
//- These are the function for getting the SSO token for aerial
function GenerateViewpointToken()
{   //Changed to comply with IE 5.5
    var loginBox = document.getElementById("ViewpointLogin");
    var passwordBox = document.getElementById("ViewpointPassword");
    
    CreateRequestForToken(loginBox, passwordBox, "Viewpoint");
}

//-------
///Creats the proper request to for the token based on what the user selects.
function CreateRequestForToken(loginBox, passwordBox, service)
{
    //Don't even send the token request if the username or password is blank.
    if(!IsBlank(loginBox.value) && !IsBlank(passwordBox.value))
    {
        var requestInfo = '?uName=' + JSrot13(loginBox.value) + "&pWord="+ JSrot13(passwordBox.value) + "&siteType=" + service;
    
        GenerateToken(requestInfo);
    }
    else
    {
        ShowLoginError();
    }
}

//-------
//Checks to see if the value passed in is blank
function IsBlank(stringValue)
{
    var returnValue = false;
    if(jQuery.trim(stringValue).length < 1)
    {
        returnValue = true;
    }
    
    return returnValue;
}

///
//Gets the sso handler location and make the call.
function GenerateToken(requestInfo)
{
    var ssoURL = GetSSOLocation();
    ssoURL += requestInfo;
    
    jQuery.get(ssoURL, null, GenerateToken_Callback);

}

///Checks the sso function to see if a valid location was generated.
function GenerateToken_Callback(evt, XMLHttpRequest, ajaxOptions)
{
    if(evt != ':INVALID:')
    {
        window.location.href = evt;
    }
    else
    {
        ShowLoginError();
    }
}


//gets the location of the SSO service from the 
function GetSSOLocation ()
{
    var returnstring = '';
    var ssoURL = document.getElementById("SSOHandlerURL");

    if(typeof(ssoURL) != 'undefined')
    {
        returnstring = jQuery.trim(ssoURL.innerHTML);
    }
    
    return returnstring;
}

//If the enter key was pressed, try to generate the viewpoint token
function CheckViewpointSSOKeyPress(keyPressedInfo)
{
    //ClearLoginError();
    if(IsEnterKeyPressed(keyPressedInfo) == true)
    {
        GenerateViewpointToken();
    }
}

//If the enter key was pressed, try to generate the aerial token
function CheckAerialSSOKeyPress(keyPressedInfo)
{
    //ClearLoginError();
    if(IsEnterKeyPressed(keyPressedInfo) == true)
    {
        GenerateAerialToken();
    }
}

//Checks to see if the enter key was pressed.
function IsEnterKeyPressed(keyPressedInfo)
{
    var keynum;
    var returnVal = false;
    
    if(window.event) // IE
    {
        keynum = keyPressedInfo.keyCode;
    }
    else if(keyPressedInfo.which) // Netscape/Firefox/Opera
    {
        keynum = keyPressedInfo.which;
    }
    
    if(keynum == 13)
    {
        returnVal = true;
    }
    return returnVal;
}

//Removes the login error from the screen
function ClearLoginError()
{
    var errorObject = document.getElementById("LoginError");
    if(errorObject.style.display == "")
    {
        SetObjectVisibility(errorObject, false);
    }
}

//Shows the login error
function ShowLoginError()
{
    var errorObject = document.getElementById("LoginError");
    //SetObjectVisibility(errorObject, true);
    
    alert(jQuery.trim(errorObject.innerHTML));
}

//Sets the visibility on an object
function SetObjectVisibility(varObject, isVisible)
{
    if(isVisible)
    {
        varObject.style.display = "";
    }
    else
    {
        varObject.style.display = "none";
    }
}