

/*
 * Setup variables for use in form calcs
 */
var tf_numMonths = 14;
// Moved Months names into PHP include for multi-lingual purposes
//var tf_monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

var tf_showDateMessages = true;

// Moved messages into PHP include for multi-lingual purposes
//var messageDepartBeforeArrive = "Your arrival date can't be after your departure date";
//var messageArriveAfterDepart = "Your departure date can't be before your arrival date";
//var messageNoAirportSelected = "Please select an airport";
//var messageNoDestinationSelected = "Please select a destination";
//var messageRequiredContactDetails = "We will need your Name, email and a contact number.";

var tf_datesIncludeToday = false;

//var tf_refDate = new Date();
var tf_outDate = new Date();
var tf_retDate = new Date();

var tf_hidden_out_date = document.getElementById("tf_hidden_out_date").value;
var tf_hidden_ret_date = document.getElementById("tf_hidden_ret_date").value;

if ( !tf_datesIncludeToday) {
	tf_outDate.setDate( tf_outDate.getDate()+1 );
	tf_retDate.setDate( tf_retDate.getDate()+1 );
}	

if (tf_hidden_out_date!="") {
	var dateParts = tf_hidden_out_date.split("-");
	tf_outDate.setFullYear(dateParts[0]);
	tf_outDate.setDate(dateParts[2]);
	tf_outDate.setMonth(dateParts[1]-1);
}

if (tf_hidden_ret_date!="") {
	var dateParts = tf_hidden_ret_date.split("-");
	tf_retDate.setFullYear(dateParts[0]);
	tf_retDate.setDate(dateParts[2]);
	tf_retDate.setMonth(dateParts[1]-1);
}

//alert(tf_outDate.getFullYear()+"/"+tf_outDate.getMonth()+"/"+tf_outDate.getDate());
//alert(tf_retDate.getFullYear()+"/"+tf_retDate.getMonth()+"/"+tf_retDate.getDate());

var tf_yearRange = new Array();

// setup the arrive and depart calendars
var tf_outTrigger = document.getElementById("tf_out_date_trigger");
var tf_retTrigger = document.getElementById("tf_ret_date_trigger");

var tf_outCal = null;
var tf_retCal = null;

var tf_outSet = false;
var tf_retSet = false;

setup_quick_quote_form();




/*
 * Setup the form and onjects used by the quick quote form
 */ 
function setup_quick_quote_form() {
	var transfer_form = document.getElementById("transfer_form");

	var tf_today = new Date();
	if (!tf_datesIncludeToday) {
		tf_today.setDate( tf_today.getDate()+1 );
	}
	var todaysDate = tf_today.getDate();
	var todaysMonth = tf_today.getMonth();
	var todaysYear = tf_today.getFullYear();

	var numYears = Math.ceil(tf_numMonths/12);
	var startYear = todaysYear;

	for (var i=0; i<numYears; i++){
		tf_yearRange[i] = startYear;
		startYear++;
	}

	buildSelectDates( transfer_form.tf_out_day, transfer_form.tf_out_month );
	buildSelectDates( transfer_form.tf_ret_day, transfer_form.tf_ret_month );

	transfer_form.tf_out_day.selectedIndex = tf_today.getDate()-1;
	transfer_form.tf_ret_day.selectedIndex = tf_today.getDate()-1;
  
	setDateSels( "outbound" );
	setDateSels( "return" );
	
	var selectLang = document.getElementById("widg_lang").value;

	//x_get_new_depart_locs(selectLang, get_depart_locs_cb);
	
}



/*
 * Handle the Depart location change
 */
function countryChanged( sel ) {
	// user has changed the country select menu
	var departCountry = sel.options[sel.selectedIndex].value;
	var selectLang = document.getElementById("widg_lang").value;
        var fl_public = document.getElementById("fl_public").value;
	var fl_agent = document.getElementById("fl_agent").value;
        var id_website = document.getElementById("id_website").value;
	
	x_get_new_depart_locs(selectLang, departCountry, fl_public, 0, fl_agent, id_website, get_depart_locs_cb);
} 

