// startXML and processData functions do all the work here
var isPT;

function startXML() {
  var rateform = document.ratefrm;
  var tdays = calcDays(fromDay, fromMonth, fromYear, "nosubtract");

  // check to see if we are doing a rates search from countryclubresort.com.au or wrestpoint.com.au
  isPT = (location.href.indexOf("book/rates.asp") != -1 || location.href.indexOf("wrestpointbooking/rates.asp") != -1) ? false : true;

	isPT = false;	// override above line..

  if (isPT)
    var contact = "1800 635 344";
  else
    var contact = (location.href.indexOf("book/rates.asp") != -1) ? "1800 635 344" : "1800 703 006";

  fromDay = parseInt(rateform.fromDay.value);
  fromMonth = parseInt(rateform.fromMonth.value);
  fromYear = parseInt(rateform.fromYear.value);
  fromDate = fromDay +"."+ fromMonth +"."+ rateform.fromYear.value.substring(2, 4);

  var currentstatus = true;
  if (fromDay < currentDay && fromMonth == currentMonth && fromYear == currentYear) currentstatus = false;
  if (fromDay >= currentDay && fromMonth < currentMonth && fromYear == currentYear) currentstatus = false;
  if (fromDay < currentDay && fromMonth < currentMonth && fromYear == currentYear) currentstatus = false;

  if (!currentstatus) {
    if (rtype == 1 || property_id == 1684 || property_id == 1682)
      alert("The dates selected cannot be less than the present date.\nThe select boxes will be reset.\n\nPlease select a different date range.");
    else
      alert("Your selection cannot be less than 10 days from the present date.\nThe date range will be changed to the next available date\n\nPlease revise the new date range.");

    rateform.fromDay.selectedIndex = currentDay - 1;
    rateform.fromMonth.selectedIndex = currentMonth - 1;
    changeDates();
    return void(0);
  }

  if (checkMaxPeople()) return void(0);

  if (rtype == 1) {
    toDay = parseInt(rateform.toDay.value);
    toMonth = parseInt(rateform.toMonth.value);
    toYear = parseInt(rateform.toYear.value);
    toDate = toDay +"."+ toMonth +"."+ rateform.toYear.value.substring(2, 4);

    numAdult = parseInt(rateform.adult.selectedIndex) + 1;
    numChild = parseInt(rateform.child.selectedIndex);
  } else {
    // single tour page
    toDay = fromDay;
    toMonth = fromMonth;
    toYear = fromYear;
    toDate = fromDate;

    numAdult = parseInt(rateform.adult.selectedIndex) + 1;
    if (rateform.child)
      numChild = parseInt(rateform.child.selectedIndex);
    else
      numChild = 0;
  }

  /*if (toYear > fromYear) {
    if ((12 - currentMonth) + fromMonth > 6) currentstatus = false;
  } else {
    if ((fromMonth - currentMonth) > 6) currentstatus = false;
  }*/

  if (!currentstatus) {
    alert("There are only 6 months of future rates available for viewing.\nThe select boxes will be reset.\n\nPlease select a date range within 6 months from the present date");
    rateform.fromDay.selectedIndex = currentDay - 1;
    rateform.fromMonth.selectedIndex = currentMonth - 1;
    if (tdays <= fromDay + 1) {
      rateform.toDay.selectedIndex = 0;
      if (fromMonth == 12) {
        rateform.toMonth.selectedIndex = 0;
        rateform.toYear.selectedIndex = rateform.fromYear.selectedIndex + 1;
      } else
        rateform.toMonth.selectedIndex = rateform.fromMonth.selectedIndex;
    } else {
      rateform.toDay.selectedIndex = rateform.fromDay.selectedIndex + 1;
      rateform.toMonth.selectedIndex = rateform.fromMonth.selectedIndex;
      rateform.toYear.selectedIndex = rateform.fromYear.selectedIndex;
    }
    changeDates();
    return void(0);
  }

  if (fromMonth == toMonth) {
    if (toDay - fromDay > 10) currentstatus = false;
  } else {
    if ((tdays - fromDay) + toDay > 10) currentstatus = false;
  }

  if (!currentstatus) {
    alert("The maximum number of days you can book online is 10.\n\nShould you wish to stay longer than this, please call our Customer Contact Centre\non "+ contact +" (national) or +613 6225 7092 (overseas) and they will assist\nwith your booking.");
    if (tdays <= fromDay + 1) {
      rateform.toDay.selectedIndex = 0;
      if (fromMonth == 12) {
        rateform.toMonth.selectedIndex = 0;
        rateform.toYear.selectedIndex = rateform.fromYear.selectedIndex + 1;
      } else
        rateform.toMonth.selectedIndex = rateform.fromMonth.selectedIndex;
    } else {
      rateform.toDay.selectedIndex = rateform.fromDay.selectedIndex + 1;
      rateform.toMonth.selectedIndex = rateform.fromMonth.selectedIndex;
      rateform.toYear.selectedIndex = rateform.fromYear.selectedIndex;
    }
    changeDates();
    return void(0);
  }

  // Freycinet & Wrest Point min 2 night exception
  //if (property_id == 1676 || property_id == 1679)
  if (property_id == 1679 || property_id == 1676) {
    currentstatus = true;

    // Freycinet check
    if (property_id == 1676 && (toMonth == 12 || toMonth <= 4)) {
      //alert("TRUE ID: "+property_id+" Month: "+toMonth+" Day: "+toDay);
      if ((toMonth == 12 && toDay >= 20) || (toMonth == 3 && toDay <= 31)) {
        if (toDay - fromDay < 2) { currentstatus = false; }
      } else if (toMonth >= 1 && toMonth < 4) {
        if (fromMonth == toMonth) {
          if (toDay - fromDay < 2) { currentstatus = false; }
        } else {
          if ((tdays - fromDay) < 2)
            if ((tdays - fromDay) + toDay < 2) { currentstatus = false; }
        }
      }
    }

    // Wrest Point check
    if (property_id == 1679 && (toMonth == 12 || toMonth == 1)) {
      if ((toMonth == 12 && toDay >= 30) || (toMonth == 1 && toDay == 1)) {
        if (toDay - fromDay < 2) currentstatus = false;
      } else if (toMonth >= 1 && toMonth < 4) {
        if (fromMonth == toMonth) {
          if (toDay - fromDay < 2) currentstatus = false;
        } else {
          if ((tdays - fromDay) < 2)
            if ((tdays - fromDay) + toDay < 2) currentstatus = false;
        }
      }
    }

    if (!currentstatus) {
      if (property_id == 1676)
        alert("The minimum stay at all room types at Freycinet Lodge between\n20th December 2007 and 31st March 2008 is two nights.\n\nThe select boxes will be reset for you to make another date selection.");
      else
        alert("The minimum stay at all room types at Wrest Point between\n30th December 2006 and 1st January 2007 is two nights.\n\nThe select boxes will be reset for you to make another date selection.");

      if (tdays <= fromDay + 1) {
        rateform.toDay.selectedIndex = 0;
        if (fromMonth == 12) {
          if (tdays == fromDay)
            rateform.toDay.selectedIndex = 1;
          else
            rateform.toDay.selectedIndex = 0;

          rateform.toMonth.selectedIndex = 0;
          rateform.toYear.selectedIndex = rateform.fromYear.selectedIndex + 1;
        } else
          rateform.toMonth.selectedIndex = fromMonth;
      } else
        rateform.toDay.selectedIndex = fromDay + 1;

      changeDates();
      return void(0);
    }
  }

  // display and reset hidden elements
  with (document) {
    getElementById("currentRoom_header").style.display = "block";
    getElementById("currentRoom").innerHTML = "<div style=\"padding:20px 20px 21px 12px\"><strong>LOADING...</strong></div>"
    getElementById("currentRoom").style.display = "block";
    getElementById("currentRoom_footer").style.display = "block";
    getElementById("error").style.display = "none";
    getElementById("addholiday").style.display = "none";
    getElementById("total").innerHTML = "&nbsp;";
    if (rtype == 1) {
      getElementById("otherRoom_holder").style.display = "block";
      getElementById("otherRoom").innerHTML = "<div style=\"padding:20px 20px 21px 12px\"><strong>LOADING...</strong></div>";
      getElementById("otherRoom").style.display = "block";
    }
  }

  // calculate nights
  if (fromMonth == toMonth)
    nights = toDay - fromDay;
  else
    nights = (tdays - fromDay) + toDay;

  calcDateSpan(); // function found below

  // loadxmlDoc function found in xmlhttp.js
  if (isPT)
    loadXMLDoc("includes/searchrooms.asp?property_id="+ property_id +"&rtype="+ rtype +"&fromDate="+ datearr[0] +"&adult="+ numAdult +"&child="+ numChild +"&ran="+ getRan());
  else
    //loadXMLDoc("../includes/searchrooms.asp?property_id="+ property_id +"&rtype="+ rtype +"&fromDate="+ datearr[0] +"&adult="+ numAdult +"&child="+ numChild +"&ran="+ getRan());
    loadXMLDoc("includes/searchrooms.asp?property_id="+ property_id +"&rtype="+ rtype +"&fromDate="+ datearr[0] +"&adult="+ numAdult +"&child="+ numChild +"&ran="+ getRan());

}

