	
	var idPage = 139;
	var typAkce = 0;
	
	
	$(document).ready(function () {
		// jen cisla
		$('.numOnly').keyup(function () { 
		    this.value = this.value.replace(/[^0-9\.]/g,'');
		});

		$('.stats_tbl:first').css({'border-top':'dotted 1px #ccc'});
		$('#tbl_podtyp').hide();
		$('#formInner').hide();
		$('#tbl_termin2').hide();
		$('#tbl_url').hide();
		$('#tbl_klientName').hide();
		$('.prevToHide').hide();
		
		$('#typ_akce').change(function(){
		typAkce = $(this).val();
			if(typAkce == 1 ){
				
				$('input.podTyp').removeAttr("checked");
				
				//alert($("input.podTyp:checked").length)
				
				$('#formInner').fadeOut();
				$('#tbl_podtyp').fadeIn();
				$('.important').fadeIn();
				$('span#denHolder').fadeIn();
				$('#pocet_prev').text(1);
				$('.prevToHide').fadeIn();
			}else if(typAkce > 1){
				$('#tbl_klientName').hide();
				$('#tbl_termin2').hide();
				$('#den2, #mes2, #rok2, #klientName').removeClass('formReq');
				$('input,select').removeClass('errorInput');
				//$("input.podTyp").attr('checked', false);
				
				$('input.podTyp').removeAttr("checked");
				
				$('#tbl_klientName').removeClass('errorInput');
				$('#ucast option:eq(1)').attr('disabled', false);
				$('#tbl_podtyp').fadeOut();
				$('#formInner').fadeIn();
				$('span#denHolder').hide();
				$('#pocet_prev').text(1);
				$('#den2_prev').text('');
				$('#mes2_prev').text('');
				$('#rok2_prev').text('');
				$('#pocet').val(1).attr('readonly', false);
				$('#tbl_url').fadeOut();
				$('#tbl_klientName').fadeOut();
				$('.prevToHide').hide();
			}else{
				$('#tbl_klientName').hide();
				$('#tbl_termin2').hide();
				$('#den2, #mes2, #rok2, #klientName').removeClass('formReq');
				$('input,select').removeClass('errorInput');
				//$("input.podTyp").attr('checked', false);
				
				$('input.podTyp').removeAttr("checked");
				
				$('#tbl_klientName').removeClass('errorInput');
				$('#ucast option:eq(1)').attr('disabled', false);
				$('#tbl_podtyp').fadeOut();
				$('#formInner').fadeOut();
				$('span#denHolder').fadeOut();	
				$('#pocet_prev').text('');
				$('#den2_prev').text('');
				$('#mes2_prev').text('');
				$('#rok2_prev').text('');
				$('#pocet').val(1).attr('readonly', false);
				$('#tbl_url').fadeOut();
				$('#tbl_klientName').fadeOut();
				$('.prevToHide').hide();
			}
		})

		$('.podTyp').click(function(){

			//alert($("input.podTyp:checked").length)
			
			if($("input.podTyp:checked").length == 3){
				$('#formInner').fadeIn();
				$('#pocet').val(1).attr('readonly', true);
				$('#pocet_prev').text(1);
				$('#ucast_prev').text('');
				$('#formType').val(2);
				$('#ucast option:eq(1)').attr('disabled', true);
				$('#ucast option:eq(0)').attr('selected', true);
				$('span#denHolder').fadeIn();
				$('#tbl_url').fadeIn();
				$('#tbl_klientName').fadeIn();
				$('#tbl_termin2').fadeIn();
				$('#den2, #mes2, #rok2, #klientName').addClass('formReq');
				$('.prevToHide').show();
			}else{
				$('#formInner').fadeIn();
				$('#formType').val(1);
				$('#pocet').val(1).attr('readonly', false);
				$('#ucast option:eq(1)').attr('disabled', false);
				$('span#denHolder').fadeOut();
				$('#tbl_url').fadeOut();
				$('#url_prev').text('');
				$('#url_klientName').text('');
				$('#tbl_klientName').fadeOut();
				$('#tbl_termin2').fadeOut();
				$('#den2_prev').text('');
				$('#mes2_prev').text('');
				$('#rok2_prev').text('');
				$('#den2, #mes2, #rok2, #klientName').removeClass('formReq').removeClass('errorInput');
				$('#den2 option:eq(0), #mes2 option:eq(0), #rok2 option:eq(0)').attr('selected', true);
				$('.prevToHide').hide();
			}
		})

		
		$('.continue').click(function(){
		typAkce = $(this).val();
			
			//$('#tbl_podtyp').fadeOut();
			$('.important').fadeOut();
			$('#formInner').fadeIn();
			$('span#denHolder').hide();
			$("input.podTyp").attr('checked', false);
			$('#pocet_prev').text(1);
			$('#den2_prev').text('');
			$('#mes2_prev').text('');
			$('#rok2_prev').text('');
			$('#pocet').val(1).attr('readonly', false);
			$('#tbl_url').fadeOut();
			$('#tbl_klientName').fadeOut();
			$('.prevToHide').hide();
			
		})
		
	});

	// odeslani formulare
	$(document).ready(function () {
		$('form#formStats').submit(function(){
			return false;
		})
		$('#formCheck').click(function(){
			checkStatsForm(typAkce);
			
		})
		$('#formRepair').click(function(){
			$('#formPreview').animate({'left':'-600px'});
			$('#formHolder').animate({'left':'0px'});
			$('#submitReport').animate({'left':'-900px'}).val('');
		})
		$('#formSend').click(function(){
			/*$('#formPreview').animate({'left':'-600px'});
			$('#formHolder').animate({'left':'0px'});*/
			//alert('Teď jako odesílám ...');
			$('#formSend').attr('disabled', true);
			$('#submitReport').animate({'left':'0px'}).text($('#submitProcess').val());
			
			
			$.post("index.php", $("form#formStats").serializeArray(), function(data){
				if(data == true){
					//alert('ulozeno');
					$('#submitReport').text($('#submitSuccessText').val());
					setTimeout(function(){
					
							$('form#formStats').clearForm();
							$('#pocet').val(1);
							$('#submitReport').animate({'left':'-900px'}).val('');
							$('#formPreview').animate({'left':'-600px'});
							$('#formHolder').animate({'left':'0px'}, function(){
							$('#formSend').attr('disabled', false);

							$('#tbl_podtyp').hide();
							$('#formInner').hide();
							$('#tbl_termin2').hide();
							$('#tbl_url').hide();
							$('#tbl_klientName').hide();
							
							$('.leftColum').load('index.php?page=' + idPage + ' .leftColum');
							});
					}		
					, 3000)
					

				}else{
					//alert('some alert');
					$('#submitReport').text($('#submitErrorText').val());
					$('#formSend').attr('disabled', false);
				};
	  		}, "json");
			
		})
	})
	
	// obnoveni resp. odstraneni chybove classy + vyplneni nahledu
	$(document).ready(function () {
		$('select.formReq, input.formReq').change(function(){
			if($(this).val() != ''){
				$(this).removeClass('errorInput');
			}
		})
		$('select#den, select#mes, select#rok, select#den2, select#mes2, select#rok2').change(function(){
			if($(this).val() != ''){
				$(this).removeClass('errorInput');
			}
		})
		$('input#pocet, input#url, input#trzby_1, input#trzby_2, input#trzby_3, input#klientName').change(function(){
			if($(this).val() != '' || $(this).val() != '0'){
				$(this).removeClass('errorInput');
			}
		})
	})
	
	function checkStatsForm(typAkce) {
		// kontrola povinych poli
		$("select.formReq, input.formReq").each(function(){
			if($(this).val() == ''){
				$(this).addClass('errorInput');
			}
		})
		// kontrola terminu
		if(typAkce == 1 && $("input.podTyp:checked").length == 3){
			if($('select#den').val() == ''){
				$('#den').addClass('errorInput');
			}

		}
		// kontrola poctu akci
		if($('input#pocet').val() == '' || $('input#pocet').val() == '0'){
			$('input#pocet').addClass('errorInput');
		}	

		// sestavim nahled

			$('input#url, input#trzby_1, input#trzby_2, input#trzby_3').each(function(){
				if($(this).val() != ''){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').text($(this).val());
				}else{
					$('div#'+$(this).attr('id')+'_prev').text(' n/a');
				}
			});		
			$('select.formReq').each(function(){
				if($(this).val() != ''){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').text($(':selected',this).text());
				}else{
					$('div#'+$(this).attr('id')+'_prev').text('');
				}
			});		
			$('input.formReq, input#klientName').each(function(){
				if($(this).val() != ''){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').text($(this).val());
				}else{
					$('div#'+$(this).attr('id')+'_prev').text('');
				}
			});	
			$('input#pocet').each(function(){
				if($(this).val() != '' || $(this).val() != '0'){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').text($(this).val());
				}else{
					$('div#'+$(this).attr('id')+'_prev').text('');
				}
			});
			$('input#ubyt').each(function(){
				var anone = $(this).attr('rel').split('_');
				if($(this).attr('checked') == true){
					$('div#'+$(this).attr('id')+'_prev').text(anone[0]);
				}else{
					$('div#'+$(this).attr('id')+'_prev').text(anone[1]);
				}
			});
			$('select#den').each(function(){
				if($(this).val() != '' && typAkce == 1){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').html($(':selected',this).text() + '&nbsp/&nbsp');
				}else{
					$('div#'+$(this).attr('id')+'_prev').text('');
				}
			});
			$('select#mes').each(function(){
				if($(this).val() != ''){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').html($(':selected',this).text() + '&nbsp/&nbsp');
				}else{
					$('div#'+$(this).attr('id')+'_prev').text('');
				}
			});
			$('select#rok').each(function(){
				if($(this).val() != ''){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').text($(':selected',this).text());
				}else{
					$('div#'+$(this).attr('id')+'_prev').text('');
				}
			});	
			
			$('select#den2').each(function(){
				if($(this).val() != '' && $("input.podTyp:checked").length == 3){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').html($(':selected',this).text() + '&nbsp/&nbsp');
				}else{
					$('div#'+$(this).attr('id')+'_prev').text('');
				}
			});
			$('select#mes2').each(function(){
				if($(this).val() != '' && $("input.podTyp:checked").length == 3){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').html($(':selected',this).text() + '&nbsp/&nbsp');
				}else{
					$('div#'+$(this).attr('id')+'_prev').text('');
				}
			});
			$('select#rok2').each(function(){
				if($(this).val() != '' && $("input.podTyp:checked").length == 3){
					$(this).removeClass('errorInput');
					$('div#'+$(this).attr('id')+'_prev').text($(':selected',this).text());
				}else{
					$('div#'+$(this).attr('id')+'_prev').text('');
				}
			});
		// konec nahledu
			
		// jsou nejake chyby ?
		if($(".errorInput").length > 0){
			$('#errReport').text($('#errorText').val());
			$('#formPreview').animate({'left':'-600px'});
			$('#formHolder').animate({'left':'0px'});
		}else{
			$('#errReport').text('')
			scrollToElem('#h1_div');
			$('#formPreview').animate({'left':'0px'});
			$('#formHolder').animate({'left':'-600px'});
		}
	}
	
	/////////////////////////
	// SCROLL TO ELEMENT
	/////////////////////////
	function scrollToElem(elem){
		
		var destination = $(elem).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 700);
		return false;
	}
	
	/////////////////////////
	// CLEAR FORM
	/////////////////////////	
	$.fn.clearForm = function() {
		 return this.each(function() {
		 var type = this.type, tag = this.tagName.toLowerCase();
		 if (tag == 'form')
		   return $(':input',this).clearForm();
		 if (type == 'text' || type == 'password' || tag == 'textarea')
		   this.value = '';
		 else if (type == 'checkbox' || type == 'radio')
		   this.checked = false;
		 else if (tag == 'select')
		   this.selectedIndex = 0;
		  });
		};	
	
