var	hroot;
var	language	= "italian";
var	active		= "main";

l_showdebug = 'n';
v_br = navigator.appName;
v_br_version = navigator.userAgent.toLowerCase();
br_NS7    = v_br_version.indexOf('netscape/7') > -1;
br_OPERA  = v_br_version.indexOf('opera') > -1;
br_IE6 = false;
br_IE5 = false;
br_MOZZ = false;
br_SAFARI = false;
br_FIREFOX = false;

if ((v_br_version.indexOf('msie 6') > -1) && !br_OPERA)
{
  br_IE6 = true;
}
if ((v_br_version.indexOf('msie 5') > -1))
{
  br_IE5 = true;
}

if ((v_br_version.indexOf('mozilla') > -1) && !br_IE6)
{
  br_MOZZ = true;
}
if ((v_br_version.indexOf('safari') > -1) && !br_IE6 && br_MOZZ)
{
  br_SAFARI = true;
}
if ((v_br_version.indexOf('firefox') > -1) && !br_IE6 && br_MOZZ)
{
  br_FIREFOX = true;
}

l_debug("br_MOZZ = "+br_MOZZ+", br_IE5 = "+br_IE5+", br_SAFARI = "+			br_SAFARI+", br_FIREFOX = "+br_FIREFOX+", br_IE6 = "+br_IE6);


c_css_folder = "/css/";
c_js_folder = "/js/";
c_portale_path = "portale/";
c_portale_admin_path = "portale_admin/";
c_myunitopage ="portale/myunito.jsp";
c_bookmarkurlo ="bookmark";
c_invalidation_page = "pls/portal/unito.pkg_webcache.web_advinvalidate_page?p_uri=";
c_popup_admin_url = "pls/portal/unito.pkg_webcache.webcache_popup";

function l_debug (msg)
{
  if (l_showdebug=='y')
    alert("debug message"+msg);
}

function openNewWin (p_url,p_append_opener,p_title,p_width,p_height,
		     p_left,p_top)
{
  var	urlo = ""

  if ( p_append_opener == null)
    urlo = p_url;
  else
  {
    v_opener = ""+document.location;
    if (p_url.indexOf("?") > 0)
      urlo = p_url +"&p_opener_url="+ escape (v_opener);
    else
      urlo = p_url +"?p_opener_url="+ escape (v_opener); 
  } 
  childWin = window.open(urlo,p_title,"status=no,menubar=no,directories=no"+
			 ",resizable=yes,scollbars=yes,width="+p_width+
			 ",height="+p_height+",left="+p_left+",top="+p_top);
  childWin["opener"]= self;
  childWin.focus();
}

function openPrintPage(p_url,currentpage,newpage)
{
  childWin = window.open(p_url.replace(currentpage,newpage),
			 "Stampa_Pagina","toolbar=yes,menubar=no,status=no,"+
			 "scrollbars=1,resizable=1,width=800,height=600");
  childWin.opener= self;
  childWin.focus();
}

/******** start manage cookie ********/
var bikky = document.cookie;

function	getCookie(name)
{ // use: getCookie("name");	
  if (bikky == null) return null;
  var index = bikky.indexOf(name + "=");
  if (index == -1) return null;
  index = bikky.indexOf("=", index) + 1;
  var endstr = bikky.indexOf(";", index);
  if (endstr == -1) endstr = bikky.length;
  v_value = unescape(bikky.substring(index, endstr));
  if (v_value.length == 0 || v_value == "") return null;
  return v_value;
}

function	setCookie(name, value)
{ // use: setCookie("name", value);
  if (value != null && value != "")
    document.cookie=name + "=" + escape(value);
  bikky = document.cookie; // update bikky
}

