
function submitForm() {
  //alert("123");
  //make sure hidden and iframe values are in sync for all rtes before submitting form
  updateRTEs();
/*	
  alert("rte1 = " + (document.RTEDemo.rte1.value));

  params = "rte1=" + document.RTEDemo.rte1.value + "&sact=ofirmie&save=1&submit=Submit";  
  
	$.ajax({
		 url: "?save=1&act=ofirmie",
		 context: document.body,
		 type: "POST",
		 data: params,
		 success: function(data){
		   alert(data);	
		 }
	  });  	
  */
  return true;
}
//------------------------------------
function GetFormUp(elem)
{
  while(elem.tagName!=="FORM"){
	elem = elem.parentNode;  
  }	 
  return elem;	
}
//------------------------------------
function GetUpToTag(elem,tag)
{
  while(elem.tagName!==tag){
	elem = elem.parentNode;  
  }	 
  return elem;	
}
//------------------------------------
function GetUpToClass(elem,class_name1,class_name2)
{
  while(elem && elem.className!==class_name1 && elem.className!==class_name2){
	elem = elem.parentNode;  
  }	 
  return elem;	
}

function Rozwin(elems,elmclass)
{
  if (elmclass == "hidden"){
      $(elems).children('.descrow').animate({				    
        "height": "toggle", "opacity": "toggle" 
     }, 900);
    
      $(elems).toggleClass("hidden visible");
  }  
}
//------------------------------------
// rotacja opisów w stopce
var foot_page_nr = 2;
var foot_img_nr = 3;
function FootRotate() {
	  switch (foot_page_nr)
	  {
	    case 0:
	    	$('#footp1').animate({
	    		"opacity": 0.0
	        }, 900);
	    	$('#footp2').animate({
	    		"opacity": 1.0 
	        }, 900);
	    	$('#footp3').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footp2').css('display','block');
	    	break;
	    case 1:
	    	$('#footp1').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footp2').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footp3').animate({
	    		"opacity": 1.0 
	        }, 900);
	    	$('#footp3').css('display','block');
	    	break;
	    case 2:
	    	$('#footp1').animate({
	    		"opacity": 1.0 
	        }, 900);
	    	$('#footp2').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footp3').animate({
	    		"opacity": 0.0 
	        }, 900);    	
	    	$('#footp1').css('display','block');
	  }
	  switch (foot_img_nr)
	  {
	    case 0:
	    	$('#footimg1').animate({
	    		"opacity": 0.0
	        }, 900);
	    	$('#footimg2').animate({
	    		"opacity": 1.0 
	        }, 900);
	    	$('#footimg3').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footimg4').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footimg2').css('display','block');
	    	break;
	    case 1:
	    	$('#footimg1').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footimg2').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footimg3').animate({
	    		"opacity": 1.0 
	        }, 900);
	    	$('#footimg4').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footimg3').css('display','block');
	    	break;
	    case 2:
	    	$('#footimg1').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footimg2').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footimg3').animate({
	    		"opacity": 0.0 
	        }, 900);    	
	    	$('#footimg4').animate({
	    		"opacity": 1.0 
	        }, 900);
	    	$('#footimg4').css('display','block');
	    	break;
	    case 3:
	    	$('#footimg1').animate({
	    		"opacity": 1.0 
	        }, 900);
	    	$('#footimg2').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footimg3').animate({
	    		"opacity": 0.0 
	        }, 900);    	
	    	$('#footimg4').animate({
	    		"opacity": 0.0 
	        }, 900);
	    	$('#footimg1').css('display','block');
	  }  	
	  foot_page_nr = (foot_page_nr+1) % 3;
	  foot_img_nr = (foot_img_nr+1) % 4;
}

window.setInterval(FootRotate, 6000);


