/***************************************************/
/* Operacje na kluczach - ComArchForm	ver. 1.0   */
/* Autor: Mrowca & White  dla ComArch S.A.	   */
/***************************************************/
var text2sign;

function openKeyWindow(url, name, w, h, perc) 
{        
	var winX = (screen.availWidth - w)*perc*.01;        
	var winY = (screen.availHeight - h)*perc*.01;        
	popupWin = window.open(url, name,'scrollbars=auto,resizable=no,width=' + w + ',height=' + h + ',left=' + winX + ',top=' + winY);
	popupWin.focus ();
}

function podpisz (Text, OrdNo, OrdId, MarketId) 
{
	/* otwieramy okno do wpisania hasla do klucza */
	url = "keypasswd?Text=" +  Text + "&OrdNo=" + OrdNo + "&OrdId=" + OrdId + "&MarketId=" + MarketId;
	//alert(url);
	openKeyWindow(url, "passwd", 450, 350, 50);
}

function ReturnSignedText(text, OrdNo, OrdId, MarketId)
{
	// pobranie tekstu do podpisania i kontrolki
	//alert(top.opener.name);
	mainform = top.opener.document.forms.main;
	if(!top.opener.parent.parent.signer) {
		signtool = top.opener.parent.opener.parent.parent.signer.document.sign;
	} else {
		signtool = top.opener.parent.parent.signer.document.sign;
	}
	if (top.opener.document.forms.act) {
		SignedText = signtool.SignText(text, document.forms[0].haslo.value);
		cancelForm = top.opener.document.forms.act;
		address = "anuluj?ordno=" + OrdNo + "&ordid=" + OrdId + "&mid=" + MarketId + "&cp_podpis=" + SignedText + "&cp_tresc=" + text;
		top.opener.document.location=address;

	} else if (top.opener.document.forms.length==0) {
		SignedText = signtool.SignText(text, document.forms[0].haslo.value);
		address = "anuluj?ordno=" + OrdNo + "&ordid=" + OrdId + "&mid=" + MarketId + "&cp_podpis=" + SignedText + "&cp_tresc=" + text;
		top.opener.parent.opener.parent.strona.document.location=address;
	} else {
		mainform.cp_podpis.value = signtool.SignText(text, document.forms[0].haslo.value);
		mainform.cp_tresc.value = text;
		mainform.submit();
		if (mainform.portalnol) {
			top.opener.setTimeout('close();','1000');
		}

	}
	window.close();
}

/**
 * GetTextToSign - Funkcja sklada z prostego formularza zlecen tekst
 * ktory uzytkownik podpisuje swoim kluczem prywatnym
 */
function GetTextToOrderSignNCFIX2()
{
	var text = "ZLECENIE:";
	mianForm = document.forms.main;
	text += "papier:" 	+ mainForm.papier.options[mainForm.papier.selectedIndex].value + ",";
	text += "kierunek:" + mainForm.kierunek.options[mainForm.kierunek.selectedIndex].value + ",";
	text += "otp:" 		+ mainForm.tmp.options[mainForm.tmp.selectedIndex].value + ",";
	text += "ilosc:" 	+ mainForm.ilosc.value + ",";
	text += "czy_limit:" + mianForm.czy_limit.value + ",";
	text += "limit:" 	+ mainForm.limit.value + ",";
	text += "dataod:" 	+ mainForm.dataod.value + ",";
	text += "wazne:" 	+ mainForm.daty.options[mainForm.daty.selectedIndex].value + ",";
	text += "datado:" 	+ mainForm.datado.value + ",";
	text += "typ_ceny:" + mainForm.typ_ceny.options[mainForm.typ_ceny.selectedIndex].value + ",";
	text += "wuj:" 		+ mainForm.wuj.value + ",";
	text += "limitakt:" + mainForm.limakt.value + ",";
	text += "wmin:" 	+ mainForm.wmin.value;
	text2sign = text;
	// zapisanie danych do podpisu z poziomu ramki strona
	//top.parent.signer.document.texttosign = text;
}

/**
 * GetTextToSign - Funkcja sklada z prostego formularza zlecen tekst
 * ktory uzytkownik podpisuje swoim kluczem prywatnym
 */
function GetTextToOrderSignKT()
{
	var text = "ZLECENIE:";
	mianForm = document.forms.main;
	text += "portfel:" 	+ mainForm.portfel.options[mainForm.portfel.selectedIndex].value + ",";
	text += "papier:" 	+ mainForm.papier.options[mainForm.papier.selectedIndex].value + ",";
	text += "kierunek:" + mainForm.kierunek.options[mainForm.kierunek.selectedIndex].value + ",";
	text += "ilosc:" 	+ mainForm.ilosc.value + ",";
	text += "czy_limit:" + mianForm.czy_limit.value + ",";
	text += "limit:" 	+ mainForm.limit.value + ",";
	text += "dataod:" 	+ mainForm.dataod.value + ",";
	text += "wazne:" 	+ mainForm.daty.options[mainForm.daty.selectedIndex].value + ",";
	text += "datado:" 	+ mainForm.datado.value + ",";
	text += "typ_ceny:" + mainForm.typ_ceny.options[mainForm.typ_ceny.selectedIndex].value + ",";
	text += "wuj:" 		+ mainForm.wuj.value + ",";
	text += "limitakt:" + mainForm.limakt.value + ",";
	text += "wmin:" 	+ mainForm.wmin.value;
	text2sign = text;
	// zapisanie danych do podpisu z poziomu ramki strona
	//top.parent.signer.document.texttosign = text;
}

