function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}


function focus_sel(obj) {
	d=document.getElementById('nav').getElementsByTagName('a');	
	if (d){
	for  (var i=0; i<d.length; i++) {
		d[i].style.color = "#FFFFFF";
		d[i].style.textDecoration = "none";
		d[i].style.border= '0px dotted'; 
		if(d[i].parentNode.nodeName!="H2") {
			d[i].style.fontWeight = "normal";	
		}
	}}

	selected=document.getElementById(obj);
	if (selected){
	selected.style.fontWeight = "bold";
	selected.style.textDecoration = "none";
	selected.style.color = "#95A9F2";/*"#96FF32";*/
	selected.style.border= '1px dotted'; 
	selected.style.bordercolor='#FFFF99'; 
	}
}


  $(document).ready(function(){
			$("h2").live("click",function(){
				hideAll();
				$(this).siblings("ul").show('slow');
			});

  });

$(document).ready(function()
		{
		
			$("div.jsenable ul ul").hide();	// chiude in partenza tutti i menu
			
			//apre in partenza menu e sottomenu della pagina caricata
			var id = $('span.open_section').attr('name');
			if (id != undefined)
			{
				$("h2." + id).siblings("ul").show('slow');
			}			
			var id2 = $('span.open_subsection').attr('name');
			if (id2 != undefined)
			{
				$("h3." + id2).siblings("ul").show('slow');
			}
		});

function hideAll()
		{
			$("div.jsenable ul ul").hide('slow');
		}		
	
		
/*Dinamic pages*/



function showMenu(menu,submenu){
//
	/*i parametrimenu e submenu servono a far aprire e chiudere le sezioni
	del menu java sulla sinistra*/
	
	/*senza parametri non chiude nessun menu. in questo modo il menu rimane fisso quando 
	si passa da una pagina all'altra all'interno della stessa scheda*/
	if (menu=='')	{}
	/*quando si seleziona un elemento della scheda modelli, che ha un sottomenu, devono venire
	chiuse tutte le altre schede del menu principale, e le altre sottoschede all'interno del menu
	MODELLI*/
	else {
		if (menu==3) {
			$("div.jsenable ul ul ul").hide('slow');
			for (var i = 1; i<=10; i++) {
				if (("h2." + i) && i!=menu) {
					$("h2." + i).siblings("ul").hide('slow');
				}
			}
		}
		else{
			//chiude tutti i menu
			
			hideAll();
		};
		
		
	}

	/*rende visibili MENU e SOTTOMENU della pagina che viene caricata come contenuto*/
	if (menu!=""){	$("h2." + menu).siblings("ul").show('slow');}
	if (submenu!=""){ $("h3." + submenu).siblings("ul").show('slow');}

}

var actualPage="";
var lang = 'IT';
var LastVisited = "";
var LastMenu = "";
var LastSubmenu = "";
var LastFocus = "";



function showPage(targetPage,menu,submenu,focus){

	LastVisited = targetPage;
	LastFocus = focus;
		
	if (menu!='') LastMenu = menu;
	else if (focus!='') LastMenu = focus.substr(0,1);
	else LastMenu = "";
	if (submenu!='') LastSubmenu = submenu;
	else if (focus!='') LastSubmenu = focus.substr(0,2);
	else LastSubmenu ="";
	createCookie('LastVisit',LastVisited,1);
	createCookie('os',LastMenu,1);
	createCookie('oss',LastSubmenu,1);

	
	if (targetPage!='start.html')
		showMenu(menu,submenu);
	else hideAll();

	try{
	if (focus!='') focus_sel(focus);
	else focus_sel();
	}catch(err){alert("error")};

//CARICA LA PAGINA DEI CONTENUTI
	actualPage="./contenuti/" + lang + "/" + targetPage;
		
	//Main section
	/*http_main.open("get",actualPage);
	http_main.onreadystatechange = handleResponseMain; 
	http_main.send(null);*/
	
	frames['MainFrame'].location.href=actualPage;
}

var http_main = createRequestObject(); 
var http_menu = createRequestObject(); 
var http_top = createRequestObject(); 

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		// Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		// Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

function handleResponseMain(){

	if(http_main.readyState == 4){ //Finished loading the response
		var response = http_main.responseText;
		var doc=frames["MainFrame"].document;
		doc.open("text/html","replace");
		doc.writeln(response);
		doc.close();
	}
}
function handleResponseMenu(){
	if(http_menu.readyState == 4){ //Finished loading the response
		var response = http_menu.responseText;
		document.getElementById('nav').innerHTML = response;
		hideAll();
	}
}

function handleResponseTop(){
	if(http_top.readyState == 4){ //Finished loading the response
		var response = http_top.responseText;
		document.getElementById('divTop').innerHTML = response;
	}
}
/*ENDDinamic pages*/


function changeLanguage(language){
	lang=language;
	
	window.location.assign("main.php?pg=" + LastVisited +"&lang="+lang + "&os="+LastMenu+ "&oss="+LastSubmenu);

	if (LastFocus!='') focus_sel(LastFocus);
	else focus_sel();
	
	/*
	http_menu.open('get',"contenuti/" + language+"/menu.php");
	http_menu.onreadystatechange = handleResponseMenu; 
	http_menu.send(null);

	showMenu(LastMenu,LastSubmenu,LastFocus);

	http_top.open('get',"contenuti/" + language+"/top.html");
	http_top.onreadystatechange = handleResponseTop; 
	http_top.send(null);
	
	if (LastVisited != "")
		frames['MainFrame'].location.href="./contenuti/" + lang + "/" + LastVisited;
		
	else
		frames['MainFrame'].location.href="./contenuti/" + lang + "/start.html";*/
}
