function validate_registration(form)
{
  var name = form.firstname.value;
  var surname1 = form.surname1.value;
  var surname2 = form.surname2.value;
  var institution = form.institution.value;
  var position = form.position.value;
  var username = form.username.value;
  var email = form.email.value;
  var password = form.password.value;
  var password2 = form.password2.value;
  var nameRegex = /^[A-Za-z0-9_\-]{5,30}$/;
  var passwordRegex = /^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  
  if(name == "") {
    inlineMsg('firstname','Introduzca su nombre.',2);
	return false;
  }
  if(surname1 == "") {
    inlineMsg('surname1','Introduzca su primer apellido.',2);
    return false;
  }
  if(surname2 == "") {
    inlineMsg('surname2','Introduzca su segundo apellido.',2);
    return false;
  }
  if(institution == "") {
    inlineMsg('institution','Introduzca su instituci&oacute;n o compa&ntilde;a.',2);
    return false;
  }
   if(position == "") {
    inlineMsg('position','Introduzca su posici&oacute;n.',2);
    return false;
   }
  if(username == "") {
    inlineMsg('username','Introduzca su usuario.',2);
    return false;
  }
  if(!username.match(nameRegex)) {
    inlineMsg('username','Usuario de 5 caracteres al menos, s&oacute;lo letras, n&uacute;meros y guiones medio y bajo.',2);
    return false;
  }
  if(password == "") {
    inlineMsg('password','Introduzca el Password.',2);
    return false;
  }
   if(!password.match(passwordRegex)) {
    inlineMsg('password','Password de 8 caracteres m&iacute;nimo, una letra min&uacute;scula, una may&uacute;scula y un d&iacute;gito.',5);
    return false;
  }
  if(password2 != password) {
    inlineMsg('password2','Los passwords no coinciden.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','Introduzca su correo electr&oacute;nico.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','Email no v&aacute;lido.',2);
    return false;
  }
  return true;
}

function validate_login(form)
{
  var username = form.username.value;
  var password = form.password.value;
  var nameRegex = /^[A-Za-z0-9_\-]{5,}$/;
  var passwordRegex = /^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/;
 
  
  if(username == "") {
    inlineMsg('username','Introduzca su usuario.',2);
    return false;
  }
  if(!username.match(nameRegex)) {
    inlineMsg('username','Usuario incorrecto.',2);
    return false;
  }
  if(password == "") {
    inlineMsg('password','Introduzca el password.',2);
    return false;
  }
   if(!password.match(passwordRegex)) {
    inlineMsg('password','Password incorrecto.',2);
    return false;
  }
  return true;
}

function validate_lostpassword(form) {
  var username = form.username.value;
  var email = form.email.value;
  var nameRegex = /^[A-Za-z0-9_\-]{5,}$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
 
  
  if(username == "") {
    inlineMsg('username','Introduzca su usuario.',2);
    return false;
  }
  if(!username.match(nameRegex)) {
    inlineMsg('username','Usuario incorrecto.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','Introduzca su correo electr&oacute;nico.',2);
    return false;
  }
   if(!email.match(emailRegex)) {
    inlineMsg('email','Email incorrecto.',2);
    return false;
  }
  return true;
}

function validate_changepassword(form) {
  var username = form.username.value;
  var oldpassword = form.oldpassword.value;
  var password = form.password.value;
  var password2 = form.password2.value;
  var nameRegex = /^[A-Za-z0-9_\-]{5,}$/;
  var passwordRegex = /^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$/;
  
  if(oldpassword == "") {
    inlineMsg('oldpassword','Introduzca el password actual.',2);
    return false;
  }
   if(!oldpassword.match(passwordRegex)) {
    inlineMsg('oldpassword','Password incorrecto.',2);
    return false;
  }
  if(password == "") {
    inlineMsg('password','Introduzca el nuevo password.',2);
    return false;
  }
   if(!password.match(passwordRegex)) {
    inlineMsg('password','Password incorrecto.',2);
    return false;
  }
  if(password2 == "") {
    inlineMsg('password2','Repita el nuevo password.',2);
    return false;
  }
   if(!password2.match(passwordRegex)) {
    inlineMsg('password2','Password incorrecto.',2);
    return false;
  }
   if(password2 != password) {
    inlineMsg('password2','Los passwords no coinciden.',2);
    return false;
  }
  return true;
}


// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,140); 
  arrow.src = "images/msg_arrow.gif"; 
}