if (clientSideBrowserSpecific){
  loadStylesheet("common","common.css");
	if (browser.isMozilla) {
		loadStylesheet("common","mozilla.css");
		if (browser.versionMinor<1.6)
			loadStylesheet("common","mozilla_1.6down.css");
		else if (browser.versionMinor<1.7){
			loadStylesheet("common","mozilla_1.7down.css");
			}
	} else if (browser.isNS){
		loadStylesheet("common","nav.css");
		if (browser.versionMinor<7.5){
			loadStylesheet("common","nav_7down.css");
		}
	} else if (browser.isIE){
		if (browser.isWin){
			loadStylesheet("common","ie_win.css");
			if (browser.isIE55Down){
				loadStylesheet("common","ie_win_5.5down.css");
			}
			else if (!browser.isIE6up){
				loadStylesheet("common","ie_win_6down.css");
			}
		} else if (browser.isMac){
			loadStylesheet("common","ie_mac.css");
		}
	} else if (browser.isOpera){
		loadStylesheet("common","opera.css");
		if (browser.versionMinor<7.5){

			loadStylesheet("common","opera_7.5down.css");
		}
		else if (browser.versionMinor<8)
			loadStylesheet("common","opera_8down.css");
	} else if (browser.isFirefox){
		loadStylesheet("common","firefox.css");
	} else if (browser.isKonqueror){
		loadStylesheet("common","konqueror.css");
	} else if (browser.isSafari){
		loadStylesheet("common","safari.css");
	}
	loadStylesheet(tocId,"main.css");

}

function loadStylesheet(aDirectory,aStylesheet){
	document.write("<link href=\""+resourcesPrefix+"/"+aDirectory+"/web/css/"+aStylesheet+"\" rel=\"stylesheet\" type=\"text/css\"/>");
}

function correctPage(){
	hideHiddenElements();

	//this function causes problems in Mac IE.
	if(document.forms[0]  && !(browser.isMac && browser.isIE)){
		correctActionButtons();
	}
	correctPNG();
	correctLinks();
	//repeated to show service calling stations button in IE 5.0
	if(browser.PNGTransSupport==0){
		hideHiddenElements();
	}
    
}

//requests confirmation from the user when certain form buttons are clicked.
//(curenlty just Remove Me button)
function validate(aObject){
	if (confirmSubmit){
		var doAction = true;
		doAction=confirm(confirmationQuery);
		if(doAction)
			return true;
		else
			return false;
	}
	else{
		return true;
	}
}

//called by the input button's onmousedown or onkeydown event if confirmation is required
function setConfirmSubmit(aConfirmationQuery){
	confirmationQuery = aConfirmationQuery;
	confirmSubmit = true;
}

//if Javascript is supported, the href is changed to call a javascript function
//can this be made more generic??
function correctLinks(){
	var elements = document.getElementsByTagName('a');
	for(i=0;i<elements.length;i++){
	//help links
		if(elements[i].id=="termsAndConditions"){
			//elements[i].onEnter = "getHelpBoxLocation(event)";
			//alert(elements[i].onmousedown);
			elements[i].href="javascript:showBox('-termsAndConditions')";
		}
        else if (elements[i].id=="iphone") {
            elements[i].href="javascript:showBox('-iphone')";
        }
        else if(elements[i].className=="help-other"){
			elements[i].href = "javascript:showBox('-other')";
		}
		else if(elements[i].className=="help"){
			elements[i].href = "javascript:showBox('')";
		}
		/*
		//further info links
		else if(elements[i].className=="furtherInfo-link" && browser.ajaxSupport){
			var start = elements[i].id.lastIndexOf('-')+1;
			var end = elements[i].id.length;
			var id = elements[i].id.substring(start,end);
			start = 0;
			end = window.location.pathname.lastIndexOf('/')+1;
			var host = window.location.hostname;
			var path = window.location.pathname.substring(start,end);
			var url = "http://" + host + path + "furtherInfo";
			elements[i].href = "javascript:getFurtherInfo('" + url +"','" + id + "')";
		}
		else if(elements[i].className=="furtherInfo-hideLink" && browser.ajaxSupport){
			var start = elements[i].id.lastIndexOf('-')+1;
			var end = elements[i].id.length;
			var id = elements[i].id.substring(start,end);
			elements[i].href = "javascript:showHideElements('furtherInfo-link-" + id + "','furtherInfo-" + id + "','furtherInfo-hideLink-" + id + "')";
		}
		*/
		// show | hide timetable calling stations links
		else if(elements[i].id=="departuresShowTimetableCallingStationsLink" && browser.ajaxSupport){
			elements[i].href = "javascript:getTimetableCallingStations('timetableCallingStations?type=departures','show','departures')";
		}
		else if(elements[i].id=="arrivalsShowTimetableCallingStationsLink" && browser.ajaxSupport){
			elements[i].href = "javascript:getTimetableCallingStations('timetableCallingStations?type=arrivals','show','arrivals')";
		}
		else if(elements[i].id=="departuresHideTimetableCallingStationsLink" && browser.ajaxSupport){
			elements[i].href = "javascript:showHideTimetableCallingStations('hide','departures')";
		}
		else if(elements[i].id=="arrivalsHideTimetableCallingStationsLink" && browser.ajaxSupport){
			elements[i].href = "javascript:showHideTimetableCallingStations('hide','arrivals')";
		}
	}
}

