<!DOCTYPE html>
<html>
<script langagure="Javascript" src="./tripledes.js"></script>
<script>

  function openNewWindow(url, name, width, height, onlyContent, centerOnScreen) {
    var titleBarSize = 34;
    var borderSize = 4;
    var posX = 0;
    var posY = 0;
    if (centerOnScreen) {
      posX = parseInt((window.screen.availHeight - height - titleBarSize - borderSize) / 2);
      posY = parseInt((window.screen.availWidth - width - borderSize) / 2);
    }
    var param = "height=" + height + ", width=" + width;
    if (onlyContent) {
      param += ", toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no";
    }
    param += ", top=" + posX + ", left=" + posY;
    window.open(url, name, param);
  }

  function openModalWindow(url, name, width, height, onlyContent, centerOnScreen) {
    var param = "dialogHeight:" + height + "px; dialogWidth:" + width + "px";  
    if (centerOnScreen) {
      param += "; center=yes";
    }
    if (onlyContent) {
      param += "; toolbar=no; menubar=no; scroll=no; resizable=no; location=no; status=no";
    }
    param += ";";  
    window.showModalDialog(url, name, param);
  }  

  function encryptBy3DES(message, key) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var ivHex = CryptoJS.enc.Hex.parse('0123456789abcdef');        
    var encrypted = CryptoJS.TripleDES.encrypt(message, keyHex, 
      { iv: ivHex, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });

    return encrypted.toString();
  }

  function decryptBy3DES(ciphertext, key) {
      var keyHex = CryptoJS.enc.Utf8.parse(key);
    var ivHex = CryptoJS.enc.Hex.parse('0123456789abcdef');       
      var decrypted = CryptoJS.TripleDES.decrypt({
          ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
      }, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });

      return decrypted.toString(CryptoJS.enc.Utf8);
  }

  function openSDSViewer(useModal) {
    // modify Finacle URI parameters 
    var tellerid = 'TellerId';
    var usergroup = 'Normal';
    var bankcode = '016';
    var accttype = 'S';
    var acctno = '1234567890';    
    var ipaddr = '127.0.0.1';  
    var source = 'SOURCE';

    // static vars
    var sdsurl = 'http://localhost/web/interface.aspx?data=';
    var enckey = 'abcdefhigjkhal12345';    

    // construct DES encrypted URL
    var sdsuri = 'UserID='     + tellerid + 
        '&UserGroup='     + usergroup +        
        '&BankCode='     + bankcode +
        '&AccountType='   + accttype +
        '&AccountNo='     + acctno +    
        '&WorkstationIP='   + ipaddr +      
        '&SysDesc='     + source;

    sdsuri = encryptBy3DES(sdsuri, enckey);    // encrypt with 3DES
    sdsuri = encodeURIComponent(sdsuri);    // encode for http
    sdsurl += sdsuri;              // concat string

    // open new 1024x768 window
    if (useModal) {
      openModalWindow(sdsurl, '', 1015, 700, true, true);
    } else {
      openNewWindow(sdsurl, '', 1015, 700, true, true);
    }
  }

</script>

<body>
  <h1>Finacle System</h1><hr />
  <button onclick="openSDSViewer(true);">View Signature - Open SDS Modal Viewer</button><br />  
  <button onclick="openSDSViewer(false);">View Signature - Open SDS Window Viewer</button><br />
</body>
</html>