// JavaScript Document
/*
*This code is used to encode inputs within form elements before submitting them
*To use this code put:
	<script type="text/javascript" src="md5.js"></script>
	<script type="text/javascript" src="EncodeForms.js"></script>
	<script type="text/javascript">
	<!--
	window.onload = function ()
	{
		//Whatever else you need
		fixAllEncodedForms(document);
		//Stuff here too depending
	}
	//-->
	</script>
*in your <head> section, and add Encoded="true" in all of your input tags that you would like to encode
*
*If you don't want the encoded fields to be submittable if javascipt is disabled add disabled to their tags
*the scipt will re-enable them and make sure that the form cannot be submitted without encoding first
*
*I used md5.js for the function hex_md5() See http://pajhome.org.uk/crypt/md5 for more info.
*but this can be easily replaced as it is only called once
*/
function encodeSubtree(root)			//encodes all the "Encoded" elements within root recursivly
{
	if(root.tagName == "INPUT" && root.className.match(/(?:^| )Encoded(?: |$)/))	//if an "Encoded" Element
		root.value=hex_md5(root.value);							//encode it
	else if(root.childNodes)									//otherwise recurse
	{
		for(var i=0;i<root.childNodes.length; i++)
			encodeSubtree(root.childNodes[i]);
	}
}

function encodeAndSubmit(form)				//this is called from the old submit input (now a button)
{
	encodeSubtree(form);					// recersivly encode all the nessisary inputs
	form.submit();							// and submit
}

function fixEncodedForm(root)
{
	if(root.tagName == "INPUT" && root.className.match(/(?:^| )Encoded(?: |$)/))             //re-enable all of the disabled encoded feilds
	{
		root.disabled = false;
		root.onkeypress = function(e) {if (!e) e = window.event; if (e.keyCode == 13) encodeAndSubmit(this.form);}
	}
	else if(root.tagName == "INPUT" && root.type.toLowerCase() == "submit") //this disables the submit button so if the user
	{																		//disables javascript no password will be sent
		if(document.all)//If IE: the stupid things we have to do for mircosoft :(
		{
			root.form.appendChild(document.createElement('<input type="hidden" value="'+root.value+'" name="'+root.name+'" />'));
			root.parentNode.replaceChild(document.createElement('<input type="button" value="'+root.value+'" title="'+root.title+'" onclick="encodeAndSubmit(this.form);" />'),root);
		}
		else
		{
			var submit_sub = root.cloneNode(true);				//copy the submit button so that whatever was being submitted
			root.type = "button";							// will still be submitted by the sub, the real one is changed
			root.onclick = function(){encodeAndSubmit(this.form);}	// to a button so it can olny submit via js with encodeAndSumbmit
			submit_sub.type = "hidden";						// make the copy hidden
			root.form.appendChild(submit_sub);				// add the copy to the form
		}
	}
	else if(root.childNodes)							// nothing exciting, recurse
	{
		for(var i=0;i<root.childNodes.length; i++)
			fixEncodedForm(root.childNodes[i]);
	}
}

function fixAllEncodedForms(doc)					//recusivly looks in doc for forms with "Encoded" children and "fixes" them
{
	if(doc.tagName && doc.className.match(/(?:^| )Encoded(?: |$)/))  //if item is encoded return true (this will be used to identifiy
		return true;								// a form that has encoded elements
	else if(doc.childNodes)							//if it has children we need to recursivly search them
	{
		var returnVal = false;						//used to see if we found an encoded child
		for(var i=0;i<doc.childNodes.length; i++)	//loop though children
			if(fixAllEncodedForms(doc.childNodes[i]))	//if any of them had encoded elements
			{
				returnVal = true;						//say we found one
				break;									//and stop looking
			}
		if(doc.tagName == "FORM" && returnVal)			//if this is the form that an encoded element was found in
		{
			fixEncodedForm(doc);					//"fix" all the elements in the form
			javascript_flag = document.createElement("input");	//add a hidden "javascriptEnabled" input to the form
			javascript_flag.type = "hidden";					//this allows the user of the form to see if javascript
			javascript_flag.name = "javascriptEnabled";			// was enabled, and thus the form encoded
			javascript_flag.value = "true";
			doc.appendChild(javascript_flag);
			return false;							//we don't want to stop looking in the form's parent
		}
		return returnVal;							//pass the return value up if you're not a form
	}
	return false;									//dead end but don't stop looking.
}
if (window.addEventListener)
	window.addEventListener('load',function(){fixAllEncodedForms(document);},false);
else if (window.attachEvent)
	window.attachEvent('onload',function(){fixAllEncodedForms(document);});