//------------------------------------
$(function() {
	
 $(document).ready(function(){
   FootRotate();
   //$('#footp1').css('display','none');
   //$('#footp2').css('display','block');
   //$('#footp3').css('display','none');
 });

 $(".submitcontact").live('click', function() {	  
	  elem = GetFormUp(this);	 
	  if (elem){
	    var params = $(elem).serialize();
        SendEmail(params,elem);	
      }  
 });
 
 $(".submitworkshop").live('click', function() {  	
  elem = GetFormUp(this);	 
  if (elem){
    var params = $(elem).serialize();
    if (elem.elements['id_workshop'].value == -1){
      SaveNewWorkshop(params,elem);	
    }else{	
      UpdateWorkshop(params,elem);
    }
  }  
 });
	
	
 $(".submitprice").live('click', function() {
  elem = GetFormUp(this);	 
  if (elem){
    var params = $(elem).serialize();
    if (elem.elements['id_price'].value == -1){
      SaveNewPrice(params,elem);	
    }else{	
      UpdatePrice(params,elem);
    }
  }  
 });
	
 $(".deleteprice").live('click', function() {
	  elem = GetFormUp(this);
	  if (elem){
	    if (elem.elements['id_price'].value == -1){
	      $(elem).remove();
	    }else{
		  var answer = confirm("Na pewno chcesz usunąć cenę '" + elem.elements['price'].value + "'?");
		  if (elem && answer){
		    var params = $(elem).serialize();
		    DelPrice(elem.elements['id_price'].value,elem);
		  }
	    }  
	  }
	 });
		
 $(".submitschedule").live('click', function() {
	  elem = GetFormUp(this);	 
	  if (elem){
	    var params = $(elem).serialize();
	    if (elem.elements['id_schedule'].value == -1){
	      SaveNewSchedule(params,elem);	
	    }else{	
	      UpdateSchedule(params,elem);
	    }
	  }  
 });
 
// $(":text[.editprice]").live('focus', function() {
 $(":text, textarea").live('focus', function() {
   $("#container").text($(this).val());
   $(this).data('last',$(this).val())
   var elem = GetFormUp(this);	 
   if (elem){
	 $(this).data('submit_disabled',elem.elements['submitButton'].disabled);	   
   }
 });

 $(":text, textarea").live('blur', function() { 
   elem = GetFormUp(this);	 
   var changes = $(this).data('last') !== $(this).val();
  
   if (elem && changes){
     elem.elements['submitButton'].disabled = false;
   }
 });

 $(":text, textarea").live('keydown', function () {
   var changes = $(this).data('last') !== $(this).val();	 
   var elem = GetFormUp(this);	 
   if (elem){
	 if (changes)  
       elem.elements['submitButton'].disabled = false;
	 else{
	   if ($(this).data('submit_disabled'))	 
         elem.elements['submitButton'].disabled = true;
	 }
   }
 });
 
 $(":text, textarea").live('keyup', function () {   	 
   var changes = $(this).data('last') !== $(this).val();	 
   var elem = GetFormUp(this);	 
   if (elem){
	 if (changes)  
       elem.elements['submitButton'].disabled = false;
	 else{
	   if ($(this).data('submit_disabled'))	 
         elem.elements['submitButton'].disabled = true;
	 }
   }
 });
 
 $("select").live('change', function () {
   elem = GetFormUp(this);	 
   if (elem){
     elem.elements['submitButton'].disabled = false;
   }
 });

 $("#workshop").live('change', function () {
	 UpdateWorkshopDate($(this).val()); 
	//elem = GetFormUp(this); 
    //if (elem){
      //elem.elements['submitButton'].
    //}	
	//);
	//$("select#work_date").html("<option>AA</option>");  
 });
 
 $(".addnewprice").live('click', function() { 	 
   elem = this.parentNode;	 
   if (elem){ 
     NewPrice(this,$('#id_workshop').val());
   }  
 });
 
 $(".addnewschedule").live('click', function() { 	 
   elem = this.parentNode;	 
   if (elem){ 
     NewSchedule(this,$('#id_workshop').val());
   }  
 });

 $(".deleteschedule").live('click', function() {
	  elem = GetFormUp(this);
	  if (elem){
	    if (elem.elements['id_schedule'].value == -1){
	      $(elem).remove();
	    }else{
	      var term = elem.elements['day_from'].value + "." + elem.elements['month_from'].value + "-" 
	          + elem.elements['day_to'].value + "." + elem.elements['month_to'].value + "." 
	          + elem.elements['year'].value;
		  var answer = confirm("Na pewno chcesz usunąć termin '" + term + "'?");
		  if (elem && answer){
		    var params = $(elem).serialize();
		    DelSchedule(elem.elements['id_schedule'].value,elem);
		  }
	    }  
	  }
 });
 
 $(".price_tab .namerow,.price_tab .namerow2").click(function(){
	var wrkid = $(this).attr('id').substr(4);
	if ($(this).attr('id').substr(0,4) == "work") 
	  window.location.href = "?act=trainings&wrkid=" + wrkid + "#wrk" + wrkid;
	else
	  window.location.href = "?act=exams&egzid=" + wrkid + "#wrk" + wrkid;
 });
 /*
 $(".menuitem3").mouseover(function(){
	$(".submenu3").animate({
	    opacity: 1.0
	  }, 100, function() {
	    // Animation complete.
	  }); 
 }); 

 $(".menuitem3").mouseout(function(){
		$(".submenu3").animate({
		    opacity: 0.0
		  }, 50, function() {
		    // Animation complete.
		  }); 
	 }); 

 $(".submenu3").mouseover(function(){
		$(".submenu3").animate({
		    opacity: 1.0
		  }, 100, function() {
		    // Animation complete.
		  }); 
	 }); 
 
 $(".submenu3").mouseout(function(){
		$(".submenu3").animate({
		    opacity: 0.0
		  }, 50, function() {
		    // Animation complete.
		  }); 
	 }); 
  */	
 
 $(".namerow, .namerow2").click(function(){
	// $("tbody.visible").removeClass("visible").addClass("hidden");
	// elem = GetUpToTag(this,"TBODY");
	// $(elem).toggleClass("hidden visible");

	 elem = GetUpToClass(this,"hidden","visible");
	 visy = $(".visible");

	 if (elem)
	 {
		 var elmclass = elem.className;
		 $(visy).children('.descrow').animate({				    
			    "height": "toggle", "opacity": "toggle" 
				  }, 900, function() {
						 $(visy).removeClass("visible").addClass("hidden");
						 Rozwin(elem,elmclass);					  
				  }); 		         
		 		         
		 if ($(visy).length == 0){
			 $(visy).removeClass("visible").addClass("hidden");
			 Rozwin(elem,elmclass);					  			 
		 }
	              
//		 if (elem.className == "hidden"){
			 //$("div.visible").removeClass("visible").addClass("hidden");
			 //$("div.visible").removeClass("visible").addClass("hidden");
		//	 $(elem).children('.descrow').css("height","0%");
//			 $(elem).toggleClass("hidden visible");	
//			 $(elem).children('.descrow').animate({				    
//			    height: "100%", "opacity": 1
//				  }, 1500);
			 
//		 }else{
			 //$(elem).toggleClass("hidden visible");			 			 
//			 $(elem).css("height","0");			 			 
//			 $(elem).toggleClass("hidden visible");			 			 
//			 $(elem).animate({				    
//				    height: '100%'
///				  }, 5000, function() {
				    // Animation complete.
//				  });
			 
//		 }
	 }
		 
	 
 });

});

