var _URL = "/";

$(function() {
	$(".ok").click(function(){$("#tel2").slideDown('slow');$(this).hide();});
	$("#devis_date_circ_j").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_circ_m").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_circ_a").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_achat_j").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_achat_m").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_achat_a").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_debut_j").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_debut_m").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_debut_a").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_fin_j").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_fin_m").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_date_fin_a").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_ddn_j").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_ddn_m").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_ddn_a").click(function(){$(this).val(""); $(this).css("color","#000");});
	$("#devis_coef").click(function(){$(this).val("");});
});

$(document).ready(function() {
	$("#showlettre").click(function() {
		$("#lettre").fadeIn('slow');
	});
	$("#linkplus").click(function () { $("#linksplus").fadeIn(1000); });
	$("#linkplusbutton1").click(function () { $("#linksplus1").slideDown(3000); });
	$("#linkplusbutton2").click(function () { $("#linksplus2").slideDown(3000); });
	$("#gar_for").keyup(function () {
		$('.formules').html('');
		$('#gar_names').html('&nbsp;<br />');
		if ($("#gar_for").val() > 4)
			$("#gar_for").val(4);
		var i = 1;
		while (i <= $("#gar_for").val())
		{
			$('.formules').append('<input type="text" name="r1_gar1_option1_form' + i + '" id="r1_gar1_option1_form' + i + '" value="Formule' + i + '" class="formule" />');
			$('#gar_names').append('<label>Nom de la Formule ' + i + '</label> <input type="text" name="gar_forname' + i + '" id="gar_forname' + i + '" value="Formule ' + i + '" /><br />');
			i++;
		}
	});

	$("#gar_for").blur(function () {
		$('.formules').html('');
		$('#gar_names').html('&nbsp;<br />');
		if ($("#gar_for").val() > 4)
			$("#gar_for").val(4);
		var i = 1;
		while (i <= $("#gar_for").val())
		{
			$('.formules').append('<input type="text" name="r1_gar1_option1_form' + i + '" id="r1_gar1_option1_form' + i + '" value="Formule' + i + '" class="formule" />');
			$('#gar_names').append('<label>Nom de la Formule ' + i + '</label> <input type="text" name="gar_forname' + i + '" id="gar_forname' + i + '" value="Formule ' + i + '" /><br />');
			i++;
		}
	});
	$('#gar_desc').focus(function () { $('#gar_desc').val(''); });
	$('#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 (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:45%; 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++)
	{
		$('#sizeform' + test[i]).css('height', $('#size' + test[i]).css('height'));
	}
}

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('http://www.call-assurances.fr/images/accept2.png') no-repeat");
}

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

function validate2(input)
{
	if (input == 'ddn')
	{
		var error = 0;
		day = $('#ddn').val().substr(0, 2);
		month = $('#ddn').val().substr(3, 2);
		year = $('#ddn').val().substr(6, 4);
		if (isNaN (day))
		{
			bad2('ddn');
			error = 1;
		}
		if (isNaN (month))
		{
			bad2('ddn');
			error = 1;
		}
		if (isNaN (year))
		{
			bad2('ddn');
			error = 1;
		}
		if (day >= 1 && day < 32 && month >= 1 && month < 13 && year < 2010)
		{
			good2('ddn');
		}
		else
		{
			bad2('ddn');
		}
	}
	else 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)+"')");
			
		} 
	}
}

function delFormField (type, blockid, id) {
	if (type == "risque")
	{
		$('#' + blockid).remove();
		$('#risque_del_' + blockid).remove();
	}
	else if (type == "garantie")
	{
		$('#' + blockid + '_gar' + id).remove();
		$('#' + blockid + '_del_garantie').remove();
	}
	else if (type == "option")
	{
		$('#' + blockid + '_opt' + id).remove();
		$('#option_del_' + blockid).remove();
	}
}

