////////////////////////////////////////////////////////////////
//
// function: ExpandCollapseTable
//
// description:
//
// notes:
//
// 1/14/2004 MDL
//
// (C) Copyright 2003 Venux.net
//
function ExpandCollapseTable( linkname, tablename, bgcolor )
{

	var linktochange = linkname;

	if (tablename.style.display == 'none')
	{
		linktochange.innerHTML = '<IMG src="images/folder_open.gif" border=0>';
		tablename.style.display='inline';
		tablename.style.backgroundColor=bgcolor;
	}
	else
	{
		linktochange.innerHTML = '<IMG src="images/folder_closed.gif" border=0>';
		tablename.style.display='none';
		tablename.style.backgroundColor=bgcolor;
	}

	return( true );

} // function ExpandCollapseTable()



////////////////////////////////////////////////////////////////
//
// function: bulletSwap
//
// description:
//
// notes:
//
// 03/11/2005 TLR
//
// (C) Copyright 2005 Venux.net
//
function bulletSwap( bulletID )
{
	oBullet = eval( document.getElementById( bulletID ) );
	
	if ( oBullet.src.indexOf( "images/bullet.gif" ) >0 )
	{
		oBullet.src = "images/dark-bullet.gif";
	}
	else
	{
		oBullet.src = "images/bullet.gif";
	}

} // function bulletSwap()


////////////////////////////////////////////////////////////////
//
// function: hideAnchorLines
//
// description:	hide ugly anchor lines in mozilla
//			and older IE browsers
//
// notes:		should hide ugly anchor lines in mozilla
//			and older IE browsers, butright now it is
//			shooting the IE window to the 
//			background - gotta figure that out
//
// 03/11/2005 TLR
//
// (C) Copyright 2005 Venux.net
//
function hideAnchorLines( oLink )
{
	if ( oLink.blur )
	{
		oLink.blur();
	}

} // function hideAnchorLines()



////////////////////////////////////////////////////////////////////////////////
function OnClickApplyValues()
//
// Description:
//	Final check of the "appy payments" form in the "cash receipts" and
//	apply "apply credits" forms (accunts rec.->cash receipt etc.). Makes
//	sure that "apply" value is not blank in the case that the user clicks
// 	the apply button before moving focus form the apply textbox.
//
// Returns:
//		true
//
// 09/02/03 RPS Initial version.
//
{
	//
	// Uncheck any of the "Open invoice #" checkboxes if their corresponding
	// "apply" textbox is empty
	//
	for ( x = 0; x < document.forms[ 0 ].length; x++ )
	{
		oItem = document.forms[ 0 ].item( x );

		if ( ( oItem.name ).substring( 0, 10 ) == "invoice_id" )
		{
			if ( oItem.checked )
			{

				oTxt_Apply = document.forms[ 0 ].item( "apply_to_" + oItem.value );

				if( oTxt_Apply.value == '' )
				{
					//oItem.checked = false;
				}
			}
		}
	}

	return( true );

}


