window.updater = null;
window.addEvent('domready', function(){
	var netto= $('order_form_tx_salesdb_bills.netto');	
	var frozenSet = $$('#order_form fieldset.frozen');
	//var frozen = frozenSet.length>0;
	
	if (netto && frozenSet.length==0) { 
		// enable the DHTML fields if we have JS
		$$('#order_form dt.JSonly, #order_form dd.JSonly').setStyle('display','block');
		window.updater = new Updater(netto);	
		window.updater.recalculate();
	}
	/*
	$$('input.autofocus','select.autofocus','textarea.autofocus').each(
		function(el) {
			if (el.focus) {
				el.focus();
			}
		}
	);
	*/


	if (frozenSet.length==0 && $('selectedStaff')) {
		new Sortables(
			$('selectedStaff'),
			{
				onComplete:function() {					
					var list = [];
					//console.log($$('#selectedStaff li'));
					$$('#selectedStaff li').each(function(e) {
						list[list.length] = e.id.split('_')[1];
					});
					$('order_form_tx_salesdb_bills.hostesses_rel').value = list.join(',');
					
				}
				
			}
			
		);
	}
});


function Updater(el) {
	this.init = function () {	
		this.netto = $(el);
		this.vat = $('order_form_tx_salesdb_bills.vat');
		this.brutto = $('order_form_tx_salesdb_bills.brutto');
		
		this.days = $('order_form_tx_salesdb_bills.days');
		this.dailyrate = $('order_form_tx_salesdb_bills.dailyrate');
		this.totaldays = $('order_form_tx_salesdb_bills.totaldays');
		
		this.persons = $('order_form_tx_salesdb_bills.hostesses');
		this.persons2 = $('order_form_tx_salesdb_bills.hostesses2');
		if (this.persons && this.persons.value) {
			this.persons.addEvents({
				keypress:this.somethingChanged.bindAsEventListener(this),
				change	:this.somethingChanged.bindAsEventListener(this)
			});		
		}
		
		this.country = $('order_form_tx_salesdb_customers.country');
		if (this.country) {
			this.country.addEvents({
				change	:this.somethingChanged.bindAsEventListener(this)
			});		
		}
		
		
	}
	
	this.somethingChanged = function(e,timePassed) {
		// wait for the changes to arrive
		if (!timePassed) {
			this.somethingChanged.delay(100,this,[e,true]);
		} else {		
			this.recalculate();
		}
	}
	
	this.recalculate = function() {
		var date1 = this.getDate('order_form_tx_salesdb_bills.activity_begin');
		var date2 = this.getDate('order_form_tx_salesdb_bills.activity_end');
		

		var days = 1 + Math.round((date2.getTime() - date1.getTime()) / (60*60*24*1000));
		days = (isNaN(days) || days < 0) ? 0 : days;
		var persons = parseInt(this.persons.value);
		persons = isNaN(persons) ? 0 : persons;
		var dailyrate = this.calcPricePerDay(days,persons);
		var totaldays = days * persons;
		var netto = dailyrate * totaldays;
		var vat = 0;
		var brutto = 0;
		
		var country = this.country.getValue();		
		
		var vatRate = window.priceList.vat[country]		
		$$('#order_form dd.german, #order_form dt.german').setStyle('display', (vatRate? 'block' : 'none'));		
		if (vatRate) {
			vat = vatRate * netto;
			brutto  = netto + vat;
			
			this.vat.innerHTML = vat.format(2,',','.');
			this.brutto.innerHTML = brutto.format(2,',','.');
		}
		
		
		this.dailyrate.innerHTML = dailyrate.format(2,',','.');
		this.days.innerHTML = days;
		this.persons2.innerHTML = persons;		
		this.totaldays.innerHTML = totaldays;		
		this.netto.innerHTML = netto.format(2,',','.');
	}
		
	this.calcPricePerDay = function(days,persons) {
		var calcPersons = persons < window.priceList.person.length ? persons : window.priceList.person.length - 1;
		var calcDays= days < window.priceList.day.length ? days : window.priceList.day.length - 1;
		//console.log(window.priceList.person[calcPersons]);
		//console.log(window.priceList.day[calcDays]);
		var pricePerDay = 
			1 * window.priceList.person[calcPersons] + 
			1 * window.priceList.day[calcDays]
		;
		return pricePerDay;
	}
		
		
	this.checkDateAgainstStart = function(date) {
		var date1 = this.getDate('order_form_tx_salesdb_bills.activity_begin');
		return date.getTime() < date1.getTime();		
	}
		
	this.checkDateAgainstEnd = function(date) {
		var date2 = this.getDate('order_form_tx_salesdb_bills.activity_end');
		return date.getTime() > date2.getTime();		
	}
		
		
	this.getDate = function(el) {
		var el = $(el);
		if (el && el.value) {
			var dateparts = el.value.split('.');
			return new Date(dateparts[2],dateparts[1]-1,dateparts[0]);
		}
	}
	
	
	this.init();
}


function dateStatusFunc(el,date){
	if (window.updater) {
		if (el == 'tx_salesdb_bills.activity_end')
			return window.updater.checkDateAgainstStart(date);
		else if (el == 'tx_salesdb_bills.activity_begin')
			return window.updater.checkDateAgainstEnd(date);
	}
}

function updateDays(cal) {
	if (window.updater) {
		window.updater.recalculate();
	}
}