
var BASKET_COOK_NAME = "BASKET"+GID;
var SELECT_COOK_NAME = "SELECT"+GID;
var IDENT_COOK_NAME = "IDENT"+GID;
var ADR_COOK_NAME = "ADR"+GID;

/*** Fonction de construction d'URL ****************************/


function getUrl(v, params) {
    var url = "";
    var squery = "";
    if (typeof(params) == "string") {
	squery = params;
    } 
    else if (typeof(params) != "undefined") {
	for(var name in params) {
	    var value = params[name];
	    if (typeof(value) == "object" && value.length > 0) {
		for(var i=0; i < value.length ; i++) {
		    squery += "&" + name + "[]=" + urlencode(value[i]);
		}
	    }
	    else {
		squery += "&" + name + "=" + urlencode(value);
	    }

	}
	if (squery.length > 0) squery = squery.substr(1);
    }
    if (isDiffusion) {
	if (v == "catalogue") url = "./catalogue"+GID+".php";
	else url = "./cop_"+v+GID+".php";
	if (squery.length > 0) url += "?" + squery;	
    }
    else {
	var node = context.mainDocument.nodeFromGId(GID);
	var render = v == "catalogue" ? 0 : "cop_"+v;
	var search = "node="+context.nodeSrc(node);
	if (render != 0) search += "&render="+render;
	if (squery.length > 0) search += "&" + squery;
	url = context.processor.processUrl(search, null);
    }
    return url;
}

/*** Fonction de Localisation ****************************/

function Locale(index, params) {
    var res = "";
    if (!thesaurus[index])
	res = index;
    else {
	res = thesaurus[index];
	if (params != null) {
	    if (typeof(params) == "string") {
		var v = params;
		params = new Array(1);
		params[0] = v;
	    }
	    for(var i=0; i < params.length ; i++) {
		var j = res.indexOf("%"+(i+1));
		if (j != -1) {
		    var g = res.substr(0,j);
		    var d = res.substr(j+2);
		    res = g + params[i] + d;
		}
	    }
	}
    }
    return res;
}

// Détermination du navigateur
IsNav = new Object();
IsNav.agt   = navigator.userAgent.toLowerCase();
IsNav.os    = navigator.platform.toLowerCase();

var p = IsNav.agt.indexOf('msie');
if (p != -1) {
    var q = IsNav.agt.indexOf(';',p);
    IsNav.version = parseFloat(IsNav.agt.substring(p+5,q));
    IsNav.IE = true;
}
else if (IsNav.agt.indexOf('mozilla')!=-1 && IsNav.agt.indexOf('spoofer')==-1 && IsNav.agt.indexOf('compatible') == -1) {
    var q = IsNav.agt.indexOf(' ');
    IsNav.version = parseFloat(IsNav.agt.substring(8,q));
    IsNav.NAV = true;
}
IsNav.IE4   = IsNav.IE && parseInt(IsNav.version) >= 4;
IsNav.NAV4  = IsNav.NAV && parseInt(IsNav.version) == 4;
IsNav.DOM   = IsNav.NAV && IsNav.version >= 5.0; 


/*** Fonction codant une chaine pour qu'elle soit une valeur valide de variable passée par GET ****/

function urlencode(v)  {
    var src = escape(new String(v));
    var dest = "";
    for(var i=0 ; i < src.length ; i++)	{
	var c = src.charAt(i);
	if (c == "+") {
	    dest += "%2B";
	}
	else if (c == "=") {
	    dest += "%3D";
	}
	else if (c == "/") {
	    dest += "%2F";
	}
	else {
	    dest += c;
	}
    }
    return dest;
}


/*** Fonction de manipulation de chaines : suppression des blancs en début et en fin de chaine ***/

function trim( s ) {
    var l = s.length;
    var i,j;
    for(i=0 ; i < l && s.charAt(i) == " " ; i++);
    for(j=l-1; j > i && s.charAt(j) == " " ; j--);
    return i == l ? "" : s.substring(i,j+1);
}

