var valoareTVA = 0.24;

var nrProduseProforma = 0;

var indexProduseProforma = 0;

var subTotalValoare = 0.00;

var subTotalTva = 0.00;

var total = 0.00;


function eliminaProdusProforma(produsNr, um, cantitate, pretUnitar){
	nrProduseProforma --;	var proformaRow = document.getElementById("proformaRow"+ produsNr);	proformaRow.parentNode.removeChild(proformaRow);	eProdusServiciu = false;		if(cantitate=='-')		eProdusServiciu = true;	else		cantitate = parseFloat(cantitate);		if(eProdusServiciu == false){ 		subTotalValoare -= pretUnitar * cantitate ;		subTotalTva -=  pretUnitar * valoareTVA * cantitate ;	}	else{		subTotalValoare -= pretUnitar;		subTotalTva -=  pretUnitar * valoareTVA;
	}total = subTotalTva + subTotalValoare;

document.getElementById("proformaSubtotalValoare").innerHTML = Math.round(100 * subTotalValoare) / 100;

document.getElementById("proformaSubtotalTva").innerHTML = Math.round(100 * subTotalTva) / 100;

document.getElementById("proformaTotal").innerHTML = Math.round(100 * total) / 100;

document.getElementById("n").innerHTML ="";

document.getElementById("n").innerHTML = "<INPUT TYPE='hidden' NAME='numar_produse' VALUE='" + nrProduseProforma + "'>"
										+"<INPUT TYPE='hidden' NAME='subtotalValoare' VALUE='" + Math.round(100 * subTotalValoare) / 100  + "'>"
                      					+"<INPUT TYPE='hidden' NAME='subtotalTva' VALUE='" + Math.round(100 * subTotalTva) / 100  + "'>"  
                                        +"<INPUT TYPE='hidden' NAME='total' VALUE='" + Math.round(100 * total) / 100  + "'>";  

}



