var initFormSerialized;

function isFormModified(form, ignoreNamesFunction) {
	if (typeof form == 'string') {
		form = document.getElementById(form);
	}
//	alert( initFormSerialized );
//	alert( serializeFormData(form, ignoreNamesFunction) );
	if (initFormSerialized != serializeFormData(form, ignoreNamesFunction)) {
		return true;
	} else {
		return false;
	}
}

// Function to be called in the "onload" page event 
// <body onload="checkFormOnExit('mainForm')">
// The first parameter is either the form ID or the form object itself.
// The second one (which may be null) is a function that filters the form fields that must be ignored.

function checkFormOnExit(form, ignoreNamesFunction) {
	if (typeof form == 'string') {
		form = document.getElementById(form);
	}
	// Serialize the form data, to keep a reference
	initFormSerialized = serializeFormData(form, ignoreNamesFunction);
	// Register the function to trigger when page is to be leaved
	window.onbeforeunload = function() {
		if (document.getElementById('_ACTION').value != 'Exporter') {
			showWaiting( 'Veuillez patienter SVP...' );
		}
		if (needToConfirm) {
			// If the current state of form data is different from 
			// the reference serialized state, prompt a message to the user
//			alert( initFormSerialized );
//			alert( serializeFormData(form, ignoreNamesFunction) );
// <DAN>
			// Implique l'ajout d'un champ masqué avec l'id "makeIdFormModifiedTrue" dans Page.html
			if (initFormSerialized != serializeFormData(form, ignoreNamesFunction)
					|| document.getElementById("makeIdFormModifiedTrue").value==1) {
// </DAN>
				hideWaiting( );
				return "Les modifications ne sont pas enregistr\u00E9es et seront perdues.";
			}
		}
	}
}

// Function to be called on the "Save" button, to avoid throwing the message 
function checkFormIgnoreChanges() {
	needToConfirm = false;
}

var needToConfirm = true;
var initFormSerialized = "";

// Compose a string with all the form data, in order to compare easily
// if some data have changed.
// The second parameter (which may be null) is a function that filters the form fields that must be ignored.
function serializeFormData(form, ignoreNamesFunction) {
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
	
	if ( typeof(tinyMCE) != 'undefined' ) {
		tinyMCE.triggerSave(false, false);
	}
	
	var fields = form.elements;
	var s = "";
	for (var i=0; i<fields.length; i++) {
// 	TCH
	  	if (fields[i].name==null || fields[i].name=='') {
			// Ignore fieldsets
			continue;
		}
// 	TCH
//		alert( fields[i].name );
		if (fields[i].nodeType!=1) continue;
		if (ignoreNamesFunction!=null && ignoreNamesFunction(fields[i].name)==true) continue;
		if (fields[i].name.substring(0,7)=='_ACTION') continue;
		if (fields[i].name.substring(0,2)=='__') continue;
	        
		if (fields[i].tagName.toLowerCase()=='input') {
			if (fields[i].type=='' 
				|| fields[i].type.toLowerCase()=='text' 
				|| fields[i].type.toLowerCase()=='password'
				|| fields[i].type.toLowerCase()=='hidden') 
			{
				s = s+ "{"+fields[i].name+"|"+fields[i].value.trim()+"}";
			} else if (fields[i].type.toLowerCase()=='checkbox' 
				|| fields[i].type.toLowerCase()=='radio') 
			{
				if (fields[i].checked) {
					s = s+ "{"+fields[i].name+"|"+fields[i].value.trim()+"}";
				}
			} else if (fields[i].type.toLowerCase()=='submit') {
			} else if (fields[i].type.toLowerCase()=='button') {
			} else if (fields[i].type.toLowerCase()=='file') {
			} else {
				alert("Unexpected input type : "+fields[i].type);
			}
		} else if (fields[i].tagName.toLowerCase()=='select') {
			s = s+= "{"+fields[i].name+"|"+fields[i].value.trim()+"}";
		} else if (fields[i].tagName.toLowerCase()=='textarea') {
			var v = fields[i].value.trim().replace('<br />', '<br>');
			if (v=='<br>') v='';
			s = s+ "{"+fields[i].name+"|"+v+"}";
		} else {
			alert("Unexpected element : "+fields[i].tagName);      
		}
	}
	return s;
}



