//-----------------------------------------------------------------
// File: wclogin.js
// Version: v6.3.453.2PCI
// Date: 09/14/2009
// (c) Copyright 2005-2009 Santronics Software, Inc.
//-----------------------------------------------------------------

var EnableSavePassword     = true;
var EnableSignup           = true;
var EnableSecuredMode      = false;
var EnableForgotPassword   = true;
var EnableWcNavMode        = true;
var LoginPageTarget        = "_top";
var SystemHostName         = "";
var SiteLoginTitle         = "";
var RequireSecuredAuth     = false;  // 452.9a  - see \wc63\http-4529\public\wclogin.js
var RequireSecuredConnect  = false;  // 453.2a9

//--------------------------------------------------------------------------
// InitWcLoginOptions()
//--------------------------------------------------------------------------

var wcLoginOptionsDefault = {
   BoxColor:       { secured: "#FFFFCC", normal: "#CEE0CE"},
   BoxClass:       { enable: true,   secured: "boxSecured", normal: "boxNormal"},
   SplashWindow:   { enable: false,  src: "/public/graphics/wcLogoTop.bmp"},
   WebDomains:     { enable: true,   nonsecured: "", secured: ""},
   ErrorWindow:    { enable: false },
   PCI:            { enable: false, fmethod: "POST" },
   LockIP:         { enable: false },
   reserved: null
};

function element(o) { return document.getElementById(o); }
function GetWcLoginCSS() { return element("wcLoginCSS"); }

function InitWcLoginOptions()
{
  DeleteCookie("wcpci");
  DeleteCookie("wcauth");
  if (typeof(wcLoginOptions) == "undefined") {
     wcLoginOptions = wcLoginOptionsDefault;
     var css = GetWcLoginCSS();
     if (!css || (css.sheet && !css.sheet.cssRules.length)) wcLoginOptions.BoxClass.enable = false;
  }
}

function wcMergeLoginOptions(o2)
{
  var o1 = wcLoginOptions;
  if (typeof o2 == "object") for (var k in o2){o1[k] = o2[k];}
  return o1;
}

function CheckWcLoginStyles()
{
  var css = GetWcLoginCSS();
  if (!css || (css.sheet && !css.sheet.cssRules.length))
  {
     document.writeln('<style>');
     document.writeln('.btnGrey    { background-color:#ccc; border-width:1px; border-style:solid; border-color:#000000; font:bold 10.5px Verdana,Arial,Helvetica,sans-serif;cursor: pointer; }');
     document.writeln('.btnGreyOn  { background-color:#666; border-width:1px; border-style:solid; border-color:#000000; color:#fff; font:bold 10.5px Verdana,Arial,Helvetica,sans-serif;cursor: pointer; }');
     document.writeln('.btnGreen   { background-color:#9c6; border-width:1px; border-style:solid; border-color:#000000; font:bold 10.5px Verdana,Arial,Helvetica,sans-serif;cursor: pointer; }');
     document.writeln('.btnGreenOn { background-color:#693; border-width:1px; border-style:solid; border-color:#000000; color:#fff; font:bold 10.5px Verdana,Arial,Helvetica,sans-serif;cursor: pointer; }');
     document.writeln('</style>');
  }
}

function onSecureSwitch(o, ssl)
{
   if (ssl) {
      if (!confirm("Switch to secure mode?")) return false;
   } else {
      if (!confirm("Switch to unsecure mode?")) return false;
   }
   var sProto  = (ssl?"https":"http") + "://";
   var sDomain = document.domain;
   if (ssl) {
      if (typeof(wcLoginOptions) != "undefined" && wcLoginOptions.WebDomains.secured) {
         sDomain = wcLoginOptions.WebDomains.secured;
      }
   } else {
      if (typeof(wcLoginOptions) != "undefined" && wcLoginOptions.WebDomains.nonsecured) {
         sDomain = wcLoginOptions.WebDomains.nonsecured;
      }
   }
   var href = sProto+sDomain+document.location.pathname+document.location.search;
   // opera has a problem when switching back and forth the 2nd time, neither method
   // below fixes it.  All other browser work fine.
   document.location = href;
   //window.location.assign(href);
   return true;
}

