// This function is specific to Gordon River Cruises & West Coast Railway
var output_current = ""; var output_other = ""; var daterow = ""; var pricerow = ""; var daterow_output = "";
var loopcount = 0; var persontype = "Adult";
var ddate, dday, dmonth, dyear;
var ndate, nday, nmonth, nyear;
var available, maxppl, rtype, roomtype; var check = "";
var nodecount = 0; var proceed = false; var loopcount = 0;
ready = true; priceAdult = 0; priceChild = 0; total = 0;
adult_price=0; child_price=0;

// write html for tour
var output_current_head = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" class=\"rates-current\"><tr class=\"top\"><th>Type</th>";

// ==========================================================================================
// EP (2007-12-21): Fixed a bug when roomrez returns less than 12 items for each room.
var arrRoomItems;
var TOTAL_DISPLAY_DAYS = 10;

function InitialiseRoomItems(objXml)	{
	var idCurrent;
	var intIndex, intCount = 0;
	
	if (objXml.length > 0) {
		if (objXml[0].childNodes[0] && objXml[0].childNodes[0].firstChild) {
			idCurrent = parseInt(objXml[0].childNodes[0].firstChild.nodeValue, 10);
		}
	}
	
	arrRoomItems = new Array();
	
	for (intIndex = 0; intIndex < objXml.length; intIndex++)	{
		intCount++;
		if (idCurrent != parseInt(objXml[intIndex].childNodes[0].firstChild.nodeValue,10))	{
			arrRoomItems[arrRoomItems.length] = intCount - 1;
			intCount = 1;
			idCurrent = parseInt(objXml[intIndex].childNodes[0].firstChild.nodeValue,10);
		}
	}
	arrRoomItems[arrRoomItems.length] = intCount;
}
function getNodeCount(intRoomIndex)	{
	if (arrRoomItems[intRoomIndex] > TOTAL_DISPLAY_DAYS)	{
		return arrRoomItems[intRoomIndex] - (TOTAL_DISPLAY_DAYS - 1);
	}
	else	{
		return 1;
	}
}
function getItemCount(intRoomIndex)	{
	return arrRoomItems[intRoomIndex];
}
// ==========================================================================================

