/**
 * Catharina Bloom
 * (c) 2006-2009 Omines - www.omines.com
 * 
 * All rights explicitly reserved - unauthorized reproduction strictly prohibited
 */

// Consultant class - main active content wrapper
var Consultant = {
	start: function()
	{
		Menu.initialize();
		
		if ($('plan-event'))
			Eventplanner.initialize();
		
		TabList.initialize();
		Tasklist.initialize();
		
		this.tooltips	= new Tips('.tooltip', {className:'mootooltip'});
	}
};

var Eventplanner = {
	initialize: function()
	{
		$$('div.addEvent img').each(function (r) {
			r.addEvent('click', function () {
				this.start(r.get('alt'));
				return false;
			}.bind(this));
		}.bind(this));
	},
	start: function(date)
	{
		this.planDate	= date;
		
		$('plan-event-type-party').set('checked', false);
		$('plan-event-type-task').set('checked', false);

		$('plan-event-type-party').addEvent('click', function() { this.plan(0, $('plan-event-type-party').get('checked'), $('plan-party')); }.bind(this));
		$('plan-event-type-task').addEvent('click', function() { this.plan(1, $('plan-event-type-task').get('checked'), $('plan-task')); }.bind(this));
		$('plan-event').setStyle('display', 'block');
	},
	plan: function(type, activate, targetForm)
	{
		switch (type)
		{
			case 0:	// Party
			case 1:	// Task
				break;
			// Error
			default:
				alert('Error: Invalid state reached in Eventplanner::plan()');
				return false;
				break;
		}
		if (!activate)
		{
			this.resetForm(activate);
			return;
		}
		targetForm.setStyle('display', 'block');
		switch (type)
		{
			// Party
			case 0:
				$('party-autoreminder-notice').setStyle('display', 'block');
				$('party-date-date').set('value', this.parseDate(this.planDate, 0));
				MagicTime.start($('party-date-time'));
				break;
			// Task
			case 1:
				$('task-date-date').set('value', this.parseDate(this.planDate, 0));
				MagicTime.start($('task-date-time'));
				break;
		}
		/*
		$('event-reminder-invitation').set('value', this.parseDate(img.get('alt'), -14));
		$('eventdate-time').focus();
		*/
	},
	resetForm: function(targetForm)
	{
		targetForm.setStyle('display', 'none');
		targetForm.getChildren('input[type=text]').each(function (r) { Log(r); r.set('value', ''); Log(r); });
		targetForm.getChildren('input[type=checkbox]').each(function (r) { r.set('checked', false); Log(r); });
	},
	parseDate: function(dateString, offset)
	{
		var pattern	= new RegExp("([0-9]{4})\-([0-9]{2})\-([0-9]{2})");
		var val		= pattern.exec(dateString);
		var eDate	= new Date();
		
		eDate.setFullYear(val[1], val[2]-1, val[3]);
		eDate.setDate(eDate.getDate() + offset);
		
		return eDate.getFullYear()+'-'+padNumber(eDate.getMonth()+1)+'-'+padNumber(eDate.getDate());
	},
	completeTask: function(element, type, id)
	{
		this.request	= new Request.JSON({
			url:		localeUrl+'/ajax/agenda/complete-task',
			method:		'post',
			link:		'cancel',
			onFailure:	function(errorCode) {
				alert('Fatal error:\n\nErrorcode '+errorCode+' was encountered. Our apologies.');
			},
			onComplete:	function(resultJson) {
				if (resultJson.success == 1)
				{
					$('mark-'+element).removeEvents();
					$('mark-'+element).destroy();
					$(element).removeClass('not-completed');
					$(element).addClass('completed');
				}
			}.bind(this)
		}).send('element='+element+'&type='+type+'&id='+id);
	}
};

