/*COMMON*/
/* Copyright (c) 2006 Brandon Aaron (http://brandonaaron.net) * Version 2.1.1 */
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(5($){$.i.Z=$.i.f=5(s){l($.T.R&&/6.0/.E(B.x)){s=$.r({b:\'3\',c:\'3\',7:\'3\',8:\'3\',k:H,e:\'D:h;\'},s||{});A a=5(n){g n&&n.q==t?n+\'4\':n},p=\'<o S="f"P="0"O="-1"e="\'+s.e+\'"\'+\'N="M:L;K:I;z-G:-1;\'+(s.k!==h?\'F:J(C=\\\'0\\\');\':\'\')+\'b:\'+(s.b==\'3\'?\'9(((j(2.d.m.y)||0)*-1)+\\\'4\\\')\':a(s.b))+\';\'+\'c:\'+(s.c==\'3\'?\'9(((j(2.d.m.Q)||0)*-1)+\\\'4\\\')\':a(s.c))+\';\'+\'7:\'+(s.7==\'3\'?\'9(2.d.w+\\\'4\\\')\':a(s.7))+\';\'+\'8:\'+(s.8==\'3\'?\'9(2.d.v+\\\'4\\\')\':a(s.8))+\';\'+\'"/>\';g 2.u(5(){l($(\'> o.f\',2).U==0)2.V(W.X(p),2.Y)})}g 2}})(10);',62,63,'||this|auto|px|function||width|height|expression||top|left|parentNode|src|bgiframe|return|false|fn|parseInt|opacity|if|currentStyle||iframe|html|constructor|extend||Number|each|offsetHeight|offsetWidth|userAgent|borderTopWidth||var|navigator|Opacity|javascript|test|filter|index|true|absolute|Alpha|position|block|display|style|tabindex|frameborder|borderLeftWidth|msie|class|browser|length|insertBefore|document|createElement|firstChild|bgIframe|jQuery'.split('|'),0,{}))



function dateToArray(string) {
	var s = string.split('-');
	return new Array(parseInt(s[0],10),(parseInt(s[1],10)),parseInt(s[2]));
}
function leadZero(n) {
	return (parseInt(n,10)<10) ? "0"+n : n;
}
function futureDate(value) {
	var d = new Date();
	d.setMilliseconds(d.getMilliseconds() + parseInt(value*86400000,10));
	return leadZero(d.getDate())+'-'+leadZero(d.getMonth()+1)+'-'+d.getFullYear();
}


function selectAirportBack(code, field) {
	$('#'+field).val(code);
}
function selectCityBack(code, city){
	$('#city').val(city);
	$('input[name="autoSelect"]').val("true");
	$('input[name="cityCode"]').val(code);
	closeCitySelct();
}
function closeCitySelct() {
	window.close();
}


function noenter() {
	return !(window.event && window.event.keyCode == 13);
}

function processCityNames() {

	$('#qsfFlightsForm').submit();
}
function calculateNights(ret_nights) {
	var cin = dateToArray($('#cindate').val());
	var cinDate = new Date(cin[2],(cin[1]-1),cin[0]);

	var cot = dateToArray($('#cotdate').val());
	var cotDate = new Date(cot[2],(cot[1]-1),cot[0]);

	var dif = new Date();
	dif.setTime(cotDate.getTime()-cinDate.getTime());
	var nights = dif.getTime()/(86400*1000);
	if(ret_nights) {
		return nights;
	} else {
		return (nights>=1&&nights<=90) ? true : false ;
	}
}

function processCarsForm() {
	var re = new RegExp("^[0-9]{2}-[0-9]{2}-[0-9]{4}$","i");
	var pupdate = $('#tmp_pupdate').val();
	var doffdate = $('#tmp_doffdate').val();

	if($('#pupcity').val()==''||$('#doffcity').val()=='') {
		alert(txt_select_city);
		return false;
	} else if(re.test(pupdate)==false||pupdate=='') {
		alert(txt_select_cindate);
		return false;
	} else if(re.test(doffdate)==false||doffdate=='') {
		alert(txt_select_cotdate);
		return false;
	} else {
		var pup = dateToArray(pupdate);
		$('input[name="pupday"]').val(pup[0]);
		$('input[name="pupmonth"]').val(pup[1]-1);
		var doff = dateToArray(doffdate);
		$('input[name="doffday"]').val(doff[0]);
		$('input[name="doffmonth"]').val(doff[1]-1);

		$('#qsfCarsForm').submit();
	}
}

function evalTrip(AgentId,Sprzedawca,language) {
	return false;
}
var evaltrip = evalTrip;

/**
 * Special function for spliting form element name/id
 * into 2 pieces: name (letters) and number (digits).
 * This function is used by hotels/flights ASF
 */
function splitValue(value) {
	if(typeof value != "string") value = value.toString();
	var objectName = value.match(/(\.|\#)?[\/\?\.\:=a-z_]+/g);
	var objectId = value.match(/\d+/g);
	return {
		name:objectName,
		id:objectId,
		array:( objectId != parseInt(objectId) && objectId != null )?true:false
	}
};
function FindId(value) {
	if(typeof value != "string") value = value.toString();
	return {
		string:value,
		name:value.match(/^[\w_\-]+[^\d]/g)[0],
		id:value.match(/\d+$/g)[0]
	}
};

/* ---------- JQUERY EXTENSIONS ---------- */
jQuery.fn.extend({
	fieldNumberControl: function(o,i) {
		$this = $(this);
		for(f in o) {
			$(f,$this).each(function(){
				var attr = o[f].split(',');
				for (a in attr) {
					if (typeof $(this).attr(attr[a]) != 'undefined') {
						var attrFullName = $(this).attr(attr[a]);
						var attrSplitted = splitValue(attrFullName);
						var newAttrValue = (attrSplitted.name == null) ? parseInt(attrSplitted.id) + i : attrSplitted.name + parseInt(parseInt(attrSplitted.id) + i);
						$(this).attr(attr[a], newAttrValue);
					}
				}
			});
		}
		return $this;
	},
	fieldReset: function(o) {
		$this = $(this);
		var fields = o.split(',');
		for(f in fields) {
			switch(fields[f]) {
				case 'input':
				case 'select':
					$(fields[f],$this).val('');
				break;
				case 'label':
					$(fields[f],$this).text('').removeClass(cl_Error);
				break;
			}
		}
		return $this;
	}
});


$(document).ready(function(){

	// bof: przelaczanie tabow
	var tabs = [];
	var tabContainers = [];
	$('#eskynav a').each(function () {
		tabs.push(this);
		tabContainers.push($(this.hash).get(0));
	});
	$(tabs).click(function() {
		$(tabContainers).hide().filter(this.hash).show();
		return false;
	});
	// eof: przelaczanie tabow

	var removeVirgin = function(){
		if ($(this).val() == 'RRRR-MM-DD') {
			$(this).val('');
		}
		$(this).removeClass('virgin');
	};
	$('#outdate,#cindate,#retdate,#cotdate').val('RRRR-MM-DD').addClass('virgin').focus(removeVirgin);

	$('#triptype').change(function(){
		$retdate = $('#retdate');
		switch($(this).val()){
			case 'ONEWAY':
			$('#flights-dates-ret').hide();
			$('#retdate').focus().attr('disabled','disabled');
			break;

			case 'ROUNDTRIP':
			$('#retdate').removeAttr('disabled');
			$('#flights-dates-ret').show();
			break;

			case 'MULTIPLE':
			break;
		}
	});

	//initialize qsf: show form only for selected tab
	var hash_raw = window.location.hash;
	var hash = (hash_raw) ? hash_raw.substr(1,parseInt(hash_raw.length)) : $('.qsfBox:first').attr('id'); //'qsfFlights';

	$('.qsfBox,.set-hiden').hide();
	$('a[rel='+hash+']').parent().addClass('selected');
	$('#'+hash).show();

	$('#qsf-tabs li a').each(function(){
		$(this).click(function(){
			$('#qsf-tabs li').removeClass('selected');
			$(this).parent().addClass('selected');
			$('.qsfBox').hide();

			var rel = $(this).attr('rel');
			$('#'+rel).show();

			return false;
		});
	});

	/**
	 * Control of holel room numbers, and children age
	 */
	$.fn.childrenAgeSelect = childrenAgeSelect = function(e){
		$parent = $(this).parent('label');
		$parent.next('.age').remove();
		if(typeof (index = $parent.parent('li').data('index')) === 'undefined'){
			index = 1;
		}
		var children = $(this).val();
		if (children != 0) {
			var select = '';
			for (i = 0; i < children; i++) {
				select += '<select name="hotnumchildrenage['+index+']['+i+']" class="children-age"><option>-?-</option>';
				for (j = 0; j < 18; j++) {
					select += '<option value="' + j + '">' + j + '</option>';
				}
				select += '</select>';
			}
			var label = '<div class="label age"><span>Wiek dzieci *&nbsp;</span>' + select + '</div>';
			$parent.after(label);
		}
		if($(".label.age").size()>0) {
			$("#children-age-info").removeClass('hidden').show();
		} else {
			$("#children-age-info").hide();
		}
	};
	$('#hotel-rooms-select select').change(function(){
		$rooms = $('#hotel-rooms li');
		$rooms.not(':first').remove();
		var i = 1;
		while ($('#hotel-rooms li').size() < $(this).val()) {
			$('#hotel-rooms li:first').clone().insertAfter('#hotel-rooms li:last');
			$('#hotel-rooms li:last').fieldReset('select').fieldNumberControl({
				'select':'name'
			},i++).data('index',i).find('strong').html('Pokój <em>'+i+'</em>');
			$('#hotel-rooms li:last').find('.age').remove();
		}
		$('.children select').change(childrenAgeSelect); // IE8 jQuery.fn.live fix
	}).change();

	$('.children select').live("change",childrenAgeSelect).each(function(){
		$(this).childrenAgeSelect(); /* Does not work with .change() */
	});

	$('a.pop-calendar').click(function(){
		var $this = $(this);
		$this.esky_calendar({thisInput:$this.attr('rel'),linkedInput:$this.attr('rev'),loop:2});
		var offsets = {
			'left':parseInt($('.qsfBox').width()/2) - parseInt($('#esky_calendar').width() / 2),
			'top':parseInt($('.qsfBox').height()/2) - parseInt($('#esky_calendar').height() / 2)
		}
		$('#esky_calendar').css({
			top : (offsets.top>0) ? offsets.top : 0,
			left : offsets.left
		});
		return false;
	});
	$('a.pop-cities').click(function(){
		var qsfCityPopup = window.open($(this).attr('href'),'qsfCityPopup',"height=350,width=300,scrollbars=yes");
		return false;
	});
	$('#city').change(function(){
				$('input[name="autoSelect"]').val('false');
				$('input[name="cityCode"]').val('');
	});
});