/*
 * Handle the Depart country change
 */

function departLocChanged( sel ) {
	// user has changed the Depart (airport) select menu
	var departLoc = sel.options[sel.selectedIndex].value;
	var selectLang = document.getElementById("widg_lang").value;
	var fl_public = document.getElementById("fl_public").value;
	var fl_agent = document.getElementById("fl_agent").value;
	var selectCompany = document.getElementById("id_website").value;
	
	if (document.getElementById('is_admin_area'))
	{
		var id_tab = 1;
	}
	else
	{
		var id_tab = document.getElementById('id_tab').value;
	}
	
	x_get_new_destinations( departLoc, selectLang, fl_public, fl_agent, selectCompany, id_tab, departLocChanged_cb );
} 


/*
 * Arrive calendar functions
 */
function arriveShowCalendar(id) {
	// assign the passed img element for the onclick event
	var el = document.getElementById(id);
  if (tf_outCal != null) {
    tf_outCal.hide();
  } else {
    tf_outCal = new Calendar(1, null, arriveCalSelected, arriveCalCloseHandler);
		tf_outCal.weekNumbers = false;
		tf_outCal.showsOtherMonths = false;
		tf_outCal.setRange(tf_yearRange[0], tf_yearRange[1]);
	  tf_outCal.setDateStatusHandler(dateStatusHandler);
	  tf_outCal.setDateFormat("%Y-%m-%d");
    tf_outCal.create();
  }
	tf_outCal.setDate(tf_outDate);
  tf_outCal.showAtElement(el, "Tr");        // show the calendar
  return false;
}
function arriveCalCloseHandler(cal) {
  cal.hide();                        // hide the calendar
  //tf_outCal.hide();                        // hide the calendar
  tf_outCal = null;
//alert(tf_outDate);
}
function arriveCalSelected(cal, date) {

	dateParts = date.split('-');
	tf_outDate.setFullYear(dateParts[0]);
	tf_outDate.setMonth(dateParts[1]-1);
	//tf_outDate.setMonth(dateParts[1]);
	tf_outDate.setDate(dateParts[2]);
//	document.getElementById("tf_hidden_out_date").value = date;
	document.getElementById("tf_hidden_out_date").value = dateParts[0]+"-"+(dateParts[1]-1)+"-"+dateParts[2];
	//checkOutOfDate( "outbound" );
	//checkDateOrder( "outbound" );
	setDateSels( "outbound" );
	updateHiddenDate( "outbound" );
	if (cal.dateClicked) {
		cal.callCloseHandler();
	}
}


/*
 * Depart calendar functions
 */
function departShowCalendar(id) {
	// assign the passed img element for the onclick event
	var el = document.getElementById(id);
  if (tf_retCal != null) {
    tf_retCal.hide();
  } else {
    tf_retCal = new Calendar(1, null, departCalSelected, departCalCloseHandler);
		tf_retCal.weekNumbers = false;
		tf_retCal.showsOtherMonths = false;
		tf_retCal.setRange(tf_yearRange[0], tf_yearRange[1]);
	  tf_retCal.setDateStatusHandler(dateStatusHandler);
	  tf_retCal.setDateFormat("%Y-%m-%d");
    tf_retCal.create();
  }
	tf_retCal.setDate(tf_retDate);
  tf_retCal.showAtElement(el, "Tr");        // show the calendar
  return false;
}
function departCalCloseHandler(cal) {
	cal.hide();                        // hide the calendar
  //tf_retCal.hide();                        // hide the calendar
  //tf_retCal = null;
}
function departCalSelected(cal, date) {

	dateParts = date.split('-');
	tf_retDate.setFullYear(dateParts[0]);
	tf_retDate.setMonth(dateParts[1]-1);
	tf_retDate.setDate(dateParts[2]);
//	document.getElementById("tf_hidden_ret_date").value = date;
	document.getElementById("tf_hidden_ret_date").value = dateParts[0]+"-"+(dateParts[1]-1)+"-"+dateParts[2];

	//checkOutOfDate( "return" );
	//checkDateOrder( "return" );
	setDateSels( "return" );
	updateHiddenDate( "return" );
	if (cal.dateClicked) {
		cal.callCloseHandler();
	}
/*
	if (tf_retDate.dateClicked) {
		tf_retDate.callCloseHandler();
	}
*/
}


