// *********************************************************************************
//	* Desarrollo: Sistran S.A
//	* Fecha: 14/05/08
//	* Utilidad: funciones javascript usadas en diferentes pantallas de la aplicacion
// *********************************************************************************

// variables globales para paginacion
var totalPaginas= 0;
var paginaActual=0;

function displayFormatted(num){
	if (num == '')
		return("");
	else
		return (NumberToSpFormat(num, 2));
}

function NumberToSpFormat(ss, icntDec){
	var sSign = ""
   
	ss = ss.replace(".", ",");   

	if (ss.slice(0,1) == "-")
	{  
  		var sSign = ss.slice(0,1);
		ss = ss.slice(1, ss.length);
	}

	ss = RoundDecimal(ss, icntDec);
   
	var n = ss.indexOf(",");	
	var iInteger = 0;
	var sInteger = ss.slice(0, n);
	var sDecimal = ss.slice(n, ss.length);
	if (sDecimal.slice(0,1) == "1")
	{
		iInteger = parseInt(sInteger.slice(n-2, n-1));
		iInteger = iInteger + 1 ;
		iInteger.toString();
		sInteger = sInteger.slice(0,n-1) + iInteger
	}   

	var sTemp = ""
	var iOrder;

	for (i=0; (i + 3) < sInteger.length; i=i+3) 
	{     
		if (i == (sInteger.length - 3))
			sTemp = sInteger.slice((sInteger.length - i - 3), (sInteger.length - i)) + sTemp; 
		else
			sTemp = '.' + sInteger.slice((sInteger.length - i - 3), (sInteger.length - i)) + sTemp; 
	}
	var sSpNumber = sSign + sInteger.slice(0, (sInteger.length - i)) + sTemp + sDecimal;   
	
	return (sSpNumber);
}

function RoundDecimal (sNumber, iCntDec){
	var iSuma = 0;
	var sDecimal = "";
	var iInteger = 0;
	
	var n = sNumber.indexOf(",");
	if (n > 0) 
	{
		var sIntNumber = sNumber.slice(0, n);
		var sDecimalNumber = sNumber.slice(n, sNumber.length);
	}
	else
	{
		var sIntNumber = sNumber;
		var sDecimalNumber = ",00";
	}
	
	for (i=sDecimalNumber.length-1; i >= iCntDec; i=i-1) 
		{
			iInteger = parseInt(sDecimalNumber.slice(i, i + 1));
			iInteger = iInteger + iSuma;
			
			if (iInteger > 5)
				iSuma = 1;
			else
				iSuma = 0;
		}
				
		i=i+1;
		while(iInteger >= 10)
		{
			if (i == 0)
				break;
			
			iInteger = iInteger - 10;
			sDecimal = iInteger.toString() + sDecimal;
			
			if (i > 0)
			{
				i=i-1;
				iInteger = parseFloat(sDecimalNumber.slice(i, i+1)) + 1;
			}
			else
				iInteger = 0;
		}
	
		if (i == 0)
		{
			iInteger = parseFloat(sIntNumber);
			iInteger = iInteger + 1;
			sEntero = iInteger.toString();
			sIntNumber = sEntero;
		}
		else
		{
			sDecimal = iInteger.toString() + sDecimal;
			sDecimal = sDecimalNumber.slice(1, i) + sDecimal;
	}
	
	return(sIntNumber + "," + sDecimal);
}

// muestra mensaje de proceso
function MostrarMensaje(mostrar, mensaje) {
   if (mostrar) {
   	var oDiv;
   	var newposY;
   	
   	oDiv = document.getElementById('divMensaje');
      oDiv.innerHTML = mensaje;
      oDiv.style.display = 'inline';
      oDiv.style.marginLeft = "-" + parseInt(oDiv.offsetWidth / 2) + "px";
  		newposY = document.documentElement.scrollTop - parseInt(oDiv.offsetHeight / 2);
  		oDiv.style.marginTop = newposY + "px";
   } else {
      document.getElementById('divMensaje').style.display = 'none';
   }
}

