// Used for pricing pages
var locationname, tempname, accomname, accom_id, tour_id, cruise_id = 1684, railway_id = 1682, count, icon, rname, adult;
var accomstatus, accomcount, tourstatus, tourcount, cruisestatus = false, cruisecount = 8, railwaystatus = false, railwaycount = 16;
var output, output_days, output_dates, oDate, process = false;
var fromYear, fromMonth, fromDay, fromDate;

function displayPricingTables(locname) {
	var i, d; var rateform = document.ratefrm;
	// EP (2007-11-02): Fixed array construction for WeekDays.
	//var weekday = new Array(6);
	var weekday = new Array(7);
	weekday[0] = "SUN"; weekday[1] = "MON"; weekday[2] = "TUE"; weekday[3] = "WED"; weekday[4] = "THU"; weekday[5] = "FRI"; weekday[6] = "SAT";
	
	if (locname.length == 0)
		//var locationarr = new Array("Strahan", "Cradle", "Freycinet", "Hobart", "Launceston");
		//2007-10-12: JM - Only process Launceston for Wrestpoint
		var locationarr = new Array("Hobart");
	else
		var locationarr = new Array(locname);
	
	output = ""; output_days = ""; output_dates = "";
	
	fromYear = parseInt(rateform.fromYear.value);
	fromMonth = parseInt(rateform.fromMonth.value);
	fromDay = parseInt(rateform.fromDay.value);
	fromDate = fromDay +"/"+ fromMonth +"/"+ fromYear;
	toYear = fromYear;
	toMonth = fromMonth;
	toDay = fromDay;
	
	var currentstatus = true;
	// EP (2007-11-02): Reinforcing if statement conditions for year bit, just in case!
	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 (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.");
		changeDay("from", currentDay, currentMonth, currentYear);
		rateform.fromDay.selectedIndex = currentDay - 1;
		rateform.fromMonth.selectedIndex = currentMonth - 1;
		return void(0);
	}
	
	if (currentDay == 0) {
		if ((currentMonth - 1) == 0) {
			currentMonth = 12;
			currentYear -= 1;
		}
		// calculate the number of days in the month and year.
		currentDay = calcDays(1, currentMonth, currentYear, "");
	}
	
	calcDateSpan(); // function found in js/planner.js
	
	// get date and day name span
	for (d = 0; d <= 9; d++) {
		datespan = datearr[d];
		// EP (2007-11-02): Fixed length of year to 4 for Date() object to pickup the date correctly
		//dyear = datespan.substring(2, 4);
		dyear = datespan.substring(0, 4);
		dmonth = datespan.substring(5, datespan.indexOf("-", 5));
		dday = datespan.substring(6 + dmonth.length, datespan.length);
		ddate = dday +"/"+ dmonth +"/"+ dyear;
		
		oDate = new Date(dmonth +"/"+ dday +"/"+ dyear);
		// EP (2007-11-02): We need to have two digit year now.
		dyear = dyear.substring(2);

		output_dates += "<td class=\"type\">"+ dday +"."+ dmonth +"."+ dyear +"</td>";
		
		if (d == 9)
			// EP (2007-11-02): No if statement needed
			output_days += "<th class=\"border\">"+ weekday[oDate.getDay()] +"</th>";
			//if (oDate.getDay() == 0)
			//	output_days += "<th class=\"border\">"+ weekday[6] +"</th>";
			//else
			//	output_days += "<th class=\"border\">"+ weekday[oDate.getDay() - 1] +"</th>";
		else
			// EP (2007-11-02): No if statement needed
			output_days += "<th>"+ weekday[oDate.getDay()] +"</th>";
			//if (oDate.getDay() == 0)
			//	output_days += "<th>"+ weekday[6] +"</th>";
			//else
			//	output_days += "<th>"+ weekday[oDate.getDay() - 1] +"</th>";
	}
	
	// EP (2007-11-02): Code looks confusing.
	//for (i = 0; i <= (locationarr.length - 1); i++) {
	for (i = 0; i < locationarr.length; i++) {
		locationname = locationarr[i];
		output += "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" class=\""+ locationname.toLowerCase() + "-rates\" id=\""+ locationname.toLowerCase() +"\">";
		output += "<tr><th class=\"indent-title\">";
		
		if (locationname == "Cradle")
			tempname = "Cradle Mountain";
		else
			tempname = locationname;
		
		output += tempname + "</th>" + output_days;
		
		if (locationname == "Hobart")
			accomname = "Wrest Point";
		else if (locationname == "Launceston")
			accomname = "Country Club Resort & Villas";
		else
			accomname = "Accommodation";
		
		output += "<tr><td class=\"indent\" onClick=\"displayRates('"+ locationname.toLowerCase() +"', 1);\"><img src=\"Images/icons/booknow_plus.gif\" name=\"accom"+ locationname.toLowerCase() +"\" alt=\"\" width=\"9\" height=\"9\" border=\"0\" class=\"sign\">"+ accomname +"</td>";
		output += output_dates;
		if (locationname != "Hobart" && locationname != "Launceston") {
			output += "<tr><td class=\"indent\" onClick=\"displayRates('"+ locationname.toLowerCase() +"', 2);\"><img src=\"Images/icons/booknow_plus.gif\" name=\"tour"+ locationname.toLowerCase() +"\" alt=\"\" width=\"9\" height=\"9\" border=\"0\" class=\"sign\">Experiences</td>";
			output += output_dates;
		}
		output += "</table>";
	}
	
	document.getElementById("displayrates").innerHTML = output;
	document.getElementById("display").style.display = "block";
	
	
	//2007-10-12: JM - Always load hobart as it's the only venue for Wrestpoint.
	displayRates('hobart', 1);
}

