			function nil() { return ; }

			function show_ajax_errors(req) { // {{{
				// wiwo - debug: show response text
				//alert(req.responseText);
				
				if (req.responseXML) {
					xml = req.responseXML;

					/* ---------------------------------------------
					 *  Build the error messages
					 * --------------------------------------------- */

					err_num = 0;
					// check for erros
					items = xml.getElementsByTagName("Errors");
					//alert("Items " + items.length);
					err_str = "";
					for (var i = 0; i < items.length; i++) {
						// get first "Error" tag in "Errors"
						err = items[i].firstChild;
						while(err) {
							if (err.tagName == "Error") {
								value = err.firstChild.nodeValue;
								err_str = err_str + "<li>" + value + "</li>\n";
								err_num += 1;
							}
							err = err.nextSibling;
						}
					}
					if (err_num > 0) {
						show_error(err_str);
					}

					items = xml.getElementsByTagName("Scripts");
					for (var i = 0; i < items.length; i++) {
						script = items[i].firstChild;
						while(script) {
							if (script.tagName == "Script") {
								value = script.firstChild.nodeValue;
								eval(value);
							}
							script = script.nextSibling;
						}
					}

				}
			} // }}}

			function show_error(err_str) {
						err_div = $('error-div');
						// put the err_div somewhere the user can see it
						err_div.style.top = window.getScrollTop() + 'px';
						err_str = "<ul>" + err_str + "</ul>";
						//err_str += "</ul>";
						//alert(err_str);
						if (err_div) {
							var div = new Element('div');
							div.innerHTML = err_str;
							div.injectInside(err_div);	
							highlight(div,3000,true);
						} else {
							alert(err_str);
						}
			}



			/* UPDATE REGULAR AMOUNT */
			function init_regular(all,focus) { // {{{
				if (all) {
					var reg = $$('.regular-amount');
				} else {
					var reg = $$('.regular-amount-ajax');
				}

				reg.addEvents({
					'keypress': function(event) { amount_key_press(event,this); },
					'change': 	function(event) { save_regular_amount(this); }
				});
				if (focus && reg[reg.length-1]) { reg[reg.length-1].focus(); }

				if (all) {
					var active = $$('.active-checkbox');
				} else {
					var active = $$('.active-checkbox-ajax');
				}
				active.each( function(elem) {
					elem.addEvent('change', function() {
						//update_max_combiwin();	
						update_total_combi_bets_odd()
					});
				});

				// initialize the field for setting all regular-bet amounts at once
				if (all) {
					$('all-regular-amount').addEvents({
						'keypress': function(event) { amount_key_press(event,this); },
						'change': 	function(event) {
							$$('input.regular-amount').each(function(elem) {
								elem.value = $('all-regular-amount').value; 
								elem.fireEvent('change');
								//save_all_regular_amounts($('all-regular-amount').value);
							})
						}
					});
				}

			} // }}}

			function save_regular_amount(elem) { // {{{
				var id = elem.id.split('-')[1];
				var value = elem.value;
				value = parseNum(value);
				//alert(id);
				//var url = "/bets/ajax/save-regular-amount";
				var url = "/bets/ajax/save-amount";
				var ajax = new Request({
					url: url,
					method: 'get',
					data: Hash.toQueryString({ubid: id, amount: value}),
					onSuccess: function(request) { elem.value = value.numberFormat(2,',','.'); log('OK', elem); },
					onFailure: function(request) { save_regular_amount_failure(request); }
					//update: $('log')
				}).send();
			} // }}}
			function save_regular_amount_failure(req) { // {{{
				show_ajax_errors(req);
			} // }}}

			function add_bet(qid) { // {{{
				var url = "/bets/ajax/place-bet"
				var ajax = new Request({
					url: url,
					method: 'get',
					//update: $('log'),
					onSuccess: function(request) { add_bet_success(request,qid); },
					onFailure: function(request) { add_bet_failure(request); }
				}).send(Hash.toQueryString({quote_id: qid, rnd: new Date().getTime()}));
			} // }}}

			function ie_insert_tr(id, new_parent, req) {
					var table_id = 'tmp-'+ new Date().getTime();
					var req = '<table id="' + table_id + '" style="display:none"><tr id="' + id + '" class="user-bet-line">' + req + '</tr></table>';

					$('bet-bill-body').insertAdjacentHTML('beforeEnd',req);
					newrow = $(id);

					newrow.injectBefore(new_parent);
					$(table_id).dispose();
			}

			function add_bet_success(req,qid) { // {{{
				
				var single_footer = $('user-bet-list-tbl-footer'); // get the tbody
				var combo_footer = $('user-combo-list-tbl-footer'); // get the tbody
				var system_footer = $('user-system-list-tbl-footer'); // get the tbody
				var id = 'quote-' + qid;


				// if is ie
				if(Browser.Engine.trident) {

					//alert(single_footer);
					ie_insert_tr(id + "-single", single_footer,req);
					ie_insert_tr(id + "-combo", combo_footer,req);
					ie_insert_tr(id + "-system", system_footer,req);

				} else {
					var tr = new Element('tr', {'id': id + "-single", 'class': 'user-bet-line'});
					tr.injectBefore(single_footer);
					tr.innerHTML = req; //setHTML(req);
					var tr = new Element('tr', {'id': id + "-combo", 'class': 'user-bet-line'});
					tr.injectBefore(combo_footer);
					tr.innerHTML = req; //setHTML(req);
					var tr = new Element('tr', {'id': id + "-system", 'class': 'user-bet-line'});
					tr.injectBefore(system_footer);
					tr.innerHTML = req; //setHTML(req);
				}

				// update the max win of the combination bets
				update_total_combi_bets_odd()
				init_combi_cbx(false);
				init_system_cbx(false);
				// add events to the new items
				init_regular(false);
				
				// inform the quote-list, that the bet was successfully added
				quote_list_select_quote(qid,true);
				$('bill-games').setStyle('height','auto');
				
			} // }}}
			function add_bet_failure(req) { // {{{
				show_ajax_errors(req);
			} // }}}

			function add_combi_bet() { // {{{
				var ids = get_selected('user-combo-list-tbl');
				var url = "/bets/ajax/add-combi-bet";
				// Error handling is done by the server
				var ajax = new Request({
					url: url,
					method: 'get',
					data: Hash.toQueryString({quote_ids: ids, rnd: new  Date().getTime()}),
					//update: $('log'),
					onSuccess: function(request) {  add_combi_bet_success(request); },
					onFailure: function(request) {  add_combi_bet_failure(request); }
				}).send();
			} // }}}
			function add_combi_bet_success(req) { // {{{
				//log(req)	
				var div = new Element('div');
				/*
				if (!focus_on_init) {
					div.setStyle('border','3px solid red');
					div.setStyle('width','100%');
				}
				*/


				//div.setStyle('opacity',0.1);
				div.innerHTML = req;
				div.injectAfter($('add-combi-bet-tbl'));

				init_combi(false,true);
				/*
				var myEffect = new Fx.Morph(div);
				myEffect.start({
					'opacity': 1
				}).chain(
					function() {init_combi(false,true)}
				);
				*/

				$('combi-bets').setStyle('height','auto');
				// need this for firefox quirk
				$('combi-bets').setStyle('position','relative');
			} // }}}
			function add_combi_bet_failure(req) { // {{{
				show_ajax_errors(req);
			} // }}}
			function del_combi_bet(cbid,sbid) { // {{{
				url = "/bets/ajax/combi-bet-del"
				//alert(qid);
				var ajax = new Request({
					url: url,
					method: 'get',
					data: Hash.toQueryString({combination_bet_id: cbid, system_bet_id: sbid}),
					//update: $('log'),
					onSuccess: function(request) { del_combi_bet_success(request, cbid); }
				}).send();
			} // }}}
			function del_combi_bet_success(req, cbid) { // {{{
				//alert($('ubid-row-' + req).parentNode.remove);
				var cbet_div = $('combi-bet-' + cbid).getParent();
				//cbet_div.getParent().setStyle('height','auto');
				//cbet_div.getParent().setStyle('top','0px');
				var myEffect = new Fx.Morph(cbet_div);
				myEffect.start({
					'opacity': 0
				}).chain(
					function() { myEffect.start({ 'height': 0 }) }
				).chain(
					function() { cbet_div.dispose() }
				);
				////cbet_div.fade('out');
				/*
				cbet_div.chain(
					function() { this.fade('out') },
					function() { this.dispose() }
				)
				*/
				$('combi-bets').setStyle('height','auto');
			} // }}}

			function add_system_bet() { // {{{
				//var amount = parseNum($('system-amount').value);
				var ids = get_selected('user-system-list-tbl');

				var url = "/bets/ajax/add-combi-bet";
				// Error handling is done by the server
				var ajax = new Request({
					url: url,
					method: 'get',
					data: Hash.toQueryString({quote_ids: ids, system_p: 't', rnd: new  Date().getTime()}),
					//update: $('log'),
					onSuccess: function(request) { add_system_bet_success(request); },
					onFailure: function(request) { add_system_bet_failure(request); }
				}).send();
			} // }}}
			function add_system_bet_success(req) { // {{{
				//log(req)	
				var div = new Element('div');
				div.innerHTML = req;
				div.injectAfter($('add-system-bet-tbl'));
				init_system_bet_forms(false);
				//init_combi(false);
				init_system(false);
				$('system-bets').setStyle('height','auto');
			} // }}}
			function add_system_bet_failure(req) { // {{{
				show_ajax_errors(req);
			} // }}}

			function submit_system_bet_form(form) { // {{{
				//alert(form.action);
				var p = form.getParent();
				form.set('send',{
					onSuccess: function(req)  {
						p.innerHTML = req;
						init_system_bet_forms(false);
					}
					/*
					,
					onComplete: function(req) {
						//log.removeClass('ajax-loading');
						init_system_bet_forms(false);
					}
					*/
				});
				form.send();
			} // }}}
			function init_system_bet_forms(all) { // {{{
				if (all) {
					var forms = $$('form.system-bet-form');
				} else {
					var forms = $$('form.system-bet-form-ajax');
				}
				forms.each( function(elem) {
					elem.addEvent('submit', function(e) {
							new Event(e).stop();
							//var log = $('log').empty().addClass('ajax-loading');
							submit_system_bet_form(this);
					});
				});

				// delayed submit vor "bank" checkboxes and the system_bet_widget
				if (all) {
					var banks = $$('input.bank-checkbox','.system-bet-combos');
				} else {
					var banks = $$('input.bank-checkbox-ajax','.system-bet-combos-ajax');
				}
				//alert(banks);

				banks.addEvents({
					'change': function(event) {
						// td - tr - tbody - table - form
						var form = this.getParent('form'); //.getParent().getParent().getParent().getParent();
						var timer = Timers[form.id];

						if (timer) { $clear(timer) } 
						timer = (function() { submit_system_bet_form(form); }).delay(100);
						Timers[form.id] = timer;

					}
				});
				if (!all) {
					banks.each(function (elem) {
						if (elem.hasClass('bank-checkbox-ajax')) {
							elem.addClass('bank-checkbox');
							elem.removeClass('bank-checkbox-ajax');
						}
						if (elem.hasClass('system-bet-combos-ajax')) {
							elem.addClass('system-bet-combos');
							elem.removeClass('system-bet-combos-ajax');
						}
					});
				}


				init_system();

			} // }}}
			var Timers = new Array();
			/* SYSTEM AMOUNT */
			function init_system(all,focus) { // {{{
				// initialize the amount field of system bets
				if (all) {
					var reg = $$('.system-amount');
				} else {
					var reg = $$('.system-amount-ajax');
				}
				reg.addEvents({
					'keypress': function(event) { amount_key_press(event,this); },
					'change': 	function(event) { save_regular_amount(this); }
				});

				if (focus && reg[0]) { reg[0].focus(); }
			} // }}}

			function quote_list_deselect_all() { // {{{
					try {
					} catch(e) {}
						parent.window.bets.deselect_all();
			} // }}}
			function quote_list_select_quote(qid,selected) { // {{{
				try {
					parent.window.bets.select_quote(qid,selected);
				} catch(e) {}
			} // }}}

			function del_bet(ubid) { // {{{
				url = "/bets/ajax/user-bet-del"
				//alert(qid);
				var ajax = new Request({
					url: url,
					method: 'get',
					data: Hash.toQueryString({bet_user_bet_id: ubid}),
					//update: $('log'),
					onSuccess: function(request) { del_bet_success(request); }
				}).send();
			} // }}}
			function del_bet_success(req) { // {{{
				//alert($('ubid-row-' + req).parentNode.remove);
				//alert($('ubid-row-'+req));

				var quote_id; 
				var pp;
				$$('.ubid-row-' + req).each( function(elem) {
					var p = elem.getParent();
					pp = p.getParent();
					quote_id = p.id.split('-')[1];
					p.dispose();
				});

				quote_list_select_quote(quote_id,false);

				update_total_combi_bets_odd()
				update_combi_bets_odd(pp,req)
				if (pp) {
					pp.getParent('.type-body').setStyle('height','auto');
				}
			} // }}}

			function del_all() { // {{{
				var url = "/bets/ajax/del-all";
				//alert(qid);
				var ajax = new Request({
					url: url,
					method: 'get',
					//data: Object.toQueryString({bet_user_bet_id: ubid}),
					//update: $('log'),
					onSuccess: function(request) { del_all_success(request); }
				}).send();
			} // }}}
			function del_all_success() { // {{{
				$$('.user-bet-line').each( function(row) {
					var quote_id = row.id.split('-')[1];
					row.dispose();
					quote_list_select_quote(quote_id,false);
				})
				$$('table.combi-bet-view').each( function(tbl) { 
					tbl.dispose();
				});
			} // }}}

			/* END UPDATE REGULAR AMOUNT */

			/* COMBINATION AMOUNT */
			function num_of_checked_combi_elems() {
				// {{{
				cbx = $$('#user-combo-list-tbl input.active-checkbox')
				var count = 0;
				cbx.each(function(elem) {
					if (elem.checked) { 
						count ++; 
						if (count > 10) {
							elem.checked = false;
						}
					}
				})
				return count;
				// }}}
			}

			function init_combi_cbx(all) {
				// {{{
				var cbx;
				if (all) {
				 cbx = $$('#user-combo-list-tbl input.active-checkbox')
				} else {
				 cbx = $$('#user-combo-list-tbl input.active-checkbox-ajax')
				}

				cbx.each(function(elem) {
					elem.addEvent('click', function() {
						if (elem.checked && num_of_checked_combi_elems() > max_combos) {
							elem.checked = false;
							show_error("Sie koennen maximal " + max_combos + " Wetten kombinieren");
						}
					});
				});

				num_of_checked_combi_elems();
				// }}}
			}

			function num_of_checked_system_elems() {
				// {{{
				cbx = $$('#user-system-list-tbl input.active-checkbox')
				var count = 0;
				cbx.each(function(elem) {
					if (elem.checked) { 
						count ++; 
						if (count > 10) {
							elem.checked = false;
						}
					}
				})
				return count;
				// }}}
			}
			function init_system_cbx(all) {
				// {{{
				var cbx;
				if (all) {
				 cbx = $$('#user-system-list-tbl input.active-checkbox')
				} else {
				 cbx = $$('#user-system-list-tbl input.active-checkbox-ajax')
				}

				cbx.each(function(elem) {
					elem.addEvent('click', function() {
						if (elem.checked && num_of_checked_system_elems() > max_combos) {
							elem.checked = false;
							show_error("Sie koennen maximal " + max_combos + " Wetten kombinieren");
						}
					});
				});

				num_of_checked_system_elems();
				// }}}
			}

			function init_combi(all,focus) { 
				// {{{


				//alert($('combi-amount'));
				if (all) {
					var reg = $$('.combi-amount');
				} else {
					var reg = $$('.combi-amount-ajax');
				}
				reg.addEvents({
					'keypress': function(event) { amount_key_press(event,this); },
					'keyup': function(event) 		{ update_max_combiwin(this); },
					/*'change': function(event) 	{ save_regular_amount(this); },*/
					'blur': function(event) 	{ save_regular_amount(this); }
				});
				if (focus && reg[0]) { reg[0].focus(); }
				// }}}
			} 

			function update_total_combi_bets_odd() { 
				// {{{
				var combi_bets_odd = 1;

				// combi of checked bets
				$$('table#user-combo-list-tbl td.active input.check').each( function(elem) {
					if (elem.checked) {
						elem = elem.getParent();
						//alert(elem + " " + elem.tagName);

						while(elem) {
							elem = elem.getNext();
							if (elem.hasClass("odd")) {
								//alert("checked");
								q = parseNum(elem.innerHTML);
								combi_bets_odd *= q;
								break;
							}
						}
					}
				});
				// combi of all bets
				//$$('table#user-bet-list-tbl td.odd').each( function(elem) {
				//	q = parseNum(elem.innerHTML);
				//	combi_bets_odd *= q;
				//});
				if (combi_bets_odd > 1000) { combi_bets_odd = 1000; }
				$('combi-bets-odd').innerHTML = combi_bets_odd.numberFormat(2,',','.');
				// }}}
			} 

			function update_combi_bets_odd(p,ubid) { // {{{
				//var p = $('user-bet-list-tbl');
				//alert(p);
				if (p) {
					// get first child
					child = p.getFirst();
					var total_odd = 1;
					var cnt = -1;
					var old_idx = -1;
					var max = 1;
					var cubid = 0;
					while (child) {
						//alert(child.innerHTML);
						child2 = child.getFirst();
						//alert(child2.innerHTML);
						var idx = 0;
						while (child2) {
							
							if (child2.hasClass("one-combi-odd")) {
								// we found the odd
								odd = parseNum(child2.getFirst().innerHTML);
								idx = parseInt(child2.getFirst().getNext().innerHTML)-1;



								//alert(old_idx + " " + idx);
								if (old_idx != idx ) {
									//$('wiwo-log').innerHTML += ' ' + max;
									total_odd *= max;
									max = 1;
								}
								if (odd > max) max = odd;

								old_idx = idx;

								break;
							} else if (child2.hasClass("combi-quote")) {
								total_odd *= max;
								if (total_odd > 1000) { total_odd = 1000; }

								child2.getFirst().innerHTML = total_odd.numberFormat(2,',','.');
								// we need the last ubid for the update of the max win
								cubid = child2.getFirst().id.split('-')[2];

								break;
							}
							child2 = child2.getNext();
						}
						child = child.getNext();
					}

					// update max win
					update_max_combiwin($('ubid-'+cubid));
				}
			} // }}}

			function update_max_combiwin(elem) { // {{{
				//update_combi_bets_odd();
				if (elem) {
					var ubid = elem.id.split("-")[1];
					var amount = elem.value;
					amount = parseNum(amount);
					var odd = $('combi-quote-'+ubid).innerHTML;	
					odd = parseNum(odd);
					
					var maxwin = odd*amount;

					if (maxwin > max_win) { maxwin = max_win; }

					$('max-win-'+ubid).innerHTML = maxwin.numberFormat(2,',','.');
				}
			} // }}}

			/* END COMBINATION AMOUNT */

			function init_selected_quotes() { // {{{
				try {
					if(parent.window.bets.domready) {
							quote_list_deselect_all();
							$$('.user-bet-line').each( function(elem) {
									quote_list_select_quote(elem.id.split('-')[1],true);	
							});
					} else {
						setTimeout( 'init_selected_quotes()', 1000 );
					}
				} catch(e) {
						//setTimeout( 'init_selected_quotes()', 1000 );
				}
			} // }}}

			function log(txt,parentElem) { // {{{
				

				div = new Element('div',{ 'class': 'log' }); 
				/*
				if (parentElem) {
					var coords = parentElem.getCoordinates();
					alert(coords.left);
					div.style.left = coords.left;
					div.style.bottom = coords.bottom;
				}
				*/
				//$('body-content').adopt(div);
				div.injectBefore(parentElem);
				//$('log').innerHTML = txt;
				div.innerHTML = txt;
				highlight(div,300,true);
				//div.remove();
			} // }}}

			function highlight(elem, duration, remove) { // {{{
				if (!duration) {
					duration = 3000;
				}
				elem.setOpacity(0);
				//alert(id);
				elem.style.visibility = 'visible';
				var fx = new Fx.Morph( elem, {duration:400, wait:false});
				fx.start({
					opacity: [0,1]
				}).chain(function () {
					this.start.delay(duration, this, {
						'opacity': 0
					});
				}).chain(function() {
					if (remove) {
						elem.dispose();
					}
				});
			} // }}}

			// select/deselect all {{{
			function select_all() {
				//alert($$('input.check'));
				$$('table#user-combo-list-tbl td.active input.check').each( function(elem) {
					elem.checked = true;
				});
			}
			function deselect_all() {
				//alert($$('input.check'));
				$$('table#user-bet-list-tbl td.active input.check').each( function(elem) {
					elem.checked = false;
				});
			}
			function get_selected(table) {
				//alert($$('input.check'));
				var ids = "";
				$$('table#' + table + ' td.active input.check').each( function(elem) {

					if(elem.checked) {
						id =  elem.parentNode.parentNode.id;
						id = id.split("-")[1];
						ids += id + " ";
					}
				});
				//alert(ids);
				return ids;
			}
			// }}}

			function init_amount_fields() {
				$$('input.amount').each(function(elem) {
					//init_amount_field(elem);
					$extend(elem,amountButton);
					elem.init();
					//elem.initialize(50,100);
				});
			}

			// TODO: change functionality below (delayedFunct, add.periodical, ...) to a class
			var AmountButton = new Class({ // {{{
				initial_delay: 500,
				delay: 50,

				init: function(initial_delay, delay) { 
					// {{{
					if (initial_delay) 	this.initial_delay = initial_delay;
					if (delay) 						this.delay = delay; 

					// create the fields
					this.get_div('amount-up',1).inject(this,'before');
					this.get_div('amount-down',-1).inject(this,'before');
					// }}}
				}, 
				add: function(amount) { 
					// {{{
					var val = parseFloat(this.value);
					this.value = val + amount;
					// }}}
				}, 
				get_div: function(className, amount) { 
					// {{{

					var thisButton = this;
					div = new Element('div', { "class": className });
					div.innerHTML = "";

					div.addEvent('mousedown', function(event) {
						//event.stopPropagation();
						this.pressed = true;	
						//event.stopPropagation();
						var addFunc = function() { thisButton.add(amount) };
						var delayedFunc = function() { 
							if (!this.timer && this.pressed) { 
								this.timer = addFunc.periodical(thisButton.delay); 
							} 
						}
						delayedFunc.delay(thisButton.initial_delay,this);
					});
					div.addEvent('mouseup', function(event) {
						//event.stopPropagation();
						this.pressed = false;
						$clear(this.timer);
						thisButton.add(amount);
						this.timer = null;
					});

					return div;
					// }}}
				}

			}); // }}}

			var amountButton = new AmountButton();

			function init_amount_field(elem) { // {{{
				div = new Element('div', { "class": "amount-up" });
				div.innerHTML = '';
				div.inject(elem, 'before');

				elem.add = function(amount) {
					var val = parseFloat(elem.value);
					elem.value = val + amount;
				}

				div.addEvent('click', function(event) {
					var val = parseFloat(elem.value);
					elem.value = val + 1;
				});
				div.addEvent('mousedown', function(event) {
					//event.stopPropagation();
					this.pressed = true;	
					//event.stopPropagation();
					var add = function() { this.add(1); }
					var delayedFunc = function() { if (this.pressed) { this.timer = add.periodical(10,elem); } }
					delayedFunc.delay(500,this);
				});
				div.addEvent('mouseup', function(event) {
					//event.stopPropagation();
					this.pressed = false;
					$clear(this.timer);
				});

				div = new Element('div', { "class": "amount-down" });
				div.innerHTML = '';
				div.inject(elem, 'before');

				div.addEvent('click', function() {
					var val = parseFloat(elem.value);
					if (val >= 1) {
						elem.value = val - 1;
					} else {
						elem.value = 0;
					}
				});
			} // }}}

			var focus_on_init = true;
			var max_combos = 10;
			window.addEvent('domready', function() { // {{{
				init_regular(true, focus_on_init);
				init_combi(true, focus_on_init);
				init_combi_cbx(true);
				init_system_cbx(true);
				init_selected_quotes();
				init_system_bet_forms(true);
				//init_amount_fields();
			}); // }}}
		