function doSyncAjax(url)
{
   if (document.getElementById) {
      var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
   }
   if (x) {
      x.open("GET", url, false);
      x.send(null);
      if (x.status != 200)    {
         return false;
      }
      return true;
   }
}

function SetEntryStatus(msg)
{
   el = element("divInputError");
   if (el) {
      el.innerHTML = msg;
      el.style.display  = "";
      return;
   }
   if (msg != "" && msg != "&nbsp;") alert(msg);
}

function CancelBubble(e)
{
  e.cancelBubble = true;
  return true;
}

function doClearStatus(e)
{
  CancelBubble(e);
  SetEntryStatus("&nbsp;");
  return true;
}

function doWcAlert(s)
{
   SetEntryStatus(s);
}

//**************************************************************************
//**************************************************************************

function doWcValidate()
{
   var uid = document.entry.username.value;
   var pwd = document.entry.password.value;
   if (uid == "") {
      SetEntryStatus("Please enter your login user name");
      document.entry.username.focus();
      return false;
   }
   if (pwd == "") {
      SetEntryStatus("Please enter your password!");
      document.entry.password.focus();
      return false;
   }
   return true;
}

function getLoginMode()
{
   var mode = document.login.mode.value;
   if (mode == "") mode = "html";
   var frm = document.entry;
   if (frm.mode) {
      if (frm.mode.type == "checkbox") {
          return frm.mode.checked?"client":"html";
      }
      if ((frm.mode.length == 2) && (frm.mode[0].type == "radio")) {
         mode = frm.mode[0].checked?frm.mode[0].value:frm.mode[1].value;
      }
   }
   return mode;
}

function getLockIPMode()
{
   if (!document.login.lockip) return "";
   var lock = document.login.lockip.value;
   if (lock == "") lock = "";
   var frm = document.entry;
   if (frm.lockip) {
      if (frm.lockip.type == "checkbox") {
          return frm.lockip.checked?"1":"";
      }
   }
   return lock;
}

function doWcGenerateUserHash(uid, pwd, pcase)
{
   if (typeof(uid) == "undefined") {
      uid   = document.entry.username.value;
      pwd   = document.entry.password.value;
      pcase = false;
   }
   // calculate the hash
   if (!pcase && pcase == 0) pwd = pwd.toLowerCase();
   var hash = "";
   if (wcLoginOptions.PCI.enable) {
      var wcsid = GetCookie("wcsid");
      DeleteCookie("wcsid");
      hash = uid+":"+hex_md5(wcsid+":"+uid+":"+hex_md5(pwd));
   } else {
      hash = uid+":"+hex_md5(uid+":"+hex_md5(pwd));
   }
   return hash;
}

function doWcLoginAction(pcase)
{
   if (doWcValidate())  {
      var pwd  = document.entry.password.value;
      var uid  = document.entry.username.value;
      var hash = doWcGenerateUserHash(uid,pwd,pcase)

      var cname = "wcauth";
      if (wcLoginOptions.PCI.enable) cname = "wcpci";

      SetCookie(cname, hash, 0, "/", 0, document.location.protocol == "https:");

      if (document.login.mode)   document.login.mode.value   = getLoginMode();
      if (document.login.lockip) document.login.lockip.value = getLockIPMode();
      document.login.submit();
      return true;
   }
   return false;
}

//**************************************************************************
//**************************************************************************

function PrintCookieWarning()
{
   document.write("<center>");
   document.write("<iframe name='nocookie' src='/public/warning-no-cookie.wct' frameborder=0 width=100% scrolling=NO>");
   document.write("<font color='red'>No cookie support.</font>");
   document.write("</iframe>");
   document.write("</center>");
}

