var $j = jQuery.noConflict();

jQuery.extend($mconfig);

function search(init)
{
	$j('#s_miasto').removeOption(/./);

	if ($j('#s_kraj').val() == -1)
	{
		$j('#s_miasto').empty();
		$j('#s_miasto').addOption('-1', $mconfig['all'], false);
	}
	else
	{
		$mconfig['location'].each(function (i)
		{
			if (!i.country) return;

			if (i.country == $j('#s_kraj').val())
			{
				$j('#s_dzielnica').addOption('-1', $mconfig['all'], false);	
				if (i.city.length > 1)
				{
					$j('#s_miasto').addOption('-1', $mconfig['all'], false);
					
				}

				for (a = 0; a < i.city.length; a++)
				{
					$j('#s_miasto').addOption(i.city[a], i.city[a], false);
	 			}
			}
		});
	}

	if (init)
	{
		$j('#s_miasto option[value='+$mconfig['city']+']').attr('selected', 'selected');
		search_district($mconfig['district']);
		
	}
}

function search_district(set)
{
	$j('#s_dzielnica').empty();
	if ($j('#s_miasto').val() != -1)
	{

		$mconfig['location'].each(function (i, idx)
		{
			if (!i.country) return;
			
			if (i.country == $j('#s_kraj').val()){

				for (a = 0; a < i.city.length; a++)
					if (i.city[a] == $j('#s_miasto').val()) break;
				
				if (i.district[a] != "")
				{
					var dzielnice = i.district[a].split('|');
					
					if (dzielnice.length > 1) $j('#s_dzielnica').addOption('-1', $mconfig['all'], false);			
	
					for (k = 0; k < dzielnice.length; k++)
					{
						if (dzielnice[k] == set) $j('#s_dzielnica').addOption(dzielnice[k], dzielnice[k], true)
						else $j('#s_dzielnica').addOption(dzielnice[k], dzielnice[k], false);
					}
				}else
					$j('#s_dzielnica').addOption('-1', $mconfig['all'], false);	
			}
		});
	}else{
		$j('#s_dzielnica').addOption('-1', $mconfig['all'], false);	
	}
}

function vat_rows()
{
	if ($j('#vat').attr('checked'))
	{
		$j('#vat1,#vat2,#vat3,#vat4,#vat5').show();
	}
	else
	{
		$j('#vat1,#vat2,#vat3,#vat4,#vat5').hide();
	}
}

function image_reload()
{
	var now = new Date();

	$j(':text#security').val('');
	$j('img[name=security_img]').attr('src', $mconfig['urls']['security_img'].replace('%s', now.getTime()));
}

function send_chk()
{
	var s = $j('form#send_form');
	var neg = false;
	var warning = '';

	if (!checkEmail(s.find('[name=email]').val()))
	{
		warning = $mconfig['mess'][1];
		neg = true;
	}

	if (s.find('[name=aid]').val() == '' ||
	s.find('[name=imie]').val() == '' ||
	s.find('[name=pytanie]').val() == '' ||
	s.find('[name=email]').val() == '' ||
	s.find('[name=security]').val() == '')
	{
		warning = warning + $mconfig['mess'][0];
		neg = true;
	}

	if (s.find('[name=security]').val().length < 6)
	{
		warning = warning + $mconfig['mess'][2];
		neg = true;
	}

	if (neg)
	{
		alert(warning);
		return false;
	}
	else
	{
		s.submit();
	}
}

function amount_chk()
{
	var p = $j('select#pay');
	var a = $j(':text[name=amount]');

	if (p.val() == -1)
	{
		a.show();
	}
	else
	{
		a.hide();
		a.val(p.val());
	}
}

