jQuery.fn.tagName = function() { return this.get(0).tagName.toLowerCase(); };

function getUrl() {
  return window.currentUrl;
}
function sendMail(nr) {
	
	var fieldsEmail = new Array();
	var i = 0;
	
	if($('#f'+nr+'__name').val() == '' || $('#f'+nr+'__email').val() == '' || $('#f'+nr+'__subject').val() == '' || $('#f'+nr+'__info').val() == '') {
		$('.form-contact-message-'+nr).html('Pola imię i nazwisko, e-mail, temat oraz treść muszą być uzupełnione.');
		return false; 
	}
	
	$('#f'+nr+'__tst').val(121);
	jQuery('.form-contact-inner-'+nr+' input[name*="f__"], .form-contact-inner-'+nr+' textarea[name*="f__"]').each(function(index) {
		field = new Array();
		var nameItem = $(this).attr('name').substring(3,$(this).attr('name').length);
		if ($(this).tagName() == 'textarea') {
			field[0] = nameItem;
			field[1] = $(this).val(); 
			field[2] = 'Info';
		} else {
			field[0] = nameItem;
			field[1] = $(this).val(); 
			field[2] = $(this).attr('alt');
		}
		
		if(field != undefined && field != '' && field[0] != undefined && field[1] != undefined && field[2] != undefined) {
			fieldsEmail[i++] = field;
		}
	});
	
	var urlAjax = getUrl()+'ctrl/user/action/sendEmail';
	$('.form-contact-message-'+nr).html('Trwa wysyłanie wiadomości...');
	$('.sub-mit-but-'+nr).hide();
	$.ajax({
		url: urlAjax,
	    global: false,
	    type: "POST",
	    data: ({fields : fieldsEmail, 
	    		ajax: 'trigger'}),
	    dataType: "json",
	    async:false
	 }).success(function(data) {
		 $('.form-contact-message-'+nr).html(data.message);
		 if(data.variables != undefined && data.variables.correct != undefined && data.variables.correct == true) {
			 $('.form-contact-inner-'+nr).slideUp('slow', function() {
				 $('.form-contact-inner-'+nr).html('');
			 });
		 } 
		 $('.sub-mit-but-'+nr).show();
		 $('#f'+nr+'__tst').val('120');
	 });
}

function sendReservationMail() {
	if(!validateReservationMail()) return false;
	var fieldsEmail = new Array();
	var i = 0;
	$('.frm-htl input[name*="h__"], .frm-htl textarea[name*="h__"], .frm-htl select[name*="h__"]').each(function(index) {
		field = new Array();
		var nameItem = $(this).attr('name').substring(3,$(this).attr('name').length);
		if ($(this).tagName() == 'select') {
			field[0] = nameItem;
			field[2] = $(this).attr('alt');
			$('#'+$(this).attr('name')+" option:selected").each(function () {
				//field[1] = $(this).val(); 
				field[1] = $(this).html();
			});
		} else if($(this).attr('type') == 'checkbox') {
			field[0] = nameItem;
			if($(this).attr('checked') == false) field[1] = 'off';
			else field[1] = 'on';
			field[2] = $(this).attr('alt');
		} else {
			field[0] = nameItem;
			field[1] = $(this).val(); 
			field[2] = $(this).attr('alt');
		}
		if(field != undefined && field != '' && field[0] != undefined && field[1] != undefined && field[2] != undefined) {
			fieldsEmail[i++] = field;
		}
	});
	
	var hotelVar = $('#hotel_field').val();
	
	var urlAjax = getUrl()+'ctrl/content/action/sendEmailReservation';
	$('.message-form-hotel').html('Trwa wysyłanie wiadomości...').show();
	$('.but').hide();
	$.ajax({
		url: urlAjax,
	    global: false,
	    type: "POST",
	    data: ({fields : fieldsEmail, 
	    		hotel : hotelVar, 
	    		ajax: 'trigger'}),
	    dataType: "json",
	    async:false
	 }).success(function(data) {
		 $('.message-form-hotel').html(data.message).show();
		 if(data.variables != undefined && data.variables.correct != undefined && data.variables.correct == true) {
			 document.getElementById('frm_hotel').reset();
			 setTimeout("$.fancybox.close()",6000);
		 } else {
			 $('.but').show();
		 }
	 });
	
}