//--------------------------------------------------------------------------
// PrintLoginForm()
//
// This is the default cookie-based login form displayed when logic.wct
// is called and there is no macro Login.CustomForm defining a custom
// "printloginform-xxx.wct" file.
//
//--------------------------------------------------------------------------

function PrintLoginForm(title, defUserName, defCase)
{
  if (typeof(title) == "undefined") {
      title       = "Site Login: "+SystemHostName;
      if (SystemHostName == "") title = "Member Login:";
      defUserName = "";
      defCase     = false;
  }

  InitWcLoginOptions();
  CheckWcLoginStyles();

  var uname = defUserName?defUserName:"";
  var pcase = defCase?defCase:0;

  var formtag = "";
  formtag += '<form name="entry" id="formEntry" ';
  if (EnableSavePassword) {
     formtag += 'autocomplete="on" ';
     formtag += 'onSubmit="return doWcValidate();" ';
     formtag += 'action="javascript:doWcLoginAction('+pcase+');" ';
  } else {
     formtag += 'autocomplete="off" ';
     formtag += 'onSubmit="doWcLoginAction('+pcase+'); return false;"';
  }
  formtag += '>';
  document.writeln(formtag);

  document.writeln('<div id="loginWindow" align="center">');

  var secured = document.location.protocol == "https:"
  var attrs   = ' border="0" cellspacing="1" cellpadding="1"';

  if (wcLoginOptions.BoxClass.enable) {
    attrs += ' class="' + (secured?wcLoginOptions.BoxClass.secured:wcLoginOptions.BoxClass.normal) + '"';
  } else {
    var bc = secured?wcLoginOptions.BoxColor.secured:wcLoginOptions.BoxColor.normal;
    attrs += ' style="';
    attrs += 'border: 2px solid black; ';
    attrs += 'background-color:'+bc+';';
    attrs += '"';
  }

  document.writeln('<table id="loginTable"'+attrs+'>');
  document.writeln('<thead id="loginTitleBar">');
  document.writeln('<tr><td colspan="2" align="left"><b>'+title+'</b></td><td align="right"><a href="/" target="_top" class="btnGrey" onmouseover="this.className=\'btnGreyOn\'" onmouseout="this.className=\'btnGrey\'" >&nbsp;home&nbsp;</a></td></tr>');
  document.writeln('</thead>');

  if (wcLoginOptions.SplashWindow.enable) {
     document.writeln('<tr>');
     document.writeln('<td colspan="3" align="center">');
     document.writeln('<div id="SplashWindow" class="SplashWindow">');
     document.writeln('<img id="SplashImage" class="SplashWindow" src="'+wcLoginOptions.SplashWindow.src+'" alt=""/>');
     document.writeln('</div');
     document.writeln('</td>');
     document.writeln('</tr>');
  }

  var blur = "";
  if (wcLoginOptions.ErrorWindow.enable) {
     document.writeln('<tr id="divInputErrorRow">');
     document.writeln('<td colspan="3" align="center"><div id="divInputError" class="InputError"><br></div></td>');
     document.writeln('</tr>');
     blur = " onBlur='doClearStatus(event);'";
  }

  document.writeln('<tr valign=top align=left>');
  document.writeln(' <td id="loginUserPrm">&nbsp;Username:&nbsp;</td>');
  document.writeln(' <td><input type="text" name="username" id="username" value="'+uname+'" size=24 maxlength="32"'+blur+'/></td>');
  document.writeln(' <td>&nbsp;</td>');
  document.writeln('</tr>');

  document.writeln('<tr valign=top align=left>');
  document.writeln(' <td id="loginPassPrm">&nbsp;Password:&nbsp;</td>');
  document.writeln(' <td><input type="password" name="password" id="password" value="" size=24 maxlength="32"'+blur+'/></td>');
  document.writeln(' <td><input type="submit"  value="&nbsp;Login&nbsp; &raquo;" class="btnGreen" onmouseover="this.className=\'btnGreenOn\'" onmouseout="this.className=\'btnGreen\'"></td>');
  document.writeln('</tr>');

  if (wcLoginOptions.LockIP.enable) {
     document.writeln('<tr>');
     document.write('<td colspan=3 align="left">');
     document.writeln('<input type="checkbox" name="lockip"/>&nbsp;Lock Workstation IP Address');
     document.write('</td>');
     document.writeln('</tr>');
  }

  var args = document.location.search;
  var webmode = "checked";
  var navmode = "";
  if (args.toLowerCase().match("mode=client") || (GetCookie("mode") == "client")) {
     webmode = "";
     navmode = "checked";
  }

  if (EnableWcNavMode) {
     document.writeln('<tr>');
     document.writeln(' <td valign="top">Mode:</td>');
     document.writeln(' <td><input type="radio" name="mode" value="html" '+webmode+'>&nbsp;Use Web Browser<br>');
     document.writeln('     <input type="radio" name="mode" value="client" '+navmode+'>&nbsp;Use WcNavigator&nbsp;</td>');
     document.writeln(' <td align=right> </td>');
     document.writeln('</tr>');
  }

  if (EnableSecuredMode && wcLoginOptions.WebDomains.enable) {
     document.write('<tr>');
     document.write('<td colspan=3 align="center">');
     if (RequireSecuredConnect && document.location.protocol == "https:") {
        var sTxt    = "This is a SECURED Link";
        var sImage  = "lock.gif";
        document.write('<img src="/public/graphics/'+sImage+'" border="0">&nbsp;<b>'+sTxt+'</b>');
     } else {
        var sProto  = "http://";
        var sTxt    = "Switch to Unsecured Mode";
        var sImage  = "unlock.gif";
        var sDomain = document.domain;
        if (document.location.protocol == "http:") {
           sProto = "https://";
           sTxt   = "Switch to Secured Mode";
           sImage = "lock.gif";
           if (wcLoginOptions.WebDomains.secured) {
              sDomain = wcLoginOptions.WebDomains.secured;
           }
        } else {
           if (wcLoginOptions.WebDomains.nonsecured) {
              sDomain = wcLoginOptions.WebDomains.nonsecured;
           }
        }
        var href = sProto+sDomain+document.location.pathname+document.location.search;
        document.write('<a href="'+href+'">');
        document.write('<img src="/public/graphics/'+sImage+'" border="0">&nbsp;<b>'+sTxt+'</b>');
        document.write('</a>');
     }
     document.write('</td>');
     document.writeln('</tr>');
  }

  if (EnableForgotPassword) {
     document.write('<tr>');
     document.write('<td colspan=3 align="center">');
     document.write('<a href="/public/forgotpassword.wct">Forgot your password?</a>');
     document.write('</td>');
     document.writeln('</tr>');
  }
  if (EnableSignup) {
     document.write('<tr>');
     document.write('<td colspan=3 align="center">');
     document.write('<a href="/Signup">Not registered? Create new account</a>');
     document.write('</td>');
     document.writeln('</tr>');
  }

  document.writeln('</table>');
  document.writeln('</div>');
  document.writeln('</form>');

  if (typeof(document.login) == "undefined") {
     var fmethod = "get";
     if (wcLoginOptions.PCI.fmethod) fmethod = wcLoginOptions.PCI.fmethod;
     document.writeln('<form name="login" action="/login" method="'+fmethod+'" target="'+LoginPageTarget+'">');
     document.writeln('<input type="hidden" name="js"  value="1">');
     document.writeln('<input type="hidden" name="mode"  value="">');
     document.writeln('<input type="hidden" name="lockip"  value="">');
     document.writeln('</form>');
  }

  document.entry.username.focus();
}