function	setPersistentCookie(name,value,days)
{
  if (days != null)days = 1;  
  var exp = new Date()
  var expTime_l = exp.getTime() + (days * 24 * 60 * 60 * 1000)
  exp.setTime(expTime_l)   
  var expires = "; expires="+exp.toGMTString();  
  document.cookie = name+"="+value+expires+"; path=/";
  bikky = document.cookie; // update bikky
}

function removePersistentCookie(name)
{
  setPersistentCookie(name,"",-1);
}

/******** stop manage cookie ********/
/****** start css ****************/   

function	mustOpenMenu()
{    
  v_usercss   = getCookie("css_style");	

  return ((v_usercss.indexOf("_contrasto.css") == -1 ) && 
	  (v_usercss.indexOf("_testo.css") == -1));
	   
} 	    

function	changeStyle(p_css_path)
{
  if (p_css_path == "classica")
    removePersistentCookie("css_style");
  else
    setPersistentCookie("css_style", p_css_path,30) ;	 

  document.location.href = document.location; 
}	

function	setCSS(p_custom_path)
{
  var mycss=document.getElementById("cssref");

  if (mycss != null)
  {
    mycss.setAttribute("href",switchCSS(p_custom_path));
  }
}

function	switchCSS(p_custom_path)
{		 
  v_custom_path = c_css_folder;

  hroot = document.location.protocol+"//"+document.location.host;

  if (p_custom_path != null && p_custom_path != "undefined")
    v_custom_path = p_custom_path;

  v_usercss   = getCookie("css_style");	  

  if (v_usercss != null)
  {   
    l_debug("v_usercss : "+v_usercss); 	
      document.write ('<link href="'+c_css_folder+v_usercss+'" rel="stylesheet" type="text/css" />');   	
  } 
  else
  { 	
    if (br_IE6)
    {
      l_debug(" IE 6 ");
      if (document.location.href.indexOf("blue=1") > 0)
      {
	document.write ('<link href="'+c_css_folder+'iepc6.css" rel="stylesheet" type="text/css" />');   	
      }
      else
      {
	document.location.href="/html/browser.html";
//	document.write ('<link href="'+c_css_folder+'iepc6.css" rel="stylesheet" type="text/css" />');   	
      }
    }
    else
    {
      l_debug(" no explorer 6 ");
      if (br_IE5)	 
      {  
        l_debug(" explorer 5 ");
	if (document.location.href.indexOf("blue=1") > 0)
	{
	  /// fare un css per IE 5.5
	  document.write ('<link href="'+c_css_folder+'iepc6.css" rel="stylesheet" type="text/css" />');   	
	}
	else
	{
	  document.location.href="/html/browser.html";
	}
      }
      if (br_NS7)	 
      {  
	l_debug(" netscape 7 ");
	document.write ('<link href="'+c_css_folder+'netscape7.css" rel="stylesheet" type="text/css" />');   	
      }
      if (br_OPERA)	 
      {  
	l_debug(" opera ");
	document.write ('<link href="'+c_css_folder+'opera.css" rel="stylesheet" type="text/css" />');  
      } 
      if (br_SAFARI)	 
      {  
	l_debug(" safari ");
	document.write ('<link href="'+c_css_folder+'safari.css" rel="stylesheet" type="text/css" />');  
      }
      if (br_FIREFOX)	 
      {  
	l_debug(" firefox: "+c_css_folder+"firefox.css");
	document.write ('<link href="'+c_css_folder+'firefox.css" rel="stylesheet" type="text/css" />');  
      }
      if (br_MOZZ && !br_IE5 && !br_OPERA && !br_NS7 && !br_FIREFOX)	 
      {  
	l_debug(" mozilla: "+c_css_folder+"firefox.css");

	document.write ('<link href="'+c_css_folder+'firefox.css" rel="stylesheet" type="text/css" />');  
      }  
    }
    if (!br_IE6 && !br_IE5 && !br_NS7 && !br_OPERA && !br_MOZZ && 
    	!br_SAFARI && !br_FIREFOX)
    {
      l_debug(" browser version non ricosciuta");
      document.write ('<link href="'+c_css_folder+'global.css" rel="stylesheet" type="text/css" />');   	
    }     
  }    
}

