/**  
 *  Autodisable forms after a submit preventing doublesubmit (uses the Prototype JavaScript framework (version 1.4.0))
 */

var LightboxForm = Class.create();
LightboxForm.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function() {
	},

	addInstance: function(form) {
		form = $(form);
		Event.observe(form, 'submit', this.disableWithOverlay.bind(this));
	},
	
	uninstall: function(form) {
		form = $(form);
		Event.stopObserving(form, 'submit', this.disableWithOverlay.bind(this));
	},

	disableWithOverlay: function() {
        this.activate();
        return true;
	},
	
	// 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){
		var bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		var 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");
	}
}

var lightboxFormObj = new LightboxForm();

/**
 * Install the hook on all the forms in the page.
 */	
function hidePreventDoubleSubmitScreen() {
	lightboxFormObj.deactivate();
}

/**
 * Install the hook on all the forms in the page.
 */	
function installPreventDoubleSubmitHook() {
	
	var allForms = document.forms;
	for(i = 0; i < allForms.length; i++) {
		var form = allForms[i];
		lightboxFormObj.addInstance(form);
	}

	// 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.
	var bod     = document.getElementsByTagName('body')[0];
	var overlay = document.createElement('div');
	overlay.id  = 'overlay';
	bod.appendChild(overlay);

	var message       = document.createElement('div');
	message.id        = 'lightbox';
	message.className = 'leightbox';
	message.innerHTML = '<div id="submit_dialog_progress" style="margin-left: 40px; margin-top: 20px;">Please Wait... <img src="/assets/images/aload.gif"></div>';
	bod.appendChild(message);
}

/*-----------------------------------------------------------------------------------------------*/

// Event.observe(window, 'load', installPreventDoubleSubmitHook, false);
 Event.observe(window, 'load', installPreventDoubleSubmitHook);

/*-----------------------------------------------------------------------------------------------*/

