var formsParams = {
	search: {
		formInit: function() {
			if (document.forms['search_normal'] && document.forms['search_normal'].children)
				changeChilds(document.forms['search_normal'].children.value);
			if (document.forms['search_promo'] && document.forms['search_promo'].children)
				changeChilds(document.forms['search_promo'].children.value);
			if (document.forms['search_multi'])
			{
				multiBlockInit();
				if (document.forms['search_multi'].children)
					changeChilds(document.forms['search_multi'].children.value);
			}
		},
		radioChecked: 'http://bilety.pasazer.com/static/img/main/search_radio_on.gif',
		radioUnchecked: 'http://bilety.pasazer.com/static/img/main/search_radio_off.gif',
		checkboxChecked: 'http://bilety.pasazer.com/static/img/main/search_check_on.gif',
		checkboxUnchecked: 'http://bilety.pasazer.com/static/img/main/search_check_off.gif',
		helperClass: 'help',
		helperElementClass: 'helpbox',
		helperElementId: 'search_helper',
		helperTrackClass: 'help',
		autoCompleteHelperClass: 'autocomplete',
		autoCompleteClass: 'autocomplete',
		autoCompleteMinChar: 2,
		autoCompleteInterval: 500,
		autoCompleteUrl: '/proxy/index.php?z=http%3A%2F%2Fbilety.pasazer.com%2Fflight%2Fatlas%2Fcityhelper%2Fnazwa%2F:value:%2Finput%2F:name:%2Fid%2F:id:%2F',
		autoCompleteOnload: function(){if(document.getElementById(this.id+'_code')) {document.getElementById(this.id+'_code').value='';}},
		calendarClass: 'cal',
		calendarMonths: new Array('styczeń', 'luty', 'marzec', 'kwiecień', 'maj', 'czerwiec', 'lipiec', 'sierpień', 'wrzesień', 'październik', 'listopad', 'grudzień'),
		calendarDays: new Array('pn', 'wt', 'śr', 'cz', 'pt', 'so', 'nd'),
		calendarClose: 'zamknij',
		calendarMonthsCount: 2,
		calendarOnInit: function() {
			var d = new Date();
			if (matches = this.focusElements[0].id.match(/([a-z]+)([0-9]+)/i)) {
				var calNo = new Number(matches[2]) - 1;
				var inputId = matches[1];
				if (calNo == 0)
					calNo = '';
				if (FSite2.calendars[inputId + calNo + '_calendar'] && FSite2.calendars[inputId + calNo + '_calendar'].minDate)
					d = new Date(FSite2.calendars[inputId + calNo + '_calendar'].getDate());
			} 
			//d.setDate(d.getDate() + 30);
			if (this.focusElements.length && this.focusElements[0].value)
				this.setDate(Date.parseString(this.focusElements[0].value), true);
			else
				this.setDate(d);
			this.setMinDate(d, this.focusElements[0].value?true:false);
			d = new Date();
			d.setFullYear(d.getFullYear() + 2);
			this.setMaxDate(d, this.focusElements[0].value?true:false);
			this.focusElements[0].onchange = function() {
				this.calendarObject.setDate(Date.parseString(this.value));
			}
		},
		calendarOnChange: function() {
			if (this.focusElements.length) {
				this.focusElements[0].value = this.getDate().getFormatted();
				var matches;
				var inputId = this.focusElements[0].id;
				var calNo = ''; 
				if (matches = inputId.match(/([a-z]+)([0-9]+)/i)) {
					calNo = matches[2];
					inputId = matches[1];
				}
				calNo = new Number(calNo) + 1;
				while (FSite2.calendars[inputId + calNo + '_calendar']) {
					FSite2.calendars[inputId + calNo + '_calendar'].setMinDate(Date.parseString(this.focusElements[0].value));
					calNo++;
				}
			}
		}
	},
	form: {
		radioChecked: '/img/form/form_radio_on.gif',
		radioUnchecked: '/img/form/form_radio_off.gif',
		checkboxChecked: '/img/form/form_check_on.gif',
		checkboxUnchecked: '/img/form/form_check_off.gif',
		disabledRadioChecked: '/img/form/blocked_radio_on.gif',
		disabledRadioUnchecked: '/img/form/blocked_radio_off.gif',
		disabledCheckboxChecked: '/img/form/blocked_check_on.gif',
		disabledCheckboxUnchecked: '/img/form/blocked_check_off.gif',
		selectDisabledClass: 'blocked',
		helperClass: 'help',
		helperElementClass: 'helpbox',
		helperElementId: 'results_helper',
		helperTrackClass: 'help'
	},
	results: {
		helperClass: 'help',
		helperElementClass: 'helpbox',
		helperElementId: 'results_helper',
		helperTrackClass: 'help'
	},
	login: {
		helperClass: 'help',
		helperElementClass: 'helpbox',
		helperElementId: 'results_helper',
		helperTrackClass: 'help'
	}
}

