
function form_Init(form_id){

	$('#'+form_id+' fieldset legend').each(function(i){
		if($(this).html()){
			$(this).parents('fieldset').prepend('<span class="legend">'+$(this).html()+"</span>");
			$(this).html('');
			if($.browser.msie){
				if($.browser.version>=8) {
					$(this).css('display','block');
					$(this).css('text-indent','-999em');
					$(this).css('background','transparent');
				}
				else {
					$(this).css('display','none');
				}
			}
		}
	});

	/*
	$('+'+form_id+' fieldset legend').remove();
	*/

	/*
	$('#'+form_id+' input[type=checkbox]').checkbox({
		empty: '/images/forms/empty.png'
	}); 

	$('#'+form_id+' input[type=radio]').checkbox({
		empty: '/images/forms/empty.png'
	});
	*/

	/* agb */
	$('#'+form_id+' input[type=checkbox]').click(function(){
		$(this).parents('fieldset').children('p').children('span.helponerror').hide();
		$(this).parents('fieldset').children('p').children('label.error').addClass('checked').removeClass('error');
		$('#'+form_id).validate().element($(this).parents('fieldset'));
	});

	/* member set */
	$('#'+form_id+' input[type=radio]').click(function(){
		$(this).parents('fieldset').children('div.aka-p').children('span.helponerror').hide();
		$(this).parents('fieldset').children('div.aka-p').children('div.form-p').children('div.column').children('span.header').children('label.error').addClass('checked').removeClass('error');
		$('#'+form_id).validate().element($(this).parents('fieldset'));
	});


	jQuery.validator.addMethod( "phone", function(value, element){
			 var expr = /^[\+\d\/\(\)\. ]+/;
			 return this.optional(element) || expr.test(value);
			}, "Vorsicht, hier stimmt was nicht!"
		);

	jQuery.validator.addMethod( "birthdate", function(value, element){
				var t = value.split('.')
				if( t.length != 3 ) return false;
				var d = parseInt(t[0], 10);
				var m = parseInt(t[1], 10);
				var y = parseInt(t[2], 10);
				if( !d || !m || !y ) return false;
				if( 1>d || 31<d) return false;
				if( 1>m || 12<m) return false;
				var date = new Date();
				if( date.getFullYear()-16 < y || date.getFullYear() - 108 > y ) return false;
				date = new Date(y,m-1,d);
				if( y!=date.getFullYear() || m!=date.getMonth()+1 || d!=date.getDate())
					return false;
				return true;
			}, "Vorsicht, hier stimmt was nicht!"
		);


	$.extend($.validator.messages, { 
			required: 	"Vorsicht, hier fehlt was!",
			email: 		"Vorsicht, hier stimmt was nicht!",		
			digits: 	"Vorsicht, hier stimmt was nicht!",
			dateDE: 	"Vorsicht, hier stimmt was nicht!",
			number: 	"Vorsicht, hier stimmt was nicht!",
			numberDE:  	"Vorsicht, hier stimmt was nicht!",
			equalTo:	"Vorsicht, hier stimmt was nicht!",
			max: 		"Vorsicht, hier stimmt was nicht!",
		  min: 		"Vorsicht, hier stimmt was nicht!",
			minlength: "Vorsicht, hier stimmt was nicht!",
			maxlength: "Vorsicht, hier stimmt was nicht!"
	});

	form_initHints(form_id);
	form_initHelp(form_id);
}


function form_flashNotice(form_id,message){
	$('#'+form_id+' .flash').remove();
	$('#'+form_id+' .first').before('<fieldset class="flash round"><p>'+message+'</p></fieldset>')

}


function form_getParent(node, nodeType){
	$parent = $(node).parents(nodeType);
	return $parent
}

function form_clearHints(form_id){
	$('#'+form_id+' fieldset p ').each(function(i){
		form_clearHint(this,i,'p')
   	});

}

function form_clearHint(node,i,parentNodeType){
	$(node).children('input').each(function(i){
			$parent = form_getParent(this,node);
			if($parent.children('span.hint').length>1){
				var hint = $parent.children('span.hint:eq('+i+')').html();
			} else {
				var hint = $parent.children('span.hint').html();
			}
				if(hint){
					var $input = $(this);
	    	    	if (this.value === (hint) && $(this).attr('hinted') == 'true') {
	    	    	  $input.removeClass('blur').val('');
		        	}
		        }
    	    });

}

function form_initHints(form_id){
	$('#'+form_id+' fieldset p ').each(function(i){
		form_initHint(this,i,'p');
	});
}

