function MonthToString(m)
{
	var s=new String();
	
	switch (m) {
		case( 0): s = "Jan"; break;
		case( 1): s = "Feb"; break;
		case( 2): s = "Mar"; break;
		case( 3): s = "Apr"; break;
		case( 4): s = "May"; break;
		case( 5): s = "Jun"; break;
		case( 6): s = "Jul"; break;
		case( 7): s = "Aug"; break;
		case( 8): s = "Sep"; break;
		case( 9): s = "Oct"; break;
		case(10): s = "Nov"; break;
		case(11): s = "Dec"; break;
		default: s="?";
	}
	
	return s;
}

function PadLeft(i,iRequiredLen,sPad)
{
	var s=new String();
	var j;

	s = i.toString();
	for (j=s.length; j<iRequiredLen; j++)
		s = sPad + s;
		 	
	return s;
}

function PadRight(s,iRequiredLen,sPad)
{
	var sRet=new String();
	var j;

	sRet = s;
	for (j=s.length; j<iRequiredLen; j++)
		sRet = sRet + sPad;
		 	
	return sRet;
}

function DateToString(dt)
{	
	var s = new String();
	
	s = PadLeft(dt.getDate().toString(),2,"0") + " " + MonthToString(dt.getMonth()) + " " + dt.getYear().toString();
	s = s + ", " + PadLeft(dt.getHours(),2,"0") + ":" + PadLeft(dt.getMinutes(),2,"0") + ":" + PadLeft(dt.getSeconds(),2,"0");
	return s;
}

function ShowHTMLDialog( strLocation, varArguments, strFeatures )
{
	var strFeatures2 = new String();
	if (strFeatures == null)
		strFeatures2 = "center: yes; dialogWidth: 700px";
	else
		strFeatures2 = strFeatures;

	var returnValue = window.showModalDialog( strLocation, varArguments, strFeatures2 )
	return returnValue;
}

function ShowHTMLForm( strLocation, strName, strFeatures )
{
	var strFeatures2 = new String();
	if (strFeatures == null)
	{
		var intHeight = (window.screen.availHeight - 60);
		var intWidth = (window.screen.availWidth - 30);

		var intLeft = (window.screen.availWidth - intWidth) / 2 - 10;
		var intTop = 0;
		
		strFeatures2 = "left=" + intLeft + ", top=" + intTop + ", height=" + intHeight + ", width=" + intWidth + ",  scrollbars=yes, resizable=yes, status=yes,  menubar=no, directories=no, fullscreen=no, toolbar=no";
	}
	else
		strFeatures2 = strFeatures;

	return window.open( strLocation, strName, strFeatures2 )
}

function ShowPage( strURL, strName, intHeight, intWidth, intTop, intLeft, features )
{
	var top = 0;
	var left = 0;
	var height = (window.screen.availHeight - 60);
	var	width = (window.screen.availWidth - 30);

	if (intHeight != null)
		height = intHeight;
	
	if (intWidth != null)
		width = intWidth;

	if (intTop == null)
	{
		top = (window.screen.availHeight - height) / 2 - 10;
		top = top>=0?top:0;
	}
	else
		top = intTop;

	if (intLeft == null)
	{
		left = (window.screen.availWidth - width) / 2 - 10;
		left = left>=0?left:0;
	}
	else
		left = intLeft;		

	var strFeatures;
	var sizeAndPosition = "left=" + left + ", top=" + top + ", height=" + height + ", width=" + width;

	if ( features )
	{
		strFeatures = features;
		if ( features != "" )
			strFeatures += ", ";
	}
	else
	{
		strFeatures = new String( "resizable=1, scrollbars=1, status=1, menubar=0, directories=0, fullscreen=0, toolbar=0, " );
	}
	strFeatures += sizeAndPosition;

	return window.open( strURL, strName, strFeatures );
} // ShowPage

function SetClass( strID, strClass )
{
	var obj = document.getElementById(strID);
	
	if (obj)
	{
		if ( obj.className != strClass )
			obj.className = strClass;
	}
}

function ShowPageCentered( strURL, strName, intHeight, intWidth )
{
	return ShowPage( strURL, strName, intHeight, intWidth );
}

function ShowCinemaDetails( cinemaID )
{
	window.open( "images/cinema_" + cinemaID + ".pdf", "_blank" );
	//ShowPageCentered( "cinema_details.asp?CinemaID=" + cinemaID, "_blank", 300, 590 );
}

function printPage()
{
	window.print();
	return true;
}

function ShowSessionsPrinterFriendly( cinemaID, movieID )
{
	ShowPage( "sessions_printerfriendly.asp?MovieID=" + movieID + "&CinemaID=" + cinemaID, "_blank", 400, 600, null, null, 
		"resizable=1, scrollbars=1, status=1, menubar=1, directories=0, fullscreen=0, toolbar=1" );
}

