// [Cookie] Sets value in a cookie
function setCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

// [Cookie] Gets a value from a cookie
function getCookie(cookieName) {
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
}

function generatePassword(passwordlength)
{
	if (passwordlength == '') passwordlength = 8
    var pwchars = "abcdefhjmnpqrstuvwxyz23456789!*.,:";
    var passwd = '';
    for ( i = 0; i < passwordlength; i++ ) {
        passwd += pwchars.charAt( Math.floor( Math.random() * pwchars.length ) )
    }
    return passwd;
}

function popup(url, w, h, params)
{
	/*
	var width   = (w) ? w : 240;
    var height  = (h) ? h : 140;
    var left    = parseInt((screen.availWidth/2) - (width/2));
    var top     = parseInt((screen.availHeight/2) - (height/2));
    var options = "width=" + width + ",height=" + height +
                  ",directories=no,status=no,menubar=no,copyhistory=no,toolbar=no,location=no,resizable=yes" +
                  ",left=" + left + ",top=" + top + ",screenX=" + left + ",screenY=" + top;

	var parameters = (params) ? ',' + params : ',scrollbars=no';

	window.open(url, 'popup', options + params);
	*/
	return hs.htmlExpand(null, {objectType: 'iframe', src: url, width: w, height: h});
} 

function changePage(formName, pageNumber)
{
	var form = eval('document.' + formName);
	form.from.value = pageNumber;
	form.submit();
}

function favoris()
{
	if ( navigator.appName == 'Microsoft Internet Explorer' ){ window.external.AddFavorite("http://annonces.ladepeche.fr","La Dépêche Annonces"); }
	else  if (navigator.appName=='Netscape') {window.sidebar.addPanel("La Dépêche.fr","http://annonces.ladepeche.fr","");}
	else if (navigator.appName=='Opera'){ alert('Vous utilisez Opéra. Pour ajouter à vos Favoris le site de La Dépêche.fr, faites la combinaison de touches [CLTR] + D'); }
	else { alert('Pour ajouter à vos Favoris le site de La Dépêche.fr, faites la combinaison de touches [CMD] + D'); }
}

/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects
	 for(var item in arr) {
	  var value = arr[item];
	 
	  if(typeof(value) == 'object') { //If it is an array,
	   dumped_text += level_padding + "'" + item + "' ...\n";
	   dumped_text += dump(value,level+1);
	  } else {
	   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
	  }
	 }
	} else { //Stings/Chars/Numbers etc.
	 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function checkDate(formElement) {
 	// regular expression to match required date format
 	re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
 	if(formElement.value != '' && !formElement.value.match(re)) {
 		alert("Date invalide (" + formElement.value + "). La date doit être de la forme JJ/MM/AAAA");
 		formElement.focus();
 		return false;
 	}
 	return true;
}

function checkMail(mail)
{   
    var emailFormat = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if ((mail == '') && (!mail.match(emailFormat)))
		return false;
	else     
		return true;   
}


function passRecover()
{
	login = $('log').value;
	var pars = "type=passrecover" + "&log=" + login;
	var mAjax = new Ajax.Request("/ajax.php", {method: 'get', parameters: pars, asynchronous:false, onSuccess: function(transport) { recoverRetour(transport); } });
	return true;
}

function recoverRetour(transport)
{
	if(transport.responseText == 'true') {
		$('recover').update('Vos informations ont été envoyées par e-mail');
	}
	else
	{
		$('recover').update("L'adresse e-mail est incorrecte");
	}
}
function markAllRows( container_id ) {
	var rows = document.getElementsByName('del[]');
	var checkbox;

	for ( var i = 0; i < rows.length; i++ ) {
		checkbox = rows[i];
		if ( checkbox && checkbox.type == 'checkbox' ) {
			checkbox.checked = true;
		}
	}
	return true;
}
function unMarkAllRows( container_id ) {
	var rows = document.getElementsByName('del[]');
	var checkbox;

	for ( var i = 0; i < rows.length; i++ ) {
		checkbox = rows[i];
		if ( checkbox && checkbox.type == 'checkbox' ) {
			checkbox.checked = false;
		}
	}
	return true;
}
function cocheCase (container_id)
{
	if(document.getElementById(container_id).checked)
	{
		document.getElementById(container_id).checked = false;
	}
	else
	{
		document.getElementById(container_id).checked = true;
	}
}

function normalizeText(obj)
{
	obj.value = obj.value.replace(/([,;:])([^ ])/g,"$1 $2");
}
