function valida_numeric(valor)
{
	for(var i=0;i<valor.length;i++)
	{
		if(valor.substring(i,i+1) < "0" || valor.substring(i,i+1) > "9")
		{
			return -1;
		}
	}
	return valor.length;
};

function enviarColVR(colectiu, codColectiu, idPromocio, indFinanciero)
{
	document.forms["identificacionColectivos"].elements["pm_COD_COLECTIVO"].value = codColectiu;
	document.forms["identificacionColectivos"].elements["pm_ID_PROMOCION"].value = idPromocio;
	document.forms["identificacionColectivos"].elements["indFinanciero"].value = indFinanciero;
	
	var resultat = validarColectiu_NPC(colectiu, codColectiu);
	return resultat;
};

function enviarCol(colectiu, codColectiu, idPromocio, indFinanciero)
{
	document.forms["identificacionColectivos"].elements["pm_COD_COLECTIVO"].value = codColectiu;
	document.forms["identificacionColectivos"].elements["pm_ID_PROMOCION"].value = idPromocio;
	document.forms["identificacionColectivos"].elements["indFinanciero"].value = indFinanciero;
	
	var resultat = ferSubmitColectiu(colectiu, codColectiu);
	
	if(resultat == true)
	{
		 document.forms["identificacionColectivos"].submit();
	}
};

function ferSubmitColectiu(colectiu, codColectiu)
{
	if( validarColectiu(colectiu, codColectiu) == false )
	{
		return false;
	}
	return true;
};

function validarColectiu(idColectiu, codColectiu)
{
	tabla = document.getElementById(codColectiu+'_tabla_'+idColectiu);
	inputs = tabla.getElementsByTagName("input");
	
	for (var i=0;i<inputs.length;i++)
	{
		name = inputs[i].name;
		valor = inputs[i].value;
		
		if (name=="MAIL")
		{
			if (valor.length<1)
			{
				alert (missatge_email_obligatori);
				return false;
			}
			else if (valor.length>0 && !isEmail(valor))
			{
				alert (missatge_email_format);
				return false;
			}
			else
			{
				document.forms["identificacionColectivos"].elements["pm_MAIL"].value = valor;
			}
		}
		if (name=="PAN")
		{
			if (valor==null || valor=="")
	   		{
				alert(missatge_tarjeta_obligatoria);
				return false;		
	   		}
	   		else if (valor.length>0 && valida_numeric(valor)==-1)
			{
				alert(missatge_tarjeta_incorrecta);
				return false;		
			}
	   		else
			{
				document.forms["identificacionColectivos"].elements["pm_PAN"].value = valor;
			}
		}
		if (name=="NIF")
		{
			if (valor.length<1)
 	  		{
		    	alert(missatge_nif_obligatori);
		    	return false;
	  		}
	   		else if(!TestNIF(valor))
			{
				alert(missatge_nif_incorrecto);
				return false;
			}
			else
			{
				document.forms["identificacionColectivos"].elements["pm_NIF"].value = valor;
			}
		}
		if (name=="USUARIO")
		{
			if (valor.length<1)
			{
				alert(missatge_usuari_obligatori);
				return false;	
			}
			else
			{
				document.forms["identificacionColectivos"].elements["COD_USUARIO"].value = valor;
			}
		}
		if (name=="CLAVE")
		{
			if (valor.length<1)
			{
				alert(missatge_clave_obligatoria);
				return false;	
			}
			else
			{
				document.forms["identificacionColectivos"].elements["pm_CLAVE_GRUPO"].value = valor;
			}
		}
		if (name=="PASSWORD")
		{
			if (valor.length<1)
			{
				alert(missatge_contrasenya_obligatoria);
				return false;	
			}
			else
			{
				document.forms["identificacionColectivos"].elements["pm_PASSWORD"].value = valor;
			}
		}
	}		
	return true;
};


function validarColectiu_NPC(idColectiu, codColectiu)
{
	var htDatos = new Object();
	
	tabla = document.getElementById(codColectiu+'_tabla_'+idColectiu);
	inputs = tabla.getElementsByTagName("input");
	
	htDatos[1] = null;
	
	for (var i=0;i<inputs.length && htDatos[1] == null; i++)
	{
		name = inputs[i].name;
		literal = inputs[i].id;
		valor = inputs[i].value;
		// JJ - 11/06/09 - Incidencia: añadimos un trim en la validación -->
		valor = trim(valor);
		// <-- JJ - 11/06/09 - Incidencia: añadimos un trim en la validación
		if(literal != null)
			mensaje_cambiado_obligatorio = mensaje_parametre_obligatorio.replace('&',literal);
	
		if (name=="MAIL")
		{
			if (valor.length<1)
			{
				htDatos[1] = mensaje_cambiado_obligatorio;
			}
			else if (valor.length>0 && !isEmail(valor))
			{
				htDatos[1] = mensaje_cambiado_obligatorio;
			}
			else
			{
				htDatos["MAIL"] = valor;
			}
		}
		else if (name=="PAN")
		{
			if (valor==null || valor=="")
	   		{
				htDatos[1] = mensaje_cambiado_obligatorio;
			}
	   		else if (valor.length>0 && valida_numeric(valor)==-1)
			{
				htDatos[1] = mensaje_cambiado_obligatorio;
			}
	   		else
			{
				htDatos["PAN"] = valor;
			}
		}
		else if (name=="NIF")
		{
			if (valor.length<1)
 	  		{
		    	htDatos[1] = mensaje_cambiado_obligatorio;
	  		}
	   		else if(!TestNIF(valor))
			{
				htDatos[1] = mensaje_cambiado_obligatorio;
			}
			else
			{
				htDatos["NIF"] = valor;
			}
		}
		else if (name=="USUARIO")
		{
			if (valor.length<1)
			{
				htDatos[1] = mensaje_cambiado_obligatorio;
			}
			else
			{
				htDatos["USUARIO"] = valor;
			}
		}
		else if (name=="CLAVE")
		{
			if (valor.length<1)
			{
				htDatos[1] = mensaje_cambiado_obligatorio;
			}
			else
			{
				htDatos["CLAVE"] = valor;
			}
		}
		else if (name=="PASSWORD" || name=="PIN")
		{
			if (valor.length<1)
			{
				htDatos[1] = mensaje_cambiado_obligatorio;
			}
			else
			{
				htDatos["PASSWORD"] = valor;
			}
		}
	}		
	return htDatos;
};