//Funcion para invocar una ventana flotante cuadno carga una pagina
//La forma correcta de invocar este popup es asì
//popup('/boletin/comisiones/comision.html','boletin_comision',770,550)
function popup(url,nombre,ancho,alto) 
{
	nWidth = ancho;
	nHeight = alto;
	LeftPosition = (screen.width) ? (screen.width - nWidth)/2  : 0;
	TopPosition = (screen.height) ? (screen.height - nWidth)/2 : 0;
	settings = 'height=' + nHeight + ',width=' + nWidth + ',top=' + TopPosition + ',left=' + LeftPosition + ',bar=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0';
	window.open(url, nombre, settings);
}

//Funcion para invocar una ventana flotante cuadno carga una pagina sin ningun scroll, recomendado para mostrar solo imagenes
//La forma correcta de invocar este popup es asì
//popup('/boletin/comisiones/comision.html','boletin_comision',770,550)
function popup_snbarras(url,nombre,ancho,alto) 
{
	nWidth = ancho;
	nHeight = alto;
	LeftPosition = (screen.width) ? (screen.width - nWidth)/2  : 0;
	TopPosition = (screen.height) ? (screen.height - nWidth)/2 : 0;
	settings = 'height=' + nHeight + ',width=' + nWidth + ',top=' + TopPosition + ',left=' + LeftPosition + ',bar=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0';
	window.open(url, nombre, settings);
}

//funcion para insertar un swf a una página, para que no genere conflictos con el javascript flotante
function cargarSWF(direccion,ancho,alto)
{ 
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+ancho+'" height="'+alto+'">'); 
    document.write('<param name="movie" value="'+direccion+'" /><param name="quality" value="high"><param name="menu" value="false" /><param name="wmode" value="transparent" /><embed src="'+direccion+'" quality="high" wmode=transparent pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+ancho+'" height="'+alto+'"></embed><noembed> <div align="center">Su navegador no dipone de una versión apropiada de flash player. <br> Puede obtenerla gratuitamente <a href="http://www.adobe.com/es/products/flashplayer/">aqui </a> </div> </noembed></object>'); 
} 

//para ver capas
function verCapa(id1,id2,id3,opcion)
{	
	if (opcion == 0)
	{
		document.getElementById(id1).style.display = "block";
		document.getElementById(id2).style.display = "none";
		document.getElementById(id3).style.display = "none";
	}
	else
	{
		document.getElementById(id1).style.display = "block";
		document.getElementById(id2).style.display = "block";
		document.getElementById(id3).style.display = "none";
	}
	
}

function ver_capa(id)
{
	document.getElementById(id).style.display = "block";
}
	function ocultar_capa(id)
{	
	document.getElementById(id).style.display = "none";	
}

//abrir una capa con check
function verCapa_display(form,nombreCapa,check)
{
		if(document[form][check].checked == true)
		{
			document.getElementById(nombreCapa).style.display = "block";
		}
		else
		{
			document.getElementById(nombreCapa).style.display = "none";
		}
}

//funcion para direccionar los eventos
function gotoEvent(dia,mes,anio,busd)
{
	pagina = '/eventos-academicos/eventos.php?d='+dia+'&m='+mes+'&a='+anio+'&b='+busd;
	document.location.href=pagina;
}

//funcion para abrir ventanas pequeñas al estilo popup
function abrir_ventana(enlace,ancho,alto,nombre_ventana)
{
   nuevaVentana = window.open (enlace,nombre_ventana,"height="+alto+",width="+ancho+", resizable=NO,noresize=no,toolbar=0,location=0,directories=0, status=NO, menubar=0,scrollbars=1")
}

//funcion para abrir ventanas pequeñas al estilo popup pero sin scrolls, recomendado para imagenes
function abrir_ventana_snsc(enlace,ancho,alto,nombre_ventana)
{
   nuevaVentana = window.open (enlace,nombre_ventana,"height="+alto+",width="+ancho+", resizable=NO,noresize=no,toolbar=0,location=0,directories=0, status=NO, menubar=0,scrollbars=0")
}

//Para comprobar que un radio este seleccionado
function validarBotonRadio(form,radio,mensaje) 
{

	var marcado = "no";
	with (document[form])
	{		
		for ( var i = 0; i < document[form][radio].length; i++ ) 
		{			
			if ( document[form][radio][i].checked ) 
			{	
				return true;				
			}
		}		
		if ( marcado == "no" )
		{
						
		window.alert("Debe marcar " + mensaje );
		return false;
		}
	}
}

//Para comprobar que un radio este seleccionado
function validarBotonCheck(form,arraycheck,n,mensaje) 
{	
	var i = 0;
	var marcado = "no";
	
	while (i<n)
	{		
		if ( document[form][arraycheck+'['+i+']'].checked ) 
			{			
				return true;				
			}
		i++;	
	}	
			
	if ( marcado == "no" )
	{
		window.alert("Debe elegir " + mensaje );
		return false;
	}
	
}

