﻿a = 'e'
actID = -1
appletReady = -1
globNum = true
oldBackground = 'buttonface'
diagramReady = -1
imD = new Image(); imD.src="diagr.gif"
namesNumbered = 's'
javaVersion = 'unk'
infoRow = null
language = 'E'
foundCookie = false
goBack = false
goBackGMap = false

google_search_e = new Array("Hotels","Restaurants","Shopping", "Sights", "Maps")
google_search_d = new Array("Hotels","Restaurants","Einkaufen", "Attraktionen", "Karten")

google_ad_client = "pub-7251478393613561";
google_alternate_color = "F9F9F9";
google_ad_width = 300;
google_ad_height = 250;
google_ad_format = "300x250_as";
google_ad_type = "text_image";
google_ad_channel = "8220287810";
google_color_border = "8898BF";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_text = "000000";
google_color_url = "0066CC";
google_cse_client = "partner-pub-7251478393613561:54544cvf3xn";
google_cse_client_de = "partner-pub-7251478393613561:stwu7c6wfzh";

function log(txt) {
  try { 
    if (window.console)
      window.console.log(txt);
  } catch (ex) {
  } 
}

var vizMode = "";
var linkarray = document.getElementsByTagName("link");
var linkNotExists = ((linkarray == null) || (linkarray.length == 0));
try { 
  var agent = navigator.userAgent.toLowerCase();
  if ((screen.width <= 640) || (agent.indexOf("android")>=0) || (agent.indexOf("iphone")>=0) || (agent.indexOf("ipod")>=0) || (agent.indexOf("opera mobi")>=0) || (agent.indexOf("blackberry")>=0) || (agent.indexOf("symbianos")>=0)) {
    if ((window.orientation == 0) || (window.orientation == 90) || (window.orientation == -90))
      vizMode = (window.orientation == 0) ? "320" : "480";
    else
      vizMode = (window.innerWidth < 1.3*window.innerHeight) ? "320" : "480";
    document.writeln('<meta name="viewport" content="width='+vizMode+',initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=0" />');
    document.writeln('<link rel="apple-touch-icon" href="http://www.citypopulation.de/apple-touch-icon.png" />');
    //document.writeln('<link rel="apple-touch-icon-precomposed" href="http://www.citypopulation.de/apple-touch-icon.png" />');
    document.writeln('<meta name="apple-mobile-web-app-capable" content="yes" />');
  }
  document.writeln('<meta name="format-detection" content="telephone=no" />');
} catch (ex) {
} 
if (linkNotExists)
  document.writeln('<link rel="stylesheet" type="text/css" href="citypop'+vizMode+'.css" media="all,handheld" />');
else if (vizMode != "") {
  link = linkarray[0];
  link.href = "citypop"+vizMode+".css";
  link.media = "all,handheld";
}

function writeNewsFrame(suff) {
  document.writeln('<iframe id="news" scrolling="no" frameborder="0" src="news/news_'+nn+suff+'.html"></iframe>');
}

function writeWorldMapFrame(suff) {
  document.writeln('<iframe class="worldmap" scrolling="no" frameborder="0" src="citiesim'+vizMode+suff+'.html"></iframe>');
}

function resetNews(suff) {
  iframe = document.getElementById("news");
  if (iframe != null)
    iframe.setAttribute('src','news/news_'+nn+suff+'.html');
}

function getActId () {
  return actID
}

function head_e() {
  document.writeln('<iframe class="menu" scrolling="no" frameborder="0" src="head_e.html"></iframe>')
}

function head_d() {
  document.writeln('<iframe class="menu" scrolling="no" frameborder="0" src="head_d.html"></iframe>')
}

function show_map(file,country,lang,maxscale,minscale,scale,viewwidth,viewheight,provinces) {
  language = lang
  var param = window.location.hash
  if ((param != null) && (param.length >= 2))
    return;
  document.writeln('<applet mayscript="true" codebase="java" code="showmap/ShowCountryMapInFrame.class" archive="showcitymap.jar" name="ShowMap" width="0" height="0" vspace="0" hspace="0">')
  document.writeln('<param name="country" value="'+country+'" />')
  document.writeln('<param name="url" value="Daten/'+file+'.zip" />')
  document.writeln('<param name="maxscale" value="'+maxscale+'" />')
  document.writeln('<param name="minscale" value="'+minscale+'" />')
  document.writeln('<param name="scale" value="'+scale+'" />')
  document.writeln('<param name="viewwidth" value="'+viewwidth+'" />')
  document.writeln('<param name="viewheight" value="'+viewheight+'" />')
  document.writeln('<param name="provinces" value="'+provinces+'" />')
  document.writeln('<param name="language" value="'+lang+'" />')
  document.writeln('<param name="browser" value="'+navigator.appName+'" />')
  document.writeln('<param name="agent" value="'+navigator.userAgent+'" />')
  document.writeln('</applet>')
}

function activate_wms() {
  var today = new Date()
  var expire = new Date()
  expire.setTime(today.getTime() + 1000*60*60*24*92)
  document.cookie = "wms=true; expires=" + expire.toGMTString()
}