function rez_chk()
{
	var r = $j('form#rez_form');

	var neg = false;
	var inf = false;

	var warning = '';
	var info = '';

	if (r.find('[name=aid]').val() == '' ||
	r.find('[name=imie]').val() == '' ||
	r.find('[name=ulica]').val() == '' ||
	r.find('[name=kod]').val() == '' ||
	r.find('[name=miasto]').val() == '' ||
	r.find('[name=tel1]').val() == '' ||
	r.find('[name=email]').val() == '' )
	{
		warning = $mconfig['mess'][0];
		neg = true;
	}
	else if (!checkEmail(r.find('[name=email]').val()))
	{
		warning = warning + $mconfig['mess'][1];
		neg = true;
	}

	var tmp_dateFrom = $j('[name=calendar_from_data]').val().split(',');
	var date_from = new Date(Number(tmp_dateFrom[0]), Number(tmp_dateFrom[1]) - 1, Number(tmp_dateFrom[2]));	

	var tmp_dateTo = $j('[name=calendar_to_data]').val().split(',');		
	var date_to = new Date(Number(tmp_dateTo[0]), Number(tmp_dateTo[1]) - 1, Number(tmp_dateTo[2]));	
	
	var today = new Date();
	var date_today = new Date(today.getFullYear(), today.getMonth(), today.getDate());

	if (date_from.getTime() == date_today.getTime())
	{
		info = info + $mconfig['mess'][3];
		inf = true;
	}

	var dni = (date_to.getTime() - date_from.getTime()) / (24*60*60) / 1000;
	
	if (date_from.getTime() >= date_to.getTime() || date_from.getTime() < 0 || date_to.getTime() < 0) {
		warning = warning + $mconfig['mess'][4];
		neg = true;
		date__ = false;
	}
	else if (dni < $mconfig['min_time'])
	{
		warning = warning + $mconfig['mess'][5];
		neg = true;
	}

	if (r.find(':checkbox[name=reg]').attr('checked') == false)
	{
		warning = warning + $mconfig['mess'][6];
		neg = true;
	}

	if (r.find('[name=security]').val().length < 6)
	{
		warning = warning + $mconfig['mess'][2];
		neg = true;
	}

	if (neg)
	{
		alert(warning);
		return false;
	}
	else
	{
		if (inf)
		{
			alert(info);
		}

		r.submit();
	}
}

function pay_chk()
{
	var r = $j('form#rez_form');
	var neg = false;
	var typ = false;

	var warning = '';

	if (r.find(':checkbox[name=reg]').attr('checked') == false)
	{
		warning = $mconfig['mess'][6];
		neg = true;
	}

	r.find('[name=typ]').each(function (i)
	{
		if (this.checked)
		{
			typ = true;
		}
	});

	if (!typ)
	{
		warning = warning + $mconfig['mess'][7];
		neg = true;
	}

	if (neg)
	{
		alert(warning);
		return false;
	}
	else
	{
		r.submit();
	}
}

