function getMethods(obj) {
  var result = [];
  for (var id in obj) {
    try {
      if (typeof(obj[id]) == "function") {
        result.push(id + ": " + obj[id].toString());
      }
    } catch (err) {
      result.push(id + ": inaccessible");
    }
  }
  return result;
}



function flyClass(){
	this.airportFrom = '';
	this.countryCode = '';
	
	this.thereLink = '';
 	this.returnLink = '';
 	
 	this.therePrice = 0;
 	this.returnPrice = 0;
 	
	this.showArrival = function(){
		if(document.getElementById('dateArrival').value == ''){
		next = new Date();
		next.setDate(next.getDate()+14);
		document.getElementById('dateArrival').value = next.getFullYear()+'-'+ ((parseInt(next.getMonth()) + 1 < 10)?'0'+(parseInt(next.getMonth()) + 1):parseInt(next.getMonth()) + 1) +'-'+ ((parseInt(next.getDate()) < 10)?'0'+(parseInt(next.getDate())):parseInt(next.getDate()));
		}
		document.getElementById('dateArrival').style.backgroundColor = '#FFFFFF';
		document.getElementById('dateArrival').disabled = false;
		document.getElementById('monthArrival').disabled = false;
 		document.getElementById('yearArrival').disabled = false;
 	}
 	
 	this.hideArrival = function(){
 		document.getElementById('dateArrival').style.backgroundColor = '#E8E8E8';
 		document.getElementById('dateArrival').disabled = true;
 		document.getElementById('dateArrival').value = '';
 		document.getElementById('monthArrival').disabled = true;
 		document.getElementById('yearArrival').disabled = true;
 	}
 	
 	this.showConnections = function(airportFrom){
 		this.airportFrom = airportFrom;
 		
 		if(this.airportFrom == ''){
 			$('toSel').disabled = true;
 		}else{
 			$('toSel').disabled = false;
 		}
 		
 		new Ajax.Request('/getConnection.html', {
		    method:'post',
		    onSuccess: function(transport){
				var data = transport.responseText.evalJSON();
				
				document.getElementById('toSel').length = 0;
				data.each(function(item) {
					opt = new Option();
					opt.value = item.airportTo;
					opt.text = item.AirTo.airportCode+', '+item.AirTo.cityName;
					document.getElementById('toSel').appendChild(opt);
				});
			}
		});
 	}
 	this.redirectPage = function(fltId, ticket, airline, lang){
 		
 		if(airline == 'ryan'){
 			window.open('http://ryanair.com/site/PL/');
 		}else{
 			window.open('/'+lang+'/fltId,'+fltId+',ticket,'+ticket+',redirect.html');
 		}
 	}
 	
 	this.showFlyDetails = function(fltId, mode, airline){
 		if(mode == 1){
 			$('orderBut').style.display = '';
 			if (airline == 'ryan'){
 				document.getElementById('orderBut').onclick = function(){
			 		window.open('http://ryanair.com/site/PL/');
			 	}
 			} else {
 				document.getElementById('orderBut').onclick = function(rid){
			 		window.open('/'+lang+'/fltId,'+fltId+',redirect.html');
			 	}
 			}
 			$('flyDetMain').style.display = '';
 			
 		}else{
 			$('orderBut2').style.display = '';
 			$('fly2DetMain').style.display = '';
 			if (airline == 'ryan'){
 				document.getElementById('orderBut2').onclick = function(){
			 		window.open('http://ryanair.com/site/PL/');
			 	}
 			} else {
 				document.getElementById('orderBut2').onclick = function(rid){
			 		window.open('/'+lang+'/fltId,'+fltId+',redirect.html');
			 	}
 			}
 		}
 		
 		new Ajax.Request('/szukaj/showFlyDetails.html', {
		    method:'post',
		    onSuccess: function(transport){
				var data = transport.responseText.evalJSON();
				
				document.getElementById('toSel').length = 0;
				if(mode == 1){
					obj = document.getElementById('flyDet');
				}else{
					obj = document.getElementById('fly2Det');
				}
				
				if (obj.hasChildNodes() ) {
				    while (obj.childNodes.length >= 1 ){
				        obj.removeChild(obj.firstChild );       
				    } 
				}
				div1 = document.createElement('div');
				text = document.createTextNode('Odlot :');
				div1.appendChild(text);
				
				div2 = document.createElement('div');
				text = document.createTextNode(data[0].LineConnection.airportFrom + ' ' + data[0].departure);
				div2.appendChild(text);
				
				div3 = document.createElement('div');
				div3.style.marginTop = '5px';
				text = document.createTextNode('Przylot :');
				div3.appendChild(text);
				
				div4 = document.createElement('div');
				text = document.createTextNode(data[0].LineConnection.airportTo + ' ' + data[0].arrival);
				div4.appendChild(text);
				
				div5 = document.createElement('div');
				div5.style.marginTop = '5px';
				text = document.createTextNode('Podatki / Opłaty : ' + data[0].tax + ' ' + data[0].currency);
				div5.appendChild(text);
				
				div6 = document.createElement('div');
				text = document.createTextNode('Cena łącznie : ' + data[0].lowFare + ' ' + data[0].currency);
				div6.appendChild(text);
				
				obj.appendChild(div1);
				obj.appendChild(div2);
				obj.appendChild(div3);
				obj.appendChild(div4);
				obj.appendChild(div5);
				obj.appendChild(div6);
			}
		});
 	}
 	
 	this.switchDate = function(){
 		if(document.getElementById('dateArrival').value == '' || document.getElementById('dateArrival').value < document.getElementById('dateDeparture').value){
 			if(document.getElementById('dateArrival').disabled != true){
 				document.getElementById('dateArrival').value = document.getElementById('dateDeparture').value;
 			}
 		}
	}
	
 	this.checkDate = function(){
 		
 		dateDeparture = document.getElementById('dateDeparture');
 		dateArrival = document.getElementById('dateArrival');
 		
 		if(dateDeparture.value != '' && dateArrival.value < dateDeparture.value){
 			dateArrival.value = dateDeparture.value;
 			alert('Data powrotu musi być po dacie wylotu');
 			
 		}
 		if(document.getElementById('dateDeparture').value == ''){
 			document.getElementById('dateDeparture').value = document.getElementById('dateArrival').value;
 		}
 	
 	}
 	
 	this.getAirports = function(countryCode, idDes, mode, sel, setToCountry, selToAirport, lang){
 		
 		param = '';
 		if(mode == 1){
 			if(document.getElementById('countryFrom').value == ''){
 				$('airportFrom').disabled = true;
 			}else{
 				$('airportFrom').disabled = false;
 				$('countryTo').disabled = false;
 			}
 			param = '&param=1';
 		}else{
 			if(document.getElementById('countryTo').value == ''){
 				$('airportTo').disabled = true;
 			}else{
 				$('airportTo').disabled = false;
 			}
 			param = '&param='+this.airportFrom;
 		}
 		
 		this.countryCode = countryCode;
 		
 		new Ajax.Request('/'+lang+'/getAirports.html', {
		    method:'post',
		    parameters: 'countryCode=' + this.countryCode + param + '&lang=' + lang,
		    onSuccess: function(transport){
				
				var data = transport.responseText.evalJSON();
				document.getElementById(idDes).length = 0;
				
				data.data.each(function(item) {
					opt = new Option(item.cityName+', '+item.airportName+' ('+item.airportCode+')', item.airportCode);
					
					/*
					if(item.direct == 1){
						opt.style.color = '#0066a8';
					} else if (idDes != 'airportFrom'){
						opt.style.color = '#b72828';
					}
					*/
					
					if(item.direct == 1){
						opt.style.color = '#0066a8';
					}
					if (idDes == 'airportFrom' && sel == item.airportCode){
						opt.selected = true;
						fly.getCountry('countryTo', sel, '', '', lang); 
						setAirport('from', opt.text);
					} else if(idDes == 'airportTo' && sel == item.airportCode){
						opt.selected = true;
						setAirport('to', opt.text);
					}
						
					optsLen = document.getElementById(idDes).options.length;
    				document.getElementById(idDes).options[optsLen] = opt;
					
				});
				
				if(document.getElementById('countryTo').value == ''){
					fly.getCountry('countryTo', document.getElementById('airportFrom').value, setToCountry, selToAirport, lang);
				}
			}
		});
 	}
 	
 	this.getCountry = function(idDes, airportFrom, sel, selAirport, lang){
 		
 		this.airportFrom = airportFrom;
 	
 		new Ajax.Request('/'+lang+'/getCountry.html', {
		    method:'post',
		    parameters: 'airportFrom=' + this.airportFrom + '&lang=' + lang,
		    onSuccess: function(transport){
				var data = transport.responseText.evalJSON();
				document.getElementById(idDes).length = 0;
				
				data.data.each(function(item) {
					opt = new Option(item.countryName + ' ('+item.countryCode+')', item.countryCode);
					/*if(item.direct == 1){
						opt.style.color = '#0066a8';
					} else if (idDes != 'countryFrom'){
						opt.style.color = '#b72828';
					}*/
					if(item.direct == 1){
						opt.style.color = '#0066a8';
					}
					
					if (item.countryCode == sel){
						opt.selected = true;
						setCountry('to', opt.text);
					}
					optsLen = document.getElementById(idDes).options.length;
    				document.getElementById(idDes).options[optsLen] = opt;
				});
				
				value = document.getElementById('countryTo').value;
				if(value != ''){
					fly.getAirports(value, 'airportTo', 2, selAirport, '', '', lang);
				}
			}
		});
 	}
 	this.hidePrev = function(datePrev){
 		now = new Date()
 		now = now.getFullYear()+'-'+now.getMonth()+1+'-'+now.getDate();
 	 		
 	 	if( now > date){
 	 		document.getElementById('therePrev').style.display = 'none';
 	 	}
 	}
 	
 	this.checkPrev = function(datePrev, mode){
 		now = new Date()
 		now = now.getFullYear()+'-'+now.getMonth()+1+'-'+now.getDate()+8;
 	 		
 	 		if( now > datePrev){
 	 			if(mode == 1){
 	 				document.getElementById('therePrev').style.display = 'none';
 	 			}else{
 	 				document.getElementById('returnPrev').style.display = 'none';
 	 			}
 	 		}
 	}
 	
 	this.createLink = function(value, mode, aId1, aId2){
 	
 		
 		if(mode == 'there'){
 			this.thereLink = value;
 		}
 		
 		if(mode == 'return'){
 			this.returnLink = value;
 		}
 		
 		if(this.thereLink != '' && this.returnLink != ''){
 			url = this.thereLink+','+this.returnLink+',lot.html';
 		}
 		if(this.thereLink != '' && this.returnLink == ''){
 			url = this.thereLink+',lot.html';
 		}
 		if(this.thereLink == '' && this.returnLink != ''){
 			url = this.returnLink+',lot.html';
 		}
 		
 		if(document.getElementById(aId1)){
 			document.getElementById(aId1).href = url;
 		}
 		
 		if(document.getElementById(aId2)){
 			document.getElementById(aId2).href = url;
 		}
 		
 	}
 	this.pdivId1 = '';
 	this.pradioId1 = '';
 	
 	this.pdiv2Id1 = '';
 	this.pradio2Id1 = '';
 	
 	
 	this.selectCal = function(divId, radioId){
 		if(this.pdivId1 != ''){
 			document.getElementById(this.pdivId1).style.backgroundColor = '#D5E4DF';
 			document.getElementById(this.pradioId1).checked = 'false';
 		}
 		
 		this.pdivId1 = divId;
	 	this.pradioId1 = radioId;
	 	
	 	document.getElementById(divId).style.backgroundColor = '#45484F';
 		document.getElementById(radioId).checked = 'true'
 	}
 	
 	this.selectCal2 = function(divId, radioId){
 		if(this.pdiv2Id1 != ''){
 			document.getElementById(this.pdiv2Id1).style.backgroundColor = '#D5E4DF';
 			document.getElementById(this.pradio2Id1).checked = 'false';
 		}
 		
 		this.pdiv2Id1 = divId;
	 	this.pradio2Id1 = radioId;
	 	
	 	document.getElementById(divId).style.backgroundColor = '#45484F';
 		document.getElementById(radioId).checked = 'true'
 	}
 	
 	this.sumPrice = function(price, mode, ticket, airportFrom, airportTo, timeFrom, timeTo){
 	
 		if(mode == 'there'){
 			
 			this.therePrice = price;
 			document.getElementById('thereDetDeparture').innerHTML = '(' + airportFrom + ') ' + timeFrom;  
 			document.getElementById('thereDetArrival').innerHTML = '(' + airportTo + ') ' + timeTo;
 			document.getElementById('thereDetSum').innerHTML = parseFloat(this.therePrice) * parseInt(ticket) + ' PLN'; 
 			document.getElementById('detThere').style.display = '';	
 		}
 		
 		if(mode == 'return'){
 			this.returnPrice = price;
 			document.getElementById('returnDetDeparture').innerHTML = '(' + airportTo + ') ' + timeFrom;  
 			document.getElementById('returnDetArrival').innerHTML = '(' + airportFrom + ') ' + timeTo;
 			document.getElementById('returnDetSum').innerHTML = parseFloat(this.returnPrice) * parseInt(ticket) + ' PLN';
 			document.getElementById('detReturn').style.display = '';
 		}
 		
 		sum = ((parseFloat(this.therePrice) + parseFloat(this.returnPrice)) * parseInt(ticket)).toFixed(2)+' PLN';
 		
 		while (document.getElementById('priceAll').firstChild){
 			obj = document.getElementById('priceAll').firstChild;
 			document.getElementById('priceAll').removeChild(obj);
 		}
 		
 		text = document.createTextNode(sum);
 		document.getElementById('priceAll').appendChild(text);
 	}
 	
 	
 	
}


fly = new flyClass();