/*
 * Funzioni specifiche del portale
 */


//GALLERIA VIDEO:
function showVideo()
{
    target = document.getElementById('image_container');
    update('moduli/utenti/uploadvideo.php','op=show_video');
    
}

function editVideo(id,text)
{
    target = null;
    update('moduli/utenti/uploadvideo.php','op=edit_video&id='+id+'&descrizione='+text);
    showVideo();
}


function showUploadVideo(max)
{
    target = document.getElementById('iframe_container');
    update('moduli/utenti/uploadvideo.php','op=show&max='+max);
}

function deleteVideo(id)
{
    target = null;
    update('moduli/utenti/uploadvideo.php','op=delete_video&id='+id);
    showVideo();
}


//GALLERIA FOTO:
function showFoto()
{
    target = document.getElementById('image_container');
    update('moduli/utenti/uploadfoto.php','op=show_foto');
    
}

function editFoto(id,text)
{
    target = null;
    update('moduli/utenti/uploadfoto.php','op=edit_foto&id='+id+'&descrizione='+text);
    showFoto();
}


function showUpload(max)
{
    target = document.getElementById('iframe_container');
    update('moduli/utenti/uploadfoto.php','op=show&max='+max);
}

function deleteFoto(id)
{
    target = null;
    update('moduli/utenti/uploadfoto.php','op=delete_foto&id='+id);
    showFoto();
}

//SETTA IL CAP
function setCAP(id_citta)
{
    target = document.getElementById('div_CAP');
    update('moduli/utenti/edit.php','op=show_cap&id='+id_citta);
}


//FUNZIONI PROFILO MEDICO
var xmlhttp5 = null;

function insertCompetenza(id_medici)
{
    target=null;
    competenza = document.getElementById('add_competenza').value;
    if (competenza=='') return alert('Il campo e\' obbligatorio');
    update('moduli/utenti/edit.php','op=insert_competenza&id='+id_medici+'&competenza='+competenza);
    setTimeout("showCompetenze("+id_medici+")",400);
}

function deleteCompetenza(id,id_medici)
{
    target=null;
    update('moduli/utenti/edit.php','op=delete_competenza&id='+id);
    setTimeout("showCompetenze("+id_medici+")",400);
}

function showCompetenze(id)
{
    try {
    	//xmlhttp5 = getXMLHttp();
    	xmlhttp5 = getXMLHttp();
    } catch (e) {
	alert("Browser non supportato!!!");
    }
    var ret_f1 = new Function("if ((xmlhttp5.readyState == 4) && (xmlhttp5.status == 200)){ document.getElementById('div_competenze').innerHTML = xmlhttp5.responseText;}");
    update_full('moduli/utenti/edit.php','op=show_competenze&id='+id,xmlhttp5,ret_f1);
}




var xmlhttp2 = null;

function insertColl(id_medici)
{
    target=null;
    collaborazione = document.getElementById('add_collaborazione').value;
    if (collaborazione=='') return alert('Il campo Collaborazione e\' obbligatorio');
    update('moduli/utenti/edit.php','op=insert_coll&id='+id_medici+'&collaborazione='+collaborazione);
    setTimeout("showColl("+id_medici+")",400);
}

function deleteColl(id,id_medici)
{
    target=null;
    update('moduli/utenti/edit.php','op=delete_coll&id='+id);
    setTimeout("showColl("+id_medici+")",400);
}

function showColl(id)
{
    try {
	xmlhttp2 = getXMLHttp();
    } catch (e) {
	alert("Browser non supportato!!!");
    }
    var ret_f = new Function("if ((xmlhttp2.readyState == 4) && (xmlhttp2.status == 200)){ document.getElementById('div_collaborazioni').innerHTML = xmlhttp2.responseText;}");
    update_full('moduli/utenti/edit.php','op=show_coll&id='+id,xmlhttp2,ret_f);
}

var xmlhttp3 = null;

