<!-- Global Javascript Vars -->

var isIE6 = (navigator.userAgent.indexOf("MSIE 6.0") > 0) ? 1 : 0;
var isIE5 = (navigator.userAgent.indexOf("MSIE 5") > 0) ? 1 : 0;
var isIE = (isIE6 || isIE5)? 1 : 0;
var isMozilla = (navigator.userAgent.indexOf("Gecko") > 0) ? 1 : 0;
var isOpera5 = bInfo('Opera 5');
var isOpera6 = bInfo('Opera 6');




var msg_div_scrolling = 0;
cursor_hand = (isIE)? "hand" : "pointer";



<!-- Global Javascript Functions... -->


// this func will select a country from the COUNTRY_DD based on the country ISO code passed in
// NOTE: the country DD php code muct be included for this to work!
function selectCountryDD(cCode) {
	var dd = document.getElementById('COUNTRY');
	var success = -1;
	if (dd) {
		for (i=0; i < dd.options.length; i++) {
			if (dd.options[i].value == cCode) {
				success = i;
			}
		}
	dd.selectedIndex = success;	
	}
}


function checkRequiredFields(formName) {
	theFormObj = document.getElementById(formName);
	allControls = theFormObj.elements;
	success = true;
		for (u=0; u < allControls.length; u++) {
			if (allControls[u].className == "control-required") {
				obj = allControls[u];
				if (obj.value.length < 1) {
					success = false;
				}
			}
		}
	if (!success) {
		alert("Please complete all required fields before continuing.");
	} else {
		theFormObj.submit();
	}
	
}

function submitNamedForm(formName) {
	theFormObj = document.getElementById(formName);
	if (theFormObj == null) {
		alert("DEBUG: Could not find form to submit: " + formName);
	}
	theFormObj.submit();
}

//general browser functionality method...
function bInfo(what){

 if(!what) return navigator.appName+'; '+ navigator.userAgent;
// no arguments passed get the return case sensitive

 var agent= navigator.userAgent.toLowerCase();
// otherwize the uA is made lower case for comparisons

 var lcWhat=what.toLowerCase();

 switch (lcWhat){
  case 'df': return !!(document.createDocumentFragment);
  case 'dom': return !!(document.createElement);
  case 'id': return !!(document.getElementById);
  case 'ie': return !!(agent.indexOf('msie')!= -1);
  case 'moz': return !!(agent.indexOf('gecko')!= -1);

  case 'agt': return agent;
  default: return !!(agent.indexOf(lcWhat)!=-1);
 }
}

function swap_img(obj, path) {
	obj.src = path;
}


//reloads the window if Nav4 resized
function reloadPage(init) { 
  if (init==true) with (navigator) {
  	if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    	document.pageWidth=innerWidth;
		document.pageHeight=innerHeight;
		onresize=reloadPage;
		}
	} else
	if (innerWidth!=document.pageWidth || innerHeight!=document.pageHeight)
		location.reload();
}



// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}



function clearCursor(obj) {
	obj.style.cursor = 'default';
	
}



// Capture Mouse Position Code...
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

