function validar(){

    var color2='#FFFFE1'
    var color='#ffffff'
	var args=validar.arguments;
	var numargs=args.length;
	var envia,errorMsg,tipus

	
	for (i=1; i<(numargs-1); i+=3)
	{

	    
		if (numargs==3)
		{
			obj=args[0];
			envia=false;
			errorMsg=args[1]
			tipus=args[2]
		}
		else 
		{
			obj=eval(args[0] + '.' + args[i]);
			envia=true;
			errorMsg='<strong>' + args[i+1] + '</strong>';			
			tipus=args[i+2];
		}


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

			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.background=color
					}
				}
			
				if (!typevalid)
				{
					//if (!novalid){obj.focus()};
					novalid+= "<br />-&nbsp;" + errorMsg + ' no es un archivo de tipo correcto.\n'
					obj.style.background=color2		
				}
				else
					obj.style.background=color
			 }
			 else
				obj.style.background=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.background=color
					}
				}
				
				if (!typevalid)
				{
					//if (!novalid){obj.focus()};
					novalid+= "<br />-&nbsp;" + errorMsg + ' no es un archivo de tipo correcto.\n'
					obj.style.background=color2		
				}
				else
					obj.style.background=color
			}
			else
				obj.style.background=color
		}
		
        // obligatori
        if ((tipus=='') || (tipus=='no') || (tipus=='mo')){
			if (obj.value=="")
				{
					//if (!novalid){obj.focus()};
					novalid+= "<br />-&nbsp;" + errorMsg + ' es obligatorio.\n'
					obj.style.background=color2
				}
				else
				{		
					obj.style.background=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 (!novalid){obj.focus()};
				novalid+= "<br />-&nbsp;" + errorMsg + ' no es una dirección de e-Mail válida.\n'
				obj.style.background=color2						
			}
			else
			{		
				obj.style.background=color
}


		}

		// número 
        if(tipus=='n'){
			if ((obj.value!=Number(obj.value)) && (obj.value!=''))
			{
				//if (!novalid){obj.focus()};
				novalid+= "<br />-&nbsp;" + errorMsg + ' debe ser valor numérico.\n'
				obj.style.background=color2				
			}
			else
			{		
				obj.style.background=color
			}
			if (obj.value==''){obj.value='0'}
		}

		// cp
		if (tipus=='cp')
		{
			if ((obj.value!=Number(obj.value)) && (obj.value!='')  || (obj.value<10000))
			{			
				//if (!novalid){obj.focus()};
				novalid+= "<br />-&nbsp;" + errorMsg + ' debe ser valor numérico de 5 dígitos.\n'
				obj.style.background=color2				
			}
			else
			{		
				obj.style.background=color
			}
		}

		// llargada maxima 
        if(tipus>1){
			if (obj.value.length > tipus)
			{
				//if (!novalid){obj.focus()};
				novalid+= "<br />-&nbsp;" + errorMsg + ' contiene ' + obj.value.length + ' carácteres, no puede ser mas ancho de ' + tipus + '.\n'
				obj.style.background=color2				
			}
			else
			{		
				obj.style.background=color
			}
		}

		// data
		if (tipus=='di')
		{
			var obj2=eval(args[0]);
			var Any=obj2.ano.value;
			var Mes=obj2.mes.options[obj2.mes.selectedIndex].value;
			var Dia=obj2.dia.options[obj2.dia.selectedIndex].value;
			
			if ((Any == "") || (Any!=Number(Any)))
			{
			novalid+= "<br />-&nbsp;" + errorMsg + "Olvida el año o ha introducido un valo no numérico.\n"
			}
			else
				{
				if ((Any<999) || (Any>9999))
					{
					novalid+= "<br /> " + errorMsg + "El año es incorrecto.\n"
					}
				}
			
		    if ((Mes==2) && (Dia>28))
			{
				if (((Any/4)==parseInt(Any/4)) && (Dia<30))
				{
				}
				else
				{
				novalid+= "<br />-&nbsp;" + errorMsg + 'La fecha es incorrecta.\n'
				}
			}
			if ((Mes==4) || (Mes==6) || (Mes==9) || (Mes==11))
			{
				if (Dia>30)
				{
					novalid+= "<br />-&nbsp;" + errorMsg + 'La fecha es incorrecta.\n'
				}
			}
		}
		
