/* Este fichero contiene un conjunto de funciones bastante utilizadas para
   la comprobacion de campos en los formularios.*/


function mi_reset()
  { 
       if (confirm("Deseas BORRAR TODOS los datos introducidos?"))     
         {  	       	         
           document.forms[0].reset();
         } 
 } 

function comprueba_email(cadena) 
  {
	/* Comprueba que las direcciones e-mail tengan el siguiente formato:
		cadena@(cadena.cadena)+     er = /\w+@(\w+\.\w+)+/ 
	*/

	    er = /\w+@(\w+(\-\w+)*\.\w+)+/

 	     if (cadena.length==0)
	          { 
    	            /* window.alert ("Tienes que escribir tu E-MAIL !"); */
       	 	return(0);
    	          } 	    
	      else
	      {
 	         OK =er.test(cadena);
	         if (!OK)
	          {
      	            /* window.alert (cadena+ " no es una direccion e-mail válida!"); */
		return(0); 
 	          }
	       }

 }

function campo_vacio(cadena,nombre_campo)
{
	/* Comprueba si en un campo hay algun caracter */
	er=/[\w|\¿|\?|\¡|\!|\(|\)|\:|\;|\.|\,|\<|\>|\-|\+|\*|\=|$|%|&|#|@|\"]/
	OK=er.test(cadena);
	if(!OK)
	{
		/* window.alert(nombre_campo+" NO PUEDE estar VACIO!"); */
		return(0);
	}
}

function comprueba_numero(cadena)
  {
	/* Comprueba que se haya insertado solo numeros en este campo*/
	/* Tienen que haber EXACTAMENTE 9 dígitos, entre 0 y 9 */
	
	numero=/\d{9,}/
	if (cadena.length>9)
	          { 
    	            window.alert (RegExp.input + " no es un número válido !!");
        	 	return(0);
    	          } 	    
	      else
	      {
		OK=numero.test(cadena);
		if (!OK)
		  {
 	    	   window.alert (RegExp.input + " no es un número válido !!");
 		   return(0);
	   	  }
	    }
  }

function comprueba_servidor(cadena)
  {
	/* Comprueba que se haya insertado una cadena con el siguiente formato*/
 	/* cadena . cadena (. cadena)+     NOTA: Sin espacios en blanco   */
	
	er=/\w+(\.\w+)+/
	if (cadena.length==0)
	          { 
    	            window.alert (" Tiener que indicar el Servidor !!");
        	 	return(0);
    	          } 	    
	      else
	      {
		OK=er.test(cadena);
		if (!OK)
		  {
 	    	   window.alert (cadena+ " no es un servidor válido !!");
  		   return(0);
	   	  }
	    }
  }

function lista_seleccion_texto(selectObject)
{
	/* Crea una lista de las opciones que ha seleccionado el usuario en un
	campo SELECT con MULTIPLES opciones, y devuelve un string
	con todas las opciones seleccionadas, separadas por comas. */
	var lista=new Array();

	for (var i=0; i<selectObject.options.length; i++)
	{
		if (selectObject.options[i].selected)
			lista.push(selectObject.options[i].text);			
	}
	
	return lista.join();
}

function lista_seleccion_valor(selectObject)
{
	/* Crea una lista de las opciones que ha seleccionado el usuario en un
	campo SELECT con MULTIPLES opciones, y devuelve un string
	con todas los valores de las opciones seleccionadas, separadas por comas. */
	var lista=new Array();

	for (var i=0; i<selectObject.options.length; i++)
	{
		if (selectObject.options[i].selected)
		{
			lista.push(selectObject.options[i].value);			
		}
	}
	return lista.join();
}


function merda(selectObject)
{
	re=/,/g;
	encabezado="Select DISTINCT(Grupo),Servidor from News, Enlace_news where ";
	seleccion = lista_seleccion_valor(selectObject);
	newstring = seleccion.replace(re, " or Codcateg = ");
	seleccion = "(Codcateg = "+ newstring+") and Enlace_news.Codnews = News.Codnews;";
	return consulta= encabezado + seleccion;
}

function selec_usuarios_por_cat(selectObject1,selectObject2)
{
	re=/,/g;
	encabezado="Select E_mail,General.Codgeneral from General, Enlace_general where ";
	seleccion = lista_seleccion_valor(selectObject1);
	newstring = seleccion.replace(re, " or Codcateg = ");
	seleccion = "(Codcateg = "+ newstring+") and Enlace_general.Codgeneral = General.Codgeneral ";
	lista_idiomas = lista_seleccion_valor(selectObject2);
 
	re2 = /ALL/g;
     todos_idiomas =re2.test(lista_idiomas);
	if (! todos_idiomas)
	{
		newidiomas = lista_idiomas.replace(re, "' or Idioma = '");
		idiomas = " AND ( Idioma='"+newidiomas+"' )";
	}
	else { idiomas = "" }

       consulta= encabezado + seleccion + idiomas +" Group by General.E_mail ;";
	return(consulta);
}


function VerifySpace(cadena){
	text=cadena;
	if(text.indexOf(' ',0)!=-1) {
		/*alert("Escriba una cadena de caracteres sin espacios.");*/
            return(0);
	}
}

