/*
 * @author      Jens Weynans
 * @author      Stan Slaughter (idea for brokenImageCheck)
 * @author      kostenlose-javascripts.de (original version of rom2dec)
 * @copyright   2010-08-29
 * @version     6.40
*/

// mon2htm.js: some JavaScript functions for a better usage of http:money.weynans.net
//    use:  <script type="text/javascript" src="mon2htm.js"></script>


/* the function wiki displays in a small window wikipedia information using the special
   search feature, with support of choosing other languages beside EN */

function wiki(infotext) {
  var tmp = window.location.protocol ;              // online or offline ?
  if (tmp == "file:" ) return false;
  var such = infotext.replace(/ /g, "_") + ":en";    // replace spaces with underscores and set default lang
  tmp = such.split( ":");
  var lang = tmp[1]; such = tmp[0];
  var host = "http://" + lang + ".wikipedia.org/wiki/Special:Search/" ;
  var fenster=window.open(host+such,"","location=no,status=no,width=800,height=600,scrollbars=yes,resizable=yes");
  return false;
}

/* This function rom2dec converts roman number system values correctly to the decimal system. 
   The standard Roman letters are:  i,j = 1, u,v = 5, x = 10, l = 50, c = 100, d = 500, m = 1000 

   the medieval centuries "extended" values also allowed:                                   
   o = 11, f = 40, s = 70, r = 80, n = 90, y = 150, k =151, t = 160, h = 200, 
   e = 250, b = 300, g,p = 400, q,a = 500, w = 800, z = 2000 
                                                                     
   If a letter with a lower value stays in front of a higher one, the value of the lower one will 
   be substracted from the other.  ( original version presented by kostenlose-javascripts.de )    */
 
function rom2dec(rom) {
  var dec = 0;  // Starting value of the decimal number
  if (rom.replace(/[a-z]/g, "")!="") return dec;
  var len = rom.length;  // Declarates string length
  var let = 'ijvuxoflsrncykthebgpdqawmz';  // All the signs used in Roman codes
  var val = new Array(1,1,5,5,10,11,40,50,70,80,90,100,150,151,160,200,250,300,400,400,500,500,500,800,1000,2000);
  for (var i = 0; i < len; i++)  // Substracts letters from the Roman code
  {
    var l = rom.substr(i, 1);  // "l" = letter on "i"-position
    var posl = let.indexOf(l);  // Position of "l" in "letters"
    if (i != (len - 1))  // Condition to avoid wrong indexes
    {
      var lA = rom.substr((i+1), 1);  // "lA" = letter after "l"
      var poslA = let.indexOf(lA);  // Position of "lA" in "letters"
    }
    else  { poslA = 0; }  // Alternative value for the last run of the loop
    if ((posl >= poslA) || (val[posl]>=val[poslA])) { dec = dec + val[posl] }  // Adds value to variable "dec"
    else { dec = dec - val[posl] + val[poslA]; i++; } 
  } return dec;  // return the decimal value
}


/* the function will be used to calculate specimens numbers from non numerical notations */

function specs(cats) {
  var help; var speci = 0;
  cats = cats.replace(/\W/g, "");
  if (!isNaN(cats)) speci=cats ; else speci=rom2dec(cats);
  if ((speci==0)) for (var i = 0; i < cats.length; i++) {
    help=cats.charCodeAt(i);
    switch (true) { case ((help > 96) && ( help < 123)): speci=speci*27+help-96; break;
                    case ((help > 64) && ( help < 91)): speci=speci*27+help-64; break;  
                    case ((help > 47) && ( help < 58)): speci=speci*27+help-48; break; }
    if (speci > 369) break ;
  } speci=( speci % 10000);
  return speci;
}

