if (
	($('#widget-wrapper').length != 0) || ($('#booking-widget').length != 0)) {


$(document).ready(function() {
	prepareWidget();
	});

function codesPop(codesURL,windowOptions,type) {
	var newWindow = window.open(codesURL,"popup",windowOptions);
	newWindow.type = type;
}


function prepareWidget() {
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	var widget = document.getElementById("booking-widget");
	var tabs = document.getElementById("widget-tabs");
	var tab = tabs.getElementsByTagName("h4");
	var hotels_content = document.getElementById("hotels");
	var flights_content = document.getElementById("flights");
	var cruises_content = document.getElementById("cruises");
	var cars_content = document.getElementById("cars");
	var packages_content = document.getElementById("packages");
	var content = new Array();
		content[0] = hotels_content;
		content[1] = flights_content;
		content[2] = packages_content;
		content[3] = cars_content;
		content[4] = cruises_content;


	getProviders();
	
	/*for (t=0;t < tab.length;t++) {
		if (t > 0) {
			tab[t].className = "non-active";
			content[t].style.display = "none";
			tab[t].onclick = function() {
				this.className = "active";
				content[t].style.display = "block";

			}
		} else {
			tab[t].className = "active";
			content[t].style.display = "block";
			
		}
	}*/
	
	tab[0].className = "active";
	tab[1].className = "non-active";
	tab[2].className = "non-active";
	tab[3].className = "non-active";
	tab[4].className = "non-active";
			
	content[0].style.display = "block";
	content[1].style.display = "none";
	content[2].style.display = "none";
	content[3].style.display = "none";
	content[4].style.display = "none";
	
	tab[0].onclick = function() {
		tab[0].className = "active";
		tab[1].className = "non-active";
		tab[2].className = "non-active";
		tab[3].className = "non-active";
		tab[4].className = "non-active";
		content[0].style.display = "block";
		content[1].style.display = "none";
		content[2].style.display = "none";
		content[3].style.display = "none";
		content[4].style.display = "none";
	}

	tab[1].onclick = function() {
		tab[0].className = "non-active";
		tab[1].className = "active";
		tab[2].className = "non-active";
		tab[3].className = "non-active";
		tab[4].className = "non-active";
		content[0].style.display = "none";
		content[1].style.display = "block";
		content[2].style.display = "none";
		content[3].style.display = "none";
		content[4].style.display = "none";
	}
		
	tab[2].onclick = function() {
		tab[0].className = "non-active";
		tab[1].className = "non-active";
		tab[2].className = "active";
		tab[3].className = "non-active";
		tab[4].className = "non-active";
		content[0].style.display = "none";
		content[1].style.display = "none";
		content[2].style.display = "block";
		content[3].style.display = "none";
		content[4].style.display = "none";
	}

	tab[3].onclick = function() {
		tab[0].className = "non-active";
		tab[1].className = "non-active";
		tab[2].className = "non-active";
		tab[3].className = "active";
		tab[4].className = "non-active";
		content[0].style.display = "none";
		content[1].style.display = "none";
		content[2].style.display = "none";
		content[3].style.display = "block";
		content[4].style.display = "none";
	}

	tab[4].onclick = function() {
		tab[0].className = "non-active";
		tab[1].className = "non-active";
		tab[2].className = "non-active";
		tab[3].className = "non-active";
		tab[4].className = "active";
		content[0].style.display = "none";
		content[1].style.display = "none";
		content[2].style.display = "none";
		content[3].style.display = "none";
		content[4].style.display = "block";
	}

	var submit_hotels = document.getElementById("submit_hotels");
	submit_hotels.onclick = processHotels;
	
	var submit_flights = document.getElementById("submit_flights");
	submit_flights.onclick = processFlights;
	
	var submit_packages = document.getElementById("submit_packages");
	submit_packages.onclick = processPackages;

	var submit_carrentals = document.getElementById("submit_carrentals");
	submit_carrentals.onclick = processCarRentals;
	
	var submit_cruises = document.getElementById("submit_cruises");
	submit_cruises.onclick = processCruises;

	var location_link = document.getElementById("location_link");
	location_link.onclick = function () {
		codesPop(this.getAttribute("href"),"width=400,height=400","location");
		return false;
	}

	var from_link = document.getElementById("from_link");
	from_link.onclick = function () {
		codesPop(this.getAttribute("href"),"width=725,height=400,scrollbars=yes,resizable=yes","from");
		return false;
	}

	var to_link = document.getElementById("to_link");
	to_link.onclick = function () {
		codesPop(this.getAttribute("href"),"width=725,height=400,scrollbars=yes,resizable=yes","to");
		return false;
	}

	var p_from_link = document.getElementById("p_from_link");
	p_from_link.onclick = function () {
		codesPop(this.getAttribute("href"),"width=725,height=400,scrollbars=yes,resizable=yes","p_from");
		return false;
	}

	var p_to_link = document.getElementById("p_to_link");
	p_to_link.onclick = function () {
		codesPop(this.getAttribute("href"),"width=725,height=400,scrollbars=yes,resizable=yes","p_to");
		return false;
	}

	var r_from_link = document.getElementById("r_from_link");
	r_from_link.onclick = function () {
		codesPop(this.getAttribute("href"),"width=725,height=400,scrollbars=yes,resizable=yes","r_from");
		return false;
	}

	var r_to_link = document.getElementById("r_to_link");
	r_to_link.onclick = function () {
		codesPop(this.getAttribute("href"),"width=725,height=400,scrollbars=yes,resizable=yes","r_to");
		return false;
	}

	var wrapper = document.getElementById("widget-wrapper");
		if (wrapper) {
			wrapper.style.display = "block";
		}
}

//custom functions for autocomplete
function formatItem(row,position,count,search) {
	var arr = eval(row.toString());
	var display = arr[0] + '<span>' + arr[1] + '</span>';
	return display;
}
function formatResult(row,position,count,search) {
	var arr = eval(row.toString());
	return arr[2];
}
function formatMatch(row,position,count) {
	var arr = eval(row.toString());
	return arr[2];
}

//initialize autocomplate and date picker
$(document).ready(function() {

	$("#location").autocomplete("/lib/data_widget_location.cfm",{
		"minChars": 3,
		"highlight":false,
		"formatItem":formatItem,
		"formatResult":formatResult,
		"selectFirst":false,
		"refreshCallback": function() { refreshHotelPartners(); }
	});
	$("#location").result(function(event, data, formatted) { 
		var arr = eval(data.toString());
		$("#location_id").val(arr[3]); 
		$("#location_name").val(arr[0]); 
		$("#state_code").val(arr[4]); 
		$("#country_code").val(arr[5]); 
	}); 

	$("#from").autocomplete("/lib/data_widget_airport.cfm",{
		"minChars": 3,
		"highlight":false,
		"formatItem":formatItem,
		"formatResult":formatResult,
		"mustMatch":true,
		"formatMatch":formatMatch,
		"selectFirst":true,
		"refreshCallback": function() { refreshAirportPartners(2,"from","to",listFlightProviders); }
	});

	$("#to").autocomplete("/lib/data_widget_airport.cfm",{
		"minChars": 3,
		"highlight":false,
		"formatItem":formatItem,
		"formatResult":formatResult,
		"mustMatch":true,
		"formatMatch":formatMatch,
		"selectFirst":true,
		"refreshCallback": function() { refreshAirportPartners(2,"from","to",listFlightProviders); }
	});

	$("#to").result(function(event, data, formatted) { 
		var arr = eval(data.toString());
		$("#state_code").val(arr[3]);
		$("#country_code").val(arr[4]);
	}); 

	$("#p_from").autocomplete("/lib/data_widget_airport.cfm",{
		"minChars": 3,
		"highlight":false,
		"formatItem":formatItem,
		"formatResult":formatResult,
		"mustMatch":true,
		"formatMatch":formatMatch,
		"selectFirst":true,
		"refreshCallback": function() { refreshAirportPartners(4,"p_from","p_to",listPackageProviders); }
	});

	$("#p_to").autocomplete("/lib/data_widget_airport.cfm",{
		"minChars": 3,
		"highlight":false,
		"formatItem":formatItem,
		"formatResult":formatResult,
		"mustMatch":true,
		"formatMatch":formatMatch,
		"selectFirst":true,
		"refreshCallback": function() { refreshAirportPartners(4,"p_from","p_to",listPackageProviders); }
	});

	$("#r_from").autocomplete("/lib/data_widget_airport.cfm",{
		"minChars": 3,
		"highlight":false,
		"formatItem":formatItem,
		"formatResult":formatResult,
		"mustMatch":true,
		"formatMatch":formatMatch,
		"selectFirst":true,
		"refreshCallback": function() { refreshAirportPartners(5,"r_from","r_to",listCarRentalProviders); }
	});

	$("#r_to").autocomplete("/lib/data_widget_airport.cfm",{
		"minChars": 3,
		"highlight":false,
		"formatItem":formatItem,
		"formatResult":formatResult,
		"mustMatch":true,
		"formatMatch":formatMatch,
		"selectFirst":true,
		"refreshCallback": function() { refreshAirportPartners(5,"r_from","r_to",listCarRentalProviders); }
	});

	var today = new Date();
	var tomorrow = new Date();
	tomorrow.setDate(today.getDate() + 1);

	function getCheckoutReturn(str) {
			var arr = str.split("-");
			var year = arr[0];
			var month = arr[1] - 1;
			var day = parseInt(arr[2]) + 1;
			var start_date =  new Date();
			start_date.setFullYear(year);
			start_date.setMonth(month);
			start_date.setDate(day);
			var new_year = start_date.getFullYear();
			var new_month = start_date.getMonth() + 1;
			var new_day = start_date.getDate();
			var tmp = new_year + "-0" + new_month  + "-0" + new_day;
			var re = /(-)0(\d\d)/g;
			end_date = tmp.replace(re,"$1$2");
			return end_date;
	}

	$('#checkin-datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true,
		showOn: 'button',
		minDate: today,
		onSelect: function(date) {0.
			$('#checkin-date').val(date);
			var checkout = getCheckoutReturn(date);
			$('#checkout-date').val(checkout);
		},
		beforeShow: function() {
			$(this).val($('#checkin-date').val());
			return {};
		}
	});
	
	$('#checkout-datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true,
		showOn: 'button',
		minDate: tomorrow,
		onSelect: function(date) {0.
			$('#checkout-date').val(date);
		},
		beforeShow: function() {
			$(this).val($('#checkout-date').val());
			return {};
		}
	});

	$('#depart-datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true,
		showOn: 'button',
		minDate: today,
		onSelect: function(date) {0.
			$('#depart-date').val(date);
			var return_date = getCheckoutReturn(date);
			$('#return-date').val(return_date);
		},
		beforeShow: function() {
			$(this).val($('#depart-date').val());
			return {};
		}
	});

	$('#return-datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true,
		showOn: 'button',
		minDate: tomorrow,
		onSelect: function(date) {0.
			$('#return-date').val(date);
		},
		beforeShow: function() {
			$(this).val($('#return-date').val());
			return {};
		}
	});
	
	$('#p_depart-datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true,
		showOn: 'button',
		minDate: today,
		onSelect: function(date) {0.
			$('#p_depart-date').val(date);
			var return_date = getCheckoutReturn(date);
			$('#p_return-date').val(return_date);
		},
		beforeShow: function() {
			$(this).val($('#p_depart-date').val());
			return {};
		}
	});

	$('#p_return-datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true,
		showOn: 'button',
		minDate: tomorrow,
		onSelect: function(date) {0.
			$('#p_return-date').val(date);
		},
		beforeShow: function() {
			$(this).val($('#p_return-date').val());
			return {};
		}
	});

	$('#r_depart-datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true,
		showOn: 'button',
		minDate: today,
		onSelect: function(date) {0.
			$('#r_depart-date').val(date);
			var return_date = getCheckoutReturn(date);
			$('#r_return-date').val(return_date);
		},
		beforeShow: function() {
			$(this).val($('#r_depart-date').val());
			return {};
		}
	});

	$('#r_return-datepicker').datepicker({
		dateFormat: 'yy-mm-dd',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true,
		showOn: 'button',
		minDate: tomorrow,
		onSelect: function(date) {0.
			$('#r_return-date').val(date);
		},
		beforeShow: function() {
			$(this).val($('#r_return-date').val());
			return {};
		}
	});

});

};