function insertContatto(id_medici)
{
    //target=document.getElementById('div_contatti');
    target=null;
    
    regione_eser = document.getElementById('regione_eser').value;
    if (regione_eser==0) return alert('Il campo Regione e\' obbligatorio');
    provincia_eser = document.getElementById('provincia_eser').value;
    if (provincia_eser==0) return alert('Il campo Provincia e\' obbligatorio');
    citta_eser = document.getElementById('citta_eser').value;
    if (citta_eser==0) return alert('Il campo Citta\' e\' obbligatorio');
    CAP_eser = document.getElementById('CAP').value;
    if (CAP_eser==0) return alert('Il campo CAP e\' obbligatorio');
    tipologia_eser = document.getElementById('tipologia_eser').value;
    if (tipologia_eser=='') return alert('Il campo Tipologia e\' obbligatorio');
    //localita_eser = document.getElementById('localita_eser').value;
    indirizzo_eser = document.getElementById('indirizzo_eser').value;
    orari_eser = document.getElementById('orari_eser').value;
    telefono_eser = document.getElementById('telefono_eser').value;
    email_eser = document.getElementById('email_eser').value;
    fax_eser = document.getElementById('fax_eser').value;
    str = 'op=insert_contatto&id='+id_medici;
    str+='&regione='+regione_eser;
    str+='&provincia='+provincia_eser;
    str+='&citta='+clearGetString(citta_eser);
    str+='&CAP='+clearGetString(CAP_eser);
    //str+='&localita='+clearGetString(localita_eser);
    str+='&indirizzo='+clearGetString(indirizzo_eser);
    str+='&tipologia='+clearGetString(tipologia_eser);
    str+='&orari='+clearGetString(orari_eser);
    str+='&telefono='+clearGetString(telefono_eser);
    str+='&email='+clearGetString(email_eser);
    str+='&fax='+clearGetString(fax_eser);

    update('moduli/utenti/edit.php',str);
    setTimeout("showContatti("+id_medici+")",400);
}

function deleteContatto(id,id_medici)
{
    target=null;
    update('moduli/utenti/edit.php','op=delete_contatto&id='+id);
    setTimeout("showContatti("+id_medici+")",400);
}

function showContatti(id)
{
    try {
	xmlhttp3 = getXMLHttp();
    } catch (e) {
	alert("Browser non supportato!!!");
    }
    var ret_f2 = new Function("if ((xmlhttp3.readyState == 4) && (xmlhttp3.status == 200)){ document.getElementById('div_contatti').innerHTML = xmlhttp3.responseText;}");
    update_full('moduli/utenti/edit.php','op=show_contatto&id='+id,xmlhttp3,ret_f2);
}

var xmlhttp4 = null;
function insertSpec(id_medici)
{
    target=null;
    spec = document.getElementById('add_spec').value;
    if (spec==0) return alert('Il campo Specializzazione e\' obbligatorio');
    universita = document.getElementById('add_unispec').value;
    if (universita=='') return alert('Il campo Universita\' e\' obbligatorio');
    anno = document.getElementById('add_annospec').value;
    if (anno=='') return alert('Il campo Anno e\' obbligatorio');
    voto = document.getElementById('add_votospec').value;
    update('moduli/utenti/edit.php','op=insert_spec&id='+id_medici+'&spec='+spec+'&universita='+universita+'&anno='+anno+'&voto='+voto);
    setTimeout("showSpec("+id_medici+")",400);
}

function deleteSpec(id,id_medici)
{
    target=null;
    update('moduli/utenti/edit.php','op=delete_spec&id='+id);
    setTimeout("showSpec("+id_medici+")",400);
}

function showSpec(id)
{
    try {
	xmlhttp4 = getXMLHttp();
    } catch (e) {
	alert("Browser non supportato!!!");
    }
    var ret_f4 = new Function("if ((xmlhttp4.readyState == 4) && (xmlhttp4.status == 200)){ document.getElementById('div_spec').innerHTML = xmlhttp4.responseText;}");
    update_full('moduli/utenti/edit.php','op=show_spec&tipo=spec&id='+id,xmlhttp4,ret_f4);
}


var xmlhttp_titoli = null;
function insertTitoli(id_medici)
{
    target=null;
    titolo = document.getElementById('add_titoli').value;
    if (titolo==0) return alert('Il campo Titolo e\' obbligatorio');

    update('moduli/utenti/edit.php','op=insert_spec&id='+id_medici+'&spec='+titolo+'&tipo=titolo');
    setTimeout("showTitoli("+id_medici+")",400);
}