function call_javainfo() {
  document.write('<applet id="javainfo" mayscript="true" codebase="java" code="javainfo/JavaInfo.class" archive="javainfo.jar" name="JavaInfo" width="0" height="0" vspace="0" hspace="0"></applet>')
}

function start_viewer(file,country,lang,maxscale,minscale,scale,viewwidth,viewheight,provinces) {
  start_mapviewer(file,country,lang,maxscale,minscale,scale,minscale,viewwidth,viewheight,provinces,'N')
}

function start_mapviewer(file,country,lang,maxscale,minscale,scale,basescale,viewwidth,viewheight,provinces,wms) {
  language = lang
  var param = window.location.hash
  if ((param != null) && (param.length >= 2))
    return;
  var version = "unk";
  try { 
     version = document.JavaInfo.getVersion();
  } catch (ex) {
  }
  //if ((version == "unk") || (version <= '1.4') || navigator.userAgent.toLowerCase().indexOf("safari")>=0) {
  //  show_map(file,country,lang,maxscale,minscale,scale,viewwidth,viewheight,provinces)
  //  return
  //}
  if ((version == "unk") || (version == '1.1'))
    document.writeln('<applet mayscript="true" codebase="java11" code="mapviewer/CountryMapViewer.class" archive="countrymapviewer11.jar" name="ShowMap" width="0" height="0" vspace="0" hspace="0">')
  else if (version <= '1.4')
    document.writeln('<applet mayscript="true" codebase="java14" code="mapviewer/CountryMapViewer.class" archive="countrymapviewer14.jar" name="ShowMap" width="0" height="0" vspace="0" hspace="0">')
  else if ((document.cookie.length > 7) && (document.cookie.indexOf('wms=true')>=0)) {
    document.writeln('<applet mayscript="true" codebase="java5" code="mapviewer/CountryMapViewer.class" archive="scountrymapviewer.jar,shttprequest.jar,scodec.jar" name="ShowMap" width="0" height="0" vspace="0" hspace="0">')
    document.writeln('<param name="wms" value="'+wms+'" />')
  }
  else
    document.writeln('<applet mayscript="true" codebase="java5" code="mapviewer/CountryMapViewer.class" archive="countrymapviewer.jar" name="ShowMap" width="0" height="0" vspace="0" hspace="0">')
  document.writeln('<param name="country" value="'+country+'" />')
  document.writeln('<param name="url" value="Daten5/'+file+'.zip" />')
  document.writeln('<param name="maxscale" value="'+maxscale+'" />')
  document.writeln('<param name="minscale" value="'+minscale+'" />')
  document.writeln('<param name="scale" value="'+scale+'" />')
  document.writeln('<param name="basescale" value="'+basescale+'" />')
  document.writeln('<param name="viewwidth" value="'+viewwidth+'" />')
  document.writeln('<param name="viewheight" value="'+viewheight+'" />')
  document.writeln('<param name="provinces" value="'+provinces+'" />')
  document.writeln('<param name="language" value="'+lang+'" />')
  document.writeln('<param name="browser" value="'+navigator.appName+'" />')
  document.writeln('<param name="agent" value="'+navigator.userAgent+'" />')
  if (file == 'WorldPop')
    document.writeln('<param name="configfile" value="worldpop.zip" />')
  else if (file == 'World')
    document.writeln('<param name="configfile" value="worldcities.zip" />')
  document.writeln('</applet>')
}

function load_diagram(country,lang) {
  var version = "unk";
  try { 
     version = document.JavaInfo.getVersion();
  } catch (ex) {
  }
  if ((version == "unk") || (version <= '1.1'))
    return;
  document.writeln('<applet mayscript="true" codebase="java14" code="diagram/DiagramApplet.class" archive="diagram.jar" name="DiagramApplet" width="0" height="0" vspace="0" hspace="0">')
  document.writeln('<param name="country" value="'+country+'" />')
  document.writeln('<param name="language" value="'+lang+'" />')
  document.writeln('<param name="rsc" value="diagram_messages">')
  document.writeln('<param name="browser" value="'+navigator.appName+'" />')
  document.writeln('<param name="agent" value="'+navigator.userAgent+'" />')
  document.writeln('</applet>')
}

function sym(id) {
  if (id != actID) {
    if (actID >= 0) {
      var cell = document.getElementById ("i"+actID);
      if (cell != null)
        cell.style.backgroundColor = oldBackground
    }
    actID = id
    if (actID >= 0) {
      var cell = document.getElementById ("i"+actID);
      if (cell != null) {
        oldBackground = cell.style.backgroundColor
        cell.style.backgroundColor = '#ffcc33'
      }
      else
        actID = -1
    }
  }
  if ((appletReady == 1) && (diagramReady == 0)) {
    document.ShowMap.activateDiagram()
    diagramReady = 1
  }
  if (appletReady == 1)
    document.ShowMap.movePosByApplet(id)
  showinfo(id)
}

function sg(id) {
  if (appletReady == 1)
    document.ShowMap.movePosByApplet(id)
  showID(id)
}

