var dasland,derort = "";

function getland_ort()
{
 for(i=0;i<document.getElementById('land').length;i++)
 {
  if (document.getElementById('land').options[i].selected === true)
  {
   var dasland = "ort_" + document.getElementById('land').options[i].value;
//   alert(dasland);
  }
 }
 if (dasland!="" && typeof(dasland)!="undefined" && document.getElementById(dasland)!=null)
 for(i=0;i<document.getElementById(dasland).length;i++)
 {
  if (document.getElementById(dasland).options[i].selected === true)
  {
   var derort = explode("|",base64_decode(document.getElementById(dasland).options[i].value));
//   alert(derort);
  }
 }
}

// usage: format_zahl( number [, number]  [, bool]  )
/***********************************************************/
function formatZahl(zahl, k, fix)
{
    if(!k) k = 0;
    var neu = '';
    // Runden
    var f = Math.pow(10, k);
    zahl = '' + parseInt( zahl * f + (.5 * (zahl > 0 ? 1 : -1)) ) / f ;
    // Komma ermittlen
    var idx = zahl.indexOf('.');
    // fehlende Nullen einf&uuml;gen
    if(fix)
    {
         zahl += (idx == -1 ? '.' : '' )
         + f.toString().substring(1);
    }
    idx = zahl.indexOf('.');
    if( idx == -1) idx = zahl.length;
    else neu = ',' + zahl.substr(idx + 1, k);
    while(idx > 0)
    {
        if(idx - 3 > 0)
        neu = '.' + zahl.substring( idx - 3, idx) + neu;
        else
        neu = zahl.substring(0, idx) + neu;
        idx -= 3;
    }
    return neu;
}

/***********************************************************/
function formatZahl2(zahl, k, fix)
{
    if(!k) k = 0;
    var neu = '';
    // Runden
    var f = Math.pow(10, k);
    zahl = '' + parseInt( zahl * f + (.5 * (zahl > 0 ? 1 : -1)) ) / f ;
    // Komma ermittlen
    var idx = zahl.indexOf('.');
    // fehlende Nullen einf&uuml;gen
    if(fix)
    {
         zahl += (idx == -1 ? '.' : '' )
         + f.toString().substring(1);
    }
    idx = zahl.indexOf('.');
    if( idx == -1) idx = zahl.length;
    else neu = ',' + zahl.substr(idx + 1, k);
    while(idx > 0)
    {
        if(idx - 3 > 0)
        neu = '.' + zahl.substring( idx - 3, idx) + neu;
        else
        neu = zahl.substring(0, idx) + neu;
        idx -= 3;
    }
  neu = str_replace(".","",neu);
    return neu;
}

/***********************************************************/
function str_replace (search, replace, subject)
{
  var result = "";
  var  oldi = 0;
  for (i = subject.indexOf (search)
     ; i > -1
     ; i = subject.indexOf (search, i))
  {
    result += subject.substring (oldi, i);
    result += replace;
    i += search.length;
    oldi = i;
  }
  return result + subject.substring (oldi, subject.length);
}

/***********************************************************/
function cls(id)
{
 document.getElementById(id).value="";
}

/***********************************************************/
function fla()
{
 lang = str_replace(".","",document.getElementById("s2").value);
 lang = str_replace(",",".",lang);
 breit = str_replace(".","",document.getElementById("s3").value);
 breit = str_replace(",",".",breit);
 if ( lang>0 && breit>0 )
 {
  document.getElementById("s1").value=formatZahl2(lang*breit,2,true);
  document.getElementById("s2").value=formatZahl2(lang,2,true);
  document.getElementById("s3").value=formatZahl2(breit,2,true);
 }
}

/***********************************************************/
function base64_decode( data )
{
 var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];
 
 do
 {
  h1 = b64.indexOf(data.charAt(i++));
  h2 = b64.indexOf(data.charAt(i++));
  h3 = b64.indexOf(data.charAt(i++));
  h4 = b64.indexOf(data.charAt(i++));
 
  bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
  o1 = bits>>16 & 0xff;
  o2 = bits>>8 & 0xff;
  o3 = bits & 0xff;
 
  if (h3 == 64) {
   tmp_arr[ac++] = String.fromCharCode(o1);
  } else if (h4 == 64) {
   tmp_arr[ac++] = String.fromCharCode(o1, o2);
  } else {
   tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
  }
 } while (i < data.length);
   
 dec = tmp_arr.join('');
 dec = utf8_decode(dec);
    
 return dec;
}

/***********************************************************/
function utf8_decode ( str_data )
{
 var tmp_arr = [], i = ac = c = c1 = c2 = 0;
 while ( i < str_data.length )
 {
  c = str_data.charCodeAt(i);
  if (c < 128)
  {
   tmp_arr[ac++] = String.fromCharCode(c); 
   i++;
  }
  else if ((c > 191) && (c < 224))
  {
   c2 = str_data.charCodeAt(i+1);
   tmp_arr[ac++] = String.fromCharCode(((c & 31) << 6) | (c2 & 63));
   i += 2;
  } else {
   c2 = str_data.charCodeAt(i+1);
   c3 = str_data.charCodeAt(i+2);
   tmp_arr[ac++] = String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
   i += 3;
  }
 }
 return tmp_arr.join('');
}

