// ajax js


var root_url = location.protocol + '//' + location.host + '/'; // url webu






// vseobecne naplnanie
	$("select.napln").livequery('change', function(){ vs_vyber($(this).attr("id"), $('select#' +$(this).attr("id") + ' option:selected').val() ) }); 


	 

  
/* ------------------------------------------------------------------------------------------------------------------- */  
  
/* ------------------------------------------------------------------------------------------------------------------- */  

function form_validation(id) {  /* validacia form udajov*/ 
	 
	 $('.' + id + ' button[type=submit]').attr('disabled', 'disabled'); // zamknutie submitu
	
	// var post_data = $("form").serialize(); 
	 var post_data = $('#' + id).serialize();
	    
	
		
    var result = null;
    
	
	
	$.ajax({ 
				type: "POST", 
				url: root_url + 'includes/form_validation.php', 

				dataType: "json", 
				
				data: post_data,
				
				
			/*	error:function(xhr, status, errorThrown) { // vypise pripadne errory 
                alert(errorThrown+'\n'+status+'\n'+xhr.statusText);
        	},*/

				success: function(data) {  
								
					
					
					
					$('*').removeClass('nevyplnene');
					
					if (data['stav'] == 'ok') {
						
						
							if(data['prejst_na']) {	$(location).attr('href',root_url + data['prejst_na']);	}
							
							else {	
						
									$('.' + id).html(data['vlozit']); 
									$('.' + id + ' button[type=submit]').removeAttr('disabled'); // odomknutie submitu
									
								 }
						 }
					
					else {
					
							$('.' + id + ' #form_sprava').html(data['error_hl']); 			
							
							
							
										
							$.each(data['errors'], function(key, value){  // vypisanie hlasok 
							
								
								$('.' + id + ' #' + key).parent().addClass('nevyplnene'); 
							}); 
							$('.' + id + ' button[type=submit]').removeAttr('disabled'); // odomknutie submitu
					}

				
				} 
       
    	});  
    
	
	
	
	
		 return false;   
		 
}  


/* ------------------------------------------------------------------------------------------------------------------- */  

function vs_vyber(typ_vyberu, vyb_hodntota) {  /* vseobecny vyber  */ 
	   

if(typ_vyberu == 'kraj') { $("select#id_mesto option").remove(); $("<option value=\"\">--- vybrať mesto/obec ---</option>").appendTo("select#id_mesto"); }  // vymazanie hodnot v danom selecte 

	  		
// alert('++++');
        $.ajax({ 
            type: "POST", 
			url: root_url + 'includes/rozny_vyber.php', 
            dataType: "json", 
			data: {  
			akcia : typ_vyberu,
			vybrane : vyb_hodntota
			},
			
			/*	error:function(xhr, status, errorThrown) { // vypise pripadne errory 
                alert(errorThrown+'\n'+status+'\n'+xhr.statusText);
        	},*/
			
            success: function(data) {  
               
				var vystup =  data['ciel'] // urcenie selektu kam sa maju vyplnit data
               
					if(data['typ'] == 'obsah') { // ak sa jedna o vlozenie obsahu
						
						
						
						if(data['popup']) { // ak sa maju data zobrazit v modal boxe
							
							modalBox_call_Source(root_url + 'includes/ajax_blocks.php?id=' + data['obsah'] + '');		
							
							}
									
									
						else {
						
						
						$.get(root_url + 'includes/ajax_blocks.php?id=' + data['obsah'],null,function(result) {
  						  $('#' + vystup).html(result); 
						},'html');
						
						}
						
					}
					 
					else { // ak sa jedna o vlozenie hodnot do selektu

					$("select#" + vystup + " option").remove(); // vymazanie hodnot v danom selecte
					
					$.each(data['obsah'], function(key, value){  // naplnenie daneho selectu
                    
						var row = "<option value=\"" + key + "\">" + value + "</option>"; 
						
						$(row).appendTo("select#" + vystup); 
               		});
					 
					}
 				
				//////////////////////
			}
       
    });     

  }
/* ------------------------------------------------------------------------------------------------------------------- */  

function vs_vyber2(vybr_blok) {  /* vseobecny vyber  */ 
	   
	  			

      
						
						$.get(root_url + 'includes/ajax_blocks.php?id=' + vybr_blok ,null,function(result) {
  						  $('#' + vystup).html(result); 
						},'html');
						
				
       
   

  }
/* ------------------------------------------------------------------------------------------------------------------- */  


/* ------------------------------------------------------------------------------------------------------------------- */  



/* upload images */  

	
function ajaxFileUpload()
	{
		$(".loading_1")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});

		$.ajaxFileUpload
		(
			{	
			
				url: root_url + 'includes/ajax_upload_file.php', 
		
				fileElementId:'fileToUpload',
				dataType: 'json',
				
			
				success: function (data, status)
				{
					if(typeof(data['error']) != 'undefined')
					{
						if(data['error'] != '')
						{
							$('#fil_upl_inf').show().html( data['error']);
						}else
						{
							// alert(data['msg']);
						
							
							$('.up_im_pl').html(' <input id="fileToUpload" type="file" name="fileToUpload" size="40"  style="margin-top:2px;"/>');
							
							$('.upl_file_list').append('<li><img src="' + data['url_pic'] +'" height="80"/> <input name="img[]" type="hidden" value="' +data['id_pic'] + '" /><div class="del_image"></div></li>');
							$('#fil_upl_inf').show().html( data['msg']);
							
						}
					}
				},
				error: function (data, status, e)
				{
					alert(e);
				}
			}
		)
		
		return false;

	}

	
/* ------------------------------------------------------------------------------------------------------------------- */  
/* del images */  
function del_image(id_pic) {  /* delete image */ 
	 
	 
	    $.ajax({ 
				type: "POST", 
				url: root_url + 'includes/pic_del.php', 

				dataType: "json", 
				data: {  
				id : id_pic
				
				},
						
								
		/*	error:function(xhr, status, errorThrown) { // vypise pripadne errory 
                alert(errorThrown+'\n'+status+'\n'+xhr.statusText);
        	},*/

			
				success: function(data) {  
					
					$('input[value|=' + data['id'] + ']').parent().remove();
					$('#fil_upl_inf').show().html( data['msg']);
				} 
       
    	});     
	
}  



/* ------------------------------------------------------------------------------------------------------------------- */  

