// http://v2studio.com/k/code/lib/
// Librarie developee by Caio Chassot
// Modifiee par Pierre Lemieux, plemieux@caractera.com

//CONSTANTES
var chercherfr = "Chercher";
var chercheren = "Search";
var chercheres = "Buscar";

// ARRAY EXTENSIONS

if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}
Array.prototype.find = function(value, start) {
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}
Array.prototype.has = function(value) {
    return this.find(value)!==-1;
}

// FUNCTIONAL

function map(list, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < list.length; i++) result.push(func(list[i], i, list));
    return result;
}
function filter(list, func) {
    var result = [];
    func = func || function(v) {return v};
    map(list, function(v) { if (func(v)) result.push(v) } );
    return result;
}

// DOM

function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}
function hasClass(elem, className) {
    return getElem(elem).className.split(' ').has(className);
}
function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName),
        function(elem) { return hasClass(elem, className) });
}

// DOM EVENTS

function listen(event, elem, func) {
    elem = getElem(elem);
    if (elem.addEventListener){ // W3C DOM
        elem.addEventListener(event,func,false);
	}else if (elem.attachEvent){ // IE DOM
        elem.attachEvent('on'+event, function(){ func(new W3CDOM_Event(elem)) } );
        // for IE we use a wrapper function that passes in a simplified faux Event object.
    //else throw 'cannot add event listener';
	//else elem['on'+event] = function(){ func(new W3CDOM_Event(elem)) };
	}else{
		var oldfunc = elem['on'+event];
		if (typeof elem['on'+event] != 'function'){
			elem['on'+event] = function(){ func(new W3CDOM_Event(elem)) };
		}else{
			elem['on'+event] = function(){
				oldfunc();
				func(new W3CDOM_Event(elem));
			}
		}
	}
}
function mlisten(event, elem_list, func) {
    map(elem_list, function(elem) { listen(event, elem, func) } );
}
function W3CDOM_Event(currentTarget) {
    this.currentTarget  = currentTarget;
    this.preventDefault = function() { window.event.returnValue = false }
    return this;
}

// MISC CLEANING-AFTER-MICROSOFT STUFF

function isUndefined(v) {
    var undef;
    return v===undef;
}

// POPUP STUFF
function raw_popup(url, target, w, h, features, framed) {
	// pops up a window containing url optionally named target, optionally having features
	if (isUndefined(w)) w = _POPUP_W;
	if (isUndefined(h)) h = _POPUP_H;
	if (isUndefined(features)) features = _POPUP_FEATURES;
	if (isUndefined(target  )) target   = '_blank';
	if (isUndefined(framed  )) framed   = true;
	var wleft = (screen.availWidth-w-10)/2;
	var wtop = (screen.availHeight-h-90)/2;
	if (eval(framed)) {
		var theWindow = window.open('', target, 'width='+w+',height='+h+',left='+wleft+',top='+wtop+','+features);
		// var frameset et frameheader definies dans var_fr.js
		theWindow.document.open();
		theWindow.document.write(frameset);
		theWindow.document.close();
		theWindow.frames[0].location = frameheader+"&url="+url;
		theWindow.frames[1].location = url;
	}else{
		var theWindow = window.open(url, target, 'width='+w+',height='+h+',left='+wleft+',top='+wtop+','+features);
	}
	theWindow.focus();
	return theWindow;
}
function link_popup(src, inframe, w, h, features) {
	// to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
	// pops up a window grabbing the url from the event source's href
	var href = src.getAttribute('href');
	var target = src.getAttribute('target');
	if (isUndefined(inframe)) {
		inframe = true;
				
		var n = urlsinternes.length;
		for (var i=0; i<n; i++) {
			if (href.indexOf(urlsinternes[i]) != -1) {
				return false;
			}
		}
				
		n = urlvalide.length;
		for (var j=0; j<n; j++) {
			if (href.indexOf(urlvalide[j]) != -1) {
				inframe = false;
				break;
			}
		}
	}
	return raw_popup(href, target || '_blank', w, h, features, inframe);
}
function event_popup(e) {
	// to be passed as an event listener
	// pops up a window grabbing the url from the event source's href
	
	var retour = link_popup(e.currentTarget);
	if (retour != false)
	{
		e.preventDefault();
	}
}

// Envoi les requetes de rech. sans accent
function convertirRequete(fld){
	if (document.all&&document.getElementById) {
		var achar = "àáâãäåçèéêëìíîïñòóôõöøùúûüýÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝ";
		var schar = "aaaaaaceeeeiiiinoooooouuuuyAAAAAACEEEEIIIINOOOOOOUUUUY";
		var newstr = "";
		var str = fld.value;
		for (i=0; i<=str.length-1; i++) {
			var c = str.charAt(i);
			var pos = achar.indexOf(c);
			if (pos != -1) {
				newstr = newstr + schar.charAt(pos);
			}else{
				newstr = newstr + c;
			}
		}
		fld.value = newstr;
	}
	return true;
}

