var Kalkulator = {
	url : strHomeUrl+'inc.kalkulator.ajax',
	categories : '',
	anzahlArtikel : 0,
	anzahlStandorte : 0,
	price_text : 0,
	formSend : '',
	overrides : [
		'categories',
		'anzahlArtikel',
		'anzahlStandorte',
		'price_text',
		'formSend',
	],
	setParameter : function(params){
		for (var i = 0; i < Kalkulator.overrides.length; i++) {
			var name = Kalkulator.overrides[i];
			this[name] = params && typeof params[name] != 'undefined' ?
				params[name] : Kalkulator[name];
		}
	},	
	getInformation: function(identifier){
		
		var params = 'formSend=static_kalkulator&categories='+this.categories+'&anzahlArtikel='+this.anzahlArtikel;
		params += '&anzahlStandorte='+this.anzahlStandorte+'&price_text='+this.price_text;
		params += '&identifier='+identifier;
		var objThis	= this;
		
		var intAnzahlArtikel = this.anzahlArtikel;
		
		var myAjax = new Ajax.Request(
		this.url, 
		{
			method: 'post', 
			parameters: params,
			onComplete: function(transport){
				if(200 == transport.status){
					xml = transport.responseXML;
					Kalkulator.getLeadsPrice(xml,intAnzahlArtikel);	
				}
			}
		});
		
	},
	getLeadsPrice : function (xml,intAnzahlArtikel){
		if(xml.childNodes.length > 1){
			var data = xml.childNodes[1];
		} else {
			var data = xml.childNodes[0];	
		}
		
		var laenge = data.childNodes.length;
		
		document.getElementById('message').style.display 	= 'none';
		document.getElementById('message').style.visibility = 'hidden';
		
		for(var i=0;i < laenge; ++i){
			// Knoten Name
			namefunc 		= data.childNodes[i].nodeName;
			if(typeof this['set_'+namefunc] == 'function' && data.childNodes[i].childNodes.length > 0){
				if(data.childNodes[i].childNodes.length > 0){
					this['set_'+namefunc](data.childNodes[i].firstChild.nodeValue,intAnzahlArtikel);					
				}
			}
		}
	},
	set_price : function(inhalt,intAnzahlArtikel){
		document.getElementById('volumen').innerHTML = inhalt;
		
		document.getElementById('direktBuchung').style.display 		= 'block';
		document.getElementById('direktBuchung').style.visibility 	= 'visible';
		
		var link = document.getElementById('direktBuchungLink').href;
		
		tmp = link.split('?');
		
		if(intCountryId == '8'
		|| intCountryId == '28'
		){

			if(intAnzahlArtikel <= 10){
				linkParameter = 10;		
			}else if(intAnzahlArtikel <= 50){
				linkParameter = 50;			
			}else if(intAnzahlArtikel >= 51){
				linkParameter = 999;			
			}


		} else {

			if(intAnzahlArtikel <= 5){
				linkParameter = 5;		
			}else if(intAnzahlArtikel <= 10){
				linkParameter = 10;
			}else if(intAnzahlArtikel <= 30){
				linkParameter = 30;			
			}else if(intAnzahlArtikel <= 50){
				linkParameter = 50;			
			}else if(intAnzahlArtikel >= 51){
				linkParameter = 999;			
			}
		}
		document.getElementById('direktBuchungLink').href = tmp[0]+'?paket='+linkParameter; 
		
	},
	set_leads : function(inhalt,intAnzahlArtikel){
		document.getElementById('leads').innerHTML = inhalt;
	},
	set_meldung : function(inhalt,intAnzahlArtikel){
		document.getElementById('meldung').innerHTML = inhalt;
		document.getElementById('message').style.display 	= 'block';
		document.getElementById('message').style.visibility = 'visible';
		
		document.getElementById('direktBuchung').style.display 		= 'none';
		document.getElementById('direktBuchung').style.visibility 	= 'hidden';
	}
}

function getKalkInfo(identifier){
	var categories 			= document.getElementById('categories').value;
	var anzahlArtikel 		= document.getElementById('anzahlArtikel').value;
	var anzahlStandorte 	= document.getElementById('anzahlStandorte').value;
	var price_text 			= document.getElementById('price_text').value;

	Kalkulator.setParameter({categories : categories, anzahlArtikel : anzahlArtikel, anzahlStandorte : anzahlStandorte, price_text : price_text} );
	Kalkulator.getInformation(identifier);
	
	
}

