jQuery().ready(function(){
		$('#tf_onlineorder input:text').addClass('formtextinput');
		$('#tf_onlineorder input:text').wrap("<span class='inputleft'><span class='inputright'><span class='inputmain'></span></span></span>");
		$('#tf_onlineorder span.inputleft:even').css('margin-right','2px');
		$('#tf_onlineorder span.inputleft:has(.error), .formcol span.inputleft:has(.f3-form-error)').css('background-position','left -20px');
		$('#tf_onlineorder span.inputright:has(.error), .formcol span.inputright:has(.f3-form-error)').css('background-position','right -20px');
		$('#tf_onlineorder span.inputmain:has(.error), .formcol span.inputmain:has(.f3-form-error)').css('background-position','0 -20px');


		$('.radiolabel input:checked').parent().addClass('selected');
		$('.radiolabel label').click(function(){

			if ($(this).parent().find('input:disabled').length > 0) {
				return false;
			} 
				$(this).parent().parent().children().removeClass('selected');
				$(this).parent().addClass('selected');
				
				$(this).parent().parent().children(':radio[checked=true]').attr('checked', false);
				$(this).parent().children(':radio').attr('checked', true);
				
				if ($(this).parent().children(':radio[value=1]').attr('value') == 1) {
					if (!$(this).parent().parent().hasClass('donationImpossible')) {
						showAllFollowingRows($(this).closest('[class=formrowflex]').next('[class=formrowflex2]').show('fast'));
					}
				} else {
					hideAllFollowingRows($(this).closest('[class=formrowflex]').next('[class=formrowflex2]').hide('fast'));
				}
		});
		
		$('.radiolabel input:checked').each(function () {
			if ($(this).attr('value') == 0) {
				var flexrow = $(this).closest('[class~=formrowflex]').next('[class=formrowflex2]');
				if ($(this).closest('[class~=formrowflex2]').hasClass('subyesno')) {
					flexrow = $(this).closest('[class~=formrowflex2]').next('[class~=formrowflex2]');
					flexrow.hide();
				}
				flexrow.hide();
				hideAllFollowingRows(flexrow, 0);
			}
		
		});
		
		// if neither yes nor no is selected, hide everything and select no
		var radioname = '';
		$('.radiolabel input:radio').each(function () {
			radioname = $(this).attr('name');
			if(jQuery.find('.formrowflex input[name="' + radioname + '"]:checked').length == 0) {
				hideAllFollowingRows($(this).closest('[class=formrowflex]').next('[class=formrowflex2]').hide(), 0);
				if ($(this).attr('value') == 0) {
					//$(this).next('label').click();
				}
			}

		});
		
		$('.subyesno .radiolabel label').click(function() {
			var value = $(this).parent().find('input').attr('value');
			if (value == 0) {
				$(this).closest('[class~=formrowflex2]').next('[class~=formrowflex2]').hide('fast');
			} else {
				$(this).closest('[class~=formrowflex2]').next('[class~=formrowflex2]').show('fast');
			}
		});
		
		$('.go_back').click(function() {
			history.back();
			//return false;
		});
		
		$("form").submit(function () {

		});
		
		$('.anamnesis_parents input:checkbox').click(function () {
			$(this).closest('.formrowflex').find('.hide_on_uncheck').toggle('fast');
		});
		
		$('.anamnesis_parents input[value=1]:checked').each(function () {
			$(this).closest('.formrowflex').find('.hide_on_uncheck').hide();
		});
		
		$('.#tf_onlineorder input.f3-form-error').each(function () {
			$(this).parent().addClass('error').parent().addClass('error').parent().addClass('error');
		});
		

		
	});
	
function hideAllFollowingRows(row, speed) {
	if (speed === undefined) {
		speed = 'fast';
	}
	while (row.next().hasClass('formrowflex2')) {
		row = row.next().hide(speed);
	}
}

function showAllFollowingRows(row, speed) {
	if (speed === undefined) {
		speed = 'fast';
	}
	while (row.next().hasClass('formrowflex2')) {
		if (row.next().hasClass('subyesno')) {
			row = row.next().show(speed);
			if (row.find('input[value=0]:checked').length > 0) {
				if (row.next() && row.next().next()) {
					row = row.next();
					continue;
				} else {
					break;
				}
			}
		}
		row = row.next().show(speed);
	}
}

function checkFollowingInputs(row) {
	var noError = true;
	while(row.next().hasClass('formrowflex2') && !row.next().hasClass('errorDonationFlex')) {
		row = row.next();
		if ($(row).find('input[value=""]:text:visible').length > 0) {
			$(row).find('span').addClass('error');
			noError = false;
		}
	}
	return noError;
}
	
$(function() {
	$.datepicker.setDefaults({
		regional: 'de'
	});
	var dayNames = ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'];
	var dateFormat = 'dd.mm.yy';
	var dayNamesMin = ['So','Mo','Di','Mi','Do','Fr','Sa'];
	var maxDate = '+6m';
	var minDate = new Date(2011, 10, 1);
	var defaultDate = '-30y';
	var showDate = function(dateText, inst) {
		if (dateText) {
		 	$('.birthdate_show').show('fast');
		}
	}
	
	var switchToWeeksDate = new Date(2011, 9, 1);
	var now = new Date();
	if (switchToWeeksDate < now) {
		minDate = '+5w';
	}
	
	$('.datepicker').datepicker({
		defaultDate: defaultDate,
		dayNames: dayNames,
		dayNamesMin: dayNamesMin,
		dateFormat: dateFormat,
		onClose: showDate
	});	

	$('.datepicker.limited').datepicker('option', 'maxDate', maxDate);
	$('.datepicker.limited').datepicker('option', 'minDate', minDate);
	$('.datepicker.limited').datepicker('option', 'defaultDate', '+2m');
	$('.datepicker.sixW').datepicker('option', 'defaultDate', '+6w');
	$('.datepicker').focus(function() { $(this).blur(); });
});