/* the function pics will be used for displaying my images files.

                                                   |  Y  (A)   X  (B)   Z  |
   when called the function will load the image(s) |  |   |    |   |    |  | in a new window.
                                                   |  V  (B)-  U  (A)-  W  |

   the main structure <dir> depends on the filename starting chars. for <dir>="va" and <dir>="de"
   the structure is: de01,de23,de44,de55,de67,de89 and respectively va01,va23,va45,va67,va89.
   while keeping the directory structure, you can also use this function for browsing offline.
   
   catalog number defined as:  <cc>#<number>[ [ :/- ] spec ][,[catXU],[catYV],[catZW]]|[,copyright] 
   valid spec ranges, which can be calculated with the function specs():
     type a) 0-9          -> (A)+(B) = (S+T,A+B,C+D,E+F,G+H,I+J,K+L,M+N,O+P,Q+R)
     type b) 10-9999      -> (A)+(B) = "-"(1S+1T,1A+1B,1C+1D, .. 999Q+999R)
     type c) A-MRZ        -> [(0)=0,(A|1)=1..(I|9)=9..(M)=13..(R)=18..(Z)=26] to [(M)*729+(R)*27+(Z)] = 9989 -> a) b)
     type d) i-zzzmmmcmic  -> roman lower numerals notation = 1 .. 9999  -> a) b) 

   if no ":.." is given, the default will be :1.  for ":0" the letters "S" and "T" will be used (special).
   if there any files with the created names and an "-" afterwards, there will be also loaded.
   the character "." will be replaced with "_". "U" to "Z" are mostly static name extensions, but with
   type b) the name structure also follows the scheme: "-"(1X+1U,1Y+1V,1Z+1W) .. "-"(999X+999U,999Y+999V,999Z+999W)
   type c) the "pseudo" upper number will be converted to a real number and then type a) or b) will be used.
   type d) the "roman" lower numbers will be converted to a real number and then type a) or b) will be used.
   catXU, catYV and catZW can be used for a cross reference number, but restricted to X+U,Y+V and Z+W names.
    
   example "FI#35432Y" will load the images FI#35432YA and FI#35432YB from <dir>=fi
   example "VA#4835/6" will load the images VA#4835K and VA#4835L from <dir>=va45
   example "DE#C.SG346" will load the images DE#C_SG346A and DE#C_SG346B from <dir>=de67
   example "IT#4711.Z:425" will load the images IT#4711_Z-42I and IT#4711_Z-42J from <dir>=it
   example "SM#CD354W-SC" [SC->516] will load the images SM#CD354W-51K" and SM#CD354W-51L" from <dir>=sm
   example "ES#445Z/xxvi" [xxvi->26] will load the images ES#445Z-2K and ES#445Z-2L from <dir>=es    */


