//onsubmit="return validar('document.agenda','Nom','Nom','','Lloc','Lloc','','DataInici','Data inici','di','DataFi','Data fi','df')"

function validar(){
    var color2='#FFFDF5'
    var color='#F7FBFD'
	var args=validar.arguments;
	var numargs=args.length;
	var envia,missatge,tipus
	var incorrecte=''
	for (i=1; i<(numargs-1); i+=3)
	{
		if (numargs==3)
		{
			obj=args[0];
			envia=false;
			missatge=args[1]
			tipus=args[2]
		}
		else 
		{
			obj=eval(args[0] + '.' + args[i])
			envia=true;
			(!args[i+1])?nomMostrar=args[i]:nomMostrar=args[i+1];
			missatge='"' + nomMostrar + '"'			
			tipus=args[i+2]
		}

		if ((tipus == 'doc') || (tipus == 'img'))
		{
			
			if (tipus == 'doc')
				ext = ['doc', 'pdf']
			else
				ext = ['gif', 'jpg']

			elem=obj.value;
			tamanio = elem.length;
			tmp = elem.substr(tamanio-3);
			tmp = tmp.toLowerCase();
			typevalid = false
			
			if (elem != '')
			{
				for(cont = 0; cont < ext.length && !typevalid; cont++)
				{
					if (tmp == ext[cont])
					{		
						typevalid = true
						obj.style.backgroundColor=color
					}
				}
			
				if (!typevalid)
				{
					if (!incorrecte){obj.focus()};
					incorrecte+= missatge + ' ce n\'est pas un type de dossier correct.\n'
					obj.style.backgroundColor=color2		
				}
				else
					obj.style.backgroundColor=color
			 }
			 else
				obj.style.backgroundColor=color
		  }

		if (tipus == 'ext')
		{
			ext = args[i+3].split("-"); 
			i = i + 1;
			elem=obj.value;
			tamanio = elem.length;
			tmp = elem.substr(tamanio-3);
			tmp = tmp.toLowerCase();
			typevalid = false
			
			if (elem != '')
			{
				for(cont = 0; cont < ext.length; cont++)
				{
					if (tmp == ext[cont])
					{		
						typevalid = true
						obj.style.backgroundColor=color
					}
				}
				
				if (!typevalid)
				{
					if (!incorrecte){obj.focus()};
					incorrecte+= missatge + ' ce n\'est pas un type de dossier correct.\n'
					obj.style.backgroundColor=color2		
				}
				else
					obj.style.backgroundColor=color
			}
			else
				obj.style.backgroundColor=color
		}

        // obligatori
        if ((tipus=='') || (tipus=='no') || (tipus=='mo')){
				var seleccionat=true;
				if (obj.options)
				{
					if(obj.selectedIndex==0) {seleccionat=false} else {seleccionat=true};
				}
				if ((obj.value=="") || (!seleccionat))
				{
					if (!incorrecte){obj.focus()};
					incorrecte+=missatge + ' il est obligatoire\n'
					obj.style.backgroundColor=color2
					//obj.focus()
				}
				else
				{		
					obj.style.backgroundColor=color
					if (tipus=='no'){tipus='n'};
					if (tipus=='mo'){tipus='m'};
				}
		}
		// mail
        if ((tipus=='m')  && (obj.value!='')){
		mail=obj.value
		arrob=mail.indexOf('@')
		punt=mail.indexOf('.',arrob+2)
		ultim=mail.length-1		
			if ((arrob==-1) || (punt==-1) || (arrob==0) || (punt==ultim))
			{
				if (!incorrecte){obj.focus()};
				incorrecte+= missatge + ' ce n\'est pas une direction d\'email de correcte\n'
				obj.style.backgroundColor=color2						
			}
			else
			{		
				obj.style.backgroundColor=color
			}
		}
		// nombre 
        if(tipus=='n'){

		//	if ((obj.value!=Number(obj.value)) && (obj.value!=''))
			if (( obj.value!=Number(obj.value) ) && (obj.value!=''))
				{
					if (!incorrecte){obj.focus()};
					incorrecte+= missatge + ' il doit être une valeur numérique\n'
					obj.style.backgroundColor=color2			
				}	
			else
				{
				obj.style.backgroundColor=color
				} 
		
			
			if (obj.value==''){obj.value='0'}
		}
		
		// EURO 
        if ( (tipus=='e') ){
			valor=obj.value
			coma=valor.indexOf(',')
			// Comprova en cas que hi hagi "coma" (Ex.: 44,78)
			if ( (coma!=-1) && (obj.value!='')) 
			{
				posComa=valor.search(',')
				valorIni=valor.substr(0,posComa)
				valorFi=valor.substr(coma+1,valor.length)
				if ( (valorIni!=Number(valorIni)) || (valorFi!=Number(valorFi)) )
				{
					if (!incorrecte){obj.focus()};
					incorrecte+= missatge + ' il doit être une valeur en EURO\n'
					obj.style.backgroundColor=color2			
				}	
				else
				{
					obj.style.backgroundColor=color
				} 
			}
			// Comprova en cas que sigui número enter (Ex.: 450)
			else
			{
				if (( obj.value!=Number(obj.value) ) && (obj.value!=''))
				{
					if (!incorrecte){obj.focus()};
					incorrecte+= missatge + ' il doit être une valeur en EURO\n'
					obj.style.backgroundColor=color2			
				}	
				else
				{
					obj.style.backgroundColor=color
				} 
			}
			if (obj.value==''){obj.value='0'}
		}
		// cp
		if (tipus=='cp')
		{
			if ((obj.value!=Number(obj.value)) && (obj.value!='')  || (obj.value<10000))
			{			
				if (!incorrecte){obj.focus()};
				incorrecte+= missatge + ' il doit être une valeur numérique de 5 digits.\n'
				obj.style.backgroundColor=color2				
			}
			else
			{		
				obj.style.backgroundColor=color
			}
		}
		
		// llargada maxima 
        if(tipus>1){
			if (obj.value.length > tipus)
			{
				if (!incorrecte){obj.focus()};
				incorrecte+= missatge + ' il contient ' + obj.value.length + ' des caractères, ne peut pas avoir plus de ' + tipus + ' \n'
				obj.style.backgroundColor=color2				
			}
			else
			{		
				obj.style.backgroundColor=color
			}
		}



        if(tipus=='data'){
        var tmp=obj.value
			if (!validaData(obj) && (tmp!=''))
				{
					if (!incorrecte){obj.focus()};
					incorrecte+= missatge + ' n\'est pas correct\n'
					obj.style.backgroundColor=color2			
				}	
			else
				{
				obj.style.backgroundColor=color
				} 
		}

// valor del select 
        if(tipus.indexOf('VAL')!=1){
			if (('VAL' + obj.value)==tipus)
			{
				if (!incorrecte){obj.focus()};
				incorrecte+='Vous devez choisir ' + missatge + ' \n'
				obj.style.backgroundColor=color2				
			}
		}




// fi validacio
	}
	if (incorrecte){
	    incorrecte= '\nN\'a pas complété le formulaire correctement:\n\n' + incorrecte
		alert (incorrecte)
	}
	if (envia){
		return (incorrecte=='')
	}
}