/****** stop css ****************/   
function	printSpace(p_cnt,p_force)
{
  if(p_cnt >0 || p_force)
    document.write(" | ");
}

/***** start header *******/   

links_arr = new Array();
links_label_arr = new Array();

function writeHeader()
{  
  v_orasso    = getCookie("unito_unique_matricola");	
  v_usercss   = getCookie("css_style");	  
  document.write("<dl>");  

  if(v_orasso != null)
  {
    for (a=0;a<links_arr.length;a++)  
    {   
      if (links_arr[a].indexOf("_ingrandisci.css") > 0 && 
	  (links_arr[a] == v_usercss))
      {
	printSpace(a,false);
        document.write("<li><a href=\"#\" onClick=\""+
		       "javascript:changeStyle('classica')\">"+
		       "Diminuisci carattere</a></li>");         
      } 
      else if (links_arr[a].indexOf(".css") > 0 && (links_arr[a] == v_usercss))
      {
       printSpace(a,false);
       document.write("<li><a href=\"#\" onClick=\""+
		      "javascript:changeStyle('classica')\">Modalita\'"+
		      "classica</a></li>");             
      } 
      else if (links_arr[a].indexOf(".css") > 0)
      {
        printSpace(a,false);
        document.write("<li><a href=\"#\" onClick=\"javascript:changeStyle('"+
		       links_arr[a]+"')\">"+links_label_arr[a]+"</a></li>");         
      }     
      else 
      {
        currenturl = ""+document.location;     
        if (currenturl.indexOf(c_myunitopage) > 0)
        {
	  if(links_arr[a].indexOf(c_bookmarkurlo) == -1)
	  {
	    printSpace(a,false);   	  
	    document.write("<li><a href=\""+links_arr[a]+"\">"+
			    links_label_arr[a]+"</a></li>");
	  } 
        }
        else
        {
	  printSpace(a,false);
	  document.write("<li><a href=\""+links_arr[a]+"\">"+links_label_arr[a]+
	 		"</a></li>");            
        }  
      }       
    }
  } 
  else 
  {
    //for (a=0;a<4;a++)  
    for (a=0;a<4;a++)  
    {
      printSpace(a,false);

      if (links_arr[a].indexOf("_ingrandisci.css") > 0 && 
	  (links_arr[a] == v_usercss))
	document.write("<li><a href=\"#\" onClick=\"javascript:"+
		       "changeStyle('classica')\">Diminuisci "+
		       "carattere</a></li>");         
      else if (links_arr[a].indexOf(".css") > 0 && (links_arr[a] == v_usercss))
	document.write("<li><a href=\"#\" onClick=\""+
		       "javascript:changeStyle('classica')\">Modalita\' "+
		       "classica</a></li>");             
      else if (links_arr[a].indexOf(".css") > 0)
	document.write("<li><a href=\"#\" onClick=\"javascript:changeStyle('"+
		       links_arr[a]+"')\">"+links_label_arr[a]+"</a></li>"); 
      else 
	document.write("<li><a href=\""+links_arr[a]+"\">"+links_label_arr[a]+
		       "</a></li>");                        
    } 
  } 
  if (getCookie("unito_manage") == "manage")
  {
    printSpace(2,false);
    v_orasso = getCookie("unito_unique_matricola");

    if (v_orasso == null)
      v_orasso="public";

    v_location = ""+document.location;   				

    document.write('<li><a href="javascript:openNewWin(\''+c_popup_admin_url+
		   '\',\'y\',\'manage_webcache\',400,500,10,10);">'+
		   'Admin</a></li>');
  }
  document.write("</dl>");
} 