/*** Gestion des fenêtres ****************************/

win = null;

function OpenWin( url , w , h , bResizable , bScrollable ) {
    if (typeof(bResizable) == "undefined")   bResizable = true;
    if (typeof(bScrollable) == "undefined")  bScrollable = false;
    var left = (screen.width-w)/2;
    var top = (screen.height-h)/2 ;
    var xycoord = "left="+left+",top="+top;
    var params = "status=no,toolbar=no,menubar=no,scrollbars="+(bScrollable ? "yes" : "no");
    params += ",resizable=" + (bResizable ? "yes" : "no") + ",width=" +  w + ",height=" + h + "," + xycoord;
    if (win != null && !win.closed) win.close();
    return window.open( url , "win" , params);
}


/*** Gestion du panier ****************************/


var Panier = new Array();
var Panier_Taille = 0;
var Panier_bLectureCookie = false;
var C_PANIER_AJOUTER     = 1;
var C_PANIER_SUPPRIMER   = 2;
var C_PANIER_INCREMENTER = 3;


function Panier_Reponse(code,action) {
}

function Panier_Vider() {
    var k = 0;
    while (Cookie_Existe(BASKET_COOK_NAME + k)) {
	Cookie_Supprimer(BASKET_COOK_NAME + k);
	k++;
    }
}

function Panier_Incrementer(code, qt) {
    _Panier_Action(code,qt,C_PANIER_INCREMENTER);
}

function Panier_Ajouter(code, qt) {
    _Panier_Action(code,qt,C_PANIER_AJOUTER);
}

function Panier_Supprimer(code) {
    _Panier_Action(code,0,C_PANIER_SUPPRIMER);
}

function _Panier_Action(code,qt,action) {
    if (!Panier_bLectureCookie) {
	Panier_bLectureCookie = true;
	var i = 0;
	var v = "";
	while ((v = Cookie_Valeur(BASKET_COOK_NAME + i)) != "") {
	    var arr = v.split(";");
	    for(var j=0 ; j < (arr.length-1) ; j+= 2) {
		var _qt = parseInt(arr[j+1], 10);
		if (!isNaN(_qt) && _qt > 0)
		    Panier[arr[j]] = _qt;
	    }
	    i++;
	}
    }
    if (action == C_PANIER_AJOUTER) {
	if (typeof(Panier[code]) == "undefined")
	    Panier_Taille++;
	Panier[code] = qt;
    }
    else if (action == C_PANIER_INCREMENTER) {
	if (typeof(Panier[code]) == "undefined") {
	    Panier[code] = qt;
	    Panier_Taille++;
	}
	else {
	    Panier[code] += qt;
	}
    }
    else if (action == C_PANIER_SUPPRIMER) {
	delete Panier[code];
	Panier_Taille--;
    }
    var scookie = "";
    var len = 0;
    var k = 0;
    for(var _code in Panier) {
	if (scookie.length >= COOKIE_MAX_SIZE) {
	    Cookie_Ajouter(BASKET_COOK_NAME + k ,scookie);
	    k++;
	    scookie = "";
	}
	if (!isNaN(Panier[_code]) && Panier[_code] > 0) {
	    if (scookie.length > 0) {
		scookie += ";";
	    }
	    scookie += _code + ";" + Panier[_code];
	}
    }
    if (scookie.length > 0) {
	Cookie_Ajouter(BASKET_COOK_NAME + k ,scookie);
	k++;
    }
    while (Cookie_Existe(BASKET_COOK_NAME + k)) {
	Cookie_Supprimer(BASKET_COOK_NAME + k);
	k++;
    }
    Panier_Reponse(code, action);
}



/*** Gestion de la sélection ****************************/

var Selection = new Array();
var C_SELECTION_AJOUTER   = 1;
var C_SELECTION_SUPPRIMER = 2;