/**
 * GetTextToSign - Funkcja sklada z prostego formularza zlecen tekst
 * ktory uzytkownik podpisuje swoim kluczem prywatnym
 */
function GetTextToOrderSignCTO()
{
	var text = "ZLECENIE:";
	mianForm = document.forms.main;
	text += "papier:" 	+ mainForm.papier.options[mainForm.papier.selectedIndex].value + ",";
	text += "kierunek:" + mainForm.kierunek.options[mainForm.kierunek.selectedIndex].value + ",";
	text += "ilosc:" 	+ mainForm.ilosc.value + ",";
	text += "limit:" 	+ mainForm.limit.value + ",";
	text += "WAN:" 		+ mainForm.wan.value + ",";
	text += "dataod:" 	+ mainForm.dataod.value + ",";
	text += "datado:" 	+ mainForm.datado.value;
	text2sign = text;
	// zapisanie danych do podpisu z poziomu ramki strona
	//top.parent.signer.document.texttosign = text;
}
/**
 * GetTextToSign - Funkcja sklada z prostego formularza zlecen tekst
 * ktory uzytkownik podpisuje swoim kluczem prywatnym
 */
function GetTextToFastOrder()
{
	mianForm = document.forms.main;
	var text = "ZLECENIE WIELOKROTNE:";
	for ( var i=0; i<selOTPcount; i++ )
	{
		papier      = eval("mainForm.walor"    + (i+1) + "kod");
		kierunek	= eval("mainForm.kierunek" + (i+1));
		otp			= eval("mainForm.tmp" 	   + (i+1));
		portfel		= eval("mainForm.port" 	   + (i+1));
		pole_ilosci = eval("mainForm.ilosc"    + (i+1));
		pole_limitu = eval("mainForm.limit" + (i+1));
		dataod		= eval("mainForm.od" 	   + (i+1));
		wazne		= eval("mainForm.daty" 	   + (i+1));
		datado		= eval("mainForm.do" 	   + (i+1));
		
		if ( papier.value != '' )
		{
			text += "papier:" 	+ papier.value + ",";
			text += "kierunek:" + kierunek.options[kierunek.selectedIndex].value + ",";
			if (mianForm.mid.value == '6') 
			{
				text += "portfel:" 	+ portfel.options[portfel.selectedIndex].value + ",";
			}
			else
			{
				text += "otp:" + otp.options[otp.selectedIndex].value + ",";
			}
			text += "ilosc:" 	+ pole_ilosci.value + ",";
			text += "limit:" 	+ pole_limitu.value + ",";
			text += "dataod:" 	+ dataod.value + ",";
			text += "wazne:" 	+ wazne.options[wazne.selectedIndex].value + ",";
			text += "datado:" 	+ datado.value + ";";
		}
	}
	text2sign = text;
	// zapisanie danych do podpisu z poziomu ramki strona
	//top.parent.signer.document.texttosign = text;
}

/**
 * GetTextToSign - Funkcja sklada z prostego formularza zlecen tekst
 * ktory uzytkownik podpisuje swoim kluczem prywatnym
 */
function GetTextToFastOrderCTO()
{
	mianForm = document.forms.main;
	var text = "ZLECENIE WIELOKROTNE:";
	for ( var i=0; i<3; i++ )
	{
		papier      = eval("mainForm.walor"    + (i+1) + "kod");
		kierunek	= eval("mainForm.kierunek" + (i+1));
		pole_ilosci = eval("mainForm.ilosc"    + (i+1));
		pole_limitu = eval("mainForm.limit"    + (i+1));
		wan			= eval("mainForm.wan" 	   + (i+1));
		dataod		= eval("mainForm.od" 	   + (i+1));
		datado		= eval("mainForm.do" 	   + (i+1));

		if ( papier.value != '' )
		{
			text += "papier:" 	+ papier.value + ",";
			text += "kierunek:" + kierunek.options[kierunek.selectedIndex].value + ",";
			text += "ilosc:" 	+ pole_ilosci.value + ",";
			text += "limit:" 	+ pole_limitu.value + ",";
			text += "WAN:" 		+ wan.value + ",";
			text += "dataod:" 	+ dataod.value + ","; 
			text += "datado:" 	+ datado.value + ";";
		}
	}
	text2sign = text;
	// zapisanie danych do podpisu z poziomu ramki strona
	//top.parent.signer.document.texttosign = text;
}

/**
 * GetTextToSign - Funkcja sklada z prostego formularza zlecen tekst
 * ktory uzytkownik podpisuje swoim kluczem prywatnym
 */