$j(function($)
{

	// fkolo
	days_ = '<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option><option value="14">14</option><option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option>';
	
    $("#day_search_from").html(days_);		
    $("#day_search_to").html(days_);

    $("#day_from").html(days_);		
    $("#day_to").html(days_);	
	
	var months_year = '';
	var curr_date = new Date();
	var curr_m = curr_date.getMonth();
	var curr_y = curr_date.getFullYear();
	for (var i=1; i<=12; i++){
		if (curr_m == 12) {
			curr_m = 0;
			curr_y += 1;
		}
		months_year += '<option value="' + curr_y + ',' + eval(curr_m+1) + '">' + lang.months.long[curr_m] + ' ' + curr_y + '</option>';
		curr_m += 1;
	}
	
	if (used_dates != undefined)
		for(var k=0; k < used_dates.length; k++){
			var tmp_used_date = new Date(used_dates[k]);
			used_dates_stamp[k] = tmp_used_date.getTime();
		}
	
	used_dates_stamp_string = used_dates_stamp.join(' ');
	//alert(used_dates_stamp_string);	
	
	var curr_m = date_from.getMonth();
	var curr_y = date_from.getFullYear();	

	var y = curr_y,
		m = curr_m+1,
		leap = y % 400 === 0 || y % 100 !== 0 && y % 4 === 0 ? true : false,
		days = 30;

	switch ( m ) {
		case 1: case 3: case 5: case 7: case 8: case 10: case 12:
		days = 31; break;
		case 2: 
		days = leap ? 29 : 28; break; 
	}
	
	var opts = $('#day_from option').get();

	$('#day_from').empty().append('<option value="-1"> </option>').append( opts.slice(0, days) );

	$('#day_from option').each(function(){
		if ($(this).val() != -1){
			var date_off = new Date(curr_y, curr_m, Number($(this).val()));
			if(used_dates_stamp_string.indexOf(date_off.getTime()) != -1) $(this).attr('disabled', true);
		}
	});	
	
	date_to = new Date(date_to);
	var curr_m = date_to.getMonth();
	var curr_y = date_to.getFullYear();	

	var y = curr_y,
		m = curr_m+1,
		leap = y % 400 === 0 || y % 100 !== 0 && y % 4 === 0 ? true : false,
		days = 30;

	switch ( m ) {
		case 1: case 3: case 5: case 7: case 8: case 10: case 12:
		days = 31; break;
		case 2: 
		days = leap ? 29 : 28; break; 
	}
	
	var opts = $('#day_to option').get();

	$('#day_to').empty().append('<option value="-1"> </option>').append( opts.slice(0, days) );

	$('#day_to option').each(function(){
		if ($(this).val() != -1){
			var date_off = new Date(curr_y, curr_m, Number($(this).val()));
			if(used_dates_stamp_string.indexOf(date_off.getTime()) != -1) $(this).attr('disabled', true);
		}
	});	
	
	//selected="selected"
    $("#month_search_from").html(months_year);		
    $("#month_search_to").html(months_year);	

    $("#month_from").html(months_year);		
    $("#month_to").html(months_year);		
	
	curr_date = date_from;
	
	$('#search_from_data').val(curr_date.getFullYear() + ',' + eval(curr_date.getMonth()+1) + ',' + curr_date.getDate());
	$('#day_search_from').val(curr_date.getDate()).attr('selected',true);
	$('#month_search_from').val(curr_date.getFullYear() + ',' + eval(curr_date.getMonth()+1)).attr('selected',true);

	$('#calendar_from_data').val(curr_date.getFullYear() + ',' + eval(curr_date.getMonth()+1) + ',' + curr_date.getDate());
	$('#day_from option').each(function(i){
		if($(this).val() == curr_date.getDate() && $(this).is(':disabled')){
			$('#day_from').val(-1).attr('selected',true);
			return;
		}else if($(this).val() == curr_date.getDate()){
			$('#day_from').val(curr_date.getDate()).attr('selected',true);
			return;		
		}

	});

	$('#month_from').val(curr_date.getFullYear() + ',' + eval(curr_date.getMonth()+1)).attr('selected',true);		
	
	curr_date = date_to;
	$('#search_to_data').val(curr_date.getFullYear() + ',' + eval(curr_date.getMonth()+1) + ',' + curr_date.getDate());
	$('#day_search_to').val(curr_date.getDate()).attr('selected',true);
	$('#month_search_to').val(curr_date.getFullYear() + ',' + eval(curr_date.getMonth()+1)).attr('selected',true);

	$('#calendar_to_data').val(curr_date.getFullYear() + ',' + eval(curr_date.getMonth()+1) + ',' + curr_date.getDate());
	$('#day_to option').each(function(i){
		if($(this).val() == curr_date.getDate() && $(this).is(':disabled')){
			$('#day_to').val(-1).attr('selected',true);
			return;
		}else if($(this).val() == curr_date.getDate()){
			$('#day_to').val(curr_date.getDate()).attr('selected',true);
			return;		
		}

	});
	$('#month_to').val(curr_date.getFullYear() + ',' + eval(curr_date.getMonth()+1)).attr('selected',true);	
	
	$("#day_search_from, #month_search_from, #day_search_to, #month_search_to").change(function(){
		var df = $('#day_search_from').val();
		var myf = $('#month_search_from').val();
		var dt = $('#day_search_to').val();
		var myt = $('#month_search_to').val();		
		$('#search_from_data').val(myf + ',' + df);
		$('#search_to_data').val(myt + ',' + dt);
	});

	$("#day_from").change(function(){
		var df = $('#day_from').val();
		var myf = $('#month_from').val();
		$('#calendar_from_data').val(myf + ',' + df);
	});	
	
	curr_m = (curr_m == 12) ? 1 : curr_m+1;	
	
	$("#month_from").change(function(){

		var y = Number($('#month_from').val().split(',')[0]),
			m = Number($('#month_from').val().split(',')[1]),
			leap = y % 400 === 0 || y % 100 !== 0 && y % 4 === 0 ? true : false,
			days = 30;

		switch ( m ) {
			case 1: case 3: case 5: case 7: case 8: case 10: case 12:
				days = 31; break;
			case 2: 
				days = leap ? 29 : 28; break; 
		}
	
		$("#day_from").html(days_);			
		var opts = $('#day_from option').get();

		$('#day_from').empty().append('<option value="-1"> </option>').append( opts.slice(0, days) );
		var df = $('#day_from').val();
		var myf = $('#month_from').val();
		
		var tmp_my = $('#month_from').val().split(',');		
		$('#day_from option').each(function(){
			if ($(this).val() != -1){
				var date_off = new Date(Number(tmp_my[0]), Number(tmp_my[1]) - 1, Number($(this).val()));
				if(used_dates_stamp_string.indexOf(date_off.getTime()) != -1) $(this).attr('disabled', true);
			}else{
				$(this).attr('selected', true);
			}
		});
		
		$('#calendar_from_data').val(myf + ',' + df);
	});		

	$("#day_to").change(function(){
	
		var dt = $('#day_to').val();
		var myt = $('#month_to').val();		
		$('#calendar_to_data').val(myt + ',' + dt);
	});	

	$("#month_to").change(function(){

		var y = Number($('#month_to').val().split(',')[0]),
			m = Number($('#month_to').val().split(',')[1]),
			leap = y % 400 === 0 || y % 100 !== 0 && y % 4 === 0 ? true : false,
			days = 30;

		switch ( m ) {
			case 1: case 3: case 5: case 7: case 8: case 10: case 12:
				days = 31; break;
			case 2: 
				days = leap ? 29 : 28; break; 
		}
	
		$("#day_to").html(days_);			
		var opts = $('#day_to option').get();

		$('#day_to').empty().append('<option value="-1"> </option>').append( opts.slice(0, days) );
	
		var dt = $('#day_to').val();
		var myt = $('#month_to').val();	

		var tmp_my = $('#month_to').val().split(',');		
		$('#day_to option').each(function(){
			if ($(this).val() != -1){
				var date_off = new Date(Number(tmp_my[0]), Number(tmp_my[1]) - 1, Number($(this).val()));
				if(used_dates_stamp_string.indexOf(date_off.getTime()) != -1) $(this).attr('disabled', true);
			}else{
				$(this).attr('selected', true);
			}
		});
		
		$('#calendar_to_data').val(myt + ',' + dt);
	});	
	
	if ($j('input[name=przyjazd]').val() != undefined){
		var tmp_date_ = $j('input[name=przyjazd]').val().split('-');
		var przyjazd_date = new Date(Number(tmp_date_[0]), Number(eval(Number(tmp_date_[1]) - 1)), Number(tmp_date_[2])).getTime();
		var today_ = new Date();
		var today__ = new Date(today_.getFullYear(), today_.getMonth(), today_.getDate()).getTime();
		if(przyjazd_date == today__){
			$j('input[name=typ]').each(function(){
				if( $j(this).val() == 2 ) $j(this).parent().parent().remove();
			});
		}
		
	};	
	
	// END fkolo
	
	/* Init */
	$mconfig['init'].each(function (i)
	{
		eval(i + '(true)');
	});

	$('#s_kraj').change(function ()
	{
		search();
	});
	
	$('#s_miasto').change(function ()
	{
		search_district();
	});	

	$('#vat').change(function ()
	{
		vat_rows();
	});

	$('form#rez_form select#apartament').change(function ()
	{
		var ap_id = $('form#rez_form select#apartament').val();
		var res = $('form#rez_form');

		if (ap_id > 0)
		{
			res.attr('action', $mconfig['urls']['res'].replace('%s', ',id,'+ap_id));
		}
		else
		{
			res.attr('action', $mconfig['urls']['res'].replace('%s', ''));
		}

		res.submit();
	});

	$('form#rez_form select[name*=godz]').change(function ()
	{
		if (this.name == 'godz[1]')
		{
			var min = $('form#rez_form #min1');
		}
		else if (this.name == 'godz[3]')
		{
			var min = $('form#rez_form #min2');
		}

		min.removeOption(/./);

		if (this.value == '12' || this.value == '23')
		{
			min.addOption('00', '00', false);
		}
		else
		{
			min.addOption('00', '00', false);
			min.addOption('15', '15', false);
			min.addOption('30', '30', false);
			min.addOption('45', '45', false);
		}
	});

	$('form#pay_form select#pay').change(function ()
	{
		amount_chk();
	});

    $('input.search_btn').click(function ()
    {
        document.location = $(this).attr('rel');
    });
	
});