function processData() {
  var output_current = "", output_other = "", daterow = "", pricerow = "", daterow_output = "";
  var ddate, dday, dmonth, dyear;
  var ndate, nday, nmonth, nyear;
  var available, check = "", firsttime = true;
  var nodecount = 0, ratecount = 0, ratetotal, rollevents, dstatus, maxpeople;
  // EP(2007-10-19)
  var cellOnclick = "", cellClass = "";

  // write html for current room, other rooms and N/A rates
  if (rtype == 1)
  	// EP (2007-10-22)
    //var output_current_head = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" class=\"rates-current\"><tr class=\"top\"><th>Room Name</th>";
    var output_current_head = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" class=\"rates-current\"><tr class=\"top\"><th>&nbsp;</th>";
  else
    //var output_current_head = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" class=\"rates-current\"><tr class=\"top\"><th>Tour Name</th>";
    var output_current_head = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" class=\"rates-current\"><tr class=\"top\"><th>&nbsp;</th>";

  // EP (2007-10-22)
  //var output_other_head = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" class=\"rates-other\"><tr class=\"top\"><th>Room Name</th>";
  var output_other_head = "<table cellspacing=\"0\" cellpadding=\"4\" border=\"0\" class=\"rates-other\"><tr class=\"top\"><th>&nbsp;</th>";
  var output_na = "<td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td><td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td><td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td><td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td><td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td><td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td><td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td><td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td><td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td><td onmouseout=\"removeCallInfo()\" class=\"full\">CALL</td>";

  try {
  var searchxml =  xml.responseXML.documentElement.childNodes;
  var slength = searchxml.length;
  var dlength = slength / roomarr.length;

  if (dlength == 12)
    dstatus = "normal";
  else if (dlength == 0)
    dstatus = "nada";
  else {
    var divide = (slength / 12) + "";
    if (divide.indexOf(".") > -1)
      dstatus = "abnormal";
    else
      dstatus = "normal-12";
  }
  } 
  catch (err) {
  }

  // reset values
  ready = true;
  price = 0;
  total = 0;

  for (var r = 0; r < roomarr.length; r++) {
    // if tour, fast forward to the node needed
    if (roomarr.length == 1) {
      maxpeople = 10;
	  
	  /*
	  */
	  
      for (var i = 0; i < slength; i++) {
        var search_child = searchxml[nodecount].childNodes;
        if (room_id == search_child[0].firstChild.nodeValue) break;
        nodecount += 1;
      }
    }
    
		// EP (2007-11-02): Added code to find the current room
		nodecount = -1;	// initialize nodecount
		
		for (var z = 0; z < slength; z++)	{
			if (parseInt(roomarr[r],10) == parseInt(searchxml[z].childNodes[0].firstChild.nodeValue,10))	{
				// matching room id is found. Go on with the rest of code.
				nodecount = z;
				break;
			}
		}

//alert("nodecount: " + nodecount);

    if (slength > nodecount) {
      // EP (2007-11-19)
      //if (nodecount > 9 || r == 0) {
	  if (nodecount > -1) {
		// ie: nodecount was set after initialization
        var search_child = searchxml[nodecount].childNodes;
        // room id of the current room
        roomnode_id = search_child[0].firstChild.nodeValue;
      //}
	  } /*else {
        pricerow = "<tr "+ rollevents +"><th>"+ namearr[r] +"</th><td style=\"width:400px\">Call</td>";        		  
	  }*/

      if (roomnode_id != roomarr[r]) {
        rollevents = "onMouseOver=\"this.className='row-on';\" onMouseOut=\"this.className='';\" ";
        
        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 todate, then write check value
          if (roomarr[r] == room_id) {
          	// EP (2007-10-22)
            //if (ddate == fromDate) check = " class=\"checked\"";
            if (ddate == fromDate) check = "checked";
            if (rtype == 1) if (ddate == toDate) check = "";
          }

		  // EP (2007-10-22)
          //daterow += "<td"+ check +">"+ dday +"."+ dmonth +"."+ dyear +"</td>";
          daterow += "<td class=\""+ check +"\">"+ dday +"."+ dmonth +"."+ dyear +"</td>";
		}
		
        //pricerow = "<tr "+ rollevents +"><th>"+ namearr[r] +"</th><td colspan=\"10\" style=\"width:400px\">This room's maximum number of people per room has been exceeded</td>";
		pricerow = "<tr "+ rollevents +"><th>"+ namearr[r] +"</th>" + output_na;
		
      } else {
        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 todate, then write check value
          if (roomarr[r] == room_id) {
          	// EP (2007-10-22)
            //if (ddate == fromDate) check = " class=\"checked\"";
            if (ddate == fromDate) check = "checked";
            if (rtype == 1) if (ddate == toDate) check = "";
          }

		  // EP (2007-10-22)
          //daterow += "<td"+ check +">"+ dday +"."+ dmonth +"."+ dyear +"</td>";
          daterow += "<td class=\""+ check +"\">"+ dday +"."+ dmonth +"."+ dyear +"</td>";

          if (slength > nodecount) {
			if (nodecount > -1) {
            var search_child = searchxml[nodecount].childNodes;
            roomnode_id = search_child[0].firstChild.nodeValue;

            // 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 any 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) {
              ratecount += 1;
              // set max people value
              if (i == 0 && roomarr.length > 1) maxpeople = parseInt(search_child[5].firstChild.nodeValue);

              // display price, full or no rate
              price = parseInt(search_child[4].firstChild.nodeValue);
              if (rtype == 2) price = price / numAdult;
              available = parseInt(search_child[3].firstChild.nodeValue);

              if (check) total += price;

              if (available)	{
              	// EP (2007-10-19): Made price cells clickable.
                //pricerow += "<td"+ check +">$"+ price +"</td>";
                //pricerow += "<td class=\""+ check +"\">$"+ price +"</td>";
                //if (room_id == roomnode_id)	{
                //	cellOnclick = "";
                //	cellClass = "";
                //}
                //else	{
                	//cellOnclick = " onClick=\"location.href='default.asp?pID="+ pagearr[r] +"&i="+ itinerary_id +"&deleteroom=true&fdate="+ nday +"/"+ nmonth +"/"+ nyear +"'\" onMouseOver=\"roomRateMouseOver(this)\" onMouseOut=\"roomRateMouseOut(this)\"";
                	cellOnclick = " onClick=\"calculateSelectedDateRange(" + pagearr[r] + ", " + itinerary_id + ", '" + nday + "', '" + nmonth + "', '" + nyear + "');\" onMouseOver=\"roomRateMouseOver(this)\" onMouseOut=\"roomRateMouseOut(this)\"";
                	
                	if (check) {
                		cellClass = " room-rate-cell"
                	}
                	else	{
                		cellClass = "room-rate-cell"
                	}
                //}
                // EP (2007-10-22)
                //pricerow += "<td"+ check + cellOnclick + ">$"+ price +"</td>";
                pricerow += "<td class=\""+ check + cellClass + "\"" + cellOnclick + ">$"+ price +"</td>";
              }
              else {
                pricerow += "<td class=\"full\" onmouseout=\"removeCallInfo()\">CALL</td>";
                if (check) ready = false;
              }
            } else {
				  pricerow += "<td class=\"full\" onmouseout=\"removeCallInfo()\">CALL</td>";
              if (check) ready = false;
            }

            // xml holds 12 sets of dates for each room type (we only need 10)
            // sometimes there isnt 12 sets as there might not be available rates
            if (i == 9) {
              if (firsttime) {
                ratetotal = ratecount;
                firsttime = false;
              }
              if (dstatus == "normal")
                nodecount += (slength / roomarr.length) - 9;
              else if (dstatus == "normal-12")
                nodecount += (slength / divide) - 9;
              else {
                divide = (slength / ratetotal) + "";
                nodecount += (slength / divide) - 9;
              }
            } else
              nodecount += 1;
			} else {
				pricerow += "<td class=\"full\">CALL</td>";
			}
          } else {
            pricerow += "<td class=\"full\">CALL</td>";
            if (check) ready = false;
          }

          if (i == 0) {
            if (roomarr[r] == room_id) {
              pricerow = "<tr><th>"+ namearr[r] +"</th>" + pricerow;
            } else {
              rollevents = "onMouseOver=\"this.className='row-on';\" onMouseOut=\"this.className='';\" ";
	        // EP(2007_10-19)
	        /*
              if (isPT)
                rollevents += "onClick=\"location.href='default.asp?pID="+ pagearr[r] +"&i="+ itinerary_id +"&deleteroom=true&fdate="+ fromDay +"/"+ fromMonth +"/"+ fromYear +"&tdate="+ toDay +"/"+ toMonth +"/"+ toYear +"'\"";
              else
                rollevents += "onClick=\"location.href='?r="+ roomarr[r] +"&fdate="+ fromDay +"/"+ fromMonth +"/"+ fromYear +"&tdate="+ toDay +"/"+ toMonth +"/"+ toYear +"'\"";
            */
            	//rollevents = "";
              pricerow = "<tr "+ rollevents +"><th>"+ namearr[r] +"</th>" + pricerow;
            }
          }

          // if current room date matches todate, then clear check value
          if (roomarr[r] == room_id) {
            if (rtype == 2) if (ddate == toDate) check = "";
            if (nights == 10 && i == 9) check = "";
          }
        }
      }
    } else {
      if (roomarr[r] == room_id) {
        ready = false;
        pricerow = "<tr><th>"+ namearr[r] +"</th>"+ output_na;
      } else {
        rollevents = "onMouseOver=\"this.className='row-on';\" onMouseOut=\"this.className='';\" ";
        // EP(2007_10-19)
        /*
        if (isPT)
          rollevents += "onClick=\"location.href='default.asp?pID="+ pagearr[r] +"&i="+ itinerary_id +"&deleteroom=true&fdate="+ fromDay +"/"+ fromMonth +"/"+ fromYear +"&tdate="+ toDay +"/"+ toMonth +"/"+ toYear +"'\"";
        else
          rollevents += "onClick=\"location.href='?r="+ roomarr[r] +"&fdate="+ fromDay +"/"+ fromMonth +"/"+ fromYear +"&tdate="+ toDay +"/"+ toMonth +"/"+ toYear +"'\"";
		*/
		//rollevents = "";
        pricerow = "<tr "+ rollevents +"><th>"+ namearr[r] +"</th>";

        if (dstatus == "nada" || dstatus == "normal-12")
          if (slength == 0)
            pricerow += output_na;
          else
            pricerow += "<td colspan=\"10\" style=\"width:400px\">This room's maximum number of people per room has been exceeded</td>";
        else
          pricerow += "<td colspan=\"10\" style=\"width:400px\">This room's maximum number of people per room has been exceeded</td>";
      }
    }

    daterow += "</tr>";
    pricerow += "</tr>";

    // write current room type and other room type variables for output
    if (roomarr[r] == room_id) {
      // if there are rate errors
      if (!ready && dstatus != "normal") {
        daterow = "";
        for (d = 0; d < datearr.length; d++) {
          datespan = datearr[d];
          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 (ddate == fromDate) {
          	// EP (2007-10-22)
            //check = " class=\"checked\"";
            check = "checked";
            if (rtype == 1) if (ddate == toDate) check = "";
          }
          if (ddate == toDate) {
            check = "";
            if (rtype == 2) if (ddate == toDate) check = "";
          }
          // EP (2007-10-22)
          //daterow += "<td"+ check +">"+ ddate +"</td>";
          daterow += "<td class=\""+ check +"\">"+ ddate +"</td>";
        }
        daterow += "</tr>";
      }

      output_current = daterow + pricerow;
    } else {
      output_other += pricerow;

      // if there are rate errors
      if (daterow_output.length == 0) {
        for (d = 0; d < datearr.length; d++) {
          datespan = datearr[d];
          dyear = datespan.substring(2, 4);
          dmonth = datespan.substring(5, datespan.indexOf("-", 5));
          dday = datespan.substring(6 + dmonth.length, datespan.length);
          ddate = dday +"."+ dmonth +"."+ dyear;
          daterow_output += "<td>"+ ddate +"</td>";
        }
      }
    }

    pricerow = "";
    daterow = "";
  }

  document.getElementById("currentRoom").innerHTML = output_current_head + output_current + "</table>";
  if (rtype == 1) document.getElementById("otherRoom").innerHTML = output_other_head + daterow_output + output_other + "</table><div class=\"clear\">&nbsp;</div>";

  // if availability confirmed, then display add to holiday link
  // if not, display error messages and hide add to holiday link
  if (ready) {
    calcTotal();
    document.getElementById("addholiday").style.display = "block";
  } else {
    document.getElementById("error").style.display = "block";
    document.getElementById("total").innerHTML = "Total - A$0.00";
  }

  //window.scroll(0,20000);
}