function displayRates(lname, rt) {
	locationname = lname;
	rtype = rt;
	
	if (process) return void(0);
	
	switch (locationname) {
		case "strahan" : accom_id = 1678; tour_id = 1683; break;
		case "cradle" : accom_id = 1677; tour_id = 1680; break;
		case "freycinet" : accom_id = 1676; tour_id = 1681; break;
		case "hobart" : accom_id = 1679; break;
		case "launceston" : accom_id = 1675;
	}
	
	accomcount = eval("id"+ accom_id).split(",").length;
	if (document.images["accom" + locationname].src.indexOf("minus") > -1)
		accomstatus = false;
	else
		accomstatus = true;
	
	if (accom_id != 1679 && accom_id != 1675) {
		tourcount = eval("id"+ tour_id).split(",").length;
		if (document.images["tour" + locationname].src.indexOf("minus") > -1)
			tourstatus = false;
		else
			tourstatus = true;
	}
	
	if (rtype == 3)
		if (document.images["cruise"].src.indexOf("minus") > -1)
			cruisestatus = false;
		else
			cruisestatus = true;
	
	if (rtype == 4)
		if (document.images["railway"].src.indexOf("minus") > -1)
			railwaystatus = false;
		else
			railwaystatus = true;
	
	// swap plus and minus sign respectively
	switch (rtype) {
		case 1 : rname = "accom" + locationname; property_id = accom_id; adult = 2; break;
		case 2 : rname = "tour" + locationname; property_id = tour_id; adult = 1; break;
		case 3 : rname = "cruise"; property_id = cruise_id; adult = 1; break;
		case 4 : rname = "railway"; property_id = railway_id; adult = 1;
	}
	
	if (document.images[rname].src.indexOf("minus") > -1)
		icon = "plus";
	else
		icon = "minus";
	
	document.images[rname].src = eval(icon + ".src");
	
	// edit table rows
	var tbl = document.getElementById(locationname);
	var rowcol = tbl.rows;
	
	// calculate number of rows to delete/add and where to start/end
	if (rowcol.length == 3) {
		if (rtype == 1)
			count = 2;
		else
			count = 3;
	} else {
		if (rtype == 1) {
			if (!accomstatus)
				deleteRows(tbl, 1, accomcount);
			else
				count = 2;
		} else {
			if (rtype == 3) {
				if (!cruisestatus)
					if (accomstatus)
						deleteRows(tbl, tourcount + 3, cruisecount);
					else
						deleteRows(tbl, accomcount + tourcount + 3, cruisecount);
				else
					if (accomstatus)
						count = tourcount + 4;
					else
						count = accomcount + tourcount + 4;
			} else if (rtype == 4) {
				if (!railwaystatus)
					if (accomstatus)
						if (cruisestatus)
							deleteRows(tbl, tourcount + cruisecount + 4, railwaycount);
						else
							deleteRows(tbl, tourcount + 4, railwaycount);
					else
						if (cruisestatus)
							deleteRows(tbl, accomcount + tourcount + cruisecount + 4, railwaycount);
						else
							deleteRows(tbl, accomcount + tourcount + 4, railwaycount);
				else
					if (accomstatus)
						if (cruisestatus)
							count = tourcount + cruisecount + 5;
						else
							count = tourcount + 5;
					else
						if (cruisestatus)
							count = accomcount + tourcount + cruisecount + 5;
						else
							count = accomcount + tourcount + 5;
			} else {
				if (!tourstatus)
					if (locationname == "strahan")
						if (accomstatus)
							if (cruisestatus && railwaystatus) {
								deleteRows(tbl, 2, tourcount + cruisecount + railwaycount + 2);
								cruisestatus = false; railwaystatus = false;
							} else if (cruisestatus && !railwaystatus) {
								deleteRows(tbl, 2, tourcount + cruisecount + 2);
								cruisestatus = false;
							} else if (!cruisestatus && railwaystatus) {
								deleteRows(tbl, 2, tourcount + railwaycount + 2);
								railwaystatus = false;
							} else
								deleteRows(tbl, 2, tourcount + 2);
						else
							if (cruisestatus && railwaystatus) {
								deleteRows(tbl, accomcount + 2, tourcount + cruisecount + railwaycount + 2);
								cruisestatus = false; railwaystatus = false;
							} else if (cruisestatus && !railwaystatus) {
								deleteRows(tbl, accomcount + 2, tourcount + cruisecount + 2);
								cruisestatus = false;
							} else if (!cruisestatus && railwaystatus) {
								deleteRows(tbl, accomcount + 2, tourcount + railwaycount + 2);
								railwaystatus = false;
							} else
								deleteRows(tbl, accomcount + 2, tourcount + 2);
					else
						if (accomstatus)
							deleteRows(tbl, 2, tourcount);
						else
							deleteRows(tbl, accomcount + 2, tourcount);
				else
					count = accomcount + 3;
			}
		}
	}
	
	if ((rtype == 1 && accomstatus) || (rtype == 2 && tourstatus) || (rtype == 3 && cruisestatus) || (rtype == 4 && railwaystatus)) {
		// display loading
		var row = tbl.insertRow(count);
		var cellcol = row.cells;
		var loading = rowcol[row.rowIndex].insertCell(cellcol.length);
		loading.colSpan = 11;
		loading.innerHTML = "LOADING...";
		
		if (rtype == 1)
			property_id = accom_id;
		else if (rtype == 2)
			property_id = tour_id;
		else if (rtype == 3)
			property_id = 1682;
		else
			property_id = 1684;
		
		process = true;
		loadXMLDoc("includes/searchrooms.asp?property_id="+ property_id +"&rtype="+ rtype +"&fromDate="+ datearr[0] +"&adult="+ adult +"&child=0&ran="+ getRan());
		//alert("includes/searchrooms.asp?property_id="+ property_id +"&rtype="+ rtype +"&fromDate="+ datearr[0] +"&adult="+ adult +"&child=0&ran="+ getRan());
	}
}

