//Code JavaScript concernant l'interface Web du projet 
//Auteurs
//	Paul Peltier , Stéphane Bosserdet
//Date de création
//	28/11/2000

//--------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------
//Processus de validation (no applet) - Projet INRS
//--------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------

//Valide la présence de champs associés
//Paramètre
//	strForm - le nom de la notion abordée
//Auteur
//	Paul Peltier
//Date de création
//	28/12/2001
function validObligAssociateFields(
	formValue,
	tblAssociateList)
{
	var tblAssociateDestList;
	var strAssociateSourceFieldName;
	var strAssociateSourceFieldValue;
	var strAssociateDestFieldName;
	var strAssociateDestFieldValue;
	var boolReturn;
	var i;
	var j;

	boolReturn = false;
	
	if (tblAssociateList == null)
		return(boolReturn);

	for(i = 0; i < tblAssociateList.length; i++)
	{
		strAssociateSourceFieldName = tblAssociateList[i][0];
		
		if (fieldExists(formValue, strAssociateSourceFieldName))
		{
			strAssociateSourceFieldValue = formValue.elements[strAssociateSourceFieldName].value
			tblAssociateDestList = tblAssociateList[i][1];
			
			if (strAssociateSourceFieldValue == "")
			{
				//On vide les champs associés (non pas lieu d'être instanciés)
				for(j = 0; j < tblAssociateDestList.length; j++)
				{
					strAssociateDestFieldName = tblAssociateDestList[j];

					if (fieldExists(formValue, strAssociateDestFieldName))
						formValue.elements[strAssociateDestFieldName].value = "";
					if (fieldExists(formValue, strAssociateDestFieldName + "_c"))
						formValue.elements[strAssociateDestFieldName + "_c"].value = "";
				}
			}
			else
			{
				//On vérifie la présence de valeurs dans les champs associés
				for(j = 0; j < tblAssociateDestList.length; j++)
				{
					strAssociateDestFieldName = tblAssociateDestList[j];
		
					if (fieldExists(formValue, strAssociateDestFieldName))
					{
						strAssociateDestFieldValue = formValue.elements[strAssociateDestFieldName].value
						
						if (strAssociateDestFieldValue == "")
						{
							boolReturn = true;
							
							if (fieldExists(formValue, strAssociateDestFieldName + "_c"))
								formValue.elements[strAssociateDestFieldName + "_c"].value = " <- obligatoire";
						}
					}
				}
			}
		}
	}

	return(boolReturn);
}

//--------------------------------------------------------------------------------------------------------------

