$(document).ready(function() {
	// Add promoCode to SSW Booking Form
	//fare_code = $("#promoCode").val();
	
	// Disable keyboard on Booking Form
	//$('#ssw-booking-form input[type=text]').keydown(function() { return false; });
	$('#depart_city,#return_city').keydown(function() { return false; });
	$('#ssw-flight-schedule-form input[type=text]').keydown(function() { return false; });

	// Reset Booking Form
	$("#depart_city").val("");
	$("#return_city").val("");
	
	$("#departure_date").val("");
	$("#return_date").val("");
	
	if($("#numAdults").val() == "1") { 
		switch(lang) {
			case "en" : $("#adult_num").val("1 Adult"); break;
			case "th" : $("#adult_num").val("1 ผู้ใหญ่"); break;
		}
	}

	$("#child_num").val("");
	$("#infant_num").val("");
	
	//$("#promoCode").val("");
	
	// Place Holder
	$('.booking-text').placeholder();
	$('.booking-text-short').placeholder();
	$('.booking-text-promotion-code').placeholder();
	$('#login_password').placeholder();
	$('#promoCode').placeholder();
	$('#search-box').placeholder();

	$('#promoCode').blur(function(){
		$(this).val($(this).val().toUpperCase());
	});
	
	// DatePicker Calendar
	var disablePeriod = [];
	var dates = $( "#departure_date, #return_date" ).datepicker({
		minDate: 0, // new Date(2011, 2, 1), 
		maxDate: '+330D', // new Date(2011, 2, 1), 
		numberOfMonths: 1,
		dateFormat: 'DD, d MM, yy',
		altField: '#depDay',
		altFormat: 'd',
		onSelect: function( selectedDate ) {
			var option = this.id == "departure_date" ? "minDate" : "maxDate",
				instance = $( this ).data( "datepicker" );
				date = $.datepicker.parseDate(
					instance.settings.dateFormat ||
					$.datepicker._defaults.dateFormat,
					selectedDate, instance.settings );
			dates.not( this ).datepicker( "option", option, date );
			
			
			
			var altDepDay = $.datepicker.formatDate('yy-mm-dd', new Date($('#departure_date').val()));
			var altRetDay = $.datepicker.formatDate('yy-mm-dd', new Date($('#return_date').val()));

			
			if (option == "minDate") {
			  $('#departureDate').val(altDepDay);
			}
			if (option == "maxDate") {
			  $('#returnDate').val(altRetDay); 
			}
			
		},
		beforeShowDay: function(date){
           var disabledDates = disablePeriod; 
           for (var i = 0; i < disabledDates.length; i++) { 
               if (new Date(disabledDates[i]).toString() == date.toString()) {              
                    return [false]; 
               } 
           } 
           return [true];
       }
	});
	
	$('#departure_date_textbox_right').click(function(){ $('#departure_date').datepicker('show'); });
	
	$('#return_date_textbox_right').click(function(){ $('#return_date_textbox_right').datepicker('show'); });
	
	var d = new Date();
	
	$("#schedule_departure_date").datepicker({
		minDate: 0, 
		maxDate: '+330D', 
		dateFormat: 'DD, d MM, yy',
		onSelect: function( selectedDate ) {
			var altScheduleDepDay = $.datepicker.formatDate('d', new Date($('#schedule_departure_date').val()));
			var altScheduleDepMonth = $.datepicker.formatDate('M', new Date($('#schedule_departure_date').val()));

			$('#schedule_departure_day').val(altScheduleDepDay);
			$('#schedule_departure_month').val(altScheduleDepMonth.toUpperCase());
		}
	});
	
	$('#schedule-date-textbox-right').click(function() {
		$('#schedule_departure_date').datepicker('show');
	});
	
	$("#information_departure_date").datepicker({
		minDate: 0, 
		maxDate: '+330D', 
		dateFormat: 'DD, d MM, yy',
		onSelect: function( selectedDate ) {
			var altInfoDepDay = $.datepicker.formatDate('d', new Date($('#information_departure_date').val()));
			var altInfoDepMonth = $.datepicker.formatDate('M', new Date($('#information_departure_date').val()));

			$('#information_departure_day').val(altInfoDepDay);
			$('#information_departure_month').val(altInfoDepMonth.toUpperCase());
		}
	});
	
	// DatePicker onClick
	$('#information-departure-date-textbox-right').click(function() {
		$('#information_departure_date').datepicker('show');
	});
	
	// Validation Form
	$("#ssw-booking-form").validate({
		rules: {
			depart_city: "required",
			return_city: "required",
			departure_date: "required",
			return_date: "required"
		},
		messages: {
			depart_city: "",
			return_city: "",
			departure_date: "",
			return_date: ""
		},
		errorClass: "invalid"
	});
	
	$("#ssw-my-booking-guest-form").validate({
		rules: {
			recordlocator: "required",
			firstname: "required",
			lastname: "required"
		},
		messages: {
			recordlocator: "",
			firstname: "",
			lastname: ""
		},
		errorClass: "invalid"
	});
	
	$("#ssw-my-booking-login-form").validate({
		rules: {
			login_username: "required",
			login_password: "required"
		},
		messages: {
			login_username: "",
			login_password: ""
		},
		errorClass: "invalid"
	});
	
	$("#ssw-flight-schedule-form").validate({
		rules: {
			schedule_depart_city: "required",
			schedule_dest_city: "required",
			schedule_departure_date: "required"
		},
		messages: {
			schedule_depart_city: "",
			schedule_dest_city: "",
			schedule_departure_date: ""
		},
		errorClass: "invalid"
	});
	
	$("#ssw-flight-info-form").validate({
		rules: {
			flight_number: "required",
			information_departure_date: "required"
		},
		messages: {
			flight_number: "",
			information_departure_date: ""
		},
		errorClass: "invalid"
	});
	
	// Capitalize PromoCode
	$('#ssw-booking-form').submit(function() {
		$('#promoCode').val($('#promoCode').val().toUpperCase());
		return true;
	});
	

// FILTER ROUTE
	// BOOKING
	/*$("#depart_city").keyup(function () {
		var filter = $(this).val();
		$(".depart-dom-list li, .depart-inter-list li").each(function () {
			if ($(this).text().search(new RegExp(filter, "i")) < 0) {
				$(this).addClass("hidden");
			} else {
				$(this).removeClass("hidden");
			}
		});
	});
	
	$("#return_city").keyup(function () {
		var filter = $(this).val();
		$(".return-dom-list li, .return-inter-list li").each(function () {
			if ($(this).text().search(new RegExp(filter, "i")) < 0) {
				$(this).addClass("hidden");
			} else {
				$(this).removeClass("hidden");
			}
		});
	});
	
	// FLIGHT SCHEDULE
	$("#schedule_depart_city").keyup(function () {
		var filter = $(this).val();
		$(".depart-dom-list li, .depart-inter-list li").each(function () {
			if ($(this).text().search(new RegExp(filter, "i")) < 0) {
				$(this).addClass("hidden");
			} else {
				$(this).removeClass("hidden");
			}
		});
	});
	
	$("#schedule_dest_city").keyup(function () {
		var filter = $(this).val();
		$(".return-dom-list li, .return-inter-list li").each(function () {
			if ($(this).text().search(new RegExp(filter, "i")) < 0) {
				$(this).addClass("hidden");
			} else {
				$(this).removeClass("hidden");
			}
		});
	});*/
	
// END FILTER ROUTE
	
});