function changeDates() {
  var rateform = document.ratefrm, toDateExist = false;

  fromDay = parseInt(rateform.fromDay.value);
  fromMonth = parseInt(rateform.fromMonth.value);
  fromYear = parseInt(rateform.fromYear.value);

  if (document.getElementById("addholiday")) {
    removeAddLink();
    if  (roomarr.length > 1 && room_id != 0 && rtype == 1)
      toDateExist = true;
    else
      toDateExist = false;
  } else
    toDateExist = false;

  if (toDateExist) {
    toDay = parseInt(rateform.toDay.value);
    toMonth = parseInt(rateform.toMonth.value);
    toYear = parseInt(rateform.toYear.value);

    // check if the "to" day is less than the "from" day, and change if so
    if (fromDay >= toDay && fromMonth == toMonth && fromYear == toYear) {
      if (calcDays(fromDay, fromMonth, fromYear, "nosubtract") == fromDay) {
        rateform.toDay.selectedIndex = 0;
        if (fromMonth == 12) {
          rateform.toMonth.selectedIndex = 0;
          rateform.toYear.selectedIndex = rateform.fromYear.selectedIndex + 1;
        } else
          rateform.toMonth.selectedIndex = fromMonth;
      } else
        rateform.toDay.selectedIndex = fromDay;

      toDay = parseInt(rateform.toDay.value);
    }

    // check if the "to" month is less than the "from" month, and change if so
    if (fromDay < toDay && fromMonth > toMonth && fromYear == toYear && fromMonth != 12) {
      if (calcDays(fromDay, fromMonth, fromYear, "nosubtract") == fromDay) {
        rateform.toDay.selectedIndex = 0;
        rateform.toMonth.selectedIndex = fromMonth;
      } else
        rateform.toMonth.selectedIndex = fromMonth - 1;

      toDay = parseInt(rateform.toDay.value);
      toMonth = parseInt(rateform.toMonth.value);
    }

    // check if the "to" year is less than the "from" year, and change if so
    if (fromDay < toDay && fromMonth < 12 && (fromYear <= toYear || fromYear >= toYear)) {
      rateform.toYear.selectedIndex = rateform.fromYear.selectedIndex;
      toYear = parseInt(rateform.toYear.value);
    }

    // if "to" year is greater than "from" year than set month to Jan
    //if (fromMonth == toMonth && fromYear < toYear) {
    //  rateform.toDay.selectedIndex = 0;
    //  toDay = parseInt(rateform.toDay.value);
    //  rateform.toMonth.selectedIndex = 0;
    //  toMonth = parseInt(rateform.toMonth.value);
    //}

    // if changing years, switch months to Jan
    if (fromYear > toYear) {
      rateform.fromDay.selectedIndex = 0;
      fromDay = parseInt(rateform.fromDay.value);
      rateform.fromMonth.selectedIndex = 0;
      fromMonth = parseInt(rateform.fromMonth.value);

      rateform.toDay.selectedIndex = 2;
      toDay = parseInt(rateform.toDay.value);
      rateform.toMonth.selectedIndex = 0;
      toMonth = parseInt(rateform.toMonth.value);
      rateform.toYear.selectedIndex = rateform.fromYear.selectedIndex;
      toYear = parseInt(rateform.toYear.value);
    }

    // refresh the number of days according to the month
    changeDay("to", toDay, toMonth, toYear);
  }

  // refresh the number of days according to the month
  // this function needs to run every time
  changeDay("from", fromDay, fromMonth, fromYear);
}

