//http://tgbestell/Kartenbestellungen/bestellung/search

/* Basisurl zum Bestellmodul -> BEI BEDARF ZU ÄNDERN 
 * 
 * (potentielle Kandidaten:
 *  local:   /Kartenbestellungen
 *  Tg-Test: /test/bestellungen
 *  Tg-Live: ??? (vermutlich /bestellungen)
 */
 
var BESTELLROOT_LOCAL = '/Kartenbestellungen';
var BESTELLROOT_TEST  = '/test/bestellungen';
var BESTELLROOT_LIVE  = '/bestellungen';
 
var bestellungenBase = BESTELLROOT_LIVE;

var sucheVisible 	= false;
var ergebnisVisible = false;
var detailVisible   = false;

function initLastminute() {
	var url = bestellungenBase + '/lastminute/lms';
	
	document.body.style.cursor = 'wait';
	
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(t) {
			$('lastminutes').innerHTML = t.responseText;
			document.body.style.cursor = 'default';
		}
	});
}


function initSuche() {

    // Suche aus Bestellmodul holen
    var url = bestellungenBase + '/bestellung/search/0/0/1/10/0';
    
    document.body.style.cursor = 'wait';
    
    new Ajax.Request(url, {
        method: 'get',
        onSuccess: function(t) {
            $('suche_content').innerHTML = t.responseText;
            
            Calendar.setup({
 				inputField: 'suche_von',
 				ifFormat  : "%d.%m.%Y",
 				button    : 'calendar_button_von'
 			});
 			Calendar.setup({
 				inputField: 'suche_bis',
 				ifFormat  : "%d.%m.%Y",
 				button    : 'calendar_button_bis'
 			});
 			
            new Effect.BlindDown('bestellung_suche', { 
            	duration: 0.5,
            	afterFinish: function() { 
            		document.body.style.cursor = 'default';
            		sucheVisible = true; 
            	} 
           	});
            
        }
    });
}

function reset() {
	new Effect.BlindUp('bestellung_ergebnis', {
		duration: 0.5,
		afterFinish: function() {
			ergebnisVisible = false;
			new Effect.BlindDown('bestellung_suche', { 
				duration: 0.5,
				afterFinish: function() { sucheVisible = true; }
			});
		}
	});
}

