//-------------------------------------//
// Formatea la Fecha de para HOST      //
//-------------------------------------//
function dameFechaHost(valor) {
	var fecha = valor;
	var separador = '/';
	if (fecha.indexOf('-') != -1) separador = '-';
	if (fecha.indexOf(separador) != -1) {
		var fechaaux = fecha;
		sdia = fechaaux.substring(0, fechaaux.indexOf(separador));
		if (sdia.length == 1) sdia = '0' + sdia;
		fechaaux = fechaaux.substring(fechaaux.indexOf(separador) + 1);
		if (fechaaux.indexOf(separador) != -1) {
			smes = fechaaux.substring(0, fechaaux.indexOf(separador));
			if (smes.length == 1) smes = '0' + smes;
			fechaaux = fechaaux.substring(fechaaux.indexOf(separador) + 1);
			if (fechaaux.length == 1) fechaaux = '200' + fechaaux;
			else if (fechaaux.length == 2) fechaaux = '20' + fechaaux;
			else if (fechaaux.length == 3) fechaaux = '2' + fechaaux;
			fecha = sdia + smes + fechaaux;
		}
	}
	return fecha;
}
//-------------------------------------//
// Desformatea la Fecha de Pantalla    //
//-------------------------------------//
function Quitar_Barras(miFecha) {
	var fecha = miFecha;
	var separador = '/';
	if (fecha.indexOf('-') != -1) separador = '-';
	var pos = fecha.indexOf(separador);
	while(pos != -1) {
		fecha = fecha.substring(0, pos) + fecha.substring(pos+1);
		pos = fecha.indexOf(separador);
	}
	return fecha;
}
//----------------------------------//
// Formatea la Fecha de Pantalla    //
//----------------------------------//
function Poner_Barras(miFecha) {	
	var fecha;
	fecha = miFecha.value;
	if (fecha.length == 8) 
		miFecha.value = fecha.substring(0,2) + "/" + fecha.substring(2,4) + "/" + fecha.substring(4,8)	
}
// Call this function to return the current date 
function date(marco){
	if (typeof marco == 'undefined' || marco == '') marco = top;
	// Create new date object
	var RightNow;
	RightNow = new Date();
	var Month;
	var Dia;
	var Year;
	Month = RightNow.getMonth();
	Dia = RightNow.getDate();
	Year = RightNow.getFullYear();
	Month = Month + 1;
	if(Month < 10) {
		Month = "0" + Month;
	}
	if(Dia < 10){
		Dia = "0" + Dia;
	}
	Year = Year - 2000;
	if(Year < 10) {
		Year = "200" + Year;
	}
	marco.document.write(Dia + "." + Month  + "." + Year);
}
function mostrarFechaSistema() {
	fechaSist=new Date()
	anio=fechaSist.getYear()
	if (anio<2000) {
		anio=1900+anio;
	}
	mes=fechaSist.getMonth() + 1;
	dia=fechaSist.getDate();
	dia=strChar(dia,2,'0');
	mes=strChar(mes,2,'0');
	anio=strChar(anio,4,'0');
	return( dia + '/' + mes + '/' + anio )
}
function strChar(Cadena, Largo, Car) {
	Cadena = "" + Cadena;
	for(var tmp=Cadena, i=Cadena.length;i<Largo;i++)
		tmp=Car+tmp;
	return (tmp);
}
//--------------------------------------------//
// Función para validar Fechas desde y hasta  //
//--------------------------------------------//
function valida_fecha(fecha,origen,FechaSistema) {
	var mes; 
	var anio; 
	var dia;
	var mesactual; 
	var anioactual; 
	var diaactual;
	var smes; 
	var sanio; 
	var sdia;
	var smesactual; 
	var sanioactual; 
	var sdiaactual;
	var longitud_fch;
	var separador = '/';
	if (fecha.indexOf('-') != -1) separador = '-';
	if (fecha.indexOf(separador) != -1) {
		var fechaaux = fecha;
		sdia = fechaaux.substring(0, fechaaux.indexOf(separador));
		if (sdia.length == 1) sdia = '0' + sdia;
		fechaaux = fechaaux.substring(fechaaux.indexOf(separador) + 1);
		if (fechaaux.indexOf(separador) != -1) {
			smes = fechaaux.substring(0, fechaaux.indexOf(separador));
			if (smes.length == 1) smes = '0' + smes;
			fechaaux = fechaaux.substring(fechaaux.indexOf(separador) + 1);
			if (fechaaux.length == 1) fechaaux = '200' + fechaaux;
			else if (fechaaux.length == 2) fechaaux = '20' + fechaaux;
			else if (fechaaux.length == 3) fechaaux = '2' + fechaaux;
			fecha = sdia + smes + fechaaux;
		}
	}
	if(fecha.length == 8)	{
		fecha = fecha.substring(0,2) + separador + fecha.substring(2,4) + separador + fecha.substring(4,8)	
	}
	if (fecha.substring(2, 3) != separador || fecha.substring(5, 6) != separador)	{
		return false;
	}
	longitud_fch = parseInt(fecha.length);
	if (longitud_fch != 10)	{
		return false;
	}	else {
		sanio = fecha.substring(6,10);
		sdia = fecha.substring(0,2);
		smes = fecha.substring(3,5);
		sanioactual = FechaSistema.getFullYear().toString();
		sdiaactual = FechaSistema.getDate().toString();
		smesactual = (FechaSistema.getMonth() + 1).toString();
		if(smesactual.length < 2) {
			smesactual = "0" + smesactual;
		}
		if(sdiaactual.length < 2)	{
			sdiaactual = "0" + sdiaactual;
		}
		if (!top.bEsNumero(sanio) || !top.bEsNumero(sdia) || !top.bEsNumero(smes)) {
			return false;
		}
		anio = parseInt(sanio);
		dia = parseFloat(sdia);
		mes = parseFloat(smes);
		anioactual = parseInt(sanioactual);
		diaactual = parseFloat(sdiaactual);
		mesactual = parseFloat(smesactual); 
		if (mes > 0 && mes < 13 && dia > 0 && dia <= iMaxDiasMes(mes, anio) && anio >= 1900)	{
			//Validamos que la fecha final no sea mayor a la del sistema (o sea, actual)
			if(origen=='F')	{
				if(parseFloat(sanio + smes + sdia) > parseFloat(sanioactual + smesactual + sdiaactual))	{
					return false;
				}
			}
			//Si es anular, Validamos que la fecha inicial no sea mayor a la del sistema (o sea, actual)
			if(origen=='I')	{
				if(parseFloat(sanio + smes + sdia) > parseFloat(sanioactual + smesactual + sdiaactual)) {
					return false;
				}			
			}
			return true;
		}	else {
			return false;
		}
	}
}
function iMaxDiasMes(iMes, iAno) {
	if (iMes ==1 || iMes == 3 || iMes == 5 || iMes == 7 || iMes == 8 || iMes == 10 || iMes == 12) {
		return 31;
	} else {
		if ( iMes == 4 || iMes == 6 || iMes == 9 || iMes == 11 ) {
			return 30;
		} else {
			if (((iAno % 4 == 0) && (iAno % 100 != 0)) || (iAno % 400 == 0)) {
				return  29;
			} else {
				return 28;
			} 
		}
	}
}

