/* -------------------------------------------------------
   NAME             :   Validazione Giglio Presents
   AUTHOR           :  	Francesco Bianchi
   VERSION          :   1.0.0.0
   LAST DATE MOD    :   10 Maggio 2011

 --------------------------------------------------------- */

// funzioni di validazione form //

function validate_iscritto(form) {
  var email = form.email.value;
  var emailRegex = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
  var password = form.password.value;
  var password2 = form.password2.value;
  var tipologia = form.tipologia.value;
  var ragione_sociale = form.ragione_sociale.value;
  var p_iva = form.p_iva.value;
  var cod_fiscale = form.cod_fiscale.value;
  var indirizzo = form.indirizzo.value;
  var citta = form.citta.value;
  var cap = form.cap.value;
  var provincia = form.provincia.value;
  var nazione = form.nazione.value;
  var telefono = form.telefono.value;

  if(email == "") {
    inlineMsg('email','Inserisci l\'indirizzo email',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','Inserisci un indirizzo email corretto',2);
    return false;
  }
  if(password == "") {
    inlineMsg('password','Inserisci la password',2);
    return false;
  }
  if(password2 == "") {
    inlineMsg('password2','Ripetere la password',2);
    return false;
  }
  if(password2 != password) {
    inlineMsg('password2','Le password devono essere uguali',2);
    return false;
  }
  if(tipologia == "") {
    inlineMsg('tipologia','Seleziona la tipologia',2);
    return false;
  }
  if(ragione_sociale == "") {
    inlineMsg('ragione_sociale','Inserisci la ragione sociale',2);
    return false;
  }
  if(tipologia=="priv"){
      if(cod_fiscale == "") {
        inlineMsg('cod_fiscale','Inserisci il codice fiscale',2);
        return false;
      }
  }
  if(tipologia=="rive"){
      if(p_iva == "") {
        inlineMsg('p_iva','Inserisci la partita iva',2);
        return false;
      }
  }
  if(indirizzo == "") {
    inlineMsg('indirizzo','Inserisci l\'indirizzo',2);
    return false;
  }
  if(citta == "") {
    inlineMsg('citta','Inserisci la citta\'',2);
    return false;
  }
  if(cap == "") {
    inlineMsg('cap','Inserisci il cap',2);
    return false;
  }
  if(provincia == "") {
    inlineMsg('provincia','Inserisci la provincia',2);
    return false;
  }
  if(nazione == "") {
    inlineMsg('nazione','Inserisci la nazione',2);
    return false;
  }
  if(telefono == "") {
    inlineMsg('telefono','Inserisci il telefono',2);
    return false;
  }
  var pri = 0;
    for (i=0; i<form.elements.length; i++) {
        if (form.elements[i].type == "checkbox" && form.elements[i].name == "privacy") {
                if (form.elements[i].checked)
                        pri++;
        }
    }
  if(!pri){
      alert('Devi accettare la privacy');
      return false;
  }

  return true;
}

function validate_mod_iscritto(form) {
  var email = form.email.value;
  var emailRegex = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
  var tipologia = form.tipologia.value;
  var password = form.password.value;
  var password2 = form.password2.value;
  var ragione_sociale = form.ragione_sociale.value;
  var p_iva = form.p_iva.value;
  var cod_fiscale = form.cod_fiscale.value;
  var indirizzo = form.indirizzo.value;
  var citta = form.citta.value;
  var cap = form.cap.value;
  var provincia = form.provincia.value;
  var nazione = form.nazione.value;
  var telefono = form.telefono.value;

  if(email == "") {
    inlineMsg('email','Inserisci l\'indirizzo email',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','Inserisci un indirizzo email corretto',2);
    return false;
  }
  if(password!=""){
      if(password2 == "") {
        inlineMsg('password2','Ripetere la password',2);
        return false;
      }
      if(password2 != password) {
        inlineMsg('password2','Le password devono essere uguali',2);
        return false;
      }
  }
  if(tipologia == "") {
    inlineMsg('tipologia','Seleziona la tipologia',2);
    return false;
  }
  if(ragione_sociale == "") {
    inlineMsg('ragione_sociale','Inserisci la ragione sociale',2);
    return false;
  }
  if(tipologia=="priv"){
      if(cod_fiscale == "") {
        inlineMsg('cod_fiscale','Inserisci il codice fiscale',2);
        return false;
      }
  }
  if(tipologia=="rive"){
      if(p_iva == "") {
        inlineMsg('p_iva','Inserisci la partita iva',2);
        return false;
      }
  }
  if(indirizzo == "") {
    inlineMsg('indirizzo','Inserisci l\'indirizzo',2);
    return false;
  }
  if(citta == "") {
    inlineMsg('citta','Inserisci la citta\'',2);
    return false;
  }
  if(cap == "") {
    inlineMsg('cap','Inserisci il cap',2);
    return false;
  }
  if(provincia == "") {
    inlineMsg('provincia','Inserisci la provincia',2);
    return false;
  }
  if(nazione == "") {
    inlineMsg('nazione','Inserisci la nazione',2);
    return false;
  }
  if(telefono == "") {
    inlineMsg('telefono','Inserisci il telefono',2);
    return false;
  }

  return true;
}

function validate_prodotto(form) {
  var nome = form.nome_it.value;
//  var desc1 = form.desc1_it.value;
//  var desc2 = form.desc2_it.value;
  var prezzo = form.prezzo.value;
  var disponibilita = form.disponibilita.value;
  var is_img = form.is_img.value;

  if(nome == "") {
    inlineMsg('nome_it','Inserisci il nome',2);
    return false;
  }
  
  var lin = 0;
    for (i=0; i<form.elements.length; i++) {
        if (form.elements[i].type == "radio" && form.elements[i].name == "linea") {
                if (form.elements[i].checked)
                        lin++;
        }
    }

  if(!lin){
      alert('Seleziona una linea');
      return false;
  }
  
  if(prezzo == "") {
    inlineMsg('prezzo','Inserisci il prezzo',2);
    return false;
  }
  if(isNaN(prezzo) || parseInt(prezzo)<0) {
    inlineMsg('prezzo','Prezzo deve essere un numero maggiore di 0',2);
    return false;
  }
  if(disponibilita == "") {
    inlineMsg('disponibilita','Inserisci la disponibilita\'',2);
    return false;
  }
  if(isNaN(disponibilita) || parseInt(disponibilita)<0) {
    inlineMsg('disponibilita','Disponibilita\' deve essere un numero maggiore di 0',2);
    return false;
  }
  if(is_img == "no") {
    alert('Inserisci l\'immagine');
    return false;
  }


  return true;
}

// SCRIPT DEL MESSAGGIO //

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 * 4000));
}

// 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 = "msg_arrow.gif";
}


