/* Locations */
Locations = new Array();
Locations['IST'] = ['Taksim','Sultanahmet','Other destinations'];
Locations['IZM'] = ['Kusadasi', 'Selcuk (Ephesus)'];
Locations['ANT'] = ['Antalya City Hotels', 'Kemer', 'Kas'];
Locations['DAL'] = ['Fethiye'];
Locations['BOD'] = ['Bodrum'];
Locations['KAY'] = ['Cappadocia'];

AirList = new Array();
AirList['IST'] = ['Sabiha Gokcen (Istanbul)','Ataturk Airport (Istanbul)'];
AirList['IZM'] = ['Izmir Airport'];
AirList['ANT'] = ['Antalya Airport'];
AirList['DAL'] = ['Dalaman Airport'];
AirList['BOD'] = ['Bodrum Airport'];
AirList['KAY'] = ['Kayseri Airport'];
/* ----- */

/* Disabled field messages */
DisabledMsg = new Array();
DisabledMsg['airport'] 	= 'Destination';
DisabledMsg['resort']	= 'Airport';
/* ------- */

/* Form element template options */
var Airport = {
	selectbox: '<select style="width:190px" name="airports" id="airports" onchange="ResortListByAirport(this.value);"><option value="0"> -- Airport -- </option>#{options}</select>',
	option: '<option value="#{value}">#{option}</option>'
};

var Airports = {
	List:[	{value:	'city=IST&value=Sabiha Gokcen (Istanbul)',				option: 'Sabiha Gokcen (Istanbul)'},
			{value:	'city=IST&value=Ataturk Airport (Istanbul)',			option: 'Ataturk Airport (Istanbul)'},
			{value:	'city=IZM&value=Izmir Airport',				option:	'Izmir Airport'},
			{value:	'city=ANT&value=Antalya Airport',			option: 'Antalya Airport'},
			{value:	'city=DAL&value=Dalaman Airport',			option: 'Dalaman Airport'},
			{value:	'city=BOD&value=Bodrum Airport',			option: 'Bodrum Airport'},
			{value:	'city=KAY&value=Kayseri Airport',			option:	'Kayseri Airport'}
]};


var Resort = {
	selectbox:	'<select style="width:190px" name="resorts" id="resorts" onchange="AirportListByResort(this.value);"><option value="0"> -- Origin -- </option>#{options}</select>',
	option: 	'<option value="#{value}">#{option}</option>'
};

var Resorts = {
	List:[	{value: 'city=IST&value=Sultanahmet',				option:	'Sultanahmet'},
			{value: 'city=IST&value=Taksim',					option: 'Taksim'},
			{value: 'city=IST&value=Other destinations',		option:	'Other destinations'},
			{value: 'city=IZM&value=Kusadasi',					option:	'Kusadasi'},
			{value: 'city=IZM&value=Selcuk (Ephesus)',			option:	'Selcuk (Ephesus)'},
			{value: 'city=ANT&value=Antalya City Hotels',		option:	'Antalya City Hotels'},
			{value: 'city=ANT&value=Kemer',						option:	'Kemer'},
			{value: 'city=ANT&value=Kas',						option:	'Kas'},
			{value: 'city=DAL&value=Dalaman',					option:	'Dalaman'},
			{value: 'city=KAY&value=Cappadocia',				option:	'Cappadocia'}
]};




function Disabled(where)
{
	return '<select disabled style="width:190px"><option>'+DisabledMsg[where]+'</option></select>';
}

/*************************************************/

function AirportList()
{
	var willReturn;
	var OpTemp = new Template(Airport.option);
	Airports.List.each(function(val){willReturn += OpTemp.evaluate(val);});
	return willReturn = {options: willReturn};
}

function AirportToResortList()
{
	var List = new Template(Airport.selectbox);
	$('AirportDiv').innerHTML = List.evaluate(AirportList());
	$('ResortDiv').innerHTML = '<select disabled style="width:190px;" id="resorts"><option>Destination</option></select>';
}

function ResortListByAirport(airport)
{
	if(airport == 0)
	{	$('ResortDiv').innerHTML = Disabled('airport');	}
	else
	{
		var act = airport.toQueryParams(); 
		var ResortOptions = Locations[act.city];
		ResortOptions.each(function(val){ResortOptions += '<option value="'+ val +'">'+ val +'</option>'} );
		$('ResortDiv').innerHTML = '<select style="width:190px" name="Resort" id="Resort">'+ResortOptions+'</select>';		
		$('Airport').value = act.value;
		
	}
}