function form_initHint(node,i,parentNodeType){
	$(node).children('input').each(function(i){
			var $input = $(this);

			$parent = form_getParent(this,parentNodeType);
			if($parent.children('span.hint').length>1){
				var hint = $parent.children('span.hint:eq('+i+')').html();
			} else {
				var hint = $parent.children('span.hint').html();
			}
			if(hint){
				if ($input.val() == '') {
					$input.addClass('blur').val(hint);
					$input.attr('hinted','true');
				}
				$input.blur(function () {
		    	    if ($input.val() == '') {
	    	    	  $input.addClass('blur').val(hint);
	    	    	  $input.attr('hinted','true');
		        	}
	    	    });
				$input.focus(function () {
		    	    if ($input.val() == (hint) && $(this).attr('hinted') == 'true') {
	    	    	  $input.attr('hinted','false');
	    	    	  $input.val('');
	    	    	  $input.removeClass('blur');
		        	}
	    	    });
			}
			$input.focus(function () {$input.addClass('focus')});
			$input.blur(function () {$input.removeClass('focus')});
			
		});
}

function form_initHelp(form_id){
	$('#'+form_id+' fieldset p ').each(function(i){
		form_initHelpNode(this,i,'p', form_id)
	});
	$('#'+form_id+' fieldset div.aka-p').each(function(i){
		form_initHelpNode(this,i,'div.aka-p', form_id);
	});

	$('#'+form_id+' select').each(function(i){
		$select = $(this);
		$parent = form_getParent(this,'p');
		$parent.mouseenter(function(){
			var id = form_id;
			 form_showHelp($select, this,1, id);
		});
		$parent.mouseleave(function(){
			$(this).children('span.help').hide();
			$(this).children('span.helponerror').hide();
		});
	});
		
}

function form_initHelpNode(node,i,parentNodeType, form_id){
	if(parentNodeType == 'p'){
		$children = $(node).children('input')
	} else {
		$children = $(node).children('div.form-p').children('div.column').children('span.header');
	}
	$children.each(function(i){
				$parent = form_getParent(this,parentNodeType);
				if($parent.children('span.help').length) { 
					if($(this).attr('type') != 'checkbox'){
						$(this).focus(function(){
							 form_showHelp(this, $parent,i, form_id);
						});
						var $input = $(this);
						$parent.mouseenter(function(){
							 form_showHelp($input, this,i, form_id);
						});
						$parent.mouseleave(function(){
							$(this).children('span.help').hide();
							$(this).children('span.helponerror').hide();
						});
		
						$(this).blur(function(){
							$parent.children('span.help').hide();
							$parent.children('span.helponerror').hide();
						});
		
		
					} else {
		
		
						$parent.mouseenter(function(){
							 form_showHelp(this, this,i, form_id);
						});
						$parent.mouseleave(function(){
							$(this).children('span.help').hide();
							$(this).children('span.helponerror').hide();
						});
										
					
					}
				}
		})
}


function form_createHelpNode(headline,body,hint){
	return	'<span class="helponerror" generated="true"><span class="head">'+
			headline+
			'</span><span class="body">'+
			body+
			(hint?'<span class="helphint">z.B. "'+hint+'"</span>':'')+
			'</span><span class="hotline">'+
			'Fragen? Klappt was nicht?<br />Ruf uns kostenfrei an:<span class="hotlinenumber">0800 0000 759</span>'+
			'</span></span>';
}


function form_showHelp(node,paragraph,i, form_id){
	form_clearHelp(form_id);
	if($(node).hasClass('error')){
		if($(paragraph).children('span.helponerror').length){
			$(paragraph).children('span.helponerror').each(function(is){
				/* replace hint text for input groups e.g. plz ort */
				$parent = form_getParent(this,'p');
				if($parent.children('span.hint').length>1){
					$hint = "z.B. "+$parent.children('span.hint:eq('+i+')').html();
					$('span.helphint',this).html($hint);
				}
				$(this).show();
			});
		} else {

			$error = $(paragraph).children('label.error:first').html();
			if($error){
				$help = $(paragraph).children('span.help').children('span.body').html();
				if($(paragraph).children('span.hint').length>1){
					$hint = $(paragraph).children('span.hint:eq('+i+')').html();
				} else {
					$hint = $(paragraph).children('span.hint').html();
				}
				if($help){
					$(paragraph).prepend(form_createHelpNode($error,$help,$hint));
				} else {
					$(paragraph).prepend(form_createHelpNode('Vorsicht, hier stimmt was nicht!',$error,''));
				}
			} else {
				$(paragraph).children('span.help').show();
			}
		}
	} else {
		if(!$(paragraph).hasClass('formsubmit')){
			$error = false;
			if($(paragraph).children('label.error:first').length){		
				$error = $(paragraph).children('label.error:first').html();
			}
			if($(paragraph).children('div.form-p').length){
				$error = $(paragraph).children('div.form-p').children('div.column').children('span.header').children('label.error:first').html();
			}
			if($error){
				$help = $(paragraph).children('span.help').children('span.body').html();
				$(paragraph).prepend(form_createHelpNode($error,$help,''));
				$(paragraph).children('span.helponerror').show();		
			} else {
				$(paragraph).children('span.help').show();
			}

		}
	}
}

function form_clearHelp(form_id){
		$('#'+form_id+' span.helponerror').hide();
		$('#'+form_id+' span.help').hide();
}