////////////////////////////////////////////////////////////////////////////////
function OnClickSelectInvoice( oChk_Invoice )
//
// Description:
//		For the Accounts Rec.->Cash Receipts->Apply Credit/Payment form. updates
//		"apply" text boxes when an invoice's check-box is clicked
//
// Returns:
//		true
//
// 09/02/03 RPS Initial version.
//
//
{

	//
	// enable/disable the text box for this row
	//
	oTxt_Apply = document.all.item( "apply_to_" + oChk_Invoice.value );
	oTxt_Apply.disabled = !oChk_Invoice.checked;

	oTxt_Balance = 	document.all.item( "balance_" + oChk_Invoice.value );

	//
	// sanity check on this row's amount.  You'd think that blank would
	// be considered NaN
	//
	if ( isNaN( oTxt_Apply.value ) ||
		  ( rtrim( oTxt_Apply.value ) ).length == 0 )
	{
		return( false );
	}

	//
	// Make sure javascript deals with actual numbers, not strings
	//
	iValue = parseFloat( rtrim( oTxt_Apply.value ) );
	iAmount_Received = parseFloat( document.forms[ 0 ].amount_received.value );
	iBalance = parseFloat( oTxt_Balance.value );

	//
	// Update the hidden input that stores the total amount received
	//
	if ( !oChk_Invoice.checked )
	{
		document.forms[ 0 ].amount_received.value = iAmount_Received +
													oTxt_Apply.Old_Value;
		oTxt_Balance.value = iBalance + oTxt_Apply.Old_Value;
	}
	else if ( iValue > iAmount_Received )
	{
		alert( "Amount entered is greater than the balance to apply." );
		oTxt_Apply.value = '';
		oTxt_Apply.Old_Value = undefined;
		document.all.item( "invoice_id_" + iInvoice_ID ).checked = false;
		oTxt_Apply.disabled = true;
	}
	else if ( iValue > iBalance )
	{
		alert( "Amount entered is greater than the balance on the invoice." );
		oTxt_Apply.value = '';
		oTxt_Apply.Old_Value = undefined;
		document.all.item( "invoice_id_" + iInvoice_ID ).checked = false;
		oTxt_Apply.disabled = true;
	}
	else if ( iValue <= iAmount_Received && iValue >= 0 )
	{
		document.forms[ 0 ].amount_received.value = iAmount_Received - iValue;
		oTxt_Balance.value = iBalance - iValue;
		oTxt_Apply.focus();
	}

	//
	// Update the div that displays the total amount received
	//
	document.all.display_amount_received.innerText =
					currency( document.forms[ 0 ].amount_received.value );

	document.all.item( "display_balance_" + oChk_Invoice.value ).innerText =
					currency( oTxt_Balance.value );

	//
	// when amount received is used up (i.e. 0), disable all unselected checkboxes,
	// otherwise enable
	//

	for( x = 0; x < document.forms[ 0 ].length; x++ )
	{
		oElement = document.forms[ 0 ].item( x );

		if ( oElement.name.substring( 0, 10 ) == "invoice_id" )
		{
			if ( !oChk_Invoice.checked )
			{
				oElement.disabled = false;
			}
			else if ( document.forms[ 0 ].amount_received.value == 0 &&
						 !oElement.checked )
			{
				oElement.disabled = true;
			}
		}
	}

	return( true );

}


////////////////////////////////////////////////////////////////////////////////
function OnChangeApply( oTxt_Apply, iInvoice_ID )
//
// Description:
//		For the Accounts Rec.->Cash Receipts->Apply Credit/Payment form.
//		Updates the amount received when a value in an "apply" text box
//		is changed
//
// Returns:
//		true
//
// 09/02/03 RPS Initial version.
//
//
{

	//
	// Must do sanity check on value to apply so we don't get runtimes
	//
	iValue = rtrim( oTxt_Apply.value );

	if ( typeof( oTxt_Apply.Old_Value ) != "undefined" &&
		  iValue == oTxt_Apply.Old_Value )
	{
		return( true );
	}

	iAmount_Received = parseFloat( document.forms[ 0 ].amount_received.value );
	iValue = parseFloat( iValue );
	oTxt_Balance = document.all.item( "balance_" + iInvoice_ID );
	iBalance = parseFloat( oTxt_Balance.value );

	//
	// Set total amount back to what it was before the last value was
	// applied
	//
	if ( typeof( oTxt_Apply.Old_Value ) != "undefined" )
	{
		document.forms[ 0 ].amount_received.value =
													iAmount_Received + oTxt_Apply.Old_Value;
		oTxt_Balance.value = parseFloat( oTxt_Balance.value ) + oTxt_Apply.Old_Value;
		iBalance = parseFloat( oTxt_Balance.value );
		iAmount_Received = parseFloat( document.forms[ 0 ].amount_received.value );
	}

	//
	// Form validation is done here because...er, this form doesn't use
	// (i.e. wasn't designed to use) server-side validation for some reason
	//
	if ( ( rtrim( oTxt_Apply.value ) ).length == 0 )
	{
		document.all.item( "invoice_id_" + iInvoice_ID ).checked = false;
	}
	else if ( isNaN( oTxt_Apply.value ) == true )
	{
		alert( "Invalid amount." );
		oTxt_Apply.value = '';
		oTxt_Apply.Old_Value = undefined;
		document.all.item( "invoice_id_" + iInvoice_ID ).checked = false;
		oTxt_Apply.disabled = true;
	}
	else if ( iValue > iBalance )
	{
		alert( "Amount entered is greater than the balance on the invoice." );
		oTxt_Apply.value = '';
		oTxt_Apply.Old_Value = undefined;
		document.all.item( "invoice_id_" + iInvoice_ID ).checked = false;
		oTxt_Apply.disabled = true;
	}
	else if ( iValue > iAmount_Received )
	{
		alert( "Amount entered is greater than the balance to apply." );
		oTxt_Apply.value = '';
		oTxt_Apply.Old_Value = undefined;
		document.all.item( "invoice_id_" + iInvoice_ID ).checked = false;
		oTxt_Apply.disabled = true;
	}
	else if ( iValue < 0 )
	{
		alert( "Invalid amount." );
		oTxt_Apply.value = '';
		oTxt_Apply.Old_Value = undefined;
		document.all.item( "invoice_id_" + iInvoice_ID ).checked = false;
		oTxt_Apply.disabled = true;
	}
	else
	{
		document.forms[ 0 ].amount_received.value -= iValue;
		oTxt_Balance.value -= iValue;
	}

	document.all.display_amount_received.innerText =
		currency( document.forms[ 0 ].amount_received.value );

	document.all.item( "display_balance_" + iInvoice_ID ).innerText =
					currency( oTxt_Balance.value );


	if ( isNaN( oTxt_Apply.value ) == true ||
		  ( rtrim( oTxt_Apply.value ) ).length == 0 ||
		  iValue > iAmount_Received ||
		  iValue < 0 || iValue > iBalance )
	{
		oTxt_Apply.Old_Value = undefined;
	}
	else
	{
		oTxt_Apply.Old_Value = new Number( iValue );
	}

	//
	// when amount received is used up (i.e. 0), disable all unselected checkboxes,
	// otherwise enable
	//
	for( x = 0; x < document.forms[ 0 ].length; x++ )
	{
		oElement = document.forms[ 0 ].item( x );

		if ( oElement.name.substring( 0, 10 ) == "invoice_id" )
		{
			if ( document.forms[ 0 ].amount_received.value == 0 &&
				 oElement.checked == false )
			{
				oElement.disabled = true;
			}
			else
			{
				oElement.disabled = false;
			}
		}
	}


	return( true );

}