/*
 * function used by the popup calendar object to disable previous dates in the popup calendars
 */
function dateStatusHandler(date) {
	var retVal = false;
	var tf_today = new Date();
	if ( tf_datesIncludeToday ) {
		tf_today.setDate(tf_today.getDate()-1);
	}
	if (date<tf_today) {
		retVal = true;
	}
	return retVal;
}


/*
 * called when one of the day selectors is changed, changes the date object associated with arrival or departure
 */
function dayChanged( dateSel ) {
	if ( dateSel.id == "tf_out_day" ) {
		tf_outDate.setDate( dateSel.options[dateSel.selectedIndex].value );
		//checkOutOfDate( "outbound" );
		//checkDateOrder( "outbound" );
		updateHiddenDate( "outbound" );
	}
	if ( dateSel.id == "tf_ret_day" ) {
		tf_retDate.setDate( dateSel.options[dateSel.selectedIndex].value );
		//checkOutOfDate( "return" );
		//checkDateOrder( "return" );
		updateHiddenDate( "return" );
	}
}


function updateHiddenDate( selector ) {
	if (selector=="return"){
		document.getElementById("tf_hidden_ret_date").value = tf_retDate.getFullYear()+"-"+(tf_retDate.getMonth()+1)+"-"+tf_retDate.getDate();
	}
	if (selector=="outbound"){
		document.getElementById("tf_hidden_out_date").value = tf_outDate.getFullYear()+"-"+(tf_outDate.getMonth()+1)+"-"+tf_outDate.getDate();
	}
}


/*
 * check if the arrival and departure dates are not the wrong way round
 */
function checkDateOrder( selector ) {

	var tf_today = new Date();
	tf_today.setDate( tf_today.getDate()-1 );
	
	if (tf_retDate<tf_outDate) {
		if (selector=="outbound"){
			if (tf_showDateMessages){
				if ( !document.getElementById("transfer_form").tf_journey[1].checked ) { // one-way
					alert(messageArriveAfterDepart);
					return false;
				}
			}
			//tf_retDate = new Date(tf_outDate);
			//setDateSels("return");
		}

		if (selector=="return"){
			if (tf_showDateMessages){
				alert(messageDepartBeforeArrive);
				return false;
			}
			//tf_outDate = new Date(tf_retDate);
			//setDateSels("outbound");
		}
	} 
	
	if (tf_outDate<tf_today) {
			//alert(messageDepartBeforeArrive);	
			alert('Outbound date cannot be in the past');	
			return false;

	} 
	
	if (tf_retDate<tf_today) {	
			//alert(messageDepartBeforeArrive);	
			alert('Return date cannot be in the past');	
			return false;	
	} else {
		
		//alert(selector);
		
		if (selector=="return"){
			var out_str = tf_outDate.getDate()+' '+tf_outDate.getMonth()+' '+tf_outDate.getYear();
			var ret_str = tf_retDate.getDate()+' '+tf_retDate.getMonth()+' '+tf_retDate.getYear();
			 
			//alert(out_str+' '+ret_str);
			
			if (out_str==ret_str) {
				//alert(messageDepartBeforeArrive);	
				var agree = confirm('Are you sure you want to return on the same date?');
				
				if (agree){
					return true ;
				} else {
					return false ;
				}
				
			} else {
				return true;	
			}	
		} else { 
			return true;
		}	
	}	
	
	
}