function closeDiv(id){
	document.getElementById(id).style.display 		= 'none';
	document.getElementById(id).style.visibility 	= 'hidden';
}


function berechnePriceUmsatz(){
	if(document.getElementById('priceUmsatz')){
		var price =  document.getElementById('priceUmsatz').innerHTML;
		
		var boolKommaAsThousand = checkToReplaceKomma(price);
		
		
		if(boolKommaAsThousand == true) {
			getPriceUkFormated(price);
		} else {
			getPriceGermanFormated(price);
		}
		
	}
}

function getPriceGermanFormated(price){
	price = price.replace(/\./g,'');
	price = price.replace(/,/g,'.');

	newPrice = getNewPrice(price);

	strPrice = number_format(newPrice,newPrice.length,',','.');

	if(strPrice.indexOf(',') > -1){
		arrPrice = strPrice.split(',');
		
		if(arrPrice[1].length < 2 ){
			arrPrice[1] = arrPrice[1]+'0';
		}else if(arrPrice[1].length > 2){
			arrPrice[1] = arrPrice[1].substr(0,2);
		}
		
		document.getElementById('priceUmsatz').innerHTML = arrPrice[0]+','+arrPrice[1];
		
	} else {
		
		strPrice = strPrice+',00';
		
		document.getElementById('priceUmsatz').innerHTML = strPrice;
	}
	
	window.setTimeout("berechnePriceUmsatz()", 1000);
}

function getPriceUkFormated(price){
	price = price.replace(/,/g,'');

	newPrice = getNewPrice(price);

	strPrice = number_format(newPrice,newPrice.length,'.',',');

	if(strPrice.indexOf('.') > -1){
		arrPrice = strPrice.split('.');
		
		if(arrPrice[1].length < 2 ){
			arrPrice[1] = arrPrice[1]+'0';
		}else if(arrPrice[1].length > 2){
			arrPrice[1] = arrPrice[1].substr(0,2);
		}
		
		document.getElementById('priceUmsatz').innerHTML = arrPrice[0]+'.'+arrPrice[1];
		
	} else {
		
		strPrice = strPrice+'.00';
		
		document.getElementById('priceUmsatz').innerHTML = strPrice;
	}
	
	window.setTimeout("berechnePriceUmsatz()", 1000);
}

function getNewPrice(price){
	
	prozent30 = (floatUmsatzSekunde * 30 ) / 100;
		
	
		
	var unterGrenze = floatUmsatzSekunde*1 - prozent30*1;
	var oberGrenze 	= floatUmsatzSekunde*1 + prozent30*1;
		
	floatUmsatz = GetRandom(unterGrenze,oberGrenze)+'';
		
	newPrice = (price *100 + floatUmsatz*100)/100;	

	return newPrice;
}

function checkToReplaceKomma(strPrice){
	strPriceTmp = strPrice;

	intPos = strPriceTmp.indexOf(',');

	if(intPos > -1){
		strPriceTmp = strPriceTmp.substr(intPos+1);
		if(strPriceTmp.indexOf(',') > -1){
			return true;
		}
	}

	return false;
}

function /*out: String*/ number_format( /* in: float   */ number,
        /* in: integer */ laenge,
        /* in: String  */ sep,
        /* in: String  */ th_sep ) {

	number = Math.round( number * Math.pow(10, laenge) ) / Math.pow(10, laenge);
	str_number = number+"";
	arr_int = str_number.split(".");
	if(!arr_int[0]) arr_int[0] = "0";
	if(!arr_int[1]) arr_int[1] = "";
	if(arr_int[1].length < laenge){
	nachkomma = arr_int[1];
	for(i=arr_int[1].length+1; i <= laenge; i++){  nachkomma += "0";  }
	arr_int[1] = nachkomma;
	}
	if(th_sep != "" && arr_int[0].length > 3){
	Begriff = arr_int[0];
	arr_int[0] = "";
	for(j = 3; j < Begriff.length ; j+=3){
	Extrakt = Begriff.slice(Begriff.length - j, Begriff.length - j + 3);
	arr_int[0] = th_sep + Extrakt +  arr_int[0] + "";
	}
	str_first = Begriff.substr(0, (Begriff.length % 3 == 0)?3:(Begriff.length % 3));
	arr_int[0] = str_first + arr_int[0];
	}
	return arr_int[0]+sep+arr_int[1];
}


function GetRandom( min, max ) {

        if( min > max ) {
        	return( -1 );
        }

        if( min == max ) {
        	return( min );
        }

        return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}




