<!-- JavaScript Copyright (c) 2000, 2001 Whoopy Engineering Limited --> 

function Time(time)
{
if(typeof time == "number"){
	this.hour = 0;
	this.minute = startTime;
	}
else if(typeof time == "string") {
	var timeFields = time.split(":");

	if(timeFields.length == 2)
		{
		this.hour = Number(timeFields[0]);
		this.minute = Number(timeFields[1]);
		}
	else {
		this.hour = 0;
		this.minute = Number(timeFields[0]);
		}
	}
else if((typeof time == "object") && (time.constructor == Time))
	{
		this.hour = Number(time.hour);
		this.minute = Number(time.minute);
	}
else alert("Bad type");
};

Time.prototype.toString = function () {
	if(this.hour)
		{
		if(this.minute < 10) return this.hour + ":0" + this.minute;
		else return this.hour + ":" + this.minute;
		}
	else return this.minute;
	};

Time.add = function (a, b) {
    var res = new Time(a);
//    alert("sum " + a + " + " + b);
//    alert("res " + res.hour + ":" + res.minute);
	res.minute = Number(res.minute) + Number(b);
//  	alert("res " + res.hour + ":" + res.minute);
	if(res.minute >= 60)
		{
		res.hour += Math.floor(res.minute/60);
		res.minute %= 60;
		}
	res.hour %= 24;
//    alert("res " + res.hour + ":" + res.minute);
	return res;
	};

function Bus(startTime, serviceNumber)
{
this.time = new Time(startTime);
this.serviceDay = "default";
this.serviceNum = serviceNumber;
};

function BusStop(name, time, backTime)
{
this.name = name;
this.time = time;
this.backTime = backTime;
};

function stopList() { 
  if (document.images) {
    var args = stopList.arguments;
    if (document.stopArray==null)
    	{
    	document.stopArray = new Array();
    	with (document) for (var j=0; j<args.length; j+=4) {
     		if(j == 0) document.firstStop = args[j+1];
      		else document.lastStop = args[j+1];
      		stopArray[args[j+1]] = new BusStop(args[j], parseInt(args[j+2]), parseInt(args[j+3]));
//     		alert(args[j+1] + " = " +args[j] + " " + args[j+2]);
      		}
      	}
	}
}

function exceptions() { 
  if (document.images) {
    var args = exceptions.arguments;
//    	alert("excep");
   if (document.excepArray==null)
    	{
    	document.excepArray = new Array();
//    	alert("was null");
    	}
    
	var bus = args[0] + args[1];
//	   	alert("execp " + bus);

	document.excepArray[bus] = new Array();
	with (document) for (var j=2; j<args.length; j+=2) {
		excepArray[bus][args[j]] = parseInt(args[j+1]);
		}
	}
}

function Service(colour, days)
{
this.colour = colour;
this.days = days;
}

function serviceColour(tag, colour, days) { 
  if (document.images) {
   if (document.serviceDays==null)
    	{
    	document.serviceDays = new Array();
    	}
    
	document.serviceDays[tag] = new Service(colour, days);
	}
}

function serviceDay()
{ 
if (document.images)
  	{
    var args = serviceDay.arguments;
    for (var j=2; j<args.length; j++)
    	{
		var myBusArray = document.busArray[args[0]];
		for (timeIndex in myBusArray)
			{
//		    alert(myBusArray[timeIndex].time);
		    if(args[j] == myBusArray[timeIndex].time)
		    	{
				myBusArray[timeIndex].serviceDay = args[1];
				break;
				}
			}
		} 
	}
}

function busList()
{
if (document.images)
	{
    var args = busList.arguments;
    if (document.busArray==null)
     	document.busArray = new Array();
    document.busArray[args[0]] = new Array();
    for (var j=1; j<args.length; j+=2) {
    	document.busArray[args[0]][j/2] = new Bus(args[j+1], args[j]);
    	}
//    alert(args[0]);
	}
}



function stopSelectList(name, first)
{
document.writeln("<select name='" + name + "' onChange='printTimetable()'>");
for (mystop in document.stopArray)
	{
	if((first && (mystop == document.firstStop)) || (!first && (mystop == document.lastStop)))
		{
//		alert("<option value='" + mystop + "'  selected>" + document.stopArray[mystop].name + "</option>");
		document.writeln("<option value='" + mystop + "'  selected>" + document.stopArray[mystop].name + "</option>");
		}
	else
		{
//		alert("<option value='" + mystop + "'>" + document.stopArray[mystop].name + "</option>");
		document.writeln("<option value='" + mystop + "'>" + document.stopArray[mystop].name + "</option>");
		}
	}
document.writeln("</select>");
}

function printExceptions()
{
parent.frames['table'].document.open();
for(bus in document.excepArray)
     for(mystop in document.excepArray[bus])
          parent.frames['table'].document.writeln("excep " + bus + " " + mystop + " " + document.excepArray[bus][mystop]);
}