var AjaxSelector = new Class ({
	Implements: [Options, Events],
	options: {
		type: '',
		selector: '',
		targetValue: '',
		targetDisplay: '',
		resultList: '',
		messageBox: '',
		searchForm: ''	
	},
	initialize: function(obj, options)
	{
		this.setOptions(options);
		
		this.type			= this.options.type;
		this.selector		= $(this.options.selector);
		this.targetValue	= $(this.options.targetValue);
		this.targetDisplay	= $(this.options.targetDisplay);
		
		switch (this.type)
		{
			case 'address':		this.requestUrl	= localeUrl+'ajax/address-selector'; break;
			case 'customer':	this.requestUrl	= localeUrl+'ajax/customer-selector'; break;
			case 'party':		this.requestUrl	= localeUrl+'ajax/party-selector'; break;
			case 'product':		this.requestUrl	= localeUrl+'ajax/product-selector'; break;
			default:			alert('Invalid Selector Type called'); break;
		}

		this.targetDisplay.set('readonly', true);
		this.targetDisplay.addEvent('focus', function() { this.show(); }.bind(this));
		this.selector.addEvent('click', function() { this.show(); }.bind(this));
	},
	setup: function()
	{
		var closeBox	= this.close.bind(this);
		if (!this.overlay && $('selector-overlay'))
		{
			this.overlay	= $('selector-overlay').addEvent('click', closeBox);
			this.box		= $('selector-box');
			this.closeBtn	= $('selector-close').addEvent('click', closeBox);
			this.content	= $('selector-content');
		}
		else if (!$('selector-overlay'))
		{
			this.overlay	= new Element('div',{'id':'selector-overlay'}).inject(document.body, 'bottom').addEvent('click', closeBox);
			this.box		= new Element('div',{'id':'selector-box'}).inject(document.body, 'bottom');
			this.closeBtn	= new Element('a',	{'id':'selector-close'}).inject(this.box, 'top').addEvent('click', closeBox);
			this.content	= new Element('div',{'id':'selector-content'}).inject(this.box);
		}
		this.fx			= {
			overlay:	this.overlay.effect('opacity',	{duration: 500, wait: false}),
			box:		this.box.effect('opacity',		{duration: 500, wait: false})
		};
	},
	position: function()
	{
		this.overlay.setStyles({top:0,left:0,width:window.getScrollWidth(),height:window.getScrollHeight()});
		this.box.setStyles({display: '',top:(window.getScrollTop()+32)+'px'});
	},
	close: function()
	{
		this.fx.overlay.start(0);
		this.fx.box.start(0);
	},
	show: function()
	{
		this.setup();
		this.position();
		this.content.empty();
		this.loader		= new Element('p',	{'id':'selector-loader', 'text':' Loading....'}).injectInside(this.content);
		this.loaderImg	= new Element('img',{'id':'selector-loaderimg', 'src':'/static/images/ajax-loader.gif', 'alt':'loading'}).inject(this.loader, 'top');
		var requestUrl	= this.requestUrl;
 		this.fx.overlay.start(0, 0.5);
		this.fx.box.start(1);
		new Request.HTML({
			url : requestUrl,
			method :'get',
			evalScripts:true,
			update :$('selector-content'),
			onSuccess: function(resultJson, resultText)
			{
				this.resultList		= $(this.options.resultList);
				this.messageBox		= $(this.options.messageBox);
				this.searchForm		= $(this.options.searchForm);
				if (this.searchForm)
					this.searchForm.addEvent('submit', function () {this.search();return false; }.bind(this));
			}.bind(this)
		}).send();
	},
	search: function()
	{
		this.messageBox.set('html', '<img src="/static/images/ajax-loader.gif" alt="loading" /> Searching...');
		this.messageBox.removeClass('error');
		var requestUrl	= this.requestUrl;
		this.request	= new Request.JSON({
			url:		requestUrl,
			method:		'post',
			data:		this.searchForm,
			evalScripts:true,
			link:		'cancel',
			onFailure:	function(errorCode) {
				this.messageBox.set('html', '<strong>Fatal error:</strong><br />Errorcode '+errorCode+' was encountered. Our apologies.');
			}.bind(this),
			onSuccess:	function(resultJson, resultText) {
				if (resultJson.errorCode == 0)
				{
					$('box-search-results').setStyle('display', 'block');
					this.fillResult(resultJson.resultSet);
					if (resultJson.dataAdded == 1)
						$('fieldset-add-button').destroy();
					else
						$('fieldset-add-button').setStyle('display', '');
				}
				else
				{
					this.messageBox.addClass('error');
					this.messageBox.set('text', resultJson.errorMessage);
				}
			}.bind(this)
		}).send();
	},
	fillResult: function(resultset)
	{
		this.resultList.getChildren('li').each(function (li) {
			if (!li.hasClass('prefilled') && li.get('id') != 'search-results-empty')
				li.destroy();
		});
		
		this.messageBox.set('text', resultset.length+' results found.');
		$('search-results-empty').setStyle('display', 'none');
		if (resultset.length == 0)
		{
			$('search-results-empty').setStyle('display', '');
			return;
		}
		
		resultset.each(function (r) {
			var li		= new Element('li', {
				'id':	'result-'+r.Id,
				'text':	r.Label
			});
			var box		= new Element('span', {'class': 'result-selector'});
			var img		= new Element('img', {'src': '/static/images/icons/attach.png', 'alt': 'attach', 'title': 'Attach'}).inject(box);
			li.inject(this.resultList);
			box.inject(li, 'top');
			box.addEvent('click', function() {
				this.targetValue.set('value', r.Id);
				this.targetDisplay.set('value',	r.Label);
				this.close();
				this.targetValue.fireEvent('change');
			}.bind(this));
		}.bind(this));
	},
	type: '',
	selector: '',
	targetValue: '',
	targetDisplay: '',
	resultList: '',
	messageBox: '',
	searchForm: ''
});