$(function() {	
	if($('.birthdate[value!=""]').length > 0) {
		$('.birthdate_show').show();
		if($('#caesarionOperation:checked').length > 0) {
			$('#caesarionOperationDate').show();
		}
	}
	
	$('.caesarionOperation').click(function() {
		if ($('#caesarionOperation').is(':checked')) {
			$('#caesarionOperationDate').show('fast');
		} else {
			$('#caesarionOperationDate').hide('fast');
		}
	});
});

$(function () {
	$('form.final').submit(function() {
		if ($('.required_proc:checked').length > 0) {
			$('.required_proc:checked').each(function () {
				var name = $(this).attr('id');
				$('.' + name + '_error').hide('fast');
			});
		}
		if ($('.required_proc:not(:checked)').length > 0) {
			$('.required_proc:not(:checked)').each(function () {
				var name = $(this).attr('id');
				$('.' + name + '_error').show('fast');
			});
			return false;
		}
	});
	
	$('form').submit(function() {
		var noError = true;
	/*var minDate = new Date();
	var maxDate = new Date();
	var now = minDate.getTime();
	minDate.setTime(now + 84600 * 7 * 3);
	maxDate.setTime(now + 84000 * 7 * 4 * 6);
		$('.datepicker').each(function() {
		var date = $(this).datepicker('getDate')
			if(date && date.getTime() < minDate.getTime()) {
				$('#dateWarning').show('fast');
			}
		});*/
		// donation error!
		var noDonateError = true;
		if($('.donationImpossible input[value=1]:checked').length > 0) {
			$('.donationImpossible input[value=1]:not(.errorDonation):checked').each(function () {
				$(this).addClass('errorDonation').parents('.formrowflex').after('<div class="errorDonationFlex formrowflex2 "><span class="errors">In diesem Fall ist eine &ouml;ffentliche Spende leider nicht m&ouml;glich.</span></div>').show();
			});
		}
		
				$('.donationImpossible input[value=0]:checked').each(function () {
			$(this).parents('.formrowflex').next('.errorDonationFlex').hide('fast').remove();
		});
		$('.errorDonation[value=1]:not(:checked)').removeClass('errorDonation');
		
		if ($('.errorDonation').length > 0) {
			noDonateError = false;
		} else {
			noDonateError = true;
		}
		
		// ------
		$('.formrowflex .radiolabel input[value=1]:checked').each(function () {
				if(!checkFollowingInputs($(this).closest('.formrowflex'))) {
					noError = false;
				}
		});

			
			


		$('input[value=""][class*=required]:text:visible').each(function() {
			$(this).parent().addClass('error').parent().addClass('error').parent().addClass('error');
		});
		$('input[value!=""][class*=required]:text:visible').each(function() {
			$(this).parent().removeClass('error').parent().removeClass('error').parent().removeClass('error');
		});
		
		
		if ($('input[value=""][class*=required]:text:visible').length > 0) {
			noError = false;
		}
		
		
		// check if everything is selected
		var radioname = '';
		$('.radiolabel:visible input:radio').each(function () {
			radioname = $(this).attr('name');
			if($("input[name='" + radioname + "']:checked").length == 0) {
				
				noError = false;
			}
		});
		

		if (noError === true) {
			noError = ($('input[value=""][class*=required]:text:visible').length == 0);
			
		}

		if (!noError) {
			$('#tf_onlineorder h2').after($('#radiobox_error'));
			$('#radiobox_error').show('fast');
			return false;
		}
		
		return (noError && noDonateError);

		
	});
	
	$('.disabled_default').focus(function () { $(this).blur(); });
	
	jQuery('#tf_onlineorder input:text').maxlength({maxCharacters : 45, callback : function(input) {
		$(input).parent().addClass('error').parent().addClass('error').parent().addClass('error');
		setTimeout("$('[name=\"" + $(input).attr('name') + "\"]').parent().removeClass('error').parent().removeClass('error').parent().removeClass('error');", 500); }});
		
	jQuery('.tx-srfeuserregister-pi1-submit.go_on').click(function() {
		var match = jQuery('#tx-srfeuserregister-pi1-date_of_birth').attr('value').match(/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/);
		if (match) {
			if (match[1] == '.') {
				jQuery('#tx-srfeuserregister-pi1-date_of_birth').attr('value', jQuery('#tx-srfeuserregister-pi1-date_of_birth').attr('value').replace(/\./g, '-'))
			}
		} else {
			match = jQuery('#tx-srfeuserregister-pi1-date_of_birth').attr('value').match(/^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/);
			if (match) {
				var date = match[0].split(match[1]);
				jQuery('#tx-srfeuserregister-pi1-date_of_birth').attr('value', date[0] + "-" + date[1] + "-19" + date[2]); 
			} else {
				jQuery('.date_of_birth_format').css({color : '#ff0000'});
				return false;
			}
		}
	});
	
	if ($('body').hasClass('ssl')) {
		$('.sider a, .footer a').each(function() {
			$(this).attr('href', "http://www.lebensretter-nabelschnurblut.de/" + $(this).attr("href"));
		});
	};

	$('.tooltip').each(
		function () {
	   $(this).qtip(
	   {
	      content: $(this).find('p').html(),
	     // position: { target: 'mouse' },
	      show : { delay : 0 },
	      hide : { effect : { length : 300 } }
	   });

	});
});
