﻿function fnGetAJAXHttpRequest()
{var httpRequest;if(window.XMLHttpRequest)
{httpRequest=new XMLHttpRequest();}
else if(window.ActiveXObject)
{try
{httpRequest=new ActiveXObject("Msxml2.XMLHTTP");}
catch(e)
{try
{httpRequest=new ActiveXObject("Microsoft.XMLHTTP");}
catch(e)
{}}}
return httpRequest;}
function fnToggleBusServiceDetails(intBusServiceID,strBusServiceNumber)
{var strBusServiceDetailsImgId="imgBusServiceID-"+intBusServiceID;var strBusServiceDetailsLnkId="lnkBusServiceID-"+intBusServiceID;var strBusServiceDetailsDivId="divBusServiceID-"+intBusServiceID;var strTimetableCountSpnId="spnTimetableCount-"+intBusServiceID;var img=d.getElementById(strBusServiceDetailsImgId);var lnk=d.getElementById(strBusServiceDetailsLnkId);var div=d.getElementById(strBusServiceDetailsDivId);var spnTimetableCount=d.getElementById(strTimetableCountSpnId);if(div.style.display==''||div.style.display=='none')
{if(spnTimetableCount.innerHTML=='')
{fnGetBusServiceDocuments(intBusServiceID);fnGetBusServiceOperators(intBusServiceID);}
img.src=strBaseUrl+"Plugins/CHYM/Images/collapse.gif";img.alt="-";img.title="Show less information for bus service "+strBusServiceNumber;lnk.innerHTML="Show less information for bus service "+strBusServiceNumber;div.style.display='block';}
else
{img.src=strBaseUrl+"Plugins/CHYM/Images/expand.gif";img.alt="+";img.title="Show more information for bus service "+strBusServiceNumber;lnk.innerHTML="Show more information for bus service "+strBusServiceNumber;div.style.display='none';}
return false;}
function fnGetTowns(strDistrictCode,strTownDropdown)
{var httpRequest=fnGetAJAXHttpRequest();if(!httpRequest)
{alert('Sorry, there was a problem automatically retrieving the towns [Error Code 1]');return false;}
httpRequest.onreadystatechange=function()
{fnDisplayTowns(httpRequest,strTownDropdown);};httpRequest.open('GET',strBaseUrl+'Plugins/CHYM/AJAX/GetTownsByDistrictCode.aspx?DistrictCode='+strDistrictCode,true);httpRequest.send('');}
function fnDisplayTowns(httpRequest,strTownDropdown)
{if(httpRequest.readyState==4)
{if(httpRequest.status==200)
{var ddlTowns=d.getElementById(strTownDropdown);var arrTowns=httpRequest.responseText.split('*');ddlTowns.options.length=0;ddlTowns.options[ddlTowns.length]=new Option('Please Select','')
for(var x=0;x<=arrTowns.length;x++)
{if(arrTowns[x])
{var arrTownDetails=arrTowns[x].split('|');ddlTowns.options[ddlTowns.length]=new Option(arrTownDetails[2],arrTownDetails[1])}}}
else
{alert('Sorry, there was a problem autmoatically updating the list of towns [Error Code 2].');}}}
function fnGetBusServiceDocuments(intBusServiceID)
{var httpRequest=fnGetAJAXHttpRequest();if(!httpRequest)
{alert('Sorry, there was a problem retrieving the bus service timetables [Error Code 1]');return false;}
httpRequest.onreadystatechange=function()
{fnDisplayBusServiceDocuments(httpRequest,intBusServiceID);};httpRequest.open('GET',strBaseUrl+'Plugins/CHYM/AJAX/GetBusServiceTimetablesByBusServiceID.aspx?ID='+intBusServiceID,true);httpRequest.send('');}
function fnDisplayBusServiceDocuments(httpRequest,intBusServiceID)
{if(httpRequest.readyState==4)
{if(httpRequest.status==200)
{var strTimetableCountSpnId='spnTimetableCount-'+intBusServiceID;var strCurrentTimetableSpnId="spnCurrentTimetable-"+intBusServiceID;var strNextTimetableSpnId="spnNextTimetable-"+intBusServiceID;var strFloodingTimetableSpnId="spnFloodingTimetable-"+intBusServiceID;var d=document;var spnTimetableCount=d.getElementById(strTimetableCountSpnId);var spnCurrentTimetable=d.getElementById(strCurrentTimetableSpnId);var spnNextTimetable=d.getElementById(strNextTimetableSpnId);var spnFloodingTimetable=d.getElementById(strFloodingTimetableSpnId);var arrDocuments=httpRequest.responseText.split('*');blnCurrentTimetableFound=false;blnNextTimetableFound=false;blnFloodingTimetableFound=false;spnTimetableCount.innerHTML=arrDocuments.length-2;for(var x=0;x<=arrDocuments.length;x++)
{if(arrDocuments[x])
{var arrTimetableDetails=arrDocuments[x].split('|');if(arrTimetableDetails[4]!='True')
{if(!blnCurrentTimetableFound)
{spnCurrentTimetable.innerHTML=fnGetTimetableHtml('Current',arrTimetableDetails)
blnCurrentTimetableFound=true;}
else if(!blnNextTimetableFound)
{spnNextTimetable.innerHTML=fnGetTimetableHtml('Next',arrTimetableDetails)
blnNextTimetableFound=true;}}
else
{if(!blnFloodingTimetableFound)
{spnFloodingTimetable.innerHTML=fnGetTimetableHtml('Flooding',arrTimetableDetails)
blnFloodingTimetableFound=true;}}}}}
else
{alert('Sorry, there was a problem retrieving the bus service timetables [Error Code 2].');}}}
function fnGetTimetableHtml(strTitle,arrTimetableDetails)
{var strHtml='';strHtml+='<strong>'+strTitle+' Timetable</strong> <a href="http://www.worcestershire.gov.uk/chym/FileHandlers/BusServiceTimetable.aspx?ID='+arrTimetableDetails[0]+'" title="View '+arrTimetableDetails[1]+'" target="_blank">'+arrTimetableDetails[1]+'</a>';if(strTitle=='Next')
{strHtml+=' (effective from '+arrTimetableDetails[2]+')';}
if(strTitle=='Flooding')
{strHtml+='<br />\r';strHtml+='<br />\r';}
return strHtml;}
function fnGetBusServiceOperators(intBusServiceID)
{var httpRequest=fnGetAJAXHttpRequest();if(!httpRequest)
{alert('Sorry, there was a problem retrieving the bus service operators [Error Code 1]');return false;}
httpRequest.onreadystatechange=function()
{fnDisplayBusServiceOperators(httpRequest,intBusServiceID);};httpRequest.open('GET',strBaseUrl+'Plugins/CHYM/AJAX/GetBusServiceOperatorsByBusServiceID.aspx?ID='+intBusServiceID,true);httpRequest.send('');}
function fnDisplayBusServiceOperators(httpRequest,intBusServiceID)
{if(httpRequest.readyState==4)
{if(httpRequest.status==200)
{var arrBusOperators=httpRequest.responseText.split('*');var strHtml='<strong>Bus Operator(s)</strong> ';if(arrBusOperators[1])
{for(x in arrBusOperators)
{var arrBusOperator=arrBusOperators[x].split('|');if(arrBusOperator[1])
{strHtml+="<a href=\"bus-operators.aspx#BO"+arrBusOperator[0]+"\" title=\""+arrBusOperator[1]+" details\">"+arrBusOperator[1]+"</a>, ";}}
strHtml=strHtml.substring(0,strHtml.lastIndexOf(','));}
else
{strHtml+='N/A';}
var d=document;var strBusOperatorsSpnId="spnBusOperators-"+intBusServiceID;var spnBusOperators=d.getElementById(strBusOperatorsSpnId);spnBusOperators.innerHTML=strHtml;}
else
{alert('Sorry, there was a problem retrieving the bus service operators [Error Code 2].');}}}
function fnSwitchArea(strDistrictDropdown,strTownDropdown)
{var ddlDistrict=d.getElementById(strDistrictDropdown);var ddlTown=d.getElementById(strTownDropdown);var strArea='';var strCurrentUrl=location.href;var strRedirectUtl='';if(strCurrentUrl.toLowerCase().indexOf('busserviceoperatorid=')>-1)
{strCurrentUrl=strCurrentUrl.substring(0,strCurrentUrl.indexOf('?'));}
if(strCurrentUrl.toLowerCase().indexOf('?area=')>-1)
{strCurrentUrl=strCurrentUrl.substring(0,strCurrentUrl.toLowerCase().indexOf('?area='));}
if(strCurrentUrl.toLowerCase().indexOf('&area=')>-1)
{strCurrentUrl=strCurrentUrl.substring(0,strCurrentUrl.toLowerCase().indexOf('&Area='));}
if(ddlTown.options[ddlTown.selectedIndex].value!='')
{strArea=ddlTown.options[ddlTown.selectedIndex].value;}
else
{strArea=ddlDistrict.options[ddlDistrict.selectedIndex].value;;}
if(strArea.length>0)
{if(strCurrentUrl.indexOf('?')>-1)
{strRedirectUrl=strCurrentUrl+'&Area='+strArea;}
else
{strRedirectUrl=strCurrentUrl+'?Area='+strArea;}}
else
{strRedirectUrl=strCurrentUrl;}
location.href=strRedirectUrl;}