// Variabili principali
var popup_enable = 0;
var popup_status = 0;
var popup_loggato = 0;

// Varaibili popup_1
var popup_1_html = '';
var popup_1_width = 528;
var popup_1_height = 410;
var popup_1_left = 0;
var popup_1_top = 0;
var popup_1_left_margin = 116;
var popup_1_top_margin = 50;
var popup_1_w = 0;
var popup_1_h = 0;
var popup_1_step_w = ( popup_1_width / 8 );
var popup_1_step_h = ( popup_1_height / 8 );

var popup_2_html = '<img src="./avviso_login.gif">';
var popup_2_width = 300;
var popup_2_height = 300;
var popup_2_left = 0;
var popup_2_top = 0;
var popup_2_left_margin = 50;
var popup_2_top_margin = 50;
var popup_2_w = 0;
var popup_2_h = 0;
var popup_2_step_w = ( popup_1_width / 8 );
var popup_2_step_h = ( popup_1_height / 8 );

// Variabili popup_3
var popup_3_html = "<iframe src=\"./sondaggio.php\" width=\"100%\" height=\"100%\"></iframe>";
var popup_3_width = 780;
var popup_3_height = 0;
var popup_3_left = 0;
var popup_3_top = 0;
var popup_3_w = 0;
var popup_3_h = 0;

var screen_width = 0;
var screen_height = 0;


//popup_1_html += "<p>Aiutaci a migliorare</p>";
//popup_1_html += "<p>Partecipa al nostro sondaggio</p>";
//popup_1_html += "<p>Vuoi partecipare al sondaggio? <a href=\"#\" onclick=\"javascript:if(popup)popup.style.visibility='hidden';if(X)getData('./popup.php?popup_status=9999');\">Entra nell'area riservata.</a></p>";
//popup_1_html += "<br><br><br>";
//popup_1_html += "<p><a href=\"#\" onclick=\"javascript:if(popup)popup.style.visibility='hidden';\">Esci</a></p>";

popup_1_html += "<div id=\"underline_entra\" name=\"underline_entra\" style=\"position:absolute;top:178px;left:306px;width:82px;height:3px;background-color:#ff9966;visibility:hidden;font-size:1px;\"></div>";
popup_1_html += "<div id=\"underline_esci\" name=\"underline_esci\" style=\"position:absolute;top:372px;left:39px;width:155px;height:3px;background-color:#ff9966;visibility:hidden;font-size:1px;\"></div>";
popup_1_html += "<img border=\"0\" src=\"./sondaggio_on_line.gif\" usemap=\"#mappa_popup\">";
popup_1_html += "<map name=\"mappa_popup\">";
popup_1_html += "<area shape=\"circle\" coords=\"264,210,165\" href=\"#\" title=\"Clicca qui per partecipare al sondaggio\" onclick=\"javascript:if(popup)popup.style.visibility='hidden';if(X)getData('./popup.php?popup_status=9999');\" onmouseover=\"javascript:document.getElementById('underline_entra').style.visibility='visible';\" onmouseout=\"javascript:document.getElementById('underline_entra').style.visibility='hidden';\">";
popup_1_html += "<area shape=\"rect\" coords=\"40,345,260,385\" href=\"#\" title=\"Clicca qui per uscire\" onclick=\"javascript:if(popup)popup.style.visibility='hidden';\" onmouseover=\"javascript:document.getElementById('underline_esci').style.visibility='visible';\" onmouseout=\"javascript:document.getElementById('underline_esci').style.visibility='hidden';\">";
popup_1_html += "</map>";

// Variabili fisse
var popup = document.getElementById( 'popup_container' );
var X = null;
var browserUtente = navigator.userAgent.toUpperCase();

function calcola_area_schermo()
{
  if( typeof( window.innerWidth ) == 'number' )
  {
    //Non-IE
    screen_width = window.innerWidth;
    screen_height = window.innerHeight;
  }
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
  {
    //IE 6+ in 'standards compliant mode'
    screen_width = document.documentElement.clientWidth;
    screen_height = document.documentElement.clientHeight;
  }
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  {
    //IE 4 compatible
    screen_width = document.body.clientWidth;
    screen_height = document.body.clientHeight;
  }
}

if( X == null )
{
  if( typeof( XMLHttpRequest ) === "function" || typeof( XMLHttpRequest ) === "object" )
  {
    X = new XMLHttpRequest();
  }
  else if( window.ActiveXObject && browserUtente.indexOf( "MSIE 4" ) < 0 )
  {
    if( browserUtente.indexOf( "MSIE 5" ) < 0 )
    {
      X = new ActiveXObject( "Msxml2.XMLHTTP" );
    }
    else
    {
      X = new ActiveXObject( "Microsoft.XMLHTTP" );
    }
  }
}

