// /////////////////////////////////////////////////////////////////////////////
//
// PMP_USERPWD.JS - Contains JavaScript used by the form-based PMP 
//                  username / password authentication page
// Dependencies: 
//     package.css - Common Novartis style sheets
//     pmp_common.js - Common PMP functionality.
// Author: Manuel Schwarze, PKI Development 
// Department: GISM, PKI 
// 
// /////////////////////////////////////////////////////////////////////////////
// 
// $Workfile: pmp_userpwd.js $
// $Revision: 4 $
// $Date: 10.08.05 17:48 $
// $Author: Schwama8 $
//
// /////////////////////////////////////////////////////////////////////////////

//
// Statics
//

// ////////////////////////////////////////////////////////////////////////////
// THE FOLLOWING STATIC VARIABLES CAN BE CHANGED ON A SITE-TO-SITE BASIS.
// ////////////////////////////////////////////////////////////////////////////
var BUTTON_LOGIN = "Login";
var BUTTON_LOGIN_SECURE = "Secure Login";

// ////////////////////////////////////////////////////////////////////////////
// DO NOT CHANGE THE FOLLOWING STATIC CONSTANTS AS THEY ARE PMP SPECIFIC.
// ////////////////////////////////////////////////////////////////////////////

// POST Request Parameters
var REQUEST_PARAM_SITENAME = "pmpii_sitename";
var REQUEST_PARAM_USERNAME = "pmpii_username";
var REQUEST_PARAM_PASSWORD = "pmpii_password";
var REQUEST_PARAM_REMEMBER = "remember";

//
// Static code
//
var strButtonLogin = BUTTON_LOGIN;

if ((URL_PMP_FORM_AUTH.URL.indexOf("https://") == 0) ||
    ((URL_PMP_FORM_AUTH.URL.indexOf("://") == -1 || 
      URL_PMP_FORM_AUTH.URL.indexOf("://") > 10) &&
     location.protocol == "https:"))
{
    strButtonLogin = BUTTON_LOGIN_SECURE;
}

// =============================================================================

//
// Public Methods (called from page hosting the choose auth HTML code)
//

//
// Writes out the necessary HTML code to start the login form.  
//
function StartLoginForm()
{
    // Write out form
    var str = "<FORM name='userpwd' action='" + URL_PMP_FORM_AUTH.URL +
        "' onSubmit='return OnSubmit();' method='POST'>" +
        "<input type='hidden' name='" + REQUEST_PARAM_SITENAME +
        "' value='" + GetParam(URL_PARAM_SITE) + "'>";
    
    document.writeln(str);
}  

//
// Writes out the necessary HTML code for the username field of the login form.  
//
function WriteUserNameFormField()
{
    document.writeln("<INPUT type='text' size='35' name='" + REQUEST_PARAM_USERNAME + "'>");  
}

//
// Writes out the necessary HTML code for the password field of the login form.  
//
function WritePasswordFormField()
{
    document.writeln("<INPUT type='password' size='35' name='" + REQUEST_PARAM_PASSWORD + "'>");
}

//
// Writes out the necessary HTML code for the checkbox field of the login form.  
//
function WriteRememberCheckboxFormField()
{
    document.writeln("<INPUT type='checkbox' size='35' name='" + REQUEST_PARAM_REMEMBER + "'>");
}

//
// Writes out the necessary HTML code for the llogin button of the login form.  
//
function WriteLoginButton()
{
    document.writeln("<input name='submit' type='submit' value='" + strButtonLogin + "'>");
}

//
// Writes out the necessary HTML code to end the login form and to pre-populate
// form fields.  
//
function EndLoginForm()
{
    document.writeln("</FORM>");
    
    // Pre-fill user name, if cookie can be found
    var strUser = getCookie(COOKIE_PMP_USERNAME); 
    if (strUser == "null" || strUser == "undefined")
        strUser = "";
    
    var strRemember = getCookie(COOKIE_PMP_REMEMBER_USERNAME);
    if (strRemember == "null" || strRemember == "undefined")
        strRemember = "";
    
    if (document.userpwd[REQUEST_PARAM_REMEMBER])
    {
        document.userpwd[REQUEST_PARAM_REMEMBER].checked = 
            (strRemember == "true" || strRemember == "");
    }
    
    if (document.userpwd[REQUEST_PARAM_USERNAME])
    {
        document.userpwd[REQUEST_PARAM_USERNAME].value = 
            (document.userpwd[REQUEST_PARAM_REMEMBER].checked ? strUser : "");
    }
    
    if (document.userpwd[REQUEST_PARAM_USERNAME])
    {
        if (document.userpwd[REQUEST_PARAM_USERNAME].value == "")
        {
            document.userpwd[REQUEST_PARAM_USERNAME].focus();
        }
        else if (document.userpwd[REQUEST_PARAM_PASSWORD])
        {
            document.userpwd[REQUEST_PARAM_PASSWORD].focus();
        }
    }
    else if (document.userpwd[REQUEST_PARAM_PASSWORD])
    {
        document.userpwd[REQUEST_PARAM_PASSWORD].focus();
    }
}

//
// Call this method to evaluate the users credentials and to submit them
// to the PMP server.
// Return: True, if submitted successfully. False otherwise.
//
function OnSubmit()
{
    var bRet = true;
    
    if (document.userpwd[REQUEST_PARAM_USERNAME].value == "")
    {
        alert("Please enter your username and password.");
        document.userpwd[REQUEST_PARAM_USERNAME].focus();
        bRet = false;
    }
    else
    {   
        setCookie(COOKIE_PMP_USERNAME, document.userpwd[REQUEST_PARAM_REMEMBER].checked ? 
                                       document.userpwd[REQUEST_PARAM_USERNAME].value : "");
        setCookie(COOKIE_PMP_REMEMBER_USERNAME, document.userpwd[REQUEST_PARAM_REMEMBER].checked);
    }
    
    return bRet;    
}

