// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 8;
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('inlinemsg')) {
    msg = document.createElement('div');
    msg.id = 'inlinemsg';
    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('inlinemsg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.select();
  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;
  }
  //ScrollTo(target);
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('inlinemsg');
  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('inlinemsg');
  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 = "/_ezfiles/img/msg_arrow.gif";
}
function valida(){
  var fadetime='3';
  var form=$('contactForm');
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  
  var cnomereq=$('cnomereq').value;
  var ccognomereq=$('ccognomereq').value;
  var cemailreq=$('cemailreq').value;
  var crichiestareq=$('crichiestareq').value;
  if (typeof($('ccaptchareq').value) != 'undefined'){var ccaptchareq=$('ccaptchareq').value;}else{var ccaptchareq='no';}
  if (form.cnome.value == "") {
    form.cnome.focus();
    inlineMsg('cnome',cnomereq,fadetime);
    return false ;
  }
  if (form.ccognome.value == "") {
    form.ccognome.focus();
    inlineMsg('ccognome',ccognomereq,fadetime);
    return false ;
  }
  if(form.cemail.value == ""){
    form.cemail.focus();
    inlineMsg('cemail',cemailreq,fadetime);
    return false ;
  } 
    if(!form.cemail.value.match(emailRegex)) {
      form.cemail.focus();
    inlineMsg('cemail',cemailreq,fadetime);
      return false ;
    }  
  if (form.crichiesta.value == "") {
    form.crichiesta.focus();
    inlineMsg('crichiesta',crichiestareq,fadetime);
    return false ;
  }
  if (form.captcha.value == "" && ccaptchareq !='no') {
    form.captcha.focus();
    inlineMsg('captcha',ccaptchareq,fadetime);
    return false ;
  }
  return true;
}

function nascondireq(name,req) {
  var form=$('contactForm');
  if (form[name].value != "") {
	new Effect.Fade(req);   
    return false ;
  }
} 

function auth1(){
  var invia;
  var form=$('userForm');
  if (form.username.value == "") {
    form.username.focus();
	  new Effect.Pulsate('usernameUf',{duration:1.2, from:0.6, pulses:2});   
    return false ;
    //invia = "no";
  }
  if (form.passwd.value == "") {
    form.passwd.focus();
	  new Effect.Pulsate('passwdUf',{duration:1.2, from:0.6, pulses:2});   
    return false ;
    //invia = "no";
  }
  return true;
}
function auth2(){
  var form=$('adminForm');
  if (form.username.value == "") {
    form.username.focus();
	  new Effect.Pulsate('usernameAdm',{duration:1.2, from:0.6, pulses:2});   
    return false ;
    //invia = "no";
  }
  if (form.passwd.value == "") {
    form.passwd.focus();
	  new Effect.Pulsate('passwdAdm',{duration:1.2, from:0.6, pulses:2});   
    return false ;
    //invia = "no";
  }
  return true;
}

/* newsletter */
function validanl(){
  var form=$('nlForm');
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  if (form.nome.value == "") {
    form.nome.focus();
	  new Effect.Appear('nomereq'); 
	  //new Effect.Pulsate('cnomereq',{duration:1.2, from:0.6, pulses:2});   
    return false ;
  }
  if (form.cognome.value == "") {
    form.cognome.focus();
	  new Effect.Appear('cognomereq'); 
    return false ;
  }
  if(form.email.value == ""){
    form.email.focus();
  	new Effect.Appear('emailreq'); 
    return false ;
  } 
    if(!form.email.value.match(emailRegex)) {
      form.email.focus();
  	  new Effect.Appear('emailreq'); 
      return false ;
    }  
  if(form.email.value != form.email2.value){
    form.email2.focus();
  	new Effect.Appear('emailreq2'); 
    return false ;
  } 
  return true;
}

function hidereq(name,req,formname) {
  var form=$(formname);
  if (form[name].value != "") {
	new Effect.Fade(req);   
    return false ;
  }
} 

function isNumeric(strString)
   //  check for valid numeric strings 
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
/*
function alphanumeric(alphane){
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123)){
		  }else{
			 return false;
		  }
 		}
 return true;
}

*/
function calbox(month,year){
  	var pars = 'month='+month+'&year='+year;
  	new Ajax.Updater(
  		'calbox',
  		'/_ezfiles/calbox.php', {
      method: 'get',
  		parameters: pars,
  		//onLoading:function(){
      //$('calbox').update('attendere')
  		//},
  		asynchronous:true, evalScripts:true
  		}
  	);
}

document.write('<script type="text/javascript" src="/jscripts/f2.js"></script>');