// Variables Globales
//Función de Fecha y Hora
var months = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
var dayName = new Array ("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado");
var relojGlobal;
var intDiferencia;

function reloj() {	
    var date = relojGlobal
    intDiferencia = (relojGlobal - new Date())  / 1000	
    correrReloj()
}

function correrReloj() {
	var difMax = 60;
	var difSeg = (relojGlobal - new Date())  / 1000
	if ( difSeg > intDiferencia + difMax || difSeg < intDiferencia - difMax ) {
	    window.location.reload();
	}

	var date =  new Date();
	relojGlobal = new Date(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds() + intDiferencia )
	date = relojGlobal

	var year = date.getFullYear()
	var month = date.getMonth()
	var day = date.getDate()
	var hour = date.getHours()
	var minute = date.getMinutes()
	var second = date.getSeconds()
	var monthname = months[month]

	if (hour > 12) {
		var AM_PM = ""
	} else {
		var AM_PM = ""
	}
	if (minute < 10) {minute = "0" + minute}
	if (second < 10) {second = "0" + second}
	if (month < 10) {month = "0" + month}
	var dayString = dayName[date.getDay()]
	if (document.getElementById('lblFechaHora') !=null) {
	    document.getElementById('lblFechaHora').innerHTML = " " + dayString + " " + day + " de " + monthname + " de " + year + "<br />" +  hour + ":" + minute + ":" + second + " " + AM_PM
	}
	setTimeout("correrReloj()", 1000)
}
//Fin Función de Fecha y Hora

// Funciones Globales
function validarSesion(oFormulario) {
	if ( oFormulario.txtUsuario.value == "" || oFormulario.txtUsuario.value.lenght == 0 ) {
		alert("Hey... debes ingresar tu nombre de usuario...");
		oFormulario.txtUsuario.focus();
		return false;
	}
	if ( oFormulario.txtClave.value == "" || oFormulario.txtClave.value.lenght == 0 ) {
		alert("Ups!... te falto ingresar tu contrasena...");
		oFormulario.txtClave.focus();
		return false;
	}
	showPopWin(sRutaAplicacion + "iniciarSesion.php?u=" + oFormulario.txtUsuario.value + "&p=" + hex_md5(oFormulario.txtClave.value), 380, 120, null, "Iniciando Sesi&oacute;n...");
}

function fnModificarPerfil() {
	showPopWin(sRutaAplicacion + "modificarPerfil.php", 460, 400, null, "Editar mi Perfil");
	/*
	var oWinModPerf;

	oWinModPerf = window.open(sRutaAplicacion + "modificarPerfil.php", "oWinModPerf", "width=460,height=400");
	if (oWinModPerf) {
		oWinModPerf.focus();
	} else {
		alert("Error :\nUps!, debes deshabilitar tu bloqueador de ventanas o popups\no habilitar este sitio como una zona segura.");
	}
	*/
}

function fnModificarMiPerfil() {
	showPopWin(sRutaAplicacion + "modificarPerfil.php", 460, 400, null, "Editar mi Perfil");
}

function fnCambiarFoto() {
	showPopWin(sRutaAplicacion + "cambiarFotografia.php", 380, 140, null, "Cerrar Fotograf&iacute;a Perfil");
}

function fnCambiarContrasenia() {
	showPopWin(sRutaAplicacion + "cambiarClave.php", 380, 125, null, "Cambiar Contrase&ntilde;a");
}

function fnCerrarSesion() {
	showPopWin(sRutaAplicacion + "cerrarSesion.php", 380, 120, null, "Cerrar Sesi&oacute;n");
}

function fnRecuperarContrasenia() {
	showPopWin(sRutaAplicacion + "recordarClave.php", 380, 120, null, "Recuperar Contrase&ntilde;a");
}

function fnEnviarMensajeGeneral() {
	showPopWin(sRutaAplicacion + "enviarMensaje.php", 350, 300, null, "Enviar Mensaje a Jupnaf.com");
}

function fnAbrirGaleriaFotos() {
	showPopWin(sRutaAplicacion + "galeriaFotos_1-0.php", 580, 450, null, "Galer&iacute;a Fotogr&aacute;fica");
}

function fnAbrirGaleriaVideos() {
	showPopWin(sRutaAplicacion + "galeriaVideos_1-0.php", 580, 450, null, "Galer&iacute;a de Videos");
}