// muestra pantalla popup
function MostrarPopUp(mostrar, mensaje, nombre) {

   if (mostrar) {
   	var oDiv;
		if (nombre != '' && nombre!=undefined){
			oDiv = document.getElementById(nombre);
		} else{oDiv = document.getElementById('divPopUp');}

      if (mensaje != null){oDiv.innerHTML = mensaje;}
      oDiv.style.display = 'inline';
      oDiv.style.marginLeft = "-" + parseInt(oDiv.offsetWidth / 2) + "px";
  		oDiv.style.marginTop = "-" + parseInt(oDiv.offsetHeight / 2) + "px";

  		// 21/04/2009 - mauricio - sistran - testear
  		oDivFrm = document.getElementById('frmBackPopUp');
  		if (oDivFrm!=null){
  			oDivFrm.style.display = 'inline';
  			oDivFrm.style.marginLeft = "-" + parseInt(oDiv.offsetWidth / 2) + "px";
  			oDivFrm.style.marginTop = "-" + parseInt(oDiv.offsetHeight / 2) + "px";
  		}
  		// fin testeo
  		
   } else {
   	if (nombre != '' && nombre!=undefined){
			oDiv = document.getElementById(nombre).style.display = 'none';	
		} else{oDiv = document.getElementById('divPopUp').style.display = 'none';}
		
		// 21/04/2009 - mauricio - sistran - testear
		if (oDivFrm!=null){oDivFrm.style.display = 'none';}
		// fin testeo
   }
}


// valida direccion de mail
/*function ValidaMail(email) {
	var emailPat=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	var matchArray = email.value.match(emailPat);
	if (matchArray==null){
		return false;
	} else {return true;}
}*/

function ValidaMail(valor) {
valor = trim(valor);
// otro que no funcionaba bien /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3,4})+$/
if (valor != "")
{
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
return true;
} else {
return false;
}
}
else
{
return true;
}
}

// muestra el detalle del anexo de una poliza
function MostrarAnexo(oSelect) {
	var codanexo = oSelect.options[oSelect.selectedIndex].value;
	if (codanexo != '0'){window.location.href = "../../redirectpages.asp?pagina=anexo.asp?CodAnexo=" + codanexo;}
}

//devuelve true si se presiono la tecla enter
function chekKeyEnter(e) {
	var keynum;

	if(window.event) {
		// IE
		keynum = e.keyCode;
	} else if(e.which) {
		// Netscape/Firefox/Opera
		keynum = e.which;
	}
	if (keynum==13){
		return true;
	} else {return false;}
}

//completa con ceros
 function RellenarCeros(iNro, iCnt){
 	var largo = iNro.length;
 	if (largo < iCnt){
		return repl("0",(iCnt - largo)) + iNro;
	} else {
		return iNro;
	}
 }

//replica un caracter n veces 
function repl(s,n) {
	var r='';
	
	while(n) {
	  if(n&1) {r+=s;}
	  s+=s;
	  n>>=1;
	}
	
	return r;
}

// remueve espacios en blanco a la izquierda de un string
function LTrim(value) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// remueve espacios en blanco a la derecha de un string
function RTrim(value) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// remueve espacios en blanco a la derecha e izquierda de un string
function trim(value) {
	return LTrim(RTrim(value));
}

// devuelve un numero flotante redondeado a 2 decimales
function roundFloat(fltValue) {
	return Math.round(fltValue * 100) / 100;
}

