/***************************************************/
/* Calculer tarif e lettres et colis			   */
/* inhibe les options selon la selection           */
/***************************************************/
var recomander;
var destinationCheck;
var natureEnvoi;
$(function(){
	destinationCheck = ($('.calcTarifDest:checked').attr('value')=="international")?"international":(($('.calcTarifDest:checked').attr('value')=="DOM/TOM")?"domtom":"france");
	recomander = $(".calcTarifReco:radio:not(:first)");
	calculer_tarif_inhibiteur_nature();

	$('#cal').each(function(){
		calculer_tarif_inhibiteur_dest();
	}).submit(function(){return calculer_tarif_verifForm()});

	// .calcTarifDest --> calculer_tarif_inhibiteur_dest()
	// .calcTarifnature --> calculer_tarif_inhibiteur_nature()
	// .calcTarifReco --> calculer_tarif_inhibiteur_reco()
	// .calcTarifReco_init --> calculer_tarif_inhibiteur_reco_int()

	$('.calcTarifDest').click(calculer_tarif_inhibiteur_dest);
	$('.calcTarifNature').click(calculer_tarif_inhibiteur_dest);
	$('.calcTarifReco').click(calculer_tarif_inhibiteur_reco);
	$('.calcTarifReco_init').click(calculer_tarif_inhibiteur_reco_int);
});

$('#emballagedomtom').hide();
$('#list_internChrono, #list_internColis').hide();

/* destination */
var calculer_tarif_inhibiteur_dest = function () {

	destinationCheck = ($('.calcTarifDest:checked').attr('value')=="international")?"international":(($('.calcTarifDest:checked').attr('value')=="DOM/TOM")?"domtom":"france");
	calculer_tarif_inhibiteur_nature();
	if (destinationCheck=="international") {
		desactiver($('.fr'));
		desactiver($('#list_over'));
		activer($('.intl'));
		activerListePays();

	} else {
		activer	($('.fr'));
		desactiver($('.intl'));
		desactiverListePays();
		if (destinationCheck=="domtom") {
			activer($('#list_over'));
		} else {
			desactiver($('#list_over'));
		}
	}

	gererEmballage();
	calculer_tarif_inhibiteur_reco();
	calculer_tarif_inhibiteur_nature()

	//verifie qu'on ait toujours une 'nature' de l l'envoi de selectionné
	if (natureEnvoi =="") {
		if (destinationCheck=="international")
			$('#priorite').attr('checked',true);
		else
			$('#letter').attr('checked',true);
		calculer_tarif_inhibiteur_dest();
	}

	return true;
}

/* nature de l'envoi */
var calculer_tarif_inhibiteur_nature = function () {
	natureEnvoi = ($(".lettre:enabled").is(":checked"))?"lettre":(($(".colissimo:enabled").is(":checked"))?"colissimo":(($(".chronopost:enabled").is(":checked"))?"chronopost":""));

	return true;
}

//gestion de select emballage
var gererEmballage = function () {
	if ($('#colissimoEmbFr:checked').is(":enabled")) {
		if (destinationCheck=="france") {
			$('#emballagefr').show();
			activer($('#emballagefr'))
			$('#emballagedomtom').hide();
			desactiver($('#emballagedomtom'))

		} else {
			$('#emballagedomtom').show();
			activer($('#emballagedomtom'))
			$('#emballagefr').hide();
			desactiver($('#emballagefr'))

		}
		activer($('#emballagefr'));
		desactiver($('#gr'));
	} else {
		desactiver($('#emballagefr, #emballagedomtom'));
		$('#emballagedomtom').hide();
		$('#emballagefr').show();
		activer($('#gr'));

		if ($('#colissimoEmb:checked').is(":enabled")) {
			activer($('#emballageintl'));
			desactiver($('#gr'));
		} else {
			desactiver($('#emballageintl'));
			activer($('#gr'));
		}
	}

	$('.emballage').change(function() {
		var reg = new RegExp('- ([0-9]{1,})kg$');
		var chaine = $(this).find(':selected').html();
		var newpoid = 0;
		if (reg.test(chaine)) {
			var res = chaine.match(reg);
			newpoid = res[1];
		}
		$('#gr').val(1000*newpoid);
	});

	return true;
}
// gestion des liste de pays
var activerListePays= function () {
	desactiver($('.list_intern'));
	$('.list_intern').hide();

	switch (natureEnvoi) {
		case "colissimo" :
			activer($('#list_internColis'));
			$('#list_internColis').show();
			break;
		case "chronopost":
			activer($('#list_internChrono'));
			$('#list_internChrono').show();
			break;
		case "lettre":
		default :
			activer($('#list_internLettre'));
			$('#list_internLettre').show();
			break;
	}
	return true;
}
var desactiverListePays = function () {
	desactiver($('.list_intern'));
	$('.list_intern').hide();
	$('.list_intern:first').show();
}