function ComparaFechas(FechaDesde,FechaHasta)
{
	var Desde; 
	var Hasta; 
	
	//alert("D: " + FechaDesde);
	//alert("H: " + FechaHasta);
	
	if(FechaDesde.length == 8)
	{
		FechaDesde = FechaDesde.substring(0,2) + "/" + FechaDesde.substring(2,4) + "/" + FechaDesde.substring(4,8)	
	}
	var sanio = FechaDesde.substring(6, 10);
	var sdia = FechaDesde.substring(0, 2);
	var smes = FechaDesde.substring(3, 5);
	Desde = sanio + smes + sdia;

	if(FechaHasta.length == 8)
	{
		FechaHasta = FechaHasta.substring(0,2) + "/" + FechaHasta.substring(2,4) + "/" + FechaHasta.substring(4,8)	
	}
	sanio = FechaHasta.substring(6, 10);
	sdia = FechaHasta.substring(0, 2);
	smes = FechaHasta.substring(3, 5);
	Hasta = sanio + smes + sdia;
	
	if(Desde > Hasta)
	{
		return false;
	}
	else
	{
		return true;
	}
}

/**
 * Formatea la fecha desde formato yyyymmdd a formato de página dd/mm/yyyy
 */
function fechaVer(sFecha) 
{
	var sRespuesta = '';
	sRespuesta = sFecha;
	if (sRespuesta.length == 8)
	{
		sRespuesta = sFecha.substring(6,8) + "/" + sFecha.substring(4,6) + "/" + sFecha.substring(0,4);
	}	
	return sRespuesta;
}

function FechaHost(fecha) 
{
	if(fecha.length == 10)
	{
		fechaAux = fecha.substring(0,2) +  fecha.substring(3,5) + fecha.substring(6,10);
		fecha = fechaAux;
	}
	var fechaF = fecha.substring(4,8) + fecha.substring(2,4) + fecha.substring(0,2);
	return fechaF;
}

