function calcularFRA(valorNominal,plazovencimiento,tasaLiquidacionEA,tasafraEA,tipoFra)
{
  if (!isNumber(tasafraEA.value))
  {
   alert("Debe ingresar números en este campo")
   tasafraEA.focus()
   return
  }
  if (!isNumber(tasaLiquidacionEA.value))
  {
   alert("Debe ingresar números en este campo")
   tasaLiquidacionEA.focus()
   return
  }
  if (!isNumber(tasaLiquidacionEA.value))
  {
   alert("Debe ingresar números en este campo")
   tasaLiquidacionEA.focus()
   return
  }
  valorNominal.value=unFormat(valorNominal.value)
  if (!isNumber(valorNominal.value))
  {
   alert("Debe ingresar números en este campo")
   txtValorNominal.focus()
   return
  }
  txtTasaFraPV.value= convertirTasaPV(tasafraEA.value,plazovencimiento)
  txtTasaLiquidacionPV.value= convertirTasaPV(tasaLiquidacionEA.value,plazovencimiento)

  valornominal=valorNominal.value;
  tasafraPV=txtTasaFraPV.value/100
  tasaliquidacionPV=txtTasaLiquidacionPV.value/100
  tasaLiquidacionEA=tasaLiquidacionEA.value/100
  
  txtInteresFra.value = (valornominal*tasafraPV*plazovencimiento)/360
  txtInteresLiquidacion.value=(valornominal*tasaliquidacionPV*plazovencimiento)/360
  if (tipoFra=='Compra')
    txtLiquidacion.value=parseFloat(txtInteresLiquidacion.value)-parseFloat(txtInteresFra.value)   
  else
     txtLiquidacion.value=parseFloat(txtInteresFra.value)-parseFloat(txtInteresLiquidacion.value)
  txtVPNLiquidacion.value=txtLiquidacion.value/(Math.pow((1+tasaLiquidacionEA),(plazovencimiento/360)))
  
  txtInteresFra.value=round2(txtInteresFra.value,1)
  txtInteresLiquidacion.value=round2(txtInteresLiquidacion.value,1)
  txtLiquidacion.value=round2(txtLiquidacion.value,1)
  txtVPNLiquidacion.value=round2(txtVPNLiquidacion.value,1)
  valorNominal.value=round2(valorNominal.value,1)
  ref1.focus()
}

function convertirTasaPV(tasa,plazo)
{
  if (!isNumber(tasa))
  {
    return ""
  }
  if (tasa!="")
  {
    tasa=tasa/100
    t1=parseFloat(1+tasa)
    t2=parseFloat(plazo/360)
    t3=Math.pow(t1,t2)
    return round2(((t3-1)*4)*100,2)
  }
}

function btnCalcular_onclick() {
	I1=Form1.selMoneda1.item(Form1.selMoneda1.selectedIndex).value;
	I2=Form1.selMoneda1.item(Form1.selMoneda2.selectedIndex).value;
	x=parseFloat(Form1.Valor.value);
	if (Form1.selMoneda1.selectedIndex<2 && Form1.selMoneda2.selectedIndex<2)
	{
	  if (Form1.selMoneda1.selectedIndex==0)
	     {I1=1/valordolar; }
	  if (Form1.selMoneda1.selectedIndex==1)
	     {I1=1;}
	  if (Form1.selMoneda2.selectedIndex==0)
	     {I2=1/valordolar; }
	  if (Form1.selMoneda2.selectedIndex==1)
	     {I2=1;  }
	}
	y=parseFloat(I1);
	k=parseFloat(I2)
	z=parseFloat(1/k);
	w=x*y*z;
	return round(w,4);
}

function round(number,X) {
X = (!X ? 2 : X);
yy= Math.round(number*Math.pow(10,X))/Math.pow(10,X);
return formatValue(yy,"###,###,###,###,###,###.####");
}

function round2(number,X) {
X = (!X ? 2 : X);
yy= Math.round(number*Math.pow(10,X))/Math.pow(10,X);
if (X==2)
  return formatValue(yy,"###,###,###,###,###,###.##");
else
  yy= formatValue(yy,"###,###,###,###,###,###."); 
return yy.substring(0,yy.indexOf("."))
}


function formatValue(argvalue, format) {
  //alert("m1");
  var numOfDecimal = 0;
  if (format.indexOf(".") != -1) {
    numOfDecimal = format.substring(format.indexOf(".") + 1, format.length).length;
  }
  argvalue = formatDecimal(argvalue, true, numOfDecimal);

  argvalueBeforeDot = argvalue.substring(0, argvalue.indexOf("."));
  retValue = argvalue.substring(argvalue.indexOf("."), argvalue.length);

  strBeforeDot = format.substring(0, format.indexOf("."));

  for (var n = strBeforeDot.length - 1; n >= 0; n--) {
    oneformatchar = strBeforeDot.substring(n, n + 1);
    if (oneformatchar == "#") {
      if (argvalueBeforeDot.length > 0) {
        argvalueonechar = argvalueBeforeDot.substring(argvalueBeforeDot.length - 1, argvalueBeforeDot.length);
        retValue = argvalueonechar + retValue;
        argvalueBeforeDot = argvalueBeforeDot.substring(0, argvalueBeforeDot.length - 1);
      }
    }
    else {
      if (argvalueBeforeDot.length > 0 || n == 0)
        retValue = oneformatchar + retValue;
    }
  }
  if (retValue.substring(retValue.indexOf(".")+1)=="0000")
     {
     xx=retValue.substring(0,retValue.indexOf(".")) + ".00";   
     return xx;
     }
  if (retValue.indexOf("-,") != -1)
    {
      xx=retValue.substring(retValue.indexOf("-,")+2,retValue.length)
      retValue="-" + xx
    }
  return retValue;  
}

function formatDecimal(argvalue, addzero, decimaln) {
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;

  number = Math.pow(10, numOfDecimal);

  argvalue = Math.round(parseFloat(argvalue) * number) / number;
  // If you're using IE3.x, you will get error with the following line.
  // argvalue = argvalue.toString();
  // It works fine in IE4.
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;

  if (addzero == true) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";

    while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
      argvalue = argvalue + "0";
  }

  return argvalue;
}

function unFormat(str) {
var value='';
if (notNull(str) && notBlank(str))
{
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar >= "0" && mychar <= "9")) {
		 value=value + mychar;
		}
	} // for
	return value;
} // ifvaluetrue	
} // function

function isNumber(str) {
if (notNull(str) && notBlank(str))
{
	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i)
		if ((mychar >= "0" && mychar <= "9") || mychar=='.') {}
		else 
			return false
	} // for
} // if
else
  return false
return true	
} // function


function notNull(str)
{
  if (str.length == 0 )
    return false
  else
  {
    return true
  }
}

function notBlank(str) {
  for (i = 0; i < str.length; i++) {
  if (str.charAt(i) != " ")
    return true
  }
}