/* recommandes */
var calculer_tarif_inhibiteur_reco = function () {

	//gestion de la provenance
	if (destinationCheck=="international") {

		if ($('#colissimo').is(':checked')) {
			desactiver($('#r1'));
			desactiver($('#r2'));
			$('#recommande, #ar').click(function() {
				desactiver($('#r1'));
				desactiver($('#r2'));
			});
		}


		activer(recomander);
		//desactiver($('.calcTarifReco:checkbox'));
		$('#r3').attr('checked',false);
		$('#r4').attr('checked',false);
		$('#r5').attr('checked',false);
		//$('#colissimo').attr('checked',false);
		desactiver($('#r3'));
		desactiver($('#r4'));
		desactiver($('#r5'));
		$('#colissimo').click(function() {
			$('#colissimo').attr('checked', true);
			desactiver($('.calcTarifReco'));
			$('.calcTarifReco').attr('checked',false);
			activer($('#no'));
			activer($('#recommande'));
			activer($('#ar'));
			$('#no').attr('checked', true);
		});

		$('#chronopostEco').click(function() {
			desactiver($('.calcTarifReco'));
			activer($('#recommande'));
			activer($('#ar'));
			$('.calcTarifReco').attr('checked',false);
			activer($('#no'));
			$('#no').attr('checked', true);
		});

		$('#chronopost').click(function() {
			desactiver($('.calcTarifReco'));
			activer($('#no'));
			$('#no').attr('checked', true);
		});

		if (recomander.is(':checked')) {
			activer($('.calcTarifReco:checkbox'));
		}


		if ($(this).is(':checked')) {
			if ($(this).attr("id")=="recommande") {
				$('#no').attr('checked',false);
			}
			else if ($(this).attr("id")=="ar") {
				$('.calcTarifReco:checkbox').attr('checked',true);
				$('#no').attr('checked',false);
			}
		} else if ($(this).attr("id")=="recommande") {
				$('#no').attr('checked',true);
				calculer_tarif_inhibiteur_reco_int();
		}


		//pas de recommande ni AR pour les lettre eco intl
		if ($('#economie,#colissimoEmb,#colissimoEco').is(':checked')) {
			desactiver(recomander);
			desactiver($('.calcTarifReco:checkbox'));
			$('.calcTarifReco:checkbox').attr('checked',false);
			$('#no').attr('checked',true);
		}

	} else {
		activer(recomander);
		desactiver($('.calcTarifReco:checkbox'));

		//cas france + r1-r2-r3-r4-r5 coché
		if (recomander.is(':checked')) {
			activer($('.calcTarifReco:checkbox'));
		}

		if ($(this).is(':checked')) {
			 if ($(this).attr("id")=="ar") {
				$('.calcTarifReco:checkbox').attr('checked',true);
			}
		} else if ($(this).attr("id")=="recommande") {
			calculer_tarif_inhibiteur_reco_int();
		}
	}


	//colis eco outre mer dispo que quand la destination est domtom
	if (destinationCheck=="domtom") {
		activer($('#colissimoEcoDomTom'));
	} else {
		desactiver($('#colissimoEcoDomTom'));
		$('#colissimoEcoDomTom').attr('checked',false);
	}


	//pas de r4 & r5 pour tous types de lettres
	if (natureEnvoi=="lettre") {
		desactiver($("#r4,#r5"));
	}

	//pas de recommandées pour les colissimo enballage
	// pas de recommander pour chronopost
	// pas de recommander pour colis outremer Eco
	if ($(".emballage").is(':enabled') || $('#chronopostFr').is(':checked') ||  $('#colissimoEcoDomTom').is(':checked')) {
		desactiver(recomander);
		recomander.attr('checked',false);
	}
	if ($('#colissimo').is(':checked')) {
		//desactiver(recomander);
		//desactiver($('.calcTarifReco:checkbox'));
		//$('.calcTarifReco:checkbox').attr('checked',false);
		//$('#recommande').attr('checked',false);

		if ( (!($('#ar').is(':checked'))) && ($('#recommande').is(':checked')) ) {
			$('#no').attr('checked',true);
			$('#recommande').attr('checked',false);
		}
	}
	// pas de recommander & ar  pour chronopost
	if ($('#chronopost').is(':checked')) {
		desactiver($('.calcTarifReco:checkbox'));
		$('.calcTarifReco:checkbox').attr('checked',false);
		$('#no').attr('checked',true);
	}

	//pas de recommander pour lettre eco
	if ($('#ecopaquet').is(':checked')) {
		desactiver(recomander);
		desactiver($('.calcTarifReco:checkbox'));
		$('.calcTarifReco:checkbox').attr('checked',false);
		$('#no').attr('checked',true);
	}
	//si r1-r2-r3-r4-r5 on active le recommandé
	if (recomander.is(':checked')) {
		$('#recommande').attr('checked',true);
	}

	// No coché
	if ($('#no').is(':checked') && destinationCheck!="international") {
		desactiver($('.calcTarifReco:checkbox'));
	}
	return true;
}