// Gestion de la taille du texte

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function sizes() {
	this.s = "76%";
	this.l = "93%";
	this.current = "s";
}
var $sizes = new sizes();
var textSize = readCookie("textSize");
if (textSize != null) $sizes["current"] = textSize;

function setTextSize(size) {
	document.body.style.fontSize = $sizes[size];
	document.getElementById("tailletexte").style.backgroundImage = "url("+imgdir+"taille_texte_"+size+".gif)";
	$sizes["current"] = size;
	document.cookie = "textSize=" + size + "; path=/;" ;
//	document.cookie = "textSize=" + size + "; path=/; domain=.gouv.qc.ca" ;
}
function toggleSize() {
	var newSize = "";
	$sizes["current"] == "s" ? newSize = "l" : newSize = "s";
	setTextSize(newSize);
}

function focusRecherche()
{
  var toCompare  = document.getElementById('qt').value;
  
  if(toCompare !=null) {
    if ((toCompare.indexOf(chercherfr) == 0) || (toCompare.indexOf(chercheren) == 0) || (toCompare.indexOf(chercheres) == 0)) {
       document.getElementById('qt').value = '';
    }
  }
}

function validRecherche()
{  
  focusRecherche();
}
function sendMail(a,b)
{
 var mail = a+"@"+b;
 return 'mailto:' + mail;
}
function pictoRollOver(itemMenu, imgSuffix) {
	document.getElementById("cat-" + itemMenu).style.backgroundImage = "url(" + pictodir + itemMenu + imgSuffix + ".gif)";
}

// url valides pour les bannieres
//var urlvalide = ["gouv.qc.ca","emploietudiant.qc.ca","emploiquebec.net","csst.qc.ca","inspq.qc.ca","bonjourquebec.com","quebecoisalosangeles.org","ccq.org","rcq.qc.ca","criq.qc.ca","financiereagricole.qc.ca","invest-quebec.com","rrsss02.qc.ca","agencesss04.qc.ca","santeestrie.qc.ca/agence","santemontreal.qc.ca","amt.qc.ca","aqwbj.org","clsc-chsld.qc.ca","lautorite.qc.ca","bnquebec.qc.ca","criq.qc.ca","amendes.qc.ca","cfai.qc.ca","cefrio.qc.ca","cqvb.qc.ca","cplt.com","commissairelobby.qc.ca","commission-regions-ressources.qc.ca","capitale.gouv.qc.ca","ccq.org","csst.qc.ca","cdpdj.qc.ca","csj.qc.ca","conseil-des-aines.qc.ca","electionsquebec.qc.ca","enpq.qc.ca","educaloi.qc.ca","emploiquebec.net","financiereagricole.qc.ca","fondationdelafaune.qc.ca","hema-quebec.qc.ca","hydroquebec.com","ireq.ca","irsst.qc.ca","ithq.qc.ca","inspq.qc.ca","investquebec.com","loto-quebec.com","mcq.org","mnba.qc.ca","pda.qc.ca","regie-energie.qc.ca","rcq.qc.ca","zonemirabel.com","sopfeu.qc.ca","saq.com","bingos-quebec.com","casinos-quebec.com","sepaq.com","loterie-video.qc.ca","spsnq.qc.ca","convention.qc.ca","spisb.com","sgfqc.com","innovatech.qc.ca","isq.qc.ca","innovatech-quebec.qc.ca","innovatechquebec.com","innovatech-regions.qc.ca","sonacc.com","soquij.qc.ca","surete.qc.ca","table-metropolitaine.org","telequebec.qc.ca","bonjourquebec.com","espacej.com","206.162.174.213","assnat.qc.ca","bnquebec.ca","assnat.qc.ca","bnquebec.qc.ca","virusdunil.info","granddictionnaire.com","bnq.qc.ca","banq.qc.ca","aieq.qc.ca","142.213.167.138","www.inforoutiere.qc.ca","services.bnquebec.ca","142.169.112.143","fadq.qc.ca","142.213.87.17"];
//var urlsinternes = ["www.gouv.qc.ca","/gouv.qc.ca","www2.gouv.qc.ca","www3.gouv.qc.ca","formulaire.gouv.qc.ca","reptel.gouv.qc.ca",".info.gouv.qc.ca","www.guidesante.gouv.qc.ca","communiques.gouv.qc.ca" ];
// Parametres de base pour les popups
var _POPUP_W = '790';
var _POPUP_H = '420';
var _POPUP_FEATURES = 'status=1,directories=1,menubar=1,location=1,scrollbars=1,resizable=1,toolbar=1';


// Demarrage des librairies sur onload

listen('load', window, function() {
	mlisten('click', getElementsByClass('popup','a'), event_popup );
});

//Code pour la date de dernière modification PGS
var txtDateDernModif;
var arrDateDernModif = new Array();
var boolNoArrDDM = true;
var langDate = document.getElementsByTagName("html").item(0).lang;

if (langDate == "") {
	langDate = "fr";
}

if (langDate == "es") {
	txtDateDernModif = 'Última actualización : ';
} else if (langDate == "en") {
	txtDateDernModif = 'Last update : ';
} else {
	txtDateDernModif = 'Date de dernière modification : ';
}

