// ********* Validación *********
var nonull = "Por favor, rellene los campos obligatorios.\r\n\r\n Campo: ";
var noespacios = "Por favor, rellene este campo sin introducir espacios.\r\n\r\n Campo: ";
var entero = "Por favor, introduzca un dato numérico.\r\n\r\n Campo: ";
var fecha  = "Por favor, introduzca una fecha válida con formato dd/mm/aaaa (dia/mes/año). Por ejemplo, 01/04/2002.\r\n\r\n Campo: ";
var email  = "Por favor, introduzca una dirección de correo electrónico correcta.\r\n\r\n Campo: ";
var contiene  = "El contenido del campo no tiene el formato correcto.\r\n\r\n Campo: ";
var tamanio  = "El tamaño de campo no es correcto.\r\n\r\n Campo: ";
var tamanioMAX  = "El tamaño máximo de caracteres es de ";
var tamanioMIN  = "El tamaño mínimo de caracteres es de ";

// condicion: nonull, noespacios, entero, fecha, email, cadena, tamanio, tamanioMAX
buscar=false;
var chkFields = new Array();
function validar(miform)
{
	var temp = document[miform].obligatorios.value.split(",");
	
	for (i=0;i<=temp.length-1;i++)
	{
		var temp2 = temp[i].split("/");
			
			for (b=0;b<=temp2.length-1;b++)
			{
				if(!test(miform,temp2[0],"nonull",temp2[1])) return (false);
			}
		
		
	}
	
}

function test(nombreform, campo, condicion, valor)
{
  
  objeto = eval("document." + nombreform + "." + campo);
  
  
  	
  
    if ( (objeto.tagName == "INPUT" || objeto.tagName == "TEXTAREA") && ( objeto.type == "text" || objeto.type == "password" ))
  {
    if (condicion == "nonull")
    {
      if(objeto.value==""){
        alert(eval(condicion) + valor);
        objeto.focus();
        objeto.select();
        return (false);}
    }
    if (condicion == "noespacios")
    {
      if(objeto.value.indexOf(" ") != -1){
        alert(eval(condicion) + objeto.title.toUpperCase());
        //objeto.focus();
        //objeto.select();
        return (false);}
    }
    if (condicion == "entero")
    {
      if(!IsNumber(objeto.value)){
        alert(eval(condicion) + objeto.title.toUpperCase());
        //objeto.focus();
        //objeto.select();
        return (false);}
    }
    if (condicion == "fecha")
    {
      if(!EsFecha(objeto.value)&&objeto.value!=""){
        alert(eval(condicion) + objeto.title.toUpperCase());
        //objeto.focus();
        //objeto.select();
        return (false);}
    }
    if (condicion == "email")
    {
      if(objeto.value!="" && (objeto.value.indexOf("@") == -1 || objeto.value.indexOf(".") == -1)){
        alert(eval(condicion) + objeto.title.toUpperCase());
        //objeto.focus();
        //objeto.select();
        return (false);}
    }
    if (condicion == "contiene")
    {
      if(objeto.value!=""){
        if(objeto.value.indexOf(valor) == -1 ){
          alert(eval(condicion) + objeto.title.toUpperCase());
          //objeto.focus();
          //objeto.select();
          return (false);}
      }
    }
    if (condicion == "tamanio")
    {
       if(objeto.value.length != valor && objeto.value!=""){
        alert(eval(condicion) + objeto.title.toUpperCase());
        //objeto.focus();
        //objeto.select();
        return (false);}
    }
    if (condicion == "tamanioMAX")
    {
       if(objeto.value.length > valor && objeto.value!=""){
        alert(eval(condicion) + valor + "\r\n\r\nCampo: " + objeto.title.toUpperCase());
        //objeto.focus();
        //objeto.select();
        return (false);}
    }
    if (condicion == "tamanioMIN")
    {
       if(objeto.value.length < valor && objeto.value!=""){
        alert(eval(condicion) + valor + "\r\n\r\nCampo: " + objeto.title.toUpperCase());
        //objeto.focus();
        //objeto.select();
        return (false);}
    }
  }
/*
else if ( objeto.tagName == undefined ) // NO FUNCIONA EN IE6
  {
  
   
    if (condicion == "nonull")
    {
      var check=false;
      for (i=0;i<=objeto.length-1;i++){
        if (objeto(i).checked==true) check = true;}
      if(!check){
        alert(eval(condicion) + valor);
        objeto(0).focus();
        return (false);}
    }
   
  }
 */

  else if ( objeto.tagName == "SELECT")
  {
   if (condicion == "nonull")
    {
      if(objeto.options[objeto.options.selectedIndex].value==""){
        alert(eval(condicion) + valor);
        objeto.focus();
        //objeto.select();
        return (false);}
    }
  }
  

  
  

  else if ( objeto.tagName == "INPUT" && objeto.type  == "checkbox")
  {
    if (condicion == "nonull")
    {
      if(!objeto.checked){
        alert(eval(condicion) + objeto.title.toUpperCase());
        objeto.focus();
        return (false);}
    }
  }
  return(true);
}