function adaugaProdusProforma(id, eFacturaServicii, denumire, um, cantitate, pretUnitar){

if (cantitate == ''){

    alert("Campul de cantitate trebuie completat!\n");

    return;

    }
	
	
	if (pretUnitar == ''){

    alert("Campul de pret trebuie completat!\n");

    return;

    }
if(eFacturaServicii==false){
	cantitate = parseFloat(cantitate);}
pretUnitar = parseFloat(pretUnitar);

id = parseFloat(id);

if (eFacturaServicii==true && cantitate <= 0){    alert("Cantitate negativa sau 0!\n");    return;}

//stergem continutul campurilor

resetProdusForm();

nrProduseProforma++;
indexProduseProforma++;
if (indexProduseProforma>=100){
    alert("Elemente adaugate si sterse de prea multe ori!\n");    return;}
var proformaBody = document.getElementById("proformaBody");

var proformaRow = document.createElement("TR");

proformaRow.id = "proformaRow" + indexProduseProforma; 

var proformaCell1 = document.createElement("TD");
// if (magazin!=0) var proformaCell8 = document.createElement("TD");
var proformaCell2 = document.createElement("TD");var proformaCell3 = document.createElement("TD");var proformaCell4 = document.createElement("TD");var proformaCell5 = document.createElement("TD");var proformaCell6 = document.createElement("TD");var proformaCell7 = document.createElement("TD");
proformaCell1.innerHTML = 				"<INPUT TYPE='hidden' NAME='produs"+indexProduseProforma+"id' VALUE='" + id + "'>"+		
		        "<INPUT TYPE='hidden' NAME='produs"+indexProduseProforma+"denumire' VALUE='" + denumire + "'>"+

				"<INPUT TYPE='hidden' NAME='produs"+indexProduseProforma+"um' VALUE='" + um + "'>"+
				"<INPUT TYPE='hidden' NAME='produs"+indexProduseProforma+"cantitate' VALUE='" + cantitate + "'>"+
				"<INPUT TYPE='hidden' NAME='produs"+indexProduseProforma+"pretUnitar' VALUE='" + pretUnitar + "'>"+
				denumire;

proformaCell2.innerHTML = um;
proformaCell3.innerHTML = cantitate;

proformaCell4.innerHTML = Math.round(100*pretUnitar)/100;
if(eFacturaServicii==false)
	proformaCell5.innerHTML = Math.round(100*pretUnitar * cantitate)/100 ;else	proformaCell5.innerHTML = pretUnitar;
if(eFacturaServicii==false)
	proformaCell6.innerHTML = Math.round(100 * (pretUnitar*valoareTVA) * cantitate) / 100;else	proformaCell6.innerHTML = Math.round(100 * pretUnitar*valoareTVA) / 100;

proformaCell7.innerHTML = "<a onclick = 'eliminaProdusProforma("+indexProduseProforma+",\""+um+"\",\""+cantitate+"\","+ pretUnitar+");'><img src='../img/b_drop.png'</a>";
if(eFacturaServicii==false){
	subTotalValoare += (pretUnitar) * cantitate;}else{	subTotalValoare += pretUnitar;
}
if(eFacturaServicii==false){
	subTotalTva += (pretUnitar*valoareTVA) * cantitate;}else{	subTotalTva += pretUnitar*valoareTVA;
}

total = subTotalTva + subTotalValoare;

proformaRow.appendChild(proformaCell1);

proformaRow.appendChild(proformaCell2);

proformaRow.appendChild(proformaCell3);

proformaRow.appendChild(proformaCell4);

proformaRow.appendChild(proformaCell5);

proformaRow.appendChild(proformaCell6);

proformaRow.appendChild(proformaCell7);



proformaBody.appendChild(proformaRow);

document.getElementById("n").innerHTML ="";

document.getElementById("n").innerHTML = "<INPUT TYPE='hidden' NAME='numar_produse' VALUE='" + nrProduseProforma + "'>"
										+"<INPUT TYPE='hidden' NAME='subtotalValoare' VALUE='" + Math.round(100 * subTotalValoare) / 100  + "'>"
                      					+"<INPUT TYPE='hidden' NAME='subtotalTva' VALUE='" + Math.round(100 * subTotalTva) / 100  + "'>"  
                                        +"<INPUT TYPE='hidden' NAME='total' VALUE='" + Math.round(100 * total) / 100  + "'>";  

document.getElementById("proformaSubtotalValoare").innerHTML = Math.round(100 * subTotalValoare) / 100;

document.getElementById("proformaSubtotalTva").innerHTML = Math.round(100 * subTotalTva) / 100;

document.getElementById("proformaTotal").innerHTML = Math.round(100 * total) / 100;

//document.getElementById('valoareProduse').value = Math.round(100 * total) / 100;

//document.getElementById('valoareProduse').readOnly = true;

//modificareValoareAvansEvent();

} 
function resetProdusForm(){	if(document.getElementById("denumireProdus")!=null && document.getElementById("umProdus")!=null && document.getElementById("cantitateProdus")!=null && document.getElementById("pretUnitarProdus")!=null ){		document.getElementById("denumireProdus").value = "";		document.getElementById("pretUnitarProdus").value = "";							if(document.getElementById('eFacturaServicii').checked == false){			document.getElementById("umProdus").value = "";			document.getElementById("cantitateProdus").value = "";		}	}
}function resetFacturaForm(){	document.getElementById('form1').reset();	document.getElementById('umProdus').disabled = false;    document.getElementById('cantitateProdus').disabled = false;
}

//event handlers
function eFacturaServicii_onChange(obj){
  if (obj.checked == 1){
    document.getElementById('umProdus').disabled = true;
    document.getElementById('umProdus').value = '-';
    document.getElementById('cantitateProdus').disabled = true;
    document.getElementById('cantitateProdus').value = '-';    
  }
  else{
    document.getElementById('umProdus').disabled = false;
   // document.getElementById('umProdus').value = ''; //stergea campul adus cu ajaxu
    document.getElementById('cantitateProdus').disabled = false;
    document.getElementById('cantitateProdus').value = '';    
  }
}





