// JavaScript Document

jQuery(function() {

	jQuery('#ex2').jqm({
		ajax: 'calc-appli/calculatrice-form.php', 
		trigger: 'a.ex2trigger',
		onLoad:onLoad
	});

});

function loadit() {
	
	jQuery(".inp_c").inputFloat();
	//hash.w.css('opacity',0.88).show();
	
	jQuery("#long, #larg, #epai").blur(function() {
		if (jQuery(this).val() == "") {
			jQuery(this).css('border', '1px solid #ff0000');	
		} else {
			jQuery(this).css('border', '1px solid #ffffff');	
		}
	});
	
	jQuery("#btn_calculer").click(function() {
									  
		var error = false;
		
		if (jQuery(".type_b:checked").length == 0) {
			alert("Veuillez choisir un type de béton.");
			error = true;
		}
		
		if (jQuery("#long").val() == "") {
			jQuery("#long").css('border', '1px solid #ff0000');
			error = true;	
		}
		
		if (jQuery("#larg").val() == "") {
			jQuery("#larg").css('border', '1px solid #ff0000');
			error = true;	
		}
		
		if (jQuery("#epai").val() == "") {
			jQuery("#epai").css('border', '1px solid #ff0000');
			error = true;	
		}
		
		if (error == false) {
			jQuery('#container_appli').block({ 
				message: '<h1 class="loadfc"><center>Calcul du dosage en cours ...</center></h1>', 
				css: { 
					border: 'none', 
					padding: '10px', 
					width: '265px',
					backgroundColor: '#000', 
					'-webkit-border-radius': '5px', 
					'-moz-border-radius': '5px', 
					opacity: .5, 
					color: '#fff'
				}
			});
			
			jQuery.ajax({
				type: "POST",
				data: jQuery("#form_calculatrice").serialize(),
				url: "calc-appli/resultat.php",
				success: function(responseText) {
					
					jQuery("#ex2").css("top", "8%");
					jQuery("#container_appli").html(responseText);		
					jQuery("#conv_sel").change(function() {
						if (jQuery(this).val() == "kg") {
							jQuery(".sce").hide("normal");
							jQuery(".l").hide("normal");
							jQuery(".kg").show("normal");
						} else if (jQuery(this).val() == "l") {
							jQuery(".kg").hide("normal");
							jQuery(".sce").hide("normal");
							jQuery(".l").show("normal");
						} else if (jQuery(this).val() == "s") {
							jQuery(".kg").hide("normal");
							jQuery(".l").hide("normal");
							jQuery(".sce").show("normal");
						}
					});	
					jQuery("#goback").click(function() {
						jQuery.ajax({
							type: "POST",
							data: jQuery("#form_calculatrice").serialize(),
							url: "calc-appli/calculatrice-form.php",
							success: function(responseText) {
								jQuery("#container_appli").html(responseText);
								loadit();
							}
						});
					});
						
				}
			 });
			
		}
		
	});
}

function onLoad(hash) {
	
	loadit();
	
	hash.w.show();
	
}