function map_to_front() {
  if (appletReady == 1)
    document.ShowMap.toFront()
}

function showinfo(id) {
  if (infoRow != null) {
    infoRow.parentNode.removeChild(infoRow)
    infoRow = null
  }
  var row = document.getElementById('i'+id)
  if (row == null)
    return;
  var wiki = row.getAttribute('cp:data-wiki');
  if (wiki == null) {
    wiki = row.getAttribute('abbr');
    if (wiki == '')
      wiki = null;
  }
  var row = row.parentNode
  var rowParent = row.parentNode
  var tabId = rowParent.parentNode.id
  var cells = row.getElementsByTagName('td')
  infoRow = document.getElementById('inforow')
  if (infoRow == null)
    return
  infoRow = infoRow.cloneNode(true)
  var infoCell = infoRow.firstChild
  
  var newP = document.createElement('P');
  var pos = '';
  var text = (language == 'D'? 'Geographische Lage: ' : 'Geographical Location: ')
  if ((appletReady == 1) && (tabId == 'ts')) {
    pos = document.ShowMap.getPositionOfSelectedObject()
    newP.appendChild(document.createTextNode(text + pos))
    //infoCell.appendChild(newP)
  }

  var place = cells[1].firstChild;
  if (place.firstChild != null)
    place = place.firstChild;
  place = place.nodeValue
  if (place.indexOf(' [') > 0)
    place = place.substr(0,place.indexOf(' ['));
  if (place.indexOf(' (') > 0)
    place = place.substr(0,place.indexOf(' ('));
  place = place.replace(/ - /g,'-');
  place = trim(place)
  if (adminname != null)
    adminname = adminname.replace(/ - /g,'-');
  var gstxt = place;
  if ((tabId == 'ts') && (infomode > 0) && (adminname != null))
    gstxt += ' '+(adminname);
  if ((infomode == 0) && (wiki != null) && (wiki != ''))
    gstxt = wiki.replace(/_/g,' ').replace(/,|\(|\)/g,'');
  var google_search = (language == 'D'? google_search_d : google_search_e)
  var hl = (language == 'D'? 'de' : 'en')
  //var addr = (language == 'D'? 'http://de.wikipedia.org/wiki/Spezial:Suche?go=Artikel&search=' : 'http://en.wikipedia.org/wiki/Special:Search?go=Article&search=')
  var addr = 'http://'+hl+'.wikipedia.org/wiki?search=';
  if (vizMode != "")
    addr = 'http://'+hl+'.m.wikipedia.org/wiki?search=';
  if (wiki == null) {
    addr += place;
    if ((tabId == 'ts') && (infomode > 0) && (adminname != null))
      if (language == 'D')
        addr += ' (' + (adminname) + ')';
      else
        addr += ', ' + (adminname);
  }
  else
    addr += wiki
  newP = document.createElement('p');
  if (vizMode == '') {
    text = (language == 'D'? 'Google-Suche nach: ' : 'Google Search for: ')
    newP.appendChild(document.createTextNode(text))
    for (var i=0; i<google_search.length; i++) {
      var aElem = document.createElement('a');
      aElem.setAttribute('href',computeGoogleCall(gstxt,google_search[i]))
      aElem.setAttribute('target','_blank')
      aElem.appendChild(document.createTextNode(google_search[i]))
      newP.appendChild(aElem)
      newP.appendChild(document.createTextNode(' - '));
    }
    aElem = document.createElement('a');
    aElem.setAttribute('href','http://images.google.com/images?gbv=2+hl='+hl+'&q='+gstxt)
    aElem.setAttribute('target','_blank')
    text = (language == 'D')? 'Bilder' : 'Images'
    aElem.appendChild(document.createTextNode(text))
    newP.appendChild(aElem)
    infoCell.appendChild(newP)

    newP = document.createElement('p');
    var aElem = document.createElement('a');
    aElem.setAttribute('href','javascript:showObjectDiagram('+id+',"'+((tabId=="ts")?'C':'L')+'")')
    aElem.appendChild(document.createTextNode((hl == 'de'? 'Einwohnerentwicklung' : 'Population Graph')))
    newP.appendChild(aElem);
    newP.appendChild(document.createTextNode(' - '));
    var aElem = document.createElement('a');
    aElem.setAttribute('href',addr)
    aElem.setAttribute('target','_blank')
    aElem.appendChild(document.createTextNode('Wikipedia'))
    newP.appendChild(aElem)
    infoCell.appendChild(newP)
  }
  else {
    newP.innerHTML = '<a href="'+computeGoogleCall(gstxt,'')+'" target="_blank"><img class="img48" src="images/ggsearch48.png" /></a>' +
      '<a href="http://www.google.com/m?site=images&hl='+hl+'&q='+encodeURI(gstxt)+'" target="_blank"><img class="img48" src="images/ggimg48.png" /></a>' +
      '<a href="'+addr+'" target="_blank"><img class="img48" src="images/wiki48.png" /></a>' +
      '<a href="javascript:showObjectDiagram('+id+",'"+((tabId=="ts")?'C':'L')+"'"+')"><img class="img48" src="images/graph48.png" /></a>' +
      ((tabId != 'ts')? '' : ( '<a href="http://www.citypopulation.de/gmaps/mobi.php?LANG='+language+'&ID='+id+ '&MAPTYPE=normal" target="_blank"><img class="img48" src="images/gmaps48.png" /></a>' ));
    infoCell.appendChild(newP)
  }

  var sep = pos.indexOf(' ; ')
  if ((vizMode=='') && (sep > 0)) {
    newP.appendChild(document.createTextNode(' - Google Maps: '))
    aElem = document.createElement('a');
    aElem.setAttribute('href','javascript:showmap('+id+',"'+((tabId=="ts")?'C':'L')+'","'+place+'","'+adminname+'","'+countryname+'","sat")')
    aElem.appendChild(document.createTextNode(language == 'D' ? 'Satellitenkarte' : 'Satellite Map'))
    newP.appendChild(aElem)
    newP.appendChild(document.createTextNode('/'))
    aElem = document.createElement('a');
    aElem.setAttribute('href','javascript:showmap('+id+',"'+((tabId=="ts")?'C':'L')+'","'+place+'","'+adminname+'","'+countryname+'","normal")')
    aElem.appendChild(document.createTextNode(language == 'D' ? 'Strassenkarte' : 'Street Map'))
    newP.appendChild(aElem)
    newP.appendChild(document.createTextNode(' - '))
    aElem = document.createElement('a');
    aElem.setAttribute('href','javascript:showmap('+id+',"'+((tabId=="ts")?'C':'L')+'","'+place+'","'+adminname+'","'+countryname+'","osm")')
    aElem.appendChild(document.createTextNode('Open Street Map'))
    newP.appendChild(aElem)
  }
  else if ((vizMode=='') && (tabId == 'ts')) {
    newP.appendChild(document.createTextNode(' - Google: '))
    aElem = document.createElement('a');
    aElem.setAttribute('href','http://www.citypopulation.de/gmaps/mobi.php?LANG=' + language + '&ID=' + id + '&MAPTYPE=sat')
    aElem.setAttribute('target','_blank')
    aElem.appendChild(document.createTextNode(language == 'D' ? 'Satelliten-' : 'Satellite Map'))
    newP.appendChild(aElem)
    newP.appendChild(document.createTextNode('/'))
    aElem = document.createElement('a');
    aElem.setAttribute('href','http://www.citypopulation.de/gmaps/mobi.php?LANG=' + language + '&ID=' + id + '&MAPTYPE=normal')
    aElem.setAttribute('target','_blank')
    aElem.appendChild(document.createTextNode(language == 'D' ? 'Strassenkarte' : 'Street Map'))
    newP.appendChild(aElem)

    //newP.appendChild(document.createTextNode(' - '))
    //aElem = document.createElement('a');
    //aElem.setAttribute('href','http://www.citypopulation.de/osm/mobi.html?LANG=' + language + '&ID=c' + id)
    //aElem.setAttribute('target','extern')
    //aElem.appendChild(document.createTextNode('Open Street Map'))
    //newP.appendChild(aElem)
 }

 if (appletReady == 1) {
   var webaddr = document.ShowMap.getAttrOfSelectedObject("web")
   if ((webaddr != null) && (webaddr != '')) {
    newP.appendChild(document.createTextNode(' - '))
    aElem = document.createElement('a');
    aElem.setAttribute('href',webaddr)
    aElem.setAttribute('target','extern')
    text = (language == 'D'? 'Offizielle Web Site' : 'Official Web Site')
    aElem.appendChild(document.createTextNode(text))
    newP.appendChild(aElem)
   }
  }

  infoCell.appendChild(newP)
  
  if (row.nextSibling != null)
    rowParent.insertBefore(infoRow,row.nextSibling)
  else
    rowParent.appendChild(infoRow)
}