//------------------------------------
function SaveNewPrice(params,elem) {	
  $('#container').html(params);
  $.ajax({
	 url: "?addnew=price",
     context: document.body,
     type: "POST",
     data: params,
     success: function(data){
	   data = jQuery.trim(data);
	   if (parseInt(data) > 0){
	      elem.elements['submitButton'].disabled = true;
	      elem.elements['delButton'].value = 'usuń';
	      elem.elements['id_price'].value = parseInt(data);
	   }
	 }
  });
}

//------------------------------------
function UpdatePrice(params,elem) {
  $('#container').html(params);
  $.ajax({
	 url: "?update=price",
	 context: document.body,
	 type: "POST",
	 data: params,
	 success: function(data){
	   data = jQuery.trim(data);
	   //alert(data);
	   if (data === "OK"){
	      elem.elements['submitButton'].disabled = true;
	   }  
	 }
  });
}

//------------------------------------
/** pobierz wiersz nowej ceny i dodaj do listy cennika
 */
function NewPrice(dst,id_workshop) {
	$.ajax({
	 url: "?giveme=price&id_workshop=" + id_workshop,
	 context: document.body,
	 type: "GET",
	 //data: params,
	 success: function(data){
	   var $newline = $(data);	   
	   $newline.insertBefore($(dst));
	   $('.priceheader').css("display","block");  
	 }
  });  	
}