////////////////////////////////////////////////////////////////
//
// function: currency
//
// description: converts a number to currency format similar to
//				    what php's number_format can do
//
// notes:
//
// //2003
//
// (C) Copyright 2003 Venux.net
//
function currency(anynum) {
   //-- Returns passed number as string in $xxx,xxx.xx format.
   anynum=eval(anynum)
   workNum=Math.abs((Math.round(anynum*100)/100));workStr=""+workNum
   if (workStr.indexOf(".")==-1){workStr+=".00"}
   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
   pStr=workStr.substr(workStr.indexOf("."))
   while (pStr.length<3){pStr+="0"}

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=dStr.length
      dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
   }
   retval = dStr + pStr
   //-- Put numbers in parentheses if negative.
   if (anynum<0) {retval="("+retval+")"}
   return "$"+retval
}




////////////////////////////////////////////////////////////////
//
// function: SetUISize( oForm, iFont_Size )
//
// description:
//
// notes:
//
// 6/2/2003 MDL
//
// (C) Copyright 2003 Venux.net
//
function SetUISizeColor( oForm, iFont_Size )
{
        if ( typeof( oForm ) == "undefined" )
        {
                return( false );
        }
        for ( x = 0; x < oForm.length; x++ )
        {
                if ( oForm.item( x ).tagName == "INPUT" ||
                                 oForm.item( x ).tagName == "TEXTAREA" ||
                                 oForm.item( x ).tagName == "SELECT" )
                {
                        oForm.item( x ).style.fontSize = iFont_Size;
                        //oForm.item( x ).style.background = "#FFCF00";
                }
        }

        return true;
}




////////////////////////////////////////////////////////////////////////////////
function rtrim( oldstr )
//
// Description:
//
// Notes:
//

{
   if ( typeof( oldstr ) != "string" )
   {
      return( "" );
   }
   x = oldstr.length - 1;
   while ( oldstr.charAt( x ) == ' ' && x >= 0 )
   {
      x--
   }

   return oldstr.substring( 0, x + 1)
}

/****************************
 * Sign-In Password Check
 */
function doPassword(oFormObj)
{
	var hash_pass;

	/*
	 * If the password length is < 5, blank the field.
	 */
	if ( oFormObj.value.length >= 5 )
	{
		/*
		 * Make the MD5 of the users password.
		 */
		hash_pass = MD5(oFormObj.value);
		oFormObj.value = hash_pass;
	}

	else
		oFormObj.value = "";
}


/****************************
 * User form specific password checker
 */
