/*
Funciones comunes a toda la aplicación
*/
 
function f_borrar(){
  if (confirm("¿Está seguro que desea eliminar la información de la base de datos?")){
    document.forms[0].accion.value = "3"
    document.forms[0].submit()
  }
}

function f_submit(){
  document.forms[0].submit()
}

function f_submit_Amp(campos,nombres){
  var array_campos = campos.split(",");
  var array_nombres = nombres.split(",");
  submita = true;
  for (i=0;i<array_campos.length;i++){
    if (eval("document.forms[0]." + array_campos[i] + ".value==''")){
      alert("Por favor rellene el campo " + array_nombres[i]);
      document.forms[0].elements[array_campos[i]].focus();
      submita=false;
      break;
    }
  }
  if (submita){
    document.forms[0].submit()
  }
}

function f_submit_Amp_v2(formu,campos,nombres){
  var array_campos = campos.split(",");
  var array_nombres = nombres.split(",");
  submita = true;
  for (i=0;i<array_campos.length;i++){
    if (eval("document.forms['" + formu + "']." + array_campos[i] + ".value==''")){
      alert("Por favor rellene el campo " + array_nombres[i]);
      submita=false;
      document.forms[formu].elements[array_campos[i]].focus();
      break;
    }
  }
  if (submita){
    document.forms[formu].submit()
  }
}

function vercapa(capa){
  document.getElementById(capa).style.visibility='visible'
}

function inicio(idform){
  var sForm = document.getElementById(idform);
  sForm.onsubmit = submitar;
}


function insertAtCursor(myField, ind) {
  var valores = new Array("[-SLn-]","[-N-]","[-/N-]","[-I-]","[-/I-]")
  if (document.all) { //IE
    myField.focus();
    sel = document.selection.createRange();
    sel.text = valores[ind];
  }
  else if (myField.selectionStart || myField.selectionStart == '0') { //MOZILLA/NETSCAPE
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos)
    + valores[ind]
    + myField.value.substring(endPos, myField.value.length);
    } else {
      myField.value += valores[ind];
    }
}

function addfavoritos(){
  if (document.all){
    window.external.AddFavorite('http://www.videoAcces.com','VideoAcces')
  }else{
    window.sidebar.addPanel("Centro de estudios Delta", "http://www.centroestudiosdelta.com", "")
  }
}

/*Funciones para la visualización de imágenes*/
var longvector = 4
var Lista_Fotos=new Array(longvector);
var index = 0;
var vecFoto = new Array("../gifs/aula03.jpg","../gifs/recepcion01.jpg","../gifs/aula05.jpg","../gifs/aula06.jpg");
var vecTit = new Array("Aula1","Recepción","Aula2","Aula3");

function sumaIndex(){
 if (index<longvector-1){
  index= parseInt(index +1)
 }else{
  index=0
 }
}

function restaIndex(){
  if (index>0){
  index= parseInt(index -1)
 }else{
  index=5
 }
}

function cargaIndex(){
  index=parseInt(document.location.hash.replace("#",""))
  cargaFoto()
}

function cargaFoto(){
//alert(index)
  if (index=="" || isNaN(index)) index=0
  for (i=0; i<=Lista_Fotos.length-1; i++)  // Se cargan las imagenes en una tabla
    {
    Lista_Fotos[i]=new Image();
    Lista_Fotos[i].src=vecFoto[i];
    //alert(vecFoto[i])
    }
  if (document.getElementById) {
  document.getElementById('foto').src = Lista_Fotos[index].src;
  //alert(document.getElementById('foto').src)
  var newText=document.createTextNode(vecTit[index])
  borrarnodos('desc')
  //document.getElementById('desc').childNodes[0].nodeValue = newText
  document.getElementById('desc').appendChild(newText);
 }
}

function borrarnodos(div){
var d2=document.getElementById(div);
	// -- d2Len holds the fixed starting value of the number of childNodes
var d2Len=d2.childNodes.length;  

  for (i=0;i<d2Len;i++){
       d2.removeChild(d2.childNodes[0]);
  }
}


