// (c) Luis Miguel Molinero
// 31-Octubre-2002
// calcppost.js -> calcppost.htm
//-------------------------------------------------------
function getCampoVacio(textObj)
{

  if (textObj.value.length == 0) return true;
  for (var i=0; i<textObj.value.length; ++i) {
   var ch = textObj.value.charAt(i);
   if (ch != ' ' && ch != '\t') return false;
  }
  return true;
}
//-------------------------------------------------------
function decimales2(anumero)
{
  num = Math.round(anumero*100)/100;
  return num
}
//-------------------------------------------------------
function limpiaresul()
{
   document.form1.PpostP.value="";
   document.form1.PpostN.value="";
   document.form1.CPP.value="";
   document.form1.CPN.value="";
}   
//-------------------------------------------------------
function limpia()
{
   document.form1.Sensibilidad.value="";
   document.form1.Especificidad.value="";
   document.form1.Ppre.value="";
   limpiaresul()
}
//-------------------------------------------------------
function comprueba(textObj)
{
  if (!getCampoVacio(textObj)) 
  {
    num=1*textObj.value;
    if ((num <= 0) || (num >= 1)) { 
      alert('Debe ser un número entre 0 y 1');
      textObj.focus();
     }
  }
}
//-------------------------------------------------------

var
 S=0
 E=0
 CPP=0
 CPN=0
 Ppre=0
 PpostP=0
 PpostN=0 
 
function calculos()
{
  if (getCampoVacio(document.form1.Sensibilidad)) { S=-1 }
  else { S=1*document.form1.Sensibilidad.value }
  
  if (getCampoVacio(document.form1.Especificidad)) { E=-1 }
  else { E=1*document.form1.Especificidad.value }
  
  if ((S >= 0) && (E >= 0) && (E < 1))
  {
    CPP=S/(1-E);
    document.form1.CPP.value=decimales2(CPP);
  } else {
    document.form1.CPP.value="";
    CPP=-1
  }  
    
  if ((S >= 0) && (E > 0))
  {
    CPN=(1-S)/E;
    document.form1.CPN.value=decimales2(CPN);
  } else {
    document.form1.CPN.value="";
    CPN=-1
  }  
  
  if (getCampoVacio(document.form1.Ppre)) { Ppre=-1 }
  else { Ppre=1*document.form1.Ppre.value }

  if ((Ppre > 0) && (CPP > 0)) 
  {
    PpostP=Ppre*CPP/(1+Ppre*(CPP-1));
    document.form1.PpostP.value=decimales2(PpostP);
  } else {
     document.form1.PpostP.value="";  
  }

  if ((Ppre > 0) && (CPN > 0)) 
  {
    PpostN=Ppre*CPN/(1+Ppre*(CPN-1));
    document.form1.PpostN.value=decimales2(PpostN);
  } else {
     document.form1.PpostN.value="";  
  }

}