/*
 * check if the arrival and departure dates are not the wrong way round
 */
function checkOutOfDate( selector ) {
	var tf_today = new Date();
	if ( !tf_datesIncludeToday ) {
		tf_today.setDate( tf_today.getDate()+1 );
	}
	if (selector=="outbound"){
		if ( tf_outDate<tf_today ) {
			tf_outDate = new Date(tf_today);
			setDateSels("outbound");
		}
	}
	if (selector=="return"){
		if ( tf_retDate<tf_today ) {
			tf_retDate = new Date(tf_today);
			setDateSels("return");
		}
	}
}


/*
 * sets the specified selectors to reflect appropriate date
 */
function setDateSels( selector ) {
	if (selector=="outbound"){
		var monthSel = document.getElementById("tf_out_month");
		var daySel = document.getElementById("tf_out_day");

		for (var i=0; i<monthSel.options.length; i++) { 
			var val = monthSel.options[i].value;
			var year = val.substring(0,4);
			var month = val.substring(5);
			
			if ( year==tf_outDate.getFullYear() && month==tf_outDate.getMonth() ) { 
				monthSel.selectedIndex = i;
				break;
			}
		}

		// we have to rebuild the days for the current month
		buildDays( year, month, tf_outDate.getDate(), daySel );

		daySel.selectedIndex = tf_outDate.getDate()-1;
	}

	if (selector=="return"){
		var monthSel = document.getElementById("tf_ret_month");
		var daySel = document.getElementById("tf_ret_day");

		for (var i=0; i<monthSel.options.length; i++) {
			var val = monthSel.options[i].value;
			var year = val.substring(0,4);
			var month = val.substring(5);
			
			if ( year==tf_retDate.getFullYear() && month==tf_retDate.getMonth() ) {
				monthSel.selectedIndex = i;
				break;
			}
		}

		// we have to rebuild the days for the current month
		buildDays( year, month, tf_retDate.getDate(), daySel );

		daySel.selectedIndex = tf_retDate.getDate()-1;
	}
}


/*
 * handle month/year change - gets the month/year from the value of the chosen selection
 * adjusts the tf_outDate ot tf_retDate depending on which selector was changed
 */
function monthChanged( monthSel, daySel ) {
	var val = monthSel.options[monthSel.selectedIndex].value;
	var newYear = val.substring(0,4);
	var newMonth = val.substring(5);

	if ( monthSel.id == "tf_out_month" ) {
		tf_outDate.setMonth( newMonth );
		tf_outDate.setFullYear( newYear );
		buildDays( newYear, newMonth, tf_outDate.getDate(), document.getElementById(daySel) ) ;
		//checkOutOfDate( "outbound" );
		//checkDateOrder( "outbound" );
		updateHiddenDate( "outbound" );
	}
	if ( monthSel.id == "tf_ret_month" ) {
		tf_retDate.setMonth( newMonth );
		tf_retDate.setFullYear( newYear );
		buildDays( newYear, newMonth, tf_retDate.getDate(), document.getElementById(daySel) );
		//checkOutOfDate( "return" );
		//checkDateOrder( "return" );
		updateHiddenDate( "return" );
	}
}


/*
 * When a one-way journey is selected we hide the return departure dates
 */
function journeyChanged() {
	var transfer_form = document.getElementById("transfer_form");
//	var journeySel = document.getElementById("tf_journey");

	var departTab = document.getElementById("tf_out_table");
	var departLab = document.getElementById("tf_out_label");

	if ( transfer_form.tf_journey[1].checked ) {
		departTab.style.display = "none";
		departLab.style.display = "none";
	} else {
		departTab.style.display = "block";
		departLab.style.display = "block";
	}
}


/*
 * Build the months in the month selectors
 */
