/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Michael Leigeber | http://www.leigeber.com/Licensed under: U.S. Copyright
 */

function get_estensione(path) {
    posizione_punto=path.lastIndexOf(".");
	lunghezza_stringa=path.length;
	estensione=path.substring(posizione_punto+1,lunghezza_stringa);
	return estensione;
}

function controlla_estensione(path){
	if(get_estensione(path)!="pdf" && get_estensione(path)!="doc" && get_estensione(path)!="docx" && get_estensione(path)!="odt"){
		return false;
	}
	else return true;
}

// form validation function //
function validate_curricula(form) {

  var name = form.nome.value;
  var cognome = form.cognome.value;
  var regione = form.regione.value;
  var provincia = form.provincia.value;
  var comune = form.comune.value;
  var email = form.email.value;
  var allegato = form.curriculum.value;
  var datanascita = form.datanascita.value;
  //var profilo = form.profilo.value;
  //var message = form.messaggio.value;
  var privacy = form.privacy.checked;
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  //var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  
  if(cognome == "") {
    inlineMsg_cur('cognome','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci il tuo cognome.</span>',2);
	return false;
  }
  if(!cognome.match(nameRegex)) {
    inlineMsg_cur('cognome','<span class="testo_interno"><strong>Attenzione</strong><br />Hai inserito un cognome non valido.</span>',2);
    return false;
  }
  
  if(name == "") {
    inlineMsg_cur('nome','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci il tuo nome.</span>',2);
    return false;
  }
  if(!name.match(nameRegex)) {
    inlineMsg_cur('nome','<span class="testo_interno"><strong>Attenzione</strong><br />Hai inserito un nome non valido.</span>',2);
    return false;
  }
  
  if(regione == "") {
    inlineMsg_cur('regione','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci la regione.</span>',2);
    return false;
  }
  
  if(provincia == "") {
    inlineMsg_cur('provincia','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci la provincia.</span>',2);
    return false;
  }
  
  if(comune == "") {
    inlineMsg_cur('comune','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci il comune.</span>',2);
    return false;
  }
  
  if(datanascita == ""){
	  inlineMsg_cur('datanascita','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci la data di nascita.</span>');
    return false;
  }
  
  if(email == "") {
    inlineMsg_cur('email','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci la tua mail.</span>',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg_cur('email','<span class="testo_interno"><strong>Attenzione</strong><br />Hai inserito una mail non valida.</span>',2);
    return false;
  }
  
 /* if(profilo == ""){
	  inlineMsg_cur('profilo','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci un profilo professionale.</span>'),2;
    return false;
  }*/

 /* if(message == "") {
    inlineMsg_cur('messaggio','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci un messaggio.</span>');
    return false;
  }
  if(message.match(messageRegex)) {
    inlineMsg_cur('messaggio','<span class="testo_interno"><strong>Attenzione</strong><br /><br />Hai inserito un messaggio non valido.</span>');
    return false;
  }*/
  
    
  if(!allegato){
	  inlineMsg_cur('curriculum','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci un allegato.</span>',2);
    return false;
  }
  
  if(!controlla_estensione(allegato)){
	  inlineMsg_cur('curriculum','<span class="testo_interno"><strong>Attenzione</strong><br />Inserisci un formato valido.</span>',2);
    return false;
  }
  
  if(!privacy){
	  inlineMsg_cur('privacy','<span class="testo_interno"><strong>Attenzione</strong><br />Non hai accettato le condizioni.</span>',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_cur(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 = "msg_arrow.gif";
}



