// JavaScript Document

function showhide(theid) {
	var newstato='none';
	if(document.getElementById(theid).style.display=='none') newstato='';
	document.getElementById(theid).style.display=newstato;
}

function showhidepulsante(theid) {
	var statomeno = document.getElementById('meno'+theid).style.display;
	if(statomeno=='none') {
		document.getElementById('meno'+theid).style.display='';
	}
	else {
		document.getElementById('meno'+theid).style.display='none';	
	}
	document.getElementById('piu'+theid).style.display=statomeno;
}

function showone(categoria, id, totale) {
	for(var i=0;i<totale;i++) {
		document.getElementById(categoria + '' + i).style.display='none';
	}
	document.getElementById(categoria + '' + id).style.display='';
}

function hideone(categoria, id, totale) {
	for(var i=0;i<totale;i++) {
		document.getElementById(categoria + '' + i).style.display='';
	}
	document.getElementById(categoria + '' + id).style.display='none';
}

// position of the tooltip relative to the mouse in pixel //
var offsetx = 12;
var offsety =  8;

function ttnewelement(newid) { 
    if(document.createElement)
    { 
        var el = document.createElement('div'); 
        el.id = newid;     
        with(el.style)
        { 
            display = 'none';
            position = 'absolute';
        } 
        el.innerHTML = '&nbsp;'; 
        document.body.appendChild(el); 
    } 
} 

var ie5 = (document.getElementById && document.all); 
var ns6 = (document.getElementById && !document.all); 
var ua = navigator.userAgent.toLowerCase();
var isapple = (ua.indexOf('applewebkit') != -1 ? 1 : 0);

function ttgetmouseposition(e) {
    if(document.getElementById)
    {
        var iebody=(document.compatMode && 
        	document.compatMode != 'BackCompat') ? 
        		document.documentElement : document.body;
        pagex = (isapple == 1 ? 0:(ie5)?iebody.scrollLeft:window.pageXOffset);
        pagey = (isapple == 1 ? 0:(ie5)?iebody.scrollTop:window.pageYOffset);
        mousex = (ie5)?event.x:(ns6)?clientX = e.clientX:false;
        mousey = (ie5)?event.y:(ns6)?clientY = e.clientY:false;

        var lixlpixel_tooltip = document.getElementById('tooltip');
        lixlpixel_tooltip.style.left = (mousex+pagex+offsetx) + 'px';
        lixlpixel_tooltip.style.top = (mousey+pagey+offsety) + 'px';
    }
}

function tttooltip(tip) {
    if(!document.getElementById('tooltip')) ttnewelement('tooltip');
    var lixlpixel_tooltip = document.getElementById('tooltip');
    lixlpixel_tooltip.innerHTML = tip;
    lixlpixel_tooltip.style.display = 'block';
    document.onmousemove = ttgetmouseposition;
}

function ttexit() {
    document.getElementById('tooltip').style.display = 'none';
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );
  
  /*
  if the expires variable is set, make the correct 
  expires time, the current script below will set 
  it for x number of days, to make it for hours, 
  delete * 24, for minutes, delete * 60 * 24
  */
  if ( expires )
  {
  expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );
  
  document.cookie = name + "=" +escape( value ) +
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
  ( ( path ) ? ";path=" + path : "" ) + 
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	

function Delete_Cookie( name, path, domain ) {
  if ( Get_Cookie( name ) ) document.cookie = name + "=" +
  ( ( path ) ? ";path=" + path : "") +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/*
function sesamo(id) {
  var stato = document.getElementById(id).style.display;
  //alert(stato);
  if(stato == 'none') {
		//mySlider.slideIn();
	  document.getElementById(id).style.display='';
    Set_Cookie('opentitle' + id, 1);
  }
  else {
	  var mySlider = new Fx.Slide(id, {duration: 1000});
  	//mySlider.slideOut();
  	document.getElementById(id).style.display='none';
    Delete_Cookie('opentitle' + id);
  }  
}
*/

function sesamo(id) {
  var stato = document.getElementById(id).style.display;
  if(stato == 'none') {
		new Effect.BlindDown(id);
	  //document.getElementById(id).style.display='';
    Set_Cookie('opentitle' + id, 1);
  }
  else {
    new Effect.BlindUp(id);
    //document.getElementById(id).style.display='none';
    Delete_Cookie('opentitle' + id);
  }  
}

function mostraalbero(id_nodo, tipo) {
  var stato = document.getElementById('alberoposizioni').style.display;
  if(stato == 'none') {
    new Effect.BlindDown('alberoposizioni');
  }
  new Ajax.Updater('alberoposizioni','ajax_sposta.php?ID_nodo='+id_nodo+'&tipo='+tipo)
}

function checkDate(d,m,y) {
	var myDayStr = d;
	var myMonthStr = m;
	var myYearStr = y;

	var myDate = new Date();
	myDate.setFullYear( myYearStr, myMonthStr-1, myDayStr );
	
	if ( myDate.getMonth()+1 != myMonthStr ) {
  	return false;
	} else {
  	return true;
	}
}

function ControllaEMAIL(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);var res=1;if (str.indexOf(at)==-1){
		res=0;
	}
	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		res=0;
	}
	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		res=0;
	}
	else if (str.indexOf(at,(lat+1))!=-1){
		res=0;
	}
	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		res=0;
	}
	else if (str.indexOf(dot,(lat+2))==-1){
		res=0;
	}
	else if (str.indexOf(" ")!=-1){
		res=0;
	}
	if(res==1) {
		return true;
	}
	else {
		return false;
	}					
}