//elements are expanded by default; if javascript is enabled this method hides them.
function hideHiddenElements(){
	var elements = document.getElementsByTagName('div');
	for (i=0;i<elements.length;i++){
		//divs to hide
		if (elements[i].className == "summary-level1-hidden" && !getHasFurtherInfo(elements[i])){
			elements[i].style.display = "none";
		} else if(elements[i].className == "summary-level2-hidden"){
			elements[i].style.display = "none";
		} else if(elements[i].className == "serviceCallingStations"){
			elements[i].style.display = "none";
		} else if(elements[i].className=="serviceCallingStationsButton"){
			elements[i].style.display = "inline";
		}
	}

	elements=document.getElementsByTagName('select');
	for (i=0;i<elements.length;i++){
		if (elements[i].id.indexOf("updatesType")!=-1){
			//initially show/hide max SMS updates
			var span=document.getElementById("smsUpdatesFields_"+elements[i].id.substring(0,8)+".id");
			if (span!=null){
				switch (elements[i].options[elements[i].selectedIndex].value){
					case "1":
						span.style.display="inline";
						break;
					case "2":
						span.style.display="none";
						break;
					default:
						span.style.display="none";
				}
			}
		}
	}

	//for browsers that are not IE 6 and IE 5.5
	//  using if(browser.isIE55up) does not work{
	if(browser.PNGTransSupport!=1){
		var images = document.getElementsByTagName('img');
		for (i=0;i<images.length;i++){
			if (images[i].className=="serviceCallingStationsButton-image"){
				images[i].style.display="block";
			}
		}
	}

	var link;

	//display the show timetable calling stations buttons
	if(document.getElementById("arrivalsShowTimetableCallingStationsLink")!=null){
		link = document.getElementById("arrivalsShowTimetableCallingStationsLink");
		link.style.display="inline";
	}
	if(document.getElementById("departuresShowTimetableCallingStationsLink")!=null){
		link = document.getElementById("departuresShowTimetableCallingStationsLink");
		link.style.display="inline";
	}

	//display the 'update' message box (used in alerts form pages and toc list page)
	displayUpdateMessageBox();

}

function displayUpdateMessageBox(){
    if(document.getElementById("messageBox-update")!=null){
		var messageBox = document.getElementById("messageBox-update");
		messageBox.style.display="block";
	}
}

function getHasFurtherInfo(aElement){
	var hasFurtherInfo = false;
	var spans = aElement.getElementsByTagName('div');
	for(s=0;s<spans.length;s++){
		if(spans[s].className=="summary-level2"){
			hasFurtherInfo = true;
			break;
		}
	}
	return hasFurtherInfo;
}

function correctActionButtons(){
	/////////////////////////////////////////
	// change <input> tag into <a><span/></a>
	// to display png file in IE
//	for(f=0;f<document.forms.length;f++){
//		var inputs = document.forms[f].getElementsByTagName('input');
//		for(i=0;i<inputs.length;i++){
//			var input = inputs[i];
//			var imgSrc = input.src.toLowerCase();
//			var name = input.name;
//			var start = name.length;
//			var end = name.indexOf("_")+ 1;
//			var action = name.substring(start,end);
//
//			if(imgSrc.substring(imgSrc.length-4, imgSrc.length) == ".png"){
//				var imgId = "id='" + input.id + "' ";// : "";
//				var imgClass = (input.className) ? "class='" + input.className + "' " : "";
//				var imgTitle = (input.title) ? "title='" + input.title + "' " : "title='" + input.alt + "' ";
//
//				//IE 6 and IE 5.5
//				if (browser.PNGTransSupport==1){
//					var display = "display:inline-block;"
//					var imgStyle = display + input.style.cssText;
//					if (input.align == "left") imgStyle = "float:left;" + imgStyle;
//					if (input.align == "right") imgStyle = "float:right;" + imgStyle;
//					var imgHeight = input.height;
//					var imgWidth = input.width;
//					imgStyle = "cursor:hand;" + imgStyle;
//					if (imgSrc.indexOf("__t")!=-1){
//						//add a warning message to show if user clicks remove me button.
//						var actionParam = "";
//						if (input.src.indexOf("btn_remove_me")!=-1){
//							actionParam = ",'Are you sure you want to remove your account and all your alerts for all operators?'";
//						}
//						newHTML="<a " + imgTitle + " href=\"javascript:action('" + action + "'"+actionParam+")\"><span " + imgId + imgClass + imgTitle+
//									" style=\"width:" + imgWidth + "px; height:" + imgHeight + "px;" + imgStyle +
//									"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"+
//									"(src='" + imgSrc + "', sizingMethod='image');\""+
//									" onmouseover=\"iePngSwap('" + input.id + "')\" onmouseout=\"iePngSwap('" + input.id +"')\""+
//									"></span></a>";
//						input.outerHTML = newHTML;
//					}
//					else{
//						newHTML="<a title='" + imgTitle + "' href=\"javascript:action('" + action + "')\"><img src=\""+imgSrc+"\" width=\""+input.width+"\" height=\""+
//							input.height+"\" border=\"0\" "+imgClass+imgTitle+imgId;
//						newHTML+=" onmouseover=\"imageSwap(this)\" onmouseout=\"imageSwap(this)\"";
//						newHTML+="/></a>";
//						input.outerHTML=newHTML;
//						input.display = "block";
//					}
//					i=i-1;
//				}
//				else if(browser.PNGTransSupport==0){
//				//GIF
//					var newImgSrc=input.src.substring(0,imgSrc.length-4)+".gif";
//					newHTML="<a href=\"javascript:action('" + action + "')\" id=\"" + input.id + "\"><img src=\""+newImgSrc+"\" width=\""+input.width+"\" height=\""+
//						input.height+"\" border=\"0\" "+imgClass+imgTitle+" id=\"" + input.id + "-image\"";
//					newHTML+=" onmouseover=\"gifSwap(this)\" onmouseout=\"gifSwap(this)\"";
//					newHTML+="/></a>";
//					input.outerHTML=newHTML;
//					input.display = "block";
//					i=i-1;
//				}
//			}
//		}//end while
//	}//end forms loop
	/////////////////////////////////////////
	// change <img> tag into <span>
	// to display png file in IE
	for(f=0;f<document.forms.length;f++){
		var inputs = document.forms[f].getElementsByTagName('img');
		for(i=0;i<inputs.length;i++){
			var input = inputs[i];
			var imgSrc = input.src.toLowerCase();
			var name = input.name;
			var start = name.length;
			var end = name.indexOf("_")+ 1;
			var action = name.substring(start,end);

			if(imgSrc.substring(imgSrc.length-4, imgSrc.length) == ".png"){
				var imgId = "id='" + input.id + "' ";// : "";
				var imgClass = (input.className) ? "class='" + input.className + "' " : "";
				var imgTitle = (input.title) ? "title='" + input.title + "' " : "title='" + input.alt + "' ";

				//IE 6 and IE 5.5
				if (browser.PNGTransSupport==1 &&
				    (
					    action=="allRoutes" // network over button only
					    || action=="alertMe"
				    )
				){
					var display = "display:inline-block;"
					var imgStyle = display + input.style.cssText;
					if (input.align == "left") imgStyle = "float:left;" + imgStyle;
					if (input.align == "right") imgStyle = "float:right;" + imgStyle;
					var imgHeight = input.height;
					var imgWidth = input.width;
					imgStyle = "cursor:hand;" + imgStyle;
					if (imgSrc.indexOf("__t")!=-1){
						//add a warning message to show if user clicks remove me button.
						var actionParam = "";
						if (input.src.indexOf("btn_remove_me")!=-1){
							actionParam = ",'Are you sure you want to remove your account and all your alerts for all operators?'";
						}
						newHTML="<span " + imgId + imgClass + imgTitle+
									" style=\"width:" + imgWidth + "px; height:" + imgHeight + "px;" + imgStyle +
									";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"+
									"(src='" + imgSrc + "', sizingMethod='image');\""+
									" onmouseover=\"iePngSwap('" + input.id + "')\" onmouseout=\"iePngSwap('" + input.id +"')\""+
									"></span>";
						input.outerHTML = newHTML;
					}
					else{
						newHTML="<a title='" + imgTitle + "' href=\"javascript:action('" + action + "')\"><img src=\""+imgSrc+"\" width=\""+input.width+"\" height=\""+
							input.height+"\" border=\"0\" "+imgClass+imgTitle+imgId;
						newHTML+=" onmouseover=\"imageSwap(this)\" onmouseout=\"imageSwap(this)\"";
						newHTML+="/></a>";
						input.outerHTML=newHTML;
						input.display = "block";
					}
					i=i-1;
				}
				else if(browser.PNGTransSupport==0){
				//GIF
					var newImgSrc=input.src.substring(0,imgSrc.length-4)+".gif";
					newHTML="<a href=\"javascript:action('" + action + "')\" id=\"" + input.id + "\"><img src=\""+newImgSrc+"\" width=\""+input.width+"\" height=\""+
						input.height+"\" border=\"0\" "+imgClass+imgTitle+" id=\"" + input.id + "-image\"";
					newHTML+=" onmouseover=\"gifSwap(this)\" onmouseout=\"gifSwap(this)\"";
					newHTML+="/></a>";
					input.outerHTML=newHTML;
					input.display = "block";
					i=i-1;
				}
			}
		}//end while
	}//end forms loop
}//end function

