/**  
 *  Autodisable forms after a submit preventing doublesubmit (uses the Prototype JavaScript framework (version 1.5.0))
 *  (c) 2006 Pierpaolo Follia <pfollia@gmail.com>
 *
 *  This cado is freely distributable under the terms of an MIT-style license.
 *
 *  For details, see the my web site: http://madchicken.altervista.org/tech/2006/10/using-lightbox-to-prevent-form-double.html
 *  Version: 2.0
 */
var PreventDoubleSubmit = Class.create();
PreventDoubleSubmit.prototype = {
	initialize: function(message) {
        if(message)
            this.message = message;
        else
            this.message = '<p><h3>Please wait...Loading</h3></p><div class="alert_progress" id="modal_dialog_progress"></div>';
	    Event.observe(window, 'load', this.installHook.bindAsEventListener(this), false);
	},	

	/**
	 * Install the hook on all the forms in the page.
	 */	
	installHook: function(event) {
		var allForms = document.forms;
		for(i = 0; i < allForms.length; i++) {
			var form = allForms[i];
			new LightboxForm(form);
		}
		this.addLightboxMarkup();
	},
	
	// Add in markup necessary to make this work. Basically two divs:
	// Overlay holds the shadow
	// Lightbox is the centered square that the content is put into.
	addLightboxMarkup: function() {
		bod 				= document.getElementsByTagName('body')[0];
		overlay 			= document.createElement('div');
		overlay.id			= 'overlay';
		bod.appendChild(overlay);
	
		message 			= document.createElement('div');
		message.id			= 'lightbox';
		message.className	= 'leightbox';
		message.innerHTML = this.message;
		bod.appendChild(message);
	}
};


var LightboxForm = Class.create();
LightboxForm.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(form) {
		form = $(form);
		form.oldsubmitdoublesubmit = form.submit;
		form.oldonsubmitdoublesubmit = form.onsubmit;
		form._lightbox_ = this;

		form.onsubmit = this.disableWithOverlay.bindAsEventListener(form);
		form.submit = this.disableWithOverlayJS;
	},
	
	uninstall: function(form) {
		form = $(form);
		form.submit = form.oldsubmitdoublesubmit;
		form.onsubmit = form.oldonsubmitdoublesubmit;
	},

	disableWithOverlay: function(event) {
		if((this.oldonsubmitdoublesubmit && this.oldonsubmitdoublesubmit.apply(this) != false)&&(event && event.returnValue != false)) {
			this._lightbox_.activate();
			return true;
		} else {
			return false;
		}
	},

	disableWithOverlayJS: function(event) {
		this._lightbox_.activate();
		return this.oldsubmitdoublesubmit();
	},

	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displayLightbox: function(display){
		$('overlay').style.display = display;
		$('lightbox').style.display = display;
	},
	
	deactivate: function(){
		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.hideSelects("visible");
		}
		
		this.displayLightbox("none");
	}
}



