/*=================================LEGENDA FUNCTION ===================================*/
function findLegendaCustom(toWrite) {

   var toRet = "";
   var i;
   toRet = buildLegendaCustom(toWrite);
   return toRet;
}

function buildLegendaCustom(toWrite) {
   //alert("BUILD LEGENDA CALLED in custom.js");
   var strlen;
   var str;
   var i;
   var digit;
   var numSegue = -1;
   var val = "";
   var val1 = "";
   var newWrite = "" ;
   var firstT = true;
   var dots = "";
   var aCapo = false;
   var firstV = false;
   var firstT = false;
   var prova =false;
   var tmpVal;
   var lettVal;
   var isComma;

toWrite = removeExtraCharLegenda(toWrite,null);   

   str = toWrite;
   found = 1;

   if (str.indexOf("Image") >= 0 ) found = 0;
   strlen = str.length;

   if (found>0) {
   for (i=0;i<strlen;i++) {
      digit = str.charCodeAt(i);
      if ( isAlpha(str.charAt(i)) && str.charAt(i) != ' ') {
         val += str.charAt(i);
      } else {
         val1 = clearBlanks(val);
         if (val1 != "" && parseInt(val1.charAt(0),10) ) {
            lettVal = "";
            tmpVal = val1;
            if ( isAlphaUpper(val.substr(val.length-1,1))) {
               // Put letter before the number
               lettVal = val.substr(val.length-1,1);
               tmpVal = lettVal + val1.substring(0,val1.length-1);
            }

            if ( str.charAt(i) == "-" ) firstT = true;
            if ( str.charAt(i+1) == "-" ) firstT = true;

            isComma = false;
            if ( str.charAt(i) == "," ) {
               firstT = true;
               isComma = true;
            }

            if ( str.charAt(i+1) == "," ) {
               firstT = true;
               isComma = true;
            }
            var TnewWrite = "";

            if (firstT) {
               newWrite += "<A style=\"text-decoration:none; color=blue;\" href='javascript:parent.contents.gotoPage(\"A\",\"";
               if (!isComma) newWrite += "$$##";

               newWrite += tmpVal + "\");' align='right'><font face=\"Verdana\" ><B>"+ val1 +"</B></font></A>";
               firstT = false;
               firstNum = true;
            } else {
               if (!isComma) newWrite = newWrite.replace("$$##" + lettVal,lettVal);
               if (!isComma) newWrite = newWrite.replace("$$##",lettVal);
               newWrite += " <A style=\"text-decoration:none; color=blue;\" href='javascript:parent.contents.gotoPage(\"A\",\"";
               newWrite += tmpVal + "\");' align='right'><font face=\"Verdana\" ><B>"+ val1 +"</B></font></A><BR _legenda1>";
            }

            newWrite += str.charAt(i);

         } else {
            if ( val != "" ) newWrite += val;
            newWrite += str.charAt(i);
            val="";
         }
         val = "";
      }

   }

   } else {
      newWrite = toWrite;
   }

   // if last is page number
   if (val != "" && parseInt(val.charAt(0),10)) {
      tmpVal = val.substr(val.length-1,1);
      tmpVal = tmpVal + val.substring(0,val.length-1);

      newWrite += dots;
      newWrite += " <A style=\"text-decoration:none; color=blue;\" href='javascript:parent.contents.gotoPage(\"A\",\"";
      newWrite += tmpVal + "\");' align='right'><font face=\"Verdana\"><B>"+ val +"</B></font></A><BR>";
   } else {
      if ( val != "" ) newWrite += val;
   }

   return newWrite;
}
/*=================================END LEGENDA FUNCTION ===================================*/



/*=================================LINKS FUNCTION ===================================*/
function findLinks(toWrite,page) {
//   alert("findLinks is called");
   var toRet = "";
   toRet += buildLinks(toWrite,page);
   return toRet;
}