function Selection_Ajouter(code) {
    Selection_Action(C_SELECTION_AJOUTER, code);
}

function Selection_Supprimer(code) {
    Selection_Action(C_SELECTION_SUPPRIMER, code);
}

function RetourSelection(code,action) {
    if (!SELECTION_MODECOOKIE) {
	Act_UnSet();
    }
    Selection_Reponse(code,action);
}

function Selection_Reponse(code,action) {
    if (action == C_SELECTION_AJOUTER) {
	alert(Locale("cop1",code));
    }
}

function Selection_Action(action, code) {
    if (typeof(SELECTION_MODECOOKIE) != "undefined" && !SELECTION_MODECOOKIE) {
	Act_Set();
	var boxMesg = Act_GetDefaultBox();
	boxMesg.setLabel(Locale("cop2"));
	Act_SetControlBox(boxMesg);
	Act_AddParameter("code", code);
	Act_AddParameter("ACTION", action);
	Act_Lauch("ctr_selection");
    }
    else {
	Selection = new Array();
	var i = 0;
	var v = "";
	while ((v = Cookie_Valeur(SELECT_COOK_NAME + i)) != "") {
	    var arr = v.split(";");
	    for(var j=0 ; j < arr.length ; j++) {
		Selection[arr[j]] = 1;
	    }
	    i++;
	}
	if (action == C_SELECTION_AJOUTER) {
	    if (Selection[code] == 1) {
		RetourSelection(code,action);
		return;
	    }
	    Selection[code] = 1;
	}
	else {
	    if (typeof(Selection[code]) == "undefined") {
		RetourSelection(code,action);
		return;
	    }
	    delete Selection[code];
	}
	var k = 0;
	var scookie = "";
	for(var code in Selection) {
	    if (scookie.length >= COOKIE_MAX_SIZE) {
		Cookie_Ajouter(SELECT_COOK_NAME + k ,scookie, COOKIE_UNLIMITED_MEMO);
		k++;
		scookie = "";
	    }
	    if (scookie.length > 0) {
		scookie += ";";
	    }
	    scookie += code;
	}
	if (scookie.length > 0) {
	    Cookie_Ajouter(SELECT_COOK_NAME + k , scookie , COOKIE_UNLIMITED_MEMO);
	    k++;
	}
	while (Cookie_Existe(SELECT_COOK_NAME + k)) {
	    Cookie_Supprimer(SELECT_COOK_NAME + k);
	    k++;
	}
	RetourSelection(code,action);
    }
}



/*** Gestion fenêtre détail ****************************/

function OpenWin_Detail( code , bPanier, bSelection , bExpress ) {
    var params = new Array;
    params["code"]  = code;
    params["PANIER"] = bPanier ? "1" :"0";
    params["SELECTION"] = bSelection ? "1" :"0";
    params["EXPRESS"] = bExpress ? "1" :"0";
    var url = getUrl("detail", params);
    win = OpenWin (url, 640 , 360 , false , false);
    win.focus();
}


/*** Gestion fenêtre de recherche ****************************/

function OpenWin_Recherche() {
    win = OpenWin (getUrl("recherche"), 715 , 580, false,false);
}


/*** Gestion de zones d'affichage à positionnement absolu  ****************************/

var areas = new Object;

function Area(ID, bcenter) {
    this.bcenter = bcenter ? true : false;	  
    this.ID = ID;
    areas[ID] = this;
}

window.onscroll = function() {
    for(var ID in areas) {
	if (areas[ID].isVisible()) {
	    areas[ID].display(true);	
	}    
    }
}

Area.prototype.display  = Area_display;
Area.prototype.getForm  = Area_getForm;
Area.prototype.setLabel = Area_setLabel;
Area.prototype.exists = Area_exists;
Area.prototype.isVisible = Area_isVisible;

