$(document).ready(function() {
	$('#changeinfo').click(function() { $('#grrrr-consultbon').slideUp('slow', function() { $('#grrrr-changeinfo').slideDown('slow'); }); });
	$('#consultbon').click(function() { $('#grrrr-changeinfo').slideUp('slow', function() { $('#grrrr-consultbon').slideDown('slow'); }); });
	$("#login").click(function() { $(this).val(""); });
	$("#pass").click(function() { $(this).val(""); });

	$('#index_mail').focus(function () { if ($(this).val() == 'Adresse électronique') $(this).val('') } );
	$('#index_mail').blur(function () { if ($(this).val() == '') $(this).val('Adresse électronique') } );
	$('#cliquez-ici').click(function () { $('#div-cliquez-ici').slideDown(1000); });
});

// Controle les caractàres tappés par l'utilisateur dans un formulaire
function valid(evt,type) {
	if (type=="price")			var interdit = '+azertyuiopqsdfghjklmnbvcxwAZERTYUIOPMLKJHGFDSQWXCVBNàâäãçéèêëìîïòôöõùûüñ&*?@_=!-:;\t#~"^$£?²¤§%*°¨µ()[]{}<>|\\/`\'€'; 
	else if (type=="alphanum")	var interdit = '+&*?!:;\t#~"^%$£?²¤§*@°¨µ=.()[]{}<>|\\/`\'€'; 
	else if (type=="site")		var interdit = '+àâäãçéèêëìîïòôöõùûüñ*!;,\t#"=^$£²¤§%*@°¨µ()[]{}<>|\`\'€'; 
	else if (type=="num")		var interdit = 'azertyuiopqsdfghjklmnbvcxwAZERTYUIOPMLKJHGFDSQWXCVBNàâäãçéèêëìîïòôöõùûüñ&*?@_=!:;,\t#~"^$£?²¤§%*°¨µ()[]{}<>|\\/`\'€ '; 
	else if (type=="email")		var interdit = '+àâäãçéèêëìîïòôöõùûüñ= &*?!:;,\t#~"^%$£?²¤§*°¨µ()[]{}<>|\\/`\'€'; 
	else						var interdit = '\t#~^%$£?²¤§¨µ[]{}<>|\\`\'€'; 
	var keyCode = evt.which ? evt.which : evt.keyCode;
	if (keyCode==9 || keyCode==71) return true;
	if (interdit.indexOf(String.fromCharCode(keyCode)) >= 0) {
		return false;
	}
}

// Script Ajax perso (POST/GET en asynchrone)
function ajax (type, fichier, variables /*, fonction */) { 
	if (window.XMLHttpRequest) var req = new XMLHttpRequest();
	else if (window.ActiveXObject) var req = new ActiveXObject("Microsoft.XMLHTTP");
	else alert("Votre navigateur n'est pas assez récent pour accéder à cette fonction, ou les ActiveX ne sont pas autorisés");
	if (arguments.length == 4) var fonction = arguments[3];

	if (type.toLowerCase() == "post")
	{
		req.open("POST", _URL + fichier, true);
		req.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
		req.send(variables);
	}
	else if (type.toLowerCase()=="get")
	{
		req.open('get', _URL+fichier+"?"+variables, true);
		req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
		req.send(null);
	}
	else
	{ 
		alert("Méthode d'envoie des données invalide"); 
	}

	req.onreadystatechange = function()
	{ 
		if (req.readyState == 4 && req.responseText != null)
		{				
			if (fonction) eval(fonction + "('" + escape(req.responseText) + "')");
		} 
	}
}

// Connexion automatique 
function loginAjax() 
{
	$('#status_log').html("<img src='/images/indicator2.gif' style='vertical-align:middle'/> Vérification");
	$('#status_log').css('display',"inline");
	$('#status_log').css('color',"#0066FF");

	_login = escape($('#login').val());
	_pass = escape($('#pass').val());
	ajax ('get', 'ajax-login.php', 'login=' + _login + '&pass=' + _pass, 'loginAjaxAction');
}

function loginAjaxAction(r)
{
	var verif = unescape(r).split('|');
	if (verif[0] == "+")
	{
		window.location = document.location;
	}
	else
	{		
		$('#status_log').html("Identifiants incorrects");
		$('#status_log').css('display',"inline");
		$('#status_log').css('color',"#FF0066");
	}
}

function redirect (page)
{
	window.location = page;
}

function good (elem)
{
	$('#'+elem+'_info').css('background', "none");
}