function toggleBox( key )
{
	var objBox    = document.getElementById( "box" + key );
	var objAnchor = document.getElementById( "anchor" + key );
	
	if ( objBox )
	{
		if ( objBox.style.display == "none" )
		{
			// Show it
			objBox.style.display  = "block";
			if (objAnchor)
			{
				objAnchor.title     = "Hide Detail";
				objAnchor.innerText = "Hide Detail";
			}
		}
		else
		{
			// Hide it
			objBox.style.display  = "none";
			if (objAnchor)
			{
				objAnchor.title     = "Show Detail";
				objAnchor.innerText = "Show Detail";
			}
		}
	}
}

function longDateToShortDate( longDate )
{
	var dtNow = new Date();
	var dt = new Date( Date.parse( longDate + " " + dtNow.getFullYear() ) );

	// Check if the date is next year
	if ( dt.getMonth() < dtNow.getMonth() )
		dt.setFullYear( dt.getFullYear() + 1 );
	
	// Now return it as dd/mm/yyyy
	var ret = new String( 
		(dt.getDate()<10?"0":"") + dt.getDate() + "/" 
	  + (dt.getMonth()<9?"0":"") + (dt.getMonth()+1) + "/" 
	  + dt.getFullYear() );
	
	return ret;
}

function selectSessionEx( movieID, cinemaID, sessionID, sessionDate, sessionTime, ampm, priceGroup )
{
	DBG( movieID + ", " + cinemaID + ", " + sessionDate + ", " + sessionTime + " " + ampm )

	// "22/08/2002 03:05 PM"
	var sessionDateEscaped = new String( longDateToShortDate( sessionDate ) );
	sessionDateEscaped = sessionDateEscaped.replace( "/", "%2F" );
	sessionDateEscaped = sessionDateEscaped.replace( "/", "%2F" );
	var sessionDateTime = new String( sessionDateEscaped + "+" + escape(sessionTime) + "+" + ampm.toUpperCase() );
	
	var url = new String( "buy.asp?" );
	url += "Cinema=" + cinemaID;
	url += "&Movie=" + movieID;
	url += "&SessDateTime=" + sessionDateTime;
	url += "&SessID=" + sessionID;
	url += "&PriceGroup=" + priceGroup;

	// load into same frame
	//document.location.href = url;
	ShowHTMLForm( url, "winSession", null );

	return false;
}

function buy( sessionID )
{
	if ( sessionID.substring( 0, 3 ) == "R06" )
	{
		alert( "Sorry.  Online sales are not available at Rialto Palmerston North." );
		return false;
	}
	
	var url = "seatsmart/SelectTickets.aspx?SessionID=" + sessionID;
	ShowHTMLForm( url, "_blank", null );
	return false;
}

function SelectDay( id, sessionDate, index )
{
	var obj = document.getElementById( "cmbDay_" + id );
	if (obj)
	{
		DBG( "SelectDay( '" + id + "' )" );
		if (obj.selectedIndex != index)
			obj.selectedIndex = index;

		var i = 1;
		var tbl = document.getElementById( "tblSession_" + id + "_" + i );
		var className;

		while (tbl)
		{
			// if the session date is the same as the date in the select combo
			// at item # i-1, then show it
			if (obj.options(i-1).value == sessionDate)
				className = "BoxBigDetailShow";
			else
				className = "BoxBigDetailHide";

			if ( tbl.className != className )
			{
				tbl.className = className;
			}

			i++;	
			tbl = document.getElementById( "tblSession_" + id + "_" + i );
		}
	}
		
	return true;
}
function soldOutSession(seatsAvailable)
{
	alert( "This session is sold out" );
	return false;
}

function goShopping()
{
	// lets go shopping
	ShowHTMLForm( "Shopping/Selection.aspx", "Shopping", null );
	return false;	
}

// Close current window and force parent to refresh itself.
function closeAndRefreshParent()
{
	if ( window )
	{
		if ( window.opener )
		{
			setTimeout( "window.close()", 500 );
			window.opener.document.location = window.opener.document.location;
		}
		else if ( window.parent.frames )
		{
			if ( window.parent.opener )
			{
				setTimeout( "window.parent.close()", 500 );
				window.parent.opener.document.location = window.parent.opener.document.location;
			}
		}
	}
}

// Reload myself
function refreshSelf()
{
	window.location.href = window.location.href;
}

// Force parent to refresh itself.
function refreshParent()
{
	if ( window )
	{
		if ( window.opener )
		{
			if ( typeof( window.opener.refreshSelf != "undefined" ) )
			{
				window.opener.refreshSelf();
			}
		}
		else if ( window.parent.frames )
		{
			if ( window.parent.opener )
			{
				window.parent.opener.document.location = window.parent.opener.document.location;
			}
		}
	}
}
