// start: select the appropriate stylesheet
if (!path) var path = ""; // if the path variable was set before the call to this (.js) file

// start: mouseover script and image preloader
function msover(img,ref)
{
	document.images[img].src = ref;
}

function msout(img,ref)
{
	document.images[img].src = ref;
}

//xx = new Image();
//xx.src="images/question_on.gif";

var currentoption;
var currenttype;
var oldoption;


function SetRadio(id, value){
	var radio_id = document.getElementById(id);
	radio_id.checked=value;
}

function Hide(id, hidden){
	var element_id = document.getElementById(id);
	element_id.style.display = (hidden)?'none':'';
}

function SetLabel(id, value){
	var label_id = document.getElementById(id);
	label_id.innerHTML = value;
}

function SetOption(option){
	if (option==currentoption){
		return;
	}
	oldoption = currentoption;
	currentoption = option;
	if (option == 0){
		SetRadio('searchType1',true);
		SetRadio('searchType2',false);
		SetRadio('searchType3',false);
		Hide('searchType1Option',false);
		Hide('searchType2Option',false);
		Hide('searchType3Option',false);
		Hide('TextSearchLabel', true);
		Hide('TextSearchField', true);
		SetLabel('searchType1Label','I want the room reservation at the beginning of my trip.');
		SetLabel('searchType2Label','I want the room reservation at the end of my trip.');
		SetLabel('searchType3Label','I want the room reservation at the beginning and end of my trip.');
		SetType('0', true);
	}
	else if (option == 1){
		SetRadio('searchType1',true);
		SetRadio('searchType2',false);
		SetRadio('searchType3',false);
		Hide('searchType1Option',false);
		Hide('searchType2Option',false);
		Hide('searchType3Option',false);
		Hide('TextSearchLabel', true);
		Hide('TextSearchField', true);
		SetLabel('searchType1Label','I want to park my car and stay at the hotel before my cruise.');
		SetLabel('searchType2Label','I want to park my car and stay at the hotel both before and after my cruise.');
		SetLabel('searchType3Label','I am flying into the cruise port, and do not need any parking accommodations.');
		SetType('0', true);
	}
	else if (option == 2){
		SetRadio('searchType1',true);
		SetRadio('searchType2',false);
		SetRadio('searchType3',false);
		Hide('searchType1Option',true);
		Hide('searchType2Option',true);
		Hide('searchType3Option',true);
		Hide('TextSearchLabel', false);
		Hide('TextSearchField', false);
		SetLabel('searchType1Label','');
		SetLabel('searchType2Label','');
		SetLabel('searchType3Label','');
		SetType('0', true);
	}
}

function GetOption(){
	var Element = document.getElementById('searchOption1');
	if (Element.checked){
		return 0;
	}
	Element = document.getElementById('searchOption2');
	if (Element.checked){
		return 2;
	}
	Element = document.getElementById('searchOption3');
	if (Element.checked){
		return 1;
	}
}

function GetType(){
	var Element = document.getElementById('searchType1');
	if (Element.checked){
		return 0;
	}
	Element = document.getElementById('searchType2');
	if (Element.checked){
		return 1;
	}
	Element = document.getElementById('searchType3');
	if (Element.checked){
		return 2;
	}
}