// formatea un numero para utilizar en javascript, dejando solo punto decimal
function FormatNumToEnglish(valor){
	var cantdecimales=2;
	var posdecimal = valor.indexOf(",");
	var partentera = "";
	var partdecimal = "";
	var partdecimal_bis = "";
	var reFloat = /^\d{1,6}(\.\d{1,2})?$/;
	valor = valor + "";

	if (posdecimal == -1){
		var posdecimal = valor.indexOf(".");
		if (((valor.length - (posdecimal+1)) <= cantdecimales) && (posdecimal != -1)){
			partentera = valor.substr(0, posdecimal);
			partdecimal = valor.substr(posdecimal + 1,cantdecimales);
		} else {
			partentera = valor.replace(".","");
			partdecimal = "";
		}

	} else {
		partentera = valor.substr(0, posdecimal);
		partentera = partentera.replace(".","");
		partdecimal = valor.substr(posdecimal + 1,cantdecimales);
	}

	if (partdecimal.length < cantdecimales){for (var i= partdecimal.length; i < cantdecimales; i++){partdecimal_bis += "0";}}

	var num = partentera + "." + partdecimal + partdecimal_bis;

	if (reFloat.test(num)){return num;} else {return "0.00";}
}

// formateo de evento segun explorador
function formatEvent(oEvent) {
	if (isIE && isWin) {
		oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;

		oEvent.eventPhase = 2;
		oEvent.isChar = (oEvent.charCode > 0);
		oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
		oEvent.pageY = oEvent.clientY + document.body.scrollTop;

		oEvent.preventDefault = function () {this.returnValue = false;};

		if (oEvent.type == "mouseout") {
			oEvent.relatedTarget = oEvent.toElement;
		} else if (oEvent.type == "mouseover") {
			oEvent.relatedTarget = oEvent.fromElement;
		}

		oEvent.stopPropagation = function () {this.cancelBubble = true;};

		oEvent.target = oEvent.srcElement;
		oEvent.time = (new Date).getTime();
	}
	return oEvent;
}

function allowChars(oTextbox, oEvent, bBlockPaste) {
	oEvent = formatEvent(oEvent);
	if (oEvent.charCode == 0){return true;}
	var sValidChars = oTextbox.getAttribute("validchars");
	var sChar = String.fromCharCode(oEvent.charCode);
	var bIsValidChar = sValidChars.indexOf(sChar) > -1;
	if (bBlockPaste) {return bIsValidChar && !(oEvent.ctrlKey && sChar == "v");} else {return bIsValidChar || oEvent.ctrlKey;}
}

function SelOptionEnSelect(nombreSELECT, valor, indice_default){
	var oSelect = document.getElementById(nombreSELECT);
	for (i=0;i<oSelect.options.length;i++) {
		if(oSelect.options[i].value==valor){oSelect.selectedIndex=i;oSelect=null;return;}
	}
	oSelect.selectedIndex=indice_default;
	oSelect=null;
}

//FormChek.js

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return false;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isIntegerInRange (s, a, b)
{   
	
	if (isEmpty(s))
       if (isIntegerInRange.arguments.length == 1) return false;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;
    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}
// fin FormChek.js

function AbrirAyuda(destino)
{
	window.open("../help/" + destino,"help","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=700, height=400");
}

function CPcuitValido(cuit) {
//Funcion que valida numero de CUIT

	var esCuit = false;

	if (!(cuit.match(/^\d{11}$/)))
	{
		alert("El CUIT es incorrecto.");
	}
	else
	{
		var vec=new Array(10);
		esCuit=false;
		cuit_rearmado="";

		for (i=0; i < cuit.length; i++) {   
			caracter=cuit.charAt( i);
			if ( caracter.charCodeAt(0) >= 48 && caracter.charCodeAt(0) <= 57 )     {
				cuit_rearmado +=caracter;
			}
		}

		cuit=cuit_rearmado;

		x=i=dv=0;
		// Multiplico los dígitos.
		vec[0] = cuit.charAt(  0) * 5;
		vec[1] = cuit.charAt(  1) * 4;
		vec[2] = cuit.charAt(  2) * 3;
		vec[3] = cuit.charAt(  3) * 2;
		vec[4] = cuit.charAt(  4) * 7;
		vec[5] = cuit.charAt(  5) * 6;
		vec[6] = cuit.charAt(  6) * 5;
		vec[7] = cuit.charAt(  7) * 4;
		vec[8] = cuit.charAt(  8) * 3;
		vec[9] = cuit.charAt(  9) * 2;

		// Suma cada uno de los resultado.
		for( i = 0;i<=9; i++) {
			x += vec[i];
		}
		dv = (11 - (x % 11)) % 11;
		if ( dv == cuit.charAt( 10) ) {
			esCuit=true;
		}

		if ( !esCuit ) {
			alert( "El CUIT ingresado no es válido" );
		}
    }
    
    return esCuit;
}