function addFormField (type, blockid) {
	if (type == 'risque')
	{
		var id = $("#risque_id").val();
		$('#risque_add').remove();
		$('#envoie').remove();
		$('#show').remove();
		$("#form_gar").append('<fieldset class="div_risque" id="r' + id + '"><legend>Risque ' + id + '</legend><input type="hidden" id="r' + id + '_garid" value="2" class="hidden" /><label>Nom du risque</label><input type="text" name="gar_risque' + id + '" id="gar_risque' + id + '" value="" /><br /><fieldset class="div_garantie" id="r' + id + '_gar1"><legend>Garantie 1</legend><label>Nom de la garantie</label><input type="text" name="r' + id + '_garantie1" id="r' + id + '_garantie1" value="" /><br /><fieldset class="div_option" id="r' + id + '_gar1_opt"><legend>Options</legend><input type="hidden" id="r' + id + '_gar1_optid" value="2" class="hidden" /><label>Option 1</label><input type="text" name="r' + id + '_gar1_option1" id="r' + id + '_gar1_option1" value="" /><a href="#" onclick="addFormField(\'option\', \'r' + id + '_gar1\'); return false;" id="r' + id + '_gar1_add_option" class="add_option">+</a> <a href="#" onclick="delFormField(\'option\', \'r' + id + '_gar1\', \'1\'); return false;" id="r' + id + '_gar1_del_option" class="add_option">-</a><div class="formules" id="r' + id + '_gar1_option1_form"></div><br /></fieldset></fieldset><a href="#" onclick="addFormField(\'garantie\', \'r' + id + '\'); return false;" class="add_garantie" id="r' + id + '_add_garantie">Ajouter Garantie</a> <a href="#" onclick="delFormField(\'garantie\', \'r' + id + '\'); return false;" class="add_garantie" id="r' + id + '_del_garantie">Supprimer Garantie</a></fieldset><a href="#" onclick="addFormField(\'risque\'); return false;" id="risque_add" class="add_risque">Ajouter Risque</a> <a href="#" onclick="delFormField(\'risque\', \'r' + id + '\'); return false;" id="risque_del_r' + id + '" class="add_risque">Supprimer Risque</a><br /><input type="submit" class="submit" name="envoie" id="envoie" value="Valider" />');

		var i = 1;
		while (i <= $("#gar_for").val())
		{
			$('#r' + id + '_gar1_option1_form').append('<input type="text" name="r' + id + '_gar1_option1_form' + i + '" id="r' + id + '_gar1_option1_form' + i + '" value="Formule' + i + '" class="formule" />');
			i++;
		}

		id = ++id;
		$("#risque_id").val(id);
	}
	else if (type == 'garantie')
	{
		var id = $("#" + blockid + "_garid").val();
		$('#' + blockid + '_add_garantie').remove();
		$("#" + blockid).append('<fieldset class="div_garantie" id="' + blockid + '_gar' + id + '"><legend>Garantie ' + id + '</legend><label>Nom de la garantie</label><input type="text" name="' + blockid + '_garantie' + id + '" id="' + blockid + '_garantie' + id + '" value="" /><br /><fieldset class="div_option" id="' + blockid + '_gar' + id + '_opt"><legend>Options</legend><input type="hidden" id="' + blockid + '_gar' + id + '_optid" value="2" class="hidden" /><label>Option 1</label><input type="text" name="' + blockid + '_gar' + id + '_option1" id="' + blockid + '_gar' + id + '_option1" value="" /> <a href="#" onclick="addFormField(\'option\', \'' + blockid + '_gar' + id + '\'); return false;" id="' + blockid + '_gar' + id + '_add_option" class="add_option">+</a> <a href="#" onclick="delFormField(\'option\', \'' + blockid + '\', \'1\'); return false;" id="' + blockid + '_gar' + id + '_del_option" class="add_option">-</a> <div class="formules" id="' + blockid + '_gar' + id + '_option1_form"></div></fieldset></fieldset><a href="#" onclick="addFormField(\'garantie\', \'' + blockid + '\'); return false;" class="add_garantie" id="' + blockid + '_add_garantie">Ajouter Garantie</a> <a href="#" onclick="delFormField(\'garantie\', \'' + blockid + '\', \'' + id + '\'); return false;" class="add_garantie" id="' + blockid + '_del_garantie">Supprimer Garantie</a>');

		var i = 1;
		while (i <= $("#gar_for").val())
		{
			$('#' + blockid + '_gar' + id + '_option1_form').append('<input type="text" name="' + blockid + '_gar' + id + '_option1_form' + i + '" id="' + blockid + '_gar' + id + '_option1_form' + i + '" value="Formule' + i + '" class="formule" />');
			i++;
		}

		id = ++id;
		$("#" + blockid + "_garid").val(id);
	}
	else if (type == 'option')
	{
		var id = $("#" + blockid + "_optid").val();
		$("#" + blockid + "_opt").append('<div id="' + blockid + '_opt' + id + '"><input type="text" name="' + blockid + '_option' + id + '" id="' + blockid + '_option' + id + '" value="" /> <a href="#" onclick="addFormField(\'option\', \'' + blockid + '\'); return false;" id="' + blockid + '_add_option" class="add_option">+</a> <a href="#" onclick="delFormField(\'option\', \'' + blockid + '\', \'' + id + '\'); return false;" id="' + blockid + '_del_option" class="add_option">-</a> <div class="formules" id="' + blockid + '_option' + id + '_form"></div></div>');

		var i = 1;
		while (i <= $("#gar_for").val())
		{
			$('#' + blockid + '_option' + id + '_form').append('<input type="text" name="' + blockid + '_option' + id + '_form' + i + '" id="' + blockid + '_option' + id + '_form' + i + '" value="Formule' + i + '" class="formule" />');
			i++;
		}

		id = ++id;
		$("#" + blockid + "_optid").val(id);
	}
}