
// Menu Functions
// Nicola Zordan, Wednesday 21 February 2001
// Nicola Zordan, 12:39 6/13/2002, Validity Dates.

function MenuItem() {
  this.Label='';
  this.URL='';
  this.Icon='';
  this.FromDate=null;
  this.ToDate=null;
//  return (this);
};
MenuItem.prototype = new MenuItem;

function DateYMD(year,month,day) {
var d=new Date(year,month-1,day)
 return(d);
};

function newMenuItem(Label, URL) {
var m = new MenuItem();
  m.Label=Label;
  m.URL=URL;
//alert(m.Label)
  return(m);
};

function newMenuItemIcon(Label, URL, Icon) {
var m = new newMenuItem(Label,URL);
  m.Icon=Icon;
  return(m);
};

function WriteVerticalMenu (menu) {
var m,n;
 for (n in menu) {
  m=menu[n];
//alert(m);
  document.write('<A HREF="'+m.URL+'">');
  if (m.Icon != '') { document.write('<IMG SRC="'+m.Icon+'" alt="'+m.Label+'">'); };
  document.write(m.Label);
  document.write('</A>'+'<br>');
 };
};
function WriteOrizzontalMenu (menu) {
var m,n;
 for (n in menu) {
  m=menu[n];
  document.write('<A HREF="'+m.URL+'">');
  document.write(m.Label);
  document.write('</A>'+' ');
 };
};
function WriteTableMenu (menu) {
var m,n;
 document.write('<TABLE BORDER="0">');
 for (n in menu) {
  m=menu[n];
  document.write('<TR><TD>');
  document.write('<A HREF="'+m.URL+'">');
  document.write(m.Label);
  document.write('</A>'+'');
  document.write('</TD></TR>');
 };
 document.write('</TABLE>');
};

function WriteMenu (menu) {
// WriteVerticalMenu(menu);
// WriteVerticalCustomMenu (menu);
  if (!PDApage) {
    WriteCustomMenu(menu);
    //WriteCustomMenuPDA(menu);
   } else {
    WriteCustomMenuPDA(menu);
  };
};

function Array2MenuItems (menuArray) {
//Array like [['Label','URL'], ['Label','URL'], ... ['Label','URL']]
var n,menu,m;
  menu= new Array();
  for (n in menuArray) {
//    Menu[menu.length]= newMenuItem(menuArray[n][0],menuArray[n][1]);
    m=menuArray[n];
//alert(m[0]);
    menu[menu.length]= newMenuItem(m[0],m[1]);
  };
  return(menu);
};


function Array4MenuItems (menuArray) {
//Array like [['Label','URL',fromDate,toDate], ['Label','URL',fromDate,toDate], ... ['Label','URL',fromDate,toDate]]
var n,menu,m;
  menu= new Array();
  for (n in menuArray) {
//    Menu[menu.length]= newMenuItem(menuArray[n][0],menuArray[n][1]);
    m=menuArray[n];
//alert(m[0]);
    menu[menu.length]= newMenuItem(m[0],m[1]);
//alert(m[0]+'\n'+m.length+'\n'+m[2]+'\n'+m[3]+'\n'+menu[menu.length-1]);
    if (m.length>2) menu[menu.length-1].FromDate=m[2];
    if (m.length>3) menu[menu.length-1].ToDate=m[3];
  };
  return(menu);
};



var CustomMenuItemBefore, CustomMenuItemAfter;
CustomMenuItemBefore='<table><tr><td>';
CustomMenuItemAfter='</td></tr></table>';
CustomMenuItemURL='';
CustomMenuLabelBefore='<b><font 0face="Arial" onMouseOver="this.color=\'red\'" onMouseOut="this.color=\'\'">';
CustomMenuLabelAfter='</b></font>';

var CustomMenuURLBefore, CustomMenuURLAfter;
CustomMenuURLBefore='';
CustomMenuURLAfter='';