function IsNumber(Expression)
{
		Expression = Expression.toLowerCase();
		RefString = "0123456789";

		if (Expression.length < 1)
			return (true);

		for (var i = 0; i < Expression.length; i++)
		{
			var ch = Expression.substr(i, 1);
			var a = RefString.indexOf(ch, 0);
			if (a == -1) return (false);
		}
		return(true);
}

function EsFecha(dateStr)
{
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

  	var matchArray = dateStr.match(datePat)
  	if (matchArray == null)
  		return false

    month = matchArray[3]
    day = matchArray[1]
  	year = matchArray[4]
    if (month < 1 || month > 12)
  		return false

  	if (day < 1 || day > 31)
  		return false

  	if ((month==4 || month==6 || month==9 || month==11) && day==31)
  		return false

  	if (month == 2)
  	{
  		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
  		if (day>29 || (day==29 && !isleap))
  			return false;
  	}
  	return true;
}

function Split(Expression, Delimiter)
{
	var temp = Expression;
	var a, b = 0;
	var array = new Array();

	if (Delimiter.length == 0) {
		array[0] = Expression;
		return (array);	}

	if (Expression.length == '') {
		array[0] = Expression;
		return (array);	}

	Delimiter = Delimiter.charAt(0);

	for (var i = 0; i < Expression.length; i++)	{
		a = temp.indexOf(Delimiter);
		if (a == -1) {
			array[i] = temp;
			break; }
		else {
			b = (b + a) + 1;
			var temp2 = temp.substring(0, a);
			array[i] = temp2;
			temp = Expression.substr(b, Expression.length - temp2.length); }
	}
	return (array);
}











function imgupload(ruta)
{
  window.open('/web_admin/scripts/imageuploader.asp?path=' + ruta ,'imageuploader','width=309,height=485');
}

function fileupload(destdir,formname, camponame, campovalor)
{
   window.open('/web_admin/scripts/upload.asp?TARGET=' + destdir + '&FORMNAME=' + formname + '&CAMPO=' + camponame + '&VALOR=' + campovalor,'imageuploader','width=400,height=180');
}
/*
function cambiaChecks( form )
{
  if ( ! form )
        form = document.formulario
  if ( form ){
  var allcheck = true
  for (i=0;i<=document.formulario.length-1;i++)
    if ( document.formulario.item(i).tagName == "INPUT" && document.formulario.item(i).type == "checkbox" && ! ( document.formulario.item(i).disabled ) && ! document.formulario.item(i).checked )
        allcheck = false
  for (i=0;i<=document.formulario.length-1;i++)
  {
    if ( document.formulario.item(i).tagName == "INPUT" && document.formulario.item(i).type == "checkbox" && ! document.formulario.item(i).disabled )
    {
      if ( allcheck ) document.formulario.item(i).checked=false;
      else document.formulario.item(i).checked=true;
    }
  }
  allcheck = ! allcheck
  }
}
*/

function validarChecksBorrado()
{
    for (i=0;i<=document.formulario.deleteid.length-1;i++) {
        if(document.formulario.deleteid(i).checked) return(true);
    }
    alert("Debe seleccionar al menos un registro.");
    return(false);
}

function cambiaChecks( activar )
{
  form = document.formulario
  for (i=0;i<=document.formulario.length-1;i++)
  {
    if ( document.formulario.item(i).tagName == "INPUT" && document.formulario.item(i).type == "checkbox" && ! document.formulario.item(i).disabled )
    {
      document.formulario.item(i).checked=activar;
      if(activar)
        document.formulario.item(i).parentElement.className='ListadoHeader';
      else
        document.formulario.item(i).parentElement.className='';
    }
  }
}

function controla_condiciones () {
	if (document.Register.aceptar.checked)
		return true;
	else {
		alert ("Para darte de alta en el Club es necesario aceptar las condiciones del alta.");
		return false;
	}
}