var ProductSelector	= new Class({
	Implements: [Options, Events],
	options: {
		selector: '',
		resultList: '',
		messageBox: '',
		searchForm: ''
	},
	initialize: function(obj, options)
	{
		this.setOptions(options);
		this.selector		= $(this.options.selector);
		this.requestUrl		= localeUrl+'ajax/product-selector';
		this.selector.addEvent('click', function() { this.show(); }.bind(this));
		
		this.options.resultList		= 'search-results';
		this.options.messageBox		= 'search-message';
		this.options.searchForm		= 'frm-product-select';
	},
	setup: function()
	{
		var closeBox	= this.close.bind(this);
		if (!this.overlay && $('selector-overlay'))
		{
			this.overlay	= $('selector-overlay').addEvent('click', closeBox);
			this.box		= $('selector-box');
			this.closeBtn	= $('selector-close').addEvent('click', closeBox);
			this.content	= $('selector-content');
		}
		else if (!$('selector-overlay'))
		{
			this.overlay	= new Element('div',{'id':'selector-overlay'}).inject(document.body, 'bottom').addEvent('click', closeBox);
			this.box		= new Element('div',{'id':'selector-box'}).inject(document.body, 'bottom');
			this.closeBtn	= new Element('a',	{'id':'selector-close'}).inject(this.box, 'top').addEvent('click', closeBox);
			this.content	= new Element('div',{'id':'selector-content'}).inject(this.box);
		}
		this.fx			= {
			overlay:	this.overlay.effect('opacity',	{duration: 500, wait: false}),
			box:		this.box.effect('opacity',		{duration: 500, wait: false})
		};
	},
	position: function()
	{
		this.overlay.setStyles({top:0,left:0,width:window.getScrollWidth(),height:window.getScrollHeight()});
		this.box.setStyles({display: '',top:(window.getScrollTop()+32)+'px'});
	},
	close: function()
	{
		this.fx.overlay.start(0);
		this.fx.box.start(0);
	},
	show: function()
	{
		this.setup();
		this.position();
		this.content.empty();
		this.loader		= new Element('p',	{'id':'selector-loader', 'text':' Loading....'}).injectInside(this.content);
		this.loaderImg	= new Element('img',{'id':'selector-loaderimg', 'src':'/static/images/ajax-loader.gif', 'alt':'loading'}).inject(this.loader, 'top');
		
		var requestUrl	= this.requestUrl;
		new Request.HTML( {
			url : requestUrl,
			method :'get',
			evalScripts:true,
			update :$('selector-content'),
			onComplete: function()
			{
				this.resultList		= $(this.options.resultList);
				this.messageBox		= $(this.options.messageBox);
				this.searchForm		= $(this.options.searchForm);
				
				this.searchForm.addEvent('submit', function () {this.search();return false; } .bind(this));
			}.bind(this)
		} ).send();
 		this.fx.overlay.start(0, 0.5);
		this.fx.box.start(1);
	},
	search: function()
	{
		this.messageBox.set('html', '<img src="/static/images/ajax-loader.gif" alt="loading" /> Searching...');
		var requestUrl	= this.requestUrl;
		this.request	= new Request.JSON({
			url:		requestUrl,
			method:		'post',
			data:		$('frm-product-select'), 
			evalScripts:true,
			link:		'cancel',
			onFailure:	function(errorCode) {
				this.messageBox.set('html', '<strong>Fatal error:</strong><br />Errorcode '+errorCode+' was encountered. Our apologies.');
			}.bind(this),
			onComplete:	function(resultJson, resultText) {
				if (resultJson.errorCode == 0)
				{
					$('box-search-results').setStyle('display', 'block');
					this.fillResult(resultJson.resultSet);
				}
				else
					this.messageBox.set('html', '<strong>Error:</strong><br />'+resultJson.errorMessage);
			}.bind(this)
		}).send();
	},
	fillResult: function(resultSet)
	{
		this.rawId	= this.selector.get('id').replace('find-','');
		this.resultList.getChildren('li').each(function (li) {
			if (!li.hasClass('prefilled') && li.get('id') != 'search-results-empty')
				li.destroy();
		});
		
		this.messageBox.set('text', resultSet.length+' results found.');
		$('search-results-empty').setStyle('display', 'none');
		if (resultSet.length == 0)
		{
			$('search-results-empty').setStyle('display', '');
			return;
		}
		
		resultSet.each(function (r) {
			var li		= new Element('li', {
				'id':	'result-'+r.Id,
				'html':	'<span class="productcode">'+r.ProductCode+'</span><span class="productprice">&euro; '+r.RetailPrice+'</span><span class="productname">'+r.ProductName+'</span>'
			});
			var box		= new Element('span', {'class': 'result-selector'});
			var img		= new Element('img', {'src': '/static/images/icons/attach.png', 'alt': 'attach', 'title': 'Attach'}).inject(box);
			li.inject(this.resultList);
			box.inject(li, 'top');
			box.addEvent('click', function() {
				var rawId	= this.selector.get('id').replace('find-','');
				$('productvariationid-'+rawId).set('value', r.Id);
				$('productcode-'+rawId).set('value', r.ProductCode);
				$('productname-'+rawId).set('value', r.ProductName);
				$('amount-'+rawId).set('value', 1);
				$('costprice-'+rawId).set('value', r.CostPrice);
				$('vatpercentage-'+rawId).set('value', r.VatPercentage+' %');
				$('retailprice-'+rawId).set('value', r.RetailPrice);
				$('totalprice-'+rawId).set('value', r.RetailPrice);
				this.close();
				recalcOrderValue();
			}.bind(this));
		}.bind(this));
	},
	selector: '',
	resultList: '',
	messageBox: '',
	searchForm: ''
});