function bad (elem)
{
	$('#'+elem+'_info').css('background', "url('/images/exclamation.png') no-repeat");
	$('#'+elem+'_info').css('display', 'block');
	$('#'+elem+'_info').effect("pulsate", { times:3}, 1500);
}

function validate (input)
{
	if (input == "ville" && $('#ville').val().length == 0)
	{
		bad ("cp");
	}
	if (input == "ddn_j" && ($('#ddn_j').val().length == 0 || $('#ddn_j').val() == ""))
	{
		bad ("ddn_j");
	}
	if (input == "ddn_m" && ($('#ddn_m').val().length == 0 || $('#ddn_m').val() == ""))
	{
		bad ("ddn_m");
	}
	if (input == "ddn_a" && ($('#ddn_a').val().length == 0 || $('#ddn_a').val() == ""))
	{
		bad ("ddn_a");
	}
	if ($('#'+input).val().length == 0)
	{
		bad (input);
	}
	else
	{
		good (input);
	}
}

function validemail()
{
	email = $('#mail').val();
	var arobase = email.indexOf("@")
	var point = email.lastIndexOf(".")
	if ((arobase < 3)||(point + 2 > email.length) ||(point < arobase+3))
	{
		bad ('mail');
	}
	else
	{
		good ('mail');
	}
}

function validemail2()
{
	email = $('#mail').val();
	var arobase = email.indexOf("@")
	var point = email.lastIndexOf(".")
	if ((arobase < 3)||(point + 2 > email.length) ||(point < arobase+3))
	{
		bad ('mail');
	}
	else
	{
		ajax('get','ajax-verifmail.php','email='+escape(email),'verifEmailAjax');
	}
}
	
function verifEmailAjax(r)
{
	if (unescape(r)=="email_ok") good('mail');
	else redirect ("/membre/");	
}

function validcaptcha()
{
	captcha = $('#captcha').val();
	truecaptcha = $('#truecaptcha').val();
	if (captcha != truecaptcha)
	{
		bad ('captcha');
	}
	else
	{
		good ('captcha');
	}
}

function validateJ(jour)
{
	if (($('#' + jour + '_j').val().length == 0) || ($('#' + jour + '_j').val() < 1) || ($('#' + jour + '_j').val() > 31))
	{
		$('#' + jour + '_info').css('background-image', "url('/images/exclamation.png')");
		$('#'+jour+'_info').effect("pulsate", { times:3}, 1500);
	}
	else
	{
		$('#' + jour + '_info').css('background', "none");
	}
}

function validateM(mois)
{
	if (($('#' + mois + '_m').val().length == 0) || ($('#' + mois + '_m').val() < 1) || ($('#' + mois + '_m').val() > 12))
	{
		$('#' + mois + '_info').css('background-image', "url('/images/exclamation.png')");
		$('#'+mois+'_info').effect("pulsate", { times:3}, 1500);
	}
	else
	{
		$('#' + mois + '_info').css('background', "none");
	}
}

function validateA(annee)
{
	if (($('#' + annee + '_a').val().length == 0) || ($('#' + annee + '_a').val() < 1900) || ($('#' + annee + '_a').val() > 2009))
	{
		$('#' + annee + '_info').css('background-image', "url('/images/exclamation.png')");
		$('#'+annee+'_info').effect("pulsate", { times:3}, 1500);
	}
	else
	{
		$('#' + annee + '_info').css('background', "none");
	}
}

function validcivil()
{
	if ($('#civiliteH').is(':checked') || $('#civiliteF').is(':checked'))
	{
		good ('civilite');
	}
	else
	{
		bad ('civilite');
	}
}

function validcaptcha()
{
	captcha = $('#captcha').val();
	truecaptcha = $('#truecaptcha').val();
	if (captcha != truecaptcha)
	{
		bad ('captcha');
	}
	else
	{
		good ('captcha');
	}
}