function removeAddLink() {
  document.getElementById("addholiday").style.display = "none";
}

function changeDay(strDay, nDay, nMonth, nYear) {
  var selectProp = eval("document.ratefrm." + strDay + "Day");
  var selectLength = selectProp.length;

  // loops through and removes current options
  for (var i = 0;  i < selectLength; i++)
    selectProp.remove(0);

  // write new options to the day field depending on which month
  for (i = 1; i <= calcDays(nDay, nMonth, nYear); i++)
    selectProp.options[i-1] = new Option(i, i);

  // select the day that was previously selected
  if (nDay >= calcDays(nDay, nMonth, nYear))
    selectProp.selectedIndex = calcDays(nDay, nMonth, nYear, "nosubtract") - 1;
  else
    selectProp.selectedIndex = nDay - 1;
}

function calcDays(nDay, nMonth, nYear, subtract) {
  // find total days in month and calculate difference
  var totalDays;

  if (nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11)
    totalDays = 30;
  else if (nMonth == 2)
    if (Math.floor(nYear/4) == (nYear/4) && (Math.floor(nYear/100) != (nYear/100) || Math.floor(nYear/400) == (nYear/400)))
      totalDays = 29;
    else
      totalDays = 28;
  else
    totalDays = 31;

  if (subtract == "subtract") totalDays = totalDays - nDay;

  return (totalDays);
}