function computeGoogleCall(place,obj) {
  if (language == 'D')
    return "http://www.google.de/cse?q="+encodeURI(place)+' '+obj+"&lang=de&sa=Suche&cx="+google_cse_client_de+"&ie=UTF-8"
  else
    return "http://www.google.com/cse?q="+encodeURI(place)+' '+obj+"&lang=en&sa=Search&cx="+google_cse_client+"&ie=UTF-8"
}

function change_lang() {
  var addr = window.location.href.toString()
  var i = addr.indexOf('#')
  if (i > 0)
    addr = addr.substring(0,i)
  if (addr.indexOf('index_d.html') > 0)
    window.location.href = addr.substring(0,addr.length-12)
  else if (addr.indexOf('_d.html') > 0)
    window.location.href = addr.substring(0,addr.length-7)+".html"
  else if (addr.indexOf('.html') > 0)
    window.location.href = addr.substring(0,addr.length-5)+"_d.html"
  else
    window.location.href = addr+"index_d.html"
}

function go(r) {
  location.href = r
}

function showPage(pre,select,post) {
  if (select.selectedIndex>0)
    window.location.href=pre+select.options[select.selectedIndex].value+post
}

function showID (newActID) {
  //if (newActID != actID) {
    if (actID >= 0) {
      var cell = document.getElementById ("i"+actID);
      if (cell != null)
        cell.style.backgroundColor = oldBackground
    }
    actID = newActID
    if (actID >= 0) {
      var cell = document.getElementById ("i"+actID);
      if (cell != null) {
        oldBackground = cell.style.backgroundColor
        cell.style.backgroundColor = '#ffcc33'
        showinfo(actID)
        cell.scrollIntoView()
      }
      else {
        actID = -1
        showinfo(actID)
      }
    }
    else
      showinfo(actID)
  //}
  if ((appletReady == 1) && (diagramReady == 0)) {
    document.ShowMap.activateDiagram()
    diagramReady = 1
  }
}