function posiziona_popup_1()
{
  calcola_area_schermo();
  popup_1_left = ( ( screen_width - 760 ) / 2 ) + popup_1_left_margin;
  popup_1_top = popup_1_top_margin;
  if( popup_1_left < popup_1_left_margin )
  {
    popup_1_left = popup_1_left_margin;
  }
  if( popup_1_top < popup_1_top_margin )
  {
    popup_1_top = popup_1_top_margin;
  }
  if( popup )
  {
    popup.style.left = popup_1_left + "px";
    popup.style.top = popup_1_top + "px";
  }
}


function onData()
{
  if( X )
  {
    if( X.readyState == 4 )
    {
      if( X.status == 200 )
      {
        if( X.responseText != '' )
	{
	  eval( X.responseText );
          if( popup_enable )
	  {
	    if( popup_status == 1 )
	    {
	      display_popup_1();
	    }
	    if( popup_status == 9999 )
	    {
	      if( popup_loggato == 0 )
	      {
	        popup.style.visibility = "hidden";
	        display_popup_2();
	        document.getElementById( 'login' ).focus();
		document.getElementById( 'login' ).style.backgroundColor = '#ffeedd';
	      }
	      if( popup_loggato == 1 )
	      {
	        popup.style.visibility = "hidden";
	        display_popup_3();
	      }
	    }
	  }
	  //alert( X.responseText );
	}
      }
      else
      {
        alert( "Error function onData: " + X.statusText );
      }
    }
  }
}

function getData( url )
{
  if( X )
  {
    try
    {
      X.open( "GET", url, true );
      X.onreadystatechange = onData;
      X.send( null );
    }
    catch( e )
    {
      alert( "Error function getData: " + e );
    }
  }
}

function open_popup_1()
{
  popup_1_w += popup_1_step_w;
  popup_1_h += popup_1_step_h;
  if( popup_1_w > popup_1_width )
  {
    popup_1_w = popup_1_width;
  }
  if( popup_1_h > popup_1_height )
  {
    popup_1_h = popup_1_height;
  }
  popup.style.width = popup_1_w + "px";
  popup.style.height = popup_1_h + "px";
  if( popup_1_w >= popup_1_width && popup_1_h >= popup_1_height )
  {
    clearTimeout( t );
  }
  else
  {
    var t = setTimeout( 'open_popup_1()', 100 );
  }
}

function display_popup_1()
{
  if( popup )
  {
    posiziona_popup_1();
    popup.style.position = "absolute";
    popup.style.left = popup_1_left + "px";
    popup.style.top = popup_1_top + "px";
    popup.style.width = popup_1_width + "px";
    popup.style.height = popup_1_height + "px";
    popup.style.border = "2px solid #ffffff";
    popup.style.backgroundColor = "";
    popup.style.overflow = "hidden";
    popup.innerHTML = popup_1_html;
    popup.style.visibility = "visible";
    open_popup_1();
  }
}

function display_popup_2()
{
  if( popup )
  {
    var popup_4_html = "";
    
    popup_4_html += "<div id=\"menu_esci_4\" name=\"menu_esci_4\" style=\"position:absolute;top:14px;left:196px;width:22px;height:22px;border:3px solid #ff9966;visibility:hidden;font-size:1px;\"></div>";
    popup_4_html += "<img border=\"0\" src=\"./vignetta_new.gif\" usemap=\"#luca\">";
    popup_4_html += "<map name=\"luca\">";
    popup_4_html += "<area shape=\"rect\" coords=\"197,15,217,35\" href=\"#\" title=\"Clicca qui per uscire\" onclick=\"javascript:if(popup)popup.style.visibility='hidden';if(X)getData('./popup.php?popup_status=3');\" onmouseover=\"javascript:document.getElementById('menu_esci_4').style.visibility='visible';\" onmouseout=\"javascript:document.getElementById('menu_esci_4').style.visibility='hidden';\">";
    popup_4_html += "</map>";
    
    calcola_area_schermo();
    
    popup_2_left = ( ( screen_width - 760 ) / 2 ) + 490;
    if( popup_2_left < 490 )
    {
      popup_2_left = 490;
    }
    popup.style.position = "absolute";
    popup.style.left = popup_2_left + "px";
    popup.style.top = "58px";
    popup.style.width = "238px";
    popup.style.height = "153px";
    popup.style.border = "0px solid #000000";
    popup.style.backgroundColor = "";
    popup.innerHTML = popup_4_html;
    popup.style.overflow = "hidden";
    popup.style.visibility = "visible";
    
  }
}

function display_popup_3()
{
  if( popup )
  {
    location.href = "http://www.artigianisandona.it/sondaggio.php";
  }
}

function start_popup()
{
  getData( './popup.php?get_values=1' );
}

function posiziona_popup()
{
  if( popup_status == 1 )
  {
    posiziona_popup_1();
  }
  if( popup_status == 9999 && popup_loggato == 0 )
  {
    display_popup_2();
  }
}

window.onresize = posiziona_popup;