/* recommandes cas particulier */
var calculer_tarif_inhibiteur_reco_int = function () {
	$('.calcTarifReco:checkbox').attr('checked',false);
	$('#no').attr('checked',true);
	return true;
}

function calculer_tarif_verifForm(elmt) {
	// international coché sans pays choisi
	if (destinationCheck=="international" && $('.list_intern:enabled option:selected').val()==0) {
		alert((traduction.country));
		return false;
	}

	//pays d'outre mer coché sans domtom selectionner
	if (destinationCheck=="domtom" && $('#list_over:enabled option:selected').val()==0) {
		alert((traduction.overseas));
		return false;
	}

	// Si pas emballage et un poid de format'incorrect'
	if (!$('#colissimoEmbFr').is(":checked") && !$('#colissimoEmb').is(':checked') && (
			$('#gr').val()=="" || $('#gr').val()=="0" || (!IsNumeric($('#gr').val())) )) {
		alert((traduction.weight));
		return false;
	}

	//si emabalage mais pas d emballage selectionné
	if (($('#colissimoEmbFr').is(":checked") || $('#colissimoEmb').is(':checked')) && $('.emballage:enabled:visible option:selected').val() =="") {
		alert(traduction.emballage);
		return false;
	}

	//poid max colis
	if ($('#gr').val() > 30000 && natureEnvoi!="lettre") {
		alert((traduction.colis));
		return false;
	}

	// poid max lettre
	if ($('#gr').val() > 3000 && natureEnvoi=="lettre") {
			alert((traduction.postage));
			return false;
	}

	//poid max ecopli 250gr
	if ($('#gr').val() > 250 && $('#ecopaquet').is(':checked')) {
			alert((traduction.ecopacket));
			return false;
	}
	// On averti l'utilisateur si l'envoi est a destination de l'etranger, en economique et pese moins de 51 grammes.
	if ($('#gr').val() < 51 && $('#economie').is(':checked')) {
		alert ((traduction.economie));
		$('#priorite').attr('checked',true);
		return false;
	}


	return true;
}


function IsNumeric(sText) {
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++) {
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) IsNumber = false;
	}
	return IsNumber;
}

function activer (elt) {
	if (elt ==undefined) return false;
	elt.attr('disabled',false);
	return elt;
}

function desactiver (elt) {
	if (elt ==undefined) return false;
	elt.attr('disabled',true);
//	elt.attr('checked',false);
	return elt;
}