// JavaScript Document

function trim(cadena)
{
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
return cadena;
}


function validar_form_user(obj)
{
	ok = true;
	txt_ini="Cal omplir els camps obligatoris\n\n";
	fons_error='#FFCCCC';
	fons_ok='white';
	
	
	obj.username.value = trim(obj.username.value);
	obj.password.value = trim(obj.password.value);
	
	if( obj.username.value.length == 0 )
			{
				txt_private="· Has d'introduir el nom d'usuari.\n";
				txt_ini+=txt_private;
				obj.username.style.background=fons_error;
				ok= false;
			
			}
			else
			{ obj.username.style.background=fons_ok;  }


	if( obj.password.value.length == 0 )
			{
				txt_private="· Has d'introduir la contrasenya.\n";
				txt_ini+=txt_private;
				obj.password.style.background=fons_error;
				ok= false;
			
			}
			else
			{ obj.password.style.background=fons_ok;  }
	
	
	
	if (ok !=false)
		{ 
			return;	
		}
		else
		{ 	
			alert(txt_ini);
			//document.getElementById('dv_warning').style.display ='none';
	
			//document.getElementById('dv_warning').style.display ='block';
	
			return false;
		}
	
}

function Comprovar_mod_passwords(obj)
{
	
	obj.pwd.value = trim(obj.pwd.value);
	obj.noupwd1.value = trim(obj.noupwd1.value);
	obj.noupwd2.value = trim(obj.noupwd2.value);
	
	if( obj.pwd.value.length > 0 && obj.noupwd1.value.length > 0 && obj.noupwd2.value.length > 0 )
	{
		if( obj.noupwd1.value == obj.noupwd2.value )
		{
			Confirmar_mod_pass(obj);
		}
		else
		{
			alert( "Asseguris d'haver escrit correctament les noves contrasenyes." );
			obj.noupwd2.focus();
			return false;
		}
	}
	else
	{
		alert( "No pot deixar en blanc alguna de les contrasenyes" );
		return false;
	}
}

function Confirmar_mod_pass(obj)
{
	if( confirm( "Està segur que vol modificar la contrasenya?" ) )
	{
		obj.submit();
	}
}

function validar_mail(str) {
	
	var re = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;
	if ( !str.match(re) ) {
        alert("L'adreça electrònica no es correcta");
		return false;
    } else {
        return true;
    }
}

function validar_form_contacte(obj) {
	
	ok = true;
	txt_ini="Cal omplir els camps obligatoris\n\n";
	fons_error='#FFCCCC';
	fons_ok='white';
	
	obj.nom.value = trim(obj.nom.value);
	obj.cognoms.value = trim(obj.cognoms.value);
	obj.telf.value = trim(obj.telf.value);
	obj.asumpte.value = trim(obj.asumpte.value);
	obj.correo.value = trim(obj.correo.value);
	
	if( obj.nom.value.length == 0 )
			{
			/*	txt_private="· Has d'introduir el nom d'usuari.\n";
				txt_ini+=txt_private;
			*/
				obj.nom.style.background=fons_error;
				ok= false;
			
			}
			else
			{ obj.nom.style.background=fons_ok; }
	
	if( obj.cognoms.value.length == 0 )
			{
			/*	txt_private="· Has d'introduir el nom d'usuari.\n";
				txt_ini+=txt_private;
			*/
				obj.cognoms.style.background=fons_error;
				ok= false;
			
			}
			else
			{ obj.cognoms.style.background=fons_ok; }
			
/*	if( obj.telf.value.length == 0 )
			{
			/*	txt_private="· Has d'introduir el nom d'usuari.\n";
				txt_ini+=txt_private;
			*/
/*				obj.telf.style.background=fons_error;
				ok= false;
			
			}
			else
			{ obj.telf.style.background=fons_ok; }
*/			
/*	if( obj.correo.value.length == 0 )
			{
			/*	txt_private="· Has d'introduir el nom d'usuari.\n";
				txt_ini+=txt_private;
			*/
/*				obj.correo.style.background=fons_error;
				ok= false;
			
			}
			else
			{ obj.correo.style.background=fons_ok; }
*/			
	
	if (obj.correo.value.length != 0)
	{
		str =obj.correo.value;
		var re = /[\w-\.]{3,}@([\w-]{2,}\.)*([\w-]{2,}\.)[\w-]{2,4}/;
		if ( !str.match(re) ) {
			
				txt_private="·L'adreça electrònica no es correcta.\n";
				txt_ini+=txt_private;
			obj.correo.style.background=fons_error;
			ok= false;
			//alert("L'adreça electrònica no es correcta");
			//return false;
		} else {
			obj.correo.style.background=fons_ok;
			//return true;
		}	
	}
	else
	{
		obj.correo.style.background=fons_error;
		ok= false;
	}
			
	if( obj.asumpte.value.length == 0 )
			{
			/*	txt_private="· Has d'introduir el nom d'usuari.\n";
				txt_ini+=txt_private;
			*/
				obj.asumpte.style.background=fons_error;
				ok= false;
			
			}
			else
			{ obj.asumpte.style.background=fons_ok; }
			
	if (ok !=false)
		{ return; }
		else
		{ 	
			alert(txt_ini);
			//document.getElementById('dv_warning').style.display ='none';
	
			//document.getElementById('dv_warning').style.display ='block';
	
			return false;
		}
	
}

