/* -----------------------------------------------------------------------------
	Fonctions de controle
------------------------------------------------------------------------------*/
function checkEmail(email) {
	var mail = email.toLowerCase();
//	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var filter = /^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/;
	if (!filter.test(mail) || mail.substr(-3)=='.co') {
		return false;
	}
	return true;
}


/* -----------------------------------------------------------------------------
	Fonctions systématiques
------------------------------------------------------------------------------*/
if(!window.console) {
	window.console = {
		log: function() {},
		error: function() {},
		warn: function() {}
	}
}


$('input[type=password]').attr("autocomplete","off");


/* -----------------------------------------------------------------------------
	Partage sur Facebook
------------------------------------------------------------------------------*/

$('a.facebook,a.fbShare').live('click', function() {
	var thisUrl=$(this).attr('href');
	var thisTitle=$(this).attr('title');
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(thisUrl)+'&t='+encodeURIComponent(thisTitle+''),'sharer', 'toolbar=0, status=0, width=626, height=436');
	return false;
});

/* -----------------------------------------------------------------------------
	Effacer un message
------------------------------------------------------------------------------*/
$('a.delete-discussion').live('click', function() {
	var to_thread = $(this).attr('rel');
	$.post("controller/ajaxActions.php", {
		'ajaxAction': 'message_delete',
		'to_thread': to_thread
	},
	function(data){
		if (data.erreur == '0') {
			if ($('div.discussion-extract[rel='+to_thread+']').size()==1) {
				$('div.discussion-extract[rel='+to_thread+']').slideUp().remove();
			}
			else
			{
				location.href = data.redirUrl;
			}
		}
		else
		{
			Zee.Modal.create(data.html.titre,data.html.content);
		}
	}, "json");
	return false;
});



/* -----------------------------------------------------------------------------
	Barre de login
------------------------------------------------------------------------------*/
/*
var $loginBar = $('#login-bar');

$('a.log-button').click(function(e) {
   	var $this = $(this);
   	if($this.isActive()) {
	   	e.preventDefault();
	   	e.stopPropagation();
   		$this.deactivate();
   		$loginBar.turnOff();
   	}
});
*/
/* -----------------------------------------------------------------------------
	View all
------------------------------------------------------------------------------*/
$('a.view-all').click(function(e) {
	e.preventDefault();
	var $this = $(this);
	var thisRel = $this.attr('rel');
	$('div[rel='+thisRel+']').slideDown('slow');
	$this.hide();
});

/*--------------------------------------------
	checkboxes openInviter
----------------------------------------------*/
$('input.checkAll').live('click', function() {
	var thisRel = $(this).attr('rel');	// Champ d'action
	thisRel = thisRel.substr(1, thisRel.length);
	var isChecked=$(this).attr('checked')?1:0;
	if (isChecked==1) { $('input[rel='+thisRel+']').attr('checked','isChecked'); }
	else { $('input[rel='+thisRel+']').removeAttr('checked'); }
});


/* -----------------------------------------------------------------------------
	Formulaires Touche entrée sous IE7 + IE8
------------------------------------------------------------------------------*/
$('html.ie7 form input, html.ie8 form input').live("keydown", function(event) {
	if (event.keyCode == 13) {
		submitForm($(this).parents('form'));
		return false;
	}
});

/* -----------------------------------------------------------------------------
	Prise en charge des #
------------------------------------------------------------------------------*/
function rebase_anchors(){
	var anchors = document.getElementsByTagName("a");
	var basehref = document.getElementsByTagName("base")[0].href;
	var url = window.location.href;
	if(url.indexOf("#") > 0) url = url.substr(0, url.indexOf("#")); //strip hash
	if(basehref) {
		for(var i = 0; i < anchors.length; i++) {
			var anchor = anchors[i];
			poundPos = anchor.href.indexOf("/#");
			if (poundPos > 0) {
				anchor.href = url + anchor.href.substr(poundPos + 1);
			}
		}
	}
}