function ValidarCUIT(cuit) {
//Funcion que valida numero de CUIT

	var esCuit = false;

	if (!(cuit.match(/^\d{11}$/)))
	{
		return ("El CUIT es incorrecto.");
	}
	else
	{
		var vec=new Array(10);
		esCuit=false;
		cuit_rearmado="";

		for (i=0; i < cuit.length; i++) {   
			caracter=cuit.charAt( i);
			if ( caracter.charCodeAt(0) >= 48 && caracter.charCodeAt(0) <= 57 )     {
				cuit_rearmado +=caracter;
			}
		}

		cuit=cuit_rearmado;

		x=i=dv=0;
		// Multiplico los dígitos.
		vec[0] = cuit.charAt(  0) * 5;
		vec[1] = cuit.charAt(  1) * 4;
		vec[2] = cuit.charAt(  2) * 3;
		vec[3] = cuit.charAt(  3) * 2;
		vec[4] = cuit.charAt(  4) * 7;
		vec[5] = cuit.charAt(  5) * 6;
		vec[6] = cuit.charAt(  6) * 5;
		vec[7] = cuit.charAt(  7) * 4;
		vec[8] = cuit.charAt(  8) * 3;
		vec[9] = cuit.charAt(  9) * 2;

		// Suma cada uno de los resultado.
		for( i = 0;i<=9; i++) {
			x += vec[i];
		}
		dv = (11 - (x % 11)) % 11;
		if ( dv == cuit.charAt( 10) ) {
			esCuit=true;
		}

		if ( !esCuit ) {
			return( "El CUIT ingresado no es válido" );
		}
    }
    
    return "";
}

function isNumeric(value)
{
	if (/^\d{1,}$/.test(value))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function ValidarFecha(fecha)
{
	if (fecha != undefined && fecha != "" )
	{
		if (!(/^\d{2}\/\d{2}\/\d{4}$/.test(fecha)))
		{
		//alert("formato de fecha no válido (dd/mm/aaaa)");
		return false;
		}
		var dia  =  parseInt(fecha.substring(0,2),10);
		var mes  =  parseInt(fecha.substring(3,5),10);
		var anio =  parseInt(fecha.substring(6),10);
		switch(mes)
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
			numDias=31;
			break;
			case 4: case 6: case 9: case 11:
			numDias=30;
			break;
			case 2:
			if (comprobarSiBisisesto(anio)){ numDias=29; }else{ numDias=28; };
			break;
			default:
			return false;
		}
		if (dia>numDias || dia==0)
		{
			return false;
		}
	
		return true;
	}
	else
	{
		return false;
	}
}

function comprobarSiBisisesto(anio){
if ( ( anio % 100 != 0) && ((anio % 4 == 0) || (anio % 400 == 0))) {
    return true;
    }
else {
    return false;
    }
}

// DEVUELVE 1 SI LA PRIMER FECHA ES MAYOR, 0 SI SON IGUALES Y -1 SI LA SEGUNDA ES MAYOR
function CompararFechas(fecha,fecha1){
fec=fecha.split("/");
fec1=fecha1.split("/");
if(fec[2]>fec1[2])
{
return 1;
}
else if(fec[2]<fec1[2])
{
return -1;
}
else
{
if(parseInt(fec[1],10)> parseInt(fec1[1],10))
{
return 1;
}
else if(parseInt(fec[1],10) < parseInt(fec1[1],10))
{
return -1;
}
else
{
if(parseInt(fec[0],10) > parseInt(fec1[0],10))
{
return 1;
}
else if(parseInt(fec[0],10) < parseInt(fec1[0],10))
{
return -1;
}
else
{
return 0;
}
}
}
}