function formateoFecha(sFecha)
{
	if (sFecha == "" || sFecha == " ")
	{
		return sFecha;
	}

	pos=sFecha.indexOf("/");
	while(pos != -1)
	{
		sFecha = sFecha.substring(0, pos) + sFecha.substring(pos+1);
		pos=sFecha.indexOf("/");
	}
	
	if(sFecha != "" && sFecha.length < 8)
	{
		return sFecha;
	}
	else 
	{
		if(sFecha=="")
		{
			fechaF = "";
		}
		else
		{
			fechaF = sFecha.substring(0,2) +"/"+  sFecha.substring(2,4) +"/"+  sFecha.substring(4,8);
		}
		return fechaF;
	}
}

function desformateoFecha(sFecha) {
	if (sFecha == "" || sFecha == " " || sFecha < 8)
	{
		return sFecha;
	}
	
	if (sFecha.substr(2,1) == "/" && sFecha.substr(5,1) == "/")
	{
		fechaF = sFecha.substring(0,2) +  sFecha.substring(3,5) +  sFecha.substring(6,10);
		return fechaF;
	}
}

function formateoHora(sHora)
{
	if (sHora == "" || sHora == " ")
	{
		return sHora;
	}

	pos=sHora.indexOf(":");
	while(pos != -1){
		sHora = sHora.substring(0, pos) + sHora.substring(pos+1);
		pos=sHora.indexOf(":");
	}

	pos=sHora.indexOf(".");
	while(pos != -1){
		sHora = sHora.substring(0, pos) + sHora.substring(pos+1);
		pos=sHora.indexOf(".");
	}
	
	if(sHora != "" && sHora.length < 7)
	{
		return sHora;
	}
	else 
	{
		if(sHora=="")
		{
			horaF = "";
		}
		else
		{
			horaF = sHora.substring(0,2) +":"+  sHora.substring(2,4) + ":" +  sHora.substring(4,6) + "." +  sHora.substring(6);
		}
		return horaF;
	}
}

function desformateoHora(sHora) 
{
	if (sHora == "" || sHora == " " || sHora.length < 7)
	{
		return sHora;
	}
	
	if (sHora.substr(2,1) == ":" && sHora.substr(5,1) == ":" && sHora.substr(8,1) == ".")
	{
		horaF = sHora.substring(0,2) +  sHora.substring(3,5) +  sHora.substring(6,8) + sHora.substring(9);
		return horaF;
	}
}

// Funcion que formatea la fecha utilizando como separador sSeparador. Si no viene pone / por defecto
//Si fecha es 00000000 o blancos se retornará blancos
function formateaFecha(sFecha, sSeparador)
{
    var sResultado = '';

    if (Math.floor(sFecha) == 0)
    {
        return sResultado;
    }

    sResultado = sFecha;
    if (typeof sSeparador == 'undefined')
    {
        sSeparador = '/';
    }

    if (sFecha.length == 8)
    {
        sResultado = sFecha.substring(0,2) + sSeparador + sFecha.substring(2,4) + sSeparador + sFecha.substring(4);
    }

    if (sFecha.length == 6)
    {
        sResultado = sFecha.substring(0,2) + sSeparador + sFecha.substring(2);
    }
    return sResultado;
}
	function formateaFechaWeb(fechaWeb) {
		var separadorWeb = '/';
		var smesWeb; 
		var sanioWeb; 
		var sdiaWeb;
		if (fechaWeb.indexOf('-') != -1) separadorWeb = '-';
		if (fechaWeb.indexOf(separadorWeb) != -1) {
			var fechaauxWeb = fechaWeb;
			sdiaWeb = fechaauxWeb.substring(0, fechaauxWeb.indexOf(separadorWeb));
			if (sdiaWeb.length == 1) sdiaWeb = '0' + sdiaWeb;
			fechaauxWeb = fechaauxWeb.substring(fechaauxWeb.indexOf(separadorWeb) + 1);
			if (fechaauxWeb.indexOf(separadorWeb) != -1) {
				smesWeb = fechaauxWeb.substring(0, fechaauxWeb.indexOf(separadorWeb));
				if (smesWeb.length == 1) smesWeb = '0' + smesWeb;
				fechaauxWeb = fechaauxWeb.substring(fechaauxWeb.indexOf(separadorWeb) + 1);
				if (fechaauxWeb.length == 1) fechaauxWeb = '200' + fechaauxWeb;
				else if (fechaauxWeb.length == 2) fechaauxWeb = '20' + fechaauxWeb;
				else if (fechaauxWeb.length == 3) fechaauxWeb = '2' + fechaauxWeb;
				fechaWeb = sdiaWeb + separadorWeb + smesWeb + separadorWeb + fechaauxWeb;
			}
		}
		return fechaWeb;
	}

