var _URL = "/";

$(document).ready(function() {
	$("#linkplus").click(function () { $("#linksplus").fadeIn(1000); });
	$(".spanlettre a").click(function() {
		if ($('#lettre').css("display") == "none")
		{
			$("#lettre").slideDown(3000);
			$(".spanlettre a").html("Fermer");
			$(".spanlettre").css("background-color", "#009960");
			$(".spanlettre").css("color", "#FFF");
			$(".spanlettre").css("font-weight", "bold");
		}
		else
		{
			$("#lettre").slideUp(1000);
			$(".spanlettre a").html("Les courriers");
			$(".spanlettre").css("background-color", "#FFF");
			$(".spanlettre").css("color", "#666");
			$(".spanlettre").css("font-weight", "normal");
		}
	});
	$(".spanlettre2 a").click(function() {
		$("#lettre").slideDown(3000);
		$(".spanlettre a").html("Fermer");
		$(".spanlettre").css("background-color", "#009960");
		$(".spanlettre").css("color", "#FFF");
		$(".spanlettre").css("font-weight", "bold");
	});
	$(".showachat").click(function() {
		$("#devis").fadeOut(1000);
		$("#achat").fadeIn(3000);
	});
	$(".showdevis").click(function() {
		$("#achat").fadeOut(1000);
		$("#rappel").fadeOut(1000);
		$("#devis").fadeIn(3000);
	});
	$(".showrappel").click(function() {
		$("#devis").fadeOut(1000);
		$("#rappel").fadeIn(3000);
	});

	$('#link_achat').effect("pulsate", { times:100 }, 2000);
	$('#link_paypal').effect("pulsate", { times:5 }, 2000);
	$('#valeur_instru').keyup(function () { calcul_cotis('valeur_instru'); });
	$('#valeur_instru').blur(function () { calcul_cotis('valeur_instru'); });
	$('#nb_instru').keyup(function () { calcul_cotis('nb_instru'); });
	$('#nb_instru').blur(function () { calcul_cotis('nb_instru'); });
	$('#cgu').change(function () {
		val_instru = $('#valeur_instru').val();
		nb_instru = $('#nb_instru').val();
		validate2('cgu');
		if (val_instru != '' && nb_instru != '' && val_instru != 0 && nb_instru != 0 && val_instru <= 3000)
		{
			$('#link_paypal').html('Je règle ma cotisation');
		}
	});

	$('#link_paypal').click(function() {
		val_instru = $('#valeur_instru').val();
		nb_instru = $('#nb_instru').val();
		if (nb_instru == 1)
			if (val_instru > 1000)
				cotisation = "56";
			else
				cotisation = "36";
		else if (nb_instru > 1)
			if (val_instru > 1000)
				cotisation = "76";
			else
				cotisation = "50";
		else
			cotisation = null;
		$("input[name$='amount']").val(cotisation + '.00');

		if (val_instru != '' && nb_instru != '' && val_instru != '0' && nb_instru != '0' && $('#cgu').is(':checked'))
			if (val_instru > 3000)
				alert ('La valeur des instruments doit être inférieur à 3000 €.\n\nToute l\'Equipe de Call Assurances.');
			else
			{
				$('body').append('<div style="position:fixed; top:40%; left:40%; padding:10px; border:2px solid #000; background-color:#FFF; font-weight:bold; font-size:16px; text-align:center;">Vous allez être redirigé vers Paypal...<br />Veuillez patienter s\'il vous plaît.<br /><br />Merci.</div>');
				document.xclick.submit();
			}
		else
			alert ('Veuillez remplir tous les champs et accepter les présentes Conditions Générales.\n\nToute l\'Equipe de Call Assurances.');
	});
});

function calibrate () {
	test = $('#tocalibrate').val().split ('|');
	for (i=0; i < test.length; i++)
	{
		size = $('#size' + test[i]).css('height').substr(0, ($('#size' + test[i]).css('height').length - 2));
		sizeform = $('#sizeform' + test[i]).css('height').substr(0, ($('#sizeform' + test[i]).css('height').length - 2));
		padding = (size - sizeform) / 2;
		$('#sizeform' + test[i]).css('padding-top', padding + 'px');
		$('#sizeform' + test[i]).css('padding-bottom', padding + 'px');
	}
}

function calcul_cotis(input) {
	var val_instru = $('#valeur_instru').val();
	var nb_instru = $('#nb_instru').val();
	var cotisation = null;
	validate2(input);
	if (val_instru != '' && nb_instru != '' && val_instru != '0' && nb_instru != '0')
	{
		if (val_instru > 3000)
		{
			cotisation = null;
			bad2 ('valeur_instru');
			$('#cotis_contrat').val('');
			$('#cotisation_value').slideUp('slow');
		}
		else
		{
			if (nb_instru == 1)
				if (val_instru > 1000)
					cotisation = "56";
				else
					cotisation = "36";
			else if (nb_instru > 1)
				if (val_instru > 1000)
					cotisation = "76";
				else
					cotisation = "50";
			else
				cotisation = null;
			$("input[name$='amount']").val(cotisation + '.00');
			$('#cotis_contrat').val(cotisation + ' €');
			$('#cotisation_value').slideDown('slow');
		}
	}
	else
	{
		$('#cotis_contrat').val('');
		$('#cotisation_value').slideUp('slow');
	}
	
}

function good2 (elem)
{
	$('#'+elem+'_info').css('background', "url('/images/accept2.png') no-repeat");
}

function bad2 (elem)
{
	$('#'+elem+'_info').css('background', "url('/images/exclamation2.png') no-repeat");
}

function validate2(input)
{
	if (input == 'cgu')
	{
		if ($('#'+input).is(':checked'))
		{
			good2 (input);
		}
		else
		{
			bad2 (input);
		}
	}
	else
	{
		if ($('#'+input).val().length == 0 || $('#'+input).val() == 0)
		{
			bad2 (input);
		}
		else
		{
			good2 (input);
		}
	}
}

// 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&eacute;cent pour acc&eacute;der &agrave; cette fonction, ou les ActiveX ne sont pas autoris&eacute;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=iso-8859-1');
		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=iso-8859-1');
		req.send(null);
	} else { 
		alert("M&eacute;thode d'envoie des donn&eacute;es invalide"); 
	}

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