function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

$(document).ready(function() {
	
	// Delivery array ******** Array(price, from)
	delivery_price = new Array();
	delivery_price[0] = Array(9.50,0.00);
	delivery_price[1] = Array(29.50,229.00);
	delivery_price[2] = Array(45.40,1000.00);
	delivery_price[3] = Array(75.20,1999.00);
	
	// Product
	$(".qta").bind("blur", function() {
		
		total = 0;
		total_delivery = 0;
		total_extra = 0;
		
		$(".qta").each(function() {
			
			id 	= this.id;
			qta	= this.value;
			prix  = $("#prix_"+id).val();
			extra = $("#extra_"+id).val();
					
			if(qta>0) total = total + (prix*qta); else this.value = "";
			if(extra!="") total_extra = total_extra + (extra*qta);
			
		});

		
		for(i=0;i<delivery_price.length;i++) {
			if(total>delivery_price[i][1]) total_delivery = delivery_price[i][0];
		}
		
		total = total + total_delivery + total_extra;
		$("#total").val(total.toFixed(2));

		total_shipping = total_delivery + total_extra;
		
		$("#shipping").val(total_shipping.toFixed(2));
		
		if(total>0) {
			$("#delivery").val(total_shipping.toFixed(2));
			$("#commander").attr("disabled",false);
		} else {
			$("#delivery").val("");
			$("#commander").attr("disabled",true);
		}
	});
	
	$("form").submit( function() {
		
		if( $("#total").val()<0.01 ) {
			return false;
		} else {
			error = 0;
			$(".personal_data").each(function() {
				temp = this.value;
				//alert(temp);
				if( trim(temp)=="" ) { error = 1; $(this).css({backgroundColor: "yellow"}); $(this).focus(); return false; } else { $(this).css({backgroundColor: "white"}); } 
			});
			if(error==1) return false;
		}
	});
	
	$(".personal_data").bind("blur", function() {
		temp = this.value;
		if( trim(temp)=="" ) $(this).css({backgroundColor: "yellow"}); else $(this).css({backgroundColor: "white"});
	});
	
});