function printTimetable()
{
parent.frames['table'].document.open();
parent.frames['table'].document.writeln("<html>");
parent.frames['table'].document.writeln("<head>");
parent.frames['table'].document.writeln("<title>timetable</title>");
parent.frames['table'].document.writeln("<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>");
parent.frames['table'].document.writeln("</head>");
parent.frames['table'].document.writeln("<body bgcolor='#FFFFFF'>");
parent.frames['table'].document.writeln("<table width='100%' border='0' cellpadding='4' cellspacing='4'>");
parent.frames['table'].document.writeln("<tr><td>&nbsp;</td>");

var fromStop = document.controls.fromStop[document.controls.fromStop.selectedIndex].value;
var toStop = document.controls.toStop[document.controls.toStop.selectedIndex].value;
var day = document.controls.day[document.controls.day.selectedIndex].value;
var colour = document.serviceDays['weekday'].colour;

if(document.controls.fromStop.selectedIndex > document.controls.toStop.selectedIndex)
	{
	org = document.controls.fromStop[document.controls.fromStop.options.length-1].value;
	inc = -1;
	delta = document.stopArray[fromStop].backTime;
	}
else {
	org = document.controls.fromStop[0].value;
	inc = 1;
	delta = document.stopArray[fromStop].time;
	}

theBusArray = document.busArray[org];

for (timeIndex in theBusArray)
    {
	time = theBusArray[timeIndex].time;
	
//	alert(time);

	if(!(exceps = document.excepArray[org+time]))
		exceps = document.excepArray[org+"default"];
    if(!(((theBusArray[timeIndex].serviceDay != 'default') && (theBusArray[timeIndex].serviceDay != day)) || (exceps && ((exceps[fromStop] == 0) || (exceps[toStop] == 0)))))
 	    {
	    parent.frames['table'].document.writeln("<td bgcolor='" + colour + "'>" + theBusArray[timeIndex].serviceNum + "</td>");
	    }
//	else if(exceps) alert("execp " + org+time + " mystop " + exceps[fromStop] + " " + exceps[toStop]);
    }
    
parent.frames['table'].document.writeln("</tr>");
parent.frames['table'].document.writeln("<tr>");

parent.frames['table'].document.writeln("<td>" + document.stopArray[document.controls.fromStop[document.controls.fromStop.selectedIndex].value].name + "</td>");


for (timeIndex in theBusArray)
    {
	time = theBusArray[timeIndex].time;
	
	if(!(exceps = document.excepArray[org+time]))
		exceps = document.excepArray[org+"default"];
    if(!(((theBusArray[timeIndex].serviceDay != 'default') && (theBusArray[timeIndex].serviceDay != day)) || (exceps && ((exceps[fromStop] == 0) || (exceps[toStop] == 0)))))
 	    {
 	    var correct = 0;
 	    var valid = inc > 0;
		for(mystop in document.stopArray)
			{
			if((inc > 0) && (mystop == fromStop))
				break;
			if((inc < 0) && (mystop == fromStop))
				valid = 1;
			if(valid && (exceps[mystop] != null))
				correct += exceps[mystop];
			}
 	    correct += delta;
	    parent.frames['table'].document.writeln("<td bgcolor='" + colour + "'>" + Time.add(time, correct) + "</td>");
//	    parent.frames['table'].document.writeln("<td bgcolor='" + colour + "'>" + time + " + " + correct + "</td>");
	    }
//	else if(exceps) alert("execp " + org+time + " mystop " + exceps[fromStop] + " " + exceps[toStop]);
    }
parent.frames['table'].document.writeln("</tr>");

parent.frames['table'].document.writeln("<tr>");
parent.frames['table'].document.writeln("<td>" + document.stopArray[document.controls.toStop[document.controls.toStop.selectedIndex].value].name + "</td>");
if(inc < 0)
	{
	delta = document.stopArray[toStop].backTime;
	}
else {
	delta = document.stopArray[toStop].time;
	}
for (timeIndex in theBusArray)
    {
    time = theBusArray[timeIndex].time;
	if(!(exceps = document.excepArray[org+time]))
		exceps = document.excepArray[org+"default"];
    if(!(((theBusArray[timeIndex].serviceDay != 'default') && (theBusArray[timeIndex].serviceDay != day)) || (exceps && ((exceps[fromStop] == 0) || (exceps[toStop] == 0)))))
 	    {
 	    var correct = 0;
 	    var valid = 0;
		for(mystop in document.stopArray)
			{
			if((inc > 0) && (mystop == toStop))
				break;
			if((inc < 0) && (mystop == toStop))
				valid = 1;
			if(valid && (exceps[mystop] != null))
				correct += exceps[mystop];
			}
  	    correct += delta;
//	    parent.frames['table'].document.writeln("<td bgcolor='" + colour + "'>" + time + " + " + correct + "</td>");
	    parent.frames['table'].document.writeln("<td bgcolor='" + colour + "'>" + Time.add(time, correct) + "</td>");
	    }
//	else if(exceps) alert("execp " + org+time + " mystop " + exceps[fromStop] + " " + exceps[toStop]);
    }

parent.frames['table'].document.writeln("</tr>");

parent.frames['table'].document.writeln("</table>");
parent.frames['table'].document.writeln("</body>");
parent.frames['table'].document.writeln("</html>");
parent.frames['table'].document.close();
}