function buildSelectDates( daySel, monthSel ) {
	var tf_today = new Date();
	var thisYear = tf_today.getFullYear();
	var thisMonth = tf_today.getMonth();

	monthSel.length = 0;

	namePtr = thisMonth;
	yearDisplay = thisYear;
	for (var i=0; i<tf_numMonths; i++) {
		monthSel.options[i] = new Option();
		monthSel.options[i].text = tf_monthNames[namePtr]+" "+yearDisplay;
		monthSel.options[i].value = yearDisplay+"/"+namePtr;
		if (namePtr==11) {
			namePtr=0
			yearDisplay++;
		} else {
			namePtr++;
		}
	}

	buildDays( thisYear, thisMonth, tf_outDate.getDate(), daySel );

}


/*
 * build the days for the selected year
 */
function buildDays( year, month, day, daySel ) {
//	var numDays = getDaysInMonth( year, month );
	var testDay = new Date( year, month, 1 );
	var numDays = testDay.getMonthDays()
	daySel.length = 0;
	for (var i=0; i<numDays; i++) {
		daySel.options[i] = new Option();
		daySel.options[i].text = i+1;
		daySel.options[i].value = i+1;
	}
	daySel.selectedIndex = day-1;
}

/*
function getDaysInMonth( year, month ) {
	timeA = new Date(year, month+1, 1);
	timeDifference = timeA - 86400000;
	timeB = new Date(timeDifference);
	alert("month/days: "+month+"/"+timeB.getDate())
	return timeB.getDate();
}
*/



function checkQuickQuote() {
	var departLoc = document.getElementById("tf_depart_loc");
	var arriveLoc = document.getElementById("tf_destin_loc");
	
	var f = document.getElementById("transfer_form");
	
	for( var i = 0; i < f.elements.length; i++ )  
	{    
		if (f.elements[i].name == 'tf_journey' && f.elements[i].checked && f.elements[i].value == 'one-way'){
			if (!checkDateOrder( "outbound" )){
				return false;		
			}
		}	
		
		if (f.elements[i].name == 'tf_journey' && f.elements[i].checked && f.elements[i].value == 'return'){
			if (!checkDateOrder( "return" )){
				return false;		
			}
		}
	} 	
	
	if ( document.getElementById("tf_hidden_out_date").value == "" ) {
		updateHiddenDate( "outbound" );
	}
		
	if ( document.getElementById("tf_hidden_ret_date").value == "" ) {
		updateHiddenDate( "return" );
	}
	
	if ( departLoc.selectedIndex==0 ) {
		alert(messageNoAirportSelected);
		return false;
	}
			
	if ( arriveLoc.selectedIndex==0 ) {
		alert(messageNoDestinationSelected);
		return false;
	}

	return true;
	
}


function tf1_check_form() {
	var name = document.getElementById("tf1_surname").value;
	var first_name = document.getElementById("tf1_firstname").value;
	var email = document.getElementById("tf1_email").value;
	var tel = document.getElementById("tf1_tel").value;
	var mobile = document.getElementById("tf1_mobile").value;

	if ( name!="" && first_name!="" && email!="" && ( tel!="" || mobile!="" ) ) {
		return true;
	} else {
		alert(messageRequiredContactDetails);
		return false;
	}
}


/*
 * functions to set the Depart and Arrive selectors when the form is submitted to itself (because of the SAJAX calls)
 */
function setDepartSelection() {

	if ( !tf_retSet ) {
		var departSel = document.getElementById("tf_depart_loc");
		var initialDepValue = document.getElementById("tf_initial_depart_loc").value;
		for (var i=0; i<departSel.length; i++) {
			var val = departSel.options[i].value;
			if ( val == initialDepValue ) {
				departSel.selectedIndex = i;
				departLocChanged(departSel);;
				break;
			}
		}
	}
	tf_retSet = true;
}

function setArriveSelection() {

	if ( !tf_outSet ) {
		var arriveSel = document.getElementById("tf_destin_loc");
		var initialArrValue = document.getElementById("tf_initial_destin_loc").value;
		for (var i=0; i<arriveSel.length; i++) {
			var val = arriveSel.options[i].value;
			if ( val == initialArrValue ) {
				arriveSel.selectedIndex = i;
				break;
			}
		}
	}
	tf_outSet = true;
}






