// JavaScript Document
function validar()
{
  error = 0
  if (document.formulari["titol"].value == "" && !error){
    alert("Debes introducir un titulo para el anuncio")
    document.formulari["titol"].focus()
    error = 1
  }
  	t = new String(document.formulari["descripcio"].value)
	if (t.length == 0  && !error) {
		alert("Debes introducir una descripción para el anuncio")
		document.formulari["descripcio"].focus()
		error = 1
	}
	if (document.formulari["email"].value == "" && !error){
    alert("Debes introducir una dirección de contacto")
    document.formulari["email"].focus()
    error = 1
  	}
	if (document.formulari["email"].value != "" && !error) {
		if ( !error ){
			var emailPat=/^(.+)@(.+)$/
	  	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
 		  var validChars="\[^\\s" + specialChars + "\]"
      var quotedUser="(\"[^\"]*\")"
      var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
      var atom=validChars + '+'
      var word="(" + atom + "|" + quotedUser + ")"
      var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
      var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
      var matchArray=document.formulari["email"].value.match(emailPat)
      if (matchArray==null && !error) {
          alert("Dirección de contacto incorrecta")
          document.formulari["email"].focus()
          error = 1
      }
      if ( !error ){
        var user=matchArray[1]
        var domain=matchArray[2]

        if (user.match(userPat)==null && !error) {
            alert("Dirección de contacto incorrecta")
            document.formulari["email"].focus()
            error = 1
        }
        if ( !error ){
          var IPArray=domain.match(ipDomainPat)
          if (IPArray!=null && !error) {
              for (var i=1;i<=4;i++) {
                  if (IPArray[i]>255) {
                      alert("Dirección de contacto incorrecta")
                      document.formulari["email"].focus()
                      error = 1
                      break
                  }
              }
          }
          if ( !error ){
            var domainArray=domain.match(domainPat)
            if (domainArray==null && !error) {
                alert("Dirección de contacto incorrecta")
                document.formulari["email"].focus()
                error = 1
            }

            if ( !error ){
              var atomPat=new RegExp(atom,"g")
              var domArr=domain.match(atomPat)
              var len=domArr.length
              if ((domArr[domArr.length-1].length<2 ||
                  domArr[domArr.length-1].length>3 ) && !error) {
                  alert("Dirección de contacto incorrecta")
                  document.formulari["email"].focus()
                  error = 1
              }
              if (len<2 && !error) {
                 document.formulari["email"].focus()
                 alert("Dirección de contacto incorrecta")
                  error = 1
              }
            }
          }
        }
      }
  	}
	}
	if (document.formulari["code"].value == "" && !error){
    alert("Debes introducir la Palabra de control")
    document.formulari["code"].focus()
    error = 1
  }
	if ( !error ){
  	return true
	}
	else return false
}

function validar_contacte()
{
	error = 0
	if (document.formulari["email"].value == "" && !error){
    	alert("Debes introducir un e-mail de contacto")
    	document.formulari["email"].focus()
    	error = 1
  	}
	if (document.formulari["email"].value != "" && !error) {
		if ( !error ){
			var emailPat=/^(.+)@(.+)$/
	  	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
 		  var validChars="\[^\\s" + specialChars + "\]"
      var quotedUser="(\"[^\"]*\")"
      var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
      var atom=validChars + '+'
      var word="(" + atom + "|" + quotedUser + ")"
      var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
      var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
      var matchArray=document.formulari["email"].value.match(emailPat)
      if (matchArray==null && !error) {
          alert("Dirección de contacto incorrecta")
          document.formulari["email"].focus()
          error = 1
      }
      if ( !error ){
        var user=matchArray[1]
        var domain=matchArray[2]

        if (user.match(userPat)==null && !error) {
            alert("Dirección de contacto incorrecta")
            document.formulari["email"].focus()
            error = 1
        }
        if ( !error ){
          var IPArray=domain.match(ipDomainPat)
          if (IPArray!=null && !error) {
              for (var i=1;i<=4;i++) {
                  if (IPArray[i]>255) {
                      alert("Dirección de contacto incorrecta")
                      document.formulari["email"].focus()
                      error = 1
                      break
                  }
              }
          }
          if ( !error ){
            var domainArray=domain.match(domainPat)
            if (domainArray==null && !error) {
                alert("Dirección de contacto incorrecta")
                document.formulari["email"].focus()
                error = 1
            }

            if ( !error ){
              var atomPat=new RegExp(atom,"g")
              var domArr=domain.match(atomPat)
              var len=domArr.length
              if ((domArr[domArr.length-1].length<2 ||
                  domArr[domArr.length-1].length>3 ) && !error) {
                  alert("Dirección de contacto incorrecta")
                  document.formulari["email"].focus()
                  error = 1
              }
              if (len<2 && !error) {
                 document.formulari["email"].focus()
                 alert("Dirección de contacto incorrecta")
                  error = 1
              }
            }
          }
        }
      }
  	}
	}
	if (document.formulari["missatge"].value == "" && !error){
    	alert("Debes introducir un mensaje para el anunciante")
    	document.formulari["missatge"].focus()
    	error = 1
  	}
	if ( !error ){
  	return true
	}
	else return false
}

