/*
 * This is an automaic generated Javascript file from on-page CMS.
 */

/* New uncompressed script block */
jQuery.fn.autocomplete = function(url, settings ) 
{
	return this.each( function()//do it for each matched element
	{
		var textInput = $(this);
		textInput.attr('autocomplete', 'off');
		textInput.blur(function() {
			checkblur();
		});
		textInput.focus(function() {
			checkblur(true);
		});
		textInput.after('<ul class="autocomplete" style="display:none;" role="region" aria-live="assertive" aria-atomic="true" aria-relevant="additions removals"></ul>');
		textInput.parent().addClass('autocomplete-wrapper');
		var list = textInput.next();
		var oldText = '';
		var typingTimeout;
		var size = 0;
		var selected = 0;

		settings = jQuery.extend(//provide default settings
		{
			minChars : 1,
			timeout: 1000,
			after : null,
			before : null,
			validSelection : true,
			parameters : {'inputName' : textInput.attr('name'), 'inputId' : textInput.attr('id')}
		} , settings);

		function getData(text)
		{
			window.clearInterval(typingTimeout);
			if (text != oldText && (settings.minChars != null && text.length >= settings.minChars))
			{
				if (settings.before == "function") 
				{
					settings.before(textInput,text);
				}
				textInput.addClass('autocomplete-loading');
				settings.parameters.text = text;
				$.getJSON(url,settings.parameters,function(data)
				{
					var items = '';
					if (data)
					{
						size = data.length;
						for (i = 0; i < data.length; i++)
						{
							for ( key in data[i] )
							{
								items += '<li>' + data[i][key] + '</li>';
							}
							list.html(items);
							$('ul.autocomplete > li').children('a').bind('blur', function() {
								checkblur();
							});
							$('ul.autocomplete > li').children('a').bind('focus', function() {
								checkblur(true);
							});
							list.show("fast");
						}
						if (settings.after == "function") 
						{
							settings.after(textInput,text);
						}
					}
					textInput.removeClass('autocomplete-loading');
				});
				oldText = text;
			}
		}
		
		function clear()
		{
			list.hide("slow");
			size = 0;
			selected = 0;
		}	

		function checkblur(hasfocus)
		{
			if(hasfocus)
			{
				list.stop('queue');
				return;
			}
			list.delay(100, 'queue').hide("slow");
		}	
		
		textInput.keydown(function(e) 
		{
			window.clearInterval(typingTimeout);
			if(e.which == 27)//escape
			{
				clear();
			}
			else if(e.which == 13)//enter 
			{ 
				clear();
				return true;
			}
			else
			{ 
				typingTimeout = window.setTimeout(function() { getData(textInput.val()) },settings.timeout);
			}
		});
	});
};

$(document).ready(function() {
	$("input.suggest").autocomplete(abs_cms_path + "ajax.php?module=autosuggest_index_search&method=autocomplete",{minChars:3,timeout: 200});
});

/* New uncompressed script block */