function GetTextToNOLOrder()
{
	mianForm = document.forms.main;
	var text = "ZLECENIE NOL:";

	text += "papier:" 	+ mainForm.papier.value + ",";
	text += "kierunek:" + mainForm.kierunek.options[mainForm.kierunek.selectedIndex].value + ",";
	text += "otp:" 		+ mainForm.tmp.options[mainForm.tmp.selectedIndex].value + ",";
	text += "ilosc:" 	+ mainForm.ilosc.value + ",";
	text += "limit:" 	+ mainForm.limit.value + ",";
	text += "dataod:" 	+ mainForm.dataod.value + ",";
	text += "wazne:" 	+ mainForm.daty.options[mainForm.daty.selectedIndex].value + ",";
	text += "datado:" 	+ mainForm.datado.value + ",";
	
	text2sign = text;
	// zapisanie danych do podpisu z poziomu ramki strona
	//top.opener.parent.parent.signer.document.texttosign = text;
}

/**
 * GetTextToSign - Funkcja sklada z prostego formularza zlecen tekst
 * ktory uzytkownik podpisuje swoim kluczem prywatnym
 */
function GetTextToNOLOrderKT()
{
	mianForm = document.forms.main;
	var text = "ZLECENIE NOL:";
	
	text += "papier:" 	+ mainForm.papier.value + ",";
	text += "portfel:" 	+ mainForm.portfel.options[mainForm.portfel.selectedIndex].value + ",";
	text += "kierunek:" + mainForm.kierunek.options[mainForm.kierunek.selectedIndex].value + ",";
	text += "ilosc:" 	+ mainForm.ilosc.value + ",";
	text += "limit:" 	+ mainForm.limit.value + ",";
	text += "dataod:" 	+ mainForm.dataod.value + ",";
	text += "wazne:" 	+ mainForm.daty.options[mainForm.daty.selectedIndex].value + ",";
	text += "datado:" 	+ mainForm.datado.value + ",";

	text2sign = text;
	// zapisanie danych do podpisu z poziomu ramki strona
	//top.opener.parent.parent.signer.document.texttosign = text;
}

/**
 * GetTextToSign - Funkcja sklada z prostego formularza zlecen tekst
 * ktory uzytkownik podpisuje swoim kluczem prywatnym
 */
function GetTextToDDM()
{
	mianForm = document.forms.main;
	var text = "ZLECENIE DDM:";
	
	text += "papier:" + mainForm.papier.value + ",";
	if (mainForm.portfel)
	{
		text += "portfel:" + mainForm.portfel.options[mainForm.portfel.selectedIndex].value + ",";
	}
	else
	{
		text += "otp:" + mainForm.tmp.options[mainForm.tmp.selectedIndex].value + ",";
	}
	
	text += "kierunek:" + mainForm.kierunek.options[mainForm.kierunek.selectedIndex].value + ",";
	text += "ilosc:" 	+ mainForm.ilosc.value + ",";
	text += "limit:" 	+ mainForm.limit.value + ",";
	text += "cenau:" 	+ mainForm.cenau.value + ",";
	text += "dataod:" 	+ mainForm.dataod.value + ",";
	text += "datado:" 	+ mainForm.datado.value;
	
	text2sign = text;
	// zapisanie danych do podpisu z poziomu ramki strona
	//top.opener.parent.parent.signer.document.texttosign = text;
}

/**
 * GetTextToSign - Funkcja sklada z prostego formularza zlecen tekst
 * ktory uzytkownik podpisuje swoim kluczem prywatnym
 */
function GetTextToTransfer()
{
	mianForm = document.forms.main;
	var text = "PRZELEW:";
	
	text += "rorno:" + mianForm.rorno.value + ",";
	text += "bank:" 	+ mianForm.bank.value + ",";
	text += "owner:" + mianForm.owner.value + ",";
	text += "zl:" 	+ mianForm.zl.value + ",";
	text += "gr:" 	+ mianForm.gr.value + ",";
	text += "data:" 	+ mianForm.data.value + ",";
	text += "tytulem:" + mianForm.tytulem.value + ",";
	text += "own_name:" + mianForm.own_name.value + ",";
	text += "own_fname:" + mianForm.own_fname.value + ",";
	text += "rachunek:" + mianForm.rachunek.value;
	
	text2sign = text;
	//zapisanie danych do podpisu z poziomu ramki strona
	//top.parent.signer.document.texttosign = text;
}

function GetTextToBasket()
{
	mainForm = document.forms.main;
	var text = "ZLECENIE KOSZYK:";
	for ( var i=0; i<mainForm.oid.length; i++ )
	{
		pole_text = eval( "mainForm.otext" + mainForm.oid[i].value );
		text +=  pole_text.value + ";";
	}
	text2sign = text;
	// zapisanie danych do podpisu z poziomu ramki strona
	top.opener.parent.parent.signer.document.texttosign = text;
}

