	var XmlFile="scripts/circulaires.xml"; //Fichier XML de l'application
	var genericDate = "9999999999";  //Valeur générique pour start_date et end_date (circulaire régulière)
	var MSD_Y="";    //Date de début : Année 
	var MSD_M="";    //Date de début : Mois
	var MSD_D="";    //Date de début : Jour
	var MSD="";      //Date de début
	var MSD_MN="";   //Date de début : Minutes
	var MED_Y="";    //Date de fin : Année
	var MED_M="";    //Date de fin : Mois
	var MED_D="";    //Date de fin : Jour
	var MED_MN="";   //Date de fin : Minutes
	var MED="";      //Date de fin
	var PUB_CURR;    //Date de la publication courante pour formatter les urls (ex: 20090520)
	var PUB_PREV;    //Date de la publication précédente pour formatter les urls (ex: 20090520)
	var SHOW_PREV;   //Affichage de la publication précédente (true, false)
	var MPSD_M="";   //Date de début (Publication précédente) : Mois
	var MPSD_D="";   //Date de début (Publication précédente) : Jour
	var MPED_M="";   //Date de fin (Publication précédente) : Mois
	var MPED_D="";   //Date de fin (Publication précédente) : Jour
	var region="qc"; //Région (Onglet) par défaut
	var cookiecheck="0";
		
    //Début Bloc de gestion des cookies (Région par défaut du client)
    function reloadCookie(c_name,value) {
      setCookie(c_name,value,365);
    }
    
    function setCookie(c_name, value, expiredays) {
        var exdate=GetServerDate();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
		cookiecheck="1";
		if (getCookie(c_name) == null)
			checkNoCookie(value);
		else
			checkCookie();
    }
    
    function getCookie(c_name)  {
      if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
          c_start=c_start + c_name.length+1;
          c_end=document.cookie.indexOf(";",c_start);
          if (c_end==-1) c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start,c_end));
        } 
      }
      return null;
    }
    
    function checkCookie() {
		if (getCookie('region') == null) {
			if(cookiecheck=="0")
				setCookie('region','qc',365);}
		else{
				region=getCookie('region');
				displayElement(region);
				selTab(region);   
			}
	}
	
	function checkNoCookie(value){
		region=value;
		displayElement(value);
		selTab(value);
	}
//Fin Bloc de gestion des cookies  