var Menu = {
	initialize: function()
	{
		this.mainOffset		= $('container').offsetLeft;
		this.activeMenu		= false;
		this.menuHoverTimer	= false;
		this.menuOutTimer	= false;
		
		$$('ul#mainmenu li').each(function (r) {
			r.addEvent('mouseover', function() { this.onMouseOver(r.get('id')); }.bind(this));
			
			if ($('sub'+r.get('id')) && r.hasClass('submenu'))
			{
				var listItem		= r.getSize();
				var submenu			= $('sub'+r.get('id')).getSize();
				var itemCenter		= r.getPosition($('mainmenu'));
				itemCenter			= itemCenter['x'] + Math.floor(listItem['x'] / 2);
				var childLeft		= itemCenter - Math.floor(submenu['x'] / 2);
				var childRight		= itemCenter + Math.floor(submenu['x'] / 2);
				var container		= $('submenus').getSize();
				
				if (childLeft < 15)
					$('sub'+r.get('id')).setStyle('left', 15);
				else if (childRight > container['x'])
					$('sub'+r.get('id')).setStyle('right', 15);
				else
					$('sub'+r.get('id')).setStyle('left', childLeft);
				
				r.addEvents({
					click:		function() { this.onClick(r.get('id')); }.bind(this),
					mouseover:	function() { this.onMouseOver(r.get('id')); }.bind(this)
				});
				$('sub'+r.get('id')).toggleClass('hidden');
			}
			
			r.addEvents({
				mouseout:	function() { this.onMouseOut(r.get('id')); }.bind(this),
				mouseover:	function() { this.cancelMouseOut(r.get('id')); }.bind(this)
			});
		}.bind(this));
	},
	onClick: function(id)
	{
		clearTimeout(this.menuHoverTimer);
		this.activeMenu	= id;
		this.doMouseOver(id);
	},
	onMouseOver: function(id)
	{
		clearTimeout(this.menuHoverTimer);
		this.menuHoverTimer	= setTimeout(this.onClick.bind(this, id), 325);
	},
	doMouseOver: function(id)
	{
		$$('ul#mainmenu li').each(function (r) {
			r.removeClass('active');
			if ($('sub'+r.get('id')))
				$('sub'+r.get('id')).removeClass('active');
		});
		
		$(id).toggleClass('active');
		if ($('sub'+id))
			$('sub'+id).toggleClass('active');
	},
	onMouseOut: function(id)
	{
		clearTimeout(this.menuOutTimer);
		clearTimeout(this.menuHoverTimer);
	},
	cancelMouseOut: function()
	{
		clearTimeout(this.menuOutTimer);
	},
	activateMenu: function(menuId, subMenuId)
	{
		if ($(menuId))
		{
			$(menuId).toggleClass('active');
			if ($('sub'+menuId))
			{
				$('sub'+menuId).toggleClass('active');
				if ($('sub'+menuId+'-'+subMenuId))
					$('sub'+menuId+'-'+subMenuId).toggleClass('active');
			}
		}
		else
		{
			$('menu-dashboard').toggleClass('active');
		}
	}
};

