/*
 * Funzioni specifiche del portale
 */


/*
 * Fine funzioni specifiche
 */

/*
 * 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 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 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);
}




/*
 * Funzioni Taedo
 **/

/*
 * Assegna all'imagine contenuta nel link element la src url
 *
 */
function swapLinkImage(element,url)
{
    var arr = element.childNodes;
    for(i=0; i<element.childNodes.length; i++)
    {
	if(arr[i].tagName=='IMG') arr[i].src=url;
    }
}


function Controllo_condizioni()
{
    if(document.modulo.condizioni.checked=="") 
      { 
      alert('Attenzione!\n E\' necessario leggere ed accettare le condizioni') 
      return false; 
      } 
   else 
     	
    {
    	return true;
    }

}


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()

}


/*
 *fine funzioni Taedo
 */


/*
 * funzioni per il Voto
 **/
function showVoto(id)
{
    target = document.getElementById('div_voto_'+id);
    update("lib/voto.php",'op=show&id='+id);
}

function vota(val,id)
{
    update("lib/voto.php",'op=vota&id='+id+'&val='+val);
    showVoto(id);
}

function switchOrdinato(val,id,url_img)
{
    for(var i=1; i<=val; i++)
    {
	var img = document.getElementById('img_voto_'+id+'_'+i);
	img.src = url_img;
    }
}

/*
 * fine funzioni voto
 * /

/*
 * Moostra il div con l'id specificato
 */
function showDiv(id)
{
    var myDiv = document.getElementById(id);
    myDiv.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;
	}
}

/*
 * Funzioni di Dreamweaver
 *
 **/
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_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_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 MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments;
  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];
    }
}
/* Fine funzioni di Dreamweaver */

/*
 * 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);
    
}


/*
 * Funzioni per il suggerimento dei termini giˆ usati per i textfields
 */
var divSuggerimento = null;

function suggerisci(elemento,tabella,script)
{
    var nomeCampo = elemento.name;
    var divTarget = document.getElementById('suggerisci_'+nomeCampo);
    if (divSuggerimento != null) divSuggerimento.style.display='none';
    divSuggerimento = divTarget;
    setTarget(divTarget);
    divTarget.style.display='inline';
    var str="campo="+nomeCampo+"&tabella="+tabella+"&valore="+elemento.value;
    update(script,str);
}

function impostaSuggerimento(nomeCampo,valore)
{
    var campo = document.getElementById(nomeCampo);
    if (valore!='') campo.value=valore;
    var divTarget = document.getElementById('suggerisci_'+nomeCampo);
    divTarget.style.display='none';
    divSuggerimento = null;
}

