/*
'---
'--- JSFonctions.js - Page de fonction en Javascript
'---
'--- Par JP Bernard
'---     CIBLE Solutions d'affaires
'---
'--- RÉVISION
'--- 2008/09/30, JPB	Création
*/


function setParentTableWidth(ParentTable)
{
	if(document.getElementById(ParentTable))
		document.getElementById(ParentTable).width = "100%";
}

//-------------------------------------------------------------------------------------
// Message d'avertissement temporaire
//-------------------------------------------------------------------------------------
function DoPupDeveloppement()
{
    alert("IMPORTANT!!!\nCette section du site Internet est présentement en développement.\nElle sera fonctionnelle dans le plus bref délai. Merci!\n\nJean-Philip Bernard\nProgrammeur Analyste\jean-philip.bernard@ciblesolutions.com");    		
}

//-------------------------------------------------------------------------------------
//trouve la référence pour l'objet document
//-------------------------------------------------------------------------------------
function GetReference(id)
{
	if(document.layers) return document.layers[id];
	if(document.all && !document.getElementById) return document.all[id];
	if(document.all && document.getElementById) return document.getElementById(id);
	if(!document.all && document.getElementById) return document.getElementById(id);
	return false;
}


//---------------------------------------------------------------------
//Ouvrir une fenêtre
//---------------------------------------------------------------------
function OuvrirFenetre(iNomPage, iTitre, iWidth, iHeight)
{
	fenetre = window.open(iNomPage, iTitre, "top=0, left=0, width=" + iWidth + ", height=" + iHeight + ", scrollbars=yes, location=no, toolbar=no, menubar=no");
	fenetre.focus()
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* OuvrirConfidentialite() - Ouvre la page pour les politiques de confidentialité
*/
function OuvrirConfidentialite()
{         
   OuvrirFenetre("", "CONFIDENTIALITE", 630, 580);
   FormConfidentialite.submit();
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* DoInsertCodeHTML() - Insère le code HTML créé dans l'éditeur
*/
function DoInsertCodeHTML(iControlName, iContenu)
{  
   // alert("oui");
    
   // alert(iControlName);
   // alert(iContenu);
    
	if(iControlName!='')
    {
        var ObjName=GetReference(iControlName);

		ObjName.value = iContenu;
    }
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* OuvrirEditeurHTML() - Ouvre l'éditeur HTML
*/
function OuvrirEditeurHTML(iControlName)
{
    var ObjName = GetReference(iControlName);
    var ObjTexte = GetReference("TexteContenu");
    var ObjCtrl = GetReference("Ctrl");
    
    ObjTexte.value = ObjName.value;
    ObjCtrl.value = iControlName;
    
    if(iControlName!='')
    {
	   OuvrirFenetre("", "EditeurTexte", 800, 600);
       document.EditeurHTML.submit();
    }
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * RemoveLineBreakJS() - Retourne la string sans les sauts de lignes ("\n" ou "\r\n")
 *
 *
 * Paramètres:
 *   iString - La chaine de caratère (type: String)
 *
 */
function RemoveLineBreakJS(iString)
{
    iString.replace("\r\n", "");
    iString.replace("\n", "");
    
    return iString;
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * JAVA_FormatStr() - Remplace les apostrophes (') par l'équivalent UNICODE \u0027
 *                 et les guillemets (") par l'équivalent UNICODE \u0022 pour
 *                 les fonctions JavaScript.
 */
function JAVA_FormatStr(iString)
{
    alert(iString);
    var chaine = new String(iString);

    remplace  = new RegExp("\'","g"),
    chaine = chaine.replace(remplace, "&#39;");

    remplace  = new RegExp('\"',"g"),
    chaine = chaine.replace(remplace, '&#39;');

    return chaine;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* ImprimerPage() - Lance l'impression de la fenêtre en cours
*/
function ImprimerPage(iLangue)
{       
    if (navigator.appVersion.indexOf("Mac",0)>0)
    {
        if (iLangue=="en")
            alert("This function is not available for Macintosh. Please use Apple-P.");
        else
            alert("Cette fonction n\'est pas réalisable sur Macintosh.\n\nUtilisez plutôt la combinaison de touches  « Pomme-P »");
    }
    else
    {
        window.print();
    }
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* OuvrirPrint() - Ouvre la page pour l'impression
*/
function OuvrirPrint()
{
   OuvrirFenetre("", "PRINTWINDOW", 610, 600);
   document.FormPrint.submit();
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * DoOuvrirNouvelle() - Ouvre un pop-up présentant une nouvelle
 *
 * Paramètres:
 *    iNouvelleID  - Le ID de la nouvelle à ouvrir (type: Integer)
 *    iLangue      - La langue à utiliser ('fr' ou 'en')
 */
function DoOuvrirNouvelle(iNouvelleID, iLangue)
{
    // On commence par former l'URL 
    var Url = "/detail_nouvelle.php"
        + "?L=" + iLangue
        + "&NouvelleID=" + iNouvelleID;

    // On défini le format de la fenêtre
	var Format = "width=657"
		+ ",height=600"
		+ ",top=5"
		+ ",left=5"
		+ ",menubar=no"
		+ ",toolbar=no"
		+ ",addressbar=no"
		+ ",status=yes"
		+ ",resizable=yes"
		+ ",scrollbars=yes";

    // On tente d'ouvrir la fenêtre "Nouvelles"
    var NouvellesWin = window.open(Url, "Nouvelles", Format);

    // Si la création du pop-up est impossible (pop-up blocker ou autre), on
    // affiche un message d'erreur; sinon on y déplace le focus
    if (!NouvellesWin)
    {
        if (iLangue=="en")
            alert("This site uses pop-ups to display records, but your browser is blocking them. Please allow pop-ups for this site.");
        else
            alert("Ce site utilise des pop-up pour afficher les fiches mais votre navigateur semble les bloquer. Veuillez SVP autoriser l'affichage des pop-up pour ce site.");
    }
    else
    {
        NouvellesWin.focus();
    }
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * DoOuvrirImage() - Ouvre un pop-up présentant une image
 *
 * Paramètres:
 *    iImageID    - Le ID de l'image à ouvrir (type: Integer)
 *    iLangue     - La langue à utiliser ('fr' ou 'en')
 */
function DoOuvrirImage(iImageID, iLangue)
{
    // On commence par former l'URL
    var Url = "/fiche_photo.php"
        + "?L=" + iLangue
        + "&PhotoID=" + iImageID;

    // On défini le format de la fenêtre
	var Format = "width=657"
		+ ",height=600"
		+ ",top=5"
		+ ",left=5"
		+ ",menubar=no"
		+ ",toolbar=no"
		+ ",addressbar=no"
		+ ",status=yes"
		+ ",resizable=yes"
		+ ",scrollbars=yes";

    // On tente d'ouvrir la fenêtre "Photos"
    var PhotosWin = window.open(Url, "Photos", Format);

    // Si la création du pop-up est impossible (pop-up blocker ou autre), on
    // affiche un message d'erreur; sinon on y déplace le focus
    if (!PhotosWin)
    {
        if (iLangue=="en")
            alert("This site uses pop-ups to display records, but your browser is blocking them. Please allow pop-ups for this site.");
        else
            alert("Ce site utilise des pop-up pour afficher les fiches mais votre navigateur semble les bloquer. Veuillez SVP autoriser l'affichage des pop-up pour ce site.");
    }
    else
    {
        PhotosWin.focus();
    }
}



/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Ensemble de fonctions pour transferer des selections d'une fenetre a une autre
 *
 *        DEBUT 
 *
 */

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
}
/* 
 * Ensemble de fonctions pour transferer des selections d'une fenetre a une autre
 *
 *        FIN 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 */

/* 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * Tri les valeurs d'un SELECT
 *
 */
function sortArray()
{
    Liste= new Array();
    Obj= document.getElementById('SuggTous')

    for(i=0;i<Obj.options.length;i++)
    {
    Liste[i]=new Array()
    Liste[i][0]=Obj.options[i].value
    Liste[i][1]=Obj.options[i].text
    }

    Liste=Liste.sort()

    for(i=0;i<Obj.options.length;i++)
    {
    Obj.options[i].value=Liste[i][0]
    Obj.options[i].text=Liste[i][1]
    }
}
 
 


function cocherTout(Formulaire, state)
{
	var Counter;
	var TableauCheckbox = document.forms[Formulaire].getElementsByTagName("input"); 
	
	for(Counter = 0; Counter < TableauCheckbox.length; Counter++) 
	{
     	TableauCheckbox[Counter].checked = true;
	}
}
function decocherTout(Formulaire, state)
{
	var Counter;
	var TableauCheckbox = document.forms[Formulaire].getElementsByTagName("input"); 
	
	for(Counter = 0; Counter < TableauCheckbox.length; Counter++) 
	{
     	TableauCheckbox[Counter].checked = false;
	}
}



function ScrollTo(ScrollByY_)
{
	var obj = ScrollByY_;
	setTimeout("window.scrollTo(0," + obj + ")", 1);
}


function GererAffichageTextboxRecherche()
{
	var QuelleLangue;
	var QuelleMot;
	
	QuelleLangue = document.getElementById("hidden_langue").value;
	
	if(QuelleLangue == "fr")
		QuelMot = "Mots clés";
	else
		QuelMot = "Enter words";
	
	if(document.getElementById("Recherche"))
	{
		if(document.getElementById("Recherche").value == QuelMot)
		{
			document.getElementById("Recherche").value = "";
		}
		else if(document.getElementById("Recherche").value == "")
		{
			document.getElementById("Recherche").value = QuelMot;
		}
	}
}


/* FIN : FONCTIONS POUR DÉTECTION DU TYPE DE BROWSER, VERSION ET OS */
/* Voici l'adresse du site Web ou j'ai trouvé ce script : 
// http://www.quirksmode.org/js/detect.html // */

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

/* FIN : FONCTIONS POUR DÉTECTION DU TYPE DE BROWSER, VERSION ET OS */

/* DÉBUT : FONCTION POUR PLACER LES DIV DES BONHOMMES DANS LES COINS DES FOOTER */
function Ajust_Image(ImgLeftPaddingTop_value, ImgLeftPaddingLeft_value, ImgRightPaddingTop_value, ImgRightPaddingLeft_value){
	
	// On affiche les deux images du footer qui étaient cachées
	if(document.getElementById('image_gauche'))
		document.getElementById('image_gauche').style.display = "";
	if(document.getElementById('image_droite'))	
		document.getElementById('image_droite').style.display = "";
	
	BodyHeight = document.getElementById('body').offsetHeight;
	BodyWidth = document.getElementById('body').offsetWidth;
	
	MainContainerHeight = document.getElementById('main_container').offsetHeight;
	MainContainerWidth = document.getElementById('main_container').offsetWidth;
	
	
	IRContainerHeight = document.getElementById('image_droite').offsetHeight;
	IRContainerWidth = document.getElementById('image_droite').offsetWidth;
	
	ILContainerHeight = document.getElementById('image_gauche').offsetHeight;
	ILContainerWidth = document.getElementById('image_gauche').offsetWidth;
	
	
	AjustRightWidth = ((BodyWidth - MainContainerWidth) / 2) + MainContainerWidth - IRContainerWidth;
	document.getElementById('image_droite').style.top = MainContainerHeight - IRContainerHeight + ImgRightPaddingTop_value + "px";
	document.getElementById('image_droite').style.left = AjustRightWidth + ImgRightPaddingLeft_value + "px";
	
	/* SIZE POUR ÉCRAN 1024 x 768 */
	//document.getElementById('image_droite').style.left = AjustRightWidth + 20 + "px";
	
	
	AjustLeftWidth = ((BodyWidth - MainContainerWidth) / 2);
	document.getElementById('image_gauche').style.top = MainContainerHeight - ILContainerHeight + ImgLeftPaddingTop_value + "px";
	document.getElementById('image_gauche').style.left = AjustLeftWidth - ImgLeftPaddingLeft_value + "px";
	
	/* SIZE POUR ÉCRAN 1024 x 768 */
	//document.getElementById('image_gauche').style.left = AjustLeftWidth - 20 + "px";
}
/* DÉBUT : FONCTION POUR PLACER LES DIV DES BONHOMMES DANS LES COINS DES FOOTER */



/* DÉBUT : FONCTIONS POUR LE SÉLECTEUR DE DATE AVEC COMBOBOX */

//set la date d'aujourd'hui
Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();
if (NowYear < 2000) NowYear += 1900; //for Netscape

//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == "avril" || WhichMonth == "juin" || WhichMonth == "septembre" || WhichMonth == "novembre") DaysInMonth = 30;
  if (WhichMonth == "février" && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
  if (WhichMonth == "février" && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
  return DaysInMonth;
}

//function to change the available days in a months
function ChangeOptionDays(Which, QuestionID)
{
  DaysObject = eval("document.FormFormulaire." + Which + "Day_" + QuestionID);
  MonthObject = eval("document.FormFormulaire." + Which + "Month_" + QuestionID);
  YearObject = eval("document.FormFormulaire." + Which + "Year_" + QuestionID);

  Month = MonthObject[MonthObject.selectedIndex].text;
  Year = YearObject[YearObject.selectedIndex].text;

  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length;
  if (CurrentDaysInSelection > DaysForThisSelection)
  {
    for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
    {
      DaysObject.options[DaysObject.options.length - 1] = null
    }
  }
  if (DaysForThisSelection > CurrentDaysInSelection)
  {
    for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
    {
		DaysObject.options[DaysObject.options.length] = new Option(DaysObject.options.length + 1);
      //NewOption = new Option(DaysObject.options.length + 1);
     // DaysObject.add(NewOption);
    }
  }
    if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

//function to set options to today
function SetToToday(Which, QuestionID)
{
	DaysObject = eval("document.FormFormulaire." + Which + "Day_" + QuestionID);
	MonthObject = eval("document.FormFormulaire." + Which + "Month_" + QuestionID);
	YearObject = eval("document.FormFormulaire." + Which + "Year_" + QuestionID);
	
	YearObject[0].selected = true;
	MonthObject[NowMonth].selected = true;
	
	ChangeOptionDays(Which, QuestionID);
	
	DaysObject[NowDay-1].selected = true;
}

//function to write option years plus x
function WriteYearOptions(YearsAhead)
{
  line = "";
  /*for (i=0; i<YearsAhead; i++)
  {
    line += "<OPTION>";
    line += NowYear + i;
  }*/
  for (i = NowYear; i >= NowYear-110; i--)
  {
    line += "<OPTION>";
    line += i;
  }
  return line;
}

/* DÉBUT : FONCTIONS POUR LE SÉLECTEUR DE DATE AVEC COMBOBOX */


/* Fonction qui valide la longueur maximum d'un textarea pour les Formulaires à répondres et les Sondages  */
function ismaxlength(obj)
{
	var mlength = obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength)
}
