/*
 * continueButton.js
 * The file contains methods to prevent user from
 * double-click on the continue buttons.
 *
 * NOTE: The only requirement for a button to inherit the behaviour of the button
 * described in this file is to give its id 'ContinueButton' and attach this file
 * in the header of the source code file.
 */

po_ContinueButton = {

	/*
	 * NOTE: po_ContinueButton.sChangeLangButtonId is the only one exception to the rule of naming double-click continue buttons
	 * The change language button cannot be registered with the same name as all the other buttons (ie. ContinueButton)
	 * because it is being used in header.js and what's more there is a click event registered for this button.
	 */
	sContinueButtonId:'ContinueButton',
	sChangeLangButtonId:'changeLangButId',
	sContinueButtonType:'submit',
	iHitCount:0,
	aContinueButtons:Array,
	oButtonCaller:Object,
		
	init:function() {
	
		if (!document.getElementById || !document.createTextNode) { return; }
		
		var aInputElementsList = document.getElementsByTagName('button');

		po_ContinueButton.aContinueButtons = new Array();	
		po_ContinueButton.aContinueButtons = po_ContinueButton.findSubmitInputElements(aInputElementsList);
					
		po_ContinueButton.registerButtonListeners();		
	},
	
	submitForm:function(e) {
	
		po_ContinueButton.oButtonCaller = DOMHelper.getTarget(e);
	
		if (!po_ContinueButton.iHitCount) {
		
			po_ContinueButton.iHitCount++;
			
			var oButtonForm = po_ContinueButton.findParentElement(po_ContinueButton.oButtonCaller, 'form');
			po_ContinueButton.registerFormListener(oButtonForm);
	
			// Rest of the processing is done on the server side
			po_ContinueButton.addContinueParameter(po_ContinueButton.oButtonCaller.name, oButtonForm);
			oButtonForm.submit;					

			return true;
		}	
		return false;
	},
	
	/*
	 * A hidden parameter must be added to the request.
	 * Otherwise, forms checking for the existence of 'continue button' 
	 * parameter (predominantly using conditional logic)
	 * will redirect the request to the previous page instead of
	 * the next one.
	 */
	addContinueParameter:function(sParamName, oButtonForm) {
		
		var oHiddenContinueParam = po_ContinueButton.createHiddenInputObject(sParamName);
	
		oButtonForm.appendChild(oHiddenContinueParam);
	},
	
	createHiddenInputObject:function(sParamName) {
	
		var oHiddenInput = document.createElement('input');
		
		oHiddenInput.setAttribute('type', 'hidden');
		oHiddenInput.setAttribute('name', sParamName);
		oHiddenInput.setAttribute('value', '');		// it is enough for the parameter to exist
		
		return oHiddenInput;	
	},
	
	/* 
	 * An trick designed for IE only.
	 * The form has to be registered with its own submit event.
	 * Any changes to disabled buttons have to be performed in this method.
	 * If not, the button will be disabled but the form will not be refreshed or 
	 * its submit event will not fire at all.
	 */
	submitFormElement:function() {
		// TODO: A CSS class to be applied.
		po_ContinueButton.oButtonCaller.disabled = true;	
	},
	
	findSubmitInputElements:function(aInputElements) { 
	
		var aButtons = new Array();
		
		for (var j = k = 0; k < aInputElements.length; k++) {
			if (aInputElements[k].getAttribute('type').toLowerCase() == po_ContinueButton.sContinueButtonType && 
				po_ContinueButton.hasValidContinueButtonName(aInputElements[k])) {
				aButtons[j] = aInputElements[k];
				j++;
			} 
		}
		
		return aButtons;
	},
	
	registerFormListener:function(oForm) {
		DOMHelper.addEvent(oForm, 'submit', po_ContinueButton.submitFormElement, false);			
	},
	
	registerButtonListeners:function() {
		for(var i = 0; i < po_ContinueButton.aContinueButtons.length; i++) {
			DOMHelper.addEvent(po_ContinueButton.aContinueButtons[i], 'click', po_ContinueButton.submitForm, false);
		}		
	},
	
	findParentElement:function(oElm, tagName) {
	
		var oElmFormElement = null;
		
		while(oElm = oElm.parentNode) {
			if (oElm.tagName.toLowerCase() != 'form') { continue; }
			oElmFormElement = oElm;
			break;
		}
		
		return oElmFormElement;
	},
	
	hasValidContinueButtonName:function(oInputElement) {
		return (oInputElement.id.toLowerCase() == po_ContinueButton.sContinueButtonId.toLowerCase());
	}
	
}

DOMHelper.addEvent(window, 'load', po_ContinueButton.init, false);