function swap_up(element)
{
	string = new String(element.src);
	element.src = string.replace('_off.','_on.');
}

function swap_down(element)
{
	string = new String(element.src);
	element.src = string.replace('_on.','_off.');
}

function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

var openeddiv = null;
function opendiv(id)
{
	if (openeddiv!=null) document.getElementById(openeddiv).style.display = 'none';
	openeddiv = id;
	document.getElementById(id).style.display = 'block';
}

function getWindowSize() {
  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;
  }
  return new Array(myWidth,myHeight);
}

/*
 * forza il submit se premo invio
 * Es:
 * <FORM ACTION="../cgi-bin/mycgi.pl">
 *   name:     <INPUT NAME=realname SIZE=15><BR>
 *   password: <INPUT NAME=password TYPE=PASSWORD SIZE=10
 *          onKeyPress="return submitenter(this,event)"><BR>
 *   <INPUT TYPE=SUBMIT VALUE="Log In">
 *   </FORM>
 */

function submitenter(myfield,e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    
    if (keycode == 13)
       {
       myfield.form.submit();
       return false;
       }
    else
       return true;
}


/*check indirizzo Mail
 *
 */

function checkMail(string)
{
    var espressione = /^([A-z._\-])+@([A-z._\-])+\.([A-z])/;
    return espressione.test(string);
}


/*
 * Setto la select passata al valore dato
 */

function setSelect(select,value)
{
    for (j=0; j<select.options.length;j++)
	if (select.options[j].value==value)
	{
	    select.selectedIndex=j;
	    return;
	}
}


/*
 * Pulisco una stringa per passarla in get via js
 */
function clearGetString(string){
	string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

		if (c==43)
		{
			utftext += '&#43;';
		}
		else if(c==38)
		{
			utftext += '&#38;';
		}
        else if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += '&#'+c+';';
			//utftext += String.fromCharCode((c >> 6) | 192);
            //utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
			utftext += '&#'+c+';';
            //utftext += String.fromCharCode((c >> 12) | 224);
            //utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            //utftext += String.fromCharCode((c & 63) | 128);
        }

    }
    return escape(utftext);
}





function centra(url, width, height)
   {
   if (document.all)
      {
      var x = window.screenLeft;
      var y = window.screenTop;
      var w = window.document.body.offsetWidth;
      var h = window.document.body.offsetHeight;
      }
  else
   {
   var x = window.screenX;
   var y = window.screenY;
   var w = window.outerWidth;
   var h = window.outerHeight;
   }
  var cntx = x + Math.round((w - width) / 2);
  var cnty = y + Math.round((h - height) / 2);
 winContent = window.open (url, null, 'left=' + cntx + ',top=' + cnty + ',width=' + width + ',height=' + height  + ',resizable=yes')

winContent.focus()

}


/*
 * Moostra il div con l'id specificato
 */

function showDiv(id)
{
    var myDiv = document.getElementById(id);
    myDiv.style.display='inline';
}

var myMenuDiv = null;
function showMenuDiv(id)
{
	if (myMenuDiv) myMenuDiv.style.display = 'none';
	myMenuDiv = document.getElementById(id);
	myMenuDiv.style.display='inline';
}


/*
 * Apre un popup
 **/
function popup(url) {
	if (url!="") {
		newwindow=window.open(url,"","width=490,height=490,align=center,scrollbars=yes,resizable=yes");
		newwindow.creator=self;
	}
}


/*
 * Esegue il submit di una form in una nuova finestra
 * 
 **/
function doSubmit(nomeForm,nomepagina) {
    var submitForm = document.getElementById(nomeForm);
    displayWindow = window.open('', nomepagina, "width=490,height=490,align=center,scrollbars=yes,resizable=yes");
    submitForm.submit();
}

/*
 * Apre il link nel div specificato (ajax)
 * 
 **/
function openInDiv(nomeDiv,nomeScript,param)
{
    setTarget(document.getElementById(nomeDiv));
    update(nomeScript,param);
    
}