//modified from http://homepage.ntlworld.com/bobosola/
function correctPNG(){
	for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		var imgSrc = img.src.toLowerCase();
		if (imgSrc.substring(imgSrc.length-4, imgSrc.length) == ".png" && imgSrc.indexOf("__t")!=-1){
			var imgId = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
		    //IE 6 and IE 5.5
			if (browser.PNGTransSupport==1 &&
			    imgId.search("notCorrectPNG")==-1
			){
				//IE transparent PNG
				var display = "display:inline-block;"
				if(img.className=="block") display = "display:block;"
				var imgStyle = display + img.style.cssText;
				if (img.align == "left") imgStyle = "float:left;" + imgStyle;
				if (img.align == "right") imgStyle = "float:right;" + imgStyle;
				var imgHeight = img.height;
				var imgWidth = img.width;
				if(imgSrc.indexOf("btn_hide")!=-1){
				 imgHeight="18";
				 imgWidth="46";
				}
				if(imgSrc.indexOf("btn_hide_calling-stations")!=-1){
				 imgHeight="46";
				 imgWidth="136";
				}
				if(imgSrc.indexOf("btn_calling_at_stations")!=-1){
				 imgHeight="18";
				 imgWidth="121";
				}
				if (img.parentElement.href && imgId.indexOf("_norollover")==-1  && imgSrc.indexOf("nexusalpha")==-1 && imgSrc.indexOf("na")==-1){
					//rollover
					imgStyle = "cursor:hand;" + imgStyle;
					newHTML="<span " + imgId + imgClass + imgTitle+
						" style=\"width:" + imgWidth + "px; height:" + imgHeight + "px;" + imgStyle +
						"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"+
						"(src='" + img.src + "', sizingMethod='image');\""+
						" onmouseover=\"iePngSwap('" + img.id + "')\" onmouseout=\"iePngSwap('" + img.id +"')\""+
						"></span>";
				} else {
					//non-rollover
					if (img.parentElement.href)
						imgStyle = "cursor:hand;" + imgStyle;
					newHTML = "<span " + imgId + imgClass + imgTitle +
						" style=\"width:" + img.width + "px; height:" + img.height + "px;" + imgStyle +
						"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"+
						"(src=\'" + img.src + "\', sizingMethod='image');\"></span>";
				}
				img.outerHTML = newHTML;
				i = i-1;
			} else if (browser.PNGTransSupport==0){
				//GIF
				var newImgSrc=imgSrc.substring(0,imgSrc.length-4)+".gif";
				newHTML="<img src=\""+newImgSrc+"\" width=\""+img.width+"\" height=\""+
					img.height+"\" border=\"0\"" + imgClass + imgTitle + imgId;
				if (img.parentElement.href)
					newHTML+=" onmouseover=\"gifSwap(this)\" onmouseout=\"gifSwap(this)\"";
				newHTML+="/>";
				img.outerHTML=newHTML;
				img.style.display = "block";
			}
		}
	}
}