function Cancel(OrdNo, OrdID, OrdMarketID, OrdText) 
{
	if ( confirm('Czy na pewno chcesz anulować to zlecenie?') ) 
	{	
		if ( (isCard == 'T') && ( isCipher == 'T' ))
		{	
			podpisz(OrdText, OrdNo, OrdID, OrdMarketID);
		}
		else
		{
			return true;
		}
	}
	return false;
}

function CancelOutlook(OrdNo, OrdID, OrdMarketID, OrdText) 
{
	if ( confirm('Czy na pewno chcesz anulować to zlecenie?') ) 
	{	
		if ( (isCard == 'T') && ( isCipher == 'T' ))
		{
			podpisz(OrdText, OrdNo, OrdID, OrdMarketID);		
		} else {
			return true;
		}
	}
	return false;
}

/*
 * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
 * in FIPS PUB 180-1
 */

/*
 * Convert a 32-bit number to a hex string with ms-byte first
 */
var hex_chr = "0123456789abcdef";
function hex(num)
{
  var str = "";
  for(var j = 7; j >= 0; j--)
    str += hex_chr.charAt((num >> (j * 4)) & 0x0F);
  return str;
}
function str2blks_SHA1(str)
{
  var nblk = ((str.length + 8) >> 6) + 1;
  var blks = new Array(nblk * 16);
  for(var i = 0; i < nblk * 16; i++) blks[i] = 0;
  for(var i = 0; i < str.length; i++)
    blks[i >> 2] |= str.charCodeAt(i) << (24 - (i % 4) * 8);
  blks[i >> 2] |= 0x80 << (24 - (i % 4) * 8);
  blks[nblk * 16 - 1] = str.length * 8;
  return blks;
}
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}
function ft(t, b, c, d)
{
  if(t < 20) return (b & c) | ((~b) & d);
  if(t < 40) return b ^ c ^ d;
  if(t < 60) return (b & c) | (b & d) | (c & d);
  return b ^ c ^ d;
}
function kt(t)
{
  return (t < 20) ?  1518500249 : (t < 40) ?  1859775393 :
         (t < 60) ? -1894007588 : -899497514;
}
function calcSHA1(str)
{
  var x = str2blks_SHA1(str);
  var w = new Array(80);

  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;
  var e = -1009589776;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;
    var olde = e;

    for(var j = 0; j < 80; j++)
    {
      if(j < 16) w[j] = x[i + j];
      else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
      var t = safe_add(safe_add(rol(a, 5), ft(j, b, c, d)), safe_add(safe_add(e, w[j]), kt(j)));
      e = d;
      d = c;
      c = rol(b, 30);
      b = a;
      a = t;
    }

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
    e = safe_add(e, olde);
  }
  return hex(a) + hex(b) + hex(c) + hex(d) + hex(e);
}

/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 1.1 Copyright (C) Paul Johnston 1999 - 2002.
 * Code also contributed by Greg Holt
 * See http://pajhome.org.uk/site/legal.html for details.
 */

/*
 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF)
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
  return (msw << 16) | (lsw & 0xFFFF)
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt))
}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function cmn(q, a, b, x, s, t)
{
  return safe_add(rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b)
}
function ff(a, b, c, d, x, s, t)
{
  return cmn((b & c) | ((~b) & d), a, b, x, s, t)
}
function gg(a, b, c, d, x, s, t)
{
  return cmn((b & d) | (c & (~d)), a, b, x, s, t)
}
function hh(a, b, c, d, x, s, t)
{
  return cmn(b ^ c ^ d, a, b, x, s, t)
}
function ii(a, b, c, d, x, s, t)
{
  return cmn(c ^ (b | (~d)), a, b, x, s, t)
}

/*
 * Calculate the MD5 of an array of little-endian words, producing an array
 * of little-endian words.
 */