//***************************************************************************************************************************************************************
// Función que comprueba los campos del formulario
// Es necesario definir en las páginas que continen el formulario los vectores con los nombres de los campos, los tipos y las alertas (veccampos, vectiposcampos, vecalerta)
//***************************************************************************************************************************************************************
function compruebacampos(formu){
	paso=true
	oForm = document.forms[formu]
	for (i=0;i<veccampos.length;i++){
		switch (vectiposcampos[i]){
		  case 'texto':
			paso=!oForm.elements[veccampos[i]].value==""
			break;
		  case 'tel':
			paso=!(isNaN(oForm.elements[veccampos[i]].value) || oForm.elements[veccampos[i]].value.length==0)
			break;
		  case 'mail':
			paso=checkEmail(oForm.elements[veccampos[i]].value)
			break;			
		  case 'fecha':
			paso=isDate(oForm.elements[veccampos[i]].value,false)
			break;
		  case 'nif':
			paso=isNif(oForm.elements[veccampos[i]].value,false)
			break;
		  case 'combo':
			paso=!oForm.elements[veccampos[i]].selectedIndex<=0
			break;
		  case 'check':
		  	paso=checkRadio(veccampos[i])
			break;
		  }
		  
		 if (!paso){
		 	alert(vecalerta[i]);
			oForm.elements[veccampos[i]].focus();
		 	break;
		 }
	}
	
	if (paso){ 
    oForm.submit()
  } 
}

//*************************************************************************************************
// Función que chekea el mail
//*************************************************************************************************
function checkEmail(email){

   var isEmail = email.match(/^\w+(-\w+)*(\.\w+(-\w+)*)*@\w+(-\w+)*(\.\w+(-\w+)*)*\.([a-z]{3}|[a-z]{2})$/);

   if (!isEmail) {
      //La dirección de correo no es válida
      return false;
   }else{
      //La dirección de correo es correcta
      return true;
   }

}

//***************************************************************************************************************
// Función que comprueba los campos de fecha
//***************************************************************************************************************
function isDate(fecha,bAllowEmpty){
 var sMensaje = true;
 var ddTopeSup = 30;
 var dd,mm,aa;

 if (fecha.replace(/(\s)/gi,'') == ''){
    sMensaje = bAllowEmpty;
 }else{
   var nFirstBar = fecha.indexOf('/');
   var nLastBar = fecha.lastIndexOf('/');

   if(((nFirstBar > -1) && (nLastBar > -1))){
    dd = fecha.substr(0,nFirstBar);
    //alert(dd);
    mm = fecha.substr(nFirstBar + 1,(nLastBar  - nFirstBar) - 1);
    //alert(mm);
    aa = fecha.substr(nLastBar + 1,4);
    //alert(aa);
   }

   if (aa < 1 || isNaN(Number(aa))) {
     sMensaje = false;
   }
   if ((mm < 1) || (mm > 12) || isNaN(Number(mm))){
     sMensaje = false;
   }else{
     if (mm == 2){
       ddTopeSup = 28;
       if (((aa % 400) == 0) || ((aa % 4) == 0 && (aa % 100) != 0)){
         ddTopeSup = 29;
       }
     }else{
       if ((mm == 1) || (mm == 3) || (mm == 5) || (mm == 7) || (mm == 8) || (mm == 10) || (mm == 12))
         ddTopeSup = 31;
     }
   }
   if ((dd < 1) || (dd > ddTopeSup) || isNaN(Number(dd))){
     sMensaje = false;
   }
 }
 return sMensaje;
} 

function oculta(capa){

  document.getElementById(capa).style.visibility='hidden'

}

function  muestra(capa){

  document.getElementById(capa).style.visibility='visible'

}
function desplegar(id){
  if(document.getElementById('dentrofamilia'+id).style.display=='block')
	document.getElementById('dentrofamilia'+id).style.display='none'
  else
  	document.getElementById('dentrofamilia'+id).style.display='block'

}