//Début Fonction de lecture du fichier XML et affichage des données 
function displayElement(reg) {
    region=reg;
	var req = null;
 
if (window.XMLHttpRequest){
    req = new XMLHttpRequest();
   if (req.overrideMimeType){
    req.overrideMimeType('text/xml');
   }
  } 
  else if (window.ActiveXObject) 
  {
   try {
    req = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e)
   {
    try {
     req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {}
   }
          }
  req.onreadystatechange = function()
  { 
   if(req.readyState ==4 )
   {
    if(req.status == 200)
    {
     // process a XML document here
     var xmlDoc = req.responseXML;
     var frval = xmlDoc.getElementsByTagName(region)[0];
	 var items = frval.getElementsByTagName("publication");
	 //var offset = frval.getAttribute("offset");
	 var tblflyer = new Array();
	 var headerTitle=lng=='fr'?'Veuillez choisir une publication':'Please select a publication';
	 tblflyer.push("<div style='text-align: center;padding-top:5px>");
	 tblflyer.push("<div style='margin-left: auto; margin-right: auto>");
	 tblflyer.push("<h2>"+headerTitle+"</h2>");
	 tblflyer.push("</div>");
	 tblflyer.push("</div>");
     tblflyer.push("<table cellspacing='5' cellpadding='0' align='center' border='0'>");
	 tblflyer.push("<tr align='center'>");
	 for (var i = 0 ; i < items.length ; i++) {
		var item = items[i];
		var releaseday = item.getAttribute("releaseday");
		var type = item.getElementsByTagName("type")[0].firstChild.nodeValue;
		var langitems = item.getElementsByTagName(lng)[0];
		var nom = langitems.getElementsByTagName("nom")[0].firstChild.nodeValue;
		var lien = langitems.getElementsByTagName("lien")[0].firstChild.nodeValue;
		var img = langitems.getElementsByTagName("image")[0].firstChild.nodeValue;
		getStartDate(item.getElementsByTagName("start_date")[0].firstChild.nodeValue, releaseday);
		getEndDate(item.getElementsByTagName("end_date")[0].firstChild.nodeValue, releaseday);
		if(checkDateRange(MSD, MED)) {
		    if(MSD==genericDate){
			    tblflyer.push("<td valign='top'>");
			    tblflyer.push("<table cellspacing='5' cellpadding='0' align='center' border='0'>");
			    tblflyer.push("<tr align='center'><td><a href='#' onclick='javascript:openFull(\""+lien+PUB_CURR+"/?lang="+lng+"\",\"pvpop\"); return false;'>"+setTitle(MSD_D,MSD_M,MED_D,MED_M)+"</a></td></tr>");
			    tblflyer.push("<tr align='center'><td><a href='#' onclick='javascript:openFull(\""+lien+PUB_CURR+"/?lang="+lng+"\",\"pvpop\"); return false;'><img src='"+img+PUB_CURR+"' border='0'></a></td></tr>");
			    tblflyer.push("<tr align='center'><td><a href='#' onclick='javascript:openFull(\""+lien+PUB_CURR+"/?lang="+lng+"\",\"pvpop\"); return false;'>"+nom+"</a></td></tr>");
			    tblflyer.push("</table>");
			    tblflyer.push("</td>");
		        if(SHOW_PREV==true){
		            tblflyer.push("<td valign='top'>");
			        tblflyer.push("<table cellspacing='5' cellpadding='0' align='center' border='0'>");
			        tblflyer.push("<tr align='center'><td><a href='#' onclick='javascript:openFull(\""+lien+PUB_PREV+"/?lang="+lng+"\",\"pvpop\"); return false;'>"+setTitle(MPSD_D,MPSD_M,MPED_D,MPED_M)+"</a></td></tr>");
			        tblflyer.push("<tr align='center'><td><a href='#' onclick='javascript:openFull(\""+lien+PUB_PREV+"/?lang="+lng+"\",\"pvpop\"); return false;'><img src='"+img+PUB_PREV+"' border='0'></a></td></tr>");
			        tblflyer.push("<tr align='center'><td><a href='#' onclick='javascript:openFull(\""+lien+PUB_PREV+"/?lang="+lng+"\",\"pvpop\"); return false;'>"+nom+"</a></td></tr>");
			        tblflyer.push("</table>");
			        tblflyer.push("</td>");
			   }
		    }
		    else{
		        tblflyer.push("<td valign='top'>");
			    tblflyer.push("<table cellspacing='5' cellpadding='0' align='center' border='0'>");
			    tblflyer.push("<tr align='center'><td><a href='#' onclick='javascript:openFull(\""+lien+"/?lang="+lng+"\",\"pvpop\"); return false;'>"+setTitle(MSD_D,MSD_M,MED_D,MED_M)+"</a></td></tr>");
			    tblflyer.push("<tr align='center'><td><a href='#' onclick='javascript:openFull(\""+lien+"/?lang="+lng+"\",\"pvpop\"); return false;'><img class='encart' src='"+img+"' width='144' border='0'></a></td></tr>"); //width='144'
			    tblflyer.push("<tr align='center'><td><a href='#' onclick='javascript:openFull(\""+lien+"/?lang="+lng+"\",\"pvpop\"); return false;'>"+nom+"</a></td></tr>");
			    tblflyer.push("</table>");
			    tblflyer.push("</td>");
			}
		}
	 }
     tblflyer.push("</tr></table>");
     tblflyer.push("<p />");
     s = tblflyer.join("");
     var dobj = document.getElementById('xmlInfo');
	 dobj.innerHTML = s;
	 document.close();
	 checkSizes();
	 
	 }
else{ 
    if(lng=='fr')       
        document.write('Votre navigateur ne peut afficher correctement cette page.');
    else
        document.write('Your browser can not display this page correctly.');
     }
    }
   }; 
   var rndnum = Math.floor(Math.random() * 999)+1;  //Génération d'un nombre aléatoire, servira comme paramètre pour l'ouverture d'un fichier XML à la source (pas en cache)
   req.open("GET", XmlFile +"?"+rndnum ,  true);    // Ouverture du fichier XML
   req.send(null);
}
//Fin Fonction de lecture du fichier XML et affichage des données 

//Ouverture de la fenêtre de la circulaire
function openFull(p,n) {
	var r = null;
    r = window.open(p,n,'width='+screen.width+',height='+screen.height+',top='+0+',left='+0+','+'scrollbars=no,toolbar=no,location=no,status=no,menubar=no,resizable=yes,dependent=no');
	if(r!=null && window.focus) r.window.focus();
}

//Vérification de la largeur et hauteur des images largeur Max 122 heuteur Max 220
function checkSizes() {
	var t;
	
  $(".encart").each(function () {
    clearTimeout(t);
    $(this).removeAttr("width");
    $(this).removeAttr("height");
    
    var pic_real_width = $(this).width();
    var pic_real_height = $(this).height();
    
    if ((pic_real_width == 0)||(pic_real_height == 0)) {
      t=setTimeout("checkSizes()",1000);
      //return;
    }
    var maxWidth = 144;
    var maxHeight = 220;
   
    var ratio = 0;
    // Used for aspect ratio
         
    var width = pic_real_width
    // Current image width     
    
    var height = pic_real_height
    // Current image height      
      
    // Check if the current width is larger than the max     
    if(width > maxWidth){
      ratio = maxWidth / width;   
      // get ratio for scaling image
      
      $(this).css("width", maxWidth); 
      // Set new width         
      
      $(this).css("height", height * ratio);  
      // Scale height based on ratio         
      
      width = width * ratio;    
      height = height * ratio;   
      // Reset height to match scaled image     
    }      
    
    // Check if current height is larger than max     
    if(height > maxHeight) {         
      ratio = maxHeight / height; 
      // get ratio for scaling image        
      $(this).css("height", maxHeight);   
      // Set new height         
      $(this).css("width", width * ratio);    
      // Scale width based on ratio         
      width = width * ratio;    
      height = height * ratio;   
      // Reset width to match scaled image
    }    
  }); 

  
}


//Obtention des valeurs pour les dates de début - circulaire en cours ou précédente
function getStartDate(MyDate, releaseday) {
	//MyDate='2009052901';
	SHOW_PREV=false;
	if(MyDate==genericDate) {
	    rd=releaseday*1;
	    rdlive=((releaseday*1)+1)>6?0:(releaseday*1)+1;
		d = GetServerDate();
		weekDay=d.getDay();
		if(weekDay==rd){
		    dp = GetServerDate();
		    PUB_PREV = formatFullDate(-6);
		    dp.setDate(dp.getDate()-6);
		    SHOW_PREV=true;
		    MPSD_M=dp.getMonth();
		    MPSD_D=dp.getDate();
		    if(weekDay !=rdlive){
		        PUB_CURR = formatFullDate(1);
                d.setDate(d.getDate()+1);
		    }
		}
		else if(weekDay !=rdlive){
		    var sq=0;
			dir = -1;
			while (d.getDay() !=rdlive) {
				d.setDate(d.getDate()+dir);
				sq+=dir;
			}
			PUB_CURR = formatFullDate(sq);
		}
		else
		    PUB_CURR = formatFullDate(0);
		MSD_M=d.getMonth();
		MSD_D=d.getDate();
		MSD=genericDate;
    }
	else {
	        SHOW_PREV=false;
			MSD_Y=MyDate.substring(0,4);
			MSD_M=(MyDate.substring(4,6)*1)-1;
			MSD_D=MyDate.substring(6,8)* 1; 
			MSD_MN=MyDate.substring(8,10);
			MSD=new Date(MSD_Y,MSD_M,MSD_D,MSD_MN);
	}
}

//Obtention des valeurs pour les dates de fin - circulaire en cours ou précédente
function getEndDate(MyDate, releaseday) {
	//MyDate='2009052901';
	if(MyDate==genericDate) {
	    rd=releaseday*1;
	    rdlive=((releaseday*1)+1)>6?0:(releaseday*1)+1;
	    d = GetServerDate();
		weekDay=d.getDay();
		if(weekDay==rd){
		    dp = GetServerDate();
		    dp.setDate(dp.getDate());
		    MPED_M=dp.getMonth();
		    MPED_D=dp.getDate();
		    if(weekDay !=rdlive){
                d.setDate(d.getDate()+7);
		    }
		}
		else if(weekDay !=rdlive){
		    dir = (weekDay==0)?rd:1;
			while (d.getDay() !=rd) {
				d.setDate(d.getDate()+dir);
			}
	    }
	    else
	        d.setDate(d.getDate()+6);
	    MED_M=d.getMonth();
		MED_D=d.getDate();
		MED=genericDate;
	}
	else {
			MED_Y=MyDate.substring(0,4);
			MED_M=(MyDate.substring(4,6)*1)-1;
			MED_D=MyDate.substring(6,8)*1; 
			MED_MN=MyDate.substring(8,10);
			MED=new Date(MED_Y,MED_M,MED_D,MED_MN);
	}
}

function checkDateRange(start, end) 
{
   if(start==genericDate && end==genericDate)
     return true;
   else
   {  
     var dtnow = GetServerDate();
     var tempDate = new Date(start);
     var dtStart = new Date(tempDate);
     dtStart.setDate(tempDate.getDate()-1);
     //Valider sur la date de la veille car on veut afficher les promo un jour avant
     if(dtStart <= dtnow && dtnow <= end)
       return true;
   }
   return false;
 }

//Obtention du mois en français
function strMonth_fr(mnt){
	var month=new Array(12);
	month[0]="janvier";
	month[1]="f&eacute;vrier";
	month[2]="mars";
	month[3]="avril";
	month[4]="mai";
	month[5]="juin";
	month[6]="juillet";
	month[7]="ao&ucirc;t";
	month[8]="septembre";
	month[9]="octobre";
	month[10]="novembre";
	month[11]="d&eacute;cembre";
	return month[mnt];
}

//Obtention du mois en anglais
function strMonth_en(mnt){
	var month=new Array(12);
	month[0]="Jan.";
	month[1]="Feb.";
	month[2]="March";
	month[3]="April";
	month[4]="May";
	month[5]="June";
	month[6]="July";
	month[7]="Aug.";
	month[8]="Sept.";
	month[9]="Oct.";
	month[10]="Nov.";
	month[11]="Dec.";
	return month[mnt];
}

//Formattage du titre (plages horaires) de la circulaire (May 28 to June 3 //28 mai au 3 juin ou 3 au 10 juin)
function setTitle(startDay,startMonth,endDay,endMonth)
{
    if(lng=="en")
        return strMonth_en(startMonth) + " " + startDay + " to " + strMonth_en(endMonth) + " " + endDay;
    else{
        sMonth=" " + strMonth_fr(startMonth);
        eMonth=" " + strMonth_fr(endMonth);
        if(sMonth==eMonth)sMonth="";
        return  startDay + sMonth + " au " + endDay + eMonth;
    }
}

//Obtention de la date ajustée au format YYYYMMDD
function formatFullDate(sq){
    var dte = GetServerDate();
    dte.setDate(dte.getDate()+sq);
    return(dte.getUTCFullYear()+ padleft(dte.getMonth()+1,'0',2)+ padleft(dte.getDate(),'0',2)); 
}

//Assignation de la classe pour l'onglet actif (sélectionné)
function selTab(region){
    var active_class = "onglet_active";
    var inactive_class = "onglet";
    switch(region)
    {
    case 'qc':
        document.getElementById('onglet_qc').className = active_class;
        document.getElementById('onglet_nb').className = inactive_class;
        document.getElementById('onglet_on').className = inactive_class;
        break;
    case 'nb':
        document.getElementById('onglet_qc').className = inactive_class;
        document.getElementById('onglet_nb').className = active_class;
        document.getElementById('onglet_on').className = inactive_class;
        break;
    case 'on':
        document.getElementById('onglet_qc').className = inactive_class;
        document.getElementById('onglet_nb').className = inactive_class;
        document.getElementById('onglet_on').className = active_class;
        break;
   }
}
//Ajout de caractères à gauche jusqu'à une certaine longueur (params : variable à padder, caractère à ajouter, longueur max)
function padleft(str, pad, count) {
	var strc = str+"";
	while(strc.length<count)
		strc=pad+strc;
	return strc;
	}

//Ajout de caractères à gauche jusqu'à une certaine longueur (params : variable à padder, caractère à ajouter, longueur max)
/* function padleft(val, ch, num) {
    var re = new RegExp(".{" + num + "}$");
    var pad = "";
    if (!ch) 
        ch = " ";
    do {
        pad += ch;
        }while(pad.length < num);
            return re.exec(pad + val)[0];
} */

function GetServerDate()
{
	MyDate=document.getElementById('ctl00_GJCHeader1_actualdate').value;
	MD_Y=MyDate.substring(0,4);
	MD_M=MyDate.substring(4,6); // Jan-Dec=01-12
	MD_D=MyDate.substring(6,8);
	MD_H=MyDate.substring(8,10);
	MD_N=MyDate.substring(10,12);
	MD_S=MyDate.substring(12,14);
	MD_M=MD_M-1; // Jan-Dec=00-11
	return new Date(MD_Y, MD_M, MD_D, MD_H, MD_N, MD_S); 
}