function showHelp (langID) {
  if (langID == 1)
    window.open('info/appletinfo_e.html','nw','directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes')
  else
    window.open('info/appletinfo_e.html','nw','directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes')
}

function setAppletReady () {
  appletReady = 1
}

function cellValue (tableId, index, col) {
  var table = document.getElementById(tableId)
  var cell = table.rows[index].cells[col]
  return cell.innerHTML;
}

function colNum (tableId) {
  var table = document.getElementById(tableId)
  var cells = table.rows[1].cells
  return ""+cells.length
}

function rowNum (tableId) {
  var table = document.getElementById(tableId)
  return ""+table.rows.length
}

function value (cell, num) {
  if (num == true) {
    txt = cell.innerHTML.toUpperCase();
    var pos = txt.toUpperCase().indexOf("<A");
    if (pos >= 0)
      return 0;
    txt1 = "0";
    for (var i=0; i<txt.length; i++) {
      ch = txt.charAt(i);
      if ((ch >= '0') && (ch <= '9'))
        txt1 += String.fromCharCode(txt.charCodeAt(i));
    }
    return parseInt(txt1,10);
  }
  else {
    if (cell.firstChild == null)
      txt = cell.innerHTML.toUpperCase();
    else if (cell.firstChild.nodeName.toUpperCase() == "A")
      txt = cell.firstChild.firstChild.nodeValue.toUpperCase();
    else
      txt = cell.innerHTML.toUpperCase();
    txt1 = "";
    for (var i=0; i<txt.length; i++) {
      ch = txt.charAt(i);
      if ((ch != "'") && (ch != ' ') && (ch != '-'))
        txt1 += String.fromCharCode(txt.charCodeAt(i));
    }
    return txt1
  }
}

function Row (value, row) {
  this.value = value
  this.row = row
}

function compare(a, b) {
  if (globNum) {
    var diff = b.value-a.value;
    if (diff != 0)
      return diff;
    else
      return a.row.getAttribute('ordNum')-b.row.getAttribute('ordNum')
  }
  if (a.value < b.value)
    return -1;
  else if (a.value == b.value)
    return a.row.getAttribute('ordNum')-b.row.getAttribute('ordNum')
  return 1
}

function sort (id, column, num, ign) {
  var table = document.getElementById(id);
  if (table == null)
    return;
  if (infoRow != null) {
    infoRow.parentNode.removeChild(infoRow)
    infoRow = null
  }
  var tableBody = table.firstChild;
  if (tableBody.nodeName.toUpperCase() != "TBODY")
    tableBody = table.childNodes[1];
  if (tableBody.nodeName.toUpperCase() != "TBODY") {
    alert("For this browser, sorting is not supported!");
    return;
  }
  if ((namesNumbered != 'f') && (namesNumbered != id)) {
    for (var i=0; i<table.rows.length; i++)
      table.rows[i].setAttribute('ordNum',i);
    if (namesNumbered == 's')
      namesNumbered = id;
    else
      namesNumbered = 'f';
  }
  globNum = num;
  var data = new Array();
  var max = table.rows.length-ign;
  if (column > 1)
    for (var i=1; i<max; i++)
      data[i-1] = new Row(value(table.rows[i].cells[column],num),table.rows[i]);
  else
    for (var i=1; i<max; i++)
      data[i-1] = new Row(Number(table.rows[i].getAttribute('ordNum')),table.rows[i]);
  data.sort(compare)
  for (var i=0; i<max-1; i++)
    tableBody.appendChild(data[i].row);
  if (ign > 0)
    for (var i=1; i<=ign; i++)
      tableBody.appendChild(table.rows[1]);
}

function cValue (cell, num) {
  if (num == true) {
    txt = cell.innerHTML;
    var pos = txt.toUpperCase().indexOf("<A");
    if (pos >= 0)
      return "";
    txt1 = "";
    for (var i=0; i<txt.length; i++) {
      ch = txt.charAt(i);
      if ((ch >= '0') && (ch <= '9'))
        txt1 += String.fromCharCode(txt.charCodeAt(i));
    }
    return txt1;
  }
  else {
    if (cell.firstChild == null)
      txt = cell.innerHTML;
    else if (cell.firstChild.nodeName == "A")
      txt = cell.firstChild.firstChild.nodeValue;
    else
      txt = cell.innerHTML;
    var p1 = txt.indexOf('<SPAN');
    if (p1 >= 0)
        txt = txt.substr(0,p1);
    return txt
  }
}

