/*
 * $Id: availability.js,v 1.8 2008/03/11 18:17:11 yayun Exp $
 * Name: availability.js
 * Author: Yayun Sun
 * Copyright: (c) Metafour UK Ltd. 2008

 * Description:
 * Javascript functions needed on the tour availability page.
 */

/*
 * Direct to the booking page
 */
function go_to_booking_page(index, date) {
	var nbpax = document.getElementById("nbpax" + index).value;
	if (nbpax == ""){
		alert("Please select No. of Persons");
		return;
	}
	if (document.frmAgentBooking.hidCTYP.value == "Agent") {
		// Agent booking
		// Format the date from YYYY-MM-DD to DDMMYY
		var year = date.substring(2, 4);
		var month = date.substring(5, 7);
		var day = date.substring(8);
		var date = day + month + year;
		document.frmAgentBooking.txtDEPDT.value = date;
		document.frmAgentBooking.submit();
	} else {
		// Private client booking
		var tour_code = document.frmChange.tour_code.value;
		var base = document.frmChange.hidBASE.value;
		var url = "https://book.tucantravelonline.com/book/tucan/" + tour_code+ '/'+ date+ '/'+ nbpax + '/' + document.frmChange.hidE2secd.value;
		window.open(url, "", "");	
	}
}                                                                                                     

/*
 * Search for a different tour
 */
function changeTour() {
	var selTourCode = document.frmChangeTour.selTourCode.value;
	var txtTourCode = document.frmChangeTour.txtTourCode.value;
	if (selTourCode == "" && txtTourCode == "") {
		alert("Please select a tour or enter a tour code.");
		return;
	}
	// The entered tour code takes priority over the selected tour
	var tourCode = (txtTourCode != "" ? txtTourCode : selTourCode);
	document.frmChange.txtTOURC.value = tourCode;
	document.frmChange.tour_code.value = tourCode;
	document.frmChange.hidCTYP.value = (frmChangeTour.chkCTYP1.checked ? "Agent" : "Direct");
    document.frmChange.submit();
}

/*
 * Show availability using a different currency
 */
function changeCurrency() {
	document.frmChange.currency.value = document.frmChangeCurrency.currency.value;
    document.frmChange.submit();
}

/*
 * Switch to the grid view
 */
function gridView() {
	frmSwitchView.submit();
}

/*
 * Re-populate the tour drop-down list according to the destination and tour type selection
 */ 
function filterTours() {
	var dest = document.frmChangeTour.selDest.value;
	var type = document.frmChangeTour.selType.value;
	var data = "<?xml version='1.0' encoding='UTF-8'?>\n";
	data    += "<AvailabilityUtility>\n";
	data    += "  <Actions>\n";
	data    += "    <Action>\n";
	data    += "      <ActionName>filter_tours</ActionName>\n";
	data    += "      <ActionParams>\n";
	data    += makeParam('dest', dest);
	data    += makeParam('type', type);
	data    += "      </ActionParams>\n";
	data    += "    </Action>\n";
	data    += "  </Actions>\n";
	data    += "</AvailabilityUtility>";
	var url = document.frmChange.hidBASE.value + "/tucanonline/availability_util.php";
	try {
		var xmlhttp = XmlHttp.create();
		xmlhttp.open("POST", url, true);
		xmlhttp.setRequestHeader ("Content-type", "text/xml");
		xmlhttp.onreadystatechange = function (){onResponse(xmlhttp)};
		xmlhttp.send(data);
	} catch(ex) {
    	alert(ex);
	}
}

/*
 * Parse the response of the tours
 */
function parseRes(actRes) {
	var status = getActionStatus(actRes);
	var actName = get_nodeval(actRes.getElementsByTagName('ActionName')[0]);
	if (status == "false") {
		alert("Failed to get all tours");
		return;
	}
	// Clear the current tour list
	var selTourCode = document.frmChangeTour.selTourCode;
	selTourCode.length = 0;
    selTourCode.options[0] = new Option("", "");
    // Populate the new list
	var tourCodes = actRes.getElementsByTagName("Code");
	var tourNames = actRes.getElementsByTagName("Name");
	var pos = selTourCode.length;
	for (var i = 0; i < tourCodes.length; i++) {
		var tourCode = tourCodes[i].firstChild.data;
		var tourName = tourNames[i].firstChild.data;
		selTourCode.options[pos + i] = new Option(tourCode + "-" + tourName, tourCode);
		if (tourCode == document.frmChange.tour_code.value) {
			selTourCode.options[i + 1].selected = true;
		}
	}
}

/*
 * Toggle the client type ticks
 * 100308 added by Sunny
 */
function toggleTicks(pos) {
  var tick = document.getElementById("chkCTYP" + pos);
  // Tick the box
  tick.checked = true;
  if (pos == 1) {
  	// Ticked the agent box, untick the private box
  	document.getElementById("chkCTYP2").checked = false;
  } else {
    // Ticked the private box, untick the agent box
    document.getElementById("chkCTYP1").checked = false;
  }
}

/*
 * Open a new window
 */
function popUp(URL) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=450,height=200');");
}                     