function validateReservationMail() {
	$('.alert-input').removeClass('alert-input');
	if($('#h__imie_nazwisko').val() == '') return alertReservField('#h__imie_nazwisko', 'Należy podać imię i nazwisko osoby rezerwującej.'); 
	if($('#h__email').val() == '') return alertReservField('#h__email', 'Należy podać e-mail.');
	if($('#h__telefon').val() == '') return alertReservField('#h__telefon', 'Należy podać numer telefonu.');
	if($('#h__nazwiska').val() == '') return alertReservField('#h__nazwiska', 'Należy podać imię i nazwisko gościa(i).');
	var indexPokoje = 0;
	$('input[name^="h__pokoj"]').each(function(index) { 
		if($(this).is(':checked')) {
			if($('#h__ilosc'+$(this).attr('id').substring($(this).attr('id').length - 1)).val() != '') 
				indexPokoje++;
		}
	});
	if(indexPokoje == 0) return alertReservField('[id^="h__ilosc"]', 'Należy podać rodzaj pokoju oraz ilość.');
	
	var indexDateFrom = 0;
	if($('#h__dzien_przyjazdu').val() == '') { alertReservField('#h__dzien_przyjazdu'); indexDateFrom++; }
	if($('#h__miesiac_przyjazdu').val() == '') { alertReservField('#h__miesiac_przyjazdu'); indexDateFrom++; }
	if($('#h__rok_przyjazdu').val() == '') { alertReservField('#h__rok_przyjazdu'); indexDateFrom++; }
	if(indexDateFrom > 0) return alertReservField('', 'Należy podać datę przyjazdu.');
	var indexDateTo = 0;
	if($('#h__dzien_wyjazdu').val() == '') { alertReservField('#h__dzien_wyjazdu'); indexDateTo++; }
	if($('#h__miesiac_wyjazdu').val() == '') { alertReservField('#h__miesiac_wyjazdu'); indexDateTo++; }
	if($('#h__rok_wyjazdu').val() == '') { alertReservField('#h__rok_wyjazdu'); indexDateTo++; }		
	if(indexDateTo > 0) return alertReservField('', 'Należy podać datę wyjazdu.');
	$('.message-form-hotel').hide();
	return true;
}

function alertReservField(id, text) {
	if(id != '')
		$(id).addClass('alert-input');
	if(text != undefined && text != '')
		$('.message-form-hotel').html(text).show();
	return false;
}

function orderColumn(ctrl, action, divReplace, field, direct) {
  var urlPage = getUrl();
  if(urlPage == '' || urlPage == undefined) return false;
  $('div.loading-ajax').show();
  var urlAjax = urlPage+'ctrl/'+ctrl+'/action/'+action;
  $.ajax({
      url: urlAjax,
      global: false,
      type: "POST",
      data: ({postOrderField : field, postOrderDirect: direct, ajax: 'trigger'}),
      dataType: "json",
      async:false
   }).success(function(data) { 
      $('div.'+divReplace).replaceWith(data.body);
   });
   $('div.loading-ajax').hide();
}

function changePage(ctrl, action, divReplace, page) {
  var urlPage = getUrl();
  if(urlPage == '' || urlPage == undefined) return false;
  $('div.loading-ajax').show();
  var urlAjax = urlPage+'ctrl/'+ctrl+'/action/'+action;
  $.ajax({
      url: urlAjax,
      global: false,
      type: "POST",
      data: ({postPage : page, ajax: 'trigger'}),
      dataType: "json",
      async:false
   }).success(function(data) { 
	   if($('#effect_pagination').val() == 'slide_up') {
		   $('div.'+divReplace).addClass(divReplace+'-1').css('position', 'relative').after(data.body).next().addClass(divReplace+'-2').css({'display': 'none', 'margin-top' : '10px' });
		   $('div.'+divReplace+'-1').slideUp(1500, function() { $(this).remove(); });
		   $('div.'+divReplace+'-2').slideDown(1500, function() { $(this).removeClass(divReplace+'-2').css('margin-top', '0px'); });
	   } else if($('#effect_pagination').val() == 'slide_down') {
		   $('div.'+divReplace).addClass(divReplace+'-1').css('position', 'relative').before(data.body).prev().addClass(divReplace+'-2').css({'display': 'none', 'margin-bottom' : '10px' });
		   $('div.'+divReplace+'-1').slideUp(1500, function() { $(this).remove(); });
		   $('div.'+divReplace+'-2').slideDown(1500, function() { $(this).removeClass(divReplace+'-2').css('margin-bottom', '0px'); });
	   } else {
		   $('div.'+divReplace).replaceWith(data.body);
	   }
   });
  //cufonLoad();
   $('div.loading-ajax').hide();
}


function changeLimit(ctrl, action, divReplace, selectForm) {
  $('div.loading-ajax').show();
  var options = selectForm.options;
  var limitInput = '';
  if(options.length > 0) {
     for(var i = 0; i < options.length; i++) {
        if(options[i].selected) 
            limitInput = options[i].value;
     }
  } 
  var urlPage = getUrl();
  if(urlPage == '' || urlPage == undefined) { $('div.loading-ajax').hide(); return false };
  limitInput = $(selectForm).children(':selected').attr('value');
  var urlAjax = urlPage+'ctrl/'+ctrl+'/action/'+action;
  $.ajax({
      url: urlAjax,
      global: false,
      type: "POST",
      data: ({postLimit :  limitInput, ajax: 'trigger'}),
      dataType: "json",
      async:false
   }).success(function(data) { 
	  $('div.'+divReplace).replaceWith(data.body);
   });
   $('div.loading-ajax').hide();
}