/* ---------------- OLD methods form JC 1.2 JS File ------------------------------------ */

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments;
  document.MM_sr=new Array;
  for(i=0;i<(a.length-2);i+=3)
  	if ((x=MM_findObj(a[i]))!=null){
  		document.MM_sr[j++]=x;
  		if(!x.oSrc) x.oSrc=x.src;
  		x.src=a[i+2];
  	}
}

/* ------------------------------------------------------------------------------------------ */

//displays the 'update', 'help' and 'help-other' message boxes
function showBox(type){
	//set the image heights and widths for IE 6 and IE 5.5
	if (browser.PNGTransSupport==1){
	// (not working) if (browser.IE55up){
		var elements = document.all;
		for(i=0;i<elements.length;i++){
			if(elements[i].className=="helpBox-tr"){
				elements[i].style.height = "33";
				elements[i].style.width = "26";
			}
			else if(elements[i].className=="helpBox-tl"){
				elements[i].style.height = "33";
				elements[i].style.width = "20";
			}
			else if(elements[i].className=="helpBox-br"){
				elements[i].style.height = "20";
				elements[i].style.width = "26";
			}
			else if(elements[i].className=="helpBox-bl"){
				elements[i].style.height = "20";
				elements[i].style.width = "20";
			}
			else if(elements[i].className=="helpBox-closeButton"){
				elements[i].style.height = "20";
				elements[i].style.width = "87";
			}
		}
	}
	var box = document.getElementById("helpBox"+type);
	if(box==null)
		box = document.getElementById(type);

    if (type=="-iphone"){
		var coordinates = getAnchorPosition('iphone');
		//var x = link.style.left + 300;
		//var y = link.style.top;
		box.style.left = coordinates.x + 0 + "px";
		box.style.top = coordinates.y - 100 + "px";
	}

    if (type=="-termsAndConditions"){
		var coordinates = getAnchorPosition('termsAndConditions');
		//var x = link.style.left + 300;
		//var y = link.style.top;
		box.style.left = coordinates.x + 200 + "px";
		box.style.top = coordinates.y - 380 + "px";
	}
	//window.location="#helpBox";
	box.style.display = "block";

	var images = document.getElementsByTagName("img");
	for(i=0;i<images.length;i++){
		if(images[i].className=="helpBox-tr"){
//			images[i].height="33";
//			images[i].width="26";
		}
		else if(images[i].className=="helpBox-tl"){
			images[i].height="33";
			images[i].width="20";
		}
		else if(images[i].className=="helpBox-br"){
			images[i].height="20";
			images[i].width="26";
		}
		else if(images[i].className=="helpBox-bl"){
			images[i].height="20";
			images[i].width="20";
		}
		else if(images[i].className=="helpBox-closeButton"){
			images[i].height="20";
			images[i].width="87";
		}
	}

}

//redundant method.
function help(aId){
	var width=250;
	var height=350;
	if (arguments.length==3){
		width=arguments[1];
		height=arguments[2];
	}
	var aWindow=window.open(urlPrefix+"help/"+aId,"","width="+width+",height="+height+",scrollbars=yes");
	aWindow.moveTo(parseInt((screen.width-width)/2),parseInt((screen.height-height)/2));
	//window.location = urlPrefix+"help/"+aId;
}

function action(aAction){
	var doAction=true;
	//brings up a warning dialog if there is a second argument - which is the warning message
	//used in the "remove me" action on the myDetails page
	if (arguments.length>1)
		doAction=confirm(arguments[1]);

	if (doAction){
		//a second form exists for the engineering works period
		if(aAction.indexOf("changeEngWorksPeriod")!=-1){
		document.formEngWorks.action.value=aAction;
        pageTracker._trackEvent('Scope Change Event', aAction);
        document.formEngWorks.submit();
		}
		else{
		//cforms
		var widget = document.form[aAction];
		if (widget != null) {widget.value=aAction;}
		//journeycheck
		document.form.action.value=aAction;

        pageTracker._trackEvent('Route Event', aAction);

        document.form.submit();
		}
	}
}

function hide(aId){
	var element=document.getElementById(aId);
	element.style.display="none";
	if (arguments.length>1){
		//if the user is using a screen reader, this will take them to the start of the message box
		//window.location=arguments[1];
	}
}

var lockedImages=new Array();

function showAltText(aId,aX,aY){
  if (!lockedImages[aId]){
		var elem=document.getElementById(aId+"-altText");
		elem.style.left=aX;
		elem.style.top=aY;
		elem.style.visibility="visible";
	}
}

function hideAltText(aId){
	var elem=document.getElementById(aId+"-altText");
	elem.style.visibility="hidden";
}

function iePngSwap(aImageId, aShowHideAltText) {
	if (aImageId != "") {
		var oSpan = document.getElementById(aImageId);
		var currentSrc = oSpan.style.filter;
		if (currentSrc.indexOf("_f2") != -1) {
			oSpan.style.filter = currentSrc.replace("_f2", "");
			if (aShowHideAltText) {
				hideAltText(aImageId);
				lockedImages[aImageId] = true;
			}
		}
		else {
			oSpan.style.filter = currentSrc.replace(".png", "_f2.png");
			if (aShowHideAltText) {
				lockedImages[aImageId] = false;
				window.setTimeout('showAltText("' + aImageId + '",' + (getIeMouseX()) + ',' + (getIeMouseY() + 15) + ')', 800);
			}
		}
	}
}

