﻿	var i_line = new Array();
	var frais_emb = 1.50;

	function calc_prix_boite(qt, ty, cond, couleur){
		/* Asticots */
		if(ty == 1){
			if(cond == 'Vrac au litre'){
				if(couleur == 'Blanc'){
					if(qt >= 50)		pu = 3.11;
					else if(qt >= 40)       pu = 3.35;
					else if(qt >= 30)       pu = 3.59;
					else if(qt >= 20)	pu = 3.79;
					else if(qt >= 10)	pu = 4.01;
					else if(qt >= 5)	pu = 4.49;
					else			pu = 4.79;
				}else{
					if(qt >= 50)		pu = 3.31;
					else if(qt >= 40)       pu = 3.56;
					else if(qt >= 30)       pu = 3.82;
					else if(qt >= 20)	pu = 4.03;
					else if(qt >= 10)	pu = 4.26;
					else if(qt >= 5)	pu = 4.77;
					else			pu = 5.09;
				}
			}else{ /*'Petite boîte', 'Moyenne boîte', '1/4 de litre (boîte)', '1/2 litre (boîte)', 'Un litre (boîte)'*/
				if(couleur == 'Blanc'){
					if(cond == 'Petite boîte')		pu = 0.82;
					else if(cond == 'Moyenne boîte')	pu = 1.06;
					else if(cond == '1/4 de litre (boîte)')	pu = 1.29;
					else if(cond == '1/2 litre (boîte)')	pu = 2.50;
					else if(cond == 'Un litre (boîte)')	pu = 4.90;
				}else{
					if(cond == 'Petite boîte')		pu = 0.92;
					else if(cond == 'Moyenne boîte')	pu = 1.17;
					else if(cond == '1/4 de litre (boîte)')	pu = 1.44;
					else if(cond == '1/2 litre (boîte)')	pu = 2.74;
					else if(cond == 'Un litre (boîte)')	pu = 5.30;
				}
			}
		}
		/* Teignes copeaux, rouleaux ou TOC */
		if(ty == 2){
			if(qt >= 200)		pu = 1.25;
			else if(qt >= 100)	pu = 1.32;
			else if(qt >= 50)	pu = 1.48;
			else if(qt >= 20)	pu = 1.78;
			else			pu = 2.09;
		}

		/* Teignes copeaux par 50 ou 100 larves */
		if(ty == 3){
			if(cond == 'Boîte de 50 larves' || cond == 'Rouleaux 50 larves'){
				if(qt >= 200)		pu = 1.25;
				else if(qt >= 100)	pu = 1.32;
				else if(qt >= 50)	pu = 1.48;
				else if(qt >= 20)	pu = 1.78;
				else			pu = 2.09;
			}
			if(cond == 'Boîte de 100 larves'){
				if(qt >= 100)		pu = 2.50;
				else if(qt >= 50)	pu = 2.81;
				else if(qt >= 20)	pu = 3.38;
				else			pu = 3.96;
			}
		}
		/* Teignes couleur */
		if(ty == 4){
			if(qt >= 200)		pu = 1.56;
			else if(qt >= 100)	pu = 1.64;
			else if(qt >= 50)	pu = 1.85;
			else if(qt >= 20)	pu = 2.22;
			else			pu = 2.60;
		}
		/* Teignes selection*/
		 if(ty == 13){
                        if(qt >= 200)           pu = 1.25;
                        else if(qt >= 100)      pu = 1.32;
                        else if(qt >= 50)       pu = 1.48;
                        else if(qt >= 20)       pu = 1.78;
                        else                    pu = 2.09;
                }


		/* Tebos */
		if(ty == 5)			pu = 1.71;

		/* vers farine gros */
		if(ty == 6){
			if(cond == 'Boîte')		pu = 0.72;
			else if(cond == 'Vrac au kilo')	pu = 15.50;
		}

		/* Vers farine */
		if(ty == 7){
			if(cond == 'Vers de farine moyen au Kg')	pu = 11.30;
			else if(cond == 'Vers Buffalo au Kg')		pu = 19.00;
		}

		/* Fises */
		if(ty == 8){
			if(cond == 'Boîte (80cc)')		pu = 0.72;
			else if(cond == 'Vrac au litre')	pu = 18.00;
		}

		/* Pupes ou Caster */
		if(ty == 9)			pu = 3.05;

		/* Vers de terre */
		if(ty == 10){
			if(cond == 'Gros Vers Dendro\'s (bte de 15 vers)' || cond == 'Moyens Vers Dendro\'s (bte de 20 à 25 vers)' || cond == 'Petits Vers Dendro\'s (bte de 30 à 35 vers)'){
				if(qt >= 100)			pu = 0.93;
				else if(qt >= 60)		pu = 1.01;
				else if(qt >= 20)		pu = 1.10;
				else				pu = 1.26;
			}

			if(cond == 'Vers Dendro\'s par 90')	pu = 5.99;
			
			if(cond == 'Rubelius (bte de 15 vers)'){
				if(qt >= 100)			pu = 1.08;
				else if(qt >= 60)		pu = 1.16;
				else if(qt >= 20)		pu = 1.26;
				else				pu = 1.45;
			}

			if(cond == 'Terreau (bte de 40 vers)'){
				if(qt >= 100)			pu = 0.93;
				else if(qt >= 60)		pu = 1.01;
				else if(qt >= 20)		pu = 1.10;
				else				pu = 1.26;
			}
		}

		/* Vers canadien */
		if(ty == 11){
			if(qt >= 60)				pu = 1.54;
			else if(qt >= 40)			pu = 1.64;
			else if(qt >= 16)			pu = 1.75;
			else					pu = 1.95;
		}

		/* Sciure */
		if(ty == 12)			pu = 0.34;

		return pu * qt;
	}

	function calc_sub_prix(qt, ty, id, cond, couleur){
		if(ty == 'asti_fr' || ty == 'asti_beluga' || ty == 'asti_gozzers' || ty == 'asti_pinkies')
			id_type = 1;

		if(ty == 'teignes')		id_type = 2;
		if(ty == 'teignes_copeaux')	id_type = 3;
		if(ty == 'teignes_couleur')	id_type = 4;
                if(ty == 'teignes_copeaux_sel') id_type = 13;

		if(ty == 'tebos')		id_type = 5;

		if(ty == 'vers_farine_gros')	id_type = 6;
		if(ty == 'vers_farine')		id_type = 7;

		if(ty == 'fises')		id_type = 8;

		if(ty == 'pupes_caster')	id_type = 9;

		if(ty == 'vers_terre')		id_type = 10;
		if(ty == 'vers_canadien')	id_type = 11;

		if(ty == 'sciure')		id_type = 12;

		prix = calc_prix_boite(qt, id_type, cond, couleur);
		prix = Math.round(prix*1000)/1000;
		$('#prix' + ty + id).attr('value', prix);

		/* Prix total */
		calc_total_prix();
		
	}

	function calc_total_prix(){
		var total = 0;
	
		$(":input").each(function(){
			var chp_val = $(this).val();
			var chp_nom = $(this).attr("name");

			if(chp_nom.substr(0, 4) == 'prix'){
				total += parseFloat(chp_val);
			}
		});

		frais_port	= calc_total_port(total);
		
		total_ht	= frais_emb + frais_port + total;
		total_ht	= Math.round(total_ht*1000)/1000;

		tva_196		= (frais_emb + frais_port) * 0.196;
		tva_196		= Math.round(tva_196*1000)/1000;

		tva_55		= total * 0.196;
		tva_55		= Math.round(tva_55*1000)/1000;

		ttc		= total_ht + tva_196 + tva_55;
		ttc		= Math.round(ttc*1000)/1000;

		$('#ht').attr('value', total_ht);
		$('#port').attr('value', frais_port);
		$('#tva55').attr('value', tva_55);
		$('#tva196').attr('value', tva_196);
		$('#ttc').attr('value', ttc);
		$('#embal').attr('value', frais_emb);
	}

	function calc_total_port(total){
		if(total > 169)		port = 0;
		else if(total >= 100)	port = 10;
		else			port = 17;

		return port;
	}

	function get_line(id, ty, couleurs, conditionnement){
		action = '$(\'#prix' + ty + id + '\').attr(\'value\', calc_sub_prix($(\'#qt' + ty + id + '\').attr(\'value\'), \'' + ty + '\', \'' + id + '\', $(\'#condi' + ty + id + '\').attr(\'value\'), $(\'#couleur' + ty + id + '\').attr(\'value\')));';
		
		htm = '<tr id="' + ty + '_' + id + '">';

		if(couleurs.length > 0){
			htm += '<td><select onchange="' + action + '" name="couleur[' + ty + '][' + id + ']" id="couleur' + ty + id + '"  class="form_select">';

			for(var i=0; i<couleurs.length; i++){
				htm += '<option value="' + couleurs[i] + '">' + couleurs[i] + '</option>';
			}

			htm += '</select></td>';
		}


		if(conditionnement.length > 0){
			htm += '<td><select onchange="' + action + '" name="condi[' + ty + '][' + id + ']" id="condi' + ty + id + '" class="form_select">';

			for(var i=0; i<conditionnement.length; i++){
				htm += '<option value="' + conditionnement[i] + '">' + conditionnement[i] + '</option>';
			}
			
			htm += '</select></td>';
		}

		htm += '<td>' +
		'<input type="text" name="qt[' + ty + '][' + id + ']" id="qt' + ty + id + '" onKeyDown="' + action + '" onKeyUp="' + action + '" value="0" class="form_montant" />' +
		'</td><td>' +
		'<input type="text" name="prix[' + ty + '][' + id + ']" id="prix' + ty + id + '" readonly="readonly" value="0.00" class="form_montant" />' +
		'</td></tr>';

		return htm;
	}

	function add_line(ty, coul, condi){
		if(i_line[ty] > 0)
			id_div = '#' + ty + '_' + (i_line[ty] - 1);
		else{
			i_line[ty] = 0;
			id_div = '#' + ty + '_lib';
		}

		line = get_line(i_line[ty], ty, coul, condi);
		$(line).insertAfter(id_div);

		i_line[ty]++;
	}

	$(document).ready(function(){
		add_line('asti_fr', new Array('Blanc', 'Rouge', 'Jaune', 'Multi'), new Array('Vrac au litre', 'Petite boîte', 'Moyenne boîte', '1/4 de litre (boîte)', '1/2 litre (boîte)', 'Un litre (boîte)'));
		add_line('asti_beluga', new Array('Rouge', 'Jaune', 'Orange', 'Bleu', 'Vert', 'Blanc', 'Multi'), new Array('Vrac au litre', 'Petite boîte', 'Moyenne boîte', '1/4 de litre (boîte)', '1/2 litre (boîte)', 'Un litre (boîte)'));
		add_line('asti_gozzers', new Array('Blanc'), new Array('Vrac au litre', 'Petite boîte', 'Moyenne boîte', '1/4 de litre (boîte)', '1/2 litre (boîte)', 'Un litre (boîte)'));
		add_line('asti_pinkies', new Array('Blanc', 'Rouge', 'Jaune', 'Orange', 'Multi'), new Array('Vrac au litre', 'Petite boîte', 'Moyenne boîte', '1/4 de litre (boîte)', '1/2 litre (boîte)', 'Un litre (boîte)'));

		add_line('teignes', new Array(), new Array('Copeaux (bte de 20g)', 'Rouleaux (bte de 40g)', 'TOC (bte de 40g)'));

		add_line('teignes_copeaux', new Array(), new Array('Boîte de 40g'));
                add_line('teignes_copeaux_sel', new Array(), new Array('Boîte de 20g'));

		add_line('teignes_couleur', new Array('Rouge', 'Jaune', 'Multi-couleurs'), new Array('Boîte de 40g'));

		add_line('tebos', new Array(), new Array('Boîte de 125 cc (15 larves environs)'));

		add_line('vers_farine_gros', new Array(), new Array('Boîte', 'Vrac au kilo'));
		add_line('vers_farine', new Array(), new Array('Vers de farine moyen au Kg', 'Vers Buffalo au Kg'));

		add_line('fises', new Array('Rouge'), new Array('Boîte (80cc)', 'Vrac au litre'));

		add_line('pupes_caster', new Array(), new Array('250 g soit environ 0,5 Litres'));

		add_line('vers_terre', new Array(), new Array('Gros Vers Dendro\'s (bte de 15 vers)', 'Moyens Vers Dendro\'s (bte de 20 à 25 vers)', 'Petits Vers Dendro\'s (bte de 30 à 35 vers)', 'Vers Dendro\'s par 90', 'Rubelius (bte de 15 vers)', 'Terreau (bte de 40 vers)'));
		add_line('vers_canadien', new Array(), new Array('Boîte de 10'));

		add_line('sciure', new Array(), new Array('Vrac au kilo'));

		calc_total_prix();
	});

