/*
CATALÀ
------------------------

DNI
NUMERICO
MAIL
DATE
MINLENGTH
MINVALUE
MAXVALUE
IAMGEN (solo formato JPG para redimensionar)
*/
/*function checkState()
{
	if(document.readyState=="complete")
	{
		checkForms()
	}
}*/

//document.onreadystatechange = checkState

window.onload=checkForms

function checkForms()
{
		formularios=document.getElementsByTagName("FORM")
		for(i=0;i<formularios.length;i++)
		{
			if(formularios[i].getAttribute("validar"))
			{
				formularios[i].onsubmit=function(){return validar(this)}
				for(j=0;j<formularios[i].length;j++)
				{
					if(formularios[i].elements[j].type=="text" || formularios[i].elements[j].type=="file")
					{
						formularios[i].elements[j].tmp=formularios[i].elements[j].onblur
						formularios[i].elements[j].onblur=function()
						{
							if(tipo(this))
							{
								if(this.tmp!=null)
								{
									this.tmp()
								}
							}
						}
					}
					
				}
			}
		}
}
function tipo(objeto)
{
	if(objeto.getAttribute("tipo"))
	{
		tipus=objeto.getAttribute("tipo").toUpperCase()
		if(tipus!="")
		{
			switch(tipus)
			{
				case "DNI": return dni(objeto);break;
				case "NUMERIC":return numeric(objeto) ;break;
				case "MAIL": return email(objeto);break;
				case "DATE":return fecha(objeto);break;
				//case "CC":return validar_cc(objeto);break;
				case "IMAGE":if(llibreria_gd==1) { return imagen_gd1(objeto);break; } else { return imagen_gd2(objeto);break; }
			}
		}
	}
	if(objeto.getAttribute("minlength"))
	{
		if(objeto.value.length<objeto.getAttribute("minlength"))
		{
			alert("Longitud mínima de "+objeto.getAttribute("minlength")+ " caràcters.")
			objeto.focus()
			objeto.select()
			return false
		}
	}
	if(objeto.getAttribute("minvalue"))
	{
		if(parseInt(objeto.value)<parseInt(objeto.getAttribute("minvalue")))
		{
			alert("valor mínim: "+objeto.getAttribute("minvalue"))
			objeto.focus()
			objeto.select()
			return false
		}
	}
	if(objeto.getAttribute("maxvalue"))
	{
		if(parseInt(objeto.value)>parseInt(objeto.getAttribute("maxvalue")))
		{
			alert("valor màxim: "+objeto.getAttribute("maxvalue"))
			objeto.focus()
			objeto.select()
			return false
		}
	}
}
function email(objeto)
{
	var re,s
	var s = objeto.value
	if(s!="")
	{
		//re = new RegExp("[0-9a-zA-Z]{1,*}@[0-9a-zA-Z](1,*).[0-9a-zA-Z]{2,4}","i"); 		
		//re=new RegExp("[0-9a-zA-Z]+@[0-9a-zA-Z]+[\.][a-zA-Z]{2,4}$","i"); 		
		re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
		r = s.match(re);
		if(r==null)
		{
			alert("Email incorrecte")
			objeto.focus()
			objeto.select()
			return false
		}
		return true
	}
}

function numeric(objeto)
{
	if(!isFinite(objeto.value))
	{
		alert("Número Incorrecte")
		objeto.focus()
		objeto.select()
		return false
	}
	else return true
}

function imagen_gd1(objeto)
{
	objeto.value=objeto.value.toUpperCase()
	valor=objeto.value
	valor_formato=valor.toUpperCase()
	
	if ((valor!="") && (valor_formato.lastIndexOf(".JPG"))==-1)
	{
		alert("Tipus de format Incorrecte, només JPG")
		objeto.focus()
		objeto.select()
		objeto.click()
		return false
	}
	else return true
}

