// Funzioni varie per il sito

// Wrapper per la Overlib OnMouseMove
function MyOOM(Text,Caption,Type){
   // Type: 1=User, 2=User sticky, 3=tooltip con titolo, 4=Tooltip semplice
   Text = Text.split("'").join("\'");
   if (!Type) Type=1;
   if ((Type==1)||(Type==2))
      return overlib(Text, WIDTH, 320, HAUTO, VAUTO, FOLLOWMOUSE, WRAP, CAPTION, '<center>'+Caption+'</center>');
   if (Type==3)
      return overlib(Text, ADAPTIVE_WIDTH, HAUTO, VAUTO, FOLLOWMOUSE, WRAP, CAPTION, '<center>'+Caption+'</center>');
   //return overlib(Text, HAUTO, VAUTO, FOLLOWMOUSE, WRAP);
   return overlib(Text, WRAP);
}

// Help popup. Per l'help contestuale. Accetta 2 parametri: Sezione del sito o gioco e Argomento
function openHelp(SiteSection,Topic) {
	popupWin = window.open('cp_PopUpHelp.asp?sid=' + SiteSection + '&cmd=' + Topic,'EIN','width=375,height=415,left=50,top=50,scrollbars=yes,resizable=yes');
	popupWin.focus();
}

// eccezione al popup. Apre una finestra di Chat
function ApriFinestraChat(mylink, windowname)
{
if (! window.focus)return true;
var href;
var winl = (screen.width - 400) / 2;
var wint = (screen.height - 500) / 2;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
var ref=window.open(href, windowname, 'width=500,height=400,top='+wint+',left='+winl+',scrollbars=no,resizable=no');
ref.focus();
return false;
}

// Apre una finestra popup
function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
var LarghezzaFinestra = (screen.width - 300);
if (LarghezzaFinestra > 1024)
   LarghezzaFinestra=1024;
if (LarghezzaFinestra < 500)
   LarghezzaFinestra=500;
var AltezzaFinestra = (screen.height - 100);
if (AltezzaFinestra > 768)
   AltezzaFinestra=768;
if (AltezzaFinestra < 400)
   AltezzaFinestra=400;
var winl = (screen.width - LarghezzaFinestra) / 2;
var wint = (screen.height - AltezzaFinestra) / 2;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
var ref=window.open(href, windowname, 'width='+LarghezzaFinestra+',height='+AltezzaFinestra+',top='+wint+',left='+winl+',scrollbars=yes,resizable=yes');
ref.focus();
return false;
}


// Chiude la finestra e ripassa al chiamante
function targetopener(mylink, closeme, closeonly)
{
if (! (window.focus && window.opener))return true;
window.opener.focus();
if (! closeonly)window.opener.location.href=mylink.href;
if (closeme)window.close();
return false;
}

// Scrive un Coockie
function setCookie(name, value, days) {
  var expireDate = new Date ();
  expireDate.setTime( expireDate.getTime() + (days * 24 * 3600 * 1000) );
  document.cookie = name + "=" + escape(value) + "; expires=" + expireDate.toGMTString();
}

// Legge un Coockie
function getCookie(name) {
 if (document.cookie.length > 0) {
  var start;
  var end;
  start = document.cookie.indexOf(name + "=");
  if ( start == -1 ) return null;
  end = document.cookie.indexOf(";", start);
  if (end == -1) end = document.cookie.length;
   return unescape( document.cookie.substring(start + name.length + 1, end) );
  }
  return null;
 }