function	writeHeaderUser()
{  
 //v_orasso    = getCookie("unito_unique_matricola");	  
 //if(v_orasso != null)
 //{
 // v_name =   getCookie("unito_display_name");  
 // if(v_name == null) v_name = v_orasso;
 // document.write("Benvenuto "+v_name+" . ");   
 //}  

  v_name = "Leonardo MILANO";
  document.write("Benvenuto <b>"+v_name+"</b> . ");   
}   

function writeCurrentDate()
{
  var today = new Date();

  var year = today.getFullYear();

  var date = today.getDate();

  var todays_date = new Date;

  months = new Array("gennaio","febbraio","marzo","aprile","maggio","giugno","luglio","agosto","settembre","ottobre","novembre","dicembre");

  day = new Array("domenica","luned&igrave;","marted&igrave;","mercoled&igrave;","gioved&igrave;","venerd&igrave;","sabato");

  document.writeln(day[todays_date.getDay()]+ " , " + date + " " + 
		    months[todays_date.getMonth()] + " " + year);
}

/***** stop header *******/    	 
/***** start login/logout *******/ 

c_js_chpwdurl   = "pls/orasso/orasso.wwsso_app_user_mgr.change_password"; 
c_js_loginurl   = "pls/portal/PORTAL.wwsec_app_priv.login";
c_js_success_url = "pls/portal/unito.home"; 
c_js_logouturl  = "pls/orasso/orasso.wwsso_app_admin.ls_logout"; 

function	writeLogin()
{   
  document.write("<input type='hidden' name='p_requested_url' value='"+
		 c_js_success_url+"?p_success_url="+escape(document.location)+
		 "' >");
  document.write("<input type='hidden' name='p_cancel_url' value='"+
		 document.location+"' >");
  document.write("<label>");
  document.write("<input type='submit' class='button' value='Login' "+
		 "onClick='this.form.action=\""+c_js_loginurl+"\";'>");
  document.write("</label>");   
} 

function	writeLogout(v_is_in_home)
{
  document.write("<input type='hidden' name='p_done_url' value='"+
		 document.location+"' >");
  document.write("<label>");
  document.write("<input type='submit' class='button' value='Logout' "+
		 "onClick='this.form.action=\""+c_js_logouturl+"\";'>");
  document.write("</label>"); 
  if (v_is_in_home == "S")
  {
    document.write("<label>");
    document.write("<input type='submit' class='button' value='Cambia Pwd' "+
		   "onClick='this.form.action=\""+c_js_chpwdurl+"\";'>");  
    document.write("</label>"); 
  }
} 

function	writeLoginPrt(v_is_in_home)
{   
  v_orasso    = getCookie("unito_unique_matricola");	 

  if (v_orasso == null)
    writeLogin();
  else
    writeLogout(v_is_in_home);
}  

/***** stop login/logout *******/ 
/***** start myunito links *******/ 

 v_default_group=getCookie("unito_default_group");

 // start home page

 function	openMyUnitodiv()
 {  
   if (v_default_group != null)
     document.write('<div id="myunito'+v_default_group+'"> ');
   else
     document.write('<div id="myunito"> '); 
 }

 function	printLiMyUnito(p_cur_profile,p_curr_link,p_curr_title)
 { 
   if (v_default_group == null || v_default_group ==p_cur_profile)
   {
     document.write('<li> ');
     document.write('<a href="'+p_curr_link+'">'+p_curr_title+'</a> ');
     document.write('</li> ');
   }
   else  
   {
     document.write('<li class=disabilitato> ');
     document.write(p_curr_title);
     document.write('</li> ');
   }
 }

 function	closeMyUnitodiv()
 {
   document.write('</div>');
 }
 // stop home page
 // start interna

 function	printDtMyUnito(p_curr_profile,p_curr_img_name,
			       p_curr_img_src_off,p_curr_img_src_on,
			       p_curr_img_src_dis,p_curr_link,p_curr_title)
 {
   if (v_default_group == null || v_default_group == p_curr_profile)
   {
     document.write('<a href="'+p_curr_link+
		    '"  onmouseout="MM_swapImgRestore()" onmouseover="'+
		    'MM_swapImage(\''+p_curr_img_name+'\',\'\',\''+
		    p_curr_img_src_off+'\',0)">');
     document.write('<img src="'+p_curr_img_src_on+'" alt="'+p_curr_title+
		    '" border="0" name="'+p_curr_img_name+'" id="'+
		    p_curr_img_name+'"/>');
     document.write('</a>');
   }
   else
   {
     document.write('<img src="'+p_curr_img_src_dis+'" alt="'+p_curr_title+
		    '" border="0" name="'+p_curr_img_name+'" id="'+
		    p_curr_img_name+'"/>');
   }
 }

 // stop interna

