/*COMMON*/
Date.firstDayOfWeek = 1;
Date.format = 'yyyy-mm-dd';
var tb_pathToImage = "images/ajax-loader-big.gif";
var thisDay = new Date();
var d1 = new Date(); d1.zeroTime();
var d2 = new Date(); d2.zeroTime(); d2.addDays(1);

function noenter() {
	return !(window.event && window.event.keyCode == 13);
}
function formatItem(row) {
	return "<strong>" + row[1] + "</strong><br /><span class=\"ac_kraj\">" + row[2] + "</span>";
}
function processCityNames() {
	var fc = document.afpForm.fromcity.value;
	if(fc.length > 3) document.afpForm.fromcity.value = fc.substr(parseInt(fc.length-4),3);
	var tc = document.afpForm.tocity.value;
	if(tc.length > 3) document.afpForm.tocity.value = tc.substr(parseInt(tc.length-4),3);
	return true;
}
function progress() {
	if( document.afpForm.fromcity1.disabled ) {
		document.afpForm.fromcity.value = document.afpForm.fromcity2.value;
	} else {
		document.afpForm.fromcity.value = document.afpForm.fromcity1.value;
	}
	processCityNames();
	//document.afpForm.action="qsf.php";
	//document.afpForm.method="get";
	document.afpForm.submit();
	return false;
}
function evalTrip(AgentId,Sprzedawca,language)	{
	if (document.afpForm.triptype.selectedIndex == 2) {
		processCityNames();
		var redir = (language==false||language=='pl')
			? "podroz-wieloodcinkowa.html?agentid="+AgentId+"&triptype=MULTIPLE&do_multiprefs=yes&sysentrypoint=porch&command=flights&path=add/Progress&outhour=8&rethour=8&meta_do=do_power&externalprefs=Y&leg1fromcity="+document.afpForm.fromcity.value+"&leg1tocity="+document.afpForm.tocity.value+"&loclanguage=pl&udf_I_mb_partner_id="+Sprzedawca
			: "multileg-trip.html?agentid="+AgentId+"&triptype=MULTIPLE&do_multiprefs=yes&sysentrypoint=porch&command=flights&path=add/Progress&outhour=8&rethour=8&meta_do=do_power&externalprefs=Y&loclanguage=en&leg1fromcity="+document.afpForm.fromcity.value+"&leg1tocity="+document.afpForm.tocity.value+"&loccurrency=EUR&udf_I_mb_partner_id="+Sprzedawca;
		window.top.location.href = redir;
	}
}
function selectItem(li) {
	/* if (li.extra) {	alert("That's '" + li.extra[2] + "' you picked."); } */
	return false;
}
var	dateRender = function($td, thisDate, month, year)
{
	//clear loading animation - this shouldn't be here
	//$('input').removeClass('ac_loading');
	//mark weekends
	if (thisDate.isWeekend()){$td.addClass('weekend');}
	//mark 3 disabled days from today and attach a tip for each
	if(thisDate.asString()==d1.asString()||thisDate.asString()==d2.asString())
	{
		$td.addClass('blocked').unbind().click(function(){
			var b = $("#dp-popup").offset();
			var a = $td.offset();
			var pl = a.left - b.left; var pt = a.top - b.top;
			$('#dp-tip').empty().remove();
			$('#dp-popup').append('<div id="dp-tip">'+dp_info+'</div>');
			$('#dp-tip')
				//.css({'position':'absolute','top':pt+10,'left':pl+10,'width':$("#dp-popup").width()})
				.css({'position':'absolute','top':5,'left':-5,'width':$("#dp-popup").width()})
				.click(function(){
					$(this).empty().remove();
				});
		});
	}
}
$(document).ready(function(){
	$('#fromcity2,#tocity').autocomplete('ac_qsf.php',{
		delay:100,
		minChars:3,
		matchSubset:1,
		matchContains:0,
		cacheLength:10,
		autoFill:true,
		maxItemsToShow:8,
		width:150,
		formatItem:formatItem,
		onItemSelect:selectItem
	});
	//load calendar translation
	$.dpText = dp_text;
	//make calendar
	$('#outdate,#retdate').datePicker({
		clickInput: false,
		displayClose: true,
		renderCallback: dateRender
	}).dpSetOffset(-20, 0);
	//set current date to calendar
	$('#outdate,#retdate').each(function(){
		$(this).dpSetSelected($(this).val())
	});
	//always set current month as first one
	var rd = Date.fromString($('#retdate').val()).getMonth();
	var tm = thisDay.getMonth();
	if(tm!=rd){
		$('#retdate').dpSetDisplayedMonth(tm);
	}
	//overwrite default dp-choose-date tag
	$('a.dp-choose-date')
		.html('<img src="images/calendar2.gif" alt="'+dp_calendar_select+'" title="'+dp_calendar_select+'" width="18" height="17" hspace="2" align="absmiddle" border="0" />');
		//.focus(function(){$(this).prev().addClass('ac_loading');}
	//);
});