// ---------------------------------------------------------------------------
// String XOR encoding & decoding version 1.00
// Copyright (c) 1996, 1997 Tree Frog Software, all rights reserved.
// This code has been released to the public domain with no warranties nor
// guarantees.  You are welcome to use this code in any way you see fit.  Please
// keep the copyright and comments intact.
// Regards,
// Barrett Davis, http://www.thefrog.com
// Please send your comments to: barrettd@thefrog.com
// ---------------------------------------------------------------------------
// I have found it difficult to perform a simple XOR of two strings using
// JavaScript because 1. XOR operates on "numbers" not "characters" and
// 2. there does not seem to be a languages supplied method of converting between
// "numbers" and "characters."
// ---------------------------------------------------------------------------

// Note that we place %00 at the end of the escaped string.
// This "reversed" string make the aton() return non-traditional numbers for the
// character values, but at least it should return all of the 256 possible values.
// ---------------------------------------------------------------------------
var char_escaped="%FF%FE%FD%FC%FB%FA%F9%F8%F7%F6%F5%F4%F3%F2%F1%F0%EF%EE%ED%EC%EB%EA%E9%E8%E7%E6%E5%E4%E3%E2%E1%E0%DF%DE%DD%DC%DB%DA%D9%D8%D7%D6%D5%D4%D3%D2%D1%D0%CF%CE%CD%CC%CB%CA%C9%C8%C7%C6%C5%C4%C3%C2%C1%C0%BF%BE%BD%BC%BB%BA%B9%B8%B7%B6%B5%B4%B3%B2%B1%B0%AF%AE%AD%AC%AB%AA%A9%A8%A7%A6%A5%A4%A3%A2%A1%A0%9F%9E%9D%9C%9B%9A%99%98%97%96%95%94%93%92%91%90%8F%8E%8D%8C%8B%8A%89%88%87%86%85%84%83%82%81%80%7F%7E%7D%7C%7B%7A%79%78%77%76%75%74%73%72%71%70%6F%6E%6D%6C%6B%6A%69%68%67%66%65%64%63%62%61%60%5F%5E%5D%5C%5B%5A%59%58%57%56%55%54%53%52%51%50%4F%4E%4D%4C%4B%4A%49%48%47%46%45%44%43%42%41%40%3F%3E%3D%3C%3B%3A%39%38%37%36%35%34%33%32%31%30%2F%2E%2D%2C%2B%2A%29%28%27%26%25%24%23%22%21%20%1F%1E%1D%1C%1B%1A%19%18%17%16%15%14%13%12%11%10%0F%0E%0D%0C%0B%0A%09%08%07%06%05%04%03%02%01%00";
var char_all    = unescape( char_escaped );

function bound( min_val, value, max_val )
{  // Revision 1.00, becd.
   if( value < min_val ) {
      value = min_val;
   }
   if( value > max_val ) {
      value = max_val;
   }
   return value;
}

function aton( string, index )
{  // Convert a character to a number.  The range is 0x00 to 0xFF inclusive.
   // Revision 1.00, becd.
   index = bound( 0, index, string.length-1 );
   return char_all.indexOf( string.charAt( index ), 0 );
}

function ntoa( index )
{  // Convert a number to a character.  The range is 0x00 to 0xFF inclusive.
   // Revision 1.00, becd.
   index = bound( 0, index, 0xFF );
   return char_all.charAt( index );
}

function xorstr( data, pattern )
{  // Simple xor of a string with a pattern.
   // Revision 1.00, becd.
   var ii = 0;    // Data index.
   var jj = 0;    // Pattern index.
   var result = "";

   // If no pattern is supplied, then we use a simple pattern.
   if( pattern == null || pattern == "" || pattern.length <= 0 ) {
      pattern = "simple_xor_pattern";
   }
   // XOR every character in the data string with a character in the pattern string.
   for( ii = 0; ii < data.length; ii++ ) {
      if( jj >= pattern.length ) {
         jj = 0;
      }
      result += ntoa( aton( data, ii ) ^ aton( pattern, jj++ ));
   }
   return result;
}

function encode( data, pattern )
{  // Encode a string.  The result will be a well behaved escape()d string.
   // Note that the encoded string may be up to three times as long as the original.
   // Revision 1.00, becd.
   return escape( xorstr( data, pattern ));
}

function maj_cabinet (cabinet_saisi,nbcar) {
    var cinqzero = "0000000000";
    var numero_cabinet = "";
    if  ((cabinet_saisi.length < nbcar) && (cabinet_saisi.length > 0)) {
        numero_cabinet = cinqzero.substring(0,nbcar - cabinet_saisi.length) + cabinet_saisi.substring(0,cabinet_saisi.length);
    }
    else { numero_cabinet = cabinet_saisi; }
    return numero_cabinet;
}

function maj_login (login_saisi) {
    return login_saisi.toUpperCase();
}

function maj_code_entreprise (code_entreprise) {
    return code_entreprise.toUpperCase();
}

function doResponse(nbcar,fname) {
    var obj = eval("document."+fname);
    obj.userlogin.value  = maj_login(obj.userlogin.value);
    if (obj.code_entreprise)
        obj.code_entreprise.value = maj_code_entreprise(obj.code_entreprise.value, nbcar);
    var md5pwd = (obj.password) ? MD5(obj.password.value) : '';
    var str = obj.userlogin.value + ':'+ md5pwd + ':' + obj.seed.value;
    obj.kembo.value = MD5(str);
    if (obj.password) obj.password.value = '';
    return true;
}

function doChangeResponse(nbcar,fname) {
    obj = eval("document."+fname);
    obj.userlogin.value  = maj_login(obj.userlogin.value);
    stq = MD5(obj.password.value) + ':' +obj.seed.value;
    str = obj.userlogin.value + ':' + stq;
    obj.kembo.value = MD5(str);
    obj.kembo1.value = encode(obj.newpassword1.value, MD5(stq));
    obj.kembo2.value = encode(obj.newpassword2.value, MD5(stq));
    obj.password.value = '';
    obj.newpassword1.value = '';
    obj.newpassword2.value = '';
    return true;
}
