function lanzaMensaje(msg) {
  msg = msg.replace(/xxx/i, "\n");
  alert (msg);
}

var ventana;

function openIT(URL,name,prop) {
  if (ventana) {
    if (ventana.closed) {
      ventana=open(URL,name,prop+",location=no,status=no,directories=no,toolbar=no,resizable=yes,menubar=no");
      ventana.focus();
    }
    else {
      ventana=open(URL,name,prop+",location=no,status=no,directories=no,toolbar=no,resizable=yes,menubar=no");
      ventana.focus();
    }
  }
  else {
      ventana=open(URL,name,prop+",location=no,status=no,directories=no,toolbar=no,resizable=yes,menubar=no");          
      ventana.focus();
  }
}


// Copia el valor de un desplegable a un campo de texto de un mismo formulario.
function copia(f,ori,des) {
  var sel = 0;
  eval('sel=f.'+ori+'.options.selectedIndex');
  if (sel!=0) {
	  eval('f.'+des+'.value=f.'+ori+'.options[sel].text');
	}
}


function sendOperation(op,f) {
  f.op.value = op;
  confirmOperation(f,op);
}

function sendOperation2(op,URL,f) {
  f.action = URL;
  f.op.value = op;
  confirmOperation(f,op);
}

function confirmOperation(f,op) {
  if (confirm('Se dispone a '+op+' registro/s.\n\n¿Desea continuar?')) f.submit();
}

function confirmURL(URL,op) {
  if (confirm('Se dispone a '+op+' registro/s.\n\n¿Desea continuar?')) document.location=URL;
}

function sendAndNoReturn (f) {
  f.volver.value="";
  f.submit();
}

function checkAll(f) {
  for (i=0; i < f.length; i++) {
    e = f.elements[i];
    e.checked = true;
  }
}

function escribirFecha() {
   var nombres_dias = new Array("Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado")
   var nombres_meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre")

   var fecha_actual = new Date()

   dia_mes = fecha_actual.getDate()		
   dia_semana = fecha_actual.getDay()	
   mes = fecha_actual.getMonth() + 1
   anio = fecha_actual.getYear()

   if (anio < 100)				
      anio = '19' + anio
   else if ( ( anio > 100 ) && ( anio < 999 ) ) {
      var cadena_anio = new String(anio)
      anio = '20' + cadena_anio.substring(1,3)
   }

   document.write(nombres_dias[dia_semana] + ", " + dia_mes + " de " + nombres_meses[mes - 1] + " de " + anio + "&nbsp;&nbsp;")
}

function formatearTexto (type,p,f) {
  
  var selected_text = '';
  
  if (window.getSelection)
	{
		var s = f.elements[p].selectionStart;
    var e = f.elements[p].selectionEnd;
    selected_text = f.elements[p].value.substring(s, e);
	}
	else if (document.getSelection)
	{
		selected_text = document.getSelection();
	}
	else if (document.selection)
	{
		selected_text = document.selection.createRange().text;
	
	}
	else return;
	
  if (selected_text.length != 0) {
    
    switch(type) {
      case "negrita":
        ini_tag = '<b>';
        fin_tag = '</b>';
        break;
      case "cursiva":
        ini_tag = '<i>';
        fin_tag = '</i>';
        break;
      case "subraya":
        ini_tag = '<u>';
        fin_tag = '</u>';
        break;
    }
    text = f.elements[p].value;
    ini = text.indexOf(selected_text,0);
    fin = ini + selected_text.length;
    text_length = text.length;
    salida =  text.substring(0,ini) + ini_tag + selected_text + fin_tag + text.substring(fin,text_length);
    f.elements[p].value = salida;
    
  } else {
    alert ("Seleccione texto a formatear");
  }
  
}


//Elimina un parrafo, recogelos parametros del formulario
function eliminarParrafo(f,iParrafo) {
  var idParrafo;
  var imagen, imagenDetalle;
  var tablaDestino, idDestino;
  
  eval ('idParrafo = f.idParrafo'+iParrafo+'.value');
  eval ('imagen = f.parTxtImagen'+iParrafo+'.value');
  eval ('imagenDetalle = f.parTxtImagenDetalle'+iParrafo+'.value');
  eval ('tablaDestino = f.tablaDestino.value');
  eval ('idDestino = f.id.value');
  
  if (confirm('Se dispone a eliminar un párrafo.\n\n¿Desea continuar?')) {
    document.location ='parrafos_op.php?op=eliminar&idParrafo='+idParrafo+'&imagen='+imagen+'&imagenDetalle='+imagenDetalle+'&tablaDestino='+tablaDestino+'&idDestino='+idDestino;
  }
}


