function SetCookie (name,value,expdate,path){
	//recuperation des arguments et du nombre d'argument 
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	//pose du cookie
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function getCookieVal (offset){
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape (document.cookie.substring (offset, endstr));
}

function GetCookie (name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring (i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}
	return false;
}
 
function EnregisterCookie(cookie){
	var ref = window.opener.caddie;
	var path = "/";
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000));
	SetCookie(cookie,ref,expdate,path);
	setTimeout('window.close()',1000);
}
function AjouterRef(cookie,ref){
	var anciennes_ref = GetCookie(cookie);
	var cgi = "/cgi-bin/cookie/set_cookie.cgi?cookie=" + cookie;
	if(!anciennes_ref){
		caddie = ref;
		f1 = window.open(cgi,'album','height=110,width=310');
	}else{
		//On regarde si la reference n'est pas deja enregistrée;
		var resultat = anciennes_ref.search(ref);
		if(resultat == -1){
			var insert_ref = ";" + ref;
			nouvelles_refs = anciennes_ref + insert_ref;
			caddie = nouvelles_refs;
			f1 = window.open(cgi,'album','height=110,width=310');
		}else{
			caddie = anciennes_ref;
			f1 = window.open(cgi,'album','height=110,width=310');
		}
	}
}

function GetMyCookie(){
	var cookie = 'imb-immobilier_caddie';
	var refs = GetCookie(cookie);
	if(!refs){
		alert("Votre caddie est vide!");
	}else{
		window.location='/cgi-bin/album/affiche_caddie.cgi?refs=' + refs + '&cookie=' + cookie;
	}
}

function DeleteRef(cookie,ref){
	var anciennes_refs = GetCookie(cookie);
	var delete_ref = ref;
	var pointvirgule = /;$/;
	var nouvelles_refs = new String();
	nouvelles_refs = anciennes_refs.replace(delete_ref,"");
	nouvelles_refs = nouvelles_refs.replace(pointvirgule,'');
	var path = "/";
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000));
	SetCookie(cookie,nouvelles_refs,expdate,path);
	window.location = '/cgi-bin/album/affiche_caddie.cgi?refs=' + nouvelles_refs + '&cookie=' + cookie;
}

function EffacerCaddie(cookie){
	var path = "/";
	var refs = "";
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000));
	SetCookie(cookie,refs,expdate,path);
	window.location = '/';
}