function calcDateSpan() {
  // creates date array spanning each day for 10 days
  var daysleft;
  var daycount = fromDay, monthcount = fromMonth, yearcount = fromYear, tDay, tMonth, tYear;
  var totaldays = calcDays(daycount, monthcount, yearcount, "");

  if (nights < 7) {
    if (monthcount == currentMonth && yearcount == currentYear)
      if (daycount - currentDay > 3)
        daycount = daycount - 3;
      else
        daycount = daycount - (daycount - currentDay);
    else
      daycount = daycount - 3;
  }

  if (daycount < 1) {
    monthcount = monthcount - 1;
    if (monthcount == 0) {
      monthcount = 12;
      yearcount = yearcount - 1;
    }
    daycount = calcDays(daycount, monthcount, yearcount, "") + daycount;

    if (daycount < (currentDay + 1) && monthcount == currentMonth && yearcount == currentYear) {
      if (daycount + 1 > calcDays(daycount, monthcount, yearcount, "")) {
        daycount = 1;
        monthcount = monthcount + 1;
      } else
        daycount = currentDay + 1;
    }
  }

  if ((totaldays - daycount) - 10 < 1) tMonth = monthcount + 1;
  if (monthcount != tMonth) daysleft = calcDays(daycount, monthcount, yearcount, "subtract") + 1;

  if (monthcount == 12 && daycount >= 22) {
    tYear = yearcount + 1;
    tMonth = 1;
  } else
    tYear = yearcount;

  if (monthcount == 0) {
    monthcount = 12;
    yearcount = yearcount - 1;
  }

  for (var i = 0; i <= 9; i++) {
    if (i == daysleft) {
      daycount = 1;
      monthcount = tMonth;
      yearcount = tYear;
    }
    datearr[i] = yearcount +"-"+ monthcount +"-"+ daycount;
    daycount = daycount + 1;
  }
}