function getIeMouseX(){
	return window.event.clientX +
	(document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
}

function getIeMouseY(){
	return window.event.clientY +
	(document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
}

function gifSwap(aImage){
   var imageSrc = aImage.src;
   if (imageSrc.indexOf("_f2") != -1)
      aImage.src = imageSrc.replace("_f2","");
   else
      aImage.src = imageSrc.replace(".gif","_f2.gif");
}

function imageSwap(aImage){
   var imageSrc = aImage.src;
   if (imageSrc.indexOf("_f2") != -1){
      aImage.src = imageSrc.replace("_f2","");
   }
   else{
      aImage.src = imageSrc.replace(".png","_f2.png");
   }
}

function displayImage(aImage,aWidth,aHeight){
	var html="<div style=\"width:"+aWidth+"px;height:"+aHeight+"px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+aImage+".png', sizingMethod='scale')\"></div>";
	document.write(html);
}

function expandCollapseIncident(aId, aOpenAtDefault){
	elem=document.getElementById(aId);
	image=document.getElementById("image_"+aId);
	link=document.getElementById("link_"+aId);
	if (elem.style.display=="none"){
	    elem.style.display="block";
		image.src=image.src.replace("plus","minus");
		link.title = link.title.replace("show","hide");
	} else if (elem.style.display=="") {
		if (aOpenAtDefault){
			elem.style.display="none";
			image.src=image.src.replace("minus","plus");
			link.title = link.title.replace("hide","show");
		} else {
			elem.style.display="block";
			image.src=image.src.replace("plus","minus");
			link.title = link.title.replace("hide","show");
		}
	} else {
		elem.style.display="none";
		image.src=image.src.replace("minus","plus");
		link.title = link.title.replace("hide","show");
	}
}

function showHideTimetableCallingStations(aAction, aType){
	var divs = document.getElementsByTagName('div');
	var displayMode;

	//in some browsers the button inside the link needs to be shown | hidden as well
	var buttonToHide;
	var buttonToShow;
	var linkToHide;
	var linkToShow;
	var width = "121";
	var height = "18";
	if (aAction=="hide"){
		displayMode="none";
		buttonToHide = aType + "HideTimetableCallingStationsButton";
		linkToHide = aType + "HideTimetableCallingStationsLink";
		buttonToShow = aType + "ShowTimetableCallingStationsButton";
		linkToShow = aType + "ShowTimetableCallingStationsLink";
	}
	else if (aAction=="show"){
	    width = 130;
		displayMode="block";
		buttonToShow = aType + "HideTimetableCallingStationsButton";
		linkToShow = aType + "HideTimetableCallingStationsLink";
		buttonToHide = aType + "ShowTimetableCallingStationsButton";
		linkToHide = aType + "ShowTimetableCallingStationsLink";
	}

	// show|hide the calling stations table
	for(i=0;i<divs.length;i++){
		if(divs[i].className=="tCallingStations-"+aType){
			divs[i].style.display=displayMode;
		}
	}

	// hide the required button for older versions of IE and Mozilla
	if((browser.isMozilla && browser.versionMinor <=1) || (browser.isIE && browser.versionMinor < 5.5)){
		document.getElementById(buttonToHide).style.display="none";
	}
	document.getElementById(linkToHide).style.display="none";

	//tests to see if the image is in a mouseover state and removes
	//the _f2 from the image's source (required for mozilla 1.6).
	if(!browser.isIE){
		if(document.getElementById(buttonToShow).src.indexOf("_f2")!=-1){
			var imgSrc = document.getElementById(buttonToShow).src;
			document.getElementById(buttonToShow).src = imgSrc.replace("_f2","");
		}
	}

	// show the required button for older versions of IE and Mozilla
	if((browser.isMozilla && browser.versionMinor <=1) || (browser.isIE && browser.versionMinor < 5.5)){
		document.getElementById(buttonToShow).style.display="inline";
		document.getElementById(buttonToShow).width = width;
		document.getElementById(buttonToShow).height = height;
	}
	document.getElementById(linkToShow).style.display="inline";

	//set the anchor position to the start of the timetable box
	var coordinates = getAnchorPosition(aType);
	document.documentElement.scrollTop = coordinates.y;
	document.body.scrollTop = coordinates.y;
}

/*
function showHideCallingStations(aShowId,aHideId,aHideBtn){
	document.getElementsById(aShowId).style.display="block";
	document.getElementById(aHideBtn).style.display="none";
	//document.getElementById(aHideId).style.display="none";
	var divs = document.getElementsByTagName('div');
	for(i=0;i<divs.length;i++){
		if(divs[i].id.indexOf(aHideId)!=-1){
			divs[i].style.display="none";
		}
	}
}
*/

function showHideServiceCallingStations(aAction,aId){
	if(aAction=="show"){
		//hide show calling stations button
		document.getElementById("serviceCallingStationsLink-" + aId).style.display = "none";

		//show calling stations table for the service id
		document.getElementById("serviceCallingStations-" + aId).style.display = "block";
		var buttonImg;
		if(!browser.isIE){
			// get the hide calling stations button for the service id
			buttonImg = document.getElementById("hideServiceCallingStationsButton-"+aId);
			//check to see if the image button is in a mouseover state and remove the _f2 from source
			if(buttonImg.src.indexOf("_f2")!=-1){
				var imgSrc = buttonImg.src;
				buttonImg.src = imgSrc.replace("_f2","");
			}
		}
		else{
			var images = document.getElementsByTagName("img");
			for(i=0;i<images.length;i++){
				if(images[i].id=="hideServiceCallingStationsButton-"+aId){
					buttonImg = images[i];
				}
			}
		}
		//for some browsers the height and width of the hidden image must be reset
		if(browser.PNGTransSupport!=1){
			buttonImg.height = "18";
			buttonImg.width = "46";
			buttonImg.style.display = "block";
		}
	}
	// hide calling stations
	else{
		if(!browser.isIE){
			//get the show calling stations button for the service id
			var buttonImg = document.getElementById("serviceCallingStationsButton-"+aId);
			//check to see if the image button is in a mouseover state and remove the _f2 from source
			if(buttonImg.src.indexOf("_f2")!=-1){
				var imgSrc = buttonImg.src;
				buttonImg.src = imgSrc.replace("_f2","");
			}
		}
		//display the show calling stations button
		document.getElementById("serviceCallingStationsLink-" + aId).style.display = "block";
		//hide the calling stations table
		document.getElementById("serviceCallingStations-" + aId).style.display = "none";
	}
}

function showHideElements(aShowId,aHideId, aHideLinkId){
    pause(1000);
	document.getElementById(aShowId).style.display="block";
	document.getElementById(aHideId).style.display="none";
	if (arguments.length>2)
		document.getElementById(aHideLinkId).style.display="none";
}

function showHideSMSUpdatesFields(aSelectObject,aSpanId){
	var span=document.getElementById(aSpanId);
	if (span!=null){
		switch (aSelectObject.options[aSelectObject.selectedIndex].value){
			case "1":
				span.style.display="inline";
				break;
			case "2":
				span.style.display="none";
				break;
			default:
				span.style.display="none";
		}
	}
}

//was used by the change engineering works box.
function jumpMenu(aSelectObject,aAction,aParameterName){
	var urlSuffix="";
	if (arguments.length>3)
		urlSuffix=arguments[3];
	window.location="route?action="+aAction+"&"+aParameterName+"="+aSelectObject.options[aSelectObject.selectedIndex].value+urlSuffix;
}

/* ---------------------------- drag message box -------------------------------------------------- */

// allows the help and update boxes to be moved around the screen

/**
 * PortableDrag.js:
 * beginDrag(  ) is designed to be called from an onmousedown event handler.
 * elementToDrag may be the element that received the mousedown event, or it
 * may be some containing element. event must be the Event object for the
 * mousedown event. This implementation works with both the DOM Level 2
 * event model and the IE event model.
 **/
function beginDrag(elementToDrag, event, limitingHeightDiv)
    {
    // Compute the distance between the upper-left corner of the element
    // and the mouse-click. The moveHandler function below needs these values.
    var deltaX = event.clientX - parseInt(elementToDrag.style.left);
    var deltaY = event.clientY - parseInt(elementToDrag.style.top);
    // Register the event handlers that will respond to the mousemove events
    // and the mouseup event that follow this mousedown event.
    if (document.addEventListener)
        {  // DOM Level 2 Event Model
        // Register capturing event handlers
        document.addEventListener("mousemove", moveHandler, true);
        document.addEventListener("mouseup", upHandler, true);
        }
    else if (document.attachEvent)
        {  // IE 5+ Event Model
        // In the IE event model, we capture events by calling
        // setCapture() on the element to capture them.

        elementToDrag.attachEvent("onmousemove", moveHandler);
        elementToDrag.attachEvent("onmouseup", upHandler);
        elementToDrag.setCapture();
        }
    else
        {  // IE 4 Event Model
        // In IE 4 we can't use attachEvent(  ), so assign the event handlers
        // directly after storing any previously assigned handlers, so they
        // can be restored. Note that this also relies on event bubbling.
        var oldmovehandler = document.onmousemove;
        var olduphandler = document.onmouseup;
        document.onmousemove = moveHandler;
        document.onmouseup = upHandler;
        }

    // We've handled this event. Don't let anybody else see it.
    if (event.stopPropagation) event.stopPropagation();  // DOM Level 2
    else event.cancelBubble = true;
    // IE

    // Now prevent any default action.
    if (event.preventDefault) event.preventDefault();   // DOM Level 2
    else event.returnValue = false;
    // IE

/**
 * This is the handler that captures mousemove events when an element
 * is being dragged. It is responsible for moving the element.
 **/
function moveHandler(e)
    {
    if (!e) e = window.event;  // IE Event Model
    // Move the element to the current mouse position, adjusted as
    // necessary by the offset of the initial mouse-click.

    var posX = e.clientX - deltaX;
    var posY = e.clientY - deltaY;

    if(posX < 0)
        posX = 0;

    if(posY < 0)
        posY = 0;


    if(posX + 300 > document.body.clientWidth)
        posX = document.body.clientWidth - 320;

    if(posY > document.getElementById("form").scrollHeight)
        posY = document.getElementById(limitingHeightDiv).scrollHeight

    elementToDrag.style.left = posX + "px";
    elementToDrag.style.top = posY + "px";
    // And don't let anyone else see this event.
    if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
    else e.cancelBubble = true;
    // IE
    }

/**
 * This is the handler that captures the final mouseup event that
 * occurs at the end of a drag.
 **/
function upHandler(e)
    {
    if (!e) e = window.event;  // IE Event Model

    // Unregister the capturing event handlers.
    if (document.removeEventListener)
        {  // DOM Event Model
        document.removeEventListener("mouseup", upHandler, true);
        document.removeEventListener("mousemove", moveHandler, true);
        }
    else if (document.detachEvent)
        {  // IE 5+ Event Model
        elementToDrag.detachEvent("onmouseup", upHandler);
        elementToDrag.detachEvent("onmousemove", moveHandler);
        elementToDrag.releaseCapture();
        }
    else
        {  // IE 4 Event Model
        document.onmouseup = olduphandler;
        document.onmousemove = oldmovehandler;
        }

    // And don't let the event propagate any further.
    if (e.stopPropagation) e.stopPropagation();  // DOM Level 2
    else e.cancelBubble = true;
    // IE
    }
}



/* ---------------------------- anchor position code ----------------------------------------------*/

//used to reposition the page when the user hides timetable calling stations
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) {use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	//document.documentElement.scrollTop = coordinates.y;
	//document.body.scrollTop = coordinates.y;
	return coordinates;
	}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}