function imagen_gd2(objeto)
{
	objeto.value=objeto.value.toUpperCase()
	valor=objeto.value
	valor_formato=valor.toUpperCase()
	
	if ((valor!="") && ((valor_formato.lastIndexOf(".GIF"))==-1 && (valor_formato.lastIndexOf(".JPG"))==-1))
	{
		alert("Tipus de format Incorrecte, només JPG o GIF")
		objeto.focus()
		objeto.select()
		objeto.click()
		return false
	}
	else return true
}

function validar(formulario)
{
	for(i=0;i<formulario.length;i++)
	{
		var elinput=formulario.elements[i]
		if(elinput.getAttribute("obligatorio")!=null)
		{
			if(elinput.value=="")
			{
				if(elinput.nombre==null) elinput.nombre=elinput.name
				alert("Falta un camp obligatori:\n\n"+elinput.nombre)
				try{elinput.focus()}
				catch(e){
					cambia(eval(elinput.pestana_madre));
					elinput.focus()
				}
				return false
			}
		}
	}
	return formulario.submitActivo
}

function fecha(objeto)
{
	var r, re;
	var s = objeto.value
	var separador="/"
	if(s!="")
	{
		re = new RegExp("[0-3]{0,1}[0-9]{1}"+separador+"[0-1]{0,1}[0-9]{1}"+separador+"[0-9]{4}","i"); 
		r = s.match(re);
		if(r==null)
		{
			separador="-"
			re = new RegExp("[0-3]{0,1}[0-9]{1}"+separador+"[0-1]{0,1}[0-9]{1}"+separador+"[0-9]{4}","i"); 
			r = s.match(re);
		}
		if(r==null)
		{
			separador="."
			re = new RegExp("[0-3]{0,1}[0-9]{1}"+separador+"[0-1]{0,1}[0-9]{1}"+separador+"[0-9]{4}","i"); 
			r = s.match(re);
		}
		if(r!=null)
		{
			objeto.value=r
			s=objeto.value
			dia=s.substring(0,s.indexOf(separador))
			mes=s.substring(s.indexOf(separador)+1,s.lastIndexOf(separador))-1
			anyo=s.substring(s.lastIndexOf(separador)+1,s.length)
			try{
				var newfecha=new Date(anyo, mes, dia)
				dia2=newfecha.getDate()
				mes2=newfecha.getMonth()
				anyo2=newfecha.getFullYear()
				if(dia2!=dia || mes2!=mes || anyo2!=anyo || anyo<1990)
				{
					alert("Data incorrecta")
					objeto.focus()
					objeto.select()
					return false
				}
			}
			catch(e)
			{
				alert("Data incorrecta")
				objeto.focus()
				objeto.select()
				return false
			}
		}
		else
		{
			alert("Data incorrecta")
			objeto.focus()
			objeto.select()
			return false
		}
	}
}

function dni(objeto)
{
	objeto.value=objeto.value.toUpperCase()
	valor=objeto.value
	
	if (valor.length==0) return false
	if(valor.length>7 )
	{
		num=valor.substr(0,valor.length-1)
		if(valor.length==8) 
		{
			num=valor
		}
		if(valor.length==9 && isFinite(valor) && valor.substr(0,1)=="0") 
		{
			num=valor
		}
		if (valor.length>9 && valor.substr(0,1)!="0")
		{
			alert ("DNI incorrecte\n\nIntroduïr 9 digits i la lletra")
			objeto.focus()
			objeto.select()
			return false
		}
		letra=valor.substr(valor.length-1, valor.length)

		if (isFinite(num))
		{
			resto = num % 23;
			switch(resto) 
			{
				case 0: l="T"; break;
				case 1: l="R"; break;
				case 2: l="W"; break;
				case 3: l="A"; break;
				case 4: l="G"; break;
				case 5: l="M"; break;
				case 6: l="Y"; break;
				case 7: l="F"; break;
				case 8: l="P"; break;
				case 9: l="D"; break;
				case 10:l="X"; break;
				case 11:l="B"; break;
				case 12:l="N"; break;
				case 13:l="J"; break;
				case 14:l="Z"; break;
				case 15:l="S"; break;
				case 16:l="Q"; break;
				case 17:l="V"; break;
				case 18:l="H"; break;
				case 19:l="L"; break;
				case 20:l="C"; break;
				case 21:l="K"; break;
				case 22:l="E"; break;
				case 23:l="U"; break;
			}
			if(l!=letra)
			{
				objeto.value=num+l
				if(objeto.value.length<10)
				{
					objeto.value="0"+objeto.value
				}
				return true
				//alert ("DNI incorrecte\n\nIntroduïr 9 digits i la lletra")
				//objeto.focus()
				//objeto.select()
				//return false
			}
			else
			{
				if(objeto.value.length<10)
				{
					objeto.value="0"+objeto.value
				}
				return true
			}
		}
		else 
		{
			alert ("DNI incorrecte\n\nIntroduïr 9 digits i la lletra")
			objeto.focus()
			objeto.select()
			return false
		}
	}
	else 
	{
		alert ("DNI incorrecte\n\nIntroduïr 9 digits i la lletra")
		objeto.focus()
		objeto.select()
		return false
	}
}