function validaData(obj)
{
	var fecha,DataNova;
	fecha = obj.value	
	fechaArray = fecha.split("/")
	
	if (fechaArray.length!=3)
		{
		fechaArray = fecha.split("-");
		}	
	
	if (fechaArray.length==3)
		{
		DataNova = new Date(fechaArray[2],fechaArray[1]-1,fechaArray[0]);
		}
	else
		{
		obj.value = '';
		return false;
		}
	if (isNaN(DataNova))
		{
		obj.value = '';
		return false;
		}
	else
		{
		obj.value = DataNova.getDate() + '/' + (DataNova.getMonth() + 1) + '/' + DataNova.getYear() ;
		return true;
		}
}

/*
 * Función agregada para validar  que fecha2 < fecha1.
 * Param: formulario es el form que contiene a los input.
 * descripcion: los input son type text (fecha1, fecha2) , separador "-".
 */
function DiferenciaFechas (formulario) {

   //Obtiene los datos del formulario
   CadenaFecha1 = formulario.entrada.value
   CadenaFecha2 = formulario.salida.value
   
   //Obtiene dia, mes y año
   var fecha1 = new fecha( CadenaFecha1 )   
   var fecha2 = new fecha( CadenaFecha2 )
   
   //Obtiene objetos Date
   var miFecha1 = new Date(fecha1.anio, fecha1.mes ,fecha1.dia)
   var miFecha2 = new Date(fecha2.anio, fecha2.mes, fecha2.dia)

   //Resta fechas y redondea
   var diferencia = miFecha2.getTime() - miFecha1.getTime()
   var dias = Math.floor(diferencia / (1000 * 60 * 60 * 24))
/*   var segundos = Math.floor(diferencia / 1000)
   alert ('La diferencia es de ' + dias + ' dias,\no ' + segundos + ' segundos.')*/
   var segundos = Math.floor(diferencia / 1000)
   if (dias < 0)
   		alert ('Le jour de sortie doit être postérieur à à celui d\'entrée!')
	return dias;
}

function fecha( cadena ) {
   //Separador para la introduccion de las fechas
   var separador = "/"

   //Separa por dia, mes y año
   if ( cadena.indexOf( separador ) != -1 ) {
        var posi1 = 0
        var posi2 = cadena.indexOf( separador, posi1 + 1 )
        var posi3 = cadena.indexOf( separador, posi2 + 1 )
        this.dia = cadena.substring( posi1, posi2 )
        this.mes = cadena.substring( posi2 + 1, posi3 )
        this.anio = cadena.substring( posi3 + 1, cadena.length )
   } else {
        this.dia = 0
        this.mes = 0
        this.anio = 0   
   }
}