function doUserPassword()
{
	var hash_pass;

	/*
	 * If the password length is < 5, blank the field.
	 */
	if ( document.user_form.password.value.length != 0 )
	{
		if ( document.user_form.password.value.length >= 5 )
		{
			/*
			 * Make the MD5 of the users password.
			 */
			hash_pass = MD5( document.user_form.password.value);
			document.user_form.password.value = hash_pass;
		}
	}
	//document.user_form.submit();
	return( true );
}


/*
 *  md5.jvs 1.0b 27/06/96
 *
 * Javascript implementation of the RSA Data Security, Inc. MD5
 * Message-Digest Algorithm.
 *
 * Copyright (c) 1996 Henri Torgemane. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for any purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies.
 *
 * Of course, this soft is provided "as is" without express or implied
 * warranty of any kind.
 */

function array(n) {
  for(i=0;i<n;i++) this[i]=0;
  this.length=n;
}

/* Some basic logical functions had to be rewritten because of a bug in
 * Javascript.. Just try to compute 0xffffffff >> 4 with it..
 * Of course, these functions are slower than the original would be, but
 * at least, they work!
 */

function integer(n) { return n%(0xffffffff+1); }

function shr(a,b) {
  a=integer(a);
  b=integer(b);
  if (a-0x80000000>=0) {
    a=a%0x80000000;
    a>>=b;
    a+=0x40000000>>(b-1);
  } else
    a>>=b;
  return a;
}

function shl1(a) {
  a=a%0x80000000;
  if (a&0x40000000==0x40000000)
  {
    a-=0x40000000;
    a*=2;
    a+=0x80000000;
  } else
    a*=2;
  return a;
}

function shl(a,b) {
  a=integer(a);
  b=integer(b);
  for (var i=0;i<b;i++) a=shl1(a);
  return a;
}

function and(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0)
    if (t2>=0)
      return ((t1&t2)+0x80000000);
    else
      return (t1&b);
  else
    if (t2>=0)
      return (a&t2);
    else
      return (a&b);
}

function or(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0)
    if (t2>=0)
      return ((t1|t2)+0x80000000);
    else
      return ((t1|b)+0x80000000);
  else
    if (t2>=0)
      return ((a|t2)+0x80000000);
    else
      return (a|b);
}

function xor(a,b) {
  a=integer(a);
  b=integer(b);
  var t1=(a-0x80000000);
  var t2=(b-0x80000000);
  if (t1>=0)
    if (t2>=0)
      return (t1^t2);
    else
      return ((t1^b)+0x80000000);
  else
    if (t2>=0)
      return ((a^t2)+0x80000000);
    else
      return (a^b);
}

function not(a) {
  a=integer(a);
  return (0xffffffff-a);
}