// Cancella un Coockie
function deleteCookie( name ) {
 if ( getCookie(name) ) {
  document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
 }


// BROWSER SNIFFER
 function Is() {
   var agent = navigator.userAgent.toLowerCase();
   this.major = parseInt(navigator.appVersion);
   this.minor = parseFloat(navigator.appVersion);
   this.ns = ((agent.indexOf('mozilla') != -1) &&
   (agent.indexOf('spoofer') == -1) &&
   (agent.indexOf('compatible') == -1) &&
   (agent.indexOf('opera') == -1) &&
   (agent.indexOf('webtv') == -1) &&
   (agent.indexOf('hotjava') == -1));
   this.ns2 = (this.ns && (this.major == 2));
   this.ns3 = (this.ns && (this.major == 3));
   this.ns4 = (this.ns && (this.major == 4));
   this.ns6 = (this.ns && (this.major >= 5));
   this.ie = ((agent.indexOf("msie") != -1) &&
   (agent.indexOf("opera") == -1));
   this.ie3 = (this.ie && (this.major < 4));
   this.ie4 = (this.ie && (this.major == 4) &&
   (agent.indexOf("msie 4") != -1));
   this.ie5 = (this.ie && (this.major == 4) &&
   (agent.indexOf("msie 5.") != -1) &&
   (agent.indexOf("msie 5.5") == -1) &&
   (agent.indexOf("mac") == -1));
   this.iem5 = (this.ie && (this.major == 4) &&
   (agent.indexOf("msie 5.") != -1) &&
   (agent.indexOf("mac") != -1));
   this.ie55 = (this.ie && (this.major == 4) &&
   (agent.indexOf("msie 5.5") != -1));
   this.ie6 = (this.ie && (this.major == 4) &&
   (agent.indexOf("msie 6.") != -1));
   this.ie7 = (this.ie && (this.major == 4) &&
   (agent.indexOf("msie 7.0b") != -1));
   //this.nsdom = (this.ns4 || this.ns6);
   this.nsdom = ((this.ns4  || this.ns6) && !(document.getElementById || document.all));
   this.ie5dom = (this.ie5 || this.iem5 || this.ie55);
   this.iedom = (this.ie4 || this.ie5dom || this.ie6);
   this.w3dom = (this.ns6 || this.ie6 ||this.ie7);
   this.DHTML = (document.getElementById || document.all || document.layers);
   this.AnyIE = (this.ie4 || this.ie5 || this.iem5 || this.ie55 || this.ie6 ||this.ie7);
}

var is = new Is();


var DHTML = (document.getElementById || document.all || document.layers);

// Decide browser version
var ns4 = (navigator.appName == 'Netscape' && parseInt(navigator.appVersion) == 4);
var ns6 = (document.getElementById)? true:false;
var ie4 = (document.all)? true:false;

function getObj(name)
{
  if (typeof name == "string"){
     if (document.getElementById)
     {
     	this.obj = document.getElementById(name);
   	this.style = document.getElementById(name).style;
     }
     else if (document.all)
     {
   	this.obj = document.all[name];
   	this.style = document.all[name].style;
     }
     else if (document.layers)
     {
   	this.obj = getObjNN4(document,name);
   	this.style = this.obj;
     }
  }
  else{
     if (document.getElementById || document.all )
     {
   	this.obj = name;
   	this.style = name.style;
     }
      else
     {
   	this.obj = name;
   	this.style = name;
     }
  }
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

// Come cambiare lo stato di visibilità:
function CambiaStatoVisibilitaDaID(ID, bVisibile){
   // ID = ID dell'elemento
   // bVisible=1 se visibile, =0 se hidden
	if (!DHTML) return;
	var x = new getObj(ID);
	x.style.visibility = (bVisibile) ? 'hidden' : 'visible';
}

// Come invertire lo stato di visibilità:
function invertiStatoVisibilitaDaID(ID){
   // ID = ID dell'elemento
	if (!DHTML) return;
	var x = new getObj(ID);
	if (x.style.visibility == 'hidden') {
	  x.style.visibility = 'visible';
	  x.style.display= 'block';
	} else {
	  x.style.visibility = 'hidden';
	  x.style.display= 'none';
	}
}

// Come cambiare il contenuto dell'HTML
function CambiaContenutoHTMLDaID(ID, NewHTML){
	if (!DHTML) return;
	var x = new getObj(ID);
   var msg = NewHTML;
   if(document.getElementById || document.all) {
      x.obj.innerHTML = msg;
   } else
      {
    /*
    for Netscape 4. Not surprisingly, the whole implementation of Netscape 4 sucks in a really major way. Some points that we'll have to remember:
    * The target element must have a position: absolute defined in the style sheet, so you are required to place the target element at an absolute position in the page.
    * As soon as the new text is written into it, the target element loses all style information. To circumvent this, I write the text plus a surrounding <P> with the correct class into the target element.
    * Writing HTML tags into the layer may not work.
    */
		//msg = '<P CLASS="testclass">' + NewHTML + '</P>';
		x.obj.open();
		x.obj.write(msg);
		x.obj.close();
	}
}


function ChangeClassName(ID,className){
	if (!DHTML) return;
	var o = new getObj(ID);
   if (o.obj.className!=className)
      o.obj.className=className;
}

// Come spostare un layer
function SpostaOggettoDaID(ID,Left,Top){
	if (!DHTML) return;
	var x = new getObj(ID);
   if (ie4) { x.style.posTop = Top;      x.style.posLeft = Left; }
   if (ns4) { x.style.top = Top + 'px';  x.style.left =  Left + 'px'; }
   if (ns6) { x.style.top = Top;         x.style.left = Left; }
}

function SetPosX(ID,Left){
	if (!DHTML) return;
	var x = new getObj(ID);
   if (ie4) { x.style.posLeft = Left; }
   if (ns4) { x.style.left =  Left + 'px'; }
   if (ns6) { x.style.left = Left; }
}

function SetPosY(ID,Top){
	if (!DHTML) return;
	var x = new getObj(ID);
   if (ie4) { x.style.posTop = Top;}
   if (ns4) { x.style.top = Top + 'px';}
   if (ns6) { x.style.top = Top;}
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getInnerWindowWidth() {
   var x,y;
   if (self.innerHeight) // all except Explorer
   {
   	x = self.innerWidth;
   }
   else if (document.documentElement && document.documentElement.clientHeight)
   	// Explorer 6 Strict Mode
   {
   	x = document.documentElement.clientWidth;
   }
   else if (document.body) // other Explorers
   {
   	x = document.body.clientWidth - 18;
   }
  return x;
};

function getInnerWindowHeight() {
   var x,y;
   if (self.innerHeight) // all except Explorer
   {
   	y = self.innerHeight;
   }
   else if (document.documentElement && document.documentElement.clientHeight)
   	// Explorer 6 Strict Mode
   {
   	y = document.documentElement.clientHeight;
   }
   else if (document.body) // other Explorers
   {
   	y = document.body.clientHeight - 18;
   }
  return y;
};


function getViewportScrollX()  {
  var scrollX = 0;
  if( document.documentElement && document.documentElement.scrollLeft ) {
    scrollX = document.documentElement.scrollLeft;
  }
  else if( document.body && document.body.scrollLeft ) {
    scrollX = document.body.scrollLeft;
  }
  else if( window.pageXOffset ) {
    scrollX = window.pageXOffset;
  }
  else if( window.scrollX ) {
    scrollX = window.scrollX;
  }
  return scrollX;
};

function getViewportScrollY() {
  var scrollY = 0;
  if( document.documentElement && document.documentElement.scrollTop ) {
    scrollY = document.documentElement.scrollTop;
  }
  else if( document.body && document.body.scrollTop ) {
    scrollY = document.body.scrollTop;
  }
  else if( window.pageYOffset ) {
    scrollY = window.pageYOffset;
  }
  else if( window.scrollY ) {
    scrollY = window.scrollY;
  }
  return scrollY;
};

//Functions for the animation of the selected pawn (Modified from Dreamweaver)
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function My_swapImage() { //v3.0
  var i,j=0,x,a=My_swapImage.arguments; MM_swapImgRestore(); document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


/*
// Per conoscere lo spazio disponibile a video, nel tag <BODY> inserire ciò che segue:
// <body onload="available_width  = (is.iedom) ? document.body.clientWidth  : innerWidth;
//        available_height = (is.iedom) ? document.body.clientHeight : innerHeight;"
//        onresize=" available_width  = (is.iedom) ? document.body.clientWidth  : innerWidth;
//        available_height = (is.iedom) ? document.body.clientHeight : innerHeight;"
*/