$(function(){
	$('#alert').dialog({
	    autoOpen: false,
	    width: 400,
	    modal: true,
	    resizable: false,
	    buttons: {
	        "Ok": function() {
	            $(this).dialog("close");
	        }
	    }
	});
});

$(document).ready(function() {
	var checkAlert = $('#alertText').html();
	var sendCaptchaCheck = true;
	if(checkAlert != ''){
	    $('#alert').dialog('open');
	    return false;
	}

    $("#formLogin").submit(function(){
		$.post(
		    "index.php",
		    $("#formLogin").serialize() + "&id=Login&event=do_login",
		    function(data){
                if(data.tr_logged == 1){
                    redirect('', 1500);
                }
                else{
                	$('#alertText').html(data.output);
                	$('#alert').dialog('open');
                }
		    }
		    , "json"
		);
        return false;
    });

    if($('form.formDesigner').length > 0){
    	$('form.formDesigner').each(function (index) {
    		var obj = this;
			var f_id = $(this).attr('id');
    		$('#'+f_id).submit(function(){
    			if(sendCaptchaCheck){
	    			var checks = true;
	    			$('#'+f_id).find(".f_required").each(function(index2){
		    			if($(this).attr('type') == 'radio'){
							if($("input[@name="+$(this).attr('name')+"]:checked").length > 0){

							}
							else{
								checks = false;
							}
		    			}
		    			else if($(this).attr('type') == 'checkbox'){
							if(!$(this).is(':checked')){
								checks = false;
							}
		    			}
		    			else{
							if($(this).val().length < 1){
								checks = false;
							}
		    			}
		    		});
		    		if(checks == false){
	                	$('#alertText').html('Bitte füllen Sie alle Pflichtfelder aus!');
	                	$('#alert').dialog('open');
	                	return false;
		    		}
					$.post(
					    "index.php",
					    { "id": "Form_designer", "event": "checkCapture", "captcha_code": $('#captcha_code').val() },
					    function(data){
				               if(data.captcha == 1){
				               	   sendCaptchaCheck = false;
				               	   $('#'+f_id).append('<input type="hidden" name="checkedCaptcha" value="1" />');
				                   $('#'+f_id).submit();
				               }
				               else{
			              			$('#alertText').html(data.message);
			              			$('#alert').dialog('open');
				               }
					    }
					    , "json"
					);
					return false;
				}
				sendCaptchaCheck = true;
				return true;
	    	});
    	});
    	return false;
    }

});

function redirect(url, time){
	if(!time){
		time = 0;
	}
	if(url == ''){
		setTimeout(function(){ location.reload(true); }, time);
	}
	else{
		setTimeout(function(){ location.href = url }, time);
	}
	return;
}

function logout(){
	$.post(
	    "index.php",
	    { "id": "Logout", "event": "do_logout" },
	    function(data){
               if(data.tr_logout == 1){
                   redirect('index.php', 0);
               }
	    }
	    , "json"
	);
    return false;
}