function Area_isVisible() {
    var b = false;
    if (!this.exists()) return false;
    if (IsNav.IE4) {
	var area = document.all(this.ID);
	b = area.style.display == "block";
    }
    else if (IsNav.NAV4) {
	var area = document.layers[this.ID];
	b = area.visibility == "show";
    }
    else if (IsNav.DOM) {
	var area = document.getElementById(this.ID);
	b = area.style.display == "block";
    }
    return b;
}
    
function Area_exists() {
    var b = false;
    if (IsNav.IE4)
	b = document.all(this.ID);
    else if (IsNav.NAV4)
	b = document.layers[this.ID];
    else if (IsNav.DOM)
	b = document.getElementById(this.ID);
    return b ? true : false;
}

function Area_setLabel(label) {
    if (this.exists()) {
	if (IsNav.IE4) { 
	    var area = document.all(this.ID);
	    area.all("idLabel").innerHTML = label;
	}
	else if (IsNav.NAV4) {
	    var area = document.layers[this.ID];
	    area.document.layers["idLabel"].document.write(label);
	    area.document.layers["idLabel"].document.close();
	}
	else if (IsNav.DOM) {
	    var area = document.getElementById(this.ID);
	    GetElementById(area,"idLabel").innerHTML = label;
	}
    }
}

function GetElementById(obj, id) {
    var heap = new Array();
    var deep = 0;
    heap[0] = obj.childNodes;
    heap[1] = 0;
    var j = 0;
    while(deep >= 0) {
	j++;
	if (j == 100) return null;
	if (heap[deep+1] == heap[deep].length)
	    deep -= 2;
	else {
	    var i = heap[deep+1];
	    var node = heap[deep].item(i);
	    if (node.id == id) {
		return node;
	    }	
	    else {
		heap[deep+1]++;
		if (node.nodeType == 1 && node.firstChild != null) {
	  	   deep+=2;
		   heap[deep] = node.childNodes;
		   heap[deep+1] = 0;
	      }
	    }
	}
    }
}


function Area_getForm() {
    if (this.exists()) {
	var form = null;
	if (IsNav.IE4) {
	    var l = document.all(this.ID).all.tags("form");
	    if (l.length > 0)
		form = l[0];
	}
	else if (IsNav.NAV4) {
	    var l = document.layers[this.ID].document.forms;
	    if (l.length > 0)
		form = l[0];
	}
	else if (IsNav.DOM) {
	    var l = document.getElementById(this.ID).getElementsByTagName("form");
	    if (l.length > 0)
		form = l[0];
	}
    }
    return form;
}

function Area_display(b) {
    if (this.exists()) {
	if (IsNav.IE4) { 
	    var area = document.all(this.ID);
	    var w = parseInt(area.style.width,10);
	    var h = parseInt(area.style.height,10);
	    area.style.zIndex = 10;
	    if (b && this.bcenter) {
		area.style.pixelLeft = document.body.scrollLeft + (document.body.clientWidth - w) / 2;
		area.style.pixelTop =  document.body.scrollTop + (document.body.clientHeight - h) / 2;
	    }
	    area.style.display = b ? "block" : "none";
	}
	else if (IsNav.NAV4) {
	    var area = document.layers[this.ID];
	    var w = area.clip.width;
	    var h = area.clip.height;
	    area.zIndex = 10;
	    if (b && this.bcenter) {
		area.x = window.pageXOffset + (window.innerWidth - w)/ 2 ;
		area.y = window.pageYOffset + (window.innerHeight - h)/ 2 ;
	    }
	    area.visibility = b ? "show" : "hide";
	}
	else if (IsNav.DOM) {
	    var area = document.getElementById(this.ID);
	    var w = parseInt(area.style.width,10);
	    var h = parseInt(area.style.height,10);
	    area.style.zIndex = 10;
	    if (!b)
		area.style.display = "none";
	    else if (this.bcenter) {
		area.style.display = "block";
		if (IsNav.IE) {
		    area.style.left = (document.body.scrollLeft + (document.body.clientWidth - w) / 2) + "px";
		    area.style.top =  (document.body.scrollTop + (document.body.clientHeight - h) / 2) + "px";
		}
		else {
		    area.style.left = (window.pageXOffset + (window.innerWidth - w)/ 2) + "px";
		    area.style.top  = (window.pageYOffset + (window.innerHeight - h)/ 2) + "px";
		}
	    }
	}
    }
}



