var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;


var PopupArray = new Array();
var myWin = null;
var ReloadMe = false;
var myWidth = 0, myHeight = 0;

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

if (myHeight >screen.height) {
  window.resizeTo(myWidth,screen.height-100);
  moveTo((screen.width/2)-(myWidth/2),(screen.height/2)-((screen.height-100)/2)-20);
}


/*
  Funzione che accoda eventi a quelli esistenti sul load della pagina
*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
};


function popup(url,w,h,sb)
{
	var d = new Date();
	myWin = window.open(url,'popupwindow'+escape(d.getTime()),'left='+((screen.width/2)-(w/2))+',top='+((screen.height/2)-(h/2)-40)+',screenX='+((screen.width/2)-(w/2))+',screenY='+((screen.height/2)-(h/2)-40)+',Width='+w+',height='+h+',alwaysRaised=yes,history=no,resizable=yes,status=no,scrollbars=' + sb +',menubar=no,toolbar=no,location=no')	
	myWin.opener = self;	
	PopupArray[PopupArray.length] = myWin;
	myWin.focus();
}


function mostra(x,y)
{
  var temp = document.getElementById(x);
  if (temp) 
	  temp.style.display = '';
	if (y != null)
		document.getElementById(y).focus();
}


function nascondi(x)
{
  if (document.getElementById(x)) 
    document.getElementById(x).style.display = 'none';
}


function mostranascondi(x)
{
  if (document.getElementById(x)) 
  {
    if (document.getElementById(x).style.display == '')
    {
    	document.getElementById(x).style.display = 'none';
    }
    else
    {
    	document.getElementById(x).style.display = '';
    }
  }
}


function IsInteger(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

   for (var i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (var i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
}


/*
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/
function getElementsByClassName(oElm, strTagName, strClassName)
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++)
	{
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className))
		{
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}


/*
   Scorre gli elementi presenti nella pagina con classe tagsClassName
   Li nasconde tutti tranne se uguali all'elemento selectedTag
*/
function selectTag(tagsClassName,selectedTag)
{
   var arrElements = getElementsByClassName( document, "form", "formTag" );
   
	for(var i=0; i<arrElements.length; i++)
	{
	   if (   arrElements[i].id == selectedTag )
	      arrElements[i].style.display = 'block';
	   else
	      arrElements[i].style.display = 'none';
	      
	}     
}


/*
    Funzione che restituisce le coordinate assolute in pixel di un elemento
*/


function getAbsolutePosition(element) {
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
      var tmp = getAbsolutePosition(element.offsetParent);
      r.x += tmp.x;
      r.y += tmp.y;
    }
    return r;
}


/*
    INIZIO Funzioni che mostrano e nascondono il fumetto con il testo dell'help
*/

function showhelp(x) {

  var elemento = document.createElement("DIV");
  elemento.id = 'help_' + x.id;  
  elemento.className = 'helpbox';

  elemento.appendChild(document.createTextNode(x.testohelp));

  document.body.appendChild(elemento); 

  var fumetto = document.createElement("IMG");
  fumetto.id = 'helpimg_' + x.id;  
  fumetto.src = 'img/fumetto.png';
  fumetto.style.zIndex = '999';
  fumetto.style.position = 'absolute';
  
  document.body.appendChild(fumetto); 

  var posizione = getAbsolutePosition(x);

  var posx = posizione.x + x.offsetWidth ;
  var posy = posizione.y + (x.offsetHeight/2) - elemento.offsetHeight;
  elemento.style.left = posx - 20  + 'px';

  /*
    tengo conto della parte inferiore del fumetto su cui non voglio scrivere 
    230 è l'altezza complessiva del fumetto e 60 lìaltezza della parte inferiore
    calcolo la proporzione dopo il resize e riposizione gli elementi
  */

  var pb = 60 * elemento.offsetHeight / 230 ;
  elemento.style.top = posy - pb + 5 + 'px';

  fumetto.style.width = elemento.offsetWidth + 'px';
  fumetto.style.height = elemento.offsetHeight + pb + 'px';
  fumetto.style.left = elemento.style.left;
  fumetto.style.top = elemento.style.top;

}


function hidehelp(x) {
  var contenitore = x.parentNode;
  var elemento = document.getElementById('help_' + x.id);
  document.body.removeChild(elemento);  
  var fumetto = document.getElementById('helpimg_' + x.id);
  document.body.removeChild(fumetto);  
}


function loadhelp(campi) {

  if (campi) {
    for (x in campi)
    {
      if (document.getElementById(campi[x][0])) 
      {
        var campo = document.getElementById(campi[x][0]);
        campo.onfocus = function () { showhelp(this) };
        campo.onblur  = function () { hidehelp(this) };
        campo.testohelp = campi[x][1];
      }
    }
  }

}

// Encoding Email: inserisce nel testo un link di tipo mailto.
// ------------------------------------------------------------------------------------------
// mailToId    : l'id di un elemento span (o altro) che conterrà la mail.
// emUsername  : la parte prima della chiocciola della mail
// emDomain    : la parte dopo la chiocciola della mail
// emText      : il testo del link; se null viene usata la stessa mail come anchor text

function mailTo(mailToId, emUsername, emDomain, emText)
{
  if (!document.getElementById(mailToId)) return false;
  var spanobj = document.getElementById(mailToId);
  var anch = document.createElement("a");
  var email = emUsername + "@" + emDomain;
  var mailto = "mailto:" + email;
  anch.setAttribute("href",mailto);
  spanobj.appendChild(anch);
  if ( emText == null )
   emText = email;
  var txt = document.createTextNode(emText);
  anch.appendChild(txt);
  return true;
}


/*
    FINE Funzioni che mostrano e nascondono il fumetto con il testo dell'help
*/


/* Funzione che conta i caratteri inseriti in una textarea  */
function ResidualLength(tx,max)
{
 a = max - tx.value.length;    
 if ( a < 0)
  {
    tx.value = tx.value.substring(0, tx.value.length + a);
    a = 0;
    alert("Attenzione Ã© stata raggiunta la lunghezza massima consentita di " + max + " caratteri");
 }
 document.getElementById(tx.name + "_ResidualLength").innerHTML = "massimo numero di caratteri " + max + ", residui " + a;
}


/*
   Controlla i campi della maschera di ricerca e se vuoti li disabilita
*/

/*
function hideSearchBlankFields()
{
   var querystring = null;
   var fieldname = null;
   
   for ( i=1; i<=4; i++)
   {
      querystring = document.getElementById("fld_query_querystring_"+i);
      if ( querystring.value==null || querystring.value=='' )
      {
         fieldname = document.getElementById("fld_query_fieldname_"+i);
         querystring.parentNode.removeChild(querystring);
         fieldname.parentNode.removeChild(fieldname);
      }
   }
   
   var queryfilters = new Array( "inquadramentoid", "provinciaid", "contrattoid", "interinaleid", "livellostudioid", "dataid" )
   var queryfilter = null;
   
   for ( j=0; j<queryfilters.length; j++ )
   {
      queryfilter = document.getElementById("fld_query_filter_"+queryfilters[j]);
      if ( queryfilter.selectedIndex==0 )
      {
         queryfilter.options.length = 0;
      }
   }
}
*/
