/**
 * PAGEBUTLER JavaScript
 * (c) Copyright 2004-2007 Media Trooper GmbH
 * www.mediatrooper.de
 *
 *
 *
 *
 *
 *
 *
 *
 * WER LIEST HIER EIGENTLICH UNSERE JAVASCRIPTS? :-p
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

var funnycounter=0;

function showEnglishVersionNote()
{
	document.getElementById('LANGDE').style.display='none';
	document.getElementById('LANGEN').style.display='block';
	setTimeout("hideEnglishVersionNote();",6000);
	funnycounter++;
}

function hideEnglishVersionNote()
{
	document.getElementById('LANGDE').style.display='block';
	document.getElementById('LANGEN').style.display='none';
}

function printLangswitch()
{
	var s = '<p id="LANGDE">Select your Language: <span>german</span> | <a href="#" onClick="showEnglishVersionNote()">english</a></p>';
	s += '<p id="LANGEN" style="display:none;"><span>Sorry, the english version is in progress. It will be available very soon.</span></p>';
	document.write(s);
}




var BigCalcer = Class.create({
  
  initialize: function() 
  {
  	this.adresses_count = 0;
  	
  	this.calcSliderbig = new Control.Slider(	'slidehandlebig', 
												'slidetrackbig',
			{	range:$R(10000,250000)
			});
	this.calcSliderbig.options.SLIDERBAR = this;
	this.calcSliderbig.options.onSlide = function(value)
	{
		this.SLIDERBAR.setCount(this,value);
	};
	this.calcSliderbig.options.onChange = function(value)
	{
		this.SLIDERBAR.setCount(this,value);
	};

	this.updateFromInput();
	
	// manueller input
	$('calc_adresses_input').observe('change', this.updateFromInput.bindAsEventListener(this));
	$('calc_adresses_input').observe('keypress', this.checkEnter.bindAsEventListener(this));
  },
  
  checkEnter: function(event)
  {
	 var key = event.which || event.keyCode;

	 switch (key) {
	   case Event.KEY_RETURN:
	     this.updateFromInput();
	     break;
	 }
  },
  
  updateFromInput: function()
  {
  	this.calcSliderbig.setValue( Number($('calc_adresses_input').getValue()).round() );
  },
  
  setCount: function(e,v)
  {
	var v = v/1000;
	this.adresses_count = v.ceil()*1000;
	
	if( this.adresses_count>=245000 )
	{
		$("cantactnagg").show();
	}
	else
	{
		$("cantactnagg").hide();
	}
	
	$('calc_adresses_input').setValue(this.adresses_count);
	if( $("formfield_addressmenge") )
	{
		$("formfield_addressmenge").setValue(this.adresses_count);
	}
	
	var m = this.adresses_count/1000*135;
	$('schieberprice').update( formatNumber( m.round() ) );
	
	var abwrkpraemie = 0;
	var total = m+2300;
	if( total>=5000 )
	{
		abwrkpraemie = -2500;
	}
	$('praemie').update( formatNumber( (abwrkpraemie).round() ) );
	
	$('calctotal').update( formatNumber( (total+abwrkpraemie).round() ) );
  }

});


function formatNumber(num, decpoint, sep) 
{
  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ".";
  }
  if (arguments.length == 1) {
    sep = ".";
    decpoint = ",";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";

  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}

