// Pour Promotelec

function trim(s) {
    return s.replace(/^\s+/, '').replace(/\s+$/, '');
}



function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function IsInteger(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

////////////////////////////////////////////////////////////////////////////////
/// POPUP

function OpenSpecificPopModal(ascx)
{	
	ArrayParam = new Array (1); 
	ArrayParam[0] =window;
	// Ouvre la fenêtre PopUP
	window.showModalDialog( 'ModalMaster.aspx?mnu=' + ascx+ '' ,ArrayParam,'resizable: 0;status=0;scroll=0;center=yes' );

}

function OpenSpecificPopModalWithSize(ascx, var_width, var_height)
{	
	ArrayParam = new Array (1); 
	ArrayParam[0] =window;
	// Ouvre la fenêtre PopUP
	window.showModalDialog( 'ModalMaster.aspx?mnu=' + ascx+ '' ,ArrayParam,'resizable: yes;dialogWidth=' + var_width + ';dialogHeight=' + var_height + ';status=0;scroll=0;center=yes;' );
}


// Ouvre une modaldialog avec un composant spécifique avec des arguments
function OpenSpecificPopModalWithArguments( ascx, argItems )
{
	ArrayParam = new Array (1); 
	ArrayParam[0] = window;

	window.showModalDialog( 'ModalMaster.aspx?mnu=' + ascx + argItems + '' , ArrayParam,'resizable: 0;status=0;scroll=0;center=yes');
}

function OpenSpecificPopModalWithSizeAndArguments(ascx, argItems, var_width, var_height)
{	
	ArrayParam = new Array (1); 
	ArrayParam[0] =window;
	// Ouvre la fenêtre PopUP
	window.showModalDialog( 'ModalMaster.aspx?mnu=' + ascx+ argItems + '' ,ArrayParam,'resizable: yes;dialogWidth=' + var_width + ';dialogHeight=' + var_height + ';status=0;scroll=0;center=yes;' );
}


// Ouvre une modaldialog avec un composant spécifique avec des arguments
function OpenSpecificPopModalWithArgumentsAndRefresh( ascx, argItems )
{
	ArrayParam = new Array (1); 
	ArrayParam[0] = window;

	window.showModalDialog( 'ModalMaster.aspx?mnu=' + ascx + argItems + '' , ArrayParam,'resizable: 0;status=0;scroll=0;center=yes');
}


/// Ouvre une Dialog de type List/List avec les éléments à pré-selectionner dans la liste de droite
function OpenSelectionDialog( ascx, oParentList)
{
	var s="";
	// Fabrique le QueryString à envoyer à la fenêtre PopUP
	for( i=oParentList.options.length-1; i>=0; i-- )
		s += oParentList.options[i].value + '|'
					
	// Ouvre la fenêtre PopUP	
	OpenSelectionPopModal( ascx, oParentList.name,  s);
}


function OpenSelectionPopModal( ascx, listDest, argItems )
{
	
	ArrayParam = new Array (2); 
	ArrayParam[0] = window;
	ArrayParam[1] = listDest;
	
	var wResult = window.showModalDialog( 'ModalMaster.aspx?mnu=' + ascx + '&argItems=' + argItems + '', ArrayParam,'resizable: 0;status=0;scroll=0;center=yes');
	
	// Rafraîchi la fenêtre appelante
	if( wResult != null )
	{
		window.parent.frames["index"].location.href= wResult;
	}
}

function  OpenUserControlPopModalWithSizeAndArguments(ascx, argItems, var_width, var_height)
{	
	ArrayParam = new Array (1); 
	ArrayParam[0] =window;
	// Ouvre la fenêtre PopUP
	window.showModalDialog( 'ModalMaster.aspx?userControlPopup=true&mnu=' + ascx + argItems + '' ,ArrayParam,'resizable: yes;dialogWidth=' + var_width + ';dialogHeight=' + var_height + ';status=0;scroll=0;center=yes;' );
}


///////////////////////////////////////////////////////////////////////////////////

var indexTechnique = 0;
var goBackAc;
function checkKeyPressDefinitive(objetCheck, objetChamps)
{
	var checkDefinitive = eval("document.all." + objetCheck);
	var textDefinitive = eval("document.all." + objetChamps);
	
	if (checkDefinitive.checked)
	{
		// mettre le champ a vide
		alert('Vous ne pouvez pas saisir de date definitive car la case Pas de date est cochee.');
		textDefinitive.value='';
		event.returnValue = false;
	}
}


function GoBackIfNecessary(goBackAc)
{

		//alert('goBackAc');
		var curRow			= goBackAc.rowElement;
		var tblElm			= wgGetTable(curRow);
		var curRowIndex		= curRow.rowIndex;
		
		try
		{
			tblElm.selectRow();
		}
		catch(e)
		{alert(e.Description)}
		
}



// Fonction permettant de mettre à vide tous les champs de la page
function ResetField(field)
{
	document.all[field].value = '';
}

// Traitements de la fermeture des fenêtres PopUP de type Choix Personne
function FillTextAndClose( sNewText, sNewValue )
{	
	var Parent = window.dialogArguments[0];
	var ParentTextBox = eval ( 'window.dialogArguments[0].document.form1.' + window.dialogArguments[1] );
	var ParentValueBox = eval ( 'window.dialogArguments[0].document.form1.' + window.dialogArguments[2] );		
	
	ParentTextBox.value = sNewText;
	ParentValueBox.value = sNewValue;
	
	// Ferme la fenêtre
	window.close();
}

// Traitements de la fermeture des fenêtres PopUP de type Choix Personne multiple
function FillListAndClose( lstNom, lstId)
{	
    // recuperation des elements de la fenetre parente
    var Parent = window.dialogArguments[0];
    var dynLst = window.dialogArguments[1];
    var txtId  = window.dialogArguments[2];
        
    // envoi de la liste des Id
    txtId.value = lstId;
    
    // envoi les nouveaux elements dans la liste parente	
    var arrLstNom =  lstNom.split(',');
	for( i=0 ; i<arrLstNom.length ; i++) 
	{
		if ( arrLstNom[i].length >0 ) 
		{
			dynLst.Add( arrLstNom[i], arrLstNom[i] );
		}
	}    

	// Ferme la fenêtre	
	window.close();
}

//// Assure que la listbox possède bien un élément
function EnsureRequiredData( oSrcList )
{
	if( oSrcList.options.length == 0 )
		oSrcList.Add(-1,'(Faire un ou plusieurs choix)');
}

//// Traitements de la fermeture des fenêtres PopUP de type list/list
function FillAndClose(oSrcList,valMandatory) {
		
	var i;
	var j;
	var Parent = window.dialogArguments[0];
	var list = Parent.DynamicListBox_FindControl( window.dialogArguments[1]);
	
	//on vide d'abord la liste de destination
	for (j=list.length-1;j>=0;j--)
	{
		list.Remove(j);
	}
	
	if( oSrcList.options.length == 0 )
	{
		if (valMandatory != undefined)
			list.Add(-1,'(Faire un ou plusieurs choix)');
	}
	else
	{
		var arrItems	= new Array();  // Utilisé pour trier les données de destination
		var AZResult;	
		
		// Envoi les nouveaux élements dans un tableau pour etre triés
		for( i=oSrcList.options.length-1; i>=0; i-- )
		{
			arrItems[ i ] =  oSrcList.options[i].text + '|' + oSrcList.options[i].value ;
		}
		
		// Trie les éléments
		arrItems.sort();
									
		// Envoi les nouveaux élements dans la liste parente	
		for( i=0 ; i<arrItems.length ; i++) 
		{
			if ( arrItems[i].length >0 ) 
			{
				AZResult = arrItems[ i ].split('|');
				list.Add( AZResult[ 1 ], AZResult[ 0 ] );
			}
		}
		
	}	
	window.close();
}


// Fonction permettant d'afficher ou de cacher un objet dans la page
function AffCacheObjet(obj)
{
	if (document.all[obj].style.display == 'none' )
		{
			document.all[obj].style.display = 'block';
		}				
	else if (document.all[obj].style.display == 'block')
		{
			document.all[obj].style.display = 'none';
		}
}


// Fonction permettant d'afficher ou de cacher un objet dans la page selon un ordre
function AffCacheObjetOrdre(obj,ordre)
{
	document.all[obj].style.display = ordre;
}


function TrackList( ListBox, Tracker, NegativeItem )
{

	var i;
	
	Tracker.value = '';
	
	for( i=ListBox.options.length-1; i>=0; i--)
	{
		if( ListBox.options[i].value == -1 )
		{
			if( NegativeItem == 1 )
				Tracker.value = Tracker.value + ListBox.options[i].value + '|';
		}
		else
		{
			Tracker.value = Tracker.value + ListBox.options[i].value + '|';
		}
	}
}

function F_ChekSuppress(object)
{

	var list = eval("document.all." + object);
	if(list.options.selectedIndex == -1)
	{
		alert('Veuillez sélectionner un fichier dans la liste');
		return false
	}
	else
	{
		return confirm('Etes-vous certain(e) de vouloir supprimer cette piece jointe?' );
	}	
}
	
function AnneeBissex(Annee)
{
	if (Annee % 4 == 0)
		return true;
	else
		return false;

}
	
function Valide_date(nDate,nA1,nA2,nFormat,textMsgBox) {
	// Format valide: (ne pas utiliser d'autre format...
	// - dd/mm/yyyy
	// - mm/dd/yyyy
	// - yyyy/mm/dd
	//nA1,nA2 intervalle ex 1950,2030
	//nDate un champ date
	 
	var ok = "oui";
	var nChar;
	var aDate;
	var nJ;
	var nM;
	var nA;
	var jMax = 31;
	nAccepte = "0123456789/";
	var nDateTxt;
	
	if (nDate.value)
		nDateTxt = nDate.value;
	else
		nDateTxt = nDate;
		
	
	if (nDateTxt.length > 0) 
	{
    
    	if (nDateTxt.length < 6 || nDateTxt.length > 10) 
    	{
			{ok = "non";}
		}
		else
		{
			for (var i=0; i<nDateTxt.length; i++) 
			{
				nChar = "" + nDateTxt.substring(i, i+1);
				if (nAccepte.indexOf(nChar) == "-1") ok = "non";
			}
		}
		if (ok == "oui") 
		{
			aDate = nDateTxt.split("/");
			if (nFormat == "dd/mm/yyyy") 
			{ 
				nJ = aDate[0]; 
				nM = aDate[1]; 
				nA = aDate[2];
			} 
			else if (nFormat == "mm/dd/yyyy") 
			{ 
				nJ = aDate[1]; 
				nM = aDate[0]; 
				nA = aDate[2];
			} 
			else if (nFormat == "yyyy/mm/dd") 
			{ 
				nJ = aDate[2]; 
				nM = aDate[1]; 
				nA = aDate[0]; 
			}
			else if (nFormat == "mm/yyyy") 
			{ 
				nJ = 1; 
				nM = aDate[0]; 
				nA = aDate[1];
			} 
			
			if ((nJ < 1 || nJ > jMax) || (nM < 1 || nM > 12) || (nA < nA1 || nA > nA2)) 
			{
				ok = "non";
			}
			else 
			{
				if (nM == 2) 
				{ 
					if (AnneeBissex(nA) == true) 
					{ 
						jMax = 29 
					} 
					else 
					{ 
						jMax = 28 
					}
				}
				
				if ((nM == 1 || nM == 3 || nM == 5 || nM == 7 || nM == 8 || nM == 10 || nM == 12)) jMax = 31
				if ((nM == 4 || nM == 6 || nM == 9 || nM == 11)) jMax = 30
				if (nJ < 1 || nJ > jMax) ok = "non";
			}
		}
		
		if (ok == "non") 
		{
			
			if (nDate.value)
			{
				nDate.focus();
				nDate.select();
			}
			alert ( textMsgBox + "\n(format " + nFormat + ")");
			
			return false;
		}
		else
		{
			return true;
		}
	}
  
}