CustomMenuSelectColor='yellow';
CustomMenuCurrentColor='cyan';
function WriteVerticalCustomMenu0 (menu) {
var m,n;
 document.write('<table border="0">');
 for (n in menu) {
  m=menu[n];
//alert(m);
  if (m.Label!='') {
   document.write('<tr><td onMouseOver="this.bgColor=\''+CustomMenuSelectColor+'\'" ');
   if (m.URL == GetURLFileName(document.URL)) 
      { document.write(' bgColor="'+CustomMenuCurrentColor+'" onMouseOut="this.bgColor=\''+CustomMenuCurrentColor+'\'" '); }
     else { document.write(' onMouseOut="this.bgColor=\'\'" '); };
   document.write(' >');
//   document.write('<tr><td onMouseOver="this.bgColor=\'yellow\'" onMouseOut="this.bgColor=\'\'">');
   document.write(CustomMenuItemBefore);
   document.write('<A HREF="'+m.URL+'" '+CustomMenuItemURL+'>');
   if (m.Icon != '') { document.write('<IMG SRC="'+m.Icon+'" alt="'+m.Label+'">'); };
   document.write(CustomMenuLabelBefore+m.Label+CustomMenuLabelAfter);
   document.write('</A>'+'<br>');
   document.write(CustomMenuItemAfter);
   document.write('</td></tr>');
   }
   else {
//   document.write('<br>');
   document.write('<tr><td> &nbsp; </td></tr>');
  };
 };
 document.write('</table>');
};

function IsURLThisPage (URL) {
  //alert(URL+'\n'+GetURLFileName(document.URL));
  if (CustomMenuFrame=='') return (URL==GetURLFileName(document.URL));
  return (URL==GetURLFileName(parent.frames[CustomMenuFrame].document.URL));
};

function URLremoveParameters(url) {
var parameter;
var URLFileName=url;
 parameter=URLFileName.lastIndexOf('?');
 if (parameter>0) {
   URLFileName=URLFileName.substring(0,parameter);
   //alert(URLFileName);
 };
 return URLFileName;
};

function GetURLFileName0 (URL) {
//var d,p,u,URLFileName;
var slash;
 if (URL.substring(0,7)=='file://') 
   { slash=URL.lastIndexOf('\\'); }
  else { slash=URL.lastIndexOf('/'); };
// slash=URL.lastIndexOf('/');
 URLFileName=URL.substring(slash + 1,URL.length);
 if (URLFileName=='') URLFileName='index.htm';
 return (URLFileName);
// return URL.substring(slash + 1,URL.length);
};

function GetURLFileName (URL) {
//var d,p,u,URLFileName;
var slash;
 if (URL.substring(0,7)=='file://') 
   { slash=URL.lastIndexOf('\\'); }
  else { slash=URL.lastIndexOf('/'); };
// slash=URL.lastIndexOf('/');
 URLFileName=URL.substring(slash + 1,URL.length);
 URLFileName=URLremoveParameters(URLFileName);
 if (URLFileName=='') URLFileName='index.htm';
 return (URLFileName);
// return URL.substring(slash + 1,URL.length);
};


var CustomMenuSelectColor='yellow';
var CustomMenuNormalBgColor='';
var CustomMenuLabelBefore='<font face="Arial">';
var CustomMenuLabelAfter='</font>';
var CustomMenuAttributes='';
var CustomMenuCornerTL='corner_tl_silver.gif';
//CustomMenuCornerTL='';
CustomMenuCornerTL='';
var CustomMenuCornerTR='corner_tr_silver.gif';
var CustomMenuCornerBR='corner_br_silver.gif';
var CustomMenuCornerBL='corner_bl_silver.gif';
var CustomMenuFixedWidth=true;
var CustomMenuOrizzontal=false;
CustomMenuFixedWidth=false;
//CustomMenuFixedWidth=true;
//CustomMenuOrizzontal=true;
//var CustomMenuOrizzontalText='';
var CustomMenuText='';
//CustomMenuText='Main orizzontal menu';
//CustomMenuText=' ';
var CustomMenuFrame='';
//CustomMenuFrame='page';
var CustomMenuSpacer='MenuSpacer.gif';
//CustomMenuSpacer='';
//CustomMenuSpacer='image';