var TabList	= {
	initialize: function()
	{
		this.tabs		= $$('ul.tablist li');
		if (this.tabs.length < 1)
			return;
		this.tablist	= new Array(this.tabs.length);
		if (this.tabs[0].getParent().getParent().hasClass('tablist-noselect'))
			return;
		this.tabs.each(function (r, idx) {
			r.addEvent('click', function() {
				this.toggleTab(r);
			}.bind(this));
		}.bind(this));
		var current	= document.location.hash.substring(1);
		if (current != '' && $('tab-'+current))
			this.toggleTab('tab-'+current);
		else
			this.toggleTab(this.tabs[0].get('id'));
	},
	toggleTab: function(id)
	{
		tab	= $(id);
		pan	= $(tab.get('id').replace('tab-', 'panel-'));
		this.tabs.each(function (r) {
			r.removeClass('active');
			if ($(r.get('id').replace('tab-', 'panel-')))
				$(r.get('id').replace('tab-', 'panel-')).removeClass('activetab');
		});
		tab.toggleClass('active');
		if (pan)
			pan.toggleClass('activetab');
	}
};

var MagicTime = {
	start: function(element, showSeconds)
	{
		if (typeof element == 'string')
			element		= $(element);
		if (typeof showSeconds == 'undefined')
			showSeconds	= false;
		this.element		= element;
		this.showSeconds	= showSeconds;
		
		this.element.addEvent('change', function(e) {
			e.stopPropagation();
			this.parse();
			return false;
		}.bind(this));
	},
	patterns: [
		{	// Now
			pattern: /^now/i,
			handler: function() {
				return new Date();
			}
		},
		{	// HH:MM:SS
			pattern: /(\d{1,2})([\:\.\,\-])(\d{1,2})([\:\.\,\-])(\d{1,2})/,
			handler: function(parts) {
				var d = new Date();
				d.setHours(parseInt(parts[1], 10));
				d.setMinutes(parseInt(parts[3], 10));
				d.setSeconds(parseInt(parts[5], 10));
				return d;
			}
		},
		{	// HH:MM
			pattern: /(\d{1,2})([\:\.\,\-])(\d{1,2})/,
			handler: function(parts) {
				var d = new Date();
				d.setHours(parseInt(parts[1], 10));
				d.setMinutes(parseInt(parts[3], 10));
				d.setSeconds(0);
				return d;
			}
		},
		{	// HH
			pattern: /(\d{1,2})([\:\.\,\-]{0,1})/,
			handler: function(parts) {
				var d = new Date();
				d.setHours(parseInt(parts[1], 10));
				d.setMinutes(0);
				d.setSeconds(0);
				return d;
			}
		},
		{	// HHMM
			pattern: /(\d{1,4})/,
			handler: function(parts) {
				var d = new Date();
				var h = parts[1].substring(0,2).toInt();
				var m = parts[1].substring(2,4).toInt();
				d.setHours(parseInt(h, 10));
				d.setMinutes(parseInt(m, 10));
				d.setSeconds(0);
				return d;
			}
		},
		{	// HHMMSS
			pattern: /(\d{1,6})/,
			handler: function(parts) {
				var d = new Date();
				var s = parts[1].substring(4,6).toInt();
				if (isNaN(m))	{ m = '0'; }
				if (isNaN(s))	{ s = '0'; }
				d.setHours(parseInt(h, 10));
				d.setMinutes(parseInt(m, 10));
				d.setSeconds(parseInt(s, 10));
				return d;
			}
		}
	],
	getReadable: function(date)
	{
		if (this.showSeconds)
			return this.padZero(date.getHours())+':'+this.padZero(date.getMinutes())+':'+this.padZero(date.getSeconds());
		else
			return this.padZero(date.getHours())+':'+this.padZero(date.getMinutes());
	},
	padZero: function(s)
	{
		s	= s.toString();
		if (s.length == 1)
			return '0' + s;
		else
			return s;
	},
	parse: function()
	{
		try
		{
			var d	= this.parseTimeString();
			var v	= this.getReadable(d);
			if (this.showSeconds)
				var p	= /(\d{1,2}):(\d{1,2}):(\d{1,2})/;
			else
				var p	= /(\d{1,2}):(\d{1,2})/;
			if (!p.exec(v))
			    throw new Error("Invalid time string");
			this.element.set('value', v);
		}
		catch (e)
		{
			this.element.addClass('timecontrol-error');
			this.element.set('value', 'invalid value');
			this.element.addEvent('focus', function() { this.element.removeClass('timecontrol-error'); this.element.set('value', ''); }.bind(this));
			return false;
		}
	},
	parseTimeString: function () {
	    for (var i = 0; i < this.patterns.length; i++) {
	        var pattern	= this.patterns[i].pattern;
	        var handler	= this.patterns[i].handler;
	        var parts	= pattern.exec(this.element.get('value'));
	        if (parts)
	        	return handler(parts);
	    }
	    throw new Error("Invalid time string");
	}

};