// valor del select 
        if(tipus.indexOf('VAL')!=1){
			if (('VAL' + obj.value)==tipus)
			{
				//if (!novalid){obj.focus()};
				novalid+='Debe seleccionar ' + errorMsg + '.\n'
				//obj.style.background=color2				
			}
			else
			{		
				//obj.style.background=color
			}
		}

// fi validacio
	}
	if (novalid){
	    novalid = 'No ha rellenado el formulario correctamente...' + novalid + "<br />";
	}
	if (envia){
		return (novalid == '');
	}
}

function validarForm()
{
	document.getElementById("errorBox").style.display = 'none';
	document.getElementById("errorBox").innerHTML = "";
	novalid = '';

	//if (validar('document.contactform', 'dp_nombre', 'Nombre', '', 'dp_email', 'E-mail', 'mo', 'dp_observaciones', 'Comentarios', ''))
	if (validar('document.contactform', 'dp_nombre', 'Nombre', '', 'dp_email', 'E-mail', 'mo', 'dp_observaciones', 'Comentarios', comentari_obligatori))
	{


		
			if (document.getElementById("dp_acepto").checked == false)
			{
				//alert("Por favor acepte las condiciones de privacidad.");
				document.getElementById("dp_acepto").focus();
				novalid += "Por favor acepte los términos y condiciones de la <strong>Nota Legal.</strong>";
				document.getElementById("errorBox").innerHTML = '<h4>ATENCI&Oacute;N:</h4><p>' + novalid + '</p>';
				document.getElementById("errorBox").style.display = 'block';
			}
			else
				sendResponse();
				//document.formulario.submit();//			
			}


			else {

				document.getElementById("errorBox").innerHTML = '<h4>ATENCI&Oacute;N:</h4><p>' + novalid + '</p>';
				document.getElementById("errorBox").style.display = 'block';
	}
	
	
}

function fieldNumber1(objeto, numeroDecimales, e) 
{ 
	var valorCampo; 
	var evento_key = (document.all) ? e.keyCode : e.which; 
	var numPosPunto = 0; 
	var strParteEntera = ""; 
	var strParteDecimal = ""; 
	var NUM_DECIMALES = numeroDecimales; 

	switch (evento_key) 
	{ 
		case 48: 
		case 49: 
		case 50: 
		case 51: 
		case 52: 
		case 53: 
		case 54: 
		case 55: 
		case 56: 
		case 57: 
		break; 
		default: 
			if (((evento_key != 44) || (NUM_DECIMALES == 0)) && (evento_key != 8))
			{
				if (document.all) 
					e.keyCode = 0;
				else 
					e.stopPropagation();
				// return false; 
			}
			else if (evento_key == 8) return true;
	} 

	valorCampo = objeto.value; 	
	if (evento_key == 44) 
	{
		if (valorCampo == '')
		{
			if (document.all) 
				e.keyCode = 0;
			else 
				e.stopPropagation();
			// return false; 
		}
		else if (valorCampo.indexOf(",") != -1)
		{ 
			if (document.all) 
				e.keyCode = 0;
			else 
				e.stopPropagation();
			// return false; 
		} 
	}
	/* Sólo puede teclear el número de decimales indicado en NUM_DECIMALES */ 
	if ((numPosPunto = valorCampo.indexOf(",")) != -1) 
	{ 
		strParteEntera = valorCampo.substr(0,(numPosPunto - 1)); 
		strParteDecimal = valorCampo.substr((numPosPunto + 1), valorCampo.length) 
		
		if (strParteDecimal.length > (NUM_DECIMALES - 1)) 
		{ 
			if (document.all) 
				e.keyCode = 0;
			else 
				e.stopPropagation();
			// return false; 
		} 
	} 
	
	return true; 
}