function coreMD5(x)
{
  var a =  1732584193
  var b = -271733879
  var c = -1732584194
  var d =  271733878

  for(i = 0; i < x.length; i += 16)
  {
    var olda = a
    var oldb = b
    var oldc = c
    var oldd = d

    a = ff(a, b, c, d, x[i+ 0], 7 , -680876936)
    d = ff(d, a, b, c, x[i+ 1], 12, -389564586)
    c = ff(c, d, a, b, x[i+ 2], 17,  606105819)
    b = ff(b, c, d, a, x[i+ 3], 22, -1044525330)
    a = ff(a, b, c, d, x[i+ 4], 7 , -176418897)
    d = ff(d, a, b, c, x[i+ 5], 12,  1200080426)
    c = ff(c, d, a, b, x[i+ 6], 17, -1473231341)
    b = ff(b, c, d, a, x[i+ 7], 22, -45705983)
    a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416)
    d = ff(d, a, b, c, x[i+ 9], 12, -1958414417)
    c = ff(c, d, a, b, x[i+10], 17, -42063)
    b = ff(b, c, d, a, x[i+11], 22, -1990404162)
    a = ff(a, b, c, d, x[i+12], 7 ,  1804603682)
    d = ff(d, a, b, c, x[i+13], 12, -40341101)
    c = ff(c, d, a, b, x[i+14], 17, -1502002290)
    b = ff(b, c, d, a, x[i+15], 22,  1236535329)

    a = gg(a, b, c, d, x[i+ 1], 5 , -165796510)
    d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632)
    c = gg(c, d, a, b, x[i+11], 14,  643717713)
    b = gg(b, c, d, a, x[i+ 0], 20, -373897302)
    a = gg(a, b, c, d, x[i+ 5], 5 , -701558691)
    d = gg(d, a, b, c, x[i+10], 9 ,  38016083)
    c = gg(c, d, a, b, x[i+15], 14, -660478335)
    b = gg(b, c, d, a, x[i+ 4], 20, -405537848)
    a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438)
    d = gg(d, a, b, c, x[i+14], 9 , -1019803690)
    c = gg(c, d, a, b, x[i+ 3], 14, -187363961)
    b = gg(b, c, d, a, x[i+ 8], 20,  1163531501)
    a = gg(a, b, c, d, x[i+13], 5 , -1444681467)
    d = gg(d, a, b, c, x[i+ 2], 9 , -51403784)
    c = gg(c, d, a, b, x[i+ 7], 14,  1735328473)
    b = gg(b, c, d, a, x[i+12], 20, -1926607734)

    a = hh(a, b, c, d, x[i+ 5], 4 , -378558)
    d = hh(d, a, b, c, x[i+ 8], 11, -2022574463)
    c = hh(c, d, a, b, x[i+11], 16,  1839030562)
    b = hh(b, c, d, a, x[i+14], 23, -35309556)
    a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060)
    d = hh(d, a, b, c, x[i+ 4], 11,  1272893353)
    c = hh(c, d, a, b, x[i+ 7], 16, -155497632)
    b = hh(b, c, d, a, x[i+10], 23, -1094730640)
    a = hh(a, b, c, d, x[i+13], 4 ,  681279174)
    d = hh(d, a, b, c, x[i+ 0], 11, -358537222)
    c = hh(c, d, a, b, x[i+ 3], 16, -722521979)
    b = hh(b, c, d, a, x[i+ 6], 23,  76029189)
    a = hh(a, b, c, d, x[i+ 9], 4 , -640364487)
    d = hh(d, a, b, c, x[i+12], 11, -421815835)
    c = hh(c, d, a, b, x[i+15], 16,  530742520)
    b = hh(b, c, d, a, x[i+ 2], 23, -995338651)

    a = ii(a, b, c, d, x[i+ 0], 6 , -198630844)
    d = ii(d, a, b, c, x[i+ 7], 10,  1126891415)
    c = ii(c, d, a, b, x[i+14], 15, -1416354905)
    b = ii(b, c, d, a, x[i+ 5], 21, -57434055)
    a = ii(a, b, c, d, x[i+12], 6 ,  1700485571)
    d = ii(d, a, b, c, x[i+ 3], 10, -1894986606)
    c = ii(c, d, a, b, x[i+10], 15, -1051523)
    b = ii(b, c, d, a, x[i+ 1], 21, -2054922799)
    a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359)
    d = ii(d, a, b, c, x[i+15], 10, -30611744)
    c = ii(c, d, a, b, x[i+ 6], 15, -1560198380)
    b = ii(b, c, d, a, x[i+13], 21,  1309151649)
    a = ii(a, b, c, d, x[i+ 4], 6 , -145523070)
    d = ii(d, a, b, c, x[i+11], 10, -1120210379)
    c = ii(c, d, a, b, x[i+ 2], 15,  718787259)
    b = ii(b, c, d, a, x[i+ 9], 21, -343485551)

    a = safe_add(a, olda)
    b = safe_add(b, oldb)
    c = safe_add(c, oldc)
    d = safe_add(d, oldd)
  }
  return [a, b, c, d]
}

/*
 * Convert an array of little-endian words to a hex string.
 */
function binl2hex(binarray)
{
  var hex_tab = "0123456789abcdef"
  var str = ""
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((i%4)*8)) & 0xF)
  }
  return str
}

/*
 * Convert an array of little-endian words to a base64 encoded string.
 */
function binl2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  var str = ""
  for(var i = 0; i < binarray.length * 32; i += 6)
  {
    str += tab.charAt(((binarray[i>>5] << (i%32)) & 0x3F) |
                      ((binarray[i>>5+1] >> (32-i%32)) & 0x3F))
  }
  return str
}

/*
 * Convert an 8-bit character string to a sequence of 16-word blocks, stored
 * as an array, and append appropriate padding for MD4/5 calculation.
 * If any of the characters are >255, the high byte is silently ignored.
 */
function str2binl(str)
{
  var nblk = ((str.length + 8) >> 6) + 1 // number of 16-word blocks
  var blks = new Array(nblk * 16)
  for(var i = 0; i < nblk * 16; i++) blks[i] = 0
  for(var i = 0; i < str.length; i++)
    blks[i>>2] |= (str.charCodeAt(i) & 0xFF) << ((i%4) * 8)
  blks[i>>2] |= 0x80 << ((i%4) * 8)
  blks[nblk*16-2] = str.length * 8
  return blks
}