/***** stop myunito links *******/ 
/********* start global utility ****************/

function	checkLanguage()
{ 
  if (getCookie("portal")&&getCookie("portal")!=null&&getCookie("portal")!="")
//if (getCookie("LOGIN_NLS") == null || getCookie("LOGIN_NLS") !="it-it")
  {
    splits= getCookie("portal").split("+");	
    lang= splits[2];
    if (document.referrer=="" && lang!="i")
    {
      //window.location.href="/pls/orasso/orasso.wwctx_app_language.set_language?p_http_language=it&p_nls_language=i&p_nls_territory=ITALY&p_requested_url="+escape("/pls/portal/portal.wwctx_app_language.set_language?p_http_language=it&p_nls_language=i&p_nls_territory=ITALY&p_requested_url="+escape(document.URL) );           
      window.location.href="pls/portal/portal.wwctx_app_language.set_language"+
			   "?p_http_language=it&p_nls_language=i&"+
			   "p_nls_territory=ITALY&p_requested_url="+
			   escape(document.URL);           
    } 
  }
}

checkLanguage();

function	openWindowEditor(url)
{
//  window.open(url,"editorWindow","fullscreen=yes,status=no,menubar=no,directories=no,resizable=no,scollbars=yes");
  window.open(url,"editorWindow","status=no,menubar=no,directories=no,"+
		  "resizable=no,scollbars=yes,width=750,height=550");
}

function	goTo(value)
{
  document.pageForm.p_page.value = value;
  document.pageForm.submit();
}

function	checkCacheView()
{  
  currenturl = document.location;
  if(currenturl != null) 
  {
   currenturl = ""+currenturl;
   v_indx = currenturl.indexOf(c_portale_path);
   if(v_indx > -1 && getCookie("unito_manage") == "manage") 
   {
    v_redirurl = currenturl.replace(c_portale_path,c_portale_admin_path);
    //alert("Impossibile visualizzare "+currenturl+"\ncome utente amministratore.\nSi verra' rediretti verso : "+v_redirurl+".");
    document.location.href=c_invalidation_page+escape(currenturl)+"&p_redirecturi="+escape(v_redirurl)+"&p_warmup=y";    
    return;
   }   
   v_indxportaleadmin = currenturl.indexOf(c_portale_admin_path);
   if(v_indxportaleadmin > -1 && getCookie("unito_manage") != "manage") 
   {
    v_redirurl = currenturl.replace(c_portale_admin_path,c_portale_path);    
    document.location.href=v_redirurl;
    return;
   }
  }   
}

function xLChangeFontColor (eL)
{
  eL.style.color = "#333333"
}

function textMod (e)
{
  xLChangeFontColor(e);
  e.value = '';
}

/********* stop global utility ****************/

function dsp(loc)
{
  if(document.getElementById)
  {
    var foc=loc.firstChild;
    foc=loc.firstChild.innerHTML?
    loc.firstChild:
    loc.firstChild.nextSibling;
    foc.innerHTML=foc.innerHTML=='+'?'-':'+';
    foc=document.getElementById("DIV_" + loc.id);
    foc.style.display=foc.style.display=='block'?'none':'block';
    loc.blur();
  }
}
if(!document.getElementById)
  document.write('<style type="text/css"><!--\n'+
    '.dspcont{display:block;}\n'+
    '//--></style>');