//Elimina un parrafo del Contenido, recogelos parametros del formulario
function eliminarParrafoContenido(f,iParrafo) {
  var idParrafo;
  var imagen, imagenDetalle;
  var tablaDestino, idDestino;
  var tablaOrigen, idOrigen;
  
  eval ('idParrafo = f.idParrafo'+iParrafo+'.value');
  eval ('imagen = f.parTxtImagen'+iParrafo+'.value');
  eval ('imagenDetalle = f.parTxtImagenDetalle'+iParrafo+'.value');
  eval ('tablaDestino = f.tablaDestino.value');
  eval ('idDestino = f.id.value');
  eval ('tablaOrigen = f.tablaOrigen.value');
  eval ('idOrigen = f.idOrigen.value');
  
  if (confirm('Se dispone a eliminar un párrafo.\n\n¿Desea continuar?')) {
    document.location ='parrafos_op.php?op=eliminar&idParrafo='+idParrafo+'&imagen='+imagen+'&imagenDetalle='+imagenDetalle+'&tablaDestino='+tablaDestino+'&idDestino='+idDestino+'&tablaOrigen='+tablaOrigen+'&idOrigen='+idOrigen;
  }
}





function reloadAction (newAction, f) {
  f.action=newAction;
  f.submit();
}

function enviar(f) {
  errors = false;
  msg = 'Se han encontrado los siguientes\nerrores en el formulario:\n\n';
  for (i=0; i < f.length; i++) {
    e = f.elements[i];
    
    if ((e.type=="select-one") || (e.type=="select-multiple")) {
      if ((e.value=="-1" || e.value=="") && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
    }
    
    if ((e.type=="text") || (e.type == "textarea")) {
      if (((e.value == null) ||(e.value == "") || esVacio(e.value)) && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
      
      if (e.fecha == "true" && !validaFecha(e.value)) {
        if (e.value != "") {
          msg += '-- Formato de fecha erróneo.\n';
          errors = true;
          continue;
        }
      }
      
      if (e.numerico == "true" && !esNumerico(e.value)) {
        if (e.value != "") {
          msg += '-- '+e.label+' debe ser numérico.\n';
          errors = true;
          continue;
        }
      }      
      
      if (e.nocomilla != "true" && tieneComilla(e.value)) {
        msg += '-- '+e.label+' tiene un carácter no válido.\n';
        errors = true;
        continue;
      }
    }
  }
  
  if (!errors) {
    if (confirm('Se dispone a '+f.label+'.\n\n¿Desea continuar?')) f.submit();
  }
  else alert(msg);
    
}

function enviar2(f) {
  errors = false;
  msg = 'Se han encontrado los siguientes\nerrores en el formulario:\n\n';
  for (i=0; i < f.length; i++) {
    e = f.elements[i];
    
    if ((e.type=="select-one") || (e.type=="select-multiple")) {
      if ((e.value=="-1" || e.value=="") && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
    }
    
    if ((e.type=="text") || (e.type == "textarea")) {
      if (((e.value == null) ||(e.value == "") || esVacio(e.value)) && (e.optional=="false")) {
        msg += '-- '+e.label+' es un campo obligatorio.\n';
        errors = true;
        continue;
      }
      
      if (e.fecha == "true" && !validaFecha(e.value)) {
        if (e.value != "") {
          msg += '-- Formato de fecha erróneo.\n';
          errors = true;
          continue;
        }
      }
      
      if (e.numerico == "true" && !esNumerico(e.value)) {
        if (e.value != "") {
          msg += '-- '+e.label+' debe ser numérico.\n';
          errors = true;
          continue;
        }
      }      
      
      if (e.nocomilla != "true" && tieneComilla(e.value)) {
        msg += '-- '+e.label+' tiene un carácter no válido.\n';
        errors = true;
        continue;
      }
    }
  }
  
  if (!errors) {
    f.submit();
  }
  else alert(msg);
    
}

function emutation_ToObject(id) {
		if(document.layers){
			return (document.layers[id])?eval(document.layers[id]):null;
		}
		else if(document.all && !document.getElementById){
			return (eval("window."+id))?eval("window."+id):null;
		}
		else if(document.getElementById && document.body.style) {
			return (document.getElementById(id))?eval(document.getElementById(id)):null;
		}
	}
  
function emutation_FlashWrite(miEMswf, miEMcreativewidth, miEMcreativeheight, miEMwmode, miEMbgcolor, miID){
	var emswf = miEMswf; 
	var emcreativewidth = miEMcreativewidth;
	var emcreativeheight = miEMcreativeheight;
	var emwmode = miEMwmode;
	var embgcolor = miEMbgcolor;
	
	ntcode = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width='"+ emcreativewidth +"' height='"+ emcreativeheight +"' id='"+ miID +"'>";
	ntcode +="<param name='movie' value='" + emswf + "'>";
	ntcode +="<param name='quality' value='high'>";
	ntcode +="<param name='bgcolor' value='#" + embgcolor +"'>";
	ntcode +="<param name='wmode' value='"+ emwmode +"'>";
	ntcode +="<param name='menu' value='false'>";
	ntcode +="<embed src='" + emswf + "' quality='high' wmode='" + emwmode + "' width='" + emcreativewidth + "' height='" + emcreativeheight + "' bgcolor='#" + embgcolor +"' type='application/x-shockwave-flash' menu='false' name='"+ miID +"' swLiveConnect='true'></embed></object>";
  	document.write(ntcode);
}

function emutation_FlashInnerHTML(htmlElementId,code){
  var x=emutation_ToObject(htmlElementId);
  if(x){
    if(document.getElementById||document.all){
      x.innerHTML='';
      x.innerHTML=code;
      }
    else if(document.layers){
      x.document.open();
      x.document.write(code);
      x.document.close();
      }
    }
  }
  
var cal = new Array(); 

 
function fechasNacimiento(iddestino,idhijos){
    
 var x=document.getElementById(idhijos).value;   
   
 if(x<21){  
   
 var resultado;
 
 document.getElementById(iddestino).innerHTML="";
 
  for (i=1; i <=x; i++) {
      
      resultado="";
                  
      resultado+="<div style='padding-bottom:4px;'>";
      resultado+="<span><label for='txtFechaNac"+i+"'>Fecha Nac. "+i+"</label></span>";
	  resultado+="<input name='txtFechaNac[]' value ='' type='text' id='txtFechaNac"+i+"' readonly='readonly' />";
	  resultado+="<a href='javascript:cal["+i+"].popup();' style='background-image:none;padding:0 0 0 5px;'><img src='img/cal.gif' style='border:0;height: 13px;' alt='Pincha aqu&iacute; para seleccionar fecha'/></a>";
	  resultado+="</div>";
	  
	  
	  document.getElementById(iddestino).innerHTML+=resultado;
   
   
         
  }
  
  for (i=1; i <=x; i++) {
      
         cal[i] = new calendar1(document.forms['formNewUser'].elements['txtFechaNac'+i]);
         cal[i].year_scroll = true;
         cal[i].time_comp = false;
         cal[i].ant = true;
  }
  
 }else{
     
    alert("Máximo 20");   
 }
    
}

function buscaCadena(cadena, subcadena){

	if (/,/.test("hay comas -,-"))
		alert("no hay comas")
	else
		alert("hay comas")
}

function checkEmail(email) { // funciton checkEmail, para comprobar el email
				  if(email.indexOf('@')==-1 || email.indexOf('.')==-1 || email.indexOf(' ')!=-1) return true;
				  dominio=email.substring(email.indexOf('@')+1);
				  if(dominio.indexOf('@')!=-1 || dominio.indexOf('.')==-1 || dominio.lastIndexOf('.')==(dominio.length - 1)) return true;
				  return false;
} // del function checkEmail


function valEmail(valor){
    re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor)){
        return false;
    }else{
        return true;
    }
}
