/**
 * Replacing inputs of types 'button' and 'submit' in order
 * to get the correct styles in non-Mozilla browsers.
 * The 'applyFix' method searches through the entire document
 * for all input elements. Then it alters the elements of type
 * 'submit' by replacing them with a 'hidden' input and adding
 * an anchor tag with rel='formSubmit' and the id of the
 * element as the target href.
 * Additionally a handler is added to the ClickHandler. This
 * handler will recognize the new type of links, search for
 * the target input-element, enable it and submit the form.
 * By enabling the element it's value will be added to the
 * submition of the form, just like it would have been when
 * you had clicked on the 'submit' button.
 */
var FixInputs = {
	typeRE:/submit/i,
	init:Events.attach(window, "load", function() {
		FixInputs.applyFix();
		ClickHandler.addHandler("formSubmit", FixInputs.handleElement);
		Events.detach(FixInputs.init);
	}),
	/**
	 * Searches through all the inputs and targets the
	 * type='submit' for fixing.
	 */
	applyFix:function() {
		var de = document.body||document.documentElement;
		if (!de || de.fixInputsApplied) return //Either browser support is shaky, or fix has already been applied.
		de.fixInputsApplied = true;
		var inputs = document.getElementsByTagName("input"); //Get all inputs
		var rejects = new Array(); //Collect the buttons that got ousted.
		for (var i = 0; i < inputs.length; i++) {
			var input = inputs[i];
			var isSubmit = this.typeRE.test(input.type);
			if (isSubmit) {
				if (this.fixElement(input)) rejects[rejects.length] = input; //Only 'fix' submit-button
			}
		}
		for (var i=rejects.length - 1; i >=0 ; i--) { //Stuffing them away in reverse order, any button with 'default' as part of it's className will get preferential treatment
			var reject = rejects[i];
			var dmp = this.getDimensionPocket(reject.form);
			if (reject.parentNode.className.indexOf("default") > -1 && !reject.disabled && null!=dmp.firstChild) dmp.insertBefore(reject, dmp.firstChild);
			else dmp.appendChild(reject);
			
		}
	},
	getDimensionPocket: function(form) {
		var DMP_ID = form.id + "_inputfixer_dimensional_pocket";
		var element = document.getElementById(DMP_ID);
		if (element==null) {
			element = document.createElement("div");
			element.id = DMP_ID;
			element.style.position = "absolute";
			element.style.width = "0px";
			element.style.height = "0px";
			element.style.overflow = "hidden";
			form.appendChild(element);
		}
		return element;
	},
	/**
	 * Create a link that points to the original submit-button.
	 */
	fixElement:function(element) {
		var anchor = document.createElement("a"); //Create a new anchor
		var elementId = element.id;
		var value = element.value;
		var parent = element.parentNode;

		anchor.href = "#" + elementId; //Set the href to target the input
		anchor.rel = "formSubmit";
		anchor.className = "replacedInputButton";
		anchor.appendChild(document.createTextNode(value)); //Set the text to the text of the submit-button
		anchor.setAttribute("title", value);
		
		parent.appendChild(anchor);
		
		return true;
	},
	/**
	 * Handles clicks on the links generated by the fixElement
	 * method. Searches for the hidden element targetted in the
	 * href, enables it and submits its' form.
	 */
	handleElement:function(target){
		var href = target.href;
		var id = target.href.substr(target.href.indexOf("#") + 1); //Get id
		var input = document.getElementById(id); //Get the input
		if (null==input) {
			alert("Error 349467887.001: Original button was not found, probably a button without id was used! (" + id + ")");
			return true;
		}
		if (input.type == "hidden") return true; /*IE's defunct event cancellation may cause a double-fire*/
		if (input.disabled) return true; //Short-circuit this event if the original input was disabled.

		//Morphing the submit button into a hidden input so that it's value gets posted.
		try { //Try to alter the type (this will fail on IE)
			input.type = "hidden";
		} catch (e) { //IE (as usual) has some issues with altering the type of an element
			//Recreating the form element, for IE's benefit.
			var oldElement = input; //Store a link to the submit-button
			input = document.createElement("input"); //Create a new input
			//Clone the old attributes, except type...
			input.type = "hidden";
			input.value = oldElement.value;
			input.name = oldElement.name;
			oldElement.parentNode.replaceChild(input,oldElement); //Replace the submit with the hidden
			input.id = id;
		}

		input.form.submit(); //Submit the form
		return true; //Cancel the default action of the anchor
		
	}
};

(document.body||document.documentElement).className += " fixinputs_ENABLED";