function ReloadMenu () {
  location.reload();
//  NoBakground() 
};

//div

//var CustomMenuLocalLinkColor='navy';
var CustomMenuExternalLinkColor='gray';
var CustomMenuExternalLinkTarget='_blank';
//var CustomMenuExternalLinkTarget=''; // no external detection
//var CustomMenuExternalLinkTarget='window2';

function WriteCustomMenu (CustomMenu) {
var Today=new Date(), CustomMenuWritten=0;
var ExternalLink=false;
// document.write('<table border="0" cellspacing="0" cellpadding="0">');
 document.write('<table 0border=1 border="0" 0cellspacing="2" cellspacing="0" cellpadding="0">');
 if (CustomMenuOrizzontal) { document.write('<tr> '); };
 for (i=0; i<CustomMenu.length-1; i++) {
//alert(Today+'\n'+CustomMenu[i].FromDate+'\n'+CustomMenu[i].ToDate);
//alert((CustomMenu[i].FromDate!=null)+'\n'+(CustomMenu[i].FromDate>Today));

  if (CustomMenu[i].FromDate!=null && CustomMenu[i].FromDate>Today) continue;
  if (CustomMenu[i].ToDate!=null && CustomMenu[i].ToDate<Today) continue;
//  if (CustomMenu[i].ToDate!=null && CustomMenu[i].ToDate<(Today+1)) continue;

   if (!CustomMenuOrizzontal) { document.write('<tr> '); };

//   document.write('<tr><td ');
   document.write('<td ');

   CustomMenuWritten=CustomMenuWritten+1;
   ExternalLink=((CustomMenuExternalLinkTarget!='') && (CustomMenu[i].URL.substring(0,7).toLowerCase()=='http://'));

   if (CustomMenu[i].URL!='' && (CustomMenuFixedWidth || CustomMenuCornerTL=='') && (!CustomMenuOrizzontal)) {
    if (IsURLThisPage(CustomMenu[i].URL)) {
      document.write('onMouseOver="this.bgColor=\''+CustomMenuSelectColor+'\'" onMouseOut="this.bgColor=\''+CustomMenuCurrentColor+'\'" bgColor="'+CustomMenuCurrentColor+'"'); 
     }
     else {
      document.write('onMouseOver="this.bgColor=\''+CustomMenuSelectColor+'\'" onMouseOut="this.bgColor=\''+CustomMenuNormalBgColor+'\'" bgColor="'+CustomMenuNormalBgColor+'"'); 
     };
   };
   if (CustomMenuOrizzontal) { document.write(' valign="bottom" ');};
   document.write(' >');
//   document.write('  valign="center">');

   if ((CustomMenuCornerTL!='' || CustomMenuOrizzontal)  && CustomMenu[i].URL!='') {
    document.write('<table 0border=1 border="0" cellspacing="0" cellpadding="0" ');
    if (CustomMenuFixedWidth) { document.write(' width="100%" 0height=40 0height="100%" '); };
    if (CustomMenu[i].URL!='') {
      if (IsURLThisPage(CustomMenu[i].URL)) {
       document.write('onMouseOver="this.bgColor=\''+CustomMenuSelectColor+'\'" onMouseOut="this.bgColor=\''+CustomMenuCurrentColor+'\'" bgColor="'+CustomMenuCurrentColor+'"'); 
      }
      else {
       document.write('onMouseOver="this.bgColor=\''+CustomMenuSelectColor+'\'" onMouseOut="this.bgColor=\''+CustomMenuNormalBgColor+'\'" bgColor="'+CustomMenuNormalBgColor+'"'); 
      };
    };
    if (CustomMenuOrizzontal) { document.write(' 0height=40 ');};
    document.write(' >');

    document.write('<tr>');
    document.write('<td valign="top">');
    if (CustomMenuCornerTL!='') {document.write('<img src="'+CustomMenuCornerTL+'" border="0" align="left" hspace="0" vspace="0">');};
    document.write('</td>');

    document.write('<td rowspan="2" 0valign="center" ');
    if (CustomMenuFixedWidth) { document.write(' width="100%" '); };
    if (CustomMenuOrizzontal) { document.write(' height="100%"  '); };
    if (CustomMenuOrizzontal) { document.write(' valign="bottom" ');}
      else {document.write(' valign="center" ');};
    document.write(' >'); 
   };

   if (CustomMenu[i].URL!='') {
// Hiperlink to the fill width of the background !!!
    document.write('<table width="100%" cellpadding="0" cellspacing="0"><tr><td>');
    document.write(CustomMenuURLBefore);
    document.write('<A HREF="'+CustomMenu[i].URL+'" '+CustomMenuAttributes+' ');
    if (CustomMenuFrame!='') {
      //if (CustomMenu[i].URL.substring(0,7)=='http://') { document.write(' target="_top"')}
      if (ExternalLink) { document.write(' target="_top"')}
       else {document.write(' target="'+CustomMenuFrame+'"');};
      document.write(' onClick="ReloadMenu();"');
     } else {
      if (ExternalLink) { 
       //document.write(' target="_blank"')
       document.write(' target="'+CustomMenuExternalLinkTarget+'"')
      };
    };
    document.write(' >');
   };
// Hiperlink to the fill width of the background !!!
   document.write('<div 0width="100%">');
   if (CustomMenu[i].Icon!='') {
    document.write('<IMG SRC="'+CustomMenu[i].Icon+'">');
   };
   if (ExternalLink) { 
     document.write('<font color="'+CustomMenuExternalLinkColor+'">');
   };
   document.write(CustomMenuLabelBefore);
   document.write(CustomMenu[i].Label);
   document.write(CustomMenuLabelAfter);
   if (ExternalLink) { 
     document.write('</font>');
   };
// Hiperlink to the fill width of the background !!!
   document.write('</div>');
   if (CustomMenu[i].URL!='') {
    document.write('</A>');
    document.write(CustomMenuURLAfter);
// Hiperlink to the fill width of the background !!!
    document.write('</td></tr></table>');
   };
//   document.write('&nbsp;'); 
//   document.write('<br>');

   if ((CustomMenuCornerTL!='' || CustomMenuOrizzontal) && CustomMenu[i].URL!='') {
    document.write('</td>');
    document.write('<td valign="top">');
    if (CustomMenuCornerTR!='') {document.write('<img src="'+CustomMenuCornerTR+'" border="0" align="right" hspace="0" vspace="0">');};
    document.write('</td>');
 
    document.write('</tr><tr>');

    document.write('<td valign="bottom">');
    if (CustomMenuCornerBL!='') {document.write('<img src="'+CustomMenuCornerBL+'" border="0" align="left" hspace="0" vspace="0">');};
    document.write('</td>');

    document.write('<td valign="bottom">');
    if (CustomMenuCornerBR!='') {document.write('<img src="'+CustomMenuCornerBR+'" border="0" align="right" hspace="0" vspace="0">');};
    document.write('</td>');

    document.write('</tr>');

    document.write('</table>');
   }
   else {
   };

//   document.write('');
//   document.write('</td></tr>');
   document.write('</td>');

   if (CustomMenuOrizzontal && CustomMenuSpacer!='') {
     document.write('<td valign="bottom">');
     document.write('<img src="'+CustomMenuSpacer+'" border="0" align="left" hspace="0" vspace="0">');
     document.write('</td>');
     CustomMenuWritten=CustomMenuWritten+1;
   };

   if (!CustomMenuOrizzontal) { 

 if (!CustomMenuOrizzontal && (i==0)) {
   if (CustomMenuText!='') {
//    document.write('<td rowspan="'+CustomMenuWritten+'" bgColor="'+CustomMenuCurrentColor+'"> '); 
    document.write('<td rowspan="'+(CustomMenu.length*2)+'" bgColor="'+CustomMenuCurrentColor+'"> '); 
    document.write(CustomMenuText+'&nbsp &nbsp &nbsp '); 
    document.write('<br>'); 
    document.write('</td> '); 
   };
 };



     document.write('</tr> '); 

     if (!CustomMenuOrizzontal && CustomMenuSpacer!='') {
      document.write('<tr>');
      document.write('<td valign="bottom">');
      document.write('<img src="'+CustomMenuSpacer+'" border="0" align="left" hspace="0" vspace="0">');
      document.write('</tr>');
      CustomMenuWritten=CustomMenuWritten+1;
     };
   };
 };
 if (CustomMenuOrizzontal) { 
   document.write('</tr> '); 

   if (CustomMenuText!='') {
    document.write('<tr> '); 
    document.write('<td colspan="'+CustomMenuWritten+'" bgColor="'+CustomMenuCurrentColor+'"> '); 
    document.write(CustomMenuText+'&nbsp'); 
    document.write('<br>'); 
    document.write('</td> '); 
    document.write('</tr> '); 
   }
 };
 document.write('</table>');
};