/* ------------------------------------- Ajax Methods ---------------------------------------------------------- */

//shows and hides further info for engineering works incidents on the summary page
function getFurtherInfo(aUrl, aIncidentId){
	//First test to see if further info has already been added.
	//If it has just re-display the elements.
	if(document.getElementById('further-info-span-'+aIncidentId)){
		document.getElementById('furtherInfo-' + aIncidentId).style.display="block";
		document.getElementById('furtherInfo-link-'+ aIncidentId).style.display = "none";
		document.getElementById('furtherInfo-hideLink-'+ aIncidentId).style.display = "block";
	}
	else{
		var url = aUrl;
		var pars = 'incidentId=' + aIncidentId;
		url += '?' + pars;

		//call method in dojo.js library
		dojo.io.bind(
			{
			//url of xml data to obtain
			url: url,

			//function to be called afterwards to display data
			load: function(type,xmlHttpRequest, evt)    {

				//retrieve data
				var furtherInfo = xmlHttpRequest.documentElement.childNodes[0].data;

				//create new span to add data to
				var furtherInfoSpan = document.createElement('span');
			    furtherInfoSpan.id = 'further-info-span-' + aIncidentId;

			    //add data to span
			    furtherInfoSpan.appendChild(document.createTextNode(furtherInfo));

			    //retrieve div for incident in html and append data span
			    var div = document.getElementById('furtherInfo-' + aIncidentId);
			    div.appendChild(furtherInfoSpan);

			    //hide show link and display data
			    document.getElementById('furtherInfo-link-'+aIncidentId).style.display = "none";
			    div.style.display = "block";
			    document.getElementById('furtherInfo-hideLink-'+aIncidentId).style.display = "block";
			},
			mimetype: "text/xml"  });//end dojo.do.bind
	}//end if
}

