﻿// NOTE - With SSO now the URL has to match the site if SSO send people to the login page or else they will
// end up on the math login if you're going to http://demomml.intellipro.com/login_econ.htm
var locstr = location.hostname;
var locpath = location.pathname;
var force = getParam(location, "force");

if (window.location != window.parent.location) {
    window.location = "Info/Detect.aspx?cookies=3&redirect=" + window.location.search.substring(window.location.search.indexOf("&referrer=") + 10);
}
else if (!(force && force == "yes")) {
    if ((locstr.indexOf('financeworks') >= 0 || locstr.indexOf('myfinancelab') >= 0) && locpath.indexOf('login_finance') < 0)
        location.replace("login_finance.htm" + location.search);
    else if (locstr.indexOf('myeconlab') >= 0 && locpath.indexOf('login_econ') < 0)
        location.replace("login_econ.htm" + location.search);
    else if (locstr.indexOf('myaccountinglab') >= 0 && locpath.indexOf('login_acct') < 0)
        location.replace("login_acct.htm" + location.search);
    else if (locstr.indexOf('mymathtest') >= 0 && locpath.indexOf('login_mmt') < 0 && locpath.indexOf('login_mpt') < 0)
        location.replace("login_mmt.htm" + location.search);
    else if ((locstr.indexOf('myplacement') >= 0 || locstr.indexOf('myreadinesstest') >= 0) && locpath.indexOf('login_mmt') < 0 && locpath.indexOf('login_mpt') < 0)
        location.replace("login_mpt.htm" + location.search);
    else if (locstr.indexOf('mg.mathxlforschool') >= 0 && locpath.indexOf('login_mgm') < 0)
        location.replace("login_mgm.htm" + location.search);
    else if (locstr.indexOf('mathxlforschool') >= 0 && locpath.indexOf('login_school') < 0 && locpath.indexOf('login_mgm') < 0)
        location.replace("login_school.htm" + location.search);
    else if ((locstr.indexOf('espanol') >= 0 || locstr.indexOf('spanishdemo') >= 0) && locpath.indexOf('login_espanol') < 0 && locpath.indexOf('login_brasil') < 0)
        location.replace("login_espanol.htm" + location.search);
    else if (locstr.indexOf('brasil') >= 0 && locpath.indexOf('login_brasil') < 0 && locpath.indexOf('login_espanol') < 0)
        location.replace("login_brasil.htm" + location.search);
    else if (locstr.indexOf('myomlab') >= 0 && locpath.indexOf('login_om') < 0)
        location.replace("login_om.htm" + location.search);
    else if (locstr.indexOf('resultsplusbooster') >= 0 && locpath.indexOf('login_maths') < 0)
        location.replace("login_maths.htm" + location.search);
    else if (locstr.indexOf('global.mymathlabglobal.com') >= 0 && locpath.indexOf('login_hedmaths') < 0)
        location.replace("login_hedmaths.htm" + location.search);
    else if (locstr.indexOf('italiano.mymathlabglobal.com') >= 0 && locpath.indexOf('login_hedital') < 0)
        location.replace("login_hedital.htm" + location.search);
    else if (locstr.indexOf('deutsch.mymathlabglobal.com') >= 0 && locpath.indexOf('login_de') < 0 && locpath.indexOf('home_deutsch') < 0)
        location.replace("home_deutsch.htm" + location.search);
    else if (locstr.indexOf('accuplacer') >= 0 && locpath.indexOf('login_accuplacer') < 0 && locpath.indexOf('home_accuplacer') < 0)
        location.replace("home_accuplacer.htm" + location.search);
    else if (locstr.indexOf('testschool') >= 0 && locpath.indexOf('login_mgms4') < 0)
        location.replace("login_mgms4.htm" + location.search);

    if (typeof (detectedLang) != 'undefined' && detectedLang != null) {
        location.replace(locpath.substring(0, locpath.indexOf(".")) + "." + detectedLang + ".htm" + location.search);
    }
}

var submitted = false;
var inputfrmname = '';
var backfrmname = 'backfrm';
var ignoreBrowserCheck=false;

var isFormFocused = false;

var ptEnabled = true;

window.onunload = function () { }; // Comment 14158

function setupSignIn(signinbutton)
{
    if (signinbutton)
    {
        var returnurl = getParam(location, "returnurl");
        if (returnurl && returnurl.length > 0 && returnurl != "/")
        {
            signinbutton.href += "?returnurl=" + encodeURIComponent(returnurl);
        }
    }
}

// Put the cursor in the first text field and select contents, if any
function initializeUI(f, e) {

  //inputfrmname = f;

  // set focus on username
  document[f][e].onfocus = function() {isFormFocused = true;}; // Comment 10188
  document[f][e].onblur = function() { isFormFocused = false; };
  
  document[f].password.onfocus = function() { isFormFocused = true; };
  document[f].password.onblur = function() { isFormFocused = false; };

  if (document.activeElement != document[f][e] && document.activeElement != document[f].password  && location.search.indexOf('noinit')<0) {
    document[f][e].focus(); 
    document[f][e].select();
  }

  document.onkeydown = keyFilter;
    
  //document[inputfrmname].onsubmit = function() { return loginDetect(); };
  //document[inputfrmname].password.onkeydown = disableEnterKey; - may not kick in if you type your password too quickly
  //document[inputfrmname].password.onkeydown = keyFilter;
}