/*
 * Convert a wide-character string to a sequence of 16-word blocks, stored as
 * an array, and append appropriate padding for MD4/5 calculation.
 */
function strw2binl(str)
{
  var nblk = ((str.length + 4) >> 5) + 1 // number of 16-word blocks
  var blks = new Array(nblk * 16)
  for(var i = 0; i < nblk * 16; i++) blks[i] = 0
  for(var i = 0; i < str.length; i++)
    blks[i>>1] |= str.charCodeAt(i) << ((i%2) * 16)
  blks[i>>1] |= 0x80 << ((i%2) * 16)
  blks[nblk*16-2] = str.length * 16
  return blks
}

/*
 * External interface
 */
function hexMD5 (str) { return binl2hex(coreMD5( str2binl(str))) }
function hexMD5w(str) { return binl2hex(coreMD5(strw2binl(str))) }
function b64MD5 (str) { return binl2b64(coreMD5( str2binl(str))) }
function b64MD5w(str) { return binl2b64(coreMD5(strw2binl(str))) }

/**
 *	DES
 */
function binl2arr(binarray)
{ 
  var str = new Array();
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str[i] = (binarray[i>>2] >> ((i%4)*8+4) & 0xF) *16 +
             (binarray[i>>2] >> ((i%4)*8) & 0xF);
  }
  return str
}

function calcMD5(str) { return binl2arr(coreMD5( str2binl(str))) }