function writePeopleSelect(mppl) {
  var selectPeople, selectLength, i;
  if (roomarr.length > 1)
    var peopleArr = new Array("adult","child");
  else
    var peopleArr = new Array("adult");

  for (var p = 0; p <= peopleArr.length - 1; p++) {
    selectPeople = eval("document.ratefrm." + peopleArr[p]);
    selectLength = selectPeople.length;

    // loops through and removes current options
    for (i = 0;  i < selectLength; i++)
      selectPeople.remove(0);

    if (peopleArr[p] == "adult") {
      for (i = 1; i <= mppl; i++)
        selectPeople.options[i-1] = new Option(i,i);
    } else {
      mppl += -1;
      for (i = 0; i <= mppl; i++)
        selectPeople.options[i] = new Option(i,i);
    }

    // write new options to the day field depending on which month
    //for (i = 0; i <= mppl; i++) {
    //  if (peopleArr[p] == "adult")
    //    selectPeople.options[i] = new Option(i,i);
    //  else
    //    selectPeople.options[i] = new Option(i,i);
    //}

    if (peopleArr[p] == "adult")
      if (numAdult == "")
        if (mppl >= 2)
          if (roomarr.length > 1)
            selectPeople.selectedIndex = 1;
          else
            selectPeople.selectedIndex = 0;
        else
          selectPeople.selectedIndex = mppl;
      else	{
    	if (selectPeople.options.length > 0)	{
	    	if (selectPeople.options.length >= numAdult)	{
	    		if (numAdult > 0)	{
	      			selectPeople.selectedIndex = numAdult - 1;
	      		}
	    	}
	    	else	{ // no available number of adults
	    		// choose the maximum number of adults
	    		selectPeople.selectedIndex = selectPeople.options.length - 1;
	    		numAdult = selectPeople.options.length;
	    	}
		}
		else	{
			numAdult = 0;
		}
		//selectPeople.selectedIndex = numAdult - 1;
      }
    else
    	if (selectPeople.options.length > 0)	{
	    	if (selectPeople.options.length >= numChild)	{
      			selectPeople.selectedIndex = numChild;
	    	}
	    	else	{ // no available number of children
	    		// choose the maximum number of children
	    		selectPeople.selectedIndex = selectPeople.options.length - 1;
	    		numChild = selectPeople.options.length;
	    	}
		}
		else	{
			numChild = 0;
		}
  }
}