function validForm()
{
	var error = 0;
	validcivil();
	validate('nom');
	validate('prenom');
	validate('cp');
	validate('ddn_j');
	validate('ddn_m');
	validate('ddn_a');
	validemail();
	validate('pass');
	validcaptcha();
	if ($('#ismajeur').is(':checked') == false)
	{
		bad('ismajeur');
		error = 1;
	}
	else
		good('ismajeur');

	var coloneVerif = ['civilite_info', 'nom_info', 'prenom_info', 'cp_info', 'ddn_info', 'mail_info', 'pass_info', 'captcha_info']; 
	

	for (var i = 0; i <= 7; i++)
	{
		if ($('#'+coloneVerif[i]).css('background-image') != "none")
		{ 
			error = 1;
		}
	}
	if (error == 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validForm3()
{
	validate('nom');
	validate('prenom');
	validate('cp');
	validate('ddn_j');
	validate('ddn_m');
	validate('ddn_a');
	validemail();
	validate('pass');

	var coloneVerif = ['nom_info', 'prenom_info', 'cp_info', 'ddn_info', 'mail_info', 'pass_info']; 
	var error = 0;

	for (var i = 0; i <= 5; i++)
	{
		if ($('#'+coloneVerif[i]).css('background-image') != "none")
		{ 
			error = 1;
		}
	}
	if (error == 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validBanque()
{
	validate('codebanque');
	validate('codeguichet');
	validate('numerocompte');
	validate('clerib');

	var coloneVerif = ['codebanque_info', 'codeguichet_info', 'numerocompte_info', 'clerib_info']; 
	var error = 0;

	for (var i = 0; i <= 3; i++)
	{
		if ($('#'+coloneVerif[i]).css('background-image') != "none")
		{ 
			error = 1;
		}
	}
	if (error == 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validFormContact()
{
	validate('nom');
	validemail();
	validate('message');
	validcaptcha();

	var coloneVerif = ['nom_info', 'mail_info', 'message_info', 'captcha_info']; 
	var error = 0;

	for (var i = 0; i <= 2; i++)
	{
		if ($('#'+coloneVerif[i]).css('background-image') != "none")
		{ 
			error = 1;
		}
	}
	if (error == 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function zonier ()
{
	if ($('#cp').val().length == 5)
	{
		cp = $('#cp').val();
		ajax('get','ajax-zonier.php','cp='+escape(cp),'verifZonier');
	}
	else
	{
		bad ('cp');
		$("#ville").val("-- Code postal à 5 chiffres --");
	}
}

function verifZonier (r)
{
	var verif = unescape(r);
	if (verif != "error")
	{
		$("#ville").val(verif);
		validate('cp');
		validate('ville');
	}
	else
	{
		bad ("cp");
		$("#ville").val("-- Code postal inéxistant --");
	}
}

function validateDate(input)
{
	var d = new Date();
	var Nowyear = d.getFullYear(); 
	var Nowmonth = "0" + (d.getMonth() + 1); 
	var Nowday = d.getDate();
	var jour = $("#" + input + "J").val();
	var mois = $("#" + input + "M").val();
	var annee = $("#" + input + "A").val();
	if (Nowyear >= annee)
	{
		if (Nowmonth > mois)
		{
			good (input);
		}
		else if (Nowmonth == mois)
		{
			if (Nowday >= jour)
			{
				good (input);
			}
			else
			{
				bad (input);
			}
		}
		else
		{
			bad (input);
		}
	}
	else
	{
		bad (input);
	}
}

function validEncaisser()
{
	var error = 0;
	validateDate('date');
	validate('facture');
	validate('montant');

	var coloneVerif = ['date_info', 'facture_info', 'montant_info']; 

	for (var i = 0; i <= 2; i++)
	{
		if ($('#'+coloneVerif[i]).css('background-image') != "none")
		{ 
			error = 1;
		}
	}
	if (error == 1)
	{
		return false;
	}
	else
	{
		ajax ('get', 'ajax-encaisser.php', 'idmembreg4r=' + $("#idmembreg4r").val() + '&idmembre=' + $("#idmembre").val() + '&idbon=' + $("#idbon").val() + '&date=' + $("#dateJ").val() + '/' + $("#dateM").val() + '/' + $("#dateA").val() + '&facture=' + $("#facture").val() + '&montant=' + $("#montant").val());
		$('#bonstatus' + $("#idbon").val(), window.opener.document).html('En attente')
		window.close();
		return false;
	}
}

function aff_bons()
{
	ajax ('get', 'membre/ajax-type-bon.php', 'type=' + $('#bon_filter').val(), 'aff_bons2');
	return false;
}

function aff_bons2 (r)
{
	document.location.href = '/membre/bon.html';
}

function sendfriend (type, id, type2)
{
	if (type == 1)
	{
		$('#sendbon_friend_id').val(id);
		$('#expand_sendbon_friend').fadeIn(1000);
	}
	else
	{
		$('#sendmsg_friend_id').val(id);
		$('#sendmsg_friend_type').val(type2);
		$('#expand_sendmsg_friend').fadeIn(1000);
	}
}