function showIntermedia()
{
  if ( !br_IE5 || !br_IE6 )
  {
    if (document.getElementById)
    {
      if (document.getElementById("myIntermedia") != null)
        document.getElementById("myIntermedia").style.visibility="visible";
    }
  }
}

function hideIntermedia()
{
  if ( !br_IE5 || !br_IE6 )
  {
    if (document.getElementById)
    {
      if (document.getElementById("myIntermedia") != null)
        document.getElementById("myIntermedia").style.visibility="hidden";
    }
  }
}

function	loadinner(where,str)
{
  var	mt	= document.getElementById(where);

  if (mt)
  {
    debug("setting "+where+" to "+str);
    mt.innerHTML = str;
    debug("done setting "+where+" to "+str);
  }
}

function	changeMenu(itm)
{
  var	it = 'menu'+itm;
  var	lit = 'lang'+itm;
  var	noit = 'nomenu'+itm;
  var	itid;
  var	ittxt;
  var	itact;
  var	mylist=document.getElementsByTagName("span");

  for (i=0; i<mylist.length; i++)
  {
    if (mylist[i].getAttribute("type") && 
	mylist[i].getAttribute("type") == 'menuitem')
    {
      itid = mylist[i].getAttribute("id");
      ittxt = mylist[i].getAttribute("item_"+language);
      itact = mylist[i].getAttribute("action");
      if (itid == it)
      {
        mylist[i].innerHTML = ittxt;
	specific(itact);
	active = itact;
      }
      else
      {
//        mylist[i].innerHTML = "<a href=\"javascript:testinit("+
//	      "'"+hroot+"/html/"+language+"/inner"+itact+
//	      ".html');changeMenu('"+itact+"');\">"+ittxt+"</a>";
        mylist[i].innerHTML = "<a href=\"javascript:testinit("+
	      "'inner"+itact+".html');changeMenu('"+
	      itact+"');\">"+ittxt+"</a>";
  
      }
    }
  }
}

function	changeLang(itm)
{
  var	lit = 'lang'+itm;
  var	itid;
  var	ittxt;
  var	itact;
  var	mylist=document.getElementsByTagName("span");

  for (i=0; i<mylist.length; i++)
  {
    if (mylist[i].getAttribute("type") && 
	mylist[i].getAttribute("type") == 'langitem')
    {
      itid = mylist[i].getAttribute("id");
      ittxt = mylist[i].getAttribute("item");
      itact = mylist[i].getAttribute("action");

      if (itid == lit)
      {
        mylist[i].innerHTML = ittxt;
      }
      else
      {
	mylist[i].innerHTML = "<a href=\"javascript:setlang('"+itact+"');"+
			      "changeLang('"+itact+"');\">"+ittxt+"</a>";
      }
  
    }
  }
//  testinit(hroot+"/html/"+language+"/inner"+active+".html");
  testinit("inner"+active+".html");
//  testinit2(hroot+"/html/"+language+"/topmenu.html","topheader");
  changeMenu(active);
}

function	enableLeftMenu()
{
  var	lmnu;

  lmnu = document.getElementById("leftmenu");

  alert("enableLeftMenu called");
  if (lmnu != null)
  {
    lmnu.style.visibility = "visible";
  }
}

function setlang(lang)
{
  language = lang
}


function testcss(css)
{
  var	hd	= document.getElementsByTagName("head");
  var	newcss	= null;
  
  if (hd[0] != null)
  {
    alert("adding "+css);

    newcss = document.createElement('link');
    newcss.setAttribute("rel","stylesheet");
    newcss.setAttribute("href",css);
    newcss.setAttribute("type","text/css");
    hd[0].appendChild(newcss);
  }
}

