
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true
	}


/**
 * JS wykorzystuje prototype.js, http://blog.elksoft.pl/wp-content/prototype.js.pl.html
 */
   
function isNode(id) {
	if (document.getElementById(id) == null) {
		return false;
	} else {	
		return true;
	}
} 

/**
 * glowna klasa obslugujaca Aero.pl
 */

var Aero = Class.create();
Aero.prototype = {

	// metody wywolywane po pokazaniu sie strony
	initialize: function() {
		if (isNode('newsletterConfirmButton')) this.prepareNewsletterBox();
		this.addRoundedBorders();
		this.handleWindowWidth();
		this.handleExternalLinks();
		this.fixTabindexProblem();
		if (isNode('reservationButton')) this.handleReservationButton();
		if (isNode('priceCheckButton')) this.handlePriceCheckButton();
		if (isNode('searchAdvanced') || isNode('siriusSearchForm')) new AeroSearchAdvancedForm;
		if (isNode('reservationForm')) new AeroReservationForm;
		if (isNode('siriusReservationForm')) new AeroSiriusReservationForm;
		if (isNode('flightTabs')) new NokautTabs('flightTabs');
	},
	
	// zamiana przycisku zapisania sie do newslettera na link
	prepareNewsletterBox: function() {
		new Insertion.After('newsletterConfirmButton','<a href="#" id="newsletterConfirmLink" class="arrow" onclick="if (echeck(document.newsletter.email.value)) document.newsletter.submit(); else {alert(\'Należy wprowadzić prawidłowy adres e-mail\');  return false;}">zapisz</a>');
		$('newsletterConfirmButton').remove();
		Event.observe('newsletter_email','focus', function(){ this.value = ''; });
	},
	
	// dodaje okrlagle rogi do reklam
	// TODO: daj mozliwosc obslugi wielu tych samych reklam na jednej stronie, wykorzystujac class
	addRoundedBorders: function() {
		if (isNode('ad246_1')) {
			//new Insertion.Bottom('ad246_1', '<img src="/img/aero/ad246x167.png" width="246" height="167" alt="" class="filter" />');
		}
		if (isNode('ad160_1')) {
			new Insertion.Bottom('ad160_1', '<img src="/img/aero/ad160x156.png" width="160" height="156" alt="" class="filter" />');
		}
	},
	
	// usuwa reklamy po prawej stronie
	handleWindowWidth: function() {
		var windowWidth = document.getElementsByTagName('html')[0].offsetWidth;

		if (windowWidth < 918) {
			if (isNode('adSide')) $('adSide').hide();
			if (isNode('adAero')) $('adAero').hide();
			document.getElementsByTagName('body')[0].style.marginLeft='auto';
		}

	},

	// zamiast target=_blank
    handleExternalLinks: function() {
	  if (!document.getElementsByTagName) return;
	  var anchors = document.getElementsByTagName("a");
	  for (var i=0; i<anchors.length; i++) {
	   var anchor = anchors[i];
	   if (anchor.getAttribute("href") &&
	       anchor.getAttribute("rel") == "external")
	     anchor.target = "_blank";
	  }

    },

   // poprawia problem z TAB w formularzach
   fixTabindexProblem: function() {
    	if (!document.getElementsByTagName) return;
    	var formList = document.getElementsByTagName("form");
    	var pList = [];
    	for (var i=0; i<formList.length; i++) {
    		pList = formList[i].getElementsByTagName("p");
    		for (var j=0; j<pList.length; j++) {
				pList[j].setAttribute('tabindex',-1);
			};
    	}
    },

	// zajmuje się przyciskiem rezerwacji na stronach ze szczególami oferty
	handleReservationButton: function() {
		if (document.getElementsByTagName('body')[0].className.indexOf('promocje_szczegoly') > 0) {
			Event.observe('reservationButton','click', function(){
				alert('Jeśli chcesz zarezerwować bilet, wypełnij formularz poniżej podając planowaną datę wylotu, datę powrotu i inne opcje.')
			});
		}
		if (document.getElementsByTagName('body')[0].className.indexOf('tanie_szczegoly') > 0) {
			Event.observe('reservationButton','click', function(){
				alert('Jeśli chcesz zarezerwować bilet, wypełnij formularz poniżej podając planowaną datę wylotu, datę powrotu i inne opcje.')
			});
		}
		if (document.getElementsByTagName('body')[0].className.indexOf('czartery_szczegoly') > 0) {
			Event.observe('reservationButton','click', function(){
				alert('Jeśli chcesz zarezerwować bilet, wypełnij formularz wstępnej rezerwacji. Jego wypełnienie nie zobowiązuje do dokonania zakupu.')
			});
		}

	},

	// zajmuje się przyciskiem rezerwacji na stronach ze szczególami oferty
	handlePriceCheckButton: function() {
		if (document.getElementsByTagName('body')[0].className.indexOf('promocje_szczegoly') > 0) {
			Event.observe('priceCheckButton','click', function(){
				alert('Jeśli chcesz sprawdzić cenę wraz z opłatami dodatkowymi, wypełnij formularz poniżej podając planowaną datę wylotu, datę powrotu i inne opcje.')
			});
		}
		if (document.getElementsByTagName('body')[0].className.indexOf('tanie_szczegoly') > 0) {
			Event.observe('priceCheckButton','click', function(){
				alert('Jeśli chcesz sprawdzić cenę wraz z opłatami dodatkowymi, zadzwoń pod numer 0 801 470 502 lub 071 371 49 13.'+"\n"+'Nasz konsultant odpowie na wszystkie Twoje pytania.')
			});
		}
		if (document.getElementsByTagName('body')[0].className.indexOf('czartery_szczegoly') > 0) {
			Event.observe('priceCheckButton','click', function(){
				alert('Jeśli chcesz sprawdzić cenę wraz z opłatami dodatkowymi, wypełnij formularz wstępnej rezerwacji. Jego wypełnienie nie zobowiązuje do dokonania zakupu.')
			});
		}
	}

}

