// Funciones Ajax

function handleHttpResponse() { 
    if (http.readyState == 4) { 
       if (http.status == 200) { 
          if (http.responseText.indexOf('invalid') == -1) {
             // Armamos un array, usando la coma para separar elementos
             results = http.responseText.split("$$");
             document.getElementById("importe").value = results[0];
			 document.getElementById("gastos").value = results[1];
			 document.getElementById("retencion").value = results[2];
			 document.getElementById("cartera_importe").value = results[3];
			 document.getElementById("dto").value = results[4];
			 document.getElementById("timbres").value = results[5];
			 document.getElementById("tae").value = results[6];
			 document.getElementById("percibido").value = results[7];
			 document.getElementById("vtomedio").value = results[8];
			 document.getElementById("entregas").value = results[9];
			 document.getElementById("liquido").value = results[10];
			 document.getElementById("impagados").value = results[11];
			 //document.getElementById("principal").innerHTML = http.responseText;
             enProceso = false;
          }
      }
   }
}



function wait() {
document.getElementById("principal").innerHTML = '<br><br><br><br><br><br><br><br><img src="images/cargando.gif">';
}

function calcula_todo(fecha, timbres, liquido, retencion, entregas, impagados, fecha2, importe2, idOP) {
    if (!enProceso && http) {
	/*alert ("***");*/
	   /*wait();*/
       var url = "calcula.asp?fecha="+fecha+"&timbres="+timbres+"&liquido="+liquido+"&retencion="+retencion+"&entregas="+entregas+"&impagados="+impagados+"&fecha2="+fecha2+"&importe2="+importe2+"&idOP="+idOP;
	   //alert (url)
       http.open("GET", url, true);
       http.onreadystatechange =  handleHttpResponse;
       enProceso = true;
       http.send(null);
    }
}


function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
       try {
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
          try {
             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (E) { xmlhttp = false; }
       }
    @else
    xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
       try {
          xmlhttp = new XMLHttpRequest();
       } catch (e) { xmlhttp = false; }
    }
    return xmlhttp;
}

var enProceso = false; // lo usamos para ver si hay un proceso activo
var http = getHTTPObject(); // Creamos el objeto XMLHttpRequest


	
//***********************************************************************************************
// validarFecha(dia,mes, año)
//
// Valida que el día y el mes introducidos sean correctos. Además valida que el año introducido
// sea o no bisiesto
//
//***********************************************************************************************
function validarFecha(dia,mes,anio)
{
var elMes = parseInt(mes);

if(elMes>12)
return 1;
// MES FEBRERO
if(elMes == 2){
if(esBisiesto(anio)){
if(parseInt(dia) > 29){
return 1;
}
else
return 0;
}
else{
if(parseInt(dia) > 28){
return 1;
}
else
return 0;
}
}
//RESTO DE MESES

if(elMes== 4 || elMes==6 || elMes==9 || elMes==11){
if(parseInt(dia) > 30){
return 1;
}
}
return 0;

}
//*****************************************************************************************
// esBisiesto(anio)
//
// Determina si el año pasado com parámetro es o no bisiesto
//*****************************************************************************************
function esBisiesto(anio)
{
var BISIESTO;
if(parseInt(anio)%4==0){
if(parseInt(anio)%100==0){
if(parseInt(anio)%400==0){
BISIESTO=true;
}
else{
BISIESTO=false;
}
}
else{
BISIESTO=true;
}
}
else
BISIESTO=false;

return BISIESTO;
} 

function formatNumber(num,prefix){
prefix = prefix || '';
num += '';
var splitStr = num.split(',');
var splitLeft = splitStr[0];
var splitRight = splitStr.length > 1 ? ',' + splitStr[1] : '';
var regx = /(\d+)(\d{3})/;
while (regx.test(splitLeft)) {
splitLeft = splitLeft.replace(regx, '$1' + '.' + '$2');
}

return prefix + splitLeft + splitRight;
}

function unmask (num) {
	return num.replace(".",",");
}