function fnEnviarMensajeUsuario(sUsuario, nId) {
	showPopWin(sRutaAplicacion + "enviarMensaje.php?id=" + nId + "&usuario=" + sUsuario, 350, 300, null, "Enviar Mensajito");
}

function fnEliminarMensaje(nOrigen, nIdMensaje, sUsuario) {
	if ( nOrigen == 1 ) {
		showPopWin(sRutaAplicacion + "eliminarMensaje.php?origen=1&idmensaje=" + nIdMensaje, 380, 110, null, "Eliminar Mensaje");
	} else {
		showPopWin(sRutaAplicacion + "eliminarMensaje.php?origen=2&idmensaje=" + nIdMensaje + "&usuario=" + sUsuario, 380, 110, null, "Eliminar Mensajito");
	}
}

function fnRegistrarse() {
	showPopWin(sRutaAplicacion + "registrarUsuario.php", 460, 400, null, "Registrarse");
}

function fnBuscarUsuarios() {
	showPopWin(sRutaAplicacion + "buscarUsuarios.php", 510, 300, null, "Buscar Usuarios");
}

function fnVerEnLinea() {
	showPopWin(sRutaAplicacion + "usuariosEnLinea.php", 510, 300, null, "Usuarios en L&iacute;nea");
}

function fnAbrirNoticia(nId) {
	document.location.replace(sRutaAplicacion + "noticias.php?id=" + nId);
}

function ingresarNumeros(myfield, e, dec) {
var key;var keychar;

if (window.event)
	key = window.event.keyCode;
	else if (e)
	key = e.which;
else
	return true;
	keychar = String.fromCharCode(key);
if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )	
          return true;
   else if ((("0123456789").indexOf(keychar) > -1))
          return true;
   else if (dec && (keychar == "."))
   {
    myfield.form.elements[dec].focus();
    return false;
   }
   else
   return false;
}

function ingresarDv(myfield, e, dec) {
var key;var keychar;

if (window.event)
	key = window.event.keyCode;
	else if (e)
	key = e.which;
else
	return true;
	keychar = String.fromCharCode(key);
if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )	
          return true;
   else if ((("0123456789kK").indexOf(keychar) > -1))
          return true;
   else if (dec && (keychar == "."))
   {
    myfield.form.elements[dec].focus();
    return false;
   }
   else
   return false;
}

function validarDv(T){var M=0,S=1;for(;T;T=Math.floor(T/10))
S=(S+T%10*(9-M++%6))%11;return S?S-1:'K';}

/*
Función para validar un mail.
Devuelve FALSE si el mail tiene el formato correcto.
Devuelve TRUE si el mail tiene un formato incorrecto o el dominio no exista.
*/
function fnValidarEmail(valor){
    re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor))    {
        return true;
    }else{
        return false;
    }
}

function fnValidarVacio(fld) {
    var error = false;
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = true;
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function fnValidarNombreUsuario(campo) {
    var error = false;
	var caracteres = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ_-.";
	var contador = 0;
	var ubicacion;

	if ( (isNaN(campo.charAt(0) ) == false) || (campo.charAt(0) == '_') || (campo.charAt(0) == '-') ) {
		error = true;
	}

	for ( var i = 0; i < campo.length; i++ ) {
		ubicacion = campo.substring(i, i + 1);
		if ( caracteres.indexOf(ubicacion) != -1 ) {
			contador++
		} else {
			error = true;
		}
	} 
    return error;
}

function fnValidarFecha(nDia, nMes, nAnnio) {
	var error = false;
	var febrero = 0;

	if (fnAnoBisiesto(nAnnio) ) {
		febrero = 29;
	} else {
		febrero = 28;
		if ( nMes == 2 ) {
			if ( nDia > febrero ){
				error = true;
			}
		}
	}

	if ( ( ( nMes == 4 ) || ( nMes == 6) || ( nMes == 9) || ( nMes == 11 ) ) && ( nDia > 30 ) ) {
		error = true;
	}
	return error;
}

function fnAnoBisiesto(nAnno) {
	if ( nAnno % 4 != 0 ) {
		return false;
	} else {
		if ( nAnno % 100 == 0 ) {
			if ( nAnno % 400 == 0 ) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}
}