// FILTER ROUTE FUNCTION
// BOOKING
function clear_depart_filter(){
	$(".depart-dom-list li,.depart-inter-list li").each(function () {
		$(this).removeClass("hidden");
	});
}

function clear_return_filter(){
	$(".return-dom-list li,.return-inter-list li").each(function () {
		$(this).removeClass("hidden");
	});
}

// FLIGHT SCHEDULE
/*function clear_schedule_depart_filter(){
	$(".depart-dom-list li,.depart-inter-list li").each(function () {
		$(this).removeClass("hidden");
	});
}

function clear_schedule_return_filter(){
	$(".return-dom-list li,.return-inter-list li").each(function () {
		$(this).removeClass("hidden");
	});
}*/
// END FILTER ROUTE FUNCTION

function show_booking(){
	$("#booking-title a").addClass('active');
	$("#my-booking-title a").removeClass('active');
	$("#flight-info-title a").removeClass('active');
	
	$("#booking-title a").attr('href','javascript:void(0);');
	$("#my-booking-title a").attr('href','javascript:show_mybooking();');
	$("#flight-info-title a").attr('href','javascript:show_flight_info();');
		
	$("#flight-info-form").hide();
	$("#my-booking-form").hide();
	$("#booking-form").show();
	
	$("#booking-footer-panel").html('<a href="javascript:close_booking();"><img src="/images/booking-panel-footer.png" border="0" /></a>');
	close_all();
}
function show_mybooking(){
	$("#booking-title a").removeClass('active');
	$("#my-booking-title a").addClass('active');
	$("#flight-info-title a").removeClass('active');
	
	$("#booking-title a").attr('href','javascript:show_booking();');
	$("#my-booking-title a").attr('href','javascript:void(0);');
	$("#flight-info-title a").attr('href','javascript:show_flight_info();');
	
	$("#booking-form").hide();
	$("#flight-info-form").hide();
	$("#my-booking-form").show();
	
	$("#booking-footer-panel").html('<a href="javascript:close_booking();"><img src="/images/booking-panel-footer.png" border="0" /></a>');
	close_all();
}
function show_flight_info(){
	$("#booking-title a").removeClass('active');
	$("#my-booking-title a").removeClass('active');
	$("#flight-info-title a").addClass('active');
	
	$("#booking-title a").attr('href','javascript:show_booking();');
	$("#my-booking-title a").attr('href','javascript:show_mybooking();');
	$("#flight-info-title a").attr('href','javascript:void(0);');
	
	$("#my-booking-form").hide();
	$("#booking-form").hide();
	$("#flight-info-form").show();
	
	$("#booking-footer-panel").html('<a href="javascript:close_booking();"><img src="/images/booking-panel-footer.png" border="0" /></a>');
	close_all();
}
function close_all(){
	$("#booking-theme").hide();
	$("#booking-theme-to").hide();
	$("#booking-adult-select").hide();
	$("#booking-searchby-select").hide();
	$("#booking-flight-select").hide();
	$("#booking-child-select").hide();
	$("#booking-lap-select").hide();
	$("#flight-schedule-theme").hide();
	$("#flight-schedule-theme-to").hide();
	$("#flight-info-depart-date-select").hide();
	$("#flight-info-depart-month-select").hide();
}
function open_theme(){
	close_all();
	$("#booking-theme").show("fast", function(){
		$("body").click(function() {
			close_theme();
		});
		
		$("#depart_city, #depart_city_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_theme(){
	$("#booking-theme").hide();
}
function open_theme_to(){
	close_all();
	$("#booking-theme-to").show("fast", function(){
		$("body").click(function() {
			close_theme_to();
		});
		
		$("#return_city, #return_city_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_theme_to(){
	$("#booking-theme-to").hide();
}
function open_flight_schedule_theme(){
	close_all();
	$("#flight-schedule-theme").show("fast", function(){
		$("body").click(function() {
			close_flight_schedule_theme();
		});
		
		$("#schedule_depart_city, #schedule_depart_city_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_flight_schedule_theme(){
	$("#flight-schedule-theme").hide();
}
function open_flight_schedule_theme_to(){
	close_all();
	$("#flight-schedule-theme-to").show("fast", function(){
		$("body").click(function() {
			close_flight_schedule_theme_to();
		});
		
		$("#schedule_dest_city, #schedule_dest_city_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_flight_schedule_theme_to(){
	$("#flight-schedule-theme-to").hide();
}
function open_adult(){
	close_all();
	$("#booking-adult-select").show("fast", function(){
		$("body").click(function() {
			close_adult();
		});
		
		$("#adult_num, #adult_num_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_adult(){
	$("#booking-adult-select").hide();
}
function open_child(){
	close_all();
	$("#booking-child-select").show("fast", function(){
		$("body").click(function() {
			close_child();
		});
		
		$("#child_num, #child_num_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_child(){
	$("#booking-child-select").hide();
}
function open_lap(){
	close_all();
	$("#booking-lap-select").show("fast", function(){
		$("body").click(function() {
			close_lap();
		});
		
		$("#infant_num, #infant_num_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_lap(){
	$("#booking-lap-select").hide();
}
function open_searchby(){
	close_all();
	$("#booking-searchby-select").show("fast", function(){
		$("body").click(function() {
			close_searchby();
		});
		
		$("#action_type, #action_type_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_searchby(){
	$("#booking-searchby-select").hide();
}
function open_cabinClass(){
	close_all();
	$("#booking-cabinClass-select").show("fast", function(){
		$("body").click(function() {
			close_cabinClass();
		});
		
		$("#cabin_class, #cabin_class_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_cabinClass(){
	$("#booking-cabinClass-select").hide();
}
function open_flight(){
	close_all();
	$("#booking-flight-select").show("fast", function(){
		$("body").click(function() {
			close_flight();
		});
		
		$("#flight_type, #flight_type_textbox_right").click(function(event){
			event.stopPropagation();
		});
	});
}
function close_flight(){
	$("#booking-flight-select").hide();
}
function open_depart_date(num){
	close_all();
	$("#flight-info-depart-date-select").css({"top": num +"px"});
	$("#flight-info-depart-date-select").show();
}
function close_depart_date(){
	$("#flight-info-depart-date-select").hide();
}
function open_depart_month(num){
	close_all();
	$("#flight-info-depart-month-select").css({"top": num +"px"});
	$("#flight-info-depart-month-select").show();
}
function close_depart_month(){
	$("#flight-info-depart-month-select").hide();
}
function close_booking(){
	$("#booking-form").hide();
	$("#my-booking-form").hide();
	$("#flight-info-form").hide();
	
	$("#booking-title a").removeClass('active');
	$("#my-booking-title a").removeClass('active');
	$("#flight-info-title a").removeClass('active');
	
	$("#booking-title a").attr('href','javascript:show_booking();');
	$("#my-booking-title a").attr('href','javascript:show_mybooking();');
	$("#flight-info-title a").attr('href','javascript:show_flight_info();');
	
	$("#booking-footer-panel").html('<a href="javascript:show_booking();"><img src="/images/booking-panel-footer-down.png" border="0" /></a>');
	close_all();
}
function add_city_to_ssw(element_id,element_value,ssw_id,ssw_value) {
	clear_depart_filter(); // clear depart O&D filter
	clear_return_filter(); // clear return O&D filter
	
	$('#'+element_id).val(element_value); // add to text field
	$('#'+ssw_id).val(ssw_value.toUpperCase()); // add to ssw
	
	switch (element_id) {
		case "depart_city" : $('#booking-theme-to').load('/booking-form/booking-theme-to.php?dest_code='+ssw_value+'&fare_code='+fare_code); break;
		case "schedule_depart_city" : $('#flight-schedule-theme-to').load('/booking-form/flight-schedule-theme-to.php?dest_code='+ssw_value); break;
	}
	
	// Force One-way route
	if( ($('#origin').val() == 'HKG' && $('#destination').val() == 'CNX') || 
		($('#origin').val() == 'HKG' && $('#destination').val() == 'HKT') || 
		($('#origin').val() == 'KBV' && $('#destination').val() == 'SIN') ||
		($('#origin').val() == 'LPT' && $('#destination').val() == 'SIN') ) {
			select_journey_type('oneway');
			$('#one-way').attr('checked','checked');
			$('#round-trip').attr('disabled','disabled');
	} else {
		$('#round-trip').removeAttr('disabled');
	}
	
// Hack Code for transfer SSW r.2010 to r.2009
	/*if ( ($('#origin').val() == 'CNX') && ($('#destination').val() == 'USM') || 
		 ($('#origin').val() == 'USM') && ($('#destination').val() == 'CNX') || 
		 ($('#origin').val() == 'BKK') && ($('#destination').val() == 'KBV') || 
		 ($('#origin').val() == 'KBV') && ($('#destination').val() == 'BKK') || 
		 ($('#origin').val() == 'BKK') && ($('#destination').val() == 'LPT') || 
		 ($('#origin').val() == 'LPT') && ($('#destination').val() == 'BKK') || 
		 ($('#origin').val() == 'BKK') && ($('#destination').val() == 'SIN') || 
		 ($('#origin').val() == 'SIN') && ($('#destination').val() == 'BKK') || 
		 ($('#origin').val() == 'BKK') && ($('#destination').val() == 'HKG') || 
		 ($('#origin').val() == 'HKG') && ($('#destination').val() == 'BKK') || 
		 ($('#origin').val() == 'REP') && ($('#destination').val() == 'HKT') || 
		 ($('#origin').val() == 'HKT') && ($('#destination').val() == 'REP') || 
		 ($('#origin').val() == 'REP') && ($('#destination').val() == 'USM') || 
		 ($('#origin').val() == 'CNX') && ($('#destination').val() == 'HKT') ) {
		$('#ssw-booking-form').attr('action', '/booking-form/transfer-to-ssw-2009.php');
	
	} else {
		$('#ssw-booking-form').attr('action', 'https://pg-prod.sabresonicweb.com/SSW2010/PGPG/webqtrip.html?execution=e1s1');
	}*/
// End - Hack Code for transfer SSW r.2010 to r.2009

}
function add_value_to_ssw(element_id,element_value,ssw_id,ssw_value) {
	$('#'+element_id).val(element_value); // add to text field
	$('#'+ssw_id).val(ssw_value); // add to ssw
}
function select_journey_type(type) {
	switch(type) {
		case "oneway" : 
			$('#return_date').attr('disabled','disabled');
			$('#return_date').addClass('hidden');
			$('#return_date_textbox_right').addClass('hidden');
			$('#returnDate').attr('disabled','disabled');
		break;
		
		case "roundtrip" : 
			$('#return_date').removeAttr('disabled');
			$('#return_date').removeClass('hidden');
			$('#return_date_textbox_right').removeClass('hidden');
			$('#returnDate').removeAttr('disabled');
		break;
	}
}

function check_num_promotion_code() {
	var adtNum = parseInt($('#numAdults').val());
	var chdNum = parseInt($('#numChildren').val());
	//var infNum = parseInt($('#INF').val());
	//var ifsNum = parseInt($('#IFS').val());
	
	var total_pax_num = adtNum + chdNum; // + infNum + ifsNum
	
	for (i=1; i <= 6; i++) {
		if(i <= total_pax_num) {
			$('#promotion-code-input-list-'+i).show();
			$('#coupon-'+i).removeAttr('disabled');
		} else {
			$('#promotion-code-input-list-'+i).hide();
			$('#coupon-'+i).attr('disabled','disabled');		
		}
	}
}

function checkLimitPassenger() {
	var numLimit = 7;
	var adult_num = parseInt($('#numAdults').val());
	var child_num = parseInt($('#numChildren').val());
	
	switch(lang) {
		case "en" : pax_type_adult = "Adult";
					pax_type_child = "Child";
					pax_type_children = "Children";
					pax_type_infant = "Infant";
					break;
		case "th" : pax_type_adult = "ผู้ใหญ่";
					pax_type_child = "เด็ก";
					pax_type_children = "เด็ก";
					pax_type_infant = "ทารก";
					break;
	}
	
	if(child_num > 0) {
		if (adult_num == numLimit) {
			add_value_to_ssw('child_num','0 '+pax_type_children,'numChildren','0');
			$('#booking-child-select').html(
				"<a href=\"javascript:add_value_to_ssw('child_num','0 " + pax_type_children + "','numChildren','0'); close_child();\">0 " + pax_type_children + "</a>\n\t"+
				"<a href=\"javascript:close_child();\" class=\"x-close\">Close</a>\n"
			);
		} else if(numLimit < (adult_num + child_num)) {
			var pax_left_num = numLimit - adult_num;
			add_value_to_ssw('child_num',pax_left_num + ' ' + pax_type_child,'numChildren',pax_left_num);
		}
	}
	
	child_num = numLimit - adult_num;
	$('#booking-child-select').html(function() {
		var child_txt = "";
		for(i=0; i <= child_num; i++) {
			if(i == 1) {
				child_txt = child_txt + "<li><a href=\"javascript:add_value_to_ssw('child_num','" + i + " " + pax_type_child + "','numChildren','"+ i +"'); close_child();\">"+ i + " " + pax_type_child + "</a></li>\n\t";
			} else {
				child_txt = child_txt + "<li><a href=\"javascript:add_value_to_ssw('child_num','" + i + " " + pax_type_children + "','numChildren','"+ i +"'); close_child();\">"+ i + " " + pax_type_children + "</a></li>\n\t";
			}
		}
		child_txt = child_txt + "<li><a href=\"javascript:close_child();\" class=\"x-close\">Close</a></li>\n";
		return child_txt;
	});
}

function alertInfant() {
	alert('Infant ticket price is 25% of the applicable Published adult fare.\n\nYou could pay for your infant ticket 1 day prior to the departure date at any of our ticket town office for the flights commence outside Thailand and prior to check in time for the flights commence inside Thailand.\n\nInfant ticket cannot be pay online.');
}

function alertMultiCity() {
	alert('Due to the building of a new booking engine, kindly be informed that the \"Multi-City\" function will be temporarily unavailable.\n\nIn the short–term, passengers are advised to make one-way bookings in place of any anticipated multi-city bookings.\n\nWe apologise for any inconvenience this may cause and thank you for your understanding.');
	$('#multi-trip').attr('checked','');
	$('#one-way').attr('checked','checked');
}

