var submit_confirm=true;

function confirmExit (evt) {
  if (!submit_confirm) {
	return;
  }
  var message = "ATTENTION: toute donnée non sauvegardée sera perdue !";
  if (typeof evt == 'undefined') {
    evt = window.event;
  }
  if (evt) {
    evt.returnValue = message;
  }
  return message;
}
/*
function confirmExit() {
  if (!submit_confirm) {
	return;
  } 
  return "ATTENTION: toute donnée non sauvegardée sera perdue !";
}
*/

function submitForm(form){
	submit_confirm = false;
	document.getElementById(form).submit();
	return false;
}

function enterSubmitForm(form, e){
	if (checkEnter(e)) {
		return submitForm(form);
	} else {
		return true;
	}
}

function enterSetValsSubmitForm(form, vals, e){
	if (checkEnter(e)) {
		return setValsSubmitForm(form, vals)
	} else {
		return true;
	}
}

function setValsSubmitForm(form, vals){
	var idx;
	for (idx=1; idx<vals.length; idx+=2) {
		document.getElementById(vals[idx-1]).value = vals[idx];
	}
	return submitForm(form);
}

function checkEnter(e) { //e is event object passed from function invocation
	//exit if called by a 'select'
	var elt;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
		e=window.event;
		elt=e.srcElement;
	} else {
		elt=e.target;
	}
	if(elt.type!= undefined && elt.type.toLowerCase().indexOf('select') != -1) {
		return false;
	}
	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		characterCode = e.which //character code is contained in NN4's which property
	} else{
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		return true;
	} else{
		return false;
	}
}

//------------------------------------------------------------------------
function showHideLastRow (name) {
	var rows=document.getElementById(name).rows;
	field=rows[rows.length-1];
	if (field.style.display == 'none') {
		field.style.display = '';	//table-row...
	} else {
		field.style.display = 'none';
	}
}


//----------------------------------------------------------------------------
//var keybYN = new keybEdit('yn','Valid values are \'Y\' or \'N\'.');
var keybNumeric = new keybEdit('01234567890','Veuillez saisir un nombre entier.');
var keybAlpha = new keybEdit('abcdefghijklmnopqurstuvwxy ','Veuillez saisir un texte sans chiffres.');
var keybAlphaNumeric = new keybEdit('abcdefghijklmnopqurstuvwxy01234567890 ','Veuillez saisir une valeur alpha-numérique.');
var keybDecimal = new keybEdit('01234567890.','Veuillez saisir un nombre.');
var keybDate =  new keybEdit('01234567890/','Veuillez saisir une date');
var keybDateTime =  new keybEdit('01234567890/: ','Veuillez saisir une date/heure');
var keybTime =  new keybEdit('01234567890:','Veuillez saisir une heure');
//var keybYNNM = new keybEdit('yn');
var keybNumericNM = new keybEdit('01234567890');
var keybAlphaNM = new keybEdit('abcdefghijklmnopqurstuvwxy ');
var keybAlphaNumericNM = new keybEdit('abcdefghijklmnopqurstuvwxy01234567890 ');
var keybDecimalNM = new keybEdit('01234567890.');
var keybDateNM = new keybEdit('01234567890/');
var keybDateTimeNM =  new keybEdit('01234567890/: ');
var keybTimeNM =  new keybEdit('01234567890:');

function keybEdit(strValid, strMsg) {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function is to be a constructor for
						the keybEdit object.  keybEdit objects are used by the
						function editKeyBoard to determine which keystrokes are
						valid for form objects.  In addition, if an error occurs,
						they provide the error message.
						
						Please note that the strValid is converted to both
						upper and lower case by this constructor.  Also, that
						the error message is prefixed with 'Error:'.
						
						The properties for this object are the following:
							valid	=	Valid input characters
							message	=	Error message
							
						The methods for this object are the following:
							getValid()	=	Returns a string containing valid
											characters.
							getMessage()=	Returns a string containing the
											error message.

		Update Date:	Programmer:			Description:
	*/

	//	Variables
	var reWork = new RegExp('[a-z]','gi');		//	Regular expression\

	//	Properties
	if(reWork.test(strValid))
		this.valid = strValid.toLowerCase() + strValid.toUpperCase();
	else
		this.valid = strValid;

	if((strMsg == null) || (typeof(strMsg) == 'undefined'))
		this.message = '';
	else
		this.message = strMsg;

	//	Methods
	this.getValid = keybEditGetValid;
	this.getMessage = keybEditGetMessage;
	
	function keybEditGetValid() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getValid method
						for the keybEdit object.  Please note that most of the
						following logic is for handling numeric keypad input.

		Update Date:		Programmer:			Description:
	*/

		return this.valid.toString();
	}
	
	function keybEditGetMessage() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getMessage method
						for the keybEdit object.

		Update Date:	Programmer:			Description:
	*/
	
		return this.message;
	}
}




function editKeyBoard(objForm, objKeyb, evt) {
	/*	Function:		editKeyBoard
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function is to edit edit keyboard input
						to determine if the keystrokes are valid.
	
		Update Date:		Programmer:			Description:
	*/
	
	var characterCode;
	var ie_code=false;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
		evt = window.event;
		characterCode = evt.keyCode //character code is contained in IE's keyCode property
		ie_code=true;
    } else {
		characterCode = evt.which //character code is contained in NN4's which property
		if (characterCode == 0 || characterCode == 8) return true;	//allows function keys and backspace
		ie_code=false;
	}

	strWork = objKeyb.getValid();
	strMsg = '';							// Error message
	blnValidChar = false;					// Valid character flag

	// Part 1: Validate input
	if(!blnValidChar)
		for(i=0;i < strWork.length;i++)
			if(characterCode == strWork.charCodeAt(i)) {
				blnValidChar = true;
				break;
			}

	// Part 2: Build error message
	if(!blnValidChar) {
		if(objKeyb.getMessage().toString().length != 0)
			alert('Erreur: ' + objKeyb.getMessage());

		if (ie_code) {					// Clear invalid character
			evt.returnValue = false;
		} else {
			evt.preventDefault();
		}
		objForm.focus();				// Set focus
		return false;
	}
	return true;
}


