// JavaScript Document// JavaScript Document

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); p=val.indexOf('.');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' debe contener una dirección de e-mail válida.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' debe contener un número.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' debe contener un número entre '+min+' y '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es requerido.\n'; }
    } if (errors) alert('Los siguientes errores han ocurrido:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

function validarnum(e) {
    tecla = (document.all) ? e.keyCode : e.which;
    if (tecla==8 || tecla==0 || tecla==9 || tecla==13) return true;
	
    patron = /\d/;
    te = String.fromCharCode(tecla);
    return patron.test(te);
} 

function validarletras(e) {
    tecla = (document.all) ? e.keyCode : e.which;
    if (tecla==8 || tecla==0 || tecla==9 || tecla==13) return true;
    patron =/[A-Za-zñÑíáíóúéÁÉÍÓÚ\s]/;
    te = String.fromCharCode(tecla);
    return patron.test(te);
}

function validarletrasnum(e) {
    tecla = (document.all) ? e.keyCode : e.which;
    if (tecla==8 || tecla==0 || tecla==9 || tecla==13) return true;
    patron = /[A-Za-z._-ñÑíáíóúéÁÉÍÓÚ\s\d]/; 
    te = String.fromCharCode(tecla);
    return patron.test(te);
}

function validarcorreo(e) {
    tecla = (document.all) ? e.keyCode : e.which;
    if (tecla==8 || tecla==0 || tecla==9 || tecla==13) return true;
    patron =/[a-z.@-_-\s\d]/;
    te = String.fromCharCode(tecla);
    return patron.test(te);
}

function confirmar(mensaje){
	if (!confirm(mensaje)){
	 	return false;
	}else{
    	return true; 
	}
}

function instag(tag){
	var input = document.form1.contenido;
	//para mac
	if(typeof document.selection != 'undefined' && document.selection) {
		var str = document.selection.createRange().text;
		input.focus();
		var sel = document.selection.createRange();
		if(tag=="br"){
			sel.text = str + "<" +tag+ " />";
		}else{
			sel.text = "<" + tag + ">" + str + "</" +tag+ ">";
		}
		return;
	}else if(typeof input.selectionStart != 'undefined'){
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		if(tag=="br"){
			input.value = input.value.substr(0, start) + insText + '<'+tag+' />'+ input.value.substr(end);
		}else{
			input.value = input.value.substr(0, start) + '<'+tag+'>' + insText + '</'+tag+'>'+ input.value.substr(end);
		}
		return;
	}else{
		  input.value+=' <'+tag+'>Reemplace este texto</'+tag+'>';
		  return;
	}
}

function validarcombos(){
	if (document.form1.categoria.value=="" || document.form1.subcategoria.value==""){
	alert("Para poder hacer el envío, debe seleccionar una categoría y una subcategoría");
	 return false;
	}
	return confirmar('¿Está seguro que desea guardar?');
}

function validarnoticia(mensaje){
	if (document.form1.categoria.value==""){
		alert("Para poder hacer el envío, debe seleccionar una Categoría");
	 	return false;
	}
	if (document.form1.titulo.value==""){
		alert("Para poder hacer el envío, debe insertar un Título!");
	 	return false;
	}
	if (document.form1.subtitulo.value==""){
		alert("Para poder hacer el envío, debe insertar un Subtítulo!");
	 	return false;
	}
	if (document.form1.fecha.value==""){
		alert("Para poder hacer el envío, debe seleccionar una Fecha!");
	 	return false;
	}
	if (document.form1.autor.value==""){
		alert("Para poder hacer el envío, debe insertar un Autor!");
	 	return false;
	}
	
	var editor = CKEDITOR.instances['contenido'].getData().replace(/<[^>]*>/gi, '');
    if (!editor.length){
    	alert("Para poder hacer el envío, debe insertar un Contenido a la Noticia!");
		return false;
    }

	return confirmar(mensaje);
}

function validardocumento(mensaje){
	if (document.form1.documento.value==""){
		alert("Para poder hacer el envío, debe insertar un Archivo!");
	 	return false;
	}
	
	var editor = CKEDITOR.instances['contenido'].getData().replace(/<[^>]*>/gi, '');
    if (!editor.length){
    	alert("Para poder hacer el envío, debe llenar la Descripción!");
		return false;
    }

	return confirmar(mensaje);
}

function validarcontenido(mensaje){

	return confirmar(mensaje);
}

function validarcombos2(){
	if (document.form1.area.value==""){
		alert("Para poder hacer el envío, debe seleccionar un Área de Investigación");
	 	return false;
	}
	if (document.form1.modalidad.value==""){
		alert("Para poder hacer el envío, debe seleccionar una Modalidad!");
	 	return false;
	}
	if (document.form1.recursos.value==""){
		alert("Para poder hacer el envío, debe seleccionar un Recursos!");
	 	return false;
	}
	if (document.form1.archivo.value==""){
		alert("Para poder hacer el envío, debe seleccionar un Archivo!");
	 	return false;
	}
	
	return confirmar('¿Está seguro que desea guardar?');
}

function validarcombos3(){
	if (document.form2.tipo.value==""){
		alert("Para poder hacer el envío, debe seleccionar el Tipo de Participante");
	 	return false;
	}
	if (document.form2.participacion.value==""){
		alert("Para poder hacer el envío, debe seleccionar el Tipo de Participación");
	 	return false;
	}
	if (document.form2.nucleo.disabled==false && document.form2.nucleo.value==""){
		alert("Para poder hacer el envío, debe seleccionar el Núcleo");
	 	return false;
	}
	return confirmar('¿Está seguro que desea guardar?');
}

function mostrarImagenes() {
/** recolocar indica que se recoloque el documento **/
/** si no recolocamos, solo ocultamos las imágenes **/
	document.imagen1.className = "imagen";
   /**document.imagen1.style.visibility = "visible"; **/
  
}

function ocultarImagenes(recolocar) {

/** recolocar indica que se recoloque el documento **/
/** si no recolocamos, solo ocultamos las imágenes **/
	document.imagen1.className = "imagenNO";
    /**document.imagen1.style.visibility = "hidden"; **/
  
}

function borrar(){

	document.form1.buscar.value = "";
	ocultarImagenes();
}