//Validation d'un formulaire
//Paramètre
//	strForm - le nom de la notion abordée
//Auteur
//	Paul Peltier
//Date de création
//	13/12/2001
function validCommon(
	formValue,
	strForm,
	strLanguage
	)
{
	var tblMailFieldNames;
	var tblNumericFieldNames;
	var tblObligFieldNames;
	var tblAssociateList;
	var tblDateFieldNames;
	
	var strMailFieldSpecificMsg;
	var strNumericFieldSpecificMsg;
	var strDateFieldSpecificMsg;
	var strNotFilledSpecificMsg;
	
	var strMailFieldStandardMsg;
	var strNumericFieldStandardMsg;
	var strDateFieldStandardMsg;
	var strNotFilledStandardMsg;
	
	var strConfirmation;
	var strDocInprogress;
	var strCorrectionList;
	var strDocIcon;
		
	var boolMailField;
	var boolNumericField;
	var boolObligField;
	var boolAssociateField;
	var boolDateFieldNames;

	if (strForm == "")
		strForm = strCurrentForm;
	
	//On initialisa la chaîne de correction
	strCorrectionList = "";

	//Récupération des données
	tblMailFieldNames = 
		getSystemInfo(
			strForm,
			CONST_PTY_MAIL_FIELD_NAMES);
			
	tblNumericFieldNames = 
		getSystemInfo(
			strForm,
			CONST_PTY_NUMERIC_FIELD_NAMES);

	tblObligFieldNames = 
		getSystemInfo(
			strForm,
			CONST_PTY_OBLIG_FIELD_NAMES);
	
	tblAssociateList = 
		getSystemInfo(
			strForm,
			CONST_PTY_ASSOCIATE_FIELD_NAMES);
			
	tblDateFieldNames = 
		getSystemInfo(
			strForm,
			CONST_PTY_DATE_FIELD_NAMES);
	
	//Récupération des messages d'erreur dans la langue indiquée dans le formulaire.
	strMailFieldSpecificMsg = getErrorMsg (CONST_PTY_ERROR_SPECIFIC_MSG, strLanguage, "MailField")
	strNumericFieldSpecificMsg = getErrorMsg (CONST_PTY_ERROR_SPECIFIC_MSG, strLanguage, "NumericField")
	strDateFieldSpecificMsg = getErrorMsg (CONST_PTY_ERROR_SPECIFIC_MSG, strLanguage, "DateField")
	strNotFilledSpecificMsg = getErrorMsg (CONST_PTY_ERROR_SPECIFIC_MSG, strLanguage, "NotFilled")
	
	strMailFieldStandardMsg = getErrorMsg (CONST_PTY_ERROR_STANDARD_MSG, strLanguage, "MailField")
	strNumericFieldStandardMsg = getErrorMsg (CONST_PTY_ERROR_STANDARD_MSG, strLanguage, "NumericField")
	strDateFieldStandardMsg = getErrorMsg (CONST_PTY_ERROR_STANDARD_MSG, strLanguage, "DateField")
	strNotFilledStandardMsg = getErrorMsg (CONST_PTY_ERROR_STANDARD_MSG, strLanguage, "NotFilled")

	//On réinitialise les champs corrections
	removeValues(
		formValue,
		tblMailFieldNames);
		
	removeValues(
		formValue,
		tblNumericFieldNames);
		
	removeValues(
		formValue,
		tblObligFieldNames);

	removeValues(
		formValue,
		tblAssociateList);
		
	removeValues(
		formValue,
		tblDateFieldNames);

	

	//On lance les 4 processus de validation (obligatoire, numérique, fromat mail et date valide)
	boolMailField = 
		validMailFields(
			formValue,
			tblMailFieldNames,
			strMailFieldSpecificMsg);

	if (boolMailField)
	{
		if (strCorrectionList != "")
			strCorrectionList = 
				strCorrectionList + "\n";
		strCorrectionList = 
			strCorrectionList + 
			strMailFieldStandardMsg;
	}
	
	boolNumericField = 
		validNumericFields(
			formValue,
			tblNumericFieldNames,
			strNumericFieldSpecificMsg);
	
	if (boolNumericField)
	{
		if (strCorrectionList != "")
			strCorrectionList = 
				strCorrectionList + "\n";
		strCorrectionList = 
			strCorrectionList + 
			strNumericFieldStandardMsg;
	}
	
	boolObligField = 
		validObligFields(
			formValue,
			tblObligFieldNames,
			strNotFilledSpecificMsg);
			
	boolAssociateField = 
		validObligAssociateFields(
			formValue,
			tblAssociateList,
			strNotFilledSpecificMsg );
		
	if (boolObligField || boolAssociateField)
	{
		if (strCorrectionList != "")
			strCorrectionList = 
				strCorrectionList + "\n";
		strCorrectionList = 
			strCorrectionList + 
			strNotFilledStandardMsg;
	}
			
	boolDateFieldNames = 
		valideDateFields (
			formValue,
			tblDateFieldNames,
			strDateFieldSpecificMsg);

	if (boolDateFieldNames)
	{
		if (strCorrectionList != "")
			strCorrectionList = strCorrectionList + "\n";
		strCorrectionList = 
			strCorrectionList + 
			strDateFieldStandardMsg;
	}

	if (strCorrectionList != "")
	{
		strConfirmation = "";
		strDocInprogress = "1";
		strDocIcon = CONST_DOC_ICON_INPROGRESS
	}
	else
	{
		strConfirmation = 
			getSystemInfo(
				strForm,
				CONST_PTY_CONFIRMATION_MSG);
		if (strConfirmation == null)
			strConfirmation = "";
		strDocInprogress = "0";
		strDocIcon = CONST_DOC_ICON_STANDBY;
	}
	
	//On instancie les valeurs
	formValue.elements["doc_confirmation"].value = strConfirmation;
	formValue.elements["doc_correction"].value = strCorrectionList;
	formValue.elements["doc_inprogress"].value = strDocInprogress;
	formValue.elements["doc_icon"].value = strDocIcon;
	
	return(strCorrectionList);
}

