Zee = window.Zee || {};

Zee.checkEmail = function(email) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	return !( !filter.test(email) || email.substr(-3)=='.co');
};

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

// Change le status d'un élément
function switchStatus(selecteur, status){
	if (status == 'n') {
		$(selecteur).parent('div').addClass('error');
	}
	else
	{
		$(selecteur).parent('div').removeClass('error');
	}
}


function countErrors(selector)
{
	var nbErrors = $(selector).find('div.error:visible').length;
	return nbErrors;
}

// A défaut de blink on focus sur le premier champ
function blinkError(selector){
	$(selector).find('div.error:first').children('input').focus();
//	$(selector).find('span.n:first').fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
}


/*--------------------------------------------
	formulaires standard
----------------------------------------------*/

	$('form input').live("keydown", function(event) {
		if (event.keyCode == 13) {
			return false;
		}
	});


	// Champ mail
	$('input.obligatoire_mail:visible').live('keyup blur', function() {
		if (!checkEmail($(this).val())) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});

	// Champ obligatoire
	$('input.obligatoire:visible, textarea.obligatoire:visible').live('keyup blur', function() {
		if ($(this).val() == '' || $(this).val() == null) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});

	// Champ numérique
	$('input.obligatoire_numerique, input.numerique, input.cp').keydown(function(e)
     {
		var key = e.charCode || e.keyCode || 0;
		var thisVal = $(this).attr('value');
		if (key == 38)
		{
			$(this).attr('value',parseInt(thisVal)+1);
		}
		else if (key == 40)
		{
			var newVal = parseInt(thisVal)-1;
			if (newVal < 1) {newVal = 1;}
			$(this).attr('value',newVal);
		}
		// autorise backspace, tab, delete, fleches, chiffres, pavé numérique
		if (
			key == 8 ||
			key == 9 ||
			key == 46 ||
			(key >= 37 && key <= 40) ||
			(key >= 48 && key <= 57) ||
			(key >= 96 && key <= 105))
		{
			return true;
		}
		return false;
	});

	// Checkbox obligatoire
	$('input.obligatoire_checkbox:visible').live('click', function() {
		var thisName = $(this).attr('name');
		var isChecked  = $('input[name='+thisName+']:checked').length;
		if (isChecked == 1) {
			switchStatus(this, 'o');
			//console.log('checked');
		}
		else
		{
			switchStatus(this, 'n');
		}
	});

	// Checkbox obligatoire
	$('input.obligatoire_radio:visible').live('click', function() {
		switchStatus(this, 'o');
	});


	// Champ obligatoire numérique
	$('input.obligatoire_numerique:visible, select.obligatoire_numerique:visible').live('keyup blur', function() {
		if ($(this).val() == '' || $(this).val() == null || isNaN($(this).val())) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});



	$('div.modal A.submit').live('click', function() {
		var thisForm = $(this).parents('div.modal').find('form');
		$(thisForm).find('input:visible, textarea:visible').each(function(index) {
			$(this).focus().blur();
		});
		// reste-t-il des erreurs ?
		if (countErrors($(thisForm)) == 0) {
			callFloodlight_same('1160831', 'pyrel329', '1106-278');
			$.post("api/ajaxActions.php", {
				'ajaxAction': $(thisForm).find('input[name=ajaxAction]').val(),
				'serialised': $(thisForm).serialize()
			},
			function(data){
				if (data.erreur == '0') {
					Zee.Modal.close();
					window.setTimeout(function() {
						Zee.Modal.create(data.html.titre,data.html.content);
					}, 500);
				}
			}, "json");
		}
		else
		{
			blinkError($(thisForm));
		}
		return false;
	});


/* -----------------------------------------------------------------------------
	Tracking Floodlight / Spot
------------------------------------------------------------------------------*/

function callFloodlight_new(source, type, cat) {
	var tag_url="http://fls.doubleclick.net/activityi;src=" + source + ";type=" + type + ";cat=" + cat + ";ord=1;num="+Math.floor(Math.random()*999999)+"?";
	if(document.getElementById("DCLK_FLDiv")){var flDiv=document.getElementById("DCLK_FLDiv");}
	else{var flDiv=document.body.appendChild(document.createElement("div"));flDiv.id="DCLK_FLDiv";flDiv.style.display="none";}
	var DCLK_FLIframe=document.createElement("iframe");
	DCLK_FLIframe.id="DCLK_FLIframe_"+Math.floor(Math.random()*999999);
	DCLK_FLIframe.src=tag_url;
	flDiv.appendChild(DCLK_FLIframe);
}

function callFloodlight_same(source, type, cat) {
	var tag_url="http://fls.doubleclick.net/activityi;src=" + source + ";type=" + type + ";cat=" + cat + ";ord=1;num="+Math.floor(Math.random()*999999)+"?";
	if(document.getElementById("DCLK_FLDiv")){var flDiv=document.getElementById("DCLK_FLDiv");}
	else{var flDiv=document.body.appendChild(document.createElement("div"));flDiv.id="DCLK_FLDiv";flDiv.style.display="none";}
	var DCLK_FLIframe=document.createElement("iframe");
	DCLK_FLIframe.id="DCLK_FLIframe_"+Math.floor(Math.random()*999999);
	DCLK_FLIframe.src=tag_url;
	flDiv.appendChild(DCLK_FLIframe);
}