/************************************************/


function ResortList()
{
	var willReturn;
	var OpTemp = new Template(Resort.option);
	Resorts.List.each(function(val){willReturn += OpTemp.evaluate(val);});
	return willReturn = {options: willReturn};
}


function ResortToAirportList()
{
	var List = new Template(Resort.selectbox);
	$('AirportDiv').innerHTML = List.evaluate(ResortList());
	$('ResortDiv').innerHTML = '<select style="width:190px" disabled id="airports"><option>Airport</option></select>';
}

function AirportListByResort(resort)
{

	if(resort == 0)
	{	$('ResortDiv').innerHTML = Disabled('resort');	}
	else
	{
		var act = resort.toQueryParams(); 
		var AirportOptions = AirList[act.city];
		AirportOptions.each(function(val){AirportOptions += '<option value="'+ val +'">'+ val +'</option>'} );
		$('ResortDiv').innerHTML = '<select style="width:190px" name="Airport" id="Airport">'+AirportOptions+'</select>';		
		$('Resort').value = act.value;
	}	
}

function OptionRange(start, end, type)
{
	count = $A($R(start, end));
	if (type == 'min' || type == 'hour')
	{
		count.each(function(a){ if(a < 10){count[a] = "0"+a} else{count[a] = a}});
	}

		count.each(function(a){document.write("<option value="+a+">"+a);});


}
	
function plus()
{
  	var D = new Date();
  	var F = new Date();
	ARR = $F('TransferDate').split('/');
	D.setFullYear(ARR[2], ARR[1] , ARR[0])
	F.setFullYear(D.getYear(), D.getMonth(), D.getDate())
	F.setYear(ARR[2])
	
		if(D.getDate() != 1){
		day = D.getDate()+1;
	} else {
		day = D.getDate();
	}
	
	//day = D.getDate() +1;
	month = D.getMonth();
	year = D.getYear();
	if(day < 10)
	{
		day = "0"+day;
	}
	if(month < 10)
	{
		month =  "0" + month;
	}
	
	$('ReturnDate').value = day + '/' + month + '/' + ARR[2];
}



function ValidEmail(m) {

   return (m.indexOf(".") > 2) && (m.indexOf("@") > 0);
}

function RequireValidate()
{
	Now = new Date();
	TransferD = new Date();
	ReturnD = new Date();
	D = $F('TransferDate').split('/');
	A = $F('ReturnDate').split('/');
	TransferD.setFullYear(D[2], D[1]-1, D[0]);	
	ReturnD.setFullYear(A[2], A[1]-1, A[0]); 
	
	if($('airports') !== null && $F('airports') == 0)
	{
		alert('Please select airport.');
		$('airports').activate();
		return false;
		
	}
		if($('resorts') !== null && $F('resorts') == 0)
	{
		alert('Please select origin.');
		$('resorts').activate();
		return false;
	}
	if($F('Name') == "")
	{
		alert('Please enter your name.');
		$('Name').activate();
		return false;
	}
	if($F('Email') == "" || !ValidEmail($F('Email')))
	{
		alert('Please enter a valid email.');
		$('Email').activate();
		return false;
	}
	if($F('TransferFlightCode') == "")
	{
		alert('Please enter flight code.');
		$('TransferFlightCode').activate();
		return false;		
	}
	

	
	if(TransferD >= Now)
	{
		if($F('Way') == 2)
		{
			if(ReturnD < TransferD)
			{
				alert('Please check your date information.');
				return false;
			}
		}
	}
	else
	{
		alert('Please check your date information.');
		return false;
	}
	
	if($F('ReturnFlightCode') == "" && $F('Way') == 2)
	{
		alert('Please enter return flight code.');
		$('ReturnFlightCode').activate();
		return false;		
	}

	
}

function oneway()
{
	if($F('Way') == 2)
	{
		['ReturnDay','ReturnTime','ReturnFlight'].each(function(val){$(val).hide()});
		$('Way').value = "1";
		return false;
	}
	if($F('Way') == 1)
	{
		['ReturnDay','ReturnTime','ReturnFlight'].each(function(val){$(val).show()});
		$('Way').value = "2";
		return false;
	}	
}