function SetType(searchtype, isOptionChange){
	var option = GetOption();
	var beforechange = currenttype;
	
	if (currenttype == searchtype && isOptionChange == false){
		return;
	}
	currenttype = searchtype;
	
	var channel = 0;
	if (searchtype == 0){
		if (option == 0){
			searchtype = 'PSF';
			channel = 0;
		}
		else if (option == 1){
			searchtype = 'PSC';
			channel = 1;
		}
		else if (option == 2){
			searchtype = 'HRO';
			channel = 2;
		}
	}
	else if (searchtype == 1){
		if (option == 0){
			searchtype = 'PFS';
			channel = 0;
		}
		else if (option == 1){
			searchtype = 'PSCS';
			channel = 1;
		}
		else if (option == 2){
			searchtype = 'HRO';
			channel = 2;
		}
	}
	else if (searchtype == 2){
		if (option == 0){
			searchtype = 'PSFS';
			channel = 0;
		}
		else if (option == 1){
			searchtype = 'FSC';
			channel = 3;
		}
		else if (option == 2){
			searchtype = 'HRO';
			channel = 2;
		}
	}
	
	if (beforechange == 1 && (oldoption == 0 || (isOptionChange == false && option == 0)) && searchtype != 'PFS'){
		var checkin_date = document.getElementById('CheckInDate');
		var vehicle_date = document.getElementById('VehiclePickUp');
		checkin_date.value = vehicle_date.value;
	}
	
	//show and hide fields
	if (searchtype == 'PSF' || searchtype == 'PSC'){
		Hide('CheckInDate', false);
		Hide('CheckOutDate', true);
		Hide('VehiclePickUp', false);
		Hide('CheckInDateBtn', false);
		Hide('CheckOutDateBtn', true);
		Hide('VehiclePickUpBtn', false);
		SetLabel('CheckInDateLabel', 'Check-In Date at Hotel');
		SetLabel('CheckOutDateLabel', '');
		SetLabel('VehiclePickUpLabel', 'Vehicle Pick-Up Date');
	}
	else if (searchtype == 'PFS'){
		Hide('CheckInDate', false);
		Hide('CheckOutDate', false);
		Hide('VehiclePickUp', false);
		Hide('CheckInDateBtn', false);
		Hide('CheckOutDateBtn', false);
		Hide('VehiclePickUpBtn', false);
		SetLabel('CheckInDateLabel', 'Check-In Date at Hotel');
		SetLabel('CheckOutDateLabel', 'Check-Out Date at Hotel');
		SetLabel('VehiclePickUpLabel', 'Date when parking begins');
	}
	else if (searchtype == 'PSFS' || searchtype == 'PSCS'){
		Hide('CheckInDate', false);
		Hide('CheckOutDate', false);
		Hide('VehiclePickUp', false);
		Hide('CheckInDateBtn', false);
		Hide('CheckOutDateBtn', false);
		Hide('VehiclePickUpBtn', false);
		SetLabel('CheckInDateLabel', 'Check-In Date at Hotel');
		SetLabel('CheckOutDateLabel', 'Return Check-In Date at Hotel');
		SetLabel('VehiclePickUpLabel', 'Vehicle Pick-Up Date');
	}
	else if (searchtype == 'FSC' || searchtype == 'HRO'){
		Hide('CheckInDate', false);
		Hide('CheckOutDate', false);
		Hide('VehiclePickUp', true);
		Hide('CheckInDateBtn', false);
		Hide('CheckOutDateBtn', false);
		Hide('VehiclePickUpBtn', true);
		SetLabel('CheckInDateLabel', 'Check-In Date at Hotel');
		SetLabel('CheckOutDateLabel', 'Check-Out Date at Hotel');
		SetLabel('VehiclePickUpLabel', '');
	}
		UpdateDates(searchtype);
		LoadCityList(GetCities(channel));
	return true;
}

function UpdateDates(searchtype){
	var checkin_date = document.getElementById('CheckInDate');
	var checkout_date = document.getElementById('CheckOutDate');
	var vehicle_date = document.getElementById('VehiclePickUp');
	var checkin_time = new Date();
	var checkout_time = new Date();
	var vehicle_time = new Date();
	
	checkin_time.setTime(Date.parse(checkin_date.value));

	if (searchtype == 'PSF' || searchtype == 'PSC'){
		vehicle_time.setTime(checkin_time.getTime()+86400000*7);
	}
	else if (searchtype == 'PSFS' || searchtype == 'PSCS'){
		checkout_time.setTime(checkin_time.getTime()+86400000*7);
		vehicle_time.setTime(checkin_time.getTime()+86400000*8);
	}
	else if (searchtype == 'PFS'){
		vehicle_time.setTime(checkin_time.getTime());
		checkout_time.setTime(checkin_time.getTime()+86400000*8);
		checkin_time.setTime(checkin_time.getTime()+86400000*7);
	}
	else if (searchtype == 'FSC' || searchtype == 'HRO'){
		checkout_time.setTime(checkin_time.getTime()+86400000);
	}
	checkin_date.value = checkin_time.getMonth()+1 + '/' + checkin_time.getDate() + '/' + checkin_time.getFullYear();
	checkout_date.value = checkout_time.getMonth()+1 + '/' + checkout_time.getDate() + '/' + checkout_time.getFullYear();
	vehicle_date.value = vehicle_time.getMonth()+1 + '/' + vehicle_time.getDate() + '/' + vehicle_time.getFullYear();
}