function show_diagram (id,minusRows) {
  if (diagramReady < 0)
    return;
  var table = document.getElementById(id);
  if (table == null)
    return;
  document.DiagramApplet.openDiagram(id)
}

function setData (id,minusRows) {
  var table = document.getElementById(id);
  if (table == null)
    return;
  var max = table.rows.length-minusRows;
  var header = '';
  var colNum = table.rows[0].cells.length;
  var numeric = new Array(colNum);
  for (var j=0; j<colNum; j++) {
    var cellValue = table.rows[0].cells[j]
    numeric[j] = cellValue.innerHTML.indexOf('true') >= 0
    header +=  cValue(cellValue,false)+ '##';
  }
  var cells = '';
  for (var i=1; i<max; i++) {
    cells += table.rows[i].cells[0].innerHTML + '##';
    for (var j=1; j<colNum; j++)
      cells += cValue(table.rows[i].cells[j],numeric[j]) + '##';
  }
  document.DiagramApplet.setData(id,header,cells,max,colNum)
}

function newDate(txt) {
  var y = txt.substr(0,4);
  var m = txt.substr(5,2);
  var d = txt.substr(8,2);
  return new Date(y,m-1,d);
}

function getData (tid,oid) {
  var table = document.getElementById(tid);
  if (table == null)
    return null;
  var colNum = table.rows[0].cells.length;
  var numeric = new Array(colNum);
  var first = true;
  var ref = "";
  var res = "";
  var n = 0;
  var years = 0;
  var currPopNum = null;
  var oldPopNum = null;
  var currYear = null;
  var oldYear = null;
  var oldDate = null;
  for (var j=0; j<colNum; j++) {
    var cv = cValue(table.rows[0].cells[j],false);
    var pos = cv.indexOf(' 19');
    if (pos < 0)
      pos = cv.indexOf(' 20');
    numeric[j] = (pos >= 0);
    if (numeric[j]) {
      if (first) {
        ref = cv.substr(0,2);
        first = false;
      }
      else if ((tid == 'ts') && (ref != cv.substring(0,2)))
        numeric[j] = false;
      if (numeric[j]) {
        var currDate = newDate(cv.substr(pos+1,10));
        oldYear = currYear;
        currYear = cv.substr(pos+1,4);
        if (oldDate != null)
          years = (currDate-oldDate)/(1000*60*60*24*365.25);
        res += cv.substr(pos+1,4)+",";
        n++;
        oldDate = currDate;
      }
    }
  }
  var cell0 = document.getElementById('i'+oid);
  if (cell0 == null)
    return null;
  var row = cell0.parentNode;
  var place = row.cells[1].firstChild;
  if (place.firstChild != null)
    place = place.firstChild;
  place = place.nodeValue
  place = place.replace(/&/g,'+');
  res = "lang="+(language=='D'?'de':'en')+"&name="+place+"&popnum="+n+"&years="+res+"&pop=";
  for (var j=1; j<colNum; j++)
    if (numeric[j]) {
      var cv = cValue(row.cells[j],numeric[j]);
      oldPopNum = currPopNum;
      currPopNum = parseInt(cv);
      if (cv == "0")
        cv = "";
      res += cv+",";
    }
  var resObj = {
    params: res,
    popCols: n,
    years: years,
    currPopNum: currPopNum,
    oldPopNum: oldPopNum,
    currYear: currYear,
    oldYear: oldYear
  }
  return resObj;
}

function showDiagramHelp (lang) {
   if (lang == 'D')
     window.open('info/diagraminfo_e.html','nw','directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes')
   else
     window.open('info/diagraminfo_e.html','nw','directories=no,location=no,menubar=no,status=no,scrollbars=yes,resizable=yes')
}

function setDiagramAppletReady () {
  var butt = document.getElementById("CityDiagramButton");
  if (butt != null) {
    butt.disabled = false
    diagramReady = 0
  }
  var butt = document.getElementById('ProvDiagramButton');
  if (butt != null) {
    butt.disabled = false
    diagramReady = 0
  }
  var b1 = document.getElementById("pdb");
  if (b1 != null) {
    b1.src = imD.src
    diagramReady = 0
  }
  var b2 = document.getElementById("cdb");
  if (b2 != null) {
    b2.src = imD.src
    diagramReady = 0
  }
}

function setDiagramAppletData () {
  setData('ts',0)
  setData('tl',1)
}