function search(repeat, page) {
	var requestParams = null;
	
	document.body.style.cursor = 'wait';
	
	if(repeat) {
		
		requestParams = lastSearchParams;
		
	} else {
		var spnr    = $F('suche_spielstaette');
		var von     = $F('suche_von').strip();
		var bis     = $F('suche_bis').strip();
		var gattung = $F('suche_gattung');
		var titel   = $F('suche_titel');
		var autor   = $F('suche_autor');
		
		// Von/Bis prüfen (wenn ausgefüllt)
		var fromSet  = false;
		var fromOk   = true;
		var fromDate = null;
		
		if(von != 'TT.MM.JJJJ' && von != '') {
			fromSet  = true;
			
			var fromArray = von.split('.');
			if(fromArray.length != 3 || fromArray[0].length != 2 || fromArray[1].length != 2 || fromArray[2].length != 4) 
				fromOk = false
			else von.scan(/(.{2})\.(.{2})\.(.{4})/, function(matches) {
				
				if(isNaN(matches[1]) || isNaN(matches[2] || isNaN(matches[3]))) {
					
					fromOk = false;
					
				} else {
					
				
					if(matches[1].length != 2 || matches[2].length != 2 || matches[3].length != 4) {
						fromOk = false;
					}
				
					var fromDay   = parseInt(matches[1], 10);
					var fromMonth = parseInt(matches[2], 10) - 1; // JS expects 0(Jan) - 11(Dec)
					var fromYear  = parseInt(matches[3], 10);
					
					fromDate = new Date(fromYear, fromMonth, fromDay);
					
					if(!(  fromYear  == fromDate.getFullYear() 
						&& fromMonth == fromDate.getMonth()
						&& fromDay   == fromDate.getDate())) {
						
						fromOk = false;
					} 
				}
			});
		}

		var toSet  = false;
		var toOk   = true;
		var toDate = null;
		
		if(bis != 'TT.MM.JJJJ' && bis != '') {
			toSet = true;
			
			var toArray = bis.split('.');
			if(toArray.length != 3 || toArray[0].length != 2 || toArray[1].length != 2 || toArray[2].length != 4) 
				toOk = false
			else bis.scan(/(.{2})\.(.{2})\.(.{4})/, function(matches) {
				if(isNaN(matches[1]) || isNaN(matches[2] || isNaN(matches[3]))) {
					toOk = false;
					
				} else {

					if(matches[1].length != 2 || matches[2].length != 2 || matches[3].length != 4) {
						toOk = false;
					}
	
					var toDay   = parseInt(matches[1], 10);
					var toMonth = parseInt(matches[2], 10) - 1; // JS expects 0(Jan) - 11(Dec)
					var toYear  = parseInt(matches[3], 10);

					toDate = new Date(toYear, toMonth, toDay);
					
					if(!(toYear    == toDate.getFullYear() 
						&& toMonth == toDate.getMonth()
						&& toDay   == toDate.getDate())) {
						
						toOk = false;
					}
				}
			});
		}
		
		var fromBeforeTo = true;
		if(fromSet && toSet && fromOk && toOk && ( fromDate.getTime() > toDate.getTime() )) {
			fromBeforeTo = false;
		}
		
		if(!fromOk) {
			alert('Bitte geben Sie ein gültiges Datum im Format "TT.MM.JJJJ" in das "von"-Feld ein.');
		}
		
		if(!toOk) {
			alert('Bitte geben Sie ein gültiges Datum im Format "TT.MM.JJJJ" in das "bis"-Feld ein.');
		}
		
		if(!fromBeforeTo) {
			alert('Das im "bis"-Feld eingegebene Datum liegt vor dem im "vom"-Feld');
		}
		
		if(!fromOk || !toOk || !fromBeforeTo) {
			return;
		}
		
		var tage = ''
		$A(new Array('mo','di','mi','do','fr','sa','so')).each(function(tag) {
			if($('suche_tag_' + tag).checked) {
				tage += $F('suche_tag_' + tag) + ';';
			}
		});
		
		requestParams = $H({
			'spnr'    : spnr,
			'von'     : von,
			'bis'     : bis,
			'gattung' : gattung,
			'titel'   : titel,
			'autor'   : autor,
			'tage'    : tage
		});
		
		lastSearchParams = requestParams;
	}
	
	url = bestellungenBase + '/bestellung/search/0/0';
	if(page != null) {
		url += '/' + page;
	} else {
		url += '/1';
	}
	
	if($('max_hits')) {
		url += '/' + $F('max_hits');
	} else {
		url += '/10';
	}
	
	url += '/0'
	
	new Ajax.Request(url, {
		method     : 'post',
		parameters : requestParams.toQueryString(),
		onSuccess  : function(t, json) {
			
			//new Insertion.After('bestellung_suche', t.responseText);
			
			$('bestellung_ergebnis').innerHTML = t.responseText;
			
			document.body.style.cursor = 'default';
			
			if(!ergebnisVisible) {
				
				new Effect.BlindUp('bestellung_suche', {
					duration: 0.5,
					afterFinish: function() {
						sucheVisible = false;
						new Effect.BlindDown('bestellung_ergebnis', {
							duration: 0.5,
							afterFinish: function() { 
								ergebnisVisible = true; 
							}
						});
					}
					
				});
			}
		}
	});
}

function showDetail(foo, bar, id) {
	url =  bestellungenBase + '/bestellung/detail/0/0/' + id + '/0';
	
	document.body.style.cursor = 'wait';
	
	new Ajax.Request(url, {
		onSuccess: function(t, json) {
			$('float').innerHTML = t.responseText;

            var scrollOffset = window.pageYOffset
                               || document.documentElement.scrollTop
                               || document.body.scrollTop
                               || 0;

			var detailX = (document.documentElement.clientWidth - $('float').getWidth()) / 2;
			var detailY = ((document.documentElement.clientHeight - $('float').getHeight()) / 2) + scrollOffset;
			
			new Effect.Move('float', {
				duration: 0,
				mode	: 'absolute',
				x		: detailX,
				y		: detailY
			});
			
			document.body.style.cursor = 'default';
			
			if(!detailVisible) {
                $$('select').each(function(element) {
                    element.hide();
                });
            
				new Effect.Appear('float', {
					duration: 0.5,
					afterFinish: function() {
						detailVisible = true;
					}
				});
			}
		}
	});
}

function toggleDetailResult(show) {
	if(!show) {
        $$('select').each(function(element) {
            element.show();
        });
        
		new Effect.Fade('float', {
			duration: 0.5,
			afterFinish: function() {
				detailVisible = false;
			}
		})
	}
}