// JavaScript Document

function cambiarIdioma(idioma){
	document.getElementById('idioma').value=idioma;	
	document.getElementById('contenido').value=((navigator.appName=='Microsoft Internet Explorer')?document.frames['frame_contenido'].document.location:document.getElementById('frame_contenido').contentDocument.location);
	document.getElementById('form_idioma').submit();
}

/* quita borde punteado de los links --- by Anxo------------------------------------------------------------------------*/
function noplink() {
	lnks=document.getElementsByTagName("a").length;
	for (i=0;i<lnks;i++)
	document.getElementsByTagName("a").item(i).onfocus=new Function(" (this.blur)?this.blur():NULL;")
}

/******************************************************************************************************/

function abrirImagen(img, ancho, alto, root){
	url=root+"mostrar_img.php?img="+img;
	 window.open(url , 'img', 'width='+ancho+', height='+alto+', menubar=0, location=0, resizable=0, scrollbars=0, status=0, toolbar=0');	
}

/******************************************************************************************************/

function autenticarUsuario(){
	if(encriptarContrasenha()){
		if(navigator.appName=='Microsoft Internet Explorer'){doc=document.frames['frame_contenido'].document;}
		else{doc=document.getElementById('frame_contenido').contentDocument;}
		document.getElementById('contenido_actual').value=doc.location; 
		document.getElementById('seccion_usuarios').submit();
	}	
}

/******************************************************************************************************/

function cargarTrabajo(id, imagen, titulo, descripcion, uri){
	if(eval(document.getElementById('id'))){document.getElementById('id').value=id;}
	
	document.getElementById('capa_trabajo').style.display='block';
	document.getElementById('img_trabajo').src=((imagen!='')?imagen:'../imgs/fondo_oferta_anhadir.jpg');
	document.getElementById('img_trabajo').alt=document.getElementById('img_trabajo').title=titulo;
	document.getElementById('img_trabajo').style.display='block';		
	document.getElementById('titulo_trabajo').innerHTML=titulo;
	document.getElementById('descripcion_trabajo').innerHTML=descripcion;
	if(uri==''){
		document.getElementById('uri_trabajo').innerHTML='web';
		document.getElementById('uri_trabajo').style.color='#9c9';
	}else{document.getElementById('uri_trabajo').style.color='#6c0';}
	document.getElementById('uri_trabajo').innerHTML=(uri!='')?uri:'';
	if(document.getElementById('uri_trabajo').href){document.getElementById('uri_trabajo').href=uri;}
}

/******************************************************************************************************/

/***   funciones para obtener la posición X e Y de un objeto de una página   ***/
/***   from http://blog.firetree.net/2005/07/04/javascript-find-position/   ***/
/***   by Peter-Paul Koch and Alex Tingle                                   ***/

  function findPosX(obj) { 
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj) {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
  
/********************************************************************************/  

function cargarDuracion(id, num_anhos){
	// cargamos los valores de los años de duración en el desplegable de duracion
	if(id!=''){campo='curso_'+id;}else{campo='curso';}
	if(eval(document.getElementById(campo))){
	   document.getElementById(campo).length = parseInt(num_anhos)+1 // añadimos una opción más para la 'Libre elección'
	   for(i=0;i<num_anhos;i++){
		  document.getElementById(campo).options[i].value=i+1
		  document.getElementById(campo).options[i].text=i+1
	   }
	   document.getElementById(campo).options[i].value='LE'
	   document.getElementById(campo).options[i].text='Libre elección'
	}
}

/********************************************************************************/  

function cargarProfesoresAsignatura(id_apuntes, id_asignatura, id_seleccionado){
	/* cargamos los valores de los años de duración en el desplegable de duracion
	if(id_apuntes!=''){campo='profesor_'+id_apuntes;}else{campo='profesor';}
	if(eval(document.getElementById(campo))){
	   document.getElementById(campo).length = asignaturas[id_asignatura][1].length;
	   for(i=0;i<document.getElementById(campo).length;i++){
			  document.getElementById(campo).options[i].value=asignaturas[id_asignatura][1][i][0];
			  document.getElementById(campo).options[i].text=asignaturas[id_asignatura][1][i][1];
			  if(document.getElementById(campo).options[i].value==id_seleccionado){ document.getElementById(campo).options[i].selected='selected';}
	   }
	}*/
	document.getElementById('iframe_gestion').src='gestion_apuntes.php?accion=cargar_profes_asignatura&id_asignatura='+id_asignatura+'&id_apuntes='+id_apuntes;
}



/********************************************************************************/  

// Regexp creado por Sandeep V. Tamhankar (stamhankar@hotmail.com)

function emailCheck (emailStr) {

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 parece ser inválida (verifique las @ y .)")
    return false
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) {
    alert("El nombre de usuario parece ser inválido.")
    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
      for (var i=1;i<=4;i++) {
        if (IPArray[i]>255) {
            alert("La dirección IP de destino es inválida!")
        return false
        }
    }
    return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {
    alert("El dominio no parece ser válido.")
    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("Las direcciones deben terminar con dominios de tres letras, o el código de país de dos letras.")
   return false
}

if (len<2) {
   var errStr="Dominio Inválido!";
   alert(errStr)
   return false
}

return true;
} 