function validar_registro_user()
{
  error = 0
  if (document.formulari["user"].value == "" && !error){
    alert("Debes introducir un Nombre de usuario")
    document.formulari["user"].focus()
    error = 1
  }
  if (document.formulari["password"].value == "" && !error){
    alert("Debes introducir una Contraseña")
    document.formulari["password"].focus()
    error = 1
  }
  if (document.formulari["repassword"].value == "" && !error){
    alert("Debes confirmar la Contraseña")
    document.formulari["repassword"].focus()
    error = 1
  }
  if (document.formulari["repassword"].value != document.formulari["password"].value && !error){
    alert("Las contraseñas no coinciden")
    document.formulari["repassword"].focus()
    error = 1
  }
  
	if (document.formulari["email"].value == "" && !error){
    alert("Debes introducir una dirección de contacto")
    document.formulari["email"].focus()
    error = 1
  	}
	if (document.formulari["email"].value != "" && !error) {
		if ( !error ){
			var emailPat=/^(.+)@(.+)$/
	  	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
 		  var validChars="\[^\\s" + specialChars + "\]"
      var quotedUser="(\"[^\"]*\")"
      var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
      var atom=validChars + '+'
      var word="(" + atom + "|" + quotedUser + ")"
      var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
      var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
      var matchArray=document.formulari["email"].value.match(emailPat)
      if (matchArray==null && !error) {
          alert("Dirección de contacto incorrecta")
          document.formulari["email"].focus()
          error = 1
      }
      if ( !error ){
        var user=matchArray[1]
        var domain=matchArray[2]

        if (user.match(userPat)==null && !error) {
            alert("Dirección de contacto incorrecta")
            document.formulari["email"].focus()
            error = 1
        }
        if ( !error ){
          var IPArray=domain.match(ipDomainPat)
          if (IPArray!=null && !error) {
              for (var i=1;i<=4;i++) {
                  if (IPArray[i]>255) {
                      alert("Dirección de contacto incorrecta")
                      document.formulari["email"].focus()
                      error = 1
                      break
                  }
              }
          }
          if ( !error ){
            var domainArray=domain.match(domainPat)
            if (domainArray==null && !error) {
                alert("Dirección de contacto incorrecta")
                document.formulari["email"].focus()
                error = 1
            }

            if ( !error ){
              var atomPat=new RegExp(atom,"g")
              var domArr=domain.match(atomPat)
              var len=domArr.length
              if ((domArr[domArr.length-1].length<2 ||
                  domArr[domArr.length-1].length>3 ) && !error) {
                  alert("Dirección de contacto incorrecta")
                  document.formulari["email"].focus()
                  error = 1
              }
              if (len<2 && !error) {
                 document.formulari["email"].focus()
                 alert("Dirección de contacto incorrecta")
                  error = 1
              }
            }
          }
        }
      }
  	}
	}
	if (document.formulari["nom"].value == "" && !error){
    alert("Debes introducir un Nombre")
    document.formulari["nom"].focus()
    error = 1
  }
  if (document.formulari["cognoms"].value == "" && !error){
    alert("Debes introducir los Apellidos")
    document.formulari["cognoms"].focus()
    error = 1
  }
  if (document.formulari["code"].value == "" && !error){
    alert("Debes introducir la Palabra de control")
    document.formulari["code"].focus()
    error = 1
  }
	if ( !error ){
  	return true
	}
	else return false
}

function enviamail(usuari,domini)
{
   mailurl = "mailto:" + usuari + "@" + domini;
   window.location = mailurl;
}