var multiBlockCount = 0;

function multiBlockInit() {
	multiBlockCount = getMultiBlockCount('search_block','multi_block_container');
	multiBlockUpdate(4);
}

function getMultiBlockCount(classBlock, containerID) {
	var multiBlocks = FSite2.getElementsByClassName(classBlock, document.getElementById(containerID));
	blockVisibility=0;
	for (i = 0; i < multiBlocks.length; i++)
	{
		if (multiBlocks[i].style.display!='none')
			blockVisibility++;
	}
	return blockVisibility;
}

function multiBlockUpdate(maximumCount) {
	var i, j;
	var multiBlocks = FSite2.getElementsByClassName('search_block', document.getElementById('multi_block_container'));
	for (i = 0; i < multiBlocks.length; i++)
	{
		var dis;
		if (i < multiBlockCount)
		{
			multiBlocks[i].style.display = '';
			dis = false;
		}
		else
		{
			multiBlocks[i].style.display = 'none';
			dis = true;
		}
		var inputs = multiBlocks[i].getElementsByTagName('input');
		for (j = 0; j < inputs.length; j++)
			inputs[j].disabled = dis;
	}
	var addElements = FSite2.getElementsByClassName('multi_add');
	for (i = 0; i < addElements.length; i++) {
		if (multiBlockCount >= maximumCount)
			addElements[i].style.display = 'none';
		else
			addElements[i].style.display = '';
	}
	var removeElements = FSite2.getElementsByClassName('multi_remove');
	for (i = 0; i < removeElements.length; i++) {
		if (multiBlockCount <= 1)
			removeElements[i].style.display = 'none';
		else
			removeElements[i].style.display = '';
	}
}

function multiBlockAdd(maximumCount) {
	if (multiBlockCount < maximumCount) {
		multiBlockCount++;
		multiBlockUpdate(maximumCount);
	}
}

function multiBlockRemove(maximumCount) {
	if (multiBlockCount > 1) {
		multiBlockCount--;
		multiBlockUpdate(maximumCount);
	}
}

function openCalendar(anchorId)
{
	var matches = anchorId.match(/button([0-9]+)_calendar/i);
	var calNo = '';
	if (matches && matches[1])
		calNo = matches[1];
	document.search_multi['calendar' + calNo].focus();
}

function initForm(containerId, paramsBlock) {
	var cont = document.getElementById(containerId);
	if (cont) {
		if (formsParams[paramsBlock]) {
			FSite2.extendForm(cont, formsParams[paramsBlock]);
			if (typeof formsParams[paramsBlock]['formInit'] == 'function')
				formsParams[paramsBlock].formInit();
		}
		cont.style.display = '';
		if (document.getElementById('searchloader')) document.getElementById('searchloader').style.display='none';
		setTimeout(FSite2._callRef(function(cont){cont.style.visibility = '';},cont),100);
	}
}

function initForms() {
	for (containerId in formsParams)
		initForm(containerId, containerId);
}

function loadSearcher(url, containerId) {
	document.getElementById(containerId).style.display = 'none';
	if (document.getElementById('searchloader'))
		document.getElementById('searchloader').style.display='block';
	FSite2.removeCalendars(document.getElementById(containerId));
//	document.getElementById('search').innerHTML = '';
	var req = new FSite2.HTTPRequest(url, function() {
		document.getElementById(containerId).innerHTML = this.httpRequest.responseText;
		initForm(containerId, 'search');
	});
}

function changeChilds(counter) {
	for (i = 0; i < 9; i++) {
		var container = document.getElementById('cont_child' + i);
		if (i < counter) {
			container.style.display = '';
			selects=container.getElementsByTagName('select');
			for (j=0;j<selects.length;j++)
				selects[j].disabled=false;
		} else {
			container.style.display = 'none';
			selects=container.getElementsByTagName('select');
			for (j=0;j<selects.length;j++)
				selects[j].disabled=true;
		}
	}
	document.getElementById('childcont').style.display = (counter > 0)?'block':'none';
}