/***********************************************************/
function explode( delimiter, string, limit )
{
 var emptyArray = { 0: '' };
 
 // 3. Argument wahlfrei
 if ( arguments.length < 2
  || typeof arguments[0] == 'undefined'
  || typeof arguments[1] == 'undefined' )
 {
  return null;
 }
 
 if ( delimiter === '' || delimiter === false || delimiter === null )
 {
  return false;
 }
 
 if ( typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object' )
 {
  return emptyArray;
 }
 
 if ( delimiter === true ) { delimiter = '1'; }
 
 if (!limit) {
  return string.toString().split(delimiter.toString());
 } else {
  // support f&uuml;r 3. Parameter
  var splitted = string.toString().split(delimiter.toString());
  var partA = splitted.splice(0, limit - 1);
  var partB = splitted.join(delimiter.toString());
  partA.push(partB);
  return partA;
 }
}

/***********************************************************/
function land2ort()
{
 
 lary = new Array("de","ch","at","it","gb","fr");
 with(document)
 {
  if (getElementById('land')!=null)
  {
   document.getElementById('land').options[0].selected == true;
  
   for(i=0;i<getElementById('land').length;i++)
   {
    // wenn gewähltes Element nicht Auswahl ist
    if (getElementById('land').options[i].selected === true && getElementById('land').options[i].value != "")
    {
 //    if (getElementById('land').options[i].value=="")
 //	else 
     for(a=0;a<lary.length;a++)
 	{
      if (lary[a]==getElementById('land').options[i].value)
 	 {
 	  getElementById('ort_'+lary[a]).className = "oshow";
       dasland = 'ort_'+lary[a];
 //     alert(dasland);
 	 }
 	 else 
 	  getElementById('ort_'+lary[a]).className = "onone";
     }
    }
   }
  }
 }
} // eof function

/***********************************************************/
function ort2feld(id)
{
 if (id=="")
 {
  if (dasland != "" && typeof(dasland)!="undefined")
  document.getElementById(dasland).options[0].selected = true;
  document.getElementById(dasland).options[0].value = "";
  return true;
 }
 with(document)
 {
  nn = getElementById('nn').value;
  for(i=0;i<getElementById(id).length;i++)
  {
   // wenn gewähltes Element nicht Auswahl ist
   if (getElementById(id).options[i].selected === true && getElementById(id).options[i].value != "")
   {
    wert = explode("|",base64_decode(getElementById(id).options[i].value));
	derort = wert[0];
    getElementById('ltt').value = formatZahl(wert[2],1,true);
    getElementById('ltn').value = formatZahl(wert[3],1,true);
    getElementById('lfr').value = formatZahl(wert[4],1,true);
    getElementById('nn').value = formatZahl(wert[5]);
    getElementById('hiddennn').value = formatZahl(wert[5]);

   }   
  }
 }
} // eof function

/***********************************************************/
function waehrung_aendern()
{
 with(document)
 {
  if (getElementsByName('waehrung')[0].checked === true)
   w = "&#8364;";
  else if (getElementsByName('waehrung')[1].checked === true)
   w = "SFR";
  else if (getElementsByName('waehrung')[2].checked === true)
   w = "&#163;";
  else  w = "&#8364;";
  getElementById('wsign1').innerHTML = w;
  getElementById('wsign2').innerHTML = w;
  getElementById('wsign3').innerHTML = w;
  getElementById('wsign4').innerHTML = w;
  getElementById('wsign5').innerHTML = w;
  getElementById('wsign6').innerHTML = w;
  getElementById('wsign7').innerHTML = w;
 } // with
} // eof function

/***********************************************************/
function wertwechsel()
{
 with(document)
 {
  ltt = str_replace(".","",getElementById('ltt').value);
  ltt = str_replace(",",".",getElementById('ltt').value);
  ltn = str_replace(".","",getElementById('ltn').value);
  ltn = str_replace(",",".",getElementById('ltn').value);
  lfr = str_replace(".","",getElementById('lfr').value);
  lfr = str_replace(",",".",getElementById('lfr').value);
  wert = "";
  nn = getElementById('nn').value;
  hnn = getElementById('hiddennn').value;
  d = (hnn-nn)/100;
  if (d>0) d = "+"+d;

  if (getElementById('ltt').value=="" || getElementById('ltn').value=="")
  {
   document.getElementById('hiddennn').value = document.getElementById('nn').value;
   return true;
  }

/*
  if (dasland != "" && typeof(dasland)!="undefined" && derort != "")
  {
   for(i=0;i<getElementById(dasland).length;i++)
   {
    if (getElementById(dasland).options[i].selected === true)
    {
     wert = explode("|",base64_decode(getElementById(dasland).options[i].value));
     w1 = formatZahl(eval(wert[2]+d),1,true);
     w2 = formatZahl(eval(wert[3]+d),1,true);
//     alert("Das Land: [" + dasland + "] - der Ort: [" + derort + "]");
    }
   } // for
  }
  else
*/
  {
   w1 = formatZahl(eval(ltt+d),1,true);
   w2 = formatZahl(eval(ltn+d),1,true);
  }   
//  alert(w1 + " " + w2 + " " + d + " " + wert);
  getElementById('ltt').value = w1;
  getElementById('ltn').value = w2;
 } // with
 document.getElementById('hiddennn').value = document.getElementById('nn').value;
} // eof function

land2ort();