// --- PDA ---

//var PDApage=false;
function GetPageParameters () {
  var url=document.URL;
  //var url=window.location+'';
  //alert(document.URL+'\n'+window.location);
  var ParametersPassed=(url.indexOf('?')>=0);
  var ParametersJS='';
  var PathParameter='';
  if (ParametersPassed) {
    ParametersJS=url.substring(document.URL.indexOf('?')+1);
    eval(ParametersJS);
  };
};
GetPageParameters();

var PDA, PDApage;
function UsePDAmenu () {
  if (PDA==null || PDA==false) {
  //if (PDApage==null || PDApage==false) {
    PDApage=false; 
   } else {
    PDApage=true;
  };
//alert(PDApage);
};
UsePDAmenu();


var PDAmenuCounter=0;
var PDAmenuForm='PDApageMenu';
function WriteCustomMenuPDA (CustomMenu,formName,PDApageParameterIn) {
var Today=new Date(), CustomMenuWritten=0;
var ExternalLink=false;
var formname=formName;
var PDApageParameter=PDApageParameterIn;
var pageParameter='';
var labelText='',url;
 if (CustomMenu.length-1<=0) return;
 //WriteHTMLwork();
 if (PDApageParameter==null) {
   //PDApageParameter='?'+'PDApage='+PDApage+';';
   PDApageParameter='?'+'PDA='+PDA+';';
   //PDApageParameter='?'+'PDA='+'true'+';';
 };
 //if (formname==null) formname=PDAmenuForm;
 if (formname==null) formname=PDAmenuForm+PDAmenuCounter++;
 if (formname==null) formname='menu';
 //document.write('<span>');
 document.write('<form name="'+formname+'">');
 //document.write('<select name="MenuPDA" >');
 //document.write('<select name="MenuPDA" onChange="window.location=this.form.MenuPDA[this.form.MenuPDA.selectedIndex].value">');
 document.write('<select name="MenuPDA" onChange="PDAmenuGo(this)">');
 document.write('<option value="" disabled></option>');
 for (i=0; i<CustomMenu.length-1; i++) {
  if (CustomMenu[i].FromDate!=null && CustomMenu[i].FromDate>Today) continue;
  if (CustomMenu[i].ToDate!=null && CustomMenu[i].ToDate<Today) continue;

  if (CustomMenu[i].URL=='') continue;
  if (CustomMenu[i].Label=='') continue;

  url=CustomMenu[i].URL;
  if (url=='') continue;
  labelText=HTML2Text(CustomMenu[i].Label);
  if (labelText=='') continue;

  pageParameter='';
  if (PDApage) {
    pageParameter=PDApageParameter;
  };

  ExternalLink=((CustomMenuExternalLinkTarget!='') && (CustomMenu[i].URL.substring(0,7).toLowerCase()=='http://'));
  if (!ExternalLink) {
    url=url+pageParameter;
  };

  //document.write('<option value='+url+'>'+name+'</option>');
  document.write('<option ');

    if (IsURLThisPage(CustomMenu[i].URL)) {
      document.write(' selected ');
     }
     else {
      //document.write('onMouseOver="this.bgColor=\''+CustomMenuSelectColor+'\'" onMouseOut="this.bgColor=\''+CustomMenuNormalBgColor+'\'" bgColor="'+CustomMenuNormalBgColor+'"'); 
     };

   if (CustomMenu[i].URL!='') {
     document.write(' disabled ');
   };

   //document.write(' value="'+CustomMenu[i].URL+'" ');
   //document.write(' value="'+CustomMenu[i].URL+pageParameter+'" ');
   document.write(' value="'+url+'" ');
   document.write(' >');

   //document.write(CustomMenu[i].Label);
   document.write(labelText);
   document.write('</option>');

 };
 document.write('</select>');
 ///document.write('<input type="button" value="go" onClick="window.location=this.form.MenuPDA[this.form.MenuPDA.selectedIndex].value">');
 //document.write('<input type="button" value="go" onClick="PDAmenuGo(this.form)" >');
 document.write('</form>');
 //document.write('</span>');
};

