/**
 * @author uruk
 */

window.addEvent('domready', function() {
	var g = $get();
	if(g.id) {
		loadShop({
			'step': 'show-product-details',
			'pid': g.id
		});
		addToCart();
	} else {
		loadShop({
			'step': 'show-categories'
		});
	}
	
});


function checkErsteLieferung() {
	$('modusresponse').setStyle('color', '#999');
	$('modusresponse').empty();
	var d = $('erstelieferung').get('value');
	var r = '^([0-3])([0-9])(\.)(0|1)([0-9])(\.)(20)([1-9])([0-9])$';
	if(d.test(r))
		return true;
	
	$('modusresponse').setStyle('color', '#f00');
	$('modusresponse').set('text', 'Ungültiges Datum der ersten Lieferung.');
	
	return false;
}


function addAufhebenLinks() {
	$$('.aufheben').each(function(el) {
		el.addEvent('click', function() {
			addToCart({ 'modus': 'normal' });
			if(parseInt(this.get('rel')) > 0)
			{
				loadShop({
					'step': 'show-product-details',
					'pid': parseInt(this.get('rel'))
				});
			} else {
				loadShop({
					'step': 'show-categories'
				});
			}
				
		});
	});
}

function activateInterval() {
	if($('anzahllieferungen').get('value') == 1)
		$('intervall').disabled = true;
	else
		$('intervall').disabled = false;
	$('modusresponse').empty();
}

function checkAjaxForm(f) {
	$$('.eingabefehler').each(function(el) {
		el.dispose();
	});
	$$('input, textarea, select').each(function(inp) {
		if(inp.get('rel'))
		{
			if(!inp.get('value').test(inp.get('rel')))
			{
				var sp = new Element('span', {
					'class': 'eingabefehler', 
					'text': inp.get('title')
				});
				sp.inject(inp.getParent('p'));
				
			}
		}
	});
	if($$('.eingabefehler').length == 0) {
		var req = new Request.HTML({
			'url': f.get('action'), 
			'method': 'post', 
			'update': $('shop'), 
			'data': f.toJSON()
		}).send();
	}
	return false;
}


function loadShop(args) {
	var url = 'sub/onlineshop/xml/'+args.step+'.php';
	
	var req = new Request.HTML({
		'url': url, 
		'method': 'post', 
		'data': args,
		'update': $('shop')
	});
	req.send();
}

function addToCart(args) {
	var req = new Request.HTML({
		'url': 'sub/onlineshop/xml/cart.php', 
		'data': args, 
		'method': 'post', 
		'update': $('warenkorb')
	}).send();
}

function addProductLinks() {
	$$('.prodlink').each(function(el) {
		el.addEvent('click', function() {
			if($chk($('zurkasse')))
				$('zurkasse').setStyle('display', 'inline');
			loadShop({
				'pid': parseInt(this.get('rel')),
				'step': 'show-product-details', 
				'cbk': this.get('id') ? this.get('id') : '', 
				'bon': this.get('id') ? this.get('id') : ''
			});
			addToCart();
		});
	});	
}

function addRemoveLinks(isSummary) {
	if(isSummary == undefined)
		isSummary = false;
	$$('.remove').each(function(el) {
		el.addEvent('click', function() {
			if(isSummary) {
				if(this.get('rel').contains('cbk-')) {
					loadShop({
						'step': 'summary', 
						'cbk': this.get('rel').substr(4), 
						'menge': 0
					});					
				}
				else if(this.get('rel').contains('bon-')) {
					loadShop({
						'step': 'summary', 
						'bon': this.get('rel').substr(4), 
						'menge': 0
					});					
				} 
				else {
					loadShop({
						'step': 'summary', 
						'id': parseInt(this.get('rel')), 
						'menge': 0
					});
				}
			} else {
				if(this.get('rel').contains('cbk-')) {
					addToCart({
						'cbk': this.get('rel').substr(4), 
						'menge': 0
					});
				} 
				else if(this.get('rel').contains('bon-')) {
					addToCart({
						'bon': this.get('rel').substr(4), 
						'menge': 0
					});
				} 
				else {
					addToCart({
						'id': parseInt(this.get('rel')),
						'menge': 0
					});
				}
			}
		});
	});
	$$('.plus').each(function(el) {
		el.addEvent('click', function() {
			if(isSummary) {
				if(this.get('rel').contains('cbk-')) {
					loadShop({
						'step': 'summary', 
						'cbk': this.get('rel').substr(4), 
						'menge': 1
					});					
				}
				else if(this.get('rel').contains('bon-')) {
					loadShop({
						'step': 'summary', 
						'bon': this.get('rel').substr(4), 
						'menge': 1
					});					
				}
				else {
					loadShop({
						'step': 'summary', 
						'id': parseInt(this.get('rel')), 
						'menge': 1
					});
				}
			} else {
				if(this.get('rel').contains('cbk-')) {
					addToCart({
						'cbk': this.get('rel').substr(4), 
						'menge': 1
					});
				} 
				else if(this.get('rel').contains('bon-')) {
					addToCart({
						'bon': this.get('rel').substr(4), 
						'menge': 1
					});
				}
				else {
					addToCart({
						'id': parseInt(this.get('rel')),
						'menge': 1
					});
				}
			}
		});
	});
	$$('.minus').each(function(el) {
		el.addEvent('click', function() {
			if(isSummary) {
				if(this.get('rel').contains('cbk-')) {
					loadShop({
						'step': 'summary', 
						'cbk': this.get('rel').substr(4), 
						'menge': -1
					});					
				} 
				else if(this.get('rel').contains('bon-')) {
					loadShop({
						'step': 'summary', 
						'bon': this.get('rel').substr(4), 
						'menge': -1
					});					
				} 
				else {
					loadShop({
						'step': 'summary', 
						'id': parseInt(this.get('rel')), 
						'menge': 0
					});
				}
			} else {
				if(this.get('rel').contains('cbk-')) {
					addToCart({
						'cbk': this.get('rel').substr(4), 
						'menge': -1
					});
				} 
				else if(this.get('rel').contains('bon-')) {
					addToCart({
						'bon': this.get('rel').substr(4), 
						'menge': -1
					});					
				}
				else {
					addToCart({
						'id': parseInt(this.get('rel')),
						'menge': -1
					});
				}
			}
		});
	});
}