var curBox = 0;
var boxFound = -3;
function buildLinks(toWrite,page) {
//	alert(toWrite);
   var toRep = new Array();
   var numChar = 3;
   var newWrite = "";
   var endLink = false;
   var tmpVal;
   var val = "";
   var val1 ="";
   var firstT = true;
   var findDot = 0;
   var found = -1;

	curBox ++;
  	found = -2;

   found = toWrite.indexOf("page");
   numChar = 4;
   
   
   if(found > 0){
   	boxFound = curBox;
   	found = -1;
   }
   
   if(boxFound + 1 == curBox){
   	found = 0;
   	numChar = 0;
   }

   if (found>=0) {
      var i, digit;
      var numSegue = -1;
      val = "";
      newWrite += toWrite.substring(0,found+numChar);
      var str = toWrite.substr(found+numChar);
      for (i=0;i<str.length;i++) {
         if ( isAlphaL(str.charAt(i)) ) {
            val += str.charAt(i);
         } else {
            val1 = clearBlanks(val);
            if (val1 != "" && parseInt(val1.charAt(0),10) && isAlphaUpper(val1.substr(val1.length-1,1))) {
              // Put letter before the number
              tmpVal = val1.substr(val1.length-1,1);
              tmpVal = tmpVal + val1.substring(0,val1.length-1);
               
              if (firstT) {
                newWrite += "<A style=\"text-decoration:none; color=blue;\" href='javascript:parent.contents.gotoPage(\"A\",";
                newWrite += "\"" + tmpVal + "\");' align='right'><font face=\"Verdana\" ><B>"+ toWrite.substring(found+numChar,found+numChar+i) +"</B></font></A>";
                firstT = false;
              } else {
                newWrite += "<A style=\"text-decoration:none; color=blue;\" href='javascript:parent.contents.gotoPage(\"A\",";
                newWrite += tmpVal + ");' align='right'><font face=\"Verdana\" ><B>" + tmpVal +"</B></font></A>";
              }
              newWrite += str.charAt(i);
              if ( findDot && str.charAt(i)=="." ) newWrite +="<BR _dot>";
            } else if (!firstT) {
              newWrite += val;
              newWrite += str.charAt(i);
              if ( findDot && str.charAt(i)=="." ) newWrite +="<BR _dot>";
            } else {
	           newWrite += val;
	           newWrite += str.charAt(i);
              if ( findDot && str.charAt(i)=="." ) newWrite +="<BR _dot>";
	         }
            val = "";
         }

      }

      val1 = clearBlanks(val);
      if (val1 != "" && parseInt(val1.charAt(0),10) && isAlphaUpper(val1.substr(val1.length-1,1)) ) {
            //alert("2 " + val1);
            tmpVal = val.substr(val.length-1,1);
            tmpVal = tmpVal + val1.substring(0,val1.length-1);
            //alert(val1.substring(0,val1.length-1));
            newWrite += "<A style=\"text-decoration:none; color=blue;\" href='javascript:parent.contents.gotoPage(\"A\",";
            //alert("toWrite " + toWrite.substring(found+numChar,found+numChar+i));
            if(toWrite.substring(found+numChar,found+numChar+i)){
               
            }
            newWrite += "\"" + tmpVal + "\");' align='right'><font face=\"Verdana\" ><B>"+ toWrite.substring(found+numChar,found+numChar+i) +"</B></font></A>";
      } else {
         newWrite += val;
      }

      val = "";
   } else {
      newWrite = toWrite;
   }

   // if last is page number
   if (val != "" && parseInt(val.charAt(0),10)) {
      tmpVal = val.substr(val.length-1,1);
      tmpVal = tmpVal + val.substring(0,val.length-1);
      newWrite += " <A style=\"text-decoration:none; color=blue;\" href='javascript:parent.contents.gotoPage(\"A\",";
      newWrite += tmpVal + ");' align='right'><font face=\"Verdana\"><B>"+ tmpVal +"</B></font></A>";
   }
   return newWrite;
}
function clearBlanks(str) {
   var db, sss;
   var i,j,k;
   var out = "";

   if (str != "") {
      sss = str.toString();
      db = sss.split(" ");

      for (i=0; (i<db.length) && (db[i] == ""); i++) { }
      for (j=db.length-1; (j>=0) && (db[j] == ""); j--) { }

      //alert(db.length + " - " + i + " - " + j);
      for (k=i; k<=j; k++) {
         //alert("'" + db[k] + "'");
         if (db[k] != "") {
            if (out != "") out = out + " ";
            out = out + db[k];
         }
      }
   } else {
      out = str;
   }

   return(out);
}


/********************************************* SHOW TITLE *******************************/
function showTitle(which) {
   if (window.overlib) {
      showTitleOverLib(which);
   } else {   
      if (window.showTitleEx) showTitleEx(which);
   }   
}    

