// JavaScript Document

function ver_imagen(imagen,w,h) {
    var w = 550;
    var h = 320;
    
    var x = screen.availWidth/2-w/2;
    var y = screen.availHeight/2-h/2;
    
    var opcionesPopup= "height=" + h + ",innerHeight=" + h + ",width=" + w + ",innerWidth=" + w;
    opcionesPopup += ",left=" + x + ",screenX=" + x + ",top=" + y + ",screenY=" + y;
    opcionesPopup += ",status=no,toolbar=no,menubar=no,location=no,resizable=no,scrollbars=no";

    window.open(imagen,"img",opcionesPopup)
}

function edad(fnac) {
    var sdate = fnac.split("/")
    var fnac = new Date(Math.abs(sdate[2]),(Math.abs(sdate[1])-1),Math.abs(sdate[0]))
    var hoy = new Date();

	var anos = hoy.getFullYear() - fnac.getFullYear()
	if ( hoy.getMonth() < fnac.getMonth() || 
		 ( hoy.getMonth() == fnac.getMonth() && hoy.getDate() < fnac.getDate() ) ) {
		anos -= 1
	} 
	return anos;    
}

// nos da el value del radio seleccionado o false si no hay ninguno seleccionado
function valorSeleccionado(ar_valores) {

	if ( ar_valores.length ) {
	
		for(var i=0; i < ar_valores.length; i++) {
			if ( ar_valores[i].checked ) {
				return ar_valores[i].value;
			}
		}

	// Si sólo hay un elemento
	} else {
		if ( ar_valores.checked ) {
			return ar_valores.value;
		}
	}
	
	return false;
}

/**
*	Chequea si la fecha es válida segun el formato dd/mm/yyyy
*
*	@param		string		indate		Fecha a comprobar
*
*	@return		boolean					True  -> fecha correcta
*										False -> fecha incorrecta
*/
function es_fecha(indate) {
	
	if ( indate.length != 10 ) return false
	
    var sdate = indate.split("/")
    var chkDate = new Date(Math.abs(sdate[2]),(Math.abs(sdate[1])-1),Math.abs(sdate[0]))
    var cmpDate = (chkDate.getDate())+"/"+(chkDate.getMonth()+1)+"/"+(chkDate.getFullYear())
    var indate2 = (Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]))
    if (indate2 != cmpDate || cmpDate == "NaN/NaN/NaN") return false
    else return true;
}

/**
*  Válida la sintaxis del email
*
*  Comprobaciones :
*    - Mínimo de 5 caracteres
*    - Caracteres no permitidos : ;:[](){}=+<>\/&*¿?¡!%$#|`´', ñçàèìòùáéíóúâêîôûäëïöüÑÇÀÈÌÒÙÁÉÍÓÚÂÊÎÔÛÄËÏÖÜ"
*    - Que la @ tenga algún caracter delante y alguno detrás
*    - Que exista '.' a partir del cuarto carácter (x@x.x)
*    - Que no acabe en '.'
*    - Que el punto esté detrás de la @
*/
function validamail(email){

	// Mínimo de 5 caracteres
	if (email.length < 5)
		return false
		
	// Cadena de caracteres no permitidos
	var iChars = ";:[](){}=+<>\/&*¿?¡!%$#|`´', ñçàèìòùáéíóúâêîôûäëïöüÑÇÀÈÌÒÙÁÉÍÓÚÂÊÎÔÛÄËÏÖÜ\"";	
	
	// Primero comprobamos que en el email no haya algún 
	// caracter no permitido
	var eLength = email.length;	
	for (var i=0; i < eLength; i++)	{		
		if (iChars.indexOf(email.charAt(i)) != -1)
			return false
	}	
	
	// Comprobamos que la @ tenga algún caracter delante y alguno detrás
	var atIndex = email.lastIndexOf("@");	
	if(atIndex < 1 || (atIndex == eLength - 1))
		return false

	// Comprobamos que exista '.' a partir del cuarto carácter, pero
	// que no acabé en '.'
	var pIndex = email.lastIndexOf(".");	
	if(pIndex < 3 || (pIndex == eLength - 1))	
		return false;	

	// Por último, comprobamos que el punto esté detrás de la @
	if(atIndex > pIndex)	
		return false	

	return true
}

// Comprueba si el texto que se le pasa contiene alguno de los caracteres que también se le pasan
function contieneCaracteres(texto,caracteres) {

	var eLength = texto.length;	
	for (var i=0; i < texto.length; i++)	{		
		if (caracteres.indexOf(texto.charAt(i)) != -1)
			return true
	}
	return false
}


function valida_nif (numero, letra) {
	
    letras = new Array("T","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E");
    num = Number(numero);
    
    if(num<23) indice = num;
	else indice = numero-(parseInt(num/23)*23);
	
    if (letra.toUpperCase() != letras[indice]) {
        return false;
    }
    return true;
}
