$(document).ready(function() {
	$('#ausbildungslehrgangForm').submit(function(e){		
		var error=0;
		var errorArr=0;
		errorArr = new Array();
		if (!$("input.anrede:checked").val()){
			$("input.anrede").each(function(){
				$(this).prev().css('color','red');
			});
			error=1;		
			errorArr.push('Geschlecht');
		}
		else{
			$("input.anrede").each(function(){
				$(this).prev().css('color','black');
			});
		}
		
		if (!$("input.lehrgang:checked").val()){
			$('input.lehrgang').each(function(){
				$(this).next('label').css('color','red');
			});
			$('#lehrgang_150').parent().children('.quest, .info').css('color','red');			
			error=1;
			errorArr.push('Lehrgang');			
		}
		else{
			$('input.lehrgang').each(function(){
				$(this).next('label').css('color','black');
			});
			$('#lehrgang_150').parent().children('.quest, .info').css('color','black');
		}
		
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		$('input[alt=required], .required:file').each(function(){
			if($(this).val()=="" || $(this).hasClass($(this).val())){
				$(this).css('color','red');
				error=1;
				errorArr.push($(this).attr('title'));
			}
			else if($(this).attr("class")=="Email"){
				if (!emailReg.test($(this).val())) {
					$(this).css('color','red');
					error=1;
					errorArr.push($(this).attr('title'));
				}
				else {
					$(this).css('color','black');
				}
			}
			else{
				$(this).css('color','black');
			}
		});
		
		/*$('.required:text, .required:file').each(function(){
			if($(this).val()=="" || $(this).hasClass($(this).val())){
				$(this).css('color','red');
				error=1;
			}
			else{
				$(this).css('color','black');
			}
		});*/
		
		$('textarea.required').each(function(){			
			if($(this).attr('value')=="" || $(this).hasClass($(this).attr('value'))){		
				$(this).css('border','1px solid red');			
				$(this).parent().parent().parent().parent().parent().children('.quest, .info').css('color', 'red');
				
				error=1;
				
				if($.inArray($(this).attr('title'), errorArr) == false) {
					errorArr.push($(this).attr('title'));
				}
			}
			else{
				$(this).css('border','1px solid black');
				$(this).parent().parent().parent().parent().parent().children('.quest, .info').css('color', 'black');
			}
		});
		
		$('.tinymce').each(function(){
			if($(this).hasClass('required') && $(this).val()==''){
				$(this).next().children().css({'border-width':'0 1px','border-color':'red'});
				$(this).next().children().children().children().children().css({'border-color':'red'});
				
				$(this).prev().css('color','red');
				$(this).prev().prev().css('color','red');
				
				error=1;
				errorArr.push($(this).attr('title'));
			}
			else{
				$(this).next().children().css({'border-width':'0 1px','border-color':'#ccc'});
				$(this).next().children().children().children().children().css({'border-color':'#ccc'});
				
				$(this).prev().css('color','black');
				$(this).prev().prev().css('color','black');
			}
		});
		
		if(error==1){
			var errorString='';
			$.each(errorArr,function(k,v){
				errorString+='<li>'+v+'</li>';
			});
			$('#form_error').html('Bitte füllen Sie noch folgende Pflichtfelder aus:<ul>'+errorString+'</ul>').slideDown();
			return false;
		}
		else{
			$('#form_error').slideUp();
			$('#form_submit').html('<img src="http://www.eltern-bildung.at/typo3conf/ext/ausbildungslehrgang/res/img/ajaxH.gif" class="ajaxH" /><div class="ajaxHInfo">Bitte haben Sie ein wenig Geduld. Ihr Antragsformular wird für die Vorschau generiert ...</div>');
		}
	});
	
	$('.addModule').live('click',function(){
		$(this).removeClass().addClass('addLoadingModule');
		//get No of Modules
		var modulAnzahl = $(this).parent().parent().parent().attr('id').replace('module_','');
		modulAnzahl++;
		$(this).parent().parent().parent().attr('id','module_'+modulAnzahl);
		//get No of Question
		var questNo = $(this).parent().parent().attr('title');
		
		$check = $(this).parent().parent().parent();
		$.ajax({
			url: '?id=120&type=301&quest='+questNo+'&moduleno='+modulAnzahl,
			success: function(result){
				$('.modules').append(result);
				$('.addLoadingModule').removeClass().addClass('addModule');
				$('.multi').MultiFile();
			}
		});
	});
	
	$('.showtipp').click(function(e){
		e.preventDefault();
		$(this).next().slideDown();
	});
	
	$('.uploadAdd').live('click', function(){
		var nr = parseInt($(this).parent().attr("id").substring(10));
		var nnr = nr+1;
		$(this).parent().after('<div id="materials_'+nnr+'" class="materials"><input class="matup" type="file" title="stundenplan" name="material_1[]"/><input type="hidden" name="material[]"/><div class="uploadAdd"></div></div>');
		$(this).remove();
	});
	
	$('.delete_antrfile').click(function(){
		$(this).prev().prev().remove();
		$(this).prev().remove();
		$(this).remove();
	});
	
	$('.moduleName').live('click',function(){
		if($(this).parent().next().css('display') == 'none'){
			$(this).parent().next().slideDown();
		}
		else{
			$(this).parent().next().slideUp();
		}
	});
	
	$('textarea.tinymce').tinymce({
		    // Location of TinyMCE script
		    script_url : '/typo3conf/ext/ausbildungslehrgang/res/js/tiny_mce/tiny_mce.js',
			language : 'de',
		    // General options
		    theme : "advanced",
		    plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		
		    // Theme options
		    theme_advanced_buttons1 : "bold,italic,underline,forecolor,undo,redo,fullscreen",
		    theme_advanced_buttons2 : "",
		    theme_advanced_buttons3 : "",
		    theme_advanced_buttons4 : "",
		    theme_advanced_toolbar_location : "top",
		    theme_advanced_toolbar_align : "left",
		    theme_advanced_statusbar_location : "none",
		    theme_advanced_resizing : true,
		
		    // Example content CSS (should be your site CSS)
		    content_css : "css/content.css",
		
		    // Drop lists for link/image/media/template dialogs
		    template_external_list_url : "lists/template_list.js",
		    external_link_list_url : "lists/link_list.js",
		    external_image_list_url : "lists/image_list.js",
		    media_external_list_url : "lists/media_list.js",
		    
		    paste_text_sticky : true,
			setup : function(ed){
			    	ed.onInit.add(function(ed) {
			    		ed.pasteAsPlainText = true;
					});
			},
		    cleanup:true,
		    verify_html : true,
		    paste_auto_cleanup_on_paste : true,
            paste_remove_styles: true,
            paste_remove_styles_if_webkit: true,
            paste_strip_class_attributes: true,
		    paste_retain_style_properties : "margin, padding, width, height, font-size, font-weight, font-family, color, text-align, ul, ol, li, text-decoration, border, background, float, display",
		    pasteAsPlainText: true,
		    
		    // Replace values for the template plugin
		    template_replace_values : {
		            username : "Some User",
		            staffid : "991234"
		   	}
	    });
	    
	    
});
