// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
        (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    now = now.toGMTString();
    if (f.author != undefined)
       setCookie('mtcmtauth', f.author.value, now, '/', '', '');
    if (f.email != undefined)
       setCookie('mtcmtmail', f.email.value, now, '/', '', '');
    if (f.url != undefined)
       setCookie('mtcmthome', f.url.value, now, '/', '', '');
}

function rememberMeDownload (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    now = now.toGMTString();
    if (f.email!= undefined)
       setCookie('demail', f.email.value, now, '/', '', '');
    if (f.nome != undefined)
       setCookie('dnome', f.nome.value, now, '/', '', '');
    if (f.cognome != undefined)
       setCookie('dcognome', f.cognome.value, now, '/', '', '');
    if (f.societa != undefined)
       setCookie('dsocieta', f.societa.value, now, '/', '', '');
    if (f.professione != undefined)
       setCookie('dprofessione', f.professione.value, now, '/', '', '');
    if (f.telefono != undefined)
       setCookie('dtelefono', f.telefono.value, now, '/', '', '');
    if (f.ragione_sociale != undefined)
       setCookie('dragione_sociale', f.ragione_sociale.value, now, '/', '', '');
    if (f.piva != undefined)
       setCookie('dpiva', f.piva.value, now, '/', '', '');
    if (f.cf != undefined)
       setCookie('dcf', f.cf.value, now, '/', '', '');
    if (f.indirizzo != undefined)
       setCookie('dindirizzo', f.indirizzo.value, now, '/', '', '');
    if (f.citta != undefined)
       setCookie('dcitta', f.citta.value, now, '/', '', '');
    if (f.cap != undefined)
       setCookie('dcap', f.cap.value, now, '/', '', '');
    if (f.provincia != undefined)
       setCookie('dprovincia', f.provincia.value, now, '/', '', '');
    if (f.nazione != undefined)
       setCookie('dnazione', f.nazione.value, now, '/', '', '');
    if (f.fax != undefined)
       setCookie('dfax', f.fax.value, now, '/', '', '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '/', '');
    deleteCookie('mtcmthome', '/', '');
    deleteCookie('mtcmtauth', '/', '');
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

function hideDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'none';
}

function showDocumentElement(id) {
    var el = document.getElementById(id);
    if (el) el.style.display = 'block';
}

var commenter_name;

function individualArchivesOnLoad(commenter_name) {


    hideDocumentElement('trackbacks-info');



    if (document.comments_form) {
        if (document.comments_form.email != undefined &&
            (mtcmtmail = getCookie("mtcmtmail")))
            document.comments_form.email.value = mtcmtmail;
        if (document.comments_form.author != undefined &&
            (mtcmtauth = getCookie("mtcmtauth")))
            document.comments_form.author.value = mtcmtauth;
        if (document.comments_form.url != undefined && 
            (mtcmthome = getCookie("mtcmthome")))
            document.comments_form.url.value = mtcmthome;
        if (mtcmtauth || mtcmthome) {
            document.comments_form.bakecookie.checked = true;
        } else {
            document.comments_form.bakecookie.checked = false;
        }
    }
}

function writeTypeKeyGreeting(commenter_name, entry_id) {

}



function 
downloadOnLoad() 
    {
    if (document.registration) 
        {
        if (document.registration.email != undefined &&
            (demail = getCookie("demail")))
            document.registration.email.value = demail;
        if (document.registration.nome != undefined &&
            (dnome = getCookie("dnome")))
            document.registration.nome.value = dnome;
        if (document.registration.cognome != undefined && 
            (dcognome = getCookie("dcognome")))
            document.registration.cognome.value = dcognome;
        if (document.registration.societa != undefined && 
            (dsocieta = getCookie("dsocieta")))
            document.registration.societa.value = dsocieta;
        if (document.registration.professione != undefined && 
            (dprofessione = getCookie("dprofessione")))
            document.registration.professione.value = dprofessione;
        if (document.registration.telefono != undefined && 
            (dtelefono = getCookie("dtelefono")))
            document.registration.telefono.value = dtelefono;
        if (document.registration.ragione_sociale != undefined && 
            (dragione_sociale = getCookie("dragione_sociale")))
            document.registration.ragione_sociale.value = dragione_sociale;
        if (document.registration.piva != undefined && 
            (dpiva = getCookie("dpiva")))
            document.registration.piva.value = dpiva;
        if (document.registration.cf != undefined && 
            (dcf = getCookie("dcf")))
            document.registration.cf.value = dcf;
        if (document.registration.indirizzo != undefined && 
            (dindirizzo = getCookie("dindirizzo")))
            document.registration.indirizzo.value = dindirizzo;
        if (document.registration.citta != undefined && 
            (dcitta = getCookie("dcitta")))
            document.registration.citta.value = dcitta;
        if (document.registration.cap != undefined && 
            (dcap = getCookie("dcap")))
            document.registration.cap.value = dcap;
        if (document.registration.provincia != undefined && 
            (dprovincia = getCookie("dprovincia")))
            document.registration.provincia.value = dprovincia;
        if (document.registration.nazione != undefined && 
            (dnazione = getCookie("dnazione")))
            document.registration.nazione.value = dnazione;
        if (document.registration.fax != undefined && 
            (dfax = getCookie("dfax")))
            document.registration.fax.value = dfax;
        }
    }

function 
CountWords(this_field, max) 
   {
	if(this_field.value == "")
		return;
   var fullStr = this_field.value + " ";
   var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
   var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
   var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
   var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
   var splitString = cleanedStr.split(" ");
   var word_count = splitString.length -1;
   if (word_count < max)
      {
	  return false;
	  }
	return true;
   }

function indirizzoEmailValido(indirizzo) {
  if (window.RegExp) {
    var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    var regnv = new RegExp(nonvalido);
    var regv = new RegExp(valido);
    if (!regnv.test(indirizzo) && regv.test(indirizzo))
      return true;
    return false;
	}
  else {
    if(indirizzo.indexOf("@") >= 0)
      return true;
    return false;
  	}
  }
function 
controllaDatiAmico(modulo) 
   {
   if (!indirizzoEmailValido(modulo.to.value)) 
      {
      modulo.to.select();
      alert("Si prega di inserire un indirizzo email valido");
      return false;
      }
   else  if (!indirizzoEmailValido(modulo.from.value)) 
      {
      modulo.from.select();
      alert("Si prega di inserire un indirizzo email valido");
      return false;
      }
   else
      return true;
   }
function 
controllaDatiDownload(modulo) 
   {
   if(!indirizzoEmailValido(modulo.email.value)) 
      {
      modulo.email.select();
      alert("Si prega di inserire un indirizzo email valido");
      return false;
      }

   if(!modulo.privacy.checked)
      {
      modulo.privacy.select();
      alert("Si prega di dare l'ok per la privacy.");
      return false;
      }

   if(modulo.webtime.checked)
      {
      document.lirys.email.value = modulo.email.value;
      document.lirys.submit();
      alert("Iscrizione a webTime effettuata.");
      }
   return true;
   }

function
controllaDatiPagamento(modulo) 
   {
   if(!indirizzoEmailValido(modulo.email.value)) 
      {
      modulo.email.select();
      alert("Si prega di inserire un indirizzo email valido");
      return false;
      }
   if(modulo.email.value != modulo.email_conf.value) 
      {
      modulo.email_conf.select();
      alert("Si prega di reinserire correttamente la mail");
      return false;
      }
   if(modulo.nome.value == "") 
      {
      modulo.nome.select();
      alert("Si prega di inserire il nome.");
      return false;
      }
   if(modulo.cognome.value == "") 
      {
      modulo.cognome.select();
      alert("Si prega di inserire il cognome.");
      return false;
      }
   if(modulo.piva.value == "") 
      {
      modulo.piva.select();
      alert("Si prega di inserire la partita iva.");
      return false;
      }
   if(modulo.cf.value == "") 
      {
      modulo.cf.select();
      alert("Si prega di inserire il codice fiscale.");
      return false;
      }
   if(modulo.indirizzo.value == "") 
      {
      modulo.indirizzo.select();
      alert("Si prega di inserire l' indirizzo.");
      return false;
      }
   if(modulo.citta.value == "") 
      {
      modulo.citta.select();
      alert("Si prega di inserire la citta'.");
      return false;
      }
   if(modulo.cap.value == "") 
      {
      modulo.cap.select();
      alert("Si prega di inserire il cap.");
      return false;
      }
   if(modulo.provincia.value == "") 
      {
      modulo.provincia.select();
      alert("Si prega di inserire la provincia.");
      return false;
      }
   if(modulo.nazione.value == "") 
      {
      modulo.nazione.select();
      alert("Si prega di inserire la nazione.");
      return false;
      }
   if(modulo.telefono.value == "") 
      {
      modulo.telefono.select();
      alert("Si prega di inserire il telefono.");
      return false;
      }

   if(!modulo.privacy.checked)
      {
      modulo.privacy.select();
      alert("Si prega di dare l'ok per la privacy.");
      return false;
      }

   if(modulo.webtime.checked)
      {
      document.lirys.email.value = modulo.email.value;
      document.lirys.submit();
      alert("Iscrizione a webTime effettuata.");
      }
   return true;
   }

function 
controllaDatiCommento(modulo) 
	{
	if(!CountWords(modulo.author, 2)) 
		{
 		modulo.author.focus();
		alert("Inserisci il tuo nome e il tuo cognome");
		return false;			
		}
	 if (!indirizzoEmailValido(modulo.email.value)) 
		{
		modulo.email.select();
		alert("Si prega di inserire un indirizzo email valido");
		return false;
		 }
	 if (modulo.text.value.length < 1) 
		{
		modulo.text.select();
		alert("Inserire il commento");
		return false;
		 }
	return true;
	}
function 
controllaMail(modulo) 
	{
	 if (!indirizzoEmailValido(modulo.email.value)) 
		{
		modulo.email.select();
		alert("Si prega di inserire un indirizzo email valido");
		return false;
		 }
	return true;
	}
function
controllaPrivacy(modulo)
	{
	if (!indirizzoEmailValido(modulo.email.value)) 
		{
		modulo.email.select();
		alert("Si prega di inserire un indirizzo email valido");
		return false;
		}
	if(!modulo.privacy.checked)
		{
		alert("Prendere visione della nota informativa sulla privacy.");
		return false;
		}
	if(modulo.list_webtime && modulo.list_webtime.checked)
		{
		document.lirys.email.value = modulo.email.value;
		document.lirys.submit();
		alert("Iscrizione a webTime effettuata.");
		}
	if(modulo.list_notifica && modulo.list_notifica.checked)
		{
		document.lirys_ca.email.value = modulo.email.value;
		document.lirys_ca.submit();
		alert("Iscrizione agli aggiornamenti del sito effettuata.");
		}
	return true;
	}
/*--------------------------------------------------------------------*/
/*----------------------------- AJAX ---------------------------------*/
/*--------------------------------------------------------------------*/

var myRequest = null;
var myDiv = null;

function 
CreateXmlHttpReq2(handler) 
 {
 var xmlhttp = null;
 try 
  {
  xmlhttp = new XMLHttpRequest();
  } 
 catch(e) 
  {
  try 
   {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } 
  catch(e) 
   {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
  }
 xmlhttp.onreadystatechange = handler;
 return xmlhttp;
 } // CreateXmlHttpReq2
function 
myHandler2(the_div) 
 {
    if(myRequest.readyState == 4 && myRequest.status == 200) 
  {
        e = document.getElementById(myDiv);
//alert(myRequest.responseText);
        e.innerHTML = myRequest.responseText;
  }
 }
function 
get_cont(url, the_div) 
 {
 myDiv = the_div;
 myRequest = CreateXmlHttpReq2(myHandler2);
 myRequest.open("GET", url);
 myRequest.send(null);
 }

