var clientId = null;

// Set things up upon page load
$(document).ready(function(){

// MaintenanceMsg();
  loadAttributes();
  bindEvents();
  checkToken();

});

// Attributes to remove unobtrusive when JavaScript active
function loadAttributes(){
$("#ForgotLink").attr("href","javascript:void(0);" );
$("#ThisForm").attr("action","javascript:void(0);" );
}

// Methods
function bindEvents() {

$("#ForgotLink").click(function () { $("#LoginBox").fadeOut(function () {$("#ForgotContent").fadeIn();});});
$(".loginLink").click(function () { $("#ForgotContent").fadeOut(function () {$("#LoginBox").fadeIn();});});
$(".loginLink2").click(function () { $("#PasswordSent").fadeOut(function () {$("#LoginBox").fadeIn();});});
$(".loginLink3").click(function () { $("#EmailError").fadeOut(function () {$("#LoginBox").fadeIn();});});
$("#LoginErrorLink").click(function () { $("#LoginError").fadeOut(function () {$("#LoginBox").fadeIn();});});
$("#LoginErrorLink2").click(function () { $("#LockOut").fadeOut(function () {$("#LoginBox").fadeIn();});});
$("#LoginErrorLink3").click(function () { $("#NoConnection").fadeOut(function () {$("#LoginBox").fadeIn();});});
$("#LoginErrorLink4").click(function () { $("#ForgotContent").fadeOut(function () {$("#LoginBox").fadeIn();});});
$("#Submit").click(function () { StartLogin(); });
$("#PasswordSubmit").click(function () { PasswordReminder(); });
$("#Password").keypress(function(e) { if (e.which == 13) StartLogin(); } );

}

function showHtml(html) {
  alert(html);
}

function StartLogin(){
var validationError=0;
if($("#Username").attr("value")==null){$("#UsernameValidation").slideDown();validationError=1;}else{$("#UsernameValidation").slideUp();}
if($("#Password").attr("value")==null){$("#PasswordValidation").slideDown();validationError=1;}else{$("#PasswordValidation").slideUp();}
if(validationError>0){return false;}
var urlStr = window.location.protocol + "//" + window.location.hostname + "/msi/servlet/login";
var currentUrl = window.location.toString();
var chkKeep=$("input:checked").length;
$("#LoginBox").fadeOut(function () {$("#Throbber").fadeIn(function () {
 $loginId=$("#Username").val();
 $pwd=$("#Password").val();
 if (chkKeep > 0)
   $remMe=true;
 else
   $remMe=false;
 if (currentUrl.indexOf("twa=true") > 0)
  $.post(urlStr,{id:$loginId,pw:$pwd,twa:true}, function(data) {  checkLoginVal(data); });
 else
   $.post(urlStr,{id:$loginId,pw:$pwd,keepMe:$remMe}, function(data) {  checkLoginVal(data); });
});});
}

function LoginError(){
$("#Throbber").fadeOut(function () {$("#LoginError").fadeIn(); });
}
function NoConnection(){
$("#Throbber").fadeOut(function () {$("#NoConnection").fadeIn(); });
}
function LockOut(){
$("#Throbber").fadeOut(function () {$("#LoginOut").fadeIn(); });
}

function checkLoginVal(data){
  if (data.indexOf("badAccess") > -1)
    LoginError();
  else if (data.indexOf("noConnection") > -1)
    NoConnection();
  else if (data.indexOf("lockOut") > -1)
    LockOut();
  else
    window.location.href="https://" + window.location.hostname + data;
}


function PasswordReminder(){
var validationError=0;
if($("#Email").attr("value")==null){$("#EmailValidation").slideDown();validationError=1;}else{$("#EmailValidation").slideUp();}
if(validationError>0){return false;}

/*
$("#ForgotContent").fadeOut(function () {$("#Throbber").fadeIn(function () {

  $.ajax({type:"GET", url:"ajaxTest.htm", timeout:10000,
            success: function(text) {
              setTimeout("PasswordSent()",1000);
            },
            error: function() {
             setTimeout("PasswordError()",1000);
            }
          });

});});
*/
}

function PasswordSent(){

$("#Throbber").fadeOut(function () {$("#PasswordSent").fadeIn(); });
}

function PasswordError(){

$("#Throbber").fadeOut(function () {$("#EmailError").fadeIn(); });
}

function MaintenanceMsg(){
$("#LoginBox").fadeOut(function () {$("#MaintenanceContent").fadeIn();});
}

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );

  if ( secure )
        cookie_string += "; secure";

  document.cookie = cookie_string;
}
function saveClientId() {
  clientId = $("#Username").val();
  set_cookie("clientId", clientId, 2100, 12, 31);

}
function getClientId() {
  var searchStr = "clientId=";

  if ( document.cookie.length > 0 )
  {
    offset = document.cookie.indexOf(searchStr);
    if (offset != -1)
    {
      offset += searchStr.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1)
        end = document.cookie.length;
      clientId = unescape(document.cookie.substring(offset, end));
      if ((clientId != null) && (clientId.length > 0)) {
        $("#Username").val(clientId);
        $("#Password").focus();
      }
    }
  }
}
function getCookieVal(offset) {
  var endstr = document.cookie.indexOf(";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset,endstr));
}
function getCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i,j) == arg)
      return getCookieVal(j);
    i = document.cookie.indexOf(" ",i)+1;
    if (i == 0) break;
  }
  return "";
}
function checkToken() {
  cookieVal = getCookie("TTATOKEN");
  if ((cookieVal != null) && (cookieVal.length > 0)) {
    hName = window.location.hostname;
    if (hName == "localhost") {
      hName += ":8080";
    	window.location.href="http://" + hName + "/msi/intro.jsp";
    } else {
    	window.location.href="https://" + hName + "/msi/intro.jsp";
   	}
  }
}

