//An AJAX library coded by davidonzo <http://www.davidonzo.com>
//This part of code is released under the GNU / GPL v3
//You are free to use, edit and redistribuite it under the terms of the GNU / GPL license

var url = 'http://www.camisanicalzolari.com/italiadati/';

var myReq = null;
function AJAXReq(methodtype,url,asynch,resp){
 if(window.XMLHttpRequest){
  myReq = new XMLHttpRequest();
 }else if(window.ActiveXObject){
   myReq = new ActiveXObject("Msxml2.XMLHTTP");
    if(!myReq){
      myReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
 }
 
 if(myReq){
  if(methodtype.toLowerCase() != "post"){
    execfunc(methodtype,url,asynch,resp);
  }else{
    var args = arguments[4];
    execfunc(methodtype,url,asynch,resp,args);
  }
 }else{
   alert("Your browser doesn't support AJAX utilities");
 }
}

function execfunc(methodtype,url,asynch,resp){
  try{
    myReq.onreadystatechange = resp;
    myReq.open(methodtype,url,asynch);
    
    if(methodtype.toLowerCase() == "post"){
      myReq.setRequestHeader("Content-Type",
                             "application/x-www-form-urlencoded; charset=UTF-8");
      myReq.send(arguments[4]);
    }else{
      myReq.send(null);
    }
  }catch(errv){
    alert("Enable to contact the server\nError: "+errv.message);
  }
}

function PreparaDati(nome){
  stringa = "";
  var form = document.forms[nome];
  var numeroElementi = form.elements.length;
 
  for(var i = 0; i < numeroElementi; i++){
    if(i < numeroElementi-1){
      stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
    }else{
      stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);
    } 
  }
  //document.write("E" + stringa);
}

function loader(id,text,img){
  document.getElementById(id).style.display = 'block';
  document.getElementById(id).style.visibility = 'visible';
  document.getElementById(id).innerHTML = '<div id="loader" align="center">'+text+'<img alt="loader" src="'+img+'" /></div>';
}

function disable(what,attr,gen){
  w = document.getElementById(what);
  w.setAttribute('disabled', 'disabled');
}

function enable(what){
  w = document.getElementById(what);
  w.removeAttribute('disabled');
}

function opacity_IN(id,op,duration){
 e = document.getElementById(id);
 e.style.display = 'block';
 e.style.visibility = 'visible';
 
 if(IEdetect()){
   e.style.zoom = 1;
   e.style.filter='alpha(opacity='+op*10+')';
 }else{
   e.style.opacity = op/10;
 }
 op++;
 temp = duration/10;
 if(op <= 10){
  setTimeout("opacity_IN('"+id+"',"+op+","+duration+")", temp);
 }
}

function opacity_OUT(id,op,duration){
 e = document.getElementById(id);
 
 if(IEdetect()){
   e.style.zoom = 1;
   e.style.filter='alpha(opacity='+op*10+')';
 }else{
   e.style.opacity = op/10;
 }
 op--;
 temp = duration/10;
 if(op >= 0){
  setTimeout("opacity_OUT('"+id+"',"+op+","+duration+")", temp);
  if(op == 0){
   e.style.display = 'none';
   e.style.visibility = 'hidden';  
  }
 }
}

function IEdetect(){
  if (navigator.userAgent.toLowerCase().indexOf("msie") != -1){
    return true;
  }  
}

function Operadetect(){
  if (navigator.userAgent.toLowerCase().indexOf("opera") != -1){
    return true;
  }
}

/*Funzioni Italia Dati*/

function close(id){
  var div = document.getElementById(id);
  div.style.visibility = 'hidden';
  div.style.display = 'none';
}

function emailsent(id){
  if(myReq.readyState == 4 && myReq.status == 200){
     close('loader');
   if(myReq.responseText.indexOf("Devi inserire la tua email!") != -1 ||
      myReq.responseText.indexOf("Devi inserire almeno una email a cui inviare la segnalazione!") != -1 ||
      myReq.responseText.indexOf("Devi inserire un indirizzo email valido!") != -1 ||
      myReq.responseText.indexOf("Grazie per aver segnalato ItaliaDati!") != -1){
    alert(myReq.responseText);
  }else{ 
    //opacity_IN(id,0,1000);
    setTimeout("document.getElementById('"+id+"').innerHTML += myReq.responseText;", 200);
    //document.getElementById('commento').value = '';
  }
  setTimeout("enable('submit')", 3000);
 } 
}


function emails(id,lod){
  PreparaDati('email');
  disable('submit');
  loader(lod,'<span style="color:#ccc; font-size:18px;">Attendere Prego...</span><br />',url+'img/loader.gif');
    setTimeout("AJAXReq('POST','"+url+"ajax/sendemail.php',true,function (){emailsent('"+id+"')},stringa);", 1000);
}