function pics(catalog) {

  if(catalog=="") return false; 

  var prot = window.location.protocol ;             // online or offline ?
  var numi = 0; var spec = 0; var difh = 100; var difw = 25; var siz = 0; 
  var ok, p86, tmp, idid, head, home, host, copy, hlp, numb , dirc, preb, dirn, note, infos;

  if (prot == "http:" ) {                           // OK -> online, different names possible
    host = window.location.hostname ;               // get current hostname
    home = window.location.href;                    // get current calling pathname ...
    home = home.replace(/\?.*/,"");
    home = home.replace(/#.*/,"");
     tmp = host.replace(/.*weynans/i,"");           // shorten hostname for .net, .org or other
     tmp = tmp.replace(/.*admin.*\./i,".");         // dito ...

    switch (tmp) {
    case ".homepage.t-online.de":
      prot = "https:" ;
      host = "dav-sth-se.diino.com/dav/deadnut";
      break;
    case ".net":
      host = "lbfw1-dav.sth-se.diino.net/dav/deadnut";
      break;
    case ".org":
      host = "lbfw2-dav.sth-se.diino.net/dav/deadnut";
      break;
    default:
      host = "127.0.0.1";
      break;
    }
  }
  if (prot == "file:" ) {                          // if local, then ...
    home = window.location.pathname ;              // get current calling pathname ...
     tmp = home.replace(/\.hTm.*/i,"");            // drop file extension ...
     tmp = tmp.replace(/.*\//g,"/");               // drop leading path ...   
    host = home.replace(/:.*/,":") + tmp;          // get drive letter and add item path
    home = prot + "//" + home;
  }
  host = prot + "//" + host + "/";

  hlp=catalog.split(",");
  catalog = hlp[0];
  copy=( !hlp[2] || (hlp[2].search(/#/)==2) ) ? "Jens Weynans" : hlp[2];

  tmp = catalog.replace(/[\/-]/g, ":");
  tmp = tmp.replace(/::/g, ":"); 
  tmp = tmp.replace(/[\)\(\!]/g, ""); 
  tmp = tmp.replace(/\./g, "_"); 
  idid = tmp.replace(/#/g,"-"); 
  tmp = tmp + ":1";
  var cat = tmp.split( ":" );
  var speci = cat[1];
  spec = specs(speci);
  tmp = "-"+parseInt(spec/10);
  if (tmp == "-0" ) tmp = "";
  cat[0]=cat[0]+"@"+tmp;

  for (var i = 1; i < 4 ; i++) { cat[i]=( !hlp[i] || (hlp[i].search(/#/)!=2 )) ? cat[0] : hlp[i].replace(/\./g, "_")+"@"; }

  for (var j = 0; j < 4 ; j++) {
     hlp = cat[j].split( "#" );
    numb = hlp[1].split( "@" );
    dirc = hlp[0].toLowerCase();
    dirc = dirc.substr(0,2);
    preb = hlp[0] + "%23";
    dirn = "";
    for (var i = 1; i < numb[0].length; i++) {
      tmp = numb[0].substr( i, 1); numi = Number(tmp);
      if ( !isNaN(tmp) ) {
       dirn = (2*parseInt(numi/2))+""+(numi+(1-(numi%2)));
       switch (true) {
         case ((dirc == "de")): if ((dirn=="45")) dirn=numi+""+numi; break;
         case ((dirc == "va")): break;
         default: dirn = "";
       }} 
      if (( tmp == "_"  ) && (i > 2)) break ; 
    }
    cat[j] = dirc + dirn +"/" + preb + numb[0] + numb[1];
  }

  hlp[1]=cat[2]+"V" ;   hlp[0]=cat[2]+"Y" ;
  hlp[2]="SACEGIKMOQ" ; hlp[2]=cat[0]+hlp[2].substr((spec % 10),1);
  hlp[5]=cat[1]+"U" ;   hlp[4]=cat[1]+"X" ;
  hlp[6]="TBDFHJLNPR" ; hlp[6]=cat[0]+hlp[6].substr((spec % 10),1);
  hlp[9]=cat[3]+"W" ;   hlp[8]=cat[3]+"Z" ;
  hlp[3]=hlp[6]+"-" ;   hlp[7]=hlp[2]+"-" ;

  ref=document.getElementById(idid).childNodes[0].firstChild.name;
  tmp=ref.split(":");
  head=document.getElementById(tmp[0]).childNodes[0].innerHTML;
  tmp=tmp[0]+":0";
  head=head.replace(/\>/g,">\n");
  head=head.replace(/\<.+\>/g,"");
  head=head.replace(/[ \n]+/g," ");
  var desc=document.getElementById(tmp);
  var desh=( !desc ) ? 0  : desc.clientHeight;
  var desc=( !desc ) ? "" : desc.innerHTML;
  desc=desc.replace(/\>/g,">\n");
  desc=desc.replace(/\<.+\>/g,"");
  desc=desc.replace(/[ \n]+/g," ");
  desc=desc.replace(/ -/g,"&-&");
  desc=desc.replace(/- /g,"");
  desc=desc.replace(/&-&/g," -");

  var j=document.getElementById(idid).childNodes.length; 
  with (document.getElementById(idid).firstChild.lastChild) { tmp=((nodeType == 3)) ? nodeValue : "" }
  for (var i = 1; i < j; i++) {
    if (document.getElementById(idid).childNodes[i].firstChild.length>=1)
     with (document.getElementById(idid).childNodes[i])
     tmp=((j==i+1)) ? tmp+";"+firstChild.nodeValue : tmp+";"+innerHTML ; }

  infos=tmp.replace(/&nbsp;/g,"");
  infos=infos.replace(/[ ;,]*$/g,"");
  infos=infos.replace(/[;,]/g,"&nbsp;&middot;&nbsp;");

  tmp=tmp.replace(/;/g,";\n");
  tmp=tmp.replace(/\>/g,">\n");
  tmp=tmp.replace(/\&.+\;/g,"@");
  tmp=tmp.replace(/\<.+\>/g,"");
  tmp=tmp.replace(/[)(;,/.\n]/g,"");
  siz=difw+catalog.length*5+tmp.length*10;

  if (document.getElementById(idid).lastChild.lastChild)
    with (document.getElementById(idid).lastChild.lastChild) {
    note=((nodeType == 1)) ? "note: " + innerHTML+"&nbsp;&middot;&nbsp;" : "&nbsp;" ; }

  tmp=note+copy+" print close pictures (c)copyright";
  tmp=tmp.replace(/;/g,";\n");
  tmp=tmp.replace(/\&.+\;/g,"@");
  tmp=tmp.replace(/[ \n]+/g," ");
  j=3*difw+parseInt(tmp.length*5.5);
  if ((j>siz)) siz=j;

  p86=(note.search(String.fromCharCode(167, 56, 54))!=-1); 
  difh=((p86)) ? difh+50 : difh+desh;
  speci=((speci == spec)) ? "specimen number" : "calculated specimen number for "+speci ;

  var fenster=window.open("","","location=no,status=no,width="+siz+",height=150,menubar=no,toolbar=no,scrollbars=yes,resizable=yes");
  fenster.document.open();
  fenster.document.writeln("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\"http://www.w3.org/TR/html4/loose.dtd\">");
  fenster.document.writeln("<html><head><title> - PICTURE(S) OF "+ catalog + " </title>");
  fenster.document.writeln("<script type=\"text/javascript\"> // display only valid images and resize window");

  fenster.document.writeln("function BrokenImageCheck() {");
  fenster.document.writeln("  var ok, maxh, lastw; var lastok=false; var htop=0; var hbot=0; var maxw=0; var siz="+siz+"; var difh="+difh+"; var difw="+difw+";");
  fenster.document.writeln("var siz=difw+document.getElementById(\"info\").clientWidth;");
  fenster.document.write("difh=difh+document.getElementById(\"over\").clientHeight");
  fenster.document.write("+document.getElementById(\"info\").clientHeight");
  fenster.document.writeln("+document.getElementById(\"stat\").clientHeight;");
  fenster.document.writeln("  if (window.innerheight) with (window) ");
  fenster.document.writeln("    {difw=difw+OuterWidth-InnerWidth; difh=difh+OuterHeight-InnerHeight;}");
  fenster.document.writeln("  else with (screen)");
  fenster.document.writeln("    {difw=difw+width-availWidth; difh=difh+height-availHeight;}");
  fenster.document.writeln("  for (var i = 0; i < document.images.length; i++) with (document.images[i]) {");
  fenster.document.writeln("    ok=complete; if (ok) ok=!(typeof naturalWidth != \"undefined\" && naturalWidth==0);");
  fenster.document.writeln("    if (lastok) lastok=((i % 2)); ");
  fenster.document.writeln("    if (ok) if (lastok) {if (height > hbot) hbot=height; if (width > lastw) maxw=maxw+width-lastw;}"); 
  fenster.document.writeln("    else {maxw=maxw+width; if (height > htop) htop=height; lastw=width}");
  fenster.document.writeln("    else { src = \"1x1.gif\"; style.display = \"none\"; } lastok=ok;} ");
  fenster.document.writeln("  if (maxw>0) with (screen) {maxw=maxw+difw; maxh=htop+hbot+difh;");
  fenster.document.writeln("    difw=availWidth; difh=availHeight; if (siz>maxw) maxw=siz;");
  fenster.document.writeln("    if (maxw>difw) maxw=difw; if (maxh>difh) maxh=difh; resizeTo(maxw,maxh);} }");
  fenster.document.writeln("window.onload=BrokenImageCheck; // key idea stan_slaugther@yahoo.com");

  fenster.document.writeln("</script>\n<style type=\"text/css\">"); 
  fenster.document.writeln("  * {font-family: Verdana, Helvetica, sans-serif; white-space: nowrap; color: #000000;}"); 
  fenster.document.writeln("  body, i {font-size: 9pt;}");
  fenster.document.writeln("  table, td, tr, tbody {border:solid 0px #000000; border-spacing: 0px; padding: 0px; margin: 0px;}"); 
  fenster.document.writeln("  a:link, a:visited, a:active { text-decoration: none;}"); 
  fenster.document.writeln("  a:hover { color: #ffffff; background-color:  #666666; text-decoration:none;}"); 
  fenster.document.writeln("</style>\n</head><body>");

  fenster.document.writeln("<table style=text-align:center border=0 cellspacing=0 cellpadding=0 align=center>");
  fenster.document.writeln("<!-- debug info: the internal reference number is "+ref+" -->");
  fenster.document.writeln("<!-- debug info: the "+speci+" is "+spec+" -->");

  fenster.document.writeln("<tr><td colspan=\"5\" align=\"right\" bgcolor=\"#444444\"><img src=\"1x1.gif\" alt=\"\" border=\"0\"></td></tr>");
  fenster.document.write("<tr id=\"over\"><td colspan=\"5\" bgcolor=\"#bbbbbb\"><font style=\"font-size: 10pt; font-weight: bolder; color: #ffffff\">");
  fenster.document.writeln(head+"</font></td></tr>");
  fenster.document.writeln("<tr><td colspan=\"5\" align=\"right\" bgcolor=\"#444444\"><img src=\"1x1.gif\" alt=\"\" border=\"0\"></td></tr>");
  fenster.document.writeln("<tr><td colspan=\"5\" align=\"right\" bgcolor=\"#ffffff\"><img src=\"1x1.gif\" height=\"3\" alt=\"\" border=\"0\"></td></tr>");

  fenster.document.write("<tr id=\"info\"><td colspan=\"5\" bgcolor=\"#fafafa\"><font style=\"font-size: 10pt; font-weight: bolder; color: #ffffff\">");
  fenster.document.write("<a href=\""+home+"#"+idid+"@loadpics\">"+catalog+"</a></font><font size=-1>");
  fenster.document.writeln(infos+"</font></td></tr>" );
  fenster.document.writeln("<tr><td colspan=\"5\" align=\"right\" bgcolor=\"#ffffff\"><img src=\"1x1.gif\" height=\"3\" alt=\"\" border=\"0\"></td></tr>");
  fenster.document.writeln("<tr align=\"center\" valign=\"middle\">");

  for (var i = 0; i <= 9; i++)
    { tmp=((i % 2))? "" : "<td>"; fenster.document.write( tmp );
      fenster.document.write("<img src=\"" + host + hlp[i] + ".JPG\" alt=\"\">");
      tmp=((i % 2))? "</td>\n" : "<br>"; fenster.document.write( tmp ); }

  fenster.document.writeln("</tr>");
  fenster.document.writeln("<tr><td colspan=\"5\" align=\"right\" bgcolor=\"#ffffff\"><img src=\"1x1.gif\" height=\"3\" alt=\"\" border=\"0\"></td></tr>");
  fenster.document.writeln("<tr><td colspan=\"5\" align=\"right\" bgcolor=\"#444444\"><img src=\"1x1.gif\" alt=\"\" border=\"0\"></td></tr>");
  fenster.document.write("<tr id=\"stat\"><td colspan=\"5\" bgcolor=\"#bbbbbb\"><i><font style=\"color: #ffffff\">");
  fenster.document.write("<a href=\"javascript:window.print();\">print</a>&nbsp;&nbsp;"+note+"picture(s) &copy;copyright ");
  fenster.document.writeln(copy+"&nbsp;&nbsp;<a href=\"javascript:window.close();\">close</a></font></i></td></tr>");
  fenster.document.writeln("<tr><td colspan=\"5\" align=\"right\" bgcolor=\"#444444\"><img src=\"1x1.gif\" alt=\"\" border=\"0\"></td></tr>");
  fenster.document.writeln("<tr><td colspan=\"5\" align=\"right\" bgcolor=\"#ffffff\"><img src=\"1x1.gif\" height=\"3\" alt=\"\" border=\"0\"></td></tr></table>");

  if ((p86)) {
    fenster.document.write("<font style=\"font-size: 8pt; color: #ffffff; background-color: #400000\">DE</font><i>");
    fenster.document.write("<font style=\"font-size: 8pt; white-space: normal; color: #400000; background-color: #ffffff\">&nbsp;");
    fenster.document.write(" Die Abbildung dieses zeitgeschichtlichen Gegenstandes aus der Zeit von 1933 bis ");
    fenster.document.write("1945 erfolgt nur zu Zwecken der wissenschaftlichen und kunsthistorischen Forschung, ");
    fenster.document.write("sowie der staatsb&uuml;rgerlichen Aufkl&auml;rung bzw. der Berichterstattung &uuml;ber die ");
    fenster.document.write("Vorg&auml;nge des Zeitgeschehens gem. Paragraph 86 und 86a StGB. Es ist nicht gestattet ");
    fenster.document.write("diese Abbildung in irgendweiner Weise propagandistisch, insbesondere im Sinne des ");
    fenster.document.writeln("Paragraphen 86 und 86a StGB zu benutzen.</font></i>"); 
  }
  else if ((desh>0)) {
    fenster.document.write("<font style=\"font-size: 8pt; color: #ffffff; background-color: #400000\">INFO</font><i>");
    fenster.document.write("<font style=\"font-size: 8pt; white-space: normal; color: #400000; background-color: #ffffff\">");
    fenster.document.writeln(desc+"</font></i>");
  } 
  fenster.document.writeln("</body></html>");
  fenster.document.close();
  return false;
} 

/* the function autorun will be used for pre-processing purposes while loading the main page.
   you can start calling the page with the hashes (of course) like ...#VA-0155:1 .
   but if you will give: ....#VA-0155:1@loadpics , the associated picture will loaded directly - 
   of course only when your popup settings will allow it !

   you can also load more than one image by combining the values with an ","  like ..
   ....#VA-0155:1,VA-0155:3,VA-0155:2@loadpics 

   a bonus feature can be used to calculate specimen numbers for non numerical notations e.g.
   ....#i,II,iii,iv,V@specimen 
  
   will show five alert windows like: i=1, II=252, iii=3, iv=4 and V=22

*/


function autorun()
{
 var ok, cok, pok, hlp, idid, liste, tmp, catalog;

  ok=((window.location.hash!=""));
  if ((ok)) {
    hlp=window.location.hash.split("@");
    pok=((hlp[1]) && (hlp[1]=="loadpics"));
    cok=((hlp[1]) && (hlp[1]=="specimen"));
    window.location.hash=hlp[0];
    tmp=window.location.hash.split("#");
    tmp=((!tmp[1])) ? prompt("input value for @"+hlp[1]+":","") : tmp[1]; 
    liste=tmp.split(",");
  } 
  if ((pok) || (cok)) for (var i = 0; i < liste.length; i++) {
    if ((cok)) {
      hlp=specs(liste[i]); 
      alert('the calculated specimen number for '+liste[i]+' is '+hlp+' !');  
    }
    if ((pok)) {
      catalog="";
      hlp=liste[i].split("-"); 
      pok=((hlp.length>1));
    }
    if ((pok)) {
      idid=hlp[0]+"-"+hlp[1];
      window.location.hash=idid;
      pok=(document.getElementById(idid)!=null);
    } 
    if ((pok)) {
      tmp=document.getElementById(idid).childNodes[0].firstChild.getAttribute("onclick");
      tmp=tmp.toString();
      tmp=tmp.replace(/\n/g,"");
      tmp=tmp.replace(/'\).*/,"");
      tmp=tmp.replace(/.*\('/,"");
      catalog=tmp; 
      pics(catalog); 
    }
  } 
  return false;
}

window.onload=autorun;

//$Update: August 29th, 2010$
