// Change une photo

function change_photo(photo, id, largeur, hauteur)
{
	document.getElementById(id).src = photo ;
	document.getElementById(id).width = largeur ;
	document.getElementById(id).height = hauteur ;
}		

// Supprimer les espaces blancs en début et fin de chaîne

function trim(string)
{
	return string.replace(/(^\s*)|(\s*$)/g,'') ;
}

// Ecrit un cookie
function ecrire_cookie(nom, valeur)
{
	var argv = ecrire_cookie.arguments ;
	var argc = ecrire_cookie.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 ;
	document.cookie = nom+"="+escape(valeur) +
		((expires==null) ? "" : ("; expires="+expires.toGMTString())) +
		((path==null)    ? "" : ("; path="+path)) +
		((domain==null)  ? "" : ("; domain="+domain)) +
		((secure==true)  ? "; secure" : "") ;
}

//  Utilisée par lire_cookie
function getCookieVal(offset)
{
	var endstr = document.cookie.indexOf(";", offset) ;
	if(endstr == -1)
		endstr=document.cookie.length ;
	return unescape(document.cookie.substring(offset, endstr)) ;
}

//  Lit un cookie
function lire_cookie(nom)
{
	var arg = nom + "=" ;
	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 null ;
}

// Ajoute une référence au panier
function ajoute_panier(reference)
{
	liste_reference = lire_cookie("panier") ;
	if (liste_reference == null)
	{
		liste_reference = reference + ";" ;
	}
	else
	{
		var position = liste_reference.indexOf(reference,0) ;
		if (position == -1)
		{
			liste_reference = liste_reference + reference + ";" ;
		}
	}
	date_expiration = new Date ;
	date_expiration.setMonth(date_expiration.getMonth()+1) ;
	ecrire_cookie("panier", liste_reference, date_expiration, "/location/") ;
	document.getElementById(reference).className = "annonces panier" ;
}

// Supprime une référence du panier
function supprime_panier(reference)
{
	liste_reference = lire_cookie("panier") ;
	var position_debut = liste_reference.indexOf(reference+";",0) ;
	if(position_debut != -1)
	{
		position_fin = liste_reference.indexOf(";",position_debut) ;
		liste_reference = liste_reference.substring(0,position_debut) + liste_reference.substring(position_fin+1,liste_reference.length) ;
	}
	date_expiration = new Date ;
	date_expiration.setMonth(date_expiration.getMonth()+1) ;
	ecrire_cookie("panier", liste_reference, date_expiration, "/location/") ;
	document.getElementById(reference).className = "annonces" ;
}

/**
 * Compteur pour zone TEXTAREA
 */

// Début du compteur
function initCompteur(id_textarea, id_compteur, maxlength)
{
	var el_textarea = $$('textarea')[0] ;
	var update = function()
		{
			l = el_textarea.value.length ;
			$(id_compteur).innerHTML = 'Reste : ' + (maxlength - l) + ' caractères (' + l + ' / ' + maxlength + ')' ;
			$(id_compteur).style.color = (l > maxlength) ? '#c00' : '#0a0' ;
		} ;
	el_textarea.addEvent('keyup', update) ;
	window.addEvent('domready', update) ;
}

/******************************************************************************
 *** Fonctions de popup
 ******************************************************************************/

/***
 * Position top/left pour centrer un élément selon sa taille
 ******************************************************************************/

function centerX(w)
{
	return (screen.width-w)/2 ;
}

function centerY(h)
{
	return (screen.height-h)/2 ;
}

/***
 * Ouvre une popup
 ******************************************************************************/

function popup(src, w, h)
{
	var target = '_blank' ;
	var options
		= 'toolbar='      + 'no'
		+ ',location='    + 'no'
		+ ',directories=' + 'no'
		+ ',status='      + 'no'
		+ ',menubar='     + 'no'
		+ ',scrollbars='  + 'yes'
		+ ',resizable='   + 'no'
		+ ',width='       + w
		+ ',height='      + h
		+ ',left='        + centerX(w)
		+ ',top='         + centerY(h)
		;
	var newWindow = window.open(src, target, options) ;
}

/***
 * Affiche une image dans une popup sans menus et sans bordures
 ******************************************************************************/

function popupImage(src, w, h, t)
{
	var target = '_blank' ;
	var options
		= 'toolbar='      + 'no'
		+ ',location='    + 'no'
		+ ',directories=' + 'no'
		+ ',status='      + 'no'
		+ ',menubar='     + 'no'
		+ ',scrollbars='  + 'no'
		+ ',resizable='   + 'no'
		+ ',width='       + w
		+ ',height='      + h
		+ ',left='        + centerX(w)
		+ ',top='         + centerY(h)
		;
	var newWindow = window.open('', target, options) ;
	newWindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n') ;
	newWindow.document.write('<html xmlns="http://www.w3.org/1999/xhtml">\n') ;
	newWindow.document.write('\t<head>\n') ;
	newWindow.document.write('\t\t<title>'+t+'</title>\n') ;
	newWindow.document.write('<style type="text/css">\n') ;
	newWindow.document.write('body\n') ;
	newWindow.document.write('{\n') ;
	newWindow.document.write('\tbackground: url(\''+src+'\');\n') ;
	newWindow.document.write('\toverflow: none;\n') ;
	newWindow.document.write('}\n') ;
	newWindow.document.write('</style>\n') ;
	newWindow.document.write('\t</head>\n') ;
	newWindow.document.write('\t<body></body>\n') ;
	newWindow.document.write('</html>\n') ;
	newWindow.document.close() ;
	newWindow.document.title = t ;
}

/***
 * Fermer une popup en rechargeant la fenêtre appelante
 *****************************************************************************/

function reloadOpener(url)
{
	window.opener.location = url ;
	close() ;
}