function processPricingRates() {
	var nodecount = 0; var roomarr, namearr, pagearr;
	var row, cellcol, celltitle, cell, rowlink, dstatus, firsttime = true;
	var tbl = document.getElementById(locationname);
	var rowcol = tbl.rows, ratecount = 0;
	
	//alert("rtype = "+ rtype +"\naccomstatus = "+ accomstatus +"\ntourstatus = "+ tourstatus +"\ncruisestatus = "+ cruisestatus +"\nrailwaystatus = "+ railwaystatus);
	
	// delete loading row
	if (rtype == 1)
		tbl.deleteRow(2);
	else if (rtype == 2)
		if (accomstatus)
			tbl.deleteRow(3);
		else
			tbl.deleteRow(accomcount + 3);
	else if (rtype == 3)
		if (accomstatus)
			tbl.deleteRow(tourcount + 4);
		else
			tbl.deleteRow(accomcount + tourcount + 4);
	else
		if (accomstatus && cruisestatus)
			tbl.deleteRow(tourcount + cruisecount + 5);
		else if (accomstatus && !cruisestatus)
			tbl.deleteRow(tourcount + 5);
		else if (!accomstatus && cruisestatus)
			tbl.deleteRow(accomcount + tourcount + cruisecount + 5);
		else if (!accomstatus && !cruisestatus)
			tbl.deleteRow(accomcount + tourcount + 5);
		else
			tbl.deleteRow(accomcount + tourcount + 4);
	
	roomarr = eval("id"+ property_id).split(",");
	namearr = eval("name"+ property_id).split(",");
	pagearr = eval("page"+ property_id).split(",");
	
	var searchxml =  xml.responseXML.documentElement.childNodes;
	var slength = searchxml.length;
	var dlength = slength / roomarr.length;
	var z;
	
//alert("slength: " + slength + ", dlength: " + dlength);
	
	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";
	}
	
	for (var r = 0; r < roomarr.length; r++) {
		// EP (2007-11-02): Added code to find the current room
		nodecount = -1;	// initialize nodecount
		
		for (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;
			}
		}

		// setup table row and cells
		row = tbl.insertRow(count);
		cellcol = row.cells;
		celltitle = rowcol[row.rowIndex].insertCell(cellcol.length);
		
		// write table row events
		row.id = "default.asp?pID="+ pagearr[r] +"&i=0&fdate="+ fromDate;
		row.onmouseover = function() { this.className = "row-on"; };
		row.onmouseout = function() { this.className = ""; };
		row.onclick = function() { location.href = this.id; };
		
		// setup price cells
		celltitle.className = "indent-room";
		celltitle.innerHTML = namearr[r];
		
		count += 1;
		
		for (var s = 0; s <= 9; s++) {
			// format date manually and write date row.. this is used in the loop
			datespan = datearr[s];
			dyear = datespan.substring(2, 4);
			dmonth = datespan.substring(5, datespan.indexOf("-", 5));
			dday = datespan.substring(6 + dmonth.length, datespan.length);
			ddate = dday +"."+ dmonth +"."+ dyear;
			
			// create cell
			cell = rowcol[row.rowIndex].insertCell(cellcol.length);
			
			if (slength > nodecount && nodecount != -1) {
				//alert(nodecount);
				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;
					// display price, full or no rate
					price = parseInt(search_child[4].firstChild.nodeValue);
					available = parseInt(search_child[3].firstChild.nodeValue);
					
					if (available) {
						cell.innerHTML = "$"+ price;
					} else {
						cell.className = "full";
						cell.innerHTML = "CALL";
						cell.onmouseout = function () { removeCallInfo() };
					}
				} else {
					cell.className = "full";
					cell.innerHTML = "CALL";
					cell.onmouseout = function () { removeCallInfo() };
				}
			} else {
				cell.className = "full";
				cell.innerHTML = "CALL";
				cell.onmouseout = function () { removeCallInfo() };
			}
			
			// 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 (s == 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;
		}
	}
	
	// Cruise and Railway exceptions
	if (locationname == "strahan" && rtype == 2) {
		// Gordon River Cruise
		row = tbl.insertRow(count);
		cellcol = row.cells;
		celltitle = rowcol[row.rowIndex].insertCell(cellcol.length);
		celltitle.className = "indent-special";
		celltitle.onclick = function() { displayRates("strahan", 3); };
		celltitle.innerHTML = "<img src=\"Images/icons/booknow_plus.gif\" name=\"cruise\" alt=\"\" width=\"9\" height=\"9\" border=\"0\" class=\"sign\">Gordon River Cruises";
		
		// get date and day name span
		for (d = 0; d <= 9; d++) {
			cell = rowcol[row.rowIndex].insertCell(cellcol.length);
			datespan = datearr[d];
			dyear = datespan.substring(2, 4);
			dmonth = datespan.substring(5, datespan.indexOf("-", 5));
			dday = datespan.substring(6 + dmonth.length, datespan.length);
			cell.className = "special";
			cell.innerHTML = dday +"."+ dmonth +"."+ dyear;
		}
		
		count += 1;
		
		// West Coast Railway
		row = tbl.insertRow(count);
		cellcol = row.cells;
		celltitle = rowcol[row.rowIndex].insertCell(cellcol.length);
		celltitle.className = "indent-special";
		celltitle.onclick = function() { displayRates("strahan", 4); };
		celltitle.innerHTML = "<img src=\"Images/icons/booknow_plus.gif\" name=\"railway\" alt=\"\" width=\"9\" height=\"9\" border=\"0\" class=\"sign\">West Coast Railway";
		
		// get date and day name span
		for (d = 0; d <= 9; d++) {
			cell = rowcol[row.rowIndex].insertCell(cellcol.length);
			datespan = datearr[d];
			dyear = datespan.substring(2, 4);
			dmonth = datespan.substring(5, datespan.indexOf("-", 5));
			dday = datespan.substring(6 + dmonth.length, datespan.length);
			cell.className = "special";
			cell.innerHTML = dday +"."+ dmonth +"."+ dyear;
		}
	}
	
	process = false;
}

function deleteRows(dtbl, start, total) {
	d = total + start;
	while (d > start) {
		dtbl.deleteRow(d);
		d -= 1;
	}
}