/*** Gestion des actions ****************************/

var _ACT_ACTIVE_  = false;
var _ACT_CTRBOX_  = null;
var _ACT_PARAMS_  = new Object();
var boxActMesg    = null;


if (IsNav.IE4 || IsNav.DOM) {
    //document.write("<iframe id='idBuffer_Act' style=''></iframe>");
    document.write("<iframe id='idBuffer_Act' style='visibility:hidden;width:0px;height:0px;border-style:none'></iframe>");
}
else if (IsNav.NAV4) {
    document.write("<layer id='idBuffer_Act' visibility=hide></layer>");
}

function Act_Set() {
    if (Act_IsInactive()) {
	_ACT_ACTIVE_ = true;
	_ACT_CTRBOX_ = null;
	for(var name in _ACT_PARAMS_)
	    delete _ACT_PARAMS_[name];
    }
}

function Act_GetDefaultBox() {
    if (boxActMesg == null) {
	boxActMesg = new Area("idActMesg", true);
    }
    return boxActMesg;
}

function Act_AddParameter(name,value) {
    _ACT_PARAMS_[name] = value;
}

function Act_DisplayControlBox() {
    Act_ShowControlBox();
}

function Act_HideControlBox() {
    _ACT_CTRBOX_.display(false);
}

function Act_ShowControlBox() {
    _ACT_CTRBOX_.display(true);
}

function Act_Lauch(url) {
    var buffer = null;
    if (IsNav.IE4)
	buffer = document.frames.idBuffer_Act;
    else if (IsNav.NAV4)
	buffer = document.layers.idBuffer_Act;
    else if (IsNav.DOM)
	buffer = document.getElementById("idBuffer_Act");
    
    if (buffer != null) {
	if (_ACT_CTRBOX_ != null)
	    Act_DisplayControlBox();
	var squery = "";
	for(var name in _ACT_PARAMS_)
	    squery += "&" + name + "=" + urlencode(_ACT_PARAMS_[name]);
	squery = squery.substring(1);
	if (isDiffusion) {
	    if (IsNav.IE4)
		buffer.document.location = getUrl(url, squery);
	    else if (IsNav.NAV4 || IsNav.DOM)
		buffer.src = getUrl(url, squery);
	}
	else {
	    var output = " <script language='JavaScript' src=\"";
	    output += getUrl(url, squery);
	    output += "\"></" + "script>";
	    buffer.document.write(output);
	    buffer.document.close();
	}
    }
}

function Act_SetControlBox( box ) {
    _ACT_CTRBOX_ = (box == false) ? null : box;
}

function Act_UnSet() {
    if (_ACT_ACTIVE_) {
	_ACT_ACTIVE_ = false;
	if (IsNav.IE4) {
	    var buffer = document.frames.idBuffer_Act;
	    buffer.src = "about:blank";
	}
	else if (IsNav.NAV4) {
	    var buffer = document.layers.idBuffer_Act;
	    buffer.document.open("text/html")
		buffer.document.close();
	}
	else if (IsNav.DOM) {
	    var buffer = document.getElementById("idBuffer_Act");
	    buffer.src = "about:blank";
	}
	if (_ACT_CTRBOX_ != null)
	    Act_HideControlBox();
	_ACT_CTRBOX_ = null;
    }
}


function Act_IsInactive() {
    if (_ACT_ACTIVE_) {
	if (_ACT_CTRBOX_ != null)
	    Act_ShowControlBox();
	return false;
    }
    return true;
}