function deleteTitoli(id,id_medici)
{
    target=null;
    update('moduli/utenti/edit.php','op=delete_spec&id='+id);
    setTimeout("showTitoli("+id_medici+")",400);
}

function showTitoli(id)
{
	try {
	xmlhttp_titoli = getXMLHttp();
    } catch (e) {
	alert("Browser non supportato!!!");
    }
    var ret_f_titoli = new Function("if ((xmlhttp_titoli.readyState == 4) && (xmlhttp_titoli.status == 200)){ document.getElementById('div_titoli').innerHTML = xmlhttp_titoli.responseText;}");
    update_full('moduli/utenti/edit.php','op=show_spec&tipo=titolo&id='+id,xmlhttp_titoli,ret_f_titoli);
}




function insertLaurea(id_medici)
{
    target=null;
    laurea = document.getElementById('add_laurea').value;
    if (laurea=='') return alert('Il campo Laurea e\' obbligatorio');
    universita = document.getElementById('add_universita').value;
    if (universita=='') return alert('Il campo Universita\' e\' obbligatorio');
    anno = document.getElementById('add_anno').value;
    if (anno=='') return alert('Il campo Anno e\' obbligatorio');
    voto = document.getElementById('add_voto').value;
    update('moduli/utenti/edit.php','op=insert_laurea&id='+id_medici+'&laurea='+laurea+'&universita='+universita+'&anno='+anno+'&voto='+voto);
    setTimeout("showLauree("+id_medici+")",400);
}

function deleteLaurea(id,id_medici)
{
    target=null;
    update('moduli/utenti/edit.php','op=delete_laurea&id='+id);
    setTimeout("showLauree("+id_medici+")",400);
}

function showLauree(id)
{
    target = document.getElementById('div_lauree');
    update('moduli/utenti/edit.php','op=show_laurea&id='+id);
}

/*
 * 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 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(selectEl,value)
{
    for (var j=0; j<selectEl.options.length;j++)
	if (selectEl.options[j].value==value)
	{
		selectEl.selectedIndex=j;
	    return;
	}
}


/*
 * Pulisco una stringa per passarla in get via js
 */
function clearGetString(string){
    
    //bypasso tutto
    return 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.charAt(n);
        }
        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
 **/

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

}

function Controllo_carrello()
{
    var nome = document.modulo.nome.value;
    if (nome == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare il nome");
    
    return false;
    } else 

      var cognome = document.modulo.cognome.value;
    if (cognome == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare il cognome");
    
    return false;
    } else 

	  var partita_iva = document.modulo.partita_iva.value;
    if (partita_iva == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare la partita IVA o il codice fiscale");
    
    return false;
    } else

   var indirizzo = document.modulo.indirizzo.value;
    if (indirizzo == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare l\'indirizzo");
    
    return false;
    } else 

   var citta = document.modulo.citta.value;
    if (citta == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare la citta\'");
    
    return false;
    } else 

   var provincia = document.modulo.provincia.value;
    if (provincia == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare la provincia");
    
    return false;
    } else 

   var cap = document.modulo.cap.value;
    if (cap == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare il CAP");
    
    return false;
    } else 

   var telefono = document.modulo.telefono.value;
    if (telefono == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare il telefono");
    
    return false;
    } else 

	  var email = document.modulo.email.value;
    if (email == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare l\' e-mail");
    
    return false;
    } else 
   var password = document.modulo.password.value;
    if (password == "" )
    {
        alert("Attenzione!\n E\' obbligatorio specificare la password");
    
    return false;
    } else 

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

}

function Controllo_pagamento()
{
    var radiopagamento = false;
    for (i = 0;  i < document.modulo.pagamento.length;  i++)
    {
	if (document.modulo.pagamento[i].checked)
	radiopagamento = true;
    }
    if (!radiopagamento)
    {
	alert("Attenzione!\n E\' obbligatorio specificare la modalita\' di pagamento");
	return (false);
    }

}

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;
}
