/*
	XStandard integration javascript version 4.0
	
	Generalises the javascript necessary to integrate XStandard into a form correctly in FF.
	It assumes that the author has coded the object so that, if the XStandard
	plugin is not installed, the user is presented with a textarea instead.
	It requires an additional attribute for the object, to define what the name of
	the	relevant textarea is.
	
	<object type="application/x-xstandard" ... name="[name_of_input]">
		...
		<textarea name="[name_of_input]">...</textarea>
		...
	</object>
   
    Just add the script to your page and you're set. As IE does NOT require this javascript,
	use a conditional comment to prevent IE to load the script altogether.
   
    <!--[if !IE]>-->
    <script type="text/javascript" src="xstandard.js"></script>
    <!--<![endif]-->
   
    Note: this script assumes that you have no other javascript that needs to run
    on page load or on form submission.
    If you do need to integrate other such scripts, you should consider modifying
    the code (in the two lines that add the simple onsubmit and onload handlers)
    to use something like Keith Gaughan's 
    handy EventManager (which cleverly works around IE's memory leak issue)
    from http://talideon.com/weblog/2005/03/js-memory-leaks.cfm
   
    Advantages of using this script and the above nested object/textarea construct:
   
    - script works with any number of XStandard instances, without any additional coding
    - graceful degradation: works with xstandard + javascript, no xstandard + javascript and no plugin + no javascript
    - degrades in different browsers not currently supported by the XStandard plugin

	Changes from 3.0 to 4.0:
	
	- came to the startling realisation that the whole script is only necessary for Firefox
	  compatibility to work around a bug https://bugzilla.mozilla.org/show_bug.cgi?id=319000
	  IE does NOT need any javascript to function properly - therefore suggest using IE conditional
	  comment to include the script only if the page is NOT loaded in IE; best suggestion is the
	  valid downlevel revealed conditional outlined at http://www.456bereastreet.com/archive/200511/valid_downlevelrevealed_conditional_comments/
	  
      <!--[if !IE]>-->
      <script type="text/javascript" src="xstandard.js"></script>
      <!--<![endif]-->


    Changes from 2.0 to 3.0:
   
    - removed javascript relating to setting EscapeUnicode attribute - to get UTF-8 support, include the relevant PARAM straight in the (X)HTML instead: <param name="EscapeUnicode" value="true" />
    - on submit, the script now checks if the object's value is undefined, rather than empty;
      this allows correct submission of intentionally empty fields (e.g. updating a database entry and removing any content from a field by just deleting the content currently held there)
    - another Firefox bug: when presented with an object containing a nested form control, FF submits *both*. this results in a doubling up of the key/value pair which, in ASP/Coldfusion and other
      server-side languages can cause major problems at the receiving end;
      this is now fixed by programmatically changing the name of the object (appending "_xstandard" to the object's name) after the value has been processed; interestingly, IE behaves correctly and only submits a single value
   
    Changes from 1.0 to 2.0:
   
    - code that determines the event target is now slightly more explicit (rather than using the ternary operator as before)    
    - fixed a subtle bug (feature?) in Firefox: when submitting via the keyboard, the event target that the browser returns
      is the input, rather than the form itself; to work around this, the onsubmit function checks if the target is actually
      a tag with name 'form'; if not, it loops up to the parent node until it either hits the form element or there are no
      more parent nodes (i.e. the root of the DOM has been reached, at which point the rest of the script won't execute anyway)
   
    (C)2005 Patrick H. Lauke aka redux / http://www.splintered.co.uk
   
    Distributed under the GNU General Public License http://www.opensource.org/licenses/gpl-license.php
   
*/

function XStandard_init() {
	// test browser capabilities
	if ((document.getElementsByTagName)&&(document.createElement)&&(document.appendChild)) {
		var i=0,currentForm;
		// get all forms on the page
		var forms = document.getElementsByTagName('form');
		// loop through all forms
		while ((currentForm = forms[i++])) {
			// add onsubmit behaviour (simple version)
			currentForm.onsubmit=XStandard_onsubmit;
		}
	}
}

function XStandard_onsubmit(e) {
	var i=0,currentTextarea,currentObject,newInput;
	// which form triggered the event?
	var form;
	// we could use if (!e) var e = window.event;
	// however it's dubious practice and cause a warning in FF's javascript strict mode
	var ev;
	if(e) {
		ev = e;
	} else if(window.event)	{
		ev = window.event;
	} else {
		ev = null;
	}
	if (ev.target) form = ev.target;
	else if (ev.srcElement) form = ev.srcElement;
	// defeat Safari bug
	if (form.nodeType == 3)	form = form.parentNode;
	// defeat subtle Firefox bug when form submitted via keyboard
	while ((form.tagName.toLowerCase() != 'form')&&(form.parentNode)) {
		form = form.parentNode;
	}
	// build associative array of existing textareas within current form
	var textareas = form.getElementsByTagName('textarea');
	var textareaArray = new Array();
	while ((currentTextarea = textareas[i++])) {
		textareaArray[currentTextarea.name]=currentTextarea;
	}
	// loop through all object elements within current form
	i=0;
	var objects = form.getElementsByTagName('object');
	while ((currentObject = objects[i++])) {
		if ((currentObject.type == 'application/x-xstandard')&&(currentObject.value!=undefined)) {
			if (textareaArray[currentObject.name]) {
				// if there is already a textarea with the required name, inject the value of the plugin
				// (this covers a bug in Firefox, where the textarea nested in the object is still present in the DOM)
				textareaArray[currentObject.name].value = currentObject.value;
				// change the name of the object to avoid duplicate values or value array appearing at receiving end
				// (another aspect of the same bug in Firefox, where the textarea nested in the object is still present in the DOM)
				currentObject.name = currentObject.name+'_xstandard';
			}
		}
	}
	return true;
}


// add init to the document (simple version)
window.onload=XStandard_init;