function des (key, message, encrypt, mode, iv) {
  var spfunction1 = new Array (0x1010400,0,0x10000,0x1010404,0x1010004,0x10404,0x4,0x10000,0x400,0x1010400,0x1010404,0x400,0x1000404,0x1010004,0x1000000,0x4,0x404,0x1000400,0x1000400,0x10400,0x10400,0x1010000,0x1010000,0x1000404,0x10004,0x1000004,0x1000004,0x10004,0,0x404,0x10404,0x1000000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404,0x10404,0x1010404,0x10004,0x1010000,0x1000404,0x1000004,0x404,0x10404,0x1010400,0x404,0x1000400,0x1000400,0,0x10004,0x10400,0,0x1010004);
  var spfunction2 = new Array (0x80108020,0x80008000,0x8000,0x108020,0x100000,0x20,0x80100020,0x80008020,0x80000020,0x80108020,0x80108000,0x80000000,0x80008000,0x100000,0x20,0x80100020,0x108000,0x100020,0x80008020,0,0x80000000,0x8000,0x108020,0x80100000,0x100020,0x80000020,0,0x108000,0x8020,0x80108000,0x80100000,0x8020,0,0x108020,0x80100020,0x100000,0x80008020,0x80100000,0x80108000,0x8000,0x80100000,0x80008000,0x20,0x80108020,0x108020,0x20,0x8000,0x80000000,0x8020,0x80108000,0x100000,0x80000020,0x100020,0x80008020,0x80000020,0x100020,0x108000,0,0x80008000,0x8020,0x80000000,0x80100020,0x80108020,0x108000);
  var spfunction3 = new Array (0x208,0x8020200,0,0x8020008,0x8000200,0,0x20208,0x8000200,0x20008,0x8000008,0x8000008,0x20000,0x8020208,0x20008,0x8020000,0x208,0x8000000,0x8,0x8020200,0x200,0x20200,0x8020000,0x8020008,0x20208,0x8000208,0x20200,0x20000,0x8000208,0x8,0x8020208,0x200,0x8000000,0x8020200,0x8000000,0x20008,0x208,0x20000,0x8020200,0x8000200,0,0x200,0x20008,0x8020208,0x8000200,0x8000008,0x200,0,0x8020008,0x8000208,0x20000,0x8000000,0x8020208,0x8,0x20208,0x20200,0x8000008,0x8020000,0x8000208,0x208,0x8020000,0x20208,0x8,0x8020008,0x20200);
  var spfunction4 = new Array (0x802001,0x2081,0x2081,0x80,0x802080,0x800081,0x800001,0x2001,0,0x802000,0x802000,0x802081,0x81,0,0x800080,0x800001,0x1,0x2000,0x800000,0x802001,0x80,0x800000,0x2001,0x2080,0x800081,0x1,0x2080,0x800080,0x2000,0x802080,0x802081,0x81,0x800080,0x800001,0x802000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081,0x80,0x802081,0x81,0x1,0x2000,0x800001,0x2001,0x802080,0x800081,0x2001,0x2080,0x800000,0x802001,0x80,0x800000,0x2000,0x802080);
  var spfunction5 = new Array (0x100,0x2080100,0x2080000,0x42000100,0x80000,0x100,0x40000000,0x2080000,0x40080100,0x80000,0x2000100,0x40080100,0x42000100,0x42080000,0x80100,0x40000000,0x2000000,0x40080000,0x40080000,0,0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0,0x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100);
  var spfunction6 = new Array (0x20000010,0x20400000,0x4000,0x20404010,0x20400000,0x10,0x20404010,0x400000,0x20004000,0x404010,0x400000,0x20000010,0x400010,0x20004000,0x20000000,0x4010,0,0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0,0x404010,0x20404000,0x4010,0x404000,0x20404000,0x20000000,0x20004000,0x10,0x20400010,0x404000,0x20404010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0,0x20404000,0x20000000,0x400010,0x20004010);
  var spfunction7 = new Array (0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002);
  var spfunction8 = new Array (0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x41000,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000);

  var keys = des_createKeys (key);
  var m=0, i, j, temp, temp2, right1, right2, left, right, looping;
  var cbcleft, cbcleft2, cbcright, cbcright2
  var endloop, loopinc;
  var len = message.length;
  var chunk = 0;
  var iterations = keys.length == 32 ? 3 : 9; //single or triple des
  if (iterations == 3) {looping = encrypt ? new Array (0, 32, 2) : new Array (30, -2, -2);}
  else {looping = encrypt ? new Array (0, 32, 2, 62, 30, -2, 64, 96, 2) : new Array (94, 62, -2, 32, 64, 2, 30, -2, -2);}

  result = "";
  tempresult = "";

  if (mode == 1) { //CBC mode
    cbcleft = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++);
    cbcright = (iv.charCodeAt(m++) << 24) | (iv.charCodeAt(m++) << 16) | (iv.charCodeAt(m++) << 8) | iv.charCodeAt(m++);
    m=0;
  }

  while (m < len) {
    left = (message[m++] << 24) | (message[m++] << 16) | (message[m++] << 8) | message[m++];
    right = (message[m++] << 24) | (message[m++] << 16) | (message[m++] << 8) | message[m++];

    if (mode == 1) {if (encrypt) {left ^= cbcleft; right ^= cbcright;} else {cbcleft2 = cbcleft; cbcright2 = cbcright; cbcleft = left; cbcright = right;}}

    temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
    temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16);
    temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2);
    temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
    temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);

    left = ((left << 1) | (left >>> 31)); 
    right = ((right << 1) | (right >>> 31)); 

    for (j=0; j<iterations; j+=3) {
      endloop = looping[j+1];
      loopinc = looping[j+2];
      for (i=looping[j]; i!=endloop; i+=loopinc) { //for efficiency
        right1 = right ^ keys[i]; 
        right2 = ((right >>> 4) | (right << 28)) ^ keys[i+1];
        temp = left;
        left = right;
        right = temp ^ (spfunction2[(right1 >>> 24) & 0x3f] | spfunction4[(right1 >>> 16) & 0x3f]
              | spfunction6[(right1 >>>  8) & 0x3f] | spfunction8[right1 & 0x3f]
              | spfunction1[(right2 >>> 24) & 0x3f] | spfunction3[(right2 >>> 16) & 0x3f]
              | spfunction5[(right2 >>>  8) & 0x3f] | spfunction7[right2 & 0x3f]);
      }
      temp = left; left = right; right = temp; //unreverse left and right
    } //for either 1 or 3 iterations

    left = ((left >>> 1) | (left << 31)); 
    right = ((right >>> 1) | (right << 31)); 

    temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
    temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
    temp = ((right >>> 2) ^ left) & 0x33333333; left ^= temp; right ^= (temp << 2);
    temp = ((left >>> 16) ^ right) & 0x0000ffff; right ^= temp; left ^= (temp << 16);
    temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);

    if (mode == 1) {if (encrypt) {cbcleft = left; cbcright = right;} else {left ^= cbcleft2; right ^= cbcright2;}}
    tempresult += String.fromCharCode ((left>>>24), ((left>>>16) & 0xff), ((left>>>8) & 0xff), (left & 0xff), (right>>>24), ((right>>>16) & 0xff), ((right>>>8) & 0xff), (right & 0xff));

    chunk += 8;
    if (chunk == 512) {result += tempresult; tempresult = ""; chunk = 0;}
  }
  return result + tempresult;
}

