window.onload = jsFnOnLoad;

function jsFnOnLoad()
{
	if (document.getElementById)
	{
		HideMakeBooking();
		EnableTable();
		
		aProductContent = document.getElementById('content_bottom');
		if (aProductContent != null)
		{
			var aDiv = document.getElementsByTagName("div");
			var aFirst = true;
			if (aDiv != null) 
			{
				for(aDivIndex=0; aDivIndex < aDiv.length; aDivIndex++)
				{
					if(aDiv[aDivIndex].className=='tab')
					{
						if(aFirst) aFirst = false;
						else aDiv[aDivIndex].style.display = 'none';
					}
				}
			}
		}
		

		//*** Remove confirm ***********************************************************************************************
		var remove = document.getElementsByTagName("a");
 		if (remove != null) 
		{
			for(aremindex=0;aremindex<remove.length;aremindex++)
			{
				if(remove[aremindex].className=='rem')
				{
					remove[aremindex].onclick = JSFnRemoveClick;
				}
			}
		}				
		//******************************************************************************************************************

		//*** View HTML doc new window *************************************************************************************
		var viewhtml = document.getElementsByTagName("a");
 		if (viewhtml != null) 
		{
			for(adocindex=0;adocindex<viewhtml.length;adocindex++)
			{
				if(viewhtml[adocindex].className=='viewhtml')
				{
					viewhtml[adocindex].onclick = JSFnNewWindow;
				}
			}
		}				
		//******************************************************************************************************************
	}
}


/*** Pop Up Window ***/
function JSFnNewWindow()
{
	var aWindow = window.open(this.href, 'VIewHTML', 'scrollbars=yes, menubar=no, resizeable=yes, toolbar=no, width=800px, height=800px')
	aWindow.focus();
	return false;
}
//********************************************************************************************************************************

/*** Validate Form ***/
function JSFnValidateForm(aRequiredFields)
{
	for (aIndex = 0; aIndex < aRequiredFields.length; aIndex = aIndex + 2)
	{
		currElement = document.getElementById(aRequiredFields[aIndex]);
		if (currElement != null)
		{
			if  (   (   (currElement.type == 'text')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'password')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'checkbox')
				     && (currElement.checked == false))
				 || (   (currElement.type == 'file')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'textarea')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'select-one')
				     && (currElement.value == '')))
			{
				alert(aRequiredFields[aIndex + 1]);
				return false;
			}
			else if (currElement.type == 'radio')
			{
				aIndex = aIndex + 2;
				if (!currElement.checked)
				{
					currElement = document.getElementById(aRequiredFields[aIndex]);
					if ((currElement.type == 'radio') && (!currElement.checked))
					{
						alert(aRequiredFields[aIndex + 1]);
						return false;
					}
				}
			}
		}
	}
	return true;
}
//********************************************************************************************************************************

/*** Remove Link ***/
function JSFnRemoveClick()
{
	var removed = confirm("Are you sure you want to remove this item?");

	return removed;
}
//********************************************************************************************************************************

function HideMakeBooking()
{
	var aMakeBooking = document.getElementById('nonjavascriptbooking');
	if (aMakeBooking != null)
	{
		aMakeBooking.style.display = 'none';
	}
}

function EnableTable()
{
	var aBookingTable = document.getElementById("bookingstable");
	if (aBookingTable != null)
	{
		var aBookingTableRows = aBookingTable.getElementsByTagName("tr");
		for (aIndex = 0; aIndex < aBookingTableRows.length; aIndex++)
		{
			if (aIndex > 0)
			{
				var aBookingTableCells = aBookingTableRows[aIndex].getElementsByTagName("td");
				aRoomId = aBookingTableRows[aIndex].id;
				aExtraCols = 0;
								
				for (aCellIndex = 1; aCellIndex < aBookingTableCells.length; aCellIndex++)
				{
					if (aBookingTableCells[aCellIndex].colSpan == '1')
					{	
						if (aBookingTableCells[aCellIndex].className.indexOf('valid') >= 0)
						{
							aBookingTableCells[aCellIndex].onmouseover = CrossHair;
							aBookingTableCells[aCellIndex].onmouseout = CrossHairOut;
							aBookingTableCells[aCellIndex].onclick = NewBooking;
							aBookingTableCells[aCellIndex].style.cursor = 'crosshair';
							aBookingTableCells[aCellIndex].title = 'Click to add a new booking';
							
							aBookingTableCells[aCellIndex].id = aGetPar +'=' + aRoomId + '&offsetday=' + (aCellIndex + aExtraCols);
						}
					}
					else aExtraCols += (aBookingTableCells[aCellIndex].colSpan - 1);
				}
			}
		}
	}
}

function NewBooking()
{
	window.location = aClickURL + '.php?' + this.id;
}

function CrossHair()
{
	this.parentNode.childNodes[0].className += ' highlight';
	aExtraCols = 0;
	for (aIndex = 1; aIndex < this.parentNode.childNodes.length; aIndex++)
	{
		aColSpan = this.parentNode.childNodes[aIndex].colSpan;
		if (aColSpan > 0) aExtraCols += (aColSpan - 1);
		if (this.parentNode.childNodes[aIndex] == this) aPos = aIndex + aExtraCols;
	}
//	this.parentNode.parentNode.parentNode.getElementsByTagName('thead')[0].childNodes[0].childNodes[aPos].className += ' highlight';
}

function CrossHairOut()
{
	this.parentNode.childNodes[0].className = this.parentNode.childNodes[0].className.replace(" highlight", "");;
	aExtraCols = 0;
	for (aIndex = 1; aIndex < this.parentNode.childNodes.length; aIndex++)
	{
		aColSpan = this.parentNode.childNodes[aIndex].colSpan;
		if (aColSpan > 0) aExtraCols += (aColSpan - 1);
		if (this.parentNode.childNodes[aIndex] == this) aPos = aIndex + aExtraCols;
	}
	//this.parentNode.parentNode.parentNode.getElementsByTagName('thead')[0].childNodes[0].childNodes[aPos].className = this.parentNode.parentNode.parentNode.getElementsByTagName('thead')[0].childNodes[0].childNodes[aPos].className.replace("highlight", "");
}