function showObjectDiagram (id,type) {
  var tableId = (type.charAt(0)=='C') ? 'ts' : 'tl';
  var table = document.getElementById(tableId)
  if (table == null)
    return;
  if (diagramReady >= 0) {
    focus()
    document.DiagramApplet.openObjectDiagram(tableId,id+"")
  }
  else {
    var res = getData(tableId,id);
    if (res == null)
      return;
    var url = 'http://www.citypopulation.de/php/diagramparam.php?type='+type+'&'+encodeURI(res.params);
    var frm = document.getElementById("lgraphframe");
    if (frm == null) {
      var div = document.createElement('div');
      div.setAttribute("id","lgraph");
      div.innerHTML = '<iframe id="lgraphframe" scrolling="no" frameborder="0" src="'+url+'"></iframe><img id="lgraphclose" src="images/close.gif" alt="close" onclick="closediagr()" />';
      document.getElementsByTagName("body")[0].appendChild(div);
    }
    else {
      frm.setAttribute('src',url);
      goBack = true;
    }
    var scrollY = window.pageYOffset;
    var diagr = document.getElementById("lgraph");
    if (vizMode == "")
      diagr.style.top =  (scrollY+40)+"px";
    else if (vizMode == "320")
      diagr.style.top =  (scrollY+70)+"px";
    else if (vizMode == "480")
      diagr.style.top =  (scrollY+3)+"px";
    diagr.style.visibility = "visible";
  }
}

function closediagr() {
  var diagr = document.getElementById("lgraph");
  diagr.style.visibility = "hidden";
  if ((vizMode == "") && goBack) history.back();
}

function showmap (id,objtype,place,adminname,countryname,maptype) {
  var url = (maptype == "osm")? 'http://www.citypopulation.de/osm/index.html' : 'http://www.citypopulation.de/gmaps/index.html';
  url += '?id=' + id + '&objtype=' + objtype + '&name=' + encodeURI(place) + '&admin=' + encodeURI(adminname) + '&country=' + encodeURI(countryname) + '&maptype=' + maptype + '&lang=' + language;
  var frm = document.getElementById("gmapframe");
  if (frm == null) {
    var div = document.createElement('div');
    div.setAttribute("id","gmap");
    var closeTitle = (language == 'D') ? 'Fenster schliessen' : 'Close Window';
    div.innerHTML = '<iframe id="gmapframe" scrolling="no" frameborder="0" src="'+url+'"></iframe><img id="gmapclose" src="images/close.gif" alt="close" title="'+closeTitle+'" onclick="closeGMap()" />';
    document.getElementsByTagName("body")[0].appendChild(div);
    frm = document.getElementById("gmapframe");
  }
  else {
    frm.setAttribute('src',url);
    goBackGMap = true;
  }
  var gmap = document.getElementById("gmap");
  gmap.style.right = "10px";
  gmap.style.bottom = "10px";
  gmap.style.visibility = "visible";
}

function closeGMap() {
  var gmap = document.getElementById("gmap");
  gmap.style.visibility = "hidden";
  //if ((vizMode == "") && goBackGMap) history.back();
}

function search (ncountry) {
  nplace = document.searchform.place.value;
  if (trim(nplace).length >= 1)
    location.href = "http://www.citypopulation.de/php/search.php?country=" + ncountry + "&pagelang=en&place=" +nplace;
  else
    alert ("The search term requires at least 1 character!");
}

function search_d (ncountry) {
  nplace = document.searchform.place.value;
  if (trim(nplace).length >= 1)
    location.href = "http://www.citypopulation.de/php/search.php?country=" + ncountry + "&pagelang=de&place=" +nplace;
  else
    alert ("Bitte mindestens 1 Zeichen eingeben!");
}

function trim (s) {
  if (s == "")
    return s;
  while (s.indexOf(" ") == 0)
    s = s.substr(1);
  while (s.lastIndexOf(" ") == s.length-1)
    s = s.substr(0,s.length-1);
  return s;
}

function searchplace (place, admin, country, type, pagelang) {
  if (admin != null)
    place += "%20%22"+admin+"%22";
  if (country != null)
    place += "%20%22"+country+"%22";
  if (pagelang != "de")
    window.location.href = "http://www.citypopulation.de/results4places.html?back="+window.location+"&q="+place+"%20%22"+type+"%22&cx=partner-pub-7251478393613561%3Atqli9y-khjx&cof=FORID%3A10&ie=UTF-8&sa=Search#1075"
  else
    window.location.href = "http://www.citypopulation.de/results4places_d.html?back="+window.location+"&q="+place+"%20%22"+type+"%22&cx=partner-pub-7251478393613561%3Atqli9y-khjx&cof=FORID%3A10&ie=UTF-8&sa=Suche#1075"
}

function back() {
  var pos = window.location.search.indexOf("back=");
  var pos2 = window.location.search.indexOf("q=");
  if ((pos >= 0) && (pos2 > pos))
    window.location.replace(window.location.search.substring(pos+5,pos2-1))
  else
    window.history.back();
}

function search1 (pagelang) {
  country = document.searchform1.select1.value;
  search2 (pagelang,country);
}