function PDAmenuGo(list) {
  //alert(f);
  var location=list[list.selectedIndex].value;
  //var ExternalLink=((CustomMenuExternalLinkTarget!='') && (location.substring(0,7).toLowerCase()=='http://'));
  var ExternalLink=((location.substring(0,7).toLowerCase()=='http://'));
  //alert(ExternalLink+'\n'+location);
  if (ExternalLink) {
    window.open(location);
   } else {
    window.location=location;
  };
};

// --- remove images and HTML from menu ---

var MenuHTMLworkId='MenuHTMLworkSpan';
//document.write('[<span style="{visibility: hidden}" id="'+MenuHTMLworkId+'">A</span>]');

function WriteHTMLwork() {
   if (document.all[MenuHTMLworkId]==undefined) {
     //document.write('[<span style="{visibility: hidden}" id="'+MenuHTMLworkId+'">A</span>]');
     document.write(' <span style="{visibility: hidden}" id="'+MenuHTMLworkId+'"></span>');
   };
};

function HTML2Text0(html) {
  var text='';
  document.all[MenuHTMLworkId].innerHTML=html;
  text=document.all[MenuHTMLworkId].innerText;
  //alert('['+html+']\n['+text+']');
  document.all[MenuHTMLworkId].innerHTML='';
  return text;
};

function HTML2Text1(html) {
  var text=html;
  var middleText='';
  var begin=html.indexOf('<');
  var end=html.lastIndexOf('>');
  if (begin>=0 && end>=0) {
    //text=html.substring(0,begin-1)+' '+html.substr(end+1)
    middleText=
    text=html.substring(0,begin-1)+HTML2Text(middleText+html)+text.substr(end+1)
  };
  //document.all[MenuHTMLworkId].innerHTML=html;
  //text=document.all[MenuHTMLworkId].innerText;
  //document.all[MenuHTMLworkId].innerHTML='';
  //alert('['+html+']\n['+text+']');
  return text;
};

function HTML2Text(html) {
  var text=html;
  var begin=text.indexOf('<');
  var end=text.indexOf('>');
  while (begin>=0 && end>=0 && begin<end) {
    text=text.substring(0,begin)+text.substr(end+1);
    //alert('['+html+']\n['+text+']');
    begin=text.indexOf('<');
    end=text.indexOf('>');
  };
  //alert('['+html+']\n['+text+']');
  return text;
};


