//FORMAT NUMBER FUNCTION
function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) { dec = typeof(dec) != 'undefined' ? dec : 2; thou = typeof(thou) != 'undefined' ? thou : " "; pnt = typeof(pnt) != 'undefined' ? pnt : "."; curr1 = typeof(curr1) != 'undefined' ? curr1 : ""; curr2 = typeof(curr2) != 'undefined' ? curr2 : ""; n1 = typeof(n1) != 'undefined' ? n1 : ""; n2 = typeof(n2) != 'undefined' ? n2 : ""; var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}

//ON LOAD FUNCTION
$(function() {

	/*
	$('#accueilContentRight').cycle({
	   fx:     'scrollDown',
			   speed:    500,
	   timeout:  0,
	   random:  1
	});
	*/
	
	//only numbers allowed
	$("form[name=etape1] input[type=text]").keydown(function(e){
														   
		if (e.which >= 49 && e.which <= 57 || e.which >= 96 && e.which <= 105 || e.which == 8) {
			return true;
		} else {
			return false;	
		}
		
	});
	
	//reset calculs
	if ($("form[name=etape1]").length>0) {
	
			$("#stotal").text("0.00");
			$("#tps").text("0.00");
			$("#tvq").text("0.00");
			$("#total").text("0.00");
			$("#montant_billet").attr("montant","0.00");
			$("#montant_corpo").attr("montant","0.00");
			$("#montant_vip").attr("montant","0.00");
			$("input[name=qty_billet]").val("");
			$("input[name=qty_corpo]").val("");
			$("input[name=qty_vip]").val("");
	
	}
	
	//changer montant en temps réel sur chaque champ
	$("form[name=etape1] input[type=text]").keyup(function(e){
								   
		if (e.which >= 49 && e.which <= 57 || e.which >= 96 && e.which <= 105 || e.which == 8) {
			
			//variables
			var prix = $(this).attr("prix");
			var qty = $(this).val();
			var id = $(this).attr("id");
			var nbpers = $(this).attr("pers");
			
			//calcul montant
			$("#montant_"+id).text(formatNumber(prix*qty)).attr("montant",prix*qty);
			$("#pers_"+id).text(nbpers*qty);
			
			//variables montant
			var m_billet = parseInt($("#montant_billet").attr("montant"));
			var m_corpo = parseInt($("#montant_corpo").attr("montant"));
			var m_vip = parseInt($("#montant_vip").attr("montant"));
			
			//calcul stotal, tps, tvq et total
			var stotal = m_billet+m_corpo+m_vip;
			var tps = stotal*0.05;
			var tvq = (stotal+tps)*0.095;
			var total = stotal+tps+tvq;
			
			//affichage stotal, tps, tvq et total
			$("#stotal").text(formatNumber(stotal));
			$("#tps").text(formatNumber(tps));
			$("#tvq").text(formatNumber(tvq));
			$("#total").text(formatNumber(total));
			
			//mettre à jour linput
			$("input[name=total]").val(formatNumber(total));
			
		} else {
			return false;	
		}
		
	});
	
	
	//validation form étape 2
	$("form[name=etape2]").submit(function(e){
		
		var msg = "Les champs suivant doivent être remplis:\n\n";
		var vides = "";
		
		$("input").each(function(){
			if ($(this).val() == "") {
				vides = vides+"- "+$(this).attr("title")+"\n";
			}
		});
		
		if (vides!="") {
			alert(msg+vides);
			return false;
		} else {
			return true;
		}
	});

});