function pause(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

function showLoadingImg(aType){
    var coordinates = getAnchorPosition(aType + "ShowTimetableCallingStationsLink");
    var offsetY, offsetX;
    if (browser.isIE){
        offsetY = -13;
        offsetX = -27;
    }
    else if(browser.isMozilla || browser.isFirefox){
        offsetY = -10;
        offsetX = -19;
    }
    else if(browser.isOpera){
        offsetY = -8;
        offsetX = -19;
    }
    else if(browser.isSafari){
        offsetY = -17;
        offsetX = -25;
    }
    else{
        offsetY = -13;
        offsetX = -21;
    }
    var posY = coordinates.y + offsetY;
    var posX = coordinates.x + offsetX;
	var loadingImg = document.getElementById('loadingImg');
	loadingImg.style.left = posX + "px";
	loadingImg.style.top = posY + "px";
	loadingImg.style.zIndex = 10;
	loadingImg.style.display = "block";
}

//variables to state whether timetable calling stations have previously been obtained
var depTimetableCallingStationsExist = false;
var arrTimetableCallingStationsExist = false;

function getTimetableCallingStations(aUrlSuffix, aAction, aType){
	//first check if calling stations have previously been shown
	if((aType=='departures' && depTimetableCallingStationsExist) || (aType=='arrivals' && arrTimetableCallingStationsExist)){
		showHideTimetableCallingStations(aAction,aType);
	}
	//Ajax section
	else{
	    showLoadingImg(aType);
		var start = 0;
		var end = window.location.pathname.lastIndexOf('/')+1;
		var host = window.location.hostname;
		var path = window.location.pathname.substring(start,end);
		var url = "http://" + host + path + aUrlSuffix;

		//call method in dojo.js library
		dojo.io.bind(
			{
			//url of xml data to obtain
			url: url,

			//function to be called afterwards to display data
			load: function(type,data, evt)  {

				//create an arrary of services
				var services = data.documentElement.getElementsByTagName('service');
				//alert(services.length);
				if(services.length>0){

    				for(i=0;i<services.length;i++){

    					//get service's calling stations div - identified by board type
    					//service's index
    					var index = i + 1;
    					var serviceDivId = aType + "-" + index;
    					var div = document.getElementById(serviceDivId);

    					//handle any errors getting calling stations
    					if(services[i].getElementsByTagName('error').length>0){
                            var errorSpan = document.createElement('span');
                            var errorMsg = services[i].getElementsByTagName('error')[0].attributes.getNamedItem('message').value;
                            errorSpan.appendChild(document.createTextNode(errorMsg));
                            errorSpan.style.fontStyle = "italic";
                            div.appendChild(errorSpan);
    					}
    					else if(services[i].getElementsByTagName('route').length>0){
            				var routes = services[i].getElementsByTagName('route');

            				for(r=0;r<routes.length;r++){

            					//span to hold calling stations
            					var callingStationsSpan = document.createElement('span');
            					callingStationsSpan.appendChild(document.createElement('p'));

            					//test for split calling stations
            					if(routes[r].attributes.getNamedItem('split-type')){
            						if(routes[r].attributes.getNamedItem('split-type').value=="divide"){
            							var msg = "* Divides at: ";
            							msg += routes[r].attributes.getNamedItem('split-station-name').value;
            							msg += ", a seperate portion goes to: ";
            							callingStationsSpan.appendChild(document.createTextNode(msg));
            						}
            					}

            					//calling stations array
            					var callingStations = routes[r].getElementsByTagName('calling-station');

            					for(c=0;c<callingStations.length;c++){

            						//station name attribute
            						var cStationName = callingStations[c].attributes.getNamedItem('name').value;
            						//scheduled time attribute
            						var cTime = callingStations[c].attributes.getNamedItem('time').value;
            						if(cTime!='')
            							var cStation = cStationName + " (" + cTime + ")";
            						if(callingStations[c].attributes.getNamedItem('split').value=='true')
            							cStation += " *";
            						var cStationSpan = document.createElement('span');

            						//check if it should be bold (From or To station)
            						if(callingStations[c].attributes.getNamedItem('highlighted').value=='true'){
            							cStationSpan.style.fontWeight = 'bold';
            							cStationSpan.appendChild(document.createTextNode(cStation));
            						}
            						//check for station where route splits
            						else if(callingStations[c].attributes.getNamedItem('split').value=='true'){
            							cStationSpan.style.fontStyle = 'italic';
            							cStationSpan.appendChild(document.createTextNode(cStation));
            						}
            						else{
            							cStationSpan.appendChild(document.createTextNode(cStation));
            						}
            						callingStationsSpan.appendChild(cStationSpan);

            						//add comma after all except the last one
            						if(c!=callingStations.length-1)
            							callingStationsSpan.appendChild(document.createTextNode(", "));
            					}
            					callingStationsSpan.appendChild(document.createElement('p'));
            					div.appendChild(callingStationsSpan);
        					}//end routes loop
        				}//end if
    				}// end services for loop
				}
				else{
				    var divs = getElementsByClassName('div','tCallingStations-' + aType);
				    for (i=0;i<divs.length;i++){
				        var errorSpan = document.createElement('span');
				        errorSpan.appendChild(document.createTextNode("Could not get calling stations for this service."));
				        errorSpan.style.fontStyle = "italic";
				        divs[i].appendChild(errorSpan);
				    }
				}
				//hide loading img
				document.getElementById('loadingImg').style.display = "none";
				showHideTimetableCallingStations(aAction, aType);
			},    //end of onLoad function
		mimetype: "text/xml"  });//end dojo.do.bind

		//set variable to true to prevent another ajax call
		if(aType=='departures')
			depTimetableCallingStationsExist = true;
		else if (aType=='arrivals')
			arrTimetableCallingStationsExist = true;
	}

}

function getElementsByClassName(aElementType, aClassName){
    var elementsByTagName = document.getElementsByTagName(aElementType);
    var elementsByClassName = [];
    for(i=0;i<elementsByTagName.length;i++){
        if(elementsByTagName[i].className==aClassName){
            elementsByClassName.push(elementsByTagName[i]);
        }
    }
    return elementsByClassName;
}

/**************************************************************/

function showElement(aName)
	{
	if(aName!=null)
		{
		var elements = document.getElementsByName(aName);
		document.getElementById(aName).style.display="inline";
		for(i=0;i<elements.length;i++)
			{
			elements.item(i).style.display="inline";
			}
		}
	}

function hideElement(aName)
	{
	if(aName!=null)
		{
		elements = document.getElementsByName(aName);
		document.getElementById(aName).style.display="none";
		for(i=0;i<elements.length;i++)
			{
			elements.item(i).style.display="none";
			}
		}
	}

function showFurtherInfo(aElement, aShowFurtherInfoLink, aHideFurtherInfoLink)
	{
	showElement(aElement);
	hideElement(aShowFurtherInfoLink);
	showElement(aHideFurtherInfoLink);
	}

function hideFurtherInfo(aElement, aShowFurtherInfoLink, aHideFurtherInfoLink)
	{
	hideElement(aElement);
	showElement(aShowFurtherInfoLink);
	hideElement(aHideFurtherInfoLink);
	}

function showDivHideButton(aDiv, aShowButton, aHideButton)
	{
	showElement(aDiv);
	hideElement(aShowButton);
	showElement(aHideButton);
	}

function hideDivShowButton(aDiv, aShowButton, aHideButton)
	{
	hideElement(aDiv);
	showElement(aShowButton);
	hideElement(aHideButton);
	}

function showHideDivAndButton(aDivToBeShown, aDivToBeHided, aButtonToBeShown, aButtonToBeHided)
	{
	showElement(aDivToBeShown);
	hideElement(aDivToBeHided);
	showElement(aButtonToBeShown);
	hideElement(aButtonToBeHided);
	}

function formSubmit(aForm, aAction, aElement, aValue)
	{
	var form = getFormObject(aForm);
	if(form!=null)
		{
		var element = getElement(form, aElement);
		if(element!=null)
			element.value = aValue;
		form.action.value=aAction;
//		debugForm(form);
		form.submit();
		}
	}

function debugForms()
	{
	var forms = document.forms;
	for(i=0;i<forms.length;i++)
		debugForm(forms[i]);
	}

function debugForm(aForm)
	{
	var s = "";
	var elements = aForm.elements
	s += "form.name = " + aForm.name + "\nform.action = " + aForm.action.value;
	for(j=0;j<elements.length;j++)
		s += "\n\telements["+j+"].name = " + elements[j].name + ", value = " + elements[j].value;
	alert(s);
	}

function getFormObject(aFormName)
	{
	var forms = document.forms;
	for(i=0;i<forms.length;i++)
		if(forms[i].name == aFormName)
			return forms[i];
	return null;
	}

function getElement(aForm, aElementName)
	{
	var elements = aForm.elements
	for(i=0;i<elements.length;i++)
		if(elements[i].name == aElementName)
			return elements[i];
	return null;
	}
