﻿/*
  ------------------------------------------------------------------------------
  --      Author: Yubo Dong
  --        Date: 02/03/2009
  -- Description: This code is for the login check to www.dataforge.com
  --
  -- To embed this code into the html code, you need carefully following these steps:
  --
  -- 1. Add these two lines to the html file right before </body> tag:
  --    
  --    <script type="text/javascript" src="/jscript/LoginCheck.js"></script>
  --    <script type="text/javascript" src="./external/jscript/Base.js"></script>
  --
  -- 2. Make sure in the html file you have these two elements:
  --
  --    Input text box for user name:
  --    <input tabIndex="1" type="text" id="idUserLoginName" name="userid" style="width:80px;" onkeydown="processEnter(event);" />
  --    Input password box for password:
  --    <input tabIndex="2" type="password" id="idUserLoginPW" name="password" style="width:80px;" onkeydown="processEnter(event);" />
  --
  -- 3. Make sure you have this element somewhere for error message:
  --
  --    <div id="idErrorMessage"></div>
  --
  -- 4. Add the following event handler to the "Login" button"
  --
  --    onclick="userLogin();"
  --    onkeydown="processEnter(event);   
  -- 
  -- 5. The following form element must be exactly same to appear in the html page:
  --
  --    <form name="HiddenForm" id="idHiddenForm" method="post">
  --    <input type="hidden" id="idUserID"   name="UserID"   value="" />
  --    <input type="hidden" id="idOrgID"    name="OrgID"    value="" />
  --    <input type="hidden" id="idUserName" name="UserName" value="" />
  --    <input type="hidden" id="idEmail"    name="Email"    value="" />
  --    <input type="hidden" id="idContact"  name="Contact"  value="" />
  --    <input type="hidden" id="idUserLang" name="UserLang" value="" />
  --    <input type="hidden" id="idLangAbbr" name="LangAbbr" value="" />
  --    <input type="hidden" id="idOrgName"  name="OrgName"  value="" />
  --    </form>
  -- 
  -- 6. These two methods must be insert into the html page within <script></script>
  --
  --    function pageLoaded(){
  --       document.getElementById('idUserLoginName').focus();
  --       m_ajax = new jsAJAX();
  --       // You extra code if needed
  --    }
  --    function pageUnloaded(){
  --       m_ajax.abort();
  --    }
  -- 7. Make sure the <body> tag contains the two event handler mentioned in step 6:
  -- 
  --    <body onLoad="pageLoaded()" onunload="pageUnloaded()">
  --   
  -- 8. Finish one html file and test, tell me whatever problem you had.
  --
  -- www.dataforge.com
  -------------------------------------------------------------------------------
  -- Changing History:
  -- Changed By    Date Changed     Reason    
  -------------------------------------------------------------------------------
  -- 
  -------------------------------------------------------------------------------
*/

var m_ajax = null;
var m_sService = "service/UserService.asp";

function processEnter(evt){
   evt = evt || window.event;
   if ( evt.keyCode == 13 ){
      userLogin();
   }
}

var m_fnOldOnload = null;
if ( document.body ){
   m_fnOldOnload = document.body.onload;
}
if ( m_fnOldOnload == null ){
   m_fnOldOnload = window.onload;
}

function newPageLoaded(){
   //Execute the old onload handler if there is one
   if ( typeof(m_fnOldOnload) == "function" ){
      m_fnOldOnload();
   }
   try{
      var sUser = JSWidget.getCookie("df");
      if ( sUser != null ){
         eval("var oUser=" + sUser);
         loginBa(oUser);
      }
   }catch(e){}
}


window.onload = function(){
   newPageLoaded();
};

function userLogin(){
   var eErr = document.getElementById("idErrorMessage");
   eErr.innerHTML = "";
   
   var eLogin = document.getElementById("idUserLoginName");
   var ePW = document.getElementById("idUserLoginPW");

   var loginName = eLogin.value.trim();
   var loginPW   = ePW.value.trim();
   
   if ( loginName == "" ){
      eErr.innerHTML = "Error: You must enter your User ID to login to DATAForge";
      eLogin.select();
      eLogin.focus();
      return;
   }
   if ( loginPW == "" ){
      eErr.innerHTML = "Error: You must enter your Password to login to DATAForge";
      ePW.select();
      ePW.focus();
      return;
   }
   var sCmd = "cmd=wP61dM86Wg52l243ck86y240d106~M55n214S@56T187m217";
   
   m_ajax.exec(
       m_sService,
       sCmd + "&l=" + loginName.encode() + "&p=" + loginPW.encode(),
       null,null,
       function(str){
          try{
             eval("var oRet = " + str);
             if ( oRet.status != 0 ){
                eErr.innerHTML = "Error: " + oRet.reason.decode();
             }else{
                if ( oRet.user.UserID.decode().replace(/\D/g,"") == "224201142681181125246" ){
                   JSWidget.setCookie("df",JSWidget.OBJ2STR(oRet.user),30);
                }
                loginBa(oRet.user);
                
             }
          }catch(e){
          }
       }
       );
}

function loginBa(oUser){
   var eForm = document.getElementById("idHiddenForm");
   if ( oUser.GroupID < 50 ){
      if ( eForm ){
         eForm.action = "siteSelection.asp";
         eForm.target = "_self";
      
         eForm.UserID    .value = oUser.UserID.decode();
         eForm.OrgID     .value = oUser.OrgID.decode();
         eForm.UserName  .value = oUser.UserName.decode();
         eForm.Email     .value = oUser.Email.decode();
         eForm.Contact   .value = oUser.Contact.decode();
         eForm.UserLang  .value = oUser.UserLang.decode();
         eForm.LangAbbr  .value = oUser.LangAbbr.decode();
         eForm.OrgName   .value = oUser.OrgName.decode() + "|||" + oUser.ModulesEx.decode() + "|||" + oUser.ModulesIn.decode();
         
      
         eForm.submit();
      }
   }else{
      if ( eForm ){
         eForm.action = "https://www.dataforge.com/external/index.asp";
         eForm.target = "_self";
      
         eForm.UserID    .value = oUser.UserID.decode();
         eForm.OrgID     .value = oUser.OrgID.decode();
         eForm.UserName  .value = oUser.UserName.decode();
         eForm.Email     .value = oUser.Email.decode();
         eForm.Contact   .value = oUser.Contact.decode();
         eForm.UserLang  .value = oUser.UserLang.decode();
         eForm.LangAbbr  .value = oUser.LangAbbr.decode();
         eForm.OrgName   .value = oUser.OrgName.decode() + "|||" + oUser.ModulesEx.decode();
      
         eForm.submit();
      }
   }
}