/*
** +---------------------------------------------------+
** | Name :			~/main/javascript/common.js
** | Begin :		19/12/2005
** | Last :			06/05/2007
** | User :			Genova
** | Project :		Fire-Soft-Board 2 - Copyright FSB group
** | License :		GPL v2.0
** +---------------------------------------------------+
*/

// Navigateur utilisé
var Nav_Agent =		navigator.userAgent.toLowerCase();
var Nav_IE =		((Nav_Agent.indexOf("msie") != -1)  && (Nav_Agent.indexOf("opera") == -1)) ? true : false;
var Nav_Moz =		(Nav_Agent.indexOf("firefox") != -1) ? true : false;
var Nav_Opera =		(Nav_Agent.indexOf("opera") != -1 && parseInt(navigator.appVersion) >= 9) ? true : false;
var Nav_Safari =	((Nav_Agent.indexOf('safari') != -1) && (Nav_Agent.indexOf('mac') != -1)) ? true : false;
var Nav_Konqueror = (Nav_Agent.indexOf('konqueror') != -1);

/*
** Lit un cookie
** -----
** name ::		Nom du cookie
*/
function ReadCookie(name)
{
	var result = "";
	var my_cookie = " " + document.cookie + ";";
	var tmpname =  name + "=";
	var begin = my_cookie.indexOf(tmpname);
	var end;
	if (begin != -1)
	{
		begin += tmpname.length;
		end = my_cookie.indexOf(";", begin);
		result = unescape(my_cookie.substring(begin, end));
	}
	return (result);
}
 
/*
** Envoie un cookie
** -----
** name ::		Nom du cookie
** value ::		Valeur du cookie
*/
function SetCookie(name, value, on)
{
	var this_date = new Date();
	if (on)
	{
		this_date.setMonth(this_date.getMonth() + 1);
	}
	else
	{
		this_date.setMonth(this_date.getMonth() - 1);
	}

	var cookie_time = this_date.toGMTString();
	var my_cookie = name + "="+escape(value) + ";expires=" + cookie_time + " path=/;";
	document.cookie = my_cookie;
}

/*
** Coche / décoche un ensemble de checkbox
** -----
** form_name ::			Nom du formulaire
** element_name ::		Nom de la checbox
** is_checked ::		Définit si on coche / décoche la checkbox
*/
function check_boxes(form_name, element_name, is_checked)
{
	var chkboxes = document.forms[form_name].elements[element_name];
	var count = chkboxes.length;

	if (count)
	{
		for (var i = 0; i < count; i++)
		{
			chkboxes[i].checked = is_checked;
		}
	}
	else
	{
		chkboxes.checked = is_checked;
	}
	return true;
}

/*
** Fonction servant a cacher / afficher des éléments
*/
var hide_block = Array();
var blocks_height = new Array();
function hide(id)
{
	hide_block[id] ^= true;
	$(id).style.display = (hide_block[id]) ? 'none' : 'block';
}

function build_block(id_block, id_img, src_img_open, src_img_close, use_cookie)
{
	if (use_cookie == 1 && hide_block[id_block] == undefined)
	{
		cookie_value = ReadCookie(id_block);
		if (cookie_value == 'C')
		{
			hide_block[id_block] = false;
			id_img.src = src_img_close;
		}
		else
		{
			hide_block[id_block] = true;
		}
	}
	else if (hide_block[id_block] == undefined)
	{
		hide_block[id_block] = (use_cookie == 2) ? true : false;
	}

	hide_block[id_block] ^= true;
	if (hide_block[id_block])
	{
		if (use_cookie)
		{
			$(id_block).effects({duration: 500}).custom(
				{
					'height': [0, blocks_height[id_block]],
					'opacity': [0, 1]
				}
			);
		}
		else
		{
			$(id_block).style.display = 'block';
		}
		$(id_img).src = src_img_open;
	}
	else
	{
		if (use_cookie)
		{
			blocks_height[id_block] = $(id_block).offsetHeight;
			$(id_block).effects({duration: 500}).custom(
				{
					'height': [blocks_height[id_block], 0],
					'opacity': [1, 0]
				}
			);
		}
		else
		{
			$(id_block).style.display = 'none';
		}
		$(id_img).src = src_img_close;
	}

	if (use_cookie)
	{
		SetCookie(id_block, (hide_block[id_block]) ? "O" : "C", true);
	}
}

/*
** Lit le contenu d'un cookie et affiche ou non le block
*/
function read_block(block_name, img_name, img_src)
{
	cookie_value = ReadCookie(block_name);
	if (cookie_value == 'C')
	{
		blocks_height[block_name] = $(block_name).offsetHeight;
		$(block_name).style.height = '0px';
		$(block_name).style.opacity = 0;
		$(img_name).src = img_src;
	}
}

/*
** Equivalent htmlspecialchars() en php
*/
function htmlspecialchars(str)
{
	str = str.replace(/&/g, "&amp;");
	str = str.replace(/</g, "&lt;");
	str = str.replace(/>/g, "&gt;");
	str = str.replace(/\"/g, "&quote;");
	return (str);
}

/*
** Le contraire d'un htmlspecialchars() en php
*/
function unhtmlspecialchars(str)
{
	str = str.replace(/&lt;/g, "<");
	str = str.replace(/&gt;/g, ">");
	str = str.replace(/&quote;/g, "\"");
	str = str.replace(/&amp;/g, "&");
	return (str);
}

/*
** Supprime les espaces à gauche et à droite du mot
** -----
** str ::	Chaine à traiter
*/
function trim(str)
{
	while (str.substr(0, 1) == ' ' || str.substr(0, 1) == "\n")
	{
		str = str.substr(1);
	}

	while (str.substr(str.length - 1, 1) == ' ' || str.substr(str.length - 1, 1) == "\n")
	{
		str = str.substr(0, str.length - 1);
	}
	return (str);
}
