var _saml_uid;
var _saml_pwd;
var _npc_submit_call;

//Login
var loggingIn = false;
function loginButtonClick(actionId, obj) {
    if (!loggingIn) {
        loggingIn = true;

        var login = $.trim($("input[id$='txtLogin']").val());
        var password = $.trim($("input[id$='txtPassword']").val());

	if(login == null || login.length == 0){
		login = $.trim($("input[id$='txtLogin']")[1].value);
	}

	if(password == null || password.length == 0){
		password = $.trim($("input[id$='txtPassword']")[1].value);
	}	

        if (login.length > 0 && password.length > 0) {
            if ($('div.utilityleft > div.formlogon-mainpanel').length > 0)
                $('body').append("<div class='login-loading'><img src='/global/ui/images/ux/bkg-loading-wheel.gif' /></div>");
            if (actionId != null)
                ROIAction(actionId);
            return checkCPSStatus(obj, login, password);
        }
        else {

            $("input[id$='txtLogin']").val(login);
            $("input[id$='txtPassword']").val(password);

            return true;
        }
    }
}

function checkCPSStatus(obj, login, password) {
    if (login.length > 0 && password.length > 0) {
        var samlupwd = new Array();
        samlupwd["userid"] = login;
        samlupwd["password"] = password;

        postToSAML("/profile/samlrequest.epx?kNtBzmUK9zU=1&BHEQK=1", samlupwd, "post");

        _npc_submit_call = obj.getAttribute("href");

        return false;
    }

    return true;
}

function postToSAML(path, params, method) 
{     
	method = method || "post"; 
	// Set method to post by default, if not specified.      
	var form = document.createElement("form");     
	form.setAttribute("method", method);     
	form.setAttribute("action", path);  
	form.setAttribute("target", "_samlIFrame");
	    
	for(var key in params) {         
		var hiddenField = document.createElement("input");         
		hiddenField.setAttribute("type", "hidden");         
		hiddenField.setAttribute("name", key);         
		hiddenField.setAttribute("value", params[key]);          
		form.appendChild(hiddenField);     
	}      

	document.body.appendChild(form);     
	form.submit(); 
}

function saml_login_no_sso_callback(AuthMessage, AuthStatus, ConsolidationUrl)
{
    
    var flag = true;

		var _cps_mobjForm = document.getElementById('mobjForm');
		if (_cps_mobjForm == null) {
		    _cps_mobjForm = document.getElementById('Form1');
		}
		
		var input_AUTH_STATUS = document.getElementById("X_CPS_AUTHSTATUS");
		if(input_AUTH_STATUS == null){add_input_field(_cps_mobjForm, "hidden", "X_CPS_AUTHSTATUS", AuthStatus);}else{input_AUTH_STATUS.value = AuthStatus;}
		var input_AUTH_MESSAGE = document.getElementById("X_CPS_AUTHMESSAGE");
		if (input_AUTH_MESSAGE == null) { add_input_field(_cps_mobjForm, "hidden", "X_CPS_AUTHMESSAGE", AuthMessage); } else { input_AUTH_MESSAGE.value = AuthMessage; }
		if (AuthMessage.indexOf("CONSOLIDATION_REQUIRED") > -1) {
		    flag = false;
		    callConsolidator(ConsolidationUrl);
		}
     
     if (flag) {
         eval(_npc_submit_call);
     }
}

function saml_login_callback(AuthMessage, AuthStatus, ConsolidationUrl)
{
	var flag = true;
	var _cps_mobjForm = document.getElementById('mobjForm');
	if (_cps_mobjForm == null) {
	    _cps_mobjForm = document.getElementById('Form1');
	}
	var input_AUTH_STATUS = document.getElementById("X_CPS_AUTHSTATUS");
	if(input_AUTH_STATUS == null){add_input_field(_cps_mobjForm, "hidden", "X_CPS_AUTHSTATUS", AuthStatus);}else{input_AUTH_STATUS.value = AuthStatus;}
	var input_AUTH_MESSAGE = document.getElementById("X_CPS_AUTHMESSAGE");
	if (input_AUTH_MESSAGE == null) { add_input_field(_cps_mobjForm, "hidden", "X_CPS_AUTHMESSAGE", AuthMessage); } else { input_AUTH_MESSAGE.value = AuthMessage; }
	if (AuthMessage.indexOf("CONSOLIDATION_REQUIRED") > -1) {
	    flag = false;
	    callConsolidator(ConsolidationUrl);
	}

    if (flag) {
        eval(_npc_submit_call);
    }
}

//**************************************************************************************************
//Utility Functions

function add_input_field(form_object, field_type, field_name, field_value) 
{
if(form_object) {
var input = document.createElement('INPUT');
if(document.all) {
input.type = field_type;
input.name = field_name;
input.value = field_value;
} else if(document.getElementById) {
input.setAttribute('type', field_type);
input.setAttribute('name', field_name);
input.setAttribute('value', field_value);
input.setAttribute('id', field_name);
}
form_object.appendChild(input);
}
}

function callConsolidator(url) {

    handler = function (data) {
        if (data) {
            window.open(data, "CPSConsolidationWindow", "location=no,toolbar=no,scrollbars=yes,resizable=yes", false);
        }
        eval(_npc_submit_call);
    };
    //var curl = "consolidate.epx?curl=" + url;//C5130562- Commemted this line as to avoid redirecting from https to http.
    var curl = "consolidate.epx?kNtBzmUK9zU=1&curl=" + encodeURIComponent(url);
    execute_url(curl, handler);
}

function execute_url(url, handler) {
    $.post(url,
           function (data) { if (handler) handler(data); },
           function (e) { }
          );
}
//End: Utility Functions
//**************************************************************************************************