function clearValue(code_id) {
	//if (code_id.id)
	//{
//		input=document.getElementById(code_id.id+'_code');
		//input.value='';
	//}
}

function resultSearch(f,cont) {
	if (!f.action) return false;

	if (!cont) cont='search'
	if (!document.getElementById(cont) && document.getElementById('search'))
		cont='search';
	if (!document.getElementById(cont))
		return false;

	var d = new Date();
	var w = new Date();
	var c = f.data_wylotu.calendarObject;
	d.setDate( d.getDate()+ 1 )

	var status = true;
	if( c.getDate() < d )
		status = confirm( '***UWAGA*** Rezerwacja wylotu w dniach ' + w.getDate() + '.' + (w.getMonth()+1) + ' lub ' + d.getDate() + '.' + (d.getMonth()+1) + '.' + d.getFullYear() + "\n" + "jest możliwa tylko przy płatności kartą kredytową" );
	else
		status=true;

	if (status)
	{
		document.getElementById(cont).style.display = 'none';
		document.getElementById('searchloader').style.display='block';
		
		FSite2.sendForm(f,function(){
				if (this.httpRequest.getResponseHeader('X-Form-Errors'))
				{
					document.getElementById(cont).style.display = 'none';
					FSite2.removeCalendars(document.getElementById(cont));
					document.getElementById(cont).innerHTML=this.httpRequest.responseText
					if (document.getElementById('search_layer'))
					{
						frm=document.getElementById(cont).getElementsByTagName('form')[0];
						divs=frm.getElementsByTagName('div');
						frm.removeChild(divs[divs.length-1]);
						
					}
					initForm(cont, 'search');
				}
				else
				{
					eval(this.httpRequest.responseText);
				}
		},true,f.action);
	}
}

function resultNewsletter(f,cont) {
	if (!f.action) return false;
	href=f.action;

	if (!cont) cont='layout_bottom_newsletter'
	if (!document.getElementById(cont)) return false;

	FSite2.sendForm(f,function(){
			document.getElementById(cont).innerHTML=this.httpRequest.responseText;
			initForm(cont, 'search');
			if (!this.httpRequest.getResponseHeader('X-Form-Errors'))
			{
				setTimeout(function(){
						loadNewsletter(cont,href);
					},3000);
			}
	},true,f.action);
}

function loadNewsletter(cont,href)
{
	FSite2.HTTPRequest(href, function(){
			document.getElementById(cont).innerHTML=this.httpRequest.responseText;
	});
}

function ambiguous(form)
{
 input=form.getElementsByTagName('input');
 nocheck=false;
 for (i=0;i<input.length;i++)
 {
 	if (input[i].checked)
 	{
 		input[i].onclick();
 		nocheck=true;
 	}
 }
 if (nocheck)
 	form.submit();
 else if (form.alert)
 	alert(form.alert.value);
}

function showMaps(pole)
 {
	window._atlasbg = new FSite2.Layer('background');
	window._atlasbg.fullScreen();
	window._atlas = new FSite2.Layer('mapLayer',false,'flashmaps');

	so = new SWFObject('http://bilety.pasazer.com/static/img/atlas.swf', 'flashmaps', 650, 390, 8, '#000000');
	so.addParam("quality", "high");
	so.addParam("AllowScriptAccess", "always");

	so.addVariable("tabType", "flight");
	so.addVariable("domena", "http://bilety.pasazer.com/flight/atlas/xml/");
	so.addVariable("atlasid", pole);
	for(var j in AtlasColor)
		so.addVariable(j.replace('c_','kolor_'), AtlasColor[j].slice(1,7));
	so.write("flashmaps");

	window._atlas.center();

	window._atlasbg.object.onclick = function ()
	{
		window._atlasbg.remove();
		window._atlas.remove();
	}
 }

window.insertcity = function (pole,citycode,cityname)
{
	if (document.getElementById(pole))
		document.getElementById(pole).value=cityname;
	if (document.getElementById(pole+'_code'))
		document.getElementById(pole+'_code').value=citycode;
	self.close();
}

window.close = function()
{
	if (window._atlas && window._atlas.object)
		window._atlas.remove();
	if (window._atlasbg && window._atlasbg.object)
		window._atlasbg.remove();
}

function multiHidden(el,cont) {
	if (el.checked)
		dis='none';
	else
		dis='';
	var i=1;
	while (document.getElementById(cont+i))
	{
		document.getElementById(cont+i).style.display=dis;
		i++;
	}
}

//window.onload = initForms;