function search2 (pagelang,country) {
  place = trim(document.searchform1.place1.value)
  if (place == "") {
    if (pagelang == "de")
      alert("Bitte einen vollständigen Ortsnamen zur Suche eingeben.");
    else
      alert("Please input a complete place name for search.");
    return;
  }
  if (place.length < 3) {
    if (pagelang == "de")
      alert("Der Suchname ist zu kurz.\nEin vollständiger Name mit der minimalen Länge ist erforderlich.");
    else
      alert("The search name is too short.\nA complete place name with a minimum length of 3 is required.");
    return;
  }
  if (country != "")
    if (country.indexOf(' ') >= 0)
      country = "+%22"+encodeURI(country)+"%22";
    else
      country = "+"+encodeURI(country);
  var winheight = (vizMode == "") ? "1500" : ((vizMode == "320") ? "900" : "750");
  if (pagelang != "de")
    window.location.href = "http://www.citypopulation.de/results.html?q=%22"+encodeURI(place)+"%22"+country+"+Maps+Contents&cx=partner-pub-7251478393613561%3Ab1jx8t-apwp&cof=FORID%3A10&ie=UTF-8&sa=Search#"+winheight
  else
    window.location.href = "http://www.citypopulation.de/results_d.html?q=%22"+encodeURI(place)+"%22"+country+encodeURI("+Karten+Übersicht")+"&cx=partner-pub-7251478393613561%3A9831784558&cof=FORID%3A10&ie=UTF-8&sa=Suche#"+winheight
}

function gmsearch (pagelang) {
  ncountry = document.searchform.country.value;
  gmsearch2 (pagelang,ncountry);
}

function gmsearch2 (pagelang,ncountry) {
  nplace = document.searchform.place.value;
  gmsearch3 (pagelang,ncountry,nplace);
}

function gmsearch3 (pagelang,ncountry,nplace) {
  if (trim(nplace).length >= 1)
    location.href = "http://www.citypopulation.de/php/search.php?country=" + encodeURI(ncountry) + "&pagelang=" + pagelang + "&place=" + encodeURI(nplace);
  else if (pagelang == 'de')
    alert ("Bitte mindestens ein Zeichen als Suchbegriff eingeben!");
  else
    alert ("The search term requires at least 1 character!");
}

function set_mobiadv() {
window.googleAfmcRequest = {
  client: 'ca-mb-pub-7251478393613561',
  ad_type: 'text_image',
  output: 'html',
  channel: '7759086021',
  format: '320x50_mb',
  oe: 'utf8',
  color_border: '8898BF',
  color_bg: 'FFFFFF',
  color_link: '0000FF',
  color_text: '000000',
  color_url: '0066CC'
};
}

function set_mobiadvcountry() {
window.googleAfmcRequest = {
  client: 'ca-mb-pub-7251478393613561',
  ad_type: 'text_image',
  output: 'html',
  channel: '1065437496',
  format: '320x50_mb',
  oe: 'utf8',
  color_border: '8898BF',
  color_bg: 'FFFFFF',
  color_link: '0000FF',
  color_text: '000000',
  color_url: '0066CC'
};
}

function show_mobiadv (cntry) {
  if (vizMode == "")
    return;
  if (cntry)
    set_mobiadvcountry();
  else
    set_mobiadv();
  document.writeln('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_afmc_ads.js"></script>');
}

function async_call (adr,tag,parentID) {
  function async_load(){
    var s = document.createElement('script');
    s.type = 'text/javascript'; s.async = true; s.src = adr;
    if (tag != null) {
      var x = document.getElementsByTagName(tag)[0]; x.parentNode.insertBefore(s, x);
    }
    else {
      var x = document.getElementById(parentID); x.appendChild(s);
    }
  }
  if (window.attachEvent)
    window.attachEvent('onload', async_load);
  else
    window.addEventListener('load', async_load, false);
}

function show_adv (type) {
  if (vizMode != "")
    return;
  google_ad_client = "pub-7251478393613561";
  google_alternate_color = "F9F9F9";
  google_ad_type = "text_image";
  google_ad_channel = "8220287810";
  google_color_border = "8898BF";
  google_color_bg = "FFFFFF";
  google_color_link = "0000FF";
  google_color_text = "000000";
  google_color_url = "0066CC";
  if (type == "linklist") {
    google_ad_format="468x15_0ads_al"; google_ad_width=468; google_ad_height=15; google_ad_channel="8778752441";
  }
  else if (type == "cntry_h") {
    google_ad_format="728x90_as"; google_ad_width=728; google_ad_height=90;
  }
  else if (type == "cntry_q") {
    google_ad_format="300x250_as"; google_ad_width=300; google_ad_height=250;
  }
  else if (type == "indexadv") {   // incl. "continentadv"
    google_ad_format="336x280_as"; google_ad_width=336; google_ad_height=280; google_ad_channel="7208901011"; 
  }
  else if (type == "expl") {
    google_ad_format="120x600_as"; google_ad_width=120; google_ad_height=600; google_ad_channel="7208901011";
  }
  else if (type == "index_h") {
    google_ad_format="728x90_as"; google_ad_width=728; google_ad_height=90; google_ad_channel="7208901011";
  }
  else {
    google_ad_format="728x90_as"; google_ad_width=728; google_ad_height=90;
  }
  //async_call("http://pagead2.googlesyndication.com/pagead/show_ads.js",null,type);
  document.writeln('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19698339-1']);
_gaq.push(['_trackPageview']);

async_call(('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js','script',null);