/* Here begin the real algorithm */

    var state = new array(4);
    var count = new array(2);
	count[0] = 0;
	count[1] = 0;
    var buffer = new array(64);
    var transformBuffer = new array(16);
    var digestBits = new array(16);

    var S11 = 7;
    var S12 = 12;
    var S13 = 17;
    var S14 = 22;
    var S21 = 5;
    var S22 = 9;
    var S23 = 14;
    var S24 = 20;
    var S31 = 4;
    var S32 = 11;
    var S33 = 16;
    var S34 = 23;
    var S41 = 6;
    var S42 = 10;
    var S43 = 15;
    var S44 = 21;

    function F(x,y,z) {
	return or(and(x,y),and(not(x),z));
    }

    function G(x,y,z) {
	return or(and(x,z),and(y,not(z)));
    }

    function H(x,y,z) {
	return xor(xor(x,y),z);
    }

    function I(x,y,z) {
	return xor(y ,or(x , not(z)));
    }

    function rotateLeft(a,n) {
	return or(shl(a, n),(shr(a,(32 - n))));
    }

    function FF(a,b,c,d,x,s,ac) {
        a = a+F(b, c, d) + x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function GG(a,b,c,d,x,s,ac) {
	a = a+G(b, c, d) +x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function HH(a,b,c,d,x,s,ac) {
	a = a+H(b, c, d) + x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function II(a,b,c,d,x,s,ac) {
	a = a+I(b, c, d) + x + ac;
	a = rotateLeft(a, s);
	a = a+b;
	return a;
    }

    function transform(buf,offset) {
	var a=0, b=0, c=0, d=0;
	var x = transformBuffer;

	a = state[0];
	b = state[1];
	c = state[2];
	d = state[3];

	for (i = 0; i < 16; i++) {
	    x[i] = and(buf[i*4+offset],0xff);
	    for (j = 1; j < 4; j++) {
		x[i]+=shl(and(buf[i*4+j+offset] ,0xff), j * 8);
	    }
	}

	/* Round 1 */
	a = FF ( a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
	d = FF ( d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
	c = FF ( c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
	b = FF ( b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
	a = FF ( a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
	d = FF ( d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
	c = FF ( c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
	b = FF ( b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
	a = FF ( a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
	d = FF ( d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
	c = FF ( c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
	b = FF ( b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
	a = FF ( a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
	d = FF ( d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
	c = FF ( c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
	b = FF ( b, c, d, a, x[15], S14, 0x49b40821); /* 16 */

	/* Round 2 */
	a = GG ( a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
	d = GG ( d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
	c = GG ( c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
	b = GG ( b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
	a = GG ( a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
	d = GG ( d, a, b, c, x[10], S22,  0x2441453); /* 22 */
	c = GG ( c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
	b = GG ( b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
	a = GG ( a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
	d = GG ( d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
	c = GG ( c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
	b = GG ( b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
	a = GG ( a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
	d = GG ( d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
	c = GG ( c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
	b = GG ( b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */

	/* Round 3 */
	a = HH ( a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
	d = HH ( d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
	c = HH ( c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
	b = HH ( b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
	a = HH ( a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
	d = HH ( d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
	c = HH ( c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
	b = HH ( b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
	a = HH ( a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
	d = HH ( d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
	c = HH ( c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
	b = HH ( b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
	a = HH ( a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
	d = HH ( d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
	c = HH ( c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
	b = HH ( b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */

	/* Round 4 */
	a = II ( a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
	d = II ( d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
	c = II ( c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
	b = II ( b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
	a = II ( a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
	d = II ( d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
	c = II ( c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
	b = II ( b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
	a = II ( a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
	d = II ( d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
	c = II ( c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
	b = II ( b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
	a = II ( a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
	d = II ( d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
	c = II ( c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
	b = II ( b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */

	state[0] +=a;
	state[1] +=b;
	state[2] +=c;
	state[3] +=d;

    }

    function init() {
	count[0]=count[1] = 0;
	state[0] = 0x67452301;
	state[1] = 0xefcdab89;
	state[2] = 0x98badcfe;
	state[3] = 0x10325476;
	for (i = 0; i < digestBits.length; i++)
	    digestBits[i] = 0;
    }

    function update(b) {
	var index,i;

	index = and(shr(count[0],3) , 0x3f);
	if (count[0]<0xffffffff-7)
	  count[0] += 8;
        else {
	  count[1]++;
	  count[0]-=0xffffffff+1;
          count[0]+=8;
        }
	buffer[index] = and(b,0xff);
	if (index  >= 63) {
	    transform(buffer, 0);
	}
    }

    function finish() {
	var bits = new array(8);
	var	padding;
	var	i=0, index=0, padLen=0;

	for (i = 0; i < 4; i++) {
	    bits[i] = and(shr(count[0],(i * 8)), 0xff);
	}
        for (i = 0; i < 4; i++) {
	    bits[i+4]=and(shr(count[1],(i * 8)), 0xff);
	}
	index = and(shr(count[0], 3) ,0x3f);
	padLen = (index < 56) ? (56 - index) : (120 - index);
	padding = new array(64);
	padding[0] = 0x80;
        for (i=0;i<padLen;i++)
	  update(padding[i]);
        for (i=0;i<8;i++)
	  update(bits[i]);

	for (i = 0; i < 4; i++) {
	    for (j = 0; j < 4; j++) {
		digestBits[i*4+j] = and(shr(state[i], (j * 8)) , 0xff);
	    }
	}
    }

/* End of the MD5 algorithm */

function hexa(n) {
 var hexa_h = "0123456789abcdef";
 var hexa_c="";
 var hexa_m=n;
 for (hexa_i=0;hexa_i<8;hexa_i++) {
   hexa_c=hexa_h.charAt(Math.abs(hexa_m)%16)+hexa_c;
   hexa_m=Math.floor(hexa_m/16);
 }
 return hexa_c;
}


var ascii="01234567890123456789012345678901" +
          " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"+
          "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

function MD5(entree)
{
 var l,s,k,ka,kb,kc,kd;

 init();
 for (k=0;k<entree.length;k++) {
   l=entree.charAt(k);
   update(ascii.lastIndexOf(l));
 }
 finish();
 ka=kb=kc=kd=0;
 for (i=0;i<4;i++) ka+=shl(digestBits[15-i], (i*8));
 for (i=4;i<8;i++) kb+=shl(digestBits[15-i], ((i-4)*8));
 for (i=8;i<12;i++) kc+=shl(digestBits[15-i], ((i-8)*8));
 for (i=12;i<16;i++) kd+=shl(digestBits[15-i], ((i-12)*8));
 s=hexa(kd)+hexa(kc)+hexa(kb)+hexa(ka);
 return s;
}



////////////////////////////////////////////////////////////////////////////////
   function OpenPopup( sURL, iHeight, iWidth, iLeft, iTop )
   //
   // Description:
   //    Opens a "popup" browser, a.k.a "mini" browser. I digress: child window.
   //    The child is connected to the parent by virtue of the oChild_Win
   //    variable existing in the parent's window object and vv. the oParent_Win
   //    variable existing in the child's window object..
   //
   // Returns:
   //    true
   //
   // Notes:
   //
   // 04/02/02 RPS Initial version.
   //
   {
      if ( typeof( window.oChild_Win ) != "undefined" &&
           window.oChild_Win != null )
      {
         window.oChild_Win.focus();
         return( true );
      }

      //
      // the titlebar feature doesn't work cuz apache can't serve up
      // "HTML Application" files (.hta) but I've put it here anyway just for fun.
      //
      sFeatures = "channelmods = no, directories = no, fullscreen = no, " +
                  "location = no, menubar = no, resizeable = no, scrollbars = no, " +
                  "status = no, titlebar = no, toolbar = no, " +
                  "height = " + iHeight + ", width = " + iWidth + ", " +
                  "left = " + iLeft + ", top = " + iTop;

      bReplace = true;

      sWindow_Name = "child_window";

      window.oChild_Win = window.open( sURL, sWindow_Name, sFeatures, bReplace );
      //window.open( sURL, sWindow_Name, sFeatures, bReplace );


      window.oChild_Win.oParent_Win = this;

      //
      // Invalidate the child of the user closes the parent
      //
      window.onunload = ParentForceClosePopup;

      // doesn't work. why not?
      //window.oChild_Win.onunload = ChildPopupUnload;

      return( true );

   }

////////////////////////////////////////////////////////////////////////////////
   function ParentForceClosePopup()
   //
   // Description:
   //    Makes sure that the child window is closed, if it exists.
   //
   // Returns:
   //    true;
   //
   // Notes:
   //
   // 04/02/02 RPS Initial version.
   //
   {
      //alert( 'ParentForceClosePoup()' );

      if ( typeof( window.oChild_Win ) != 'undefined' &&
           window.oChild_Win != null )
      {
         //alert( 'ParentForceClosePoup - closing child window' );

         window.oChild_Win.bParent_Critical_Exit = true;
         window.oChild_Win.close();
      }

      return( true );
   }

////////////////////////////////////////////////////////////////////////////////
   function ChildPopupUnload()
   //
   // Description:
   //    Takes care of parent-child synchronization when the child window
   //    closes.  Wire the child's onload even to this function.
   //
   // Returns:
   //
   // Notes:
   //
   // 04/02/02 RPS Initial version.
   //
   {
      //alert( 'ChildPopupunload()' );

      //
      // Critical exit happens when the parent unloads before the child
      //
      if ( window.bParent_Critical_Exit == true )
      {
         //alert( 'ChildPopupunload - critical exit' );
         window.close();
         return( true );
      }

      // breaks when child window submits
      //window.oParent_Win.oChild_Win = undefined;
      //window.oParent_Win.focus();

      window.opener.oChild_Win = null;

      if ( window.bSelf_Close == true )
      {
         //alert ( 'self close' );
         window.opener.focus();
         window.close();
      }

      return( true );

   }

////////////////////////////////////////////////////////////////////////////////
   function ChildPopupLoad()
   //
   // Description:
   //    To be executed in the child via onload event.  Wires child to parent
   //    and sets up default state variables
   //
   // Returns:
   //    true
   //
   // Notes:
   //
   // 04/21/02 RPS Initial version.
   //
   {
      //
      // Reconnect parent to child
      //
      window.opener.oChild_Win = this;

      //
      // This variable specifies that when the child window's unload occurs,
      // it *must* close itself.
      //
      window.bParent_Critical_Exit = false;
      window.bSelf_Close = true;

      return( true );
   }