var Tasklist = {
	initialize: function()
	{
		$$('.tasklist li div.task-more').each(function(tm){
			if (!tm.getParent().getFirst('div.task-top').getFirst('span.more-info'))
				return;
			var tb	= tm.getParent().getFirst('div.task-top').getFirst('span.more-info');
			var tl	= tb.getFirst('a');
			tl.set('title', tl.get('text'));
			tl.set('text', '');
			tm.getParent().setStyle('list-style-type', 'none');
			tb.setStyle('top', '0');
			new Element('img', {'src':'/static/images/icons/zoom-in.png', 'alt':tl.get('text')}).inject(tl);
			tb.toggleClass('toggle-off');
			tm.toggleClass('hide-more');
			
			tb.addEvent('click', function() { this.toggleInfo(tm, tb);return false; }.bind(this));
		}.bind(this));
	},
	toggleInfo: function(tm, tb)
	{
		if (tm.hasClass('hide-more'))
			tb.getFirst('a').getFirst('img').set('src', '/static/images/icons/zoom-out.png');
		else
			tb.getFirst('a').getFirst('img').set('src', '/static/images/icons/zoom-in.png');
		tm.toggleClass('hide-more');
	}
};

function padNumber(i)
{
	if (i < 10)
		return '0'+i;
	return i;
}
function formatNumber(num, isNeg)
{
	if (isNeg !== true)
		isNeg	= false;
	if (num < 0)
	{
		num 	= num * -1;
		isNeg	= true;
	}
	if (num == 0)
		return '0.00';
	var iNum	= num.toInt();
	if (num == iNum)
		return (isNeg ? '-' : '')+iNum+'.00';
	var iRem	= ((num - iNum) * 100).round();
	if (!iRem)
		iRem	= '00';
	if (iRem < 10)
		iRem	= '0'.iRem;

	return (isNeg ? '-' : '')+iNum+'.'+iRem;
}
function recalcOrderValue()
{
	var priceCost	= 0;
	var priceVat	= 0;
	var priceRetail	= 0;
	var table	= $$('#orderform .list tr');
	table.each(function (tr) {
		if (!tr.hasClass('headers'))
		{
			var baseId	= tr.get('id').replace('row-','');
			if ($('amount-'+baseId).get('value') != 0)
			{
				var amt			= $('amount-'+baseId).get('value');
				var valRetail	= Math.round($('retailprice-'+baseId).get('value') * 100);
				var totalRetail	= (amt * valRetail) / 100;
				$('totalprice-'+baseId).set('value', formatNumber(totalRetail, (amt < 0)));
				priceRetail	+= totalRetail;
			}
		}
	});
	if ($('additional-shippingcost'))
	{
		var shippingCost	= $('additional-shippingcost').get('value').replace(',', '.');
		priceRetail	+= Math.round(shippingCost * 100) / 100;
	}
	$('order-totalprice').set('text', formatNumber(priceRetail));
}

function magicTime(element, showSeconds)
{
}


// Set event to start running active content when DOM is loaded
window.addEvent('domready', function() { Consultant.start(); });