//------------------------------------
/** usuń wiersz z listy cennika
 */
function DelPrice(id_price,elem) {
	$.ajax({
	 url: "?delold=price&id_price=" + id_price,
	 context: document.body,
	 type: "GET",
	 //data: params,
	 success: function(data){
	   //alert(data);	
	   data = jQuery.trim(data);
	   if (data === "OK"){
         $(elem).remove();
         //$(".priceline").css('border','2px solid red');
         if ($("#price_list > form").size() == 0){
       	   $('.priceheader').css("display","none");          	 
         }
	   }  
	 }
  });  	
}

//------------------------------------
function SendEmail(params,elem) {	  
  //$('#container').html(params);
  $.ajax({
	 url: "emails.php",
     context: document.body,
     type: "POST",
     data: params,
     success: function(data){
	   data = jQuery.trim(data);
	   alert(data);
	   //if (parseInt(data) > 0){
	      //elem.elements['submitButton'].disabled = true;
	      //elem.elements['delButton'].value = 'usuń';
	      //elem.elements['id_price'].value = parseInt(data);
		 // window.location.href = "?edittraining=" + parseInt(data); 
	   //}
	 }
  });
}

//------------------------------------
function SaveNewWorkshop(params,elem) {	
  $('#container').html(params);
  $.ajax({
	 url: "?addnew=workshop",
     context: document.body,
     type: "POST",
     data: params,
     success: function(data){
	   data = jQuery.trim(data);
	   if (parseInt(data) > 0){
	      //elem.elements['submitButton'].disabled = true;
	      //elem.elements['delButton'].value = 'usuń';
	      //elem.elements['id_price'].value = parseInt(data);
		  window.location.href = "?edittraining=" + parseInt(data); 
	   }
	 }
  });
}

//------------------------------------
function UpdateWorkshop(params,elem) {
  $('#container').html(params);
  $.ajax({
	 url: "?update=workshop",
	 context: document.body,
	 type: "POST",
	 data: params,
	 success: function(data){	   
	   data = jQuery.trim(data);
	   alert(data);
	   if (data === "OK"){
	      elem.elements['submitButton'].disabled = true;
	   }  
	 }
  });
}

//------------------------------------
function UpdateWorkshopDate(value) {
  if (value == -1)
  {
	 $("select#work_date").html("");
	 //$("selecr#work_date").attr("disabled", "disabled");
     //alert('blocked');     
  }else{	
    $.ajax({
	   url: "?giveme=workshop_dates",
//	   context: document.body,
	   type: "POST",
	   data: "id_workshop="+value,
	   success: function(data){
    	 //alert(data);
	     data = jQuery.trim(data);
		 $("select#work_date").html(data);		
         //$("selecr#work_date").removeAttr('disabled');     
	   },
	   error: function(jqXHR, textStatus, errorThrown){
		 alert(textStatus);  
	   }
    });
  }  
}