/*

// Pasem en numero de c/c per a validar
function validar_cc(objeto)
{
	
	//---	
//	alert(cuenta_corriente);
	var cuenta_corriente=objeto.value;
	//---

	var banco, sucursal, dc, cuenta;

	var cuenta_array=cuenta_corriente.split("-");
	
	banco = cuenta_array[0];
	sucursal = cuenta_array[1];
	dc = cuenta_array[2];
	cuenta = cuenta_array[3];
//	alert("juas");
	
	//return false;
	
	//alert(cuenta_array[0]+" "+cuenta_array[1]+" "+cuenta_array[2]+" "+cuenta_array[3]);
	return validacio_cc(banco, sucursal, dc, cuenta, objeto);
}


function numerico(valor)
{
	cad = valor.toString();
	for (var i=0; i<cad.length; i++)
	{
		var caracter = cad.charAt(i);
		if (caracter<"0" || caracter>"9")
		return false;
	}
	return true;
}


function obtenerDigito(valor)
{
	valores = new Array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
	control = 0;
	for (i=0; i<=9; i++)
	control += parseInt(valor.charAt(i)) * valores[i];
	control = 11 - (control % 11);
	if (control == 11) control = 0;
	else if (control == 10) control = 1;
	return control;
}

//validar(banco, sucursal, dc, cuenta);
function validacio_cc(banco, sucursal, dc, cuenta, objeto)
{
	if (banco == ""  || sucursal == "" || dc == "" || cuenta == "")
	{
		alert("Si us plau, introdueix el número de compte bancari");
		objeto.focus()
		objeto.select()
		return false;
	}
	else
	{
		if (banco.length != 4 || sucursal.length != 4 || dc.length != 2 || cuenta.length != 10)
		{
			alert("Si us plau, introdueix el número de compte bancari correctament; no està completada");
			objeto.focus()
			objeto.select()
			return false;
		}
		else
		{
			if (!numerico(banco) || !numerico(sucursal) || !numerico(dc) || !numerico(cuenta))
			{
				alert("Si us plau, introdueix el número de compte bancari correctament; no són numèrics");
				objeto.focus()
				objeto.select()
				return false;
			}
			else 
			{
				if (!(obtenerDigito("00" + banco + sucursal) == parseInt(dc.charAt(0))) || 
				!(obtenerDigito(cuenta) == parseInt(dc.charAt(1))) )
				{
					alert("Els dígits de control no es corresponen amb els altres dígits del compte bancari");
					objeto.focus()
					objeto.select()
					return false;
				}
			else
				{
				// Compta corrent correcta, saltem al frame número 5 del flash
				alert ("CCC correcte");
				return true;
				//window.document.myFlash.SetVariable("Envia_form", banco+"-"+sucursal+"-"+dc+"-"+cuenta);
				//window.document.myFlash.gotoframe(5);
				}
			}
		}
	}
}
*/






function keys()
{
//	alert(event.keyCode )
//	alert(String.fromCharCode(event.keyCode))
	if(!isFinite(String.fromCharCode(event.keyCode)))
	{
		event.returnValue=false;
	}
	else return true

	alert(event.keyCode )
	
}

