var month = new Array(12);
month[0] = "Jan";
month[1] = "Feb";
month[2] = "Mar";
month[3] = "Apr";
month[4] = "May";
month[5] = "Jun";
month[6] = "Jul";
month[7] = "Aug";
month[8] = "Sep";
month[9] = "Oct";
month[10] = "Nov";
month[11] = "Dec";

$(function() {
	$.datepicker.setDefaults({
		showOn: 'both', 
		buttonImageOnly: true, 
		buttonImage: 'http://newtravelplanners.clients.squiz.co.uk/__data/assets/image/0008/107/calendar.png',
		buttonText: 'Calendar',
		dateFormat: 'yy/mm/dd',
		onSelect: function(date) {append_date(new Date(date.split('/')[0], (date.split('/')[1] - 1), date.split('/')[2]));},
		mandatory: true,
		minDate: new Date(new Date().setDate(new Date().getDate() + 3))
	});

	$("a[href='#']").click(function() {return false;});

	$("#day").change(function() {day_in_month_change();});

	$("#month").change(function() {day_in_month_change();});

	$("#search_form").submit(function() {return search_form_validation();});

	$("#date-pick").datepicker();
	if (location.toString().search('=') != -1)
	get_values();
	else
	append_date(new Date(new Date().setDate(new Date().getDate() + 4)));
});

function get_values_old() {
	values = location.toString().split("?")[1].split("&");

	if (location.toString().search('dDay') != -1) {
		var d = parseInt(array_value(values, "dDay"));
	}

	if (location.toString().search('dMonthYear') != -1) {
		var m = parseInt(array_value(values, "dMonthYear").split("+")[0].split("%")[2].substr(2,2)-1);
	}

	if (location.toString().search('dMonthYear') != -1) {
		var y = array_value(values, "dMonthYear").split("+")[1].split("%")[2].substr(2,2);
	}
	var y_full = "20" + y;

	if (location.toString().search('departure_date') != -1) {
		d = array_value(values, 'departure_date').substr(0,2);
		m = array_value(values, 'departure_date').substr(5,3);
		m = array_index(month, m);
		y_full = array_value(values, 'departure_date');
		y_full = y_full.substr(y_full.length-4,4)
	}

	if (location.toString().search('dDay') != -1 || location.toString().search('departure_date') != -1) {
		append_date(new Date(y_full,m,d));
	} else {
		append_date(new Date());	
	}

	select_index($("#leaving_from").children("option"), array_value(values, "leaving_from"));
	select_index($("#going_to").children("option"), array_value(values, "going_to"));
	select_index($("select[name='quality']").children("option"), array_value(values, "quality"));
	select_index($("select[name='duration']").children("option"), array_value(values, "duration"));
	select_index($("select[name='nRooms']").children("option"), array_value(values, "nRooms"));
	select_index($("select[name='nAdults']").children("option"), array_value(values, "nAdults"));
	select_index($("select[name='nChildren']").children("option"), array_value(values, "nChildren"));
	select_index($("select[name='nInfants']").children("option"), array_value(values, "nInfants"));
	select_index($("select[name='AirlineSel']").children("option"), array_value(values, "AirlineSel"));
	select_index($("select[name='AirlineSel']").children("option"), array_value(values, "airline"));
	select_index($("select[name='AirlineClass']").children("option"), array_value(values, "AirlineClass"));
}

function get_values() {
	select_index($("#leaving_from").children("option"), getTPSearchParamVal("leaving_from"));
	select_index($("#going_to").children("option"), getTPSearchParamVal("going_to"));
	select_index($("select[name='quality']").children("option"), getTPSearchParamVal("quality"));
	select_index($("select[name='duration']").children("option"), getTPSearchParamVal("duration"));
	select_index($("select[name='nRooms']").children("option"), getTPSearchParamVal("nRooms"));
	select_index($("select[name='nAdults']").children("option"), getTPSearchParamVal("nAdults"));
	select_index($("select[name='nChildren']").children("option"), getTPSearchParamVal("nChildren"));
	select_index($("select[name='nInfants']").children("option"), getTPSearchParamVal("nInfants"));
	select_index($("select[name='AirlineSel']").children("option"), getTPSearchParamVal("AirlineSel"));
	select_index($("select[name='AirlineClass']").children("option"), getTPSearchParamVal("AirlineClass"));

	values = location.toString().split("?")[1].split("&");

	var d = parseInt(getTPSearchParamVal("dDay"));
	var my = getTPSearchParamVal("dMonthYear");
	if (d && my.length) {
		var m = my.split("'")[1] - 1;
		var y = my.split("'")[3];
		var y_full = "20" + y;
		append_date(new Date(y_full,m,d));
	} else {
		append_date(new Date());
	}
}