function LoadCityList(list){
	var cityselect = document.getElementById('departure_city_id');
	var currentindex = cityselect.selectedIndex;
	var currentvalue = cityselect.options[currentindex].value;
	
	while (cityselect.childNodes.length){
		cityselect.removeChild(cityselect.firstChild);
	}
	
	var fisrtelement = document.createElement('option');
	fisrtelement.innerHTML='Select a City';
	fisrtelement.value='0';
	cityselect.appendChild(fisrtelement);
	
	for (i = 0; i < list.length; i++){
		var optgroup = document.createElement('optgroup');
		optgroup.label=list[i][0];
		for (y = 0; y < list[i][1].length; y++){
			var temp = list[i][1][y].split('||');
			var element = document.createElement('option');
			element.innerHTML=temp[1];
			element.value=temp[0];
			optgroup.appendChild(element);
		}
		cityselect.appendChild(optgroup);
	}
	for (i = 0; i < cityselect.length; i++){
		cityselect.selectedIndex=0;
		if (cityselect.options[i].value == currentvalue){
			cityselect.selectedIndex=i;
			break;
		}
	}
}

function SetDefaultCheckIn(){
	var element = document.getElementById('CheckInDate');
	var time = new Date();
	time.setTime(time.getTime()+86400000*7);
	element.value = time.getMonth()+1 + '/' + time.getDate() + '/' + time.getFullYear();
}

function GetSearchType(){
	var searchtype = GetType();
	var option = GetOption();
	
	if (searchtype == 0){
		if (option == 0){
			searchtype = 'PSF';
			channel = 0;
		}
		else if (option == 1){
			searchtype = 'PSC';
			channel = 1;
		}
		else if (option == 2){
			searchtype = 'HRO';
			channel = 2;
		}
	}
	else if (searchtype == 1){
		if (option == 0){
			searchtype = 'PFS';
			channel = 0;
		}
		else if (option == 1){
			searchtype = 'PSCS';
			channel = 1;
		}
		else if (option == 2){
			searchtype = 'HRO';
			channel = 2;
		}
	}
	else if (searchtype == 2){
		if (option == 0){
			searchtype = 'PSFS';
			channel = 0;
		}
		else if (option == 1){
			searchtype = 'FSC';
			channel = 3;
		}
		else if (option == 2){
			searchtype = 'HRO';
			channel = 2;
		}
	}
	return searchtype;
}

