function initform(){
    $('#dreta td.quantitat input').bind('change',calculapreu);
    calcularTotalInici();
}

function calcularTotalInici(){
    var error = false;
    var total = 0;
    var quantitat = 0;
    var preu = 0;
    for (i=0;i<$("input#totalfiles").val();i++){
            var idtr = "tr"+i;
            var trllibre = $("#"+idtr);
            if (trllibre.size()){// si el tr existeix   
                quantitat = parseInt(trllibre.find("td.quantitat input").val());
                preu = parseInt(trllibre.find("td.preu span").text());
                importllibre = quantitat * preu;
                trllibre.find("td.import span").text(importllibre);
                if (isNaN(importllibre)) error=true;
                total = total+importllibre;
                total = parseInt(total);
            }
    }
    if (error) {
        alert("Hi ha hagut un error")
    }
    else $("span#preutotal").text(total);   
}


function calculapreu(){
	var quants = this.value;
	//if (isNaN(quants)) {
	//	alert(errornum);
		//this.value = quants;		
	//	}
	//else {
		var trllibre = $(this.parentNode.parentNode);
		var preu = trllibre.find("td.preu span").text();
		preu = parseInt(preu);
		quants = parseInt(quants);
		var importe = quants * preu;
		importe = parseInt(importe); 
		trllibre.find("td.import span").text(importe);
		calculartotal();
	//}
}


function calculartotal(){
    var total = 0;
    for (i=0;i<$("input#totalfiles").val();i++){
            var idtr = "tr"+i;
            var objtr = $("#"+idtr);
            if (objtr.size()){// si el tr existeix           
                importllibre = parseInt(objtr.find(" td.import span").text());
                total = total+importllibre;
                total = parseInt(total);
            }
    }
    $("span#preutotal").text(total);
}

function selectwho(inputCheck){
	var idWho = inputCheck.name;
	var checkWho = inputCheck.checked;
	  //si l'acabem de seleccionar
	if (checkWho){  
		$.ajax({
     		type: "POST",
     		url:"omple_cistell.php",
     		data:"isbns="+idWho,    
   	   success:function(nump){
	   	   escriure_items(nump);
	   	   $("#"+idWho).parents("div.itemgran").css({'background':'#f0f0f0'});
   	   }
		});
   }
	else {
			$.ajax({
     		type: "POST",
     		url:"buida_cistell.php",
     		data:"isbns="+idWho,    
   	   success:function(nump){
				  escriure_items(nump);  	
				  $("#"+idWho).parents("div.itemgran").css({'background':'#ffffff'});   
   	   }
		});
	}

//actualitzar el total d'items
}


function checkInputs(){
	  $.ajax({
             type: "POST",
             url:"seleccionat_cistell.php",
             success:function(idsel){
               // alert(idsel);
                var idChecks = idsel.split (",");
                  
                  var allChecks = document.getElementById('form1').getElementsByTagName('input');
							for (i=0;i<allChecks.length;i++){//desselecciono tots els checks ja que firefox et guarda la última selecció que has fet, i pot ser que l'usuari no hagi guardat la última selecció
								allChecks[i].checked="";
								}
							for (i=0;i<(idChecks.length-1);i++){//selecciono només els de l'array idChecks
								//alert(idChecks[i]);
								if (document.getElementById(idChecks[i])) {
									document.getElementById(idChecks[i]).checked="checked";
									  $("#"+idChecks[i]).parents("div.itemgran").css({'background':'#f0f0f0'});
									} 
							}
             }
     });
}   
	
function selectPais(importcarro){
	var importcarro = importcarro;
	var idZona = document.getElementById("pais");
	if (idZona.value == 6){
		$.ajax({
     		type: "POST",
     		url:"provincies.php",
   	   success:function(provincia){
   	   	document.getElementById("IT").innerHTML = "... &euro;";
				document.getElementById("GE").innerHTML = "... &euro;";
   	       document.getElementById("hcontinuar").style.visibility = "hidden";
   		//	 document.getElementById("provincia").innerHTML = provincia;
				 document.getElementById("pprovincia").style.visibility = "visible";
   	   }
		});
	}	
	
	else {
		$.ajax({
     		type: "POST",
     		url:"despeses.php",
			data:"idzona="+idZona.value, 
   	   success:function(importdesp){
   	    importdesp = parseFloat(importdesp);
   	 		 document.getElementById("pprovincia").style.visibility = "hidden";
   	 		 document.formE.provincia.options[0].selected=true;
				 document.getElementById("GE").innerHTML = importdesp;
				 total = (importdesp+importcarro);
				 total = Math.round(total*100)/100;
   	   	 document.getElementById("IT").innerHTML = total;
   	 		 document.getElementById("hcontinuar").style.visibility = "visible";
   	   }
		});

	}
}	

function selectProv(importcarro){
	var importcarro = importcarro;
	var idZona = document.getElementById("provincia");
	$.ajax({
     		type: "POST",
     		url:"despeses.php",
			data:"idzona="+idZona.value, 
   	   success:function(importdesp){
   	 		 importdesp = parseFloat(importdesp);
				 document.getElementById("GE").innerHTML = importdesp;
				 total = (importdesp+importcarro);
				 total = Math.round(total*100)/100;
   	   	 document.getElementById("IT").innerHTML = total;
   	   	 document.getElementById("hcontinuar").style.visibility = "visible";
   	   }
		});
}	
	
function ajax_send(){

$.ajax({
	type: "POST",
	url:"/v2/enviar_.php",
	data:"idoma=Esp",
	complete:function(enviat){
	 //  if (enviat=="1") {
   	   	document.getElementById("hant").style.visibility = "hidden";
   	   
   	  // 	setTimeout('document.getElementById("gracies").style.display = "block"', 1000);
	//	return false;
   	 //  }
   	}
    }); 

}	
	
function enviar_email(){
   document.getElementById("hcont").style.visibility = "hidden";
//	ajax_send();
    document.FormT1.submit();
	
}