/**
 * obsluga formularza wyszukiwania
 */
var AeroSearchAdvancedForm = Class.create();
AeroSearchAdvancedForm.prototype = {

	// przygotowanie formularza do dzialania 
	initialize: function() {
		this.prepareOptions();
		this.prepareRoute();
		if (isNode('flight_from_hour_when')) {
			this.prepareHours();
			this.handleHours();
		}
		if (isNode('returnFields')) {
			this.prepareWays();
			this.handleWays();
		}
		
	},
	
	// opcje
	prepareOptions: function() {
		if (isNode('flight_ways_2')) {
			$('flight_ways_2').click();
		}
	},

	// trasa
	prepareRoute: function() {
		if (isNode('siriusSearchForm')) {
			new Insertion.After('flight_from_month', this.getCalendarHtml('from'));
			new Insertion.After('flight_to_month', this.getCalendarHtml('to'));
			new Insertion.After('flight_from', this.getGlobeHtml('from'));
			new Insertion.After('flight_to', this.getGlobeHtml('to'));
		}
		if (isNode('searchMainForm')) {
			new Insertion.After('flight_from', this.getGlobeHtml('origin_long'));		                              	
			new Insertion.After('flight_to', this.getGlobeHtml('destination_long'));
		}
    	// wykomentował: Główka Łukasz 07.05.2006 (modyfikacja maski wyszukiwarki)
	},
	
	// w jedną lub w dwie strony 
	prepareWays: function() {
		Event.observe($('field_type'),'change', this.handleWays);
	},
	
	handleWays: function() {
		if ($F('field_type') == '2') {
			$('returnFields').show();
		} else {
			$('returnFields').hide();
		}
	},
	
	// zwraca kod HTML kalendarza
	getCalendarHtml: function(id) {
		return '<img src="/img/aero/icoCalendar.png" class="calendar" style="width:20px; height: 20px" alt="wybierz datę" onclick="alert(\'kalendarz dla pola '+id+'\')" />';
	},

	// zwraca kod HTML globusa
	getGlobeHtml: function(id) {
		return '&nbsp;<img src="/img/aero/icoGlobe.png" class="globe" style="width:20px; height: 20px" alt="wybierz miasto" onclick="window.open(\'/atlas-sirius2.php?_p=getAirports&amp;_sfid=PAE&amp;_clid=357&amp;pos=2717&amp;_field='+id+'&amp;CNC=EU&amp;COC=PL\', \'Atlas\', \'width=430, height=300, scrollbars=yes\');" />';
	},

	// przygotowywuje zmiane formy gramatycznej czasu wylotu/przylotu
	prepareHours: function() {
		Event.observe($('flight_from_hour_when'), 'change', this.handleHours.bind(this));
		Event.observe($('flight_to_hour_when'), 'change', this.handleHours.bind(this));
	},

	// wlacza zmiane formy
	handleHours: function() {
		this.changeHours('1',$F('flight_from_hour_when'));
		this.changeHours('2',$F('flight_to_hour_when'));
	},
	
	// zmienia forme
	changeHours: function(id, when) {
		if (when == 'before') {
			$('hour'+id+'_night').innerHTML = 'rozpoczęciem nocy';
			$('hour'+id+'_morning').innerHTML = 'porankiem';
			$('hour'+id+'_evening').innerHTML = 'południem';
			$('hour'+id+'_afternoon').innerHTML = 'rozpoczęciem wieczoru';
		} else {		
			$('hour'+id+'_night').innerHTML = 'rozpoczęciu nocy';
			$('hour'+id+'_morning').innerHTML = 'poranku';
			$('hour'+id+'_evening').innerHTML = 'południu';
			$('hour'+id+'_afternoon').innerHTML = 'rozpoczęciu wieczoru';		
		
		}
	}
	
}

var NokautTabs = Class.create();