var searchxml;
function processDataSpecial() {
  var searchstring1 = document.ratefrm.searchstring1.value;
  if (document.ratefrm.searchstring2) var searchstring2 = document.ratefrm.searchstring2.value;
  if (document.ratefrm.searchstring3) var searchstring3 = document.ratefrm.searchstring3.value;
  
  if (xml.responseXML.documentElement.childNodes.length > 0) {
  	searchxml = xml.responseXML.documentElement.childNodes;
  }
  var slength = searchxml.length;

  // JM (2008-11-13)
  if (intPageID == 349) {
  	if (persontype == "Adult") {
		persontype = "Morning";
	} 
	if (persontype == "Child"){
		persontype = "Afternoon";
	}
//  	persontype = searchstring1;
  }
  
  // EP (2007-12-21)
  InitialiseRoomItems(searchxml);
  
  for (var r = 0; r < roomarr.length; r++) {
 //alert("slength: " + slength + ", nodecount: " + nodecount);
	proceed = false;
    if (slength > nodecount) {
		
		if (nodecount < 0) {
			// Could not find matching room in XML. Show default CALL cell.
			roomnode_id = 0;
			roomtype = "";
		} else {
			var search_child = searchxml[nodecount].childNodes;
			// room id of the current room
			roomnode_id = search_child[0].firstChild.nodeValue;
			roomtype = search_child[1].firstChild.nodeValue;
		}
    } else {
      roomtype = "";
      proceed = true;
    }
    
    // EP (2008-10-01): Hard-coded for PBC
    if (roomnode_id == 6982 || roomnode_id == 6983)	{
		nodecount += getItemCount(r);
    	continue;
    }
    
    // do string comparisons so the right type is displayed
    if (document.ratefrm.searchstring3) {
      if (roomtype.indexOf(searchstring1) > -1 && roomtype.indexOf(searchstring2) > -1 && roomtype.indexOf(searchstring3) > -1 && roomtype.indexOf(persontype) > -1) proceed = true;
    } else if (document.ratefrm.searchstring2) {
      if (roomtype.indexOf(searchstring1) > -1 && roomtype.indexOf(searchstring2) > -1 && roomtype.indexOf(persontype) > -1) proceed = true;
    }
    else	{
	  if (intPageID != 349) {
	      if (roomtype.indexOf(searchstring1) > -1 && roomtype.indexOf(persontype) > -1) proceed = true;
	  } else {
	      if (roomtype.indexOf(persontype) > -1) proceed = true;
	  }
    }
    
    //alert("roomtype: " + roomtype + ", searchstring1: " + searchstring1 + ", searchstring2: " + searchstring2 + ", persontype: " + persontype + ", proceed: " + proceed);
    
    if (proceed) {
      // EP (2008-10-01): Hard-coded to override the default room_id passed in.
      if (loopcount == 0)	{
      	  new_room_id = roomnode_id;
      	  //alert("new_room_id: " + new_room_id);
      }
      loopcount += 1;
      
	//alert("roomnode_id: " + roomnode_id);
      for (var i = 0; i <= 9; i++) {
        // format date manually and write date row.. this is used in the loop
        datespan = datearr[i];
        dyear = datespan.substring(2, 4);
        dmonth = datespan.substring(5, datespan.indexOf("-", 5));
        dday = datespan.substring(6 + dmonth.length, datespan.length);
        ddate = dday +"."+ dmonth +"."+ dyear;
        
        // if current room date matches from date, then write check value
		if (intPageID != 349) { 
			if (ddate == fromDate) 
				check = " class=\"checked\"";
	        if (loopcount == 2 && numChild == 0) check = "";
		} else { // Special functionality for Oysters on the Bay
			if (ddate == fromDate && searchstring1 == persontype) {
				check = " class=\"checked\"";
				room_id = (searchstring1 == "Morning") ? 6988 : 6989;
				new_room_id = room_id;
			}
		}

        
        daterow += "<td"+ check +">"+ dday +"."+ dmonth +"."+ dyear +"</td>";
        
        if (slength > nodecount) {
        	// EP (2007-12-21)
        	if (i < getItemCount(r))	{
		          var search_child = searchxml[nodecount].childNodes;
		          roomnode_id = search_child[0].firstChild.nodeValue;
//alert("roomnode_id: " + roomnode_id);
		          // format date from xml
		          datespan = search_child[2].firstChild.nodeValue;
		          datespan = datespan.substring(0, datespan.indexOf("T"));
		          nyear = datespan.substring(2, 4);
		          nmonth = datespan.substring(5, 7);
		          nday = datespan.substring(8, 10);
		          
		          // strip zeros out of dates
		          if (nmonth.substring(0, 1) == "0") nmonth = nmonth.substring(1);
		          if (nday.substring(0, 1) == "0") nday = nday.substring(1);
		          
		          ndate = nday +"."+ nmonth +"."+ nyear;
		          
		          if (ddate == ndate) {
		            // display price, full or no rate
		            price = parseInt(search_child[4].firstChild.nodeValue);
		            available = parseInt(search_child[3].firstChild.nodeValue);
		            
					if (price == 99999999) {
					  pricerow += "<td class=\"full\">N/A</td>";
					  if (check) ready = false;
					}
					else {
		              if (loopcount == 1) {
						   //*MONKEY DANCE* - see planner.js
		                //priceAdult = (numAdult > 1) ? price / numAdult : price;
						priceAdult = price
		                price = Math.round(priceAdult);
		                if(check != "") { adult_price = price;}
		              
		              } else {
					  	if (intPageID != 349) {
							//priceChild = (numChild > 1) ? price / numChild : price;
							priceChild = price
							//priceChild = price;
							//priceChild = price / numAdult;
							price = Math.round(priceChild);
							if (check != "") {
								child_price = price;
							}
						} else {
							priceChild = price / numAdult;
							price = Math.round(priceChild);
							if (check != "") {
								child_price = price;
							}
						}
		              }
						
  		              if (check) total += price;						
						
		              if (available) {
		                pricerow += "<td"+ check +">$"+ price +"</td>";
		              } else {
		                pricerow += "<td class=\"full\" onmouseout=\"removeCallInfo()\">CALL</td>";
		                if (check) ready = false;
		              }
					}
		          } else {
		            pricerow += "<td>N/A</td>";
		            if (check) ready = false;
		          }
			}
			else	{
				pricerow += "<td class=\"full\" onmouseout=\"removeCallInfo()\">CALL</td>";
				if (check) ready = false;
			}
        } else {
          pricerow += "<td>N/A</td>";
          if (check) ready = false;
        }
        
        // xml holds 12 sets of dates for each room type (we only need 10)
        //nodecount += (i == 9) ? (slength / roomarr.length) - 9 : 1;
        // EP (2007-12-21)
        nodecount += (i == 9) ? getNodeCount(r) : 1;
        
        // if current room date matches to date, then clear check value
        if (ddate == toDate) check = "";
        
        if (loopcount == 1 && i == 0) 
			if (intPageID == 349) {
				pricerow = "<tr><th>Morning</th>" + pricerow;
			}
			else {
				pricerow = "<tr><th>Adult</th>" + pricerow;
			}
		else {
			if (loopcount == 2 && i == 0) {
				if (intPageID == 349) {
					pricerow = "<tr><th>Afternoon</th>" + pricerow;
				}
				else {
					pricerow = "<tr><th>Child</td>" + pricerow;
				}
				
			}
		}
      }
	  
	  if (loopcount == 1) persontype = "Child";
	  
    } else
      //nodecount += slength / roomarr.length;
      // EP (2007-12-21)
      nodecount += getItemCount(r);
    
    // write current room type and other room type variables for output
//alert("loopcount: " + loopcount);
    if (loopcount == 1 && proceed) {
      daterow += "</tr>"; pricerow += "</tr>";
      output_current = daterow + pricerow;
      proceed = false;
      pricerow = ""; // daterow = "";
      loadXMLDoc("includes/searchrooms.asp?property_id="+ property_id +"&rtype=2&fromDate="+ datearr[0] +"&adult=0&child="+ numChild +"&ran="+ getRan());
      return;
    } else if (loopcount == 2) {
      pricerow += "</tr>";
      output_current += pricerow;
      proceed = false;
      pricerow = ""; daterow = "";
      break;
    }
  }
  
  document.getElementById("currentRoom").innerHTML = output_current_head + output_current + "</table>";
  
  calcTotalSpecial(); // function found below
  
  // if availability confirmed, then display add to holiday link
  // if not, display error messages and hide add to holiday link
  if (ready)
    document.getElementById("addholiday").style.display = "block";
  else
    document.getElementById("error").style.display = "block";
  
  // reset vars manually cause they are outside function
  output_current = ""; output_other = ""; daterow = ""; pricerow = ""; daterow_output = "";
  loopcount = 0; persontype = "Adult"; rtype = 2;
  nodecount = 0; proceed = false; loopcount = 0;
  ready = true; priceAdult = 0; priceChild = 0; total = 0;
  
  //window.scroll(0,20000);
}

function calcTotalSpecial() {
  
  var childtotal = (document.ratefrm.child) ? (child_price * parseInt(document.ratefrm.child.value)) : 0;
  
  total = (adult_price * numAdult) + childtotal;
  // adds a comma for totals with 4 digits and under 6 digits
  if (total == 99999999)
    total = 0;
  else {
    if (total > 999) {
      total = ""+ total +"";
      total = total.substring(0, 1) +","+ total.substring(1, 4);
    }
  }
  document.getElementById("total").innerHTML = "Total - A$"+ total +".00";
  document.ratefrm.totalamount.value = total;
}