Ver Mensaje Individual
  #3 (permalink)  
Antiguo 09/06/2009, 02:40
quimfv
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 10 meses
Puntos: 574
Respuesta: Operacion Matematica con un formulario

Uffff me has pillado con ganas....

Código HTML:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Documento sin t&iacute;tulo</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
function calcula(obj){
var hilos;
var iva;
var preciohilo;
	if (obj.value.length!=0 && !isNaN(obj.value)){
		hilos=parseFloat(obj.value);
		if (document.getElementById("preciohilo").value.length!=0 
		       && !isNaN(document.getElementById("preciohilo").value)){
			preciohilo=parseFloat(document.getElementById("preciohilo").value);
		}else{
			preciohilo=0;
			alert("Precio unitario igual a cero?");
		}
		document.getElementById("totalx").innerHTML=format_number(parseInt(preciohilo * hilos*100+.5,10)/100,2);
		if (document.getElementById("tpiva").value.length!=0 
		       && !isNaN(document.getElementById("tpiva").value)){
			iva=parseFloat(document.getElementById("tpiva").value)/100;
		}else{
			iva=0;
			alert("IVA igual a cero?");
		}
		document.getElementById("subtotal").innerHTML=format_number(parseInt(preciohilo * hilos*100+.5,10)/100,2);
		document.getElementById("iva").innerHTML=format_number(parseInt((preciohilo * hilos)*iva*100+.5,10)/100,2);
		document.getElementById("total").innerHTML=format_number(parseInt((preciohilo * hilos)*(1+iva)*100+.5,10)/100,2);
	} 
}

function format_number(pnumber,decimals){
	if (isNaN(pnumber)) { return 0};
	if (pnumber=='') { return 0};
	
	var snum = new String(pnumber);
	var sec = snum.split('.');
	var whole = parseFloat(sec[0]);
	var result = '';
	
	if(sec.length > 1){
		var dec = new String(sec[1]);
		dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
		dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
		var dot = dec.indexOf('.');
		if(dot == -1){
			dec += '.'; 
			dot = dec.indexOf('.');
		}
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	} else{
		var dot;
		var dec = new String(whole);
		dec += '.';
		dot = dec.indexOf('.');		
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	}	
	return result;
}
</script>
</head>
<body>
<form id="form2" name="form2" method="post" action="">
  <table border="0" cellspacing="0" cellpadding="0" style="font-size:11px; color:#000;">
    <tr> 
      <td width="215" valign="top"><p><span class="rows">SERVICIO</span></p></td>
      <td width="104" valign="top"><p><span class="rows">PRECIO</span></p></td>
      <td width="134" valign="top"><p><span class="rows">CANTIDAD DE HILOS</span></p></td>
      <td width="183" valign="top"><p><span class="rows">TOTAL ($70 X NUM DE HILOS) 
          </span></p></td>
    </tr>
    <tr> 
      <td width="215"><p><span class="rows">Instalación de colgante por hilo</span></p></td>
      <td width="104"><p align="center"><span class="rows">$
          <input name="hilos" type="text" id="preciohilo" size="8" value="70.00" onKeyUp="calcula(document.getElementById('hilos'))"/>
          &nbsp;x&nbsp;hilo</span></p></td>
      <td width="134" valign="top"><p> 
          <label> 
          <input name="hilos" type="text" id="hilos" size="5" onKeyUp="calcula(this)"/>
          </label>
        </p></td>
      <td width="183" valign="top" id="totalx" align="right">0</td>
    </tr>
    <tr> 
      <td colspan="4" valign="top"><p><span class="rows">Descripción :</span></p></td>
    </tr>
    <tr> 
      <td colspan="4" valign="top"><p><span class="rows">Cantidad de colgantes:</span></p></td>
    </tr>
    <tr> 
      <td colspan="3" align="right"><span class="rows">Precios mas I.V.A.</span></td>
      <td width="183" valign="top" align="right"><input name="hilos" type="text" id="tpiva" size="5" value="15" onKeyUp="calcula(document.getElementById('hilos'))"/>
        %</td>
    </tr>
    <tr> 
      <td colspan="3" valign="top"><p align="right"><span class="rows">SUB-TOTAL</span></p></td>
      <td width="183" valign="top" id="subtotal" align="right">0</td>
    </tr>
    <tr> 
      <td colspan="3" valign="top"><p align="right"><span class="rows">IVA</span></p></td>
      <td width="183" valign="top" id="iva" align="right">0</td>
    </tr>
    <tr> 
      <td colspan="3" valign="top"><p align="right"><span class="rows">TOTAL</span></p></td>
      <td width="183" valign="top" id="total" align="right">0</td>
    </tr>
  </table>
</form>
</body>
</html> 

Quim