function CheckDatesForError(){
	var checkin_date = document.getElementById('CheckInDate').value;
	var checkout_date = document.getElementById('CheckOutDate').value;
	var vehicle_date = document.getElementById('VehiclePickUp').value;
	var cityselect = document.getElementById('departure_city_id');
	
	// convert dates to common format
	checkin_date = FormatDate(checkin_date);
	checkout_date = FormatDate(checkout_date);
	vehicle_date = FormatDate(vehicle_date);
	// check for correct dates format
	if (checkin_date.search(/^\d{1,2}\/\d{1,2}\/\d{4}$/) == -1 || checkout_date.search(/^\d{1,2}\/\d{1,2}\/\d{4}$/) == -1 || vehicle_date.search(/^\d{1,2}\/\d{1,2}\/\d{4}$/) == -1 ){
		SetDefaultCheckIn();
		return('Incorrect date format. Dates must be in MM/DD/YYYY format');
	}
	//get timestamps
	var checkin_date_ts = new Date();
	var checkout_date_ts = new Date();
	var vehicle_date_ts = new Date();
	checkin_date_ts.setTime(Date.parse(checkin_date));
	checkout_date_ts.setTime(Date.parse(checkout_date));
	vehicle_date_ts.setTime(Date.parse(vehicle_date));
	
	var today = new Date();
	var today_ts = today.getTime() - 86400000;
	
	var searchtype = GetType();
	var searchoption = GetOption();
	
	// check for dates to be in desired range
	if (checkin_date_ts.getTime() < today_ts){
		SetDefaultCheckIn();
		return('Check-in date must be greater than today date');
	}
	// PSF PSC
	if ( (searchoption == 0 || searchoption == 1) && searchtype == 0){
		if (vehicle_date_ts.getTime() <= checkin_date_ts.getTime()){
			return('Vehicle pick-up date must be greater than check-out date');
		}
	}
	// PFS
	if ( searchoption == 0 && searchtype == 1){
		if (checkin_date_ts.getTime() >= checkout_date_ts.getTime()){
			return('Check-in date must be less than check-out date');
		}
		if (vehicle_date_ts.getTime() >= checkin_date_ts.getTime()){
			return('Vehicle parking begin date must be less than check-in date');
		}
		if (vehicle_date_ts.getTime() < today_ts){
			SetDefaultCheckIn();
			return('Vehicle parking begin date must be greater than today date');
		}
	}
	// PSFS PSCS
	if ( (searchoption == 0 && searchtype == 2) || (searchoption == 1 && searchtype == 1) ){
		if (checkin_date_ts.getTime() >= checkout_date_ts.getTime() ){
			return('Check-in date must be less than return check-in date');
		}
		if (vehicle_date_ts.getTime() <= checkout_date_ts.getTime()){
			return('Vehicle pick-up date must begreater than return check-in date');
		}
	}
	// HRO FSC
	if ( searchoption == 2 || (searchoption == 1 && searchtype == 2)){
		if (checkin_date_ts.getTime() >= checkout_date_ts.getTime()){
			return('Check-in date must be less than check-out date');
		}
	}
	
	return '';
}

function AutoCompleteDates(){
	// TODO
	var errorMessage = CheckDatesForError();
	var notifyMessage = 'Chosen date is not valid for selected period. Dates are set to default.';
	if (errorMessage != ''){
		UpdateDates(GetSearchType());
		//alert(notifyMessage+"\n\n"+errorMessage);
		return false;
	}
	return true;
}


function CheckInputParams(){
	var cityselect = document.getElementById('departure_city_id');
	var cityfield = document.getElementById('TextSearchField');
	var adults = document.getElementById('adults');
	var rooms = document.getElementById('room');

	var adultscount = adults.options[adults.selectedIndex].value;
	var roomscount = rooms.options[rooms.selectedIndex].value;

	// check for city selected
	if (cityselect.options[cityselect.selectedIndex].value == 0 && (GetOption() != 2 || (GetOption() == 2 && cityfield.value==''))) {
	//if (cityselect.options[cityselect.selectedIndex].value == 0) {
		alert ('Please, select departure city');
		return false;
	}
	if (adultscount < roomscount){
		alert('Number of rooms can not be greater than number of adults');
		return false;
	}
	
	var errorMessage = CheckDatesForError();
	if (errorMessage != ''){
		UpdateDates(GetSearchType);
		alert(errorMessage);
		return false;
	}
	return true;
}

function FormatDate(datestring){

	var temp = new Array();
	temp = datestring.split(/[-\/]/);
	if (temp.length != 3){
//		alert('Incorrect date format');
		return datestring;
	}

	if (temp[2].length == 2){
		temp[2] = '20'+temp[2];
	}
	datestring = temp[0]+'/'+temp[1]+'/'+temp[2];

	return datestring;
}

function SaveSearchPopUp(){
	var url = "saveSearch.php";
	myWin = window.open (url,'newWin','scrollbars=no,status=no,width=385,height=344'); //width=385,height=344'
	return false;
}