$(document).ready(function() {
	var remError = function(id){
		$("#" + id).removeClass("formerror").attr("aria-invalid", "false");
		$("#error-" + id).hide();
	},
	addError = function(id, message){
		$("#" + id).addClass("formerror").attr("aria-invalid", "true");
		if($("#error-" + id).length){
			$("#error-" + id).show().html('<li role="alert">' + message +'</li>');
		}else{
			$("#" + id).parent().before('<ul class="errormsgs" id="error-' + id + '"><li role="alert">' + message +'</li></ul>');
		}
	},
	addMessage = function(id, message){
		$("#liveregion-" + id).html(message);
			setTimeout(function(){
				$("#liveregion-" + id).text("");
			}, 10000);
	},
	chk = '',
	b = $("#betrag"),
	fb = $("#festbetrag");
	if(fb.val() != -1){
		$("#betragwrap").hide();
	}
	fb.change(function(){
		if($(this).val() == -1){
			$("#betragwrap").show();
			b.val("");
		}else{
			$("#betragwrap").hide();
			b.val($(this).val());
			remError("betrag");
		}
	});

	$("#formular input").keyup(function(){
		remError($(this).attr("id"));
	});

	$("#amount input").blur(function(){
		var fbv = fb.val();
		var bv = b.val();
		
		if(fbv == -1 && bv == "") {
			addError("betrag","Bitte geben Sie einen Spendenbetrag an.");
		} else if(fbv == -1 && bv != "" && !bv.match(/^\d+$/)) {
			bv = bv.replace(/[^\d]+/,"")
			if(bv.match(/^\d+$/)){
				$("#betrag").val(bv);
				remError("betrag");
				addMessage("betrag", "Der Betrag wurde in " + bv + " Euro korrigiert.");
			} else {
				addError("betrag","Bitte geben Sie einen Betrag in Zahlen an.");
			}
		} else {
			remError("betrag");
		}
	});

	$("#account input:text").blur(function(){
		var curid = $(this).attr("id");
		var curval = $(this).val();
		var inhaber = $("#inhaber").val();
		var kto = $("#kto").val();
		var blz = $("#blz").val();
		if(curid == "inhaber" && curval == ""){
			addError("inhaber","Bitte geben Sie den Kontoinhaber an.");
			return;
		}
		else if(curid == "inhaber"){
			remError("inhaber");
		}

		if(curid == "kto" && !curval.match(/^\d+$/)){
			addError("kto","Bitte geben Sie die Kontonummer an.");
			return;
		}
		else if(curid == "kto"){
			remError("kto");
		}

		if(curid == "blz" && !curval.match(/^\d+$/)){
			addError("blz","Bitte geben Sie die Bankleitzahl an.");
			return;
		}
		else if(curid == "blz"){
			remError("blz");
		}

		if(/* kto.match(/^\d+$/) && */ blz.match(/^\d+$/) && chk != kto + blz){
			chk = kto + blz;
			// $("#bank").addClass('autocomplete-loading');
			$.getJSON('/ajax.php?module=spendenformular&method=checkaccount','kto=' + kto + '&blz=' + blz, function(data) {
				// $("#bank").removeClass('autocomplete-loading'); 
				if(data.bank){
					$("#bank").text(data.bank);
					remError("blz");
					remError("bank");
					addMessage("bank","Ihre Bank wurde automatisch ausgew&auml;hlt.");
				}else{
					$("#bank").text("");
				} 
				$.each(data.errors, function(index, value) { 
					addError(index,value[0]);
				});
			});
		}
	});

	$("#vorname").blur(function(){
		if($(this).val() == ""){
			addError("vorname","Bitte geben Sie Ihren Vornamen an.");
		}
		else{
			remError("vorname");
		}
	});
	$("#nachname").blur(function(){
		if($(this).val() == ""){
			addError("nachname","Bitte geben Sie Ihren Nachnamen an.");
		}
		else{
			remError("nachname");
		}
	});
	$("#adresse").blur(function(){
		if($(this).val() == ""){
			addError("adresse","Bitte geben Sie Ihren Adresse an.");
		}
		else{
			remError("adresse");
		}
	});
	$("#plz").blur(function(){
		if($(this).val() == ""){
			addError("plz","Bitte geben Sie Ihren Postleitzahl an.");
		}
		else if(!$(this).val().match(/^\d{5}$/)){
			addError("plz","Bitte geben Sie eine fünfstellige Postleitzahl an.");
		}
		else{
			remError("plz");
		}
	});
	$("#ort").blur(function(){
		if($(this).val() == ""){
			addError("ort","Bitte geben Sie Ihren Ort an.");
		}
		else{
			remError("ort");
		}
	});
	$("#email").blur(function(){
		if($(this).val() == ""){
			addError("email","Bitte geben Sie Ihre E-Mail Adresse an.");
		}
		else if(!$(this).val().match(/@/)){
			// vorläufige prüfung
			addError("email","Bitte geben Sie eine gültige E-Mail Adresse an.");
		}
		else{
			remError("email");
		}
	});
});


/* EOF */
