/////////////////////////////////////////////////////////////////////////
//
// wire document.forms[0] with double check safe onsubmit action
// preserving calls to original onsubmits
//
//
// in html files users can write dblSubmitCustomCheck to overwrite default check 
// for double submit, dblSubmitCustomClearCheck to clear custom check
// and dlbSubmitCustomMsg to provide custom alert
//
//////////////////////////////////////////////////////////////////////////

var submitted = false;

function debug(str){
	if(typeof forms_debug!='undefined'){
		alert(str);
	}
}

function dblSubmitCheck(){
	if(typeof dblSubmitCustomCheck != 'undefined'){
		return dblSubmitCustomCheck();  // true = good to submit
	} else {
		if(submitted) return false;
		submitted = true;
		return true;
	}
}

function dblSubmitClearCheck(){
	if(typeof dblSubmitCustomClearCheck != 'undefined'){
		dblSubmitCustomClearCheck();
	} else {
		submitted = false;
	}
}

function dblSubmitMsg(){
	if(typeof dblSubmitCustomMsg != 'undefined'){
		dblSubmitCustomMsg();
	}else{
		alert("You have already submitted this page. Please wait for server response");		
	}
}

function dblClickSafeFormSubmit() {

	if(!document.forms || !document.forms[0]){
		return false;
	}
	
	if(document.forms[0].safe2submit && document.forms[0].safe2submit==true){
		return true;
	}

	var result = false;
	result = dblSubmitCheck();

	if(result == false){
		dblSubmitMsg();
	}
	return result;
}



function cleanUp(unload){
	if(unload==null){
		unload = true;
	}
	dblSubmitClearCheck();
	if(unload && document.body.origonunload){
		document.body.origonunload();
	}
}

function wireDblClickSafeFormSubmit(){
	if(document.forms && document.forms[0]){
		if(typeof document.forms[0].origsubmit == 'undefined'){
			document.forms[0].origsubmit = document.forms[0].submit;
			document.forms[0].submit=function(){ 
				if(dblClickSafeFormSubmit()){
					var res = document.forms[0].origsubmit();
					if(res==false){
						dblSubmitClearCheck();
					}
					return res;
				} else {
					return false;
				} 
			};
		}
		if(typeof document.forms[0].origonsubmit == 'undefined'){
			document.forms[0].origonsubmit = document.forms[0].onsubmit;
			document.forms[0].onsubmit=function(){ 
				if(dblClickSafeFormSubmit()){
					var res = document.forms[0].origonsubmit();
					if(res==false){
						dblSubmitClearCheck();
					}
					return res;
				} else {
					return false;
				} 
			};
		}
	}
	if(typeof document.body.origonunload == 'undefined'){
		document.body.origonunload=document.body.onunload;
	}
	var origUnload=document.body.getAttribute("onUnload");
	if(origUnload==null){
		origUnload="";
	}
	document.body.setAttribute("onUnload", "cleanUp(); "+origUnload );
	document.body.onunload=function(){ cleanUp(); };
	dblSubmitClearCheck();
}

function guardLink(id,msg){
	if(!document.guardedLinks){
		document.guardedLinks = new Object();
	}
	if(!document.guardedLinks[id]){
		document.guardedLinks[id]="guarded";
	} else if(document.guardedLinks[id]=="guarded") {
		if(msg){
			alert(msg);
		}
		return false;
	}
	document.guardedLinks[id]="guarded";
	return true;
}

wireDblClickSafeFormSubmit();