function selectSpecialOptions(seating, times, depart) {
  if (property_id == 1682) {
    if (seating.length > 0)
      if (seating == "Lower") document.ratefrm.searchstring1.selectedIndex = 1;

    if (times.length > 0)
      if (times == "Evening") document.ratefrm.searchstring2.selectedIndex = 1;
  } else {
    if (seating.length > 0)
      if (seating == "Tourist") document.ratefrm.searchstring1.selectedIndex = 1;

    if (times.length > 0)
      if (times == "PM") document.ratefrm.searchstring2.selectedIndex = 1;

    if (depart.length > 0)
      if (depart == "Strahan") document.ratefrm.searchstring3.selectedIndex = 1;
  }
}

function checkMaxPeople() {
  var adulttotal = parseInt(document.ratefrm.adult.value);
  if (document.ratefrm.child)
    var childtotal = parseInt(document.ratefrm.child.value);
  else
    var childtotal = 0;

  if (adulttotal + childtotal > maxppl) {
    alert("Error: The maximum amount of people allowed for this room is "+ maxppl +".");
    return true;
  } else if (adulttotal == 0) {
    alert("Error: There must be at least one adult chosen for booking a room.");
    return true;
  }
}

function calcTotal() {
  if (roomarr.length == 1) total = price * parseInt(document.ratefrm.adult.value);

  if (total == 99999999)
    total = 0;
  else {
    // adds a comma for totals with 4 digits and under 6 digits
    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;
}

function submitForm() {
  var rateform = document.ratefrm;
  if (parseInt(rateform.adult.value) > 0) {
    if (isPT)
      rateform.action = "functions/addroom.asp?itinerary_id="+ itinerary_id +"&property_id="+ property_id +"&room_id="+ room_id +"&rtype="+ rtype + "#rates";
    else	{
      rateform.action = "book/details.asp?p="+ property_id +"&r="+ room_id + "#rates";
    }
   rateform.submit();
  } else
    alert("Error: There must be at least one adult selected when booking accommodation.");
}

function submitQuickBooking() {
  var quickform = document.quickfrm;
  var property_id = quickform.property.value;
  var room_id = quickform.roomtype.value;
  var fromDay = parseInt(quickform.fromDay.value);
  var fromMonth = parseInt(quickform.fromMonth.value);
  var fromYear = parseInt(quickform.fromYear.value);
  var nights = quickform.nights.value;

  if (property_id == 0 || room_id == 0) {
    alert("To continue you must choose a destination and room type.\nPlease select one of each before proceeding.");
    return;
  }

  var currentstatus = true;
  if (fromDay < currentDay && fromMonth == currentMonth && fromYear == currentYear) currentstatus = false;
  if (fromDay >= currentDay && fromMonth < currentMonth && fromYear == currentYear) currentstatus = false;
  if (fromDay < currentDay && fromMonth < currentMonth && fromYear == currentYear) currentstatus = false;

  if (!currentstatus) {
    alert("The dates selected cannot be less than the present date.\nThe select boxes will be reset.\n\nPlease select a different date range.");
    quickform.fromDay.selectedIndex = currentDay - 1;
    quickform.fromMonth.selectedIndex = currentMonth - 1;
    return;
  } else {
    quickform.action = "functions/addroom.asp?itinerary_id=0&property_id="+ property_id +"&room_id="+ room_id +"&rtype=3&nights=" + nights + "#rates";
    quickform.submit();
  }
}

function loadRoomTypeSelect() {
  var roomselect = document.quickfrm.roomtype;
  var roomlength = roomselect.length;

  if (!xml) {
    var property_id = document.quickfrm.property.value;

    // remove existing options
    for (i = 0; i < roomlength; i++)
      roomselect.remove(0);

    if (property_id != 0) {
      roomselect.options[0] = new Option("Loading", "0");
      loadXMLDoc("includes/searchproperty.asp?property_id="+ property_id +"&ran="+ getRan());
    } else
      roomselect.options[0] = new Option("Room Type", "0");
  } else {
    var roomtypes = xml.responseXML.documentElement.getElementsByTagName("roomtype");

    // remove loading option
    roomselect.remove(0);

    // replace with new options
    for (i = 0; i < roomtypes.length; i++)
      roomselect.options[i] = new Option(roomtypes[i].getAttribute("name"), roomtypes[i].getAttribute("id"));

    xml = null;
  }
}

// add mouse move event for FireFox
if (window.addEventListener) {
  document.captureEvents(Event.MOUSEMOVE);

  Event.prototype.__defineGetter__("srcElement", function() {
    var eElement = this.target;
    return eElement;
  });
}

document.onmousemove = moveCallInfo;

function moveCallInfo(oEvent) {
  if (!oEvent) oEvent = window.event;

  if (oEvent.srcElement.innerHTML == "CALL" && oEvent.srcElement.nodeName == "TD") {
    if (!document.getElementById("call")) {
      var eDiv = document.createElement("div");
      document.body.insertAdjacentElement("beforeEnd", eDiv);
      with (eDiv) {
        id = "call";
        if (browser.isIE) {
          style.top = (event.clientY + document.documentElement.scrollTop + 6) + "px";
          style.left = (event.clientX + document.documentElement.scrollLeft + 6) + "px";
        } else {
          style.top = (oEvent.pageY + 6) + "px";
          style.left = (oEvent.pageX + 6) + "px";
        }
        style.position = "absolute";
        style.width = "138px";
        style.zIndex = "100";
        //style.color = "#FFF";
        style.color = "#000";
        //style.backgroundColor = "#C0BB9C";
        style.backgroundColor = "#FFF";
        style.textAlign = "left";
        //style.border = "1px solid #6D6763";
        style.border = "1px solid #020202";
        style.padding = "2px 4px";
        innerHTML = "Please call our Customer Contact Centre on <strong>1800 635 344</strong> (national) or <strong>+613 6225 7092</strong> (overseas) for further room options.";
      }
    }
    var eCall = document.getElementById("call");

    if (browser.isIE) {
      eCall.style.top = (event.clientY + document.documentElement.scrollTop + 6) + "px";
      eCall.style.left = (event.clientX + + document.documentElement.scrollLeft + 6) + "px";
    } else {
      eCall.style.top = (oEvent.pageY + 6) + "px";
      eCall.style.left = (oEvent.pageX + 6) + "px";
    }
  }
}

function removeCallInfo() {
  if (document.getElementById("call")) document.getElementById("call").removeNode(true);
}

function getRan() {
  var ran = new Date();
  ran = ran.getDate() +""+ ran.getMonth() +""+ ran.getYear() +""+ ran.getHours() +""+ ran.getMinutes() +""+ ran.getSeconds() +""+ ran.getMilliseconds();
  return ran;
}

/* EP (2007-10-22)	*/
function roomRateMouseOver(objSource)	{
	objSource.className += " room-rate-cell-over";
}

function roomRateMouseOut(objSource)	{
	objSource.className = objSource.className.replace(" room-rate-cell-over", "");
	// just in case,
	objSource.className = objSource.className.replace("room-rate-cell-over", "");
}

function calculateSelectedDateRange(pageid, itinerarayid, cellDay, cellMonth, cellYear)	{
	var rateform = document.ratefrm;
	var adult = parseInt(rateform.adult.value);
	var child = parseInt(rateform.child.value);
	
	var fromDay = parseInt(rateform.fromDay.value);
	var fromMonth = parseInt(rateform.fromMonth.value);
	var fromYear = parseInt(rateform.fromYear.value);
	var toDay = parseInt(rateform.toDay.value);
	var toMonth = parseInt(rateform.toMonth.value);
	var toYear = parseInt(rateform.toYear.value);
	//var one_day = 1000*60*60*24

	var d;
	var t1 = Date.UTC(fromYear, fromMonth - 1, fromDay);
	var t2 = Date.UTC(toYear, toMonth - 1, toDay);
	cellYear = "20" + cellYear;
	var t3 = Date.UTC(cellYear, cellMonth - 1, cellDay);
	var tdate = "";

	if (t2 >= t1)	{
		d = new Date(Math.round(t3 + (t2 - t1)));
		tdate = d.getUTCDate() + "/" + parseInt(d.getUTCMonth() + 1, 10) + "/" + d.getUTCFullYear();
	}
	else	{	// ignore date selection in the dropdownlists
		tdate = "";
	}

	if ((adult + child) > maxppl)	{
		alert("Error: The maximum amount of people allowed for this room is "+ maxppl +".");
	}
	else if (adult == 0)	{
		alert("Error: There must be at least one adult chosen for booking a room.");
	}
	else	{
		location.href="default.asp?pID="+ pageid +"&i="+ itinerarayid +"&deleteroom=true&fdate="+ cellDay +"/"+ cellMonth +"/"+ cellYear + "&tdate=" + tdate + "&adult=" + adult + "&child=" + child + "#rates";
	}
}
/* ------------------- */