function keyFilter(evt) 
{
  var keycode
  if (window.event) 
  {
    keycode = window.event.keyCode
  }
  else if (evt) 
  {
    keycode = evt.which
  } 
  else 
  {
    return true
  }
  if (isFormFocused && keycode == 13 && !submitted) // enter key
  {
    //submitted = true;
    //document.forms[0].submit();   
    
    loginGo();
    return false;
  }
  else
    return true;
}

function disableEnterKey(e) {
    var key;
    if (window.event)
        key = window.event.keyCode; //IE
    else
        key = e.which; //firefox     

    return (key != 13);
}

function getLoginFormName() {
    if (inputfrmname == '') {
        if (typeof (document['inputfrm']) != 'undefined')
            inputfrmname = 'inputfrm';
        else if (typeof (document['loginfrm']) != 'undefined')
            inputfrmname = 'loginfrm';
    }
    return inputfrmname;
}

function doLoginSetup() {
    getLoginFormName();
    
    var sso = getParam(location, "sso");
    var returnurl = getParam(location, "returnurl");
    var product = getParam(location, "product");
    var authsystem = getParam(location, "authsystem");
    var bp = getParam(location, "bypass");
    var anybrowser = getParam(location, "browser");
    var courseid = getParam(location, "courseid");
    var cultre = getParam(location, "cultre");
    var se = (location.href.indexOf("https") == 0) ? "yes" : null;

    if (anybrowser == "1") {
        ignoreBrowserCheck = true;
    }

    if (bp && bp == "yes") {
        var bpel = document.createElement("input");
        bpel.type = "hidden";
        bpel.name = "bypass";
        bpel.value = bp;
        document[backfrmname].appendChild(bpel);
        document[backfrmname].sso.value = "no";
    }

    // SSO Heartbeat must have failed so don't try SSO Login
    if (sso && sso == "no")
        document[backfrmname].sso.value = sso;

    if (returnurl)
        document[backfrmname].returnurl.value = returnurl;

    if (product)
        document[backfrmname].product.value = product;

    if (authsystem)
        document[backfrmname].authsystem.value = authsystem;

    if (cultre && document[f].cultre)
        document[backfrmname].cultre.value = cultre;

    if (courseid)
        document[backfrmname].courseid.value = courseid;
    else
        document[backfrmname].courseid.value = -1;

    if (se)
        document[backfrmname].se.value = se;
}

function loginDetect() {
    doLoginSetup();
	var back="../checklogin.aspx" + location.search;
	var uname=document[inputfrmname].username.value;
	var pword = document[inputfrmname].password.value;
	back=replaceParam(back, "username", uname);
	back=replaceParam(back, "password", pword);
	back = replaceParam(back, "sso", document[backfrmname].sso.value);
	back = replaceParam(back, "product", document[backfrmname].product.value);
	back = replaceParam(back, "authsystem", document[backfrmname].authsystem.value);
	back = replaceParam(back, "sso", document[backfrmname].sso.value);
	back = replaceParam(back, "returnurl", document[backfrmname].returnurl.value);
	if (document[backfrmname].cultre)
	    back = replaceParam(back, "cultre", document[backfrmname].cultre.value);

	document[backfrmname].username.value = document[inputfrmname].username.value;

	if (ptEnabled && document[backfrmname].pt && document[backfrmname].sso && document[backfrmname].sso.value == "yes") {
	    document[backfrmname].pt.value = 1;
	    document[backfrmname].password.value = hex_md5(document[inputfrmname].password.value.toLowerCase());
	    back = replaceParam(back, "password", document[backfrmname].password.value);
	    back = replaceParam(back, "pt", "1");
	}
	else
	    document[backfrmname].password.value = document[inputfrmname].password.value;
	
	back = escape(back);
    
    var allowBrowser=true;
    
    if (!ignoreBrowserCheck)
	    allowBrowser=TestBrowser();
	    
	var allowCookies=TestCookies();
	
	if ((!allowCookies) || (!allowBrowser)) {
	    location = "info/detect.aspx?detecttype=4&redirect=" + back + "&lf=" + escape(locpath); // Comment 14416
		return false;
	}
	else
		return true;
}

function inputSubmit() 
{
    getLoginFormName(); 
    document[inputfrmname].onsubmit = function() { };
    loginGo();
}

// This was auto-submitting on hitting enter due to form construction instead of going through on keypress
// this will take care of submitting only one in either case
function loginGo() {
	if (loginDetect())
	{
	  if (!submitted) {
        submitted = true;
        document.forms[backfrmname].submit();
	  }
	}  
  return false;
}



// RUMBA
function GoUniversalLogin(params) 
{
    var hostname = location.hostname;
    var protocol = location.protocol;
    var pathname = location.pathname;
    var port = location.port;

    if (pathname.toLowerCase().indexOf('.htm') > -1) // pathname contains page
    {
        pathname = pathname.substring(0, pathname.lastIndexOf('/')+1);
    }

    var cancelUrl = protocol + "//" + hostname + port + pathname;
    var serviceUrl = protocol + "//" + hostname + port + pathname + "checklogin.aspx?product=" + escape(params.Product) + "&authsystem=" + escape(params.AuthSystem) + "&cultre=" + escape(params.Language);

    var courseid = getParam(location, "courseid");
    if (courseid)
        serviceUrl+= "&courseid=" + escape(courseid);

    var returnurl = getParam(location, "returnurl");
    if (returnurl)
        serviceUrl += "&returnurl=" + escape(returnurl);

    location.href = params.AuthUrl + "?lang=" + escape(params.Language) + "&profile=" + escape(params.Profile) + "&context=" + escape(params.Context) + "&service=" + escape(serviceUrl) + "&cancel=" + escape(cancelUrl) + "&renew=true";
}

