$(document).ready(function() {
	$('#type').change( function () {
		type = $('#type').val();
		if (type == 2)
		{
			$('#showIt').slideUp('slow', function () {
				$('#error1').html('Nous ne pouvons traiter ce type de sinistre pour le moment.<br />Pour plus d\'informations, appelez le 0892 020 045 <small>(appel surtaxé 0,34€/min)</small>');
				$('#error1').effect("pulsate", { times:1}, 1500);
			});
		}
		else
		{
			$('#showIt').slideDown('slow');
			$('#error1').slideUp('slow');
		}
	});
	$('input[name=constat_signed]').click(function () {
		type = $('input[name=constat_signed]:checked').val();
		if (type == 1)
		{
			$('#showIt2').slideUp('slow', function () {
				$('#error2').html('Nous ne pouvons traiter ce type de sinistre pour le moment.<br />Pour plus d\'informations, appelez le 0892 020 045 <small>(appel surtaxé 0,34€/min)</small>');
				$('#error2').effect("pulsate", { times:1}, 1500);
			});
		}
		else
		{
			$('#showIt2').slideDown('slow');
			$('#error2').slideUp('slow');
		}
	});
	$('input[name=autre_declare]').click(function () {
		autre_declare = $('input[name=autre_declare]:checked').val() 
		if (autre_declare == 1)
		{
			$('#panel_autre_declare').slideDown('slow');
		}
		else
		{
			$('#panel_autre_declare').slideUp('slow');
		}
	});
});

// 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) + "')");
		} 
	}
}

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');
}

function validate (input)
{
	if ($('#'+input).val().length == 0)
	{
		bad (input);
	}
	else
	{
		good (input);
	}
}

function validateCheck (input)
{
	if ($('input[name=' + input + ']').is(':checked') == false)
	{
		bad (input);
	}
	else
	{
		good (input);
	}
}

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

function validemail2()
{
	email = $('#email').val();
	var arobase = email.indexOf("@")
	var point = email.lastIndexOf(".")
	if ((arobase < 3)||(point + 2 > email.length) ||(point < arobase+3))
	{
		bad ('email');
	}
	else
	{
		ajax('get','ajax-verifmail.php','email='+escape(email),'verifEmailAjax');
	}
}
	
function verifEmailAjax(r)
{
	if (unescape(r)=="email_ok") good('email');
	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 + '_j').val() == 'JJ'))
	{
		$('#' + jour + '_info').css('background-image', "url('/images/exclamation.png')");
		bad (jour);
	}
	else
	{
		good (jour);
	}
}

function validateM(mois)
{
	if (($('#' + mois + '_m').val().length == 0) || ($('#' + mois + '_m').val() < 1) || ($('#' + mois + '_m').val() > 12) || ($('#' + mois + '_m').val() == 'MM'))
	{
		bad (mois);
	}
	else
	{
		good (mois);
	}
}

function validateA(annee)
{
	if (($('#' + annee + '_a').val().length == 0) || ($('#' + annee + '_a').val() < 1900) || ($('#' + annee + '_a').val() > 2009) || ($('#' + annee + '_a').val() == 'AAAA'))
	{
		bad (annee);
	}
	else
	{
		good (annee);
	}
}

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

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

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

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);
	}
}