if (isOpera6) {
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (isIE) {
    document.onmousemove = captureMousePosition;
} else if (isMozilla) { 
    document.onmousemove = captureMousePosition;
}

function captureMousePosition(e) {
    if (isOpera6) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (isIE) {
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (isMozilla) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
}

//this finds objects inside a form by id name
//set this up due to a strange bug I have found in Mozilla where you aren't able to
//get checkboxes by ID name reliably using getElementById
function myFindFormObject(theForm, objID) {
	elms = document.getElementById(theForm).elements;
	for (u=0; u<elms.length; u++) {
		if (elms[u].id == objID) {
			obj = elms[u];				
		}
	}

	return(obj);
}

/*
	Custom popup hints, similar to alt tags in IE, will work in all supported browsers
*/
//alt div
document.write("<div id='altdiv' name='altdiv' class='altdiv' style='left:-500px; top:-500px; z-index:9999; visibility: visible;'></div>");
document.write("<div id='altdivdrop' name='altdivdrop' class='altdiv' style='left:-500px; top:-500px; z-index:9998; visibility: visible; color:#777777; background-color:#777777; border:0px;'></div>");

isAltOpen = false;
altOpenCount = 0;
altDisplayLen = 400;
pauseCount = 0;
pauseLen = 100;
countdownStopped = false;

function setAltPos(x, y) {
	//position the text div for custom alt popup
	altDiv = document.getElementById("altdiv");	
	altDiv.style.top = y + 20;
	altDiv.style.left = x + 5;
	//set up drop shadow
	dropShiftAmt = 3;
	altDivDrop = document.getElementById("altdivdrop");
	altDivDrop.style.top = altDiv.offsetTop + dropShiftAmt;
	altDivDrop.style.left = altDiv.offsetLeft + dropShiftAmt;
}

function doAlt() {
	setAltPos(xMousePos, yMousePos);
	isAltOpen = true;
	updateAlt();
}

function countdownAlt() {
	pauseCount++;
	if (pauseCount == pauseLen) {
		pauseCount = 0;
		doAlt();
	} else {
		if (countdownStopped == false) {
			setTimeout('countdownAlt()', 10);		
		}
	}
}

function showAlt(theText) {
	altDiv = document.getElementById("altdiv");	
	altDiv.innerHTML = theText;
	altDivDrop = document.getElementById("altdivdrop");
	altDivDrop.innerHTML = theText;
	pauseCount = 0;
	countdownStopped = false;
	countdownAlt();
}

function updateAlt(){
	if (isAltOpen == true) {
		setAltPos(xMousePos, yMousePos);
		setTimeout('updateAlt()', 10);
		altOpenCount++;
		if (altOpenCount == altDisplayLen) {
			closeAlt();
		}
	}
}

function closeAlt() {
	isAltOpen = false;
	countdownStopped = true;
	pauseCount = 0;
	altOpenCount=0;
	setAltPos(-500, -500);
}




//msg div
document.write("<div id='msg_div' name='msg_div' class='msg_div' style='padding:5px; position:absolute; left:-500px; top:-500px; width:400px; height:150px; z-index:1000; visibility: visible;'></div>");
//blocker div
document.write("<div id='block_div' name='block_div' class='block_div' style='position:absolute; left:-500px; top:-500px; width:1px; height:1px; z-index:999; visibility: visible;'><img src='../images/spacer.gif' name='block_image' id='block_image' height='100%' width='100%'></div>");


function removeDialog() {
	document.getElementById('block_div').style.visibility = 'hidden';
	document.getElementById('msg_div').style.visibility = 'hidden';
	msg_div_scrolling = 0;
}

//a message dialog.  postAction is the function to be executed when OK is clicked.
function msgBox(text, postAction, yesText) {
	if ((yesText == "") || (yesText == null)) {
		yesText = "&&I18N: OK&&";
	}

	alert_action_ok = "removeDialog(); " + postAction;

	html = '<table background="../images/yes-no-msg-background.gif" style="{background-repeat: no-repeat;}" width="400" height="150" border="0"><tr>';
	html += '<td valign="top" class="msgbox">';
	html += '<div align="center">' + text;

	html += '<form name="msg_form" id="msg_form">';
	html += '<div align="center">';
	html += '<input type="button" onClick="' + alert_action_ok + '" value="' + yesText + '" name="msg_ok">&nbsp;&nbsp;';
	html += '</div>';
	html += '</form>';
	
	html += '</div></td></tr></table>';
	
	document.getElementById('msg_div').innerHTML = html;
	document.getElementById('msg_div').style.visibility = 'visible';
	document.getElementById('block_div').style.visibility = 'visible';

	document.msg_form.msg_ok.focus();
	
	if (!msg_div_scrolling) {
		scroll_msg_div();
	}


}


//a yes/no dialog.  postAction is the function to be executed when OK is clicked.
function choiceBox(text, postAction, yesText, noText) {
	if ((yesText == "") || (yesText == null)) {
		yesText = "&&I18N: OK&&";
	}
	if ((noText == "") || (noText == null)) {
		noText = "&&I18N: Cancel&&";
	}

	alert_action_ok = "removeDialog(); " + postAction;
	alert_action_cancel = "removeDialog();";

	html = '<table background="../images/yes-no-msg-background.gif" style="{background-repeat: no-repeat;}" width="400" height="150" border="0"><tr>';
	html += '<td valign="top" class="msgbox">';
	html += '<div align="center">' + text;

	html += '<form name="msg_form" id="msg_form">';
	html += '<div align="center">';
	html += '<input type="button" onClick="' + alert_action_ok + '" value="' + yesText + '" name="msg_ok">&nbsp;&nbsp;';
	html += '<input type="button" onClick="' + alert_action_cancel + '" value="' + noText + '" name="msg_cancel">';
	html += '</div>';
	html += '</form>';
	
	html += '</div></td></tr></table>';
	
	document.getElementById('msg_div').innerHTML = html;
	document.getElementById('msg_div').style.visibility = 'visible';
	document.getElementById('block_div').style.visibility = 'visible';

	document.msg_form.msg_ok.focus();
	
	if (!msg_div_scrolling) {
		scroll_msg_div();
	}


}

function progress_box(text, postAction, show_ok_button) {
	alert_action = "removeDialog(); " + postAction;

	html = '<table background="../images/progress-box-background.gif" style="{background-repeat: no-repeat;}" width="400" height="150" border="0"><tr>';
	html += '<td valign="top" class="msgbox"><br />';
	html += '<div align="center">' + text;
	if (show_ok_button) {
		html += '<form name="msg_form" id="msg_form">';
		html += '<div align="center"><input type="button" onClick="' + alert_action + '" value="&&I18N: OK&&" name="msg_ok"></div>';
		html += '</form>';
	}
	
	html += '</div></td></tr></table>';
	
	document.getElementById('msg_div').innerHTML = html;
	document.getElementById('msg_div').style.visibility = 'visible';
	document.getElementById('block_div').style.visibility = 'visible';

	if (show_ok_button) {
		document.msg_form.msg_ok.focus();
	}
	
	if (!msg_div_scrolling) {
		scroll_msg_div();
	}

}

function scroll_msg_div() {

	msg_div_scrolling = 1;

	if (isIE5) {

		var YPageOffset = document.body.scrollTop;
		var XPageOffset = document.body.scrollLeft;
		var YPageSize = document.body.clientHeight;
		var XPageSize = document.body.clientWidth;

	} else if (isIE6) {

		var YPageOffset = document.body.scrollTop;
		var XPageOffset = document.body.scrollLeft;
		var YPageSize = document.body.clientHeight;
		var XPageSize = document.body.clientWidth;

	} else if (isMozilla) {

		var YPageOffset = window.scrollY;
		var XPageOffset = window.scrollX;
		var YPageSize = window.innerHeight;
		var XPageSize = window.innerWidth;


	}

	if (isNaN(YPageOffset)) YPageOffset = 0;
	if (isNaN(XPageOffset)) XPageOffset = 0;
	if (isNaN(YPageSize)) YPageSize = 0;
	if (isNaN(XPageSize)) XPageSize = 0;


	var setXLoc = (XPageSize / 2) - (parseInt(document.getElementById('msg_div').style.width) / 2) + XPageOffset - 10;
	var setYLoc = (YPageSize / 2) - (parseInt(document.getElementById('msg_div').style.height) / 2) + YPageOffset;

	document.getElementById('msg_div').style.top = setYLoc + 'px';
	document.getElementById('msg_div').style.left = setXLoc + 'px';


	//set up the "click block" div that will keep additional buttons from being clicked until the box is dismissed.
	document.getElementById('block_div').style.top = '0px';
	document.getElementById('block_div').style.left = '0px';
	document.getElementById('block_div').style.width = XPageSize + 'px';
	document.getElementById('block_div').style.height = YPageSize + 'px';
	document.getElementById('block_image').width = XPageSize;
	document.getElementById('block_image').height = YPageSize;



	if (msg_div_scrolling)
		setTimeout('scroll_msg_div()', 20);

}


// Function to pop-up a centered window with specified parameters
function centeredWindow(link,winName,H,W,resizable,scrollbars,status) {
	var iMyWidth;
	var iMyHeight;
	if (!resizable) resizable = 'no';
	if (!scrollbars) scrollbars = 'no';
	if (!status) status = 'no';

	iMyWidth = (window.screen.width/2) - (W/2);
	iMyHeight = (window.screen.height/2) - (25 + H/2);
	var winout = window.open(link,winName,"height="+H+",width="+W+",resizable="+resizable+",left="+iMyWidth+",top="+iMyHeight+",screenX="+iMyWidth+",screenY="+iMyHeight+",scrollbars="+scrollbars+",status="+status);
	winout.focus();
}


//opens a new browser window, and returns the reference to it
function openBrowser(url,windowName,height,width,menubar,toolbar,resizable,scrollbars,status) {
	if (!menubar) menubar = 'no';
	if (!resizable) resizable = 'no';
	if (!scrollbars) scrollbars = 'no';
	if (!status) status = 'no';
	if (!toolbar) toolbar = 'no';
	theWindow = window.open(url, windowName, 'height='+height+',width='+width+',menubar='+menubar+',resizable='+resizable+',scrollbars='+scrollbars+',status='+status+',toolbar='+toolbar);
	return theWindow;
}




// For programming purposes only --> gets all element attributes :)
function inspect(elm,show){
	x='<font size="+1" color="#CC0000"><b>Inspecting ' + elm.name + '</b></font><br>';
	count = 0;
	
	for (var key in elm) {
		if (count == 0) {
			x += "<div class='inspector' style='background-color:#EEEEEE; height:15px'><b>&nbsp;"+key+"</b>: "+elm[key]+"</div>";
			//x += "<div style='height:1px;background-color:#AAAAAA;font-size:1px;'></div>\n";	
			count ++;
		} else {
			x += "<div class='inspector' style='background-color:#DDDDDD; height:15px'><b>&nbsp;"+key+"</b>: "+elm[key]+"</div>";
			//x += "<div style='height:1px;background-color:#AAAAAA;font-size:1px;'></div>\n";				
			count = 0;
		}
	}
	div = document.createElement('DIV');
	div.style.position = 'absolute';
	div.style.top = '0px';
	div.style.left = '0px';
	div.style.width = '1000px';
	div.style.height = '400px';
	div.style.overflow = 'auto';
	div.style.backgroundColor = '#DEDEDE';
	div.innerHTML = x;
	div.style.zIndex = 999;
	
	if (show)
		document.body.appendChild(div);
}




/*
	Macromedia functions...
*/

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);




///security and md5 stuff

/*
 * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
 * Digest Algorithm, as defined in RFC 1321.
 * Version 2.0 Copyright (C) Paul Johnston 1999 - 2002.
 * Other contributors: Greg Holt, Ydnar
 * Distributed under the BSD License
 * See http://pajhome.org.uk/crypt/md5 for more info.
 */

/*
 * Configurable variables. You may need to tweak these to be compatible with
 * the server-side, but the defaults work in most cases.
 */
var hexcase = 0;  /* hex output format. 0 - lowercase; 1 - uppercase        */
var b64pad  = ""; /* base-64 pad character. "=" for strict RFC compliance   */
var chrsz   = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode      */

/*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base-64 encoded strings
 */
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }

/* Backwards compatibility - same as hex_md5() */
function calcMD5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}

/* 
 * Perform a simple self-test to see if the VM is working 
 */
function md5_vm_test()
{
  return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}

/*
 * Calculate the MD5 of an array of little-endian words, and a bit length
 */
function core_md5(x, len)
{
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;
  
  var a =  1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d =  271733878;

  for(var i = 0; i < x.length; i += 16)
  {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;
 
    a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = md5_ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i+15], 22,  1236535329);

    a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = md5_gg(c, d, a, b, x[i+11], 14,  643717713);
    b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = md5_gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = md5_gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);

    a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = md5_hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i+15], 16,  530742520);
    b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = md5_ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = md5_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 Array(a, b, c, d);
  
}

/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md5_cmn(q, a, b, x, s, t)
{
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md5_ff(a, b, c, d, x, s, t)
{
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md5_gg(a, b, c, d, x, s, t)
{
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md5_hh(a, b, c, d, x, s, t)
{
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md5_ii(a, b, c, d, x, s, t)
{
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

/*
 * Calculate the HMAC-MD5, of a key and some data
 */
function core_hmac_md5(key, data)
{
  var bkey = str2binl(key);
  if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);

  var ipad = Array(16), opad = Array(16);
  for(var i = 0; i < 16; i++) 
  {
    ipad[i] = bkey[i] ^ 0x36363636;
    opad[i] = bkey[i] ^ 0x5C5C5C5C;
  }

  var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  return core_md5(opad.concat(hash), 512 + 128);
}

/*
 * 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 bit_rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}

/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
 */
function str2binl(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  return bin;
}

/*
 * Convert an array of little-endian words to a hex string.
 */
function binl2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "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 base-64 string
 */
function binl2b64(binarray)
{
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i += 3)
  {
    var triplet = (((binarray[i   >> 2] >> 8 * ( i   %4)) & 0xFF) << 16)
                | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
                |  ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
    for(var j = 0; j < 4; j++)
    {
      if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
      else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
    }
  }
  return str;
}