NokautTabs.prototype = {
	
	// zakładki pogrupowane według listy
	list: [],
	
	// zaznaczone zakładki według listy
	selected: [],
	
	// id-ki list zakładek
	id: '',
	
	// przygotowanie zakładek
	initialize: function(id) {
		this.id = id;
		this.findAvailableTabs();
		this.showSelectedTabContent();
		this.attachToAnchorLinks();
	},
	
	// znajdujemy dostępne zakładki
	findAvailableTabs: function() {

		// wyciągamy element na bazie id
		var tabGroup = document.getElementById(this.id);

		// sprawdzamy linki w tych listach i wyciągamy to co jest po #
		var tabLinks = tabGroup.getElementsByTagName('a');
		for (var j=0; j < tabLinks.length; j++) {
			
			var tabName = tabLinks[j].href.substring(tabLinks[j].href.indexOf("#") + 1);
			tabLinks[j].id = tabName + 'Tab';
			
			// dodajemy zakładkę do listy zakładek
			this.list.push(tabName);
			
			// pierwsza zakładka zaznaczana automagicznie
			if (j == 0) {
				this.selected = tabName;
			}
			
			// chyba że jakaś jest już w urlu...				
			if (document.location.hash.substring(1) == tabName) {
				this.selected = tabName;	
			} 
			
		} 
		
			
	},
	
	// pokazujemy elementy o ID takiej jak 
	showSelectedTabContent: function() {

		for (var i=0; i < this.list.length; i++) {

			var contentId = this.list[i] + 'Content';

			if ($(contentId)) {
				if (this.list[i] == this.selected) {
					$(contentId).show();
				
				} else {
					$(contentId).hide();
				}
			}
		}
			
		var links = document.getElementById(this.id).getElementsByTagName('a');
		for (var k=0; k < links.length; k++) {
			var anchor = links[k].href.substr(links[k].href.lastIndexOf('#')+1);
			if (anchor != '') {
				if (anchor == this.selected) {
					links[k].parentNode.className = 'selected';					
				} else {
					links[k].parentNode.className = '';
				}
			}
		}
			
		
		
	},
	
	attachToAnchorLinks: function() {
		var links = document.getElementsByTagName('a');
		for (var k=0; k < links.length; k++) {
			if (links[k].href) {
				if (links[k].href.lastIndexOf('#') > 0) {
					var anchor = links[k].href.substr(links[k].href.lastIndexOf('#')+1);
					if (anchor != '') {		
						Event.observe(links[k], 'click', this.handleAnchorLink.bind(this));
					}
				}
			}
		}
	},
	
	handleAnchorLink: function(e) {
		var link = Event.element(e);
		this.selected = link.href.substr(link.href.lastIndexOf('#')+1);
		this.showSelectedTabContent();
	}
}

/**
 * Zwroc anchora z linku
 */
function getAnchorByUrl(url) {
	return url.substring(url.lastIndexOf('#') + 1);
}


/**
 * Obsługa formularza rezerwacji
 */
var AeroReservationForm = Class.create();
AeroReservationForm.prototype = {
	
	// inicjalizacja
	initialize: function() {
		this.preparePeopleHandling();			
	},
	
	// obsluz podawanie daty urodzenia dla osob innych niz dorosle
	preparePeopleHandling: function() {
		var selectList = document.getElementById('reservationForm').getElementsByTagName('select');
		for (var i=0; i<selectList.length; i++) {
			if (selectList[i].id.substr(selectList[i].id.lastIndexOf('_')+1) == 'type') {
				Event.observe(selectList[i], 'change', this.handlePeopleHandling);
			}
		}
	},
	
	handlePeopleHandling: function(e) {
		var select = Event.element(e);
		var divId = select.id.replace('_type','_date');
		if ($F(select) != 'dorosły') {
			$(divId).style.display = 'block';	
		} else {
			$(divId).style.display = 'none';
		}
		
	}
	
}


/**
 * Obsługa formularza rezerwacji Siriusa
 */
var AeroSiriusReservationForm = Class.create();
AeroSiriusReservationForm.prototype = {
	
	// inicjalizacja
	initialize: function() {
		this.prepareNameHandling();
		this.preparePaymentHandling();
		this.prepareInvoiceHandling();		
	},
	
	// po wyborze Pan/Pani przeskocz do imienia
	prepareNameHandling: function() {
		Event.observe($('field_mr'), 'change', function() { 
			$('field_name').focus(); 
		});
	},
	
	// obsluz wybor sposobu platnosci
	preparePaymentHandling: function() {
		$('fields_creditcard').hide();
		Event.observe($('field_payment'), 'change', function() {
			if ($('field_payment').value == 'creditcard') {
				$('fields_creditcard').show();
			} else {
				$('fields_creditcard').hide();
			}

		});
	},
	
	// obsluz wybor faktury
	prepareInvoiceHandling: function() {
		$('fields_invoice').hide();
		$('field_invoice').checked = false;
		Event.observe($('field_invoice'), 'click', function() {
			if ($('field_invoice').checked) {
				$('fields_invoice').show();
			} else {
				$('fields_invoice').hide();
			}
		});		
	}

	
}

Event.observe(window, 'load', function() { new Aero; } ); 

function alertWithDetails(variable) {
	var message = '';
	for (var i in variable) {
		if ((typeof(variable[i]) != 'object') && (typeof(variable[i]) != 'function')) {
			message = message + i + ': ' + variable[i] + "\n";	
		}
	}
	alert(message);
}



