function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// form validation login//

function validatelog(form_login) {

  var username = form_login.username.value;
  var password = form_login.password.value;
  
  if(username == "") {
    inlineMsg('username','<b>Inserisci</b> il campo Username',2);
    return false;
  }
  
  if(password == "") {
    inlineMsg('password','<b>Inserisci</b> il campo Password',2);
    return false;
  }

}

function validate_reset(form) {
 var password_reset = form.password_reset.value;
  var passwordconf_reset = form.passwordconf_reset.value;
  
  if(passwordconf_reset == "") {
    inlineMsg('passwordconf_reset','<b>Inserisci</b> il campo Conferma Password',2);
    return false;
  }
  
   if(passwordconf_reset != password_reset) {
	inlineMsg('passwordconf_reset','Le password non corrispondono',2);
     return false;
  }
  
 
  
}

// form validation function //
function validate(form) {

  var username = form.username.value;
  var password = form.password.value;
  var passwordconf = form.passwordconf.value;
  var email = form.email.value;
  /*var check = form.check.checked;*/
  var length = form.password.value.length;
  var nome = form.nome.value;
  var cognome = form.cognome.value;
  var provincia = form.provincia.value;
  var citta = form.citta.value;
  var cap = form.cap.value;
  var telefono = form.telefono.value;
  var check = getCheckedValue(form.check);
//  var gender = form.gender.value;//
//  var message = form.message.value;//
  var telRegex = /^([a-zA-z]{0}|[0-9]{8,10})?$/;
  var telSpaceRegex = /\W/;
  var capRegex = /^([a-zA-z]{0}|[0-9]{5})?$/;
  var textFieldsRegex = /^([a-zA-z]{0}|[a-zA-z\s]{3,})?$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  
//  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);//




 if(username == "") {
    inlineMsg('username','<b>Inserisci</b> il campo Username',2);
    return false;
  }
  
  
  if(email == "") {
    inlineMsg('email','<b>Inserisci</b> il campo Email',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<b>Email</b> non corretta',2);
    return false;
  }
  
  if(password == "") {
    inlineMsg('password','<b>Inserisci</b> il campo Password',2);
    return false;
  }
  
    if(length <6) {
    inlineMsg('password','La <b>Password</b> deve contenere almeno 6 caratteri',2);
    return false;
  }
  
  if(passwordconf == "") {
    inlineMsg('passwordconf','<b>Inserisci</b> il campo Conferma Password',2);
    return false;
  }
  
   if(passwordconf != password) {
	inlineMsg('passwordconf','Le password non corrispondono',2);
     return false;
  }
    
  
  
  if(check == "") {
    inlineMsg('check','E\' necessario accettare le Privacy Policy',2);
    return false;
  }
 // CAMPI FACOLTATIVI, Check se è empty passa sennò verifica regex
  
  if (!nome.match(textFieldsRegex)) {
	  inlineMsg('nome','<b>Assicurarsi</b> che queto campo sia corretto',2);
	  return false;
  }
  if (!cognome.match(textFieldsRegex)) {
	  inlineMsg('cognome','<b>Assicurarsi</b> che queto campo sia corretto',2);
	  return false;
  }
  if (!provincia.match(textFieldsRegex)) {
	  inlineMsg('provincia','<b>Assicurarsi</b> che queto campo sia corretto',2);
	  return false;
  }
  if (!citta.match(textFieldsRegex)) {
	  inlineMsg('citta','<b>Assicurarsi</b> che queto campo sia corretto',2);
	  return false;
  }
  if (!cap.match(capRegex)) {
	  inlineMsg('cap','<b>Assicurarsi</b> che queto campo sia corretto',2);
	  return false;
  }
  	if (telefono.match(telSpaceRegex)) {
  		inlineMsg('telefono','<b>Inserire il numero</b> tutto attaccato senza simboli o spazi',3);
  		return false;
  	}
  if (!telefono.match(telRegex)) {
	  inlineMsg('telefono','<b>Assicurarsi</b> che queto campo sia corretto',2);
	  return false;
  }
	  
  /*if(check != true) {
	inlineMsg('check','<b>Acetta</b> i termini della Privacy Policy',2);
     return false;
  }*/
  
}

// 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,80); 
  arrow.src = "images/arrowForm.gif"; 
}