function array_index(array, value) {
	var index= 0;
	jQuery.each(array, function(i) {
		if (array[i] == value)
		index= i;
	});
	return index;
} 

function array_value(array, name) {
	var value = "";
	jQuery.each(array, function(i) {
		if (array[i].toString().search(name) != -1)
		value = array[i].split("=")[1]; 
	});
	return value;
}

function select_value(options, value) {
	jQuery.each(options, function(i) {
		if ($(this).text().toLowerCase().split(" ")[0] == value.toLowerCase().split("%20")[0])
		$(this).attr("selected", "true");
	})
}

function select_index(options, value) {
	jQuery.each(options, function(i) {
		if ($(this).val() == value)
		$(this).attr("selected", "true");
	})
}

function append_date(date) {
	$("#month").children().remove();
	dt = new Date(date);
	dt.setMonth(dt.getMonth() -1);
	dt_current = new Date();
	index = 0;

	for (i=0; i<6; i++) {
		if(dt.getMonth() >= dt_current.getMonth() && dt.getFullYear() >= dt_current.getFullYear()) {
			index = index + 1;
			dt.setMonth(dt.getMonth() - 1);
		}
		else {
			break;
		}
	}
	dt = new Date(date);
	dt.setMonth(dt.getMonth() - index);

	for (i=0; i<13; i++) {
		$("#month").append("<option value=\"DMonth='"+verify_number(dt.getMonth()+1)+"' DYear='"+dt.getFullYear().toString().substr(2,2)+"'\">" + month[dt.getMonth()] + " " + dt.getFullYear() + "</option>");
		dt.setMonth(dt.getMonth() + 1);
	}

	$("#month option:eq("+index+")").attr("selected", "true");
	append_days(date);
}

function days_in_month(m, y) {
	if (m==3 || m==5 || m==8 || m==11)
	days = 30;
	else if (m==1 && (parseFloat(y/4)-parseInt(y/4))==0)
	days = 29;
	else if (m==1)
	days = 28;
	else
	days = 31;
	return days;
}

function append_days(date) {
	$("#day-name").text(date.toString().charAt(0)+date.toString().charAt(1)+date.toString().charAt(2));
	$("#day").children().remove();
	var days = days_in_month(date.getMonth(), date.getFullYear()), d = date.getDate();
	for (i=1; i<=days; i++)
	$("#day").append("<option value='"+verify_number(i)+"'>"+verify_number(i)+"</option>");
	if (days > d)
	$("#day option:eq("+(d-1)+")").attr("selected", "true");
	else
	$("#day option:eq("+(days-1)+")").attr("selected", "true");
}

function verify_number(number) {
	if (number < 10)
	number = "0" + number;
	return number;
}

function day_in_month_change() {
	var d = $("#day option:selected").val();
	days = days_in_month(
		parseInt($("#month option:selected").val().toString().split(" ")[0].split("=")[1].substr(1,2)) - 1,
		parseInt($("#month option:selected").text().toString().split(" ")[1])
	)
	if (d > days)
	d = days;
	append_days(new Date(
		parseInt($("#month option:selected").text().toString().split(" ")[1]),
		parseInt($("#month option:selected").val().toString().split(" ")[0].split("=")[1].substr(1,2)) - 1, d
	))
}

function search_form_validation() {
	var submit = true;
	if ($("#leaving_from option:selected").val() == "0") { 
		$("#dep_error").show();
		$("#leaving_from").prev("p").addClass("errors_p");
		submit = false;
	}
	else {
		$("#dep_error").hide();
		$("#leaving_from").prev("p").removeClass("errors_p");
	}
	if ($("#going_to option:selected").val() == "0") { 
		$("#des_error").show();
		$("#going_to").prev("p").addClass("errors_p");
		submit = false;
	}
	else {
		$("#des_error").hide();
		$("#going_to").prev("p").removeClass("errors_p");
	}
	if (parseInt($("select[name='nRooms']").val())*4 < parseInt($("select[name='nAdults']").val()) + parseInt($("select[name='nChildren']").val()) + parseInt($("select[name='nInfants']").val())) {
		$("select[name='nRooms']").prev("p").addClass("errors_p");
		$("select[name='nAdults']").prev("p").addClass("errors_p");
		$("select[name='nChildren']").prev("p").addClass("errors_p");
		$("select[name='nInfants']").prev("p").addClass("errors_p");
		$("#ppl_error").show();
		submit = false;
	}
	else {
		$("select[name='nRooms']").prev("p").removeClass("errors_p");
		$("select[name='nAdults']").prev("p").removeClass("errors_p");
		$("select[name='nChildren']").prev("p").removeClass("errors_p");
		$("select[name='nInfants']").prev("p").removeClass("errors_p");
		$("#ppl_error").hide();
	}
	if (submit)
	$("div#loader").show();
	//$("#search_loader").show('slow');
	return submit;
}