function showTitleOverLib(which) {
   var toWrite = "";
   var mainTitle = "";
   var subTitle = "";
   var exit = false;
   var oldFamily = "";
   var oldSize = 0;
   var sCr = String.fromCharCode(0X1e);
   var textV = "";
   var startedTitle = false;
   var countTitle = 0;
   var italic, size;
   var numTitle = 0;

   maxSize = 14;

   if (f.length > 0 && which >=0 && which < a.length) {
	   /* Calculate the maxsize of the article */
      for (var il=0;il<a[which].e.length;il++) {
         link = a[which].e[il].link;
         if (link >= 0) {
            par = p[link];
            for (var is=0;par && is< par.S.length;is++) {
	            family = f[par.S[is].fontId].family;
               textT = par.S[is].textV;
               if ( textT.indexOf("Image ") < 0 && textT.indexOf("Image_") < 0) {
                  if (rule_Title(family,f[par.S[is].fontId].size,par.S[is].textV.length,il)) {
                     titSize = f[par.S[is].fontId].size;
                     if (titSize > maxSize ) maxSize = titSize;
                  }
               }

            }
         }
      }
   }
   
   for (var il=0;il<a[which].e.length;il++) {
      var link = a[which].e[il].link;
      if (link >= 0) {
         var par = p[link];
         for (var is=0;par && is< par.S.length;is++) {
            family = f[par.S[is].fontId].family;
            lSize = f[par.S[is].fontId].size;
            if (rule_Title(family,f[par.S[is].fontId].size,par.S[is].textV.length,il) && par.S[is].textV.indexOf("Image_") < 0 ) numTitle++;
         }
      }
   }

   if (f.length > 0 && which >=0 && which < a.length) {
      for (var il=0;il<a[which].e.length && !exit;il++) {
         var link = a[which].e[il].link;
         if (link >= 0) {
            var par = p[link];
            for (var is=0;par && is< par.S.length && !exit;is++) {
               family = f[par.S[is].fontId].family;
               lSize = f[par.S[is].fontId].size;
               toWrite = "";
               if (countTitle < 2 && rule_Title(family,f[par.S[is].fontId].size,par.S[is].textV.length,il) && par.S[is].textV.indexOf("Image_") < 0 ) {
               	if (family.indexOf("Euro-") == 0) {
							if (isIeb) {
								textV = replaceAll(par.S[is].textV,"B","&#8364;");
   						} else {
                        textV = replaceAll(par.S[is].textV,"B","<b>euro&nbsp;</b>");
   						}
						} else {
                     textV = par.S[is].textV;
                  }
                  if (family != oldFamily && startedTitle) toWrite += "<br>";

                  size=10;
                  if (f[par.S[is].fontId].size < maxSize-1) size=9;

                  /* if text is too long truncate to n chars */
                  /*
                  if (textV.length > 50 ) {
                     textV = textV.substr(0,50);
                     textV += "..."
                  }
                  */

                  if (family.toUpperCase().indexOf("ITALIC") >= 0) toWrite += "<i>";
                  //color = "#000000";
                  //if (numTitle > 1 && countTitle == 0) color = "#666666";
                  //toWrite += "<font style='font-family: Verdana; font-size:11px; color: " + color + ";'><b>"+removeExtraChar(textV)+"</b>&nbsp;</font>";
                  //toWrite += "<font style='font-family: Verdana; font-size:" + size + "px'><b>"+removeExtraChar(textV)+"</b>&nbsp;</font>";
                  toWrite += "<b>"+removeExtraChar(textV)+"</b>&nbsp;";
                  
                  if (family.toUpperCase().indexOf("ITALIC") >= 0) toWrite += "</i>";
                  countTitle++;
                  if (size==10) { 
                     mainTitle += toWrite;
                  } else {
                     subTitle += toWrite;
                  }      
                }
               start = true;
               oldFamily = family;
               oldSize = f[par.S[is].fontId].size;
            }
         }
      }
   }
   
   if (mainTitle !="" || subTitle != "") {
      //alert(subTitle + "\n\n\n" + mainTitle);
	  subTitle = subTitle.replace("\n"," ");
	  subTitle = subTitle.replace("\n"," ");
	  subTitle = subTitle.replace("\n"," ");
	  mainTitle = mainTitle.replace("\n"," ");
	  mainTitle = mainTitle.replace("\n"," ");
	  mainTitle = mainTitle.replace("\n"," ");
      if (window.overlib) overlib(subTitle, CAPTION, mainTitle, CAPCOLOR , '#000000', FGCOLOR, '#ffffff', BGCOLOR, '#cccccc', BORDER, 5, CAPTIONFONT, 'Verdana', CAPTIONSIZE, '11px', TEXTFONT, 'Verdana', TEXTSIZE, '9px', HAUTO, VAUTO)   
   }
}



