
String.prototype.pad = function(l, s, t) {
    return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
        + 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
        + this + s.substr(0, l - t) : this;
};

function getXmlHTTPRequestObject() {
	
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xmlhttp =  new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
        			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (ee) {
				xmlhttp = false;
			}
            	}

	} else {
		xmlhttp = false;
	}
	return xmlhttp;
}



//handle ajax respoonse
function handleXmlHttpResponse(requestObject, containerId) {
	 if (requestObject.readyState == 4)
	 {
	 	if (requestObject.status==200 || window.location.href.indexOf("http")==-1)
		{
			document.getElementById(containerId).innerHTML=requestObject.responseText
		}
		else
		{
			// If error loading booking sheet data, show error message.
			document.getElementById(containerId).innerHTML="<p class='NoBookingData'>Information Not Available</p>";
		}
	}
	else if (window.location.href.indexOf("http")==-1) // Local access.
	{
		// For local access, cannot rely on status code (always 0) to detect success completion.
		// For non-existence file, readyState will never reach 4.
		document.getElementById(containerId).innerHTML="&nbsp;";
	}
}


function loadTableData(url, containerId) {
	// setStatusMessage(true);

	var requestObject = getXmlHTTPRequestObject();
	
	// Open url to fetch content.
	// Note - IE caches content in GET method, so to work around this, we append
	// a dummy parameter that is random. This will force IE not to cache the page.
	// Can't use POST method because it doesn't work with html url on Apache.
	requestObject.open('GET', url+"?random=" + Math.random(), true);
	
	// Hide status message. Use a bit of delay before hiding it
	// so that it stays visible for a while.
//	window.setTimeout('setStatusMessage(false)', 1000);
	// setStatusMessage(false);
	
 	requestObject.onreadystatechange=function() {
		handleXmlHttpResponse(requestObject, containerId);
	}
	
	requestObject.send(null);
}

function loadTable() {
   setStatusMessage(true);
   var dateList=document.getElementById("dateList");
   url = dateList.options[dateList.selectedIndex].value;
   loadTableData(url, "bookingSheet");
   window.setTimeout('setStatusMessage(false)', 3000);
}

function getDay(numDay, friendly){
	var months=new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var days=new Array("Sun", "Mon", "Tue", "Wed",  "Thu", "Fri", "Sat");
	var daysLong=new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

	var myDate=new Date()
	myDate.setDate(myDate.getDate()+numDay)
	
	if (friendly) {
	    return myDate.getDate() + " " + months[myDate.getMonth()] + " "+myDate.getFullYear() + " - " + daysLong[myDate.getDay()];
	} else {
		return myDate.getFullYear() + "-" + String(myDate.getMonth()+1).pad(2, "0", 0) + "-" + String(myDate.getDate()).pad(2, "0", 0);
	}

	
}

function writeOption() {
       for (i=0; i<14; i++) {
		var date = getDay(i, true);
		// var randomnumber=Math.floor(Math.random()*1001);
		// var docName = getDay(i, false) + ".html?rand="+randomnumber;
		var docName = getDay(i, false) + ".html";
		// var docName = getDay(i, false) + ".html?dummy=" + new Date().getTime();
		if (i==0) {
			document.write("<option selected='selected' value=" + docName + ">" + date + "</option>");
		} else {
			document.write("<option value=" + docName + ">" + date + "</option>");
		}
	}
}

/**
function writeDateLinks() {
       for (i=0; i<14; i++) {
		var date = getDay(i, true);
		var docName = getDay(i, false) + ".html";
		document.write("<a href=\"javascript:loadTableData('"+docName+"', 'bookingSheet')\">"+date+"</a><br>");
	}
}
**/

function selectNext(num) {
	 var dateList=document.getElementById("dateList");
	 selectedIndex = dateList.selectedIndex;
	 if ((selectedIndex == 0 && num<0) || (selectedIndex== 13 && num>0)){
	  	return;
	 }
	 dateList.selectedIndex = selectedIndex + num;
	 loadTable();
}

function setStatusMessage(display) {
	if (display) {
			document.getElementById("statusText").innerHTML="<font class='status'>Loading details...</font>";
	} else {
		document.getElementById("statusText").innerHTML="&nbsp;";
	}
}