//------------------------------------
function DeleteWorkshop(id_workshop,exam,title)
{
	var answer = false;
	if (exam)
	  answer = confirm("Na pewno chcesz usunąć egzamin:\r\n '" + title + "'?");
	else
	  answer = confirm("Na pewno chcesz usunąć szkolenie:\r\n '" + title + "'?");

	if (answer)
	{	
	   $.ajax({
		 url: "?delold=workshop&id_workshop=" + id_workshop,
		 context: document.body,
		 type: "GET",
		 //data: params,
		 success: function(data){
		   //alert(data);	
		   data = jQuery.trim(data);
		   if (data === "OK"){
			 if (exam){
				 window.location.href="?act=exams"; 
			 }else{
				 window.location.href="?act=trainings"; 				 
			 }  
			 
		   }  
		 }
	  });
	}
}

//------------------------------------
/** pobierz wiersz nowego terminu szkolenia lub egzaminu
 */
function NewSchedule(dst,id_workshop) {
	$.ajax({
	 url: "?giveme=schedule&id_workshop=" + id_workshop,
	 context: document.body,
	 type: "GET",
	 //data: params,
	 success: function(data){
	   var $newline = $(data);	   
	   $newline.insertBefore($(dst));
	   $('.scheduleheader').css("display","block");  
	 }
  });  	
}
//------------------------------------
function SaveNewSchedule(params,elem) {	
  $('#container').html(params);
  $.ajax({
	 url: "?addnew=schedule",
     context: document.body,
     type: "POST",
     data: params,
     success: function(data){
	   alert(data);
	   data = jQuery.trim(data);
	   if (parseInt(data) > 0){
	      elem.elements['submitButton'].disabled = true;
	      elem.elements['delButton'].value = 'usuń';
	      elem.elements['id_schedule'].value = parseInt(data);
	   }
	 }
  });
}

//------------------------------------
function UpdateSchedule(params,elem) {
  $('#container').html(params);
  $.ajax({
	 url: "?update=schedule",
	 context: document.body,
	 type: "POST",
	 data: params,
	 success: function(data){
	   data = jQuery.trim(data);
	   alert(data);
	   if (data === "OK"){		   
	      elem.elements['submitButton'].disabled = true;
	   }  
	 }
  });
}
//------------------------------------
/** usuń wiersz z listy harmonogramu
 */
function DelSchedule(id_schedule,elem) {
	$.ajax({
	 url: "?delold=schedule&id_schedule=" + id_schedule,
	 context: document.body,
	 type: "GET",
	 //data: params,
	 success: function(data){
	   //alert(data);	
	   data = jQuery.trim(data);
	   if (data === "OK"){
         $(elem).remove();
         //$(".priceline").css('border','2px solid red');
         if ($("#schedule_list > form").size() == 0){
       	   $('.scheduleheader').css("display","none");          	 
         }
	   }  
	 }
  });  	
}

//------------------------------------
/** usuń wiersz z listy harmonogramu
 */
function DeleteFile(fieldname,id_workshop) {
	$.ajax({
		 url: "?delold="+fieldname+"&id_workshop=" + id_workshop,
		 context: document.body,
		 type: "GET",
		 //data: params,
		 success: function(data){
		   //alert(data);	
		   data = jQuery.trim(data);
		   if (data === "OK"){
			 window.location.href = '?giveme=files&id_workshop='+ id_workshop;  
		   }  
		 }
	});  	
}

//------------------------------------
function DeleteNews(id_news, title, elem)
{
	var answer = false;
	answer = confirm("Na pewno chcesz usunąć ogłoszenie:\r\n '" + title + "'?");

	if (answer)
	{	
	   $.ajax({
		 url: "?delold=news&id_news=" + id_news,
		 context: document.body,
		 type: "GET",
		 //data: params,
		 success: function(data){
		   //alert(data);	
		   data = jQuery.trim(data);
		   if (data === "OK"){			 
			 elem = GetUpToClass(elem,"visible","visible always");
			 if (elem)
			   $(elem).remove();
			 else
			   window.location.href="?act=actual"; 			 			   
		   }else{
			 window.location.href="?act=actual"; 			 			   
		   }  
		 }
	  });
	}
}