/*
 * Handle SAJAX responses from getting locations
 */
function get_depart_locs_cb( response ) {

	//alert(response);
	
	var data = response.parseJSON();

	var sel_depart_loc = document.getElementById("tf_depart_loc");
	var start = 1;
	sel_depart_loc.options.length = 1;
	
	var sel_arrive_loc = document.getElementById("tf_destin_loc");
	sel_arrive_loc.options.length = 1;

	for(var i in data['depart_loc'])
	{
		if(i != 'toJSONString'){
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#232;/g,"\u00E8");
                        data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#233;/g,"\u00E9");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#226;/g,"\u00E2");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#234;/g,"\u00EA");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#224;/g,"\u00E0");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#203;/g,"\u00EB");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#219;/g,"\u00FB");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#250;/g,"\u00FA");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#241;/g,"\u00F1");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#244;/g,"\u00F4");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#252;/g,"\u00FC");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#237;/g,"\u00ED");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#243;/g,"\u00F3");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#211;/g,"\u00D3");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#246;/g,"\u00F6");
			data['depart_loc'][i] = data['depart_loc'][i].replace(/&\#214;/g,"\u00D6");
                        sel_depart_loc.options[start] = new Option(data['depart_loc'][i],i);
			start++;
		}
	}
	
	if ( setDepartSelection!=null || setDepartSelection!=undefined ) {
		setDepartSelection();
	}
}

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function departLocChanged_cb( response ) {
	// now redraw the Arrive (destination) select menu
	
	var data = response.parseJSON();

	// get the select menu and reset the length (keeping the first select me message/option)
	var sel_arrive_loc = document.getElementById("tf_destin_loc");
	var start = 1;
	sel_arrive_loc.options.length = 1;

	//if (data['destinations'].length > 0){
		for(var i in data['destinations'])
		{
			if(i != 'toJSONString'){
				
				data['destinations'][i] = data['destinations'][i].replace(/&\#232;/g,"\u00E8");
				data['destinations'][i] = data['destinations'][i].replace(/&\#233;/g,"\u00E9");
				data['destinations'][i] = data['destinations'][i].replace(/&\#226;/g,"\u00E2");
				data['destinations'][i] = data['destinations'][i].replace(/&\#234;/g,"\u00EA");
				data['destinations'][i] = data['destinations'][i].replace(/&\#224;/g,"\u00E0");
				data['destinations'][i] = data['destinations'][i].replace(/&\#203;/g,"\u00EB");
				data['destinations'][i] = data['destinations'][i].replace(/&\#219;/g,"\u00FB");
				data['destinations'][i] = data['destinations'][i].replace(/&\#250;/g,"\u00FA");
				data['destinations'][i] = data['destinations'][i].replace(/&\#241;/g,"\u00F1");
				data['destinations'][i] = data['destinations'][i].replace(/&\#244;/g,"\u00F4");
				data['destinations'][i] = data['destinations'][i].replace(/&\#252;/g,"\u00FC");
				data['destinations'][i] = data['destinations'][i].replace(/&\#237;/g,"\u00ED");
				data['destinations'][i] = data['destinations'][i].replace(/&\#243;/g,"\u00F3");
				data['destinations'][i] = data['destinations'][i].replace(/&\#211;/g,"\u00D3");
				data['destinations'][i] = data['destinations'][i].replace(/&\#246;/g,"\u00F6");
				data['destinations'][i] = data['destinations'][i].replace(/&\#214;/g,"\u00D6");
				//addOption(sel_arrive_loc, data['destinations'][i], i);
				sel_arrive_loc.options[start] = new Option(data['destinations'][i],i);
				start++;
			}
		}
	//}
	
	if ( setArriveSelection!=null || setArriveSelection!=undefined ) {
		setArriveSelection();
	}
}