function des_createKeys (key) {
  pc2bytes0  = new Array (0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204);
  pc2bytes1  = new Array (0,0x1,0x100000,0x100001,0x4000000,0x4000001,0x4100000,0x4100001,0x100,0x101,0x100100,0x100101,0x4000100,0x4000101,0x4100100,0x4100101);
  pc2bytes2  = new Array (0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808,0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808);
  pc2bytes3  = new Array (0,0x200000,0x8000000,0x8200000,0x2000,0x202000,0x8002000,0x8202000,0x20000,0x220000,0x8020000,0x8220000,0x22000,0x222000,0x8022000,0x8222000);
  pc2bytes4  = new Array (0,0x40000,0x10,0x40010,0,0x40000,0x10,0x40010,0x1000,0x41000,0x1010,0x41010,0x1000,0x41000,0x1010,0x41010);
  pc2bytes5  = new Array (0,0x400,0x20,0x420,0,0x400,0x20,0x420,0x2000000,0x2000400,0x2000020,0x2000420,0x2000000,0x2000400,0x2000020,0x2000420);
  pc2bytes6  = new Array (0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002,0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002);
  pc2bytes7  = new Array (0,0x10000,0x800,0x10800,0x20000000,0x20010000,0x20000800,0x20010800,0x20000,0x30000,0x20800,0x30800,0x20020000,0x20030000,0x20020800,0x20030800);
  pc2bytes8  = new Array (0,0x40000,0,0x40000,0x2,0x40002,0x2,0x40002,0x2000000,0x2040000,0x2000000,0x2040000,0x2000002,0x2040002,0x2000002,0x2040002);
  pc2bytes9  = new Array (0,0x10000000,0x8,0x10000008,0,0x10000000,0x8,0x10000008,0x400,0x10000400,0x408,0x10000408,0x400,0x10000400,0x408,0x10000408);
  pc2bytes10 = new Array (0,0x20,0,0x20,0x100000,0x100020,0x100000,0x100020,0x2000,0x2020,0x2000,0x2020,0x102000,0x102020,0x102000,0x102020);
  pc2bytes11 = new Array (0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200);
  pc2bytes12 = new Array (0,0x1000,0x8000000,0x8001000,0x80000,0x81000,0x8080000,0x8081000,0x10,0x1010,0x8000010,0x8001010,0x80010,0x81010,0x8080010,0x8081010);
  pc2bytes13 = new Array (0,0x4,0x100,0x104,0,0x4,0x100,0x104,0x1,0x5,0x101,0x105,0x1,0x5,0x101,0x105);

  var iterations = key.length >= 24 ? 3 : 1;
  var keys = new Array (32 * iterations);
  var shifts = new Array (0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0);
  var lefttemp, righttemp, m=0, n=0, temp;

  for (var j=0; j<iterations; j++) { //either 1 or 3 iterations
    left = (key[m++] << 24) | (key[m++] << 16) | (key[m++] << 8) | key[m++];
    right = (key[m++] << 24) | (key[m++] << 16) | (key[m++] << 8) | key[m++];
    temp = ((left >>> 4) ^ right) & 0x0f0f0f0f; right ^= temp; left ^= (temp << 4);
    temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
    temp = ((left >>> 2) ^ right) & 0x33333333; right ^= temp; left ^= (temp << 2);
    temp = ((right >>> -16) ^ left) & 0x0000ffff; left ^= temp; right ^= (temp << -16);
    temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);
    temp = ((right >>> 8) ^ left) & 0x00ff00ff; left ^= temp; right ^= (temp << 8);
    temp = ((left >>> 1) ^ right) & 0x55555555; right ^= temp; left ^= (temp << 1);

    temp = (left << 8) | ((right >>> 20) & 0x000000f0);
    left = (right << 24) | ((right << 8) & 0xff0000) | ((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0);
    right = temp;

    for (i=0; i < shifts.length; i++) {
      if (shifts[i]) {left = (left << 2) | (left >>> 26); right = (right << 2) | (right >>> 26);}
      else {left = (left << 1) | (left >>> 27); right = (right << 1) | (right >>> 27);}
      left &= 0xfffffff0; right &= 0xfffffff0;

      lefttemp = pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf]
              | pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(left >>> 16) & 0xf]
              | pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf]
              | pc2bytes6[(left >>> 4) & 0xf];
      righttemp = pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf]
                | pc2bytes9[(right >>> 20) & 0xf] | pc2bytes10[(right >>> 16) & 0xf]
                | pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf]
                | pc2bytes13[(right >>> 4) & 0xf];
      temp = ((righttemp >>> 16) ^ lefttemp) & 0x0000ffff; 
      keys[n++] = lefttemp ^ temp; keys[n++] = righttemp ^ (temp << 16);
    }
  }
  return keys;
}

function padd( indata )
{
  var PaddedDataLen = Math.floor(indata.length/8+1)*8;
  var PaddedData = new Array();
  var PaddBytes = PaddedDataLen - indata.length;  
  for( var i=0; i<indata.length;i++) PaddedData[i]=indata.charCodeAt(i);
  for( var i=indata.length ; i<PaddedDataLen; i++ ) PaddedData[i] = PaddBytes;
  return PaddedData; 
}


function cryptpass( username, password )
{
  var deskey = calcMD5( username );

  var newdeskey1 = new Array();
  var newdeskey2 = new Array();

  for( var i=0; i<8;i++ )
  {
    newdeskey1[i]=deskey[i];
  }
  for( var i=8;i<16;i++ )
  {
    newdeskey2[i-8]=deskey[i];
  }
  
  var paddedpassword = padd( password );
  var cipher1 = des( newdeskey1, paddedpassword, 1, 0 );

  var finalcipher1 = new Array();
  for( var i=0; i<cipher1.length; i++ )
  {
    finalcipher1[i]=cipher1.charCodeAt(i);
  }


  var cipher2 = des( newdeskey2, finalcipher1, 1, 0 );

  var finalcipher2 = new Array();
  for( var i=0; i<cipher2.length; i++ )
  {
    finalcipher2[i]=cipher2.charCodeAt(i);
  }

  return finalcipher2;
}