function ControllaCF(cf)
{
    var validi, i, s, set1, set2, setpari, setdisp;
    if( cf == '' )  return '';
    cf = cf.toUpperCase();
    if( cf.length != 16 )
        return "La lunghezza del codice fiscale non ?\n"
        +"corretta: il codice fiscale dovrebbe essere lungo\n"
        +"esattamente 16 caratteri.\n";
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++ ){
        if( validi.indexOf( cf.charAt(i) ) == -1 )
            return "Il codice fiscale contiene un carattere non valido `" +
                cf.charAt(i) +
                "'.\nI caratteri validi sono le lettere e le cifre.\n";
    }
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for( i = 1; i <= 13; i += 2 )
        s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    for( i = 0; i <= 14; i += 2 )
        s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
        return "Il codice fiscale non ? corretto:\n"+
            "il codice di controllo non corrisponde.\n";
    return "";
}

function ControllaPIVA(pi)
{
    if( pi == '' )  return '';
    if( pi.length != 11 )
        return "La lunghezza della partita IVA non ?\n" +
            "corretta: la partita IVA dovrebbe essere lunga\n" +
            "esattamente 11 caratteri.\n";
    validi = "0123456789";
    for( i = 0; i < 11; i++ ){
        if( validi.indexOf( pi.charAt(i) ) == -1 )
            return "La partita IVA contiene un carattere non valido `" +
                pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
    }
    s = 0;
    for( i = 0; i <= 9; i += 2 )
        s += pi.charCodeAt(i) - '0'.charCodeAt(0);
    for( i = 1; i <= 9; i += 2 ){
        c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
        if( c > 9 )  c = c - 9;
        s += c;
    }
    if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
        return "La partita IVA non ? valida:\n" +
            "il codice di controllo non corrisponde.\n";
    return '';
}
	
function checkIscrizione(){
	var cognome = document.getElementById("cognome").value;
	var nome = document.getElementById("nome").value;
	var natoa = document.getElementById("natoa").value;
	var natoprov = document.getElementById("natoprov").value;
	var natoil = document.getElementById("natoil").value;
	var cf = document.getElementById("cf").value;
	var indirizzo_p = document.getElementById("indirizzo_p").value;
	var cap_p = document.getElementById("cap_p").value;
	var citta_p = document.getElementById("citta_p").value;
	var provincia_p = document.getElementById("provincia_p").value;
	var email = document.getElementById("email").value;
	
	var professione = document.getElementById("professione").value;
	var disciplina = document.getElementById("disciplina").value;
	var specialita = document.getElementById("specialita").value;
	var interessi = document.getElementById("interessi").value;

	var regolamento = document.getElementById("regolamentosi").checked;
	
	//check session
	var messaggio = "compilare il campo segnalato";
	
	if ((cognome == "") || (cognome== "undefined")) {
		document.getElementById("cognome").style.backgroundColor = "#FFCCCC";
		document.getElementById("cognome").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((nome == "") || (nome == "undefined")) {
		document.getElementById("nome").style.backgroundColor = "#FFCCCC";
		document.getElementById("nome").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((natoa == "") || (natoa == "undefined")) {
		document.getElementById("natoa").style.backgroundColor = "#FFCCCC";
		document.getElementById("natoa").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((natoprov == "") || (natoprov == "undefined")) {
		document.getElementById("natoprov").style.backgroundColor = "#FFCCCC";
		document.getElementById("natoprov").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((natoil == "") || (natoil == "undefined")) {
		document.getElementById("natoil").style.backgroundColor = "#FFCCCC";
		document.getElementById("natoil").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((cf == "") || (cf == "undefined") || ControllaCF(cf) != "") {
		document.getElementById("cf").style.backgroundColor = "#FFCCCC";
		document.getElementById("cf").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((indirizzo_p == "") || (indirizzo_p == "undefined")) {
		document.getElementById("indirizzo_p").style.backgroundColor = "#FFCCCC";
		document.getElementById("indirizzo_p").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((cap_p == "") || (cap_p == "undefined")) {
		document.getElementById("cap_p").style.backgroundColor = "#FFCCCC";
		document.getElementById("cap_p").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((citta_p == "") || (citta_p == "undefined")) {
		document.getElementById("citta_p").style.backgroundColor = "#FFCCCC";
		document.getElementById("citta_p").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((provincia_p == "") || (provincia_p == "undefined")) {
		document.getElementById("provincia_p").style.backgroundColor = "#FFCCCC";
		document.getElementById("provincia_p").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((email == "") || (email == "undefined")) {
		document.getElementById("email").style.backgroundColor = "#FFCCCC";
		document.getElementById("email").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if (ControllaEMAIL(email)==false) {
		messaggio="email non valida";
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((professione == "") || (professione == "undefined")) {
		document.getElementById("professione").style.backgroundColor= "#FFCCCC";
		document.getElementById("professione").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((disciplina == "") || (disciplina == "undefined")) {
		document.getElementById("disciplina").style.backgroundColor= "FFCCCC";
		document.getElementById("disciplina").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((specialita == "") || (specialita == "undefined")) {
		document.getElementById("specialita").style.backgroundColor= "FFCCCC";
		document.getElementById("specialita").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if ((interessi == "") || (interessi == "undefined")) {
		document.getElementById("interessi").style.backgroundColor= "FFCCCC";
		document.getElementById("interessi").focus();
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} else if (regolamento == false) {
		document.getElementById("regolamento_msg").style.backgroundColor= "red";
		document.getElementById("messaggio").innerHTML=messaggio;
		return false;
	} 
	else {
	  document.getElementById("messaggio").innerHTML="oK!";
		return true;
	}
	
}


function cambiaPagina(topage) {
  new Ajax.Updater('annuncihome','ajax_annuncihome.php?topage='+topage);
}