//Para validar un campo vacio
function tf_vacio_str(form,tf,str) 
{   
           
        if( vacio(document[form][tf].value) == false ) 
		{   
                alert("Introduzca "+str);
				document[form][tf].focus();
                return false   
        } 
		else return true;   
           
} 
function vacio(q) {   
        for ( i = 0; i < q.length; i++ ) {   
                if ( q.charAt(i) != " " ) {   
                        return true   
                }   
        }   
        return false   
}

//El campo de texto que habilita el check no quede vacio
function tf_vacio_check(form,check,tf,str)
{
	
	if ( document[form][check].checked )
	{		
		return tf_vacio_str(form,tf,str);	
	}
}

//EL radio que habilita un campo y validamos que se ingrese texto en ese campo
//form, el nombre del formulario. radio, el nombre del radiobutton. i, el indice del radio que habilita el campo de texto. tf, el nombre del campo de texto. str, el mensaje de alerta en caso de no estar el campo lleno.
function tf_vacio_radio(form,radio,i,tf,str)
{		
	if ( document[form][radio][i].checked)
	{		
		return tf_vacio_str(form,tf,str);	
	}
}

//campo de texto acepta solo numeros
function solo_numeros(form,tf)
{
	var checkOK = "0123456789";

  	var checkStr = document[form][tf].value;	
	var allValid = true; 
  	for (i = 0; i < checkStr.length; i++) 
	{
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++)
		  if (ch == checkOK.charAt(j))
			break;
		
		if (j == checkOK.length) 
		{ 
		  allValid = false; 
		  break; 
    	}
  	}
	
	if (!allValid) 
	{ 
		alert("Escriba sólo números"); 
		document[form][tf].focus(); 
		return (false); 
  	}	
	else return true;
}


function solo_letras(form,tf)
{
	var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyzAEIOUÁÉÍÓÚaeiouáéíóú' ";
  	var checkStr = document[form][tf].value;	
	var allValid = true; 
  	for (i = 0; i < checkStr.length; i++) 
	{
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++)
		  if (ch == checkOK.charAt(j))
			break;
		
		if (j == checkOK.length) 
		{ 
		  allValid = false; 
		  break; 
    	}
  	}
		if (!allValid) { 
		alert("Escriba sólo caracteres válidos"); 
		document[form][tf].focus(); 
		allValid = false; 
  	} 	
	return allValid;
}


//El ancho de un campo
function ancho_campo(form,tf,ancho)
{
	if (document[form][tf].value.length != ancho)
	{
		alert ("Son "+ancho+" dígitos");
		document[form][tf].focus();
		return false;
	}
	else return true;
}

//ir al URL
function gotopage(url)
{
	if(confirm('¿Seguro que deseas continuar?'))
	{
		document.location.href=url;
	}; 
}

//verificar el mail correcto
function emailCheck (form,tf) 
{	
	var emailStr = document[form][tf].value
	var emailPat=/^(.+)@(.+)$/	
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"	
	var validChars="\[^\\s" + specialChars + "\]"	
	var quotedUser="(\"[^\"]*\")"	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/	
	var atom=validChars + '+'	
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")	
	var matchArray=emailStr.match(emailPat)
	
	if (matchArray==null) 
	{	
		alert("La dirección de correo no es válida! \n" +"    debe escribir  el símbolo '@' ");
		document[form][tf].focus();
		return false;
	}
	
	var user=matchArray[1]
	var domain=matchArray[2]	
	
	if (user.match(userPat)==null) 
	{
		alert("!La dirección de correo no es válida!  \n" +"debe escribir correctamente el dominio, ejm: cip.org.pe");
		document[form][tf].focus();
		return false;
	}
	
	var IPArray=domain.match(ipDomainPat)
	
	if (IPArray!=null) 
	{	
		  for (var i=1;i<=4;i++) 
		  {
			if (IPArray[i]>255) 
			{
				alert("IP de destino incorrecta.");
				document[form][tf].focus();
			return false;
			}
		}
		return true
	}

	var domainArray=domain.match(domainPat)
	
	if (domainArray==null) 
	{
		alert("! La dirección de correo no es válida!  \n" +"debe escribir correctamente el dominio, ejm: cip.org.pe");
		document[form][tf].focus();
		return false;
	}	
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) 
	{	
	   alert("!La dirección de correo no es válida!  \n" +"debe escribir correctamente el dominio, ejm: cip.org.pe");
	   document[form][tf].focus();
	   return false;
	}	
	
	if (len<2) {
	   var errStr="¡A esta dirección le falta un nombre de host!";
	   alert(errStr);
	   document[form][tf].focus();
	   return false
	}	
	
	var checkOK = "0123456789.qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM@-_";

  	var checkStr = document[form][tf].value;	
	var allValid = true; 
  	for (i = 0; i < checkStr.length; i++) 
	{
		ch = checkStr.charAt(i); 
		for (j = 0; j < checkOK.length; j++)
		  if (ch == checkOK.charAt(j))
			break;
		
		if (j == checkOK.length) 
		{ 
		  allValid = false; 
		  break; 
    	}
  	}
		if (!allValid) { 
		alert("Hay un caracter no permitido, por favor verifique"); 
		docuemnt[form][tf].focus(); 
		return (false); 
  	}
	
	return true;
}