/* Definitions */
//NS4 = (document.layer) ? true : false;
var initFromStreet = "Enter Street";
var initFromSuburb = "Enter Suburb";
var initFromLocation = "Enter Location or Landmark";
var initFromStop = "Enter Stop Number";

var initToStreet = "Enter Street";
var initToSuburb = "Enter Suburb";
var initToLocation = "Enter Location or Landmark";
var initToStop = "Enter Stop Number";
/* Functions to Handle Form Display */
//Set Initial Form View

function JP_setInitialFormView()
{		
	var fType="Street";
	var tType="Street";
	
	if (GetCookieValueFromCookieString(readCookie('JourneyPlanner'),0)!=null )
	{		
		fType=GetCookieValueFromCookieString(readCookie('JourneyPlanner'),0);
	}
	if (GetCookieValueFromCookieString(readCookie('JourneyPlanner'),5)!=null )
	{
		tType=GetCookieValueFromCookieString(readCookie('JourneyPlanner'),5);
	}
	
	try
	{
		//Show or hide From input boxes
		switch (fType)
		{
			case "Street":
				document.getElementById("fromStreetTable").style.display = "block";
				document.getElementById("fromLocationTable").style.display = "none";
				document.getElementById("fromStopTable").style.display = "none";
				break;
			case "Location":
				document.getElementById("fromStreetTable").style.display = "none";
				document.getElementById("fromLocationTable").style.display = "block";
				document.getElementById("fromStopTable").style.display = "none";
				break;
			case "Stop":
				document.getElementById("fromStreetTable").style.display = "none";
				document.getElementById("fromLocationTable").style.display = "none";
				document.getElementById("fromStopTable").style.display = "block";
				break;
			default:
				document.getElementById("fromStreetTable").style.display = "block";
				document.getElementById("fromLocationTable").style.display = "none";
				document.getElementById("fromStopTable").style.display = "none";
		}
		//Show or hide To input boxes
		switch (tType)
		{
			case "Street":
				document.getElementById("toStreetTable").style.display = "block";
				document.getElementById("toLocationTable").style.display = "none";
				document.getElementById("toStopTable").style.display = "none";
				break;
			case "Location":
				document.getElementById("toStreetTable").style.display = "none";
				document.getElementById("toLocationTable").style.display = "block";
				document.getElementById("toStopTable").style.display = "none";
				break;
			case "Stop":
				document.getElementById("toStreetTable").style.display = "none";
				document.getElementById("toLocationTable").style.display = "none";
				document.getElementById("toStopTable").style.display = "block";
				break;
			default:
				document.getElementById("toStreetTable").style.display = "block";
				document.getElementById("toLocationTable").style.display = "none";
				document.getElementById("toStopTable").style.display = "none";
		}
		//From Type
		var ddlFromTypeElement = document.getElementById('ddlFromType')
		if (ddlFromTypeElement != null)
		{
			//Set the drop down list
			switch (fType)
			{
				case "Street":
					ddlFromTypeElement.selectedIndex = 0
					break;
				case "Location":
					ddlFromTypeElement.selectedIndex = 1
					break;
				case "Stop":
					ddlFromTypeElement.selectedIndex = 2
					break;
			}
		}
		//To Type
		var ddlToTypeElement = document.getElementById('ddlToType')
		if (ddlToTypeElement != null)
		{
			//Set the drop down list
			switch (tType)
			{
				case "Street":
					ddlToTypeElement.selectedIndex = 0
					break;
				case "Location":
					ddlToTypeElement.selectedIndex = 1
					break;
				case "Stop":
					ddlToTypeElement.selectedIndex = 2
					break;
			}
		}
	}
	catch(ex)
	{

	}	
}

//set Initial Time, along with initial direction and mode of transport.

function JP_setInitialTime()
{
		
	try
	{
		//Break down current time
		date_now = new Date();
		// Get the minutes in the hour
		minute_value = date_now.getMinutes();
		if (minute_value < 10) {
			minute_value = "0" + minute_value;
		}
		intMinute = Math.floor(minute_value/5);
	
		// Get the hour value	
		hour_value = date_now.getHours();
	
		// Get the AM/ PM
		if (hour_value < 12)
		{
			AMPM = 0;
			intHour = (hour_value - 1);
		}
		else if (hour_value == 12)
		{
			AMPM = 1;
			intHour = (hour_value - 1);
		}
		else
		{
			AMPM = 1;
			intHour = (hour_value - 13);
		}

		//Direction
		//Find Leave After and set that to default
		for(i=0; i<document.forms[0].directionType.length; i++)
		{
			if(document.forms[0].directionType[i].value == "A")
			{
				document.forms[0].directionType[i].checked = true;
				JP_disableTimes(false);
			}
		}
		//Date
		//getDateField() defined in ascx
		ddlDate = getDateField();
		if (ddlDate != null)
		{
			ddlDate.selectedIndex = 0;
		}
		//Hour
		document.getElementById('ddlHour').selectedIndex = intHour;
		//Minute
		document.getElementById('ddlMinute').selectedIndex = intMinute;
		//AM or PM
		document.getElementById('ddlAMPM').selectedIndex = AMPM;
		JP_copyOptionColor();
		//Mode of transport
		//Default is: Bus[checked] School Bus[unchecked] Train[checked] Ferry[checked]
		JP_setTransportMode("b1,s0,t1,f1");
	}
	catch(ex)
	{

	}	
}

function JP_CreateCookies(fromType, toType, frStreet, frSuburb, frLocation, frStop, toStreet, toSuburb, toLocation, toStop, jpDate, jpDirection, jpAMPM, jpHour, jpMinute, transportMode,nMaxJourneys)
{
// Create JP cookies

var JP_cookies;
JP_cookies='fromType='+fromType;
JP_cookies+='&FromStreet='+frStreet;
JP_cookies+='&FromSuburb='+frSuburb;
JP_cookies+='&FromStop='+frStop;
JP_cookies+='&FromLocation='+frLocation;
JP_cookies+='&toType='+toType;
JP_cookies+='&ToStreet='+toStreet;
JP_cookies+='&ToSuburb='+toSuburb;
JP_cookies+='&ToStop='+toStop;
JP_cookies+='&ToLocation='+toLocation;

createCookie('JourneyPlanner',JP_cookies,30);		



// Create Time cookies - session cookies
var JPTime_cookies;
var datePart = jpDate.split(',');
	if (datePart.length == 2)
	{
	JPTime_cookies='jpDate='+datePart[0].trim() + ',' + datePart[1].trim();
	}
	else
	{
		JPTime_cookies='jpDate='+jpDate;
	}
JPTime_cookies+='&jpDirection='+jpDirection;
JPTime_cookies+='&jpHour='+jpHour;
JPTime_cookies+='&jpMinute='+jpMinute;
JPTime_cookies+='&jpAMPM='+jpAMPM;
JPTime_cookies+='&jpMode='+transportMode;
JPTime_cookies+='&jpnMaxJourneys='+nMaxJourneys;

createSessionCookie('JourneyPlannerTime',JPTime_cookies);		

}

function JP_RetrieveCookies()
{
	
		
		JP_AssignCookieToTextbox(GetCookieValueFromCookieString(readCookie('JourneyPlanner'),6),'txtToStreet');
		//document.getElementById("txtToSuburb").value = 'b';
		JP_AssignCookieToTextbox(GetCookieValueFromCookieString(readCookie('JourneyPlanner'),7),'txtToSuburb');
		//document.getElementById("txtToLocation").value = initToLocation;
		JP_AssignCookieToTextbox(GetCookieValueFromCookieString(readCookie('JourneyPlanner'),9),'txtToLocation');
		//document.getElementById("txtToStop").value = initToStop;
		JP_AssignCookieToTextbox(GetCookieValueFromCookieString(readCookie('JourneyPlanner'),8),'txtToStop');
		
		//document.getElementById("txtFromStreet").value = initFromStreet;
		JP_AssignCookieToTextbox(GetCookieValueFromCookieString(readCookie('JourneyPlanner'),1),'txtFromStreet');
		//document.getElementById("txtFromSuburb").value = initFromSuburb;
		JP_AssignCookieToTextbox(GetCookieValueFromCookieString(readCookie('JourneyPlanner'),2),'txtFromSuburb');
		//document.getElementById("txtFromLocation").value = initFromLocation;
		JP_AssignCookieToTextbox(GetCookieValueFromCookieString(readCookie('JourneyPlanner'),4),'txtFromLocation');
		//document.getElementById("txtFromStop").value = initFromStop;
		JP_AssignCookieToTextbox(GetCookieValueFromCookieString(readCookie('JourneyPlanner'),3),'txtFromStop');
		
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function createSessionCookie(name,value) {	
	document.cookie = name+"="+value+"; path=/";
}


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function JP_AssignCookieToTextbox(cookie,textboxName)
{
	if (cookie!=null && cookie !="")
	{
		document.getElementById(textboxName).value = cookie;
	}
}

         
function GetCookieValueFromCookieString(cookieString,n)
{
	if (cookieString!=null)
	{
		var cookie =cookieString.split('&');
	
		if (cookie!=null && cookie !="")
		{
			return cookie[n].split('=')[1];}
		}
	
	return null;
	
}


// Set initial time based on cookie, along with direction and mode of transport.

function JP_setCookieTime()
{

	var strDate=GetCookieValueFromCookieString(readCookie('JourneyPlannerTime'),0);
	var datePart=strDate.split(',');
	if (datePart.length == 2)
	{
		strDate = datePart[0].trim() + ", " + datePart[1].trim();
	}
	var strDirection=GetCookieValueFromCookieString(readCookie('JourneyPlannerTime'),1);
	var strHour=GetCookieValueFromCookieString(readCookie('JourneyPlannerTime'),2);
	var strMinute=GetCookieValueFromCookieString(readCookie('JourneyPlannerTime'),3);
	var strAMPM=GetCookieValueFromCookieString(readCookie('JourneyPlannerTime'),4);
	var strMode=GetCookieValueFromCookieString(readCookie('JourneyPlannerTime'),5);
	try
	{
		//Get DOM objects
		//getDateField() defined in ascx
		ddlDate = getDateField();
		ddlHour = document.getElementById('ddlHour');
		ddlMinute = document.getElementById('ddlMinute');
		ddlMinute = Math.floor(ddlMinute/5);
		ddlAMPM = document.getElementById('ddlAMPM');

		//Direction
		for(i=0; i<document.forms[0].directionType.length; i++)
		{
			if(document.forms[0].directionType[i].value == strDirection)
			{
				document.forms[0].directionType[i].checked = true;
			}
		}
		switch (strDirection)
		{
			// E = Earliest trip
			case "E":
				JP_disableTimes(true);
				break;
			// L = Last trip
			case "L":
				JP_disableTimes(true);
				break;
			default:
				JP_disableTimes(false);
		} 
		//Date
		if (ddlDate != null)
		{
			for(i=0; i<ddlDate.options.length; i++)
			{
				if(ddlDate.options[i].value == strDate)
				{
					ddlDate.selectedIndex = i;
				}			
			}
		}
		//Hour
		for(i=0; i<document.forms[0].ddlHour.options.length; i++)
		{
			if(document.forms[0].ddlHour.options[i].value == strHour)
			{
				document.forms[0].ddlHour.selectedIndex = i;
			}			
		}
		//Minute
		for(i=0; i<document.forms[0].ddlMinute.options.length; i++)
		{
			if(document.forms[0].ddlMinute.options[i].value == strMinute)
			{
				document.forms[0].ddlMinute.selectedIndex = i;
			}			
		}
		//AM or PM	
		for(i=0; i<document.forms[0].ddlAMPM.options.length; i++)
		{
			if(document.forms[0].ddlAMPM.options[i].value == strAMPM)
			{
				document.forms[0].ddlAMPM.selectedIndex = i;
			}			
		}
		JP_copyOptionColor();
		//Transport Mode
		JP_setTransportMode(strMode);
	}
	catch(ex)
	{

	}	
}

//Set up form view based on Radio Button Selection
//onClick function for Street/Location/Stops
function JP_setFormView(from)
{
	try
	{
		var sValue = "";
		if(from == "From")
		{
			var ddlFromTypeElement = document.getElementById('ddlFromType');
			if(ddlFromTypeElement != null)
			{
				//Read value from drop down list
				var sItem = ddlFromTypeElement.selectedIndex;
				sValue = ddlFromTypeElement.options[sItem].value;
			}
			switch (sValue)
			{
				case "Street":
					document.getElementById("fromStreetTable").style.display = "block";
					document.getElementById("fromLocationTable").style.display = "none";
					document.getElementById("fromStopTable").style.display = "none";
					break;
				case "Location":
					document.getElementById("fromStreetTable").style.display = "none";
					document.getElementById("fromLocationTable").style.display = "block";
					document.getElementById("fromStopTable").style.display = "none";
					break;
				case "Stop":
					document.getElementById("fromStreetTable").style.display = "none";
					document.getElementById("fromLocationTable").style.display = "none";
					document.getElementById("fromStopTable").style.display = "block";
					break;
			}
			// Clear input boxes
			document.getElementById("txtFromStreet").value = initFromStreet;
			document.getElementById("txtFromSuburb").value = initFromSuburb;
			document.getElementById("txtFromLocation").value = initFromLocation;
			document.getElementById("txtFromStop").value = initFromStop;
		}
		else
		{
			var ddlToTypeElement = document.getElementById('ddlToType');
			if(ddlToTypeElement != null)
			{
				//read value from drop down list
				var sItem = ddlToTypeElement.selectedIndex;
				sValue = ddlToTypeElement.options[sItem].value;
			}
			switch (sValue)
			{
				case "Street":
					document.getElementById("toStreetTable").style.display = "block";
					document.getElementById("toLocationTable").style.display = "none";
					document.getElementById("toStopTable").style.display = "none";
					break;
				case "Location":
					document.getElementById("toStreetTable").style.display = "none";
					document.getElementById("toLocationTable").style.display = "block";
					document.getElementById("toStopTable").style.display = "none";
					break;
				case "Stop":
					document.getElementById("toStreetTable").style.display = "none";
					document.getElementById("toLocationTable").style.display = "none";
					document.getElementById("toStopTable").style.display = "block";
					break;
			}
			// Clear input boxes
			document.getElementById("txtToStreet").value = initToStreet;
			document.getElementById("txtToSuburb").value = initToSuburb;
			document.getElementById("txtToLocation").value = initToLocation;
			document.getElementById("txtToStop").value = initToStop;
		}
	}
	catch(ex)
	{

	}	
}
//Logic to call JP_disableTimes based on direction input
//onClick function for Arrive by/Leave After/Earliest trip today/Last trip today
function JP_directionChange()
{
	try
	{
		var sValue = "";
		//Check value from radio buttons
		for(i=0; i<document.forms[0].directionType.length; i++)
		{
			if(document.forms[0].directionType[i].checked == true)
			{
				sValue = document.forms[0].directionType[i].value;
			}
		}
		switch (sValue)
		{
			// E = Earliest trip
			case "E":
				JP_disableTimes(true);
				break;
			// L = Last trip
			case "L":
				JP_disableTimes(true);
				break;
			default:
				JP_disableTimes(false);
		}
		//Call copyOptionColor to set/clear the color for the AM/PM drop down list
		JP_copyOptionColor();
	}
	catch(ex)
	{

	}		
}
// Function to check or uncheck the mode of transport checkboxes based on an input string.
// Definitions:
// b = bus, s = school bus, t = train, f = ferry
// 0 = unchecked, 1 = checked
// Example input string: b1,s0,t1,f1 = bus[checked], school bus[unchecked], train[checked],
// ferry[checked]
function JP_setTransportMode(strMode)
{

	try
	{
		if (strMode != "")
		{
			var modeArray = strMode.split(",");
			var charMode = ""; 
			var status = ""; 
			var value = ""; //the value of checkbox defined in ascx

			for (i=0; i<modeArray.length; i++)
			{
				//Get 1st character
				charMode = modeArray[i].slice(0,1);
				//Get 2nd character
				status = modeArray[i].slice(1,2); 
				//Translate 1 character mode code back to checkbox value
				switch (charMode)
				{
					case "b":
						value = "bus";
						break;
					case "s":
						value = "schoolbus";
						break;
					case "t":
						value = "train";
						break;
					case "f":
						value = "ferry";
						break;
				} 
				for (j=0; j<document.forms[0].ckMode.length; j++)
				{
					if (document.forms[0].ckMode[j].value == value)
					{
						if (status == "1")
						{
							document.forms[0].ckMode[j].checked = true;
						}
						else
						{
							document.forms[0].ckMode[j].checked = false;
						}
					}
				}	
			}
		}
	}
	catch(ex)
	{

	}	
}
// Function to delete the initial input when user click on the input box.
function JP_clearInitInput(objInput)
{
	try
	{
		if (objInput.value == initFromStreet ||
		    objInput.value == initFromSuburb ||
		    objInput.value == initFromLocation ||
		    objInput.value == initFromStop ||
		    objInput.value == initToStreet ||
		    objInput.value == initToSuburb ||
		    objInput.value == initToLocation ||
		    objInput.value == initToStop)
		{
			objInput.value = "";
		}
	}
	catch(ex)
	{

	}	
}
//Function to enable/disable time inputs
function JP_disableTimes(status)
{
	try
	{
		var hrs = document.getElementById('ddlHour');
		var min = document.getElementById('ddlMinute');
		var ampm = document.getElementById('ddlAMPM');
	
		hrs.disabled = status;
		min.disabled = status;
		ampm.disabled = status;
	}
	catch(ex)
	{

	}	
}
//Function to copy the specific AM/PM color from option to the drop down list visible to the user. 
function JP_copyOptionColor()
{
	try
	{
		ddlAMPM = document.getElementById('ddlAMPM')
		if (ddlAMPM != null)
		{
			if (ddlAMPM.selectedIndex >= 0)
			{
				if (ddlAMPM.disabled == false)
				{
					//copy the color
					ddlAMPM.style.color=ddlAMPM.options[ddlAMPM.selectedIndex].style.color;
				}
				else
				{
					ddlAMPM.style.color="";
				}
			}
		}
	}
	catch(ex)
	{

	}	
}
//Function to set the default button for each DIV. On user pressing the ENTER key the default button will be clicked.
function JP_SetDefaultButton(event, target) 
{
    //Code from http://www.codeproject.com/useritems/FormPanel.asp
    try
    {
	    if ((event.keyCode == 13 || event.which == 13) && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == 'textarea'))) 
	    {
	        var defaultButton = document.getElementById(target);
	        if (defaultButton == 'undefined') defaultButton = document.all[target]; 
	
	        if (defaultButton && typeof(defaultButton.click) != 'undefined') 
	        {
	            defaultButton.click();
	            event.cancelBubble = true;
	            if (event.stopPropagation) event.stopPropagation();
	            return false;
	        }
	    }
	    return true;
	 }
	 catch (ex)
	 {

	 }
}

// Function called when user click Reset. Response for resetting all inputs to system defaults.
function JP_GetClearedJP(callFrom)
{
	try
	{
		//Default
		JP_setInitialFormView("Street","Street");
		JP_setInitialTime();
		document.getElementById("txtToStreet").value = initToStreet;
		document.getElementById("txtToSuburb").value = initToSuburb;
		document.getElementById("txtToLocation").value = initToLocation;
		document.getElementById("txtToStop").value = initToStop;
		document.getElementById("txtFromStreet").value = initFromStreet;
		document.getElementById("txtFromSuburb").value = initFromSuburb;
		document.getElementById("txtFromLocation").value = initFromLocation;
		document.getElementById("txtFromStop").value = initFromStop;
	}
	catch(ex)
	{

	}	
}
//Function called when user click Reverse Trip. Swap the From and To inputs.
function JP_reverseTrip()
{
	var fromType = "";
	var toType = "";
	var fromStreet = "";
	var fromSuburb = "";
	var fromLocation = "";
	var fromStop = "";
	var toStreet = "";
	var toSuburb = "";
	var toLocation = "";
	var toStop = "";
	var ch = "";
	
	try
	{
		//Get the FROM type
		var ddlFromTypeElement = document.getElementById('ddlFromType');
		if(ddlFromTypeElement != null)
		{
			//read value from drop down list
			var sItem = ddlFromTypeElement.selectedIndex;
			ch = ddlFromTypeElement.options[sItem].value;
		}
		switch (ch)
		{
			case "Street":
				fromType = "Street";
				fromStreet = document.getElementById("txtFromStreet").value;
				fromSuburb = document.getElementById("txtFromSuburb").value;
				break;
			case "Location":
				fromType = "Location";
				fromLocation = document.getElementById("txtFromLocation").value;
				break;
			case "Stop":
				fromType = "Stop";
				fromStop = document.getElementById("txtFromStop").value;
				break;
		}
		//Get the TO type
		var ddlToTypeElement = document.getElementById('ddlToType');
		if(ddlToTypeElement != null)
		{
			//read value from drop down list
			var sItem = ddlToTypeElement.selectedIndex;
			ch = ddlToTypeElement.options[sItem].value;
		}
		switch (ch)
		{
			case "Street":
				toType = "Street";
				toStreet = document.getElementById("txtToStreet").value;
				toSuburb = document.getElementById("txtToSuburb").value;
				break;
			case "Location":
				toType = "Location";
				toLocation = document.getElementById("txtToLocation").value;
				break;
			case "Stop":
				toType = "Stop";
				toStop = document.getElementById("txtToStop").value;
				break;
		}
		//Clear all textbox fields
		document.getElementById("txtToStreet").value = initToStreet;
		document.getElementById("txtToSuburb").value = initToSuburb;
		document.getElementById("txtToLocation").value = initToLocation;
		document.getElementById("txtToStop").value = initToStop;
		document.getElementById("txtFromStreet").value = initFromStreet;
		document.getElementById("txtFromSuburb").value = initFromSuburb;
		document.getElementById("txtFromLocation").value = initFromLocation;
		document.getElementById("txtFromStop").value = initFromStop;
		//Set the new From field values based on old To values
		switch (toType)
		{
			case "Street":
				document.getElementById("fromStreetTable").style.display = "block";
				document.getElementById("fromLocationTable").style.display = "none";
				document.getElementById("fromStopTable").style.display = "none";
				document.getElementById("txtFromStreet").value = toStreet;
				document.getElementById("txtFromSuburb").value = toSuburb;
				break;
			case "Location":
				document.getElementById("fromStreetTable").style.display = "none";
				document.getElementById("fromLocationTable").style.display = "block";
				document.getElementById("fromStopTable").style.display = "none";
				document.getElementById("txtFromLocation").value = toLocation;
				break;
			case "Stop":
				document.getElementById("fromStreetTable").style.display = "none";
				document.getElementById("fromLocationTable").style.display = "none";
				document.getElementById("fromStopTable").style.display = "block";
				document.getElementById("txtFromStop").value = toStop;
				break;
		}
		//Set the new FROM Type
		if(ddlFromTypeElement != null)
		{
			//Set the right drop down list
			switch (toType)
			{
				case "Street":
					ddlFromTypeElement.selectedIndex = 0
					break;
				case "Location":
					ddlFromTypeElement.selectedIndex = 1
					break;
				case "Stop":
					ddlFromTypeElement.selectedIndex = 2
					break;
			}
		}
		//Set the new To field values based on old From values
		switch (fromType)
		{
			case "Street":
				document.getElementById("toStreetTable").style.display = "block";
				document.getElementById("toLocationTable").style.display = "none";
				document.getElementById("toStopTable").style.display = "none";
				document.getElementById("txtToStreet").value = fromStreet;
				document.getElementById("txtToSuburb").value = fromSuburb;
				break;
			case "Location":
				document.getElementById("toStreetTable").style.display = "none";
				document.getElementById("toLocationTable").style.display = "block";
				document.getElementById("toStopTable").style.display = "none";
				document.getElementById("txtToLocation").value = fromLocation;
				break;
			case "Stop":
				document.getElementById("toStreetTable").style.display = "none";
				document.getElementById("toLocationTable").style.display = "none";
				document.getElementById("toStopTable").style.display = "block";
				document.getElementById("txtToStop").value = fromStop;
				break;
		}
		//Set the new To Type
		if(ddlToTypeElement != null)
		{
			//Set the right drop down list
			switch (fromType)
			{
				case "Street":
					ddlToTypeElement.selectedIndex = 0
					break;
				case "Location":
					ddlToTypeElement.selectedIndex = 1
					break;
				case "Stop":
					ddlToTypeElement.selectedIndex = 2
					break;
			}
		}
	}
	catch (ex)
	{

	}
}
/* End Form Display Functions */

/* Journey Plan */
//Validation for main Journey Planner Screen
function JP_valForm()
{
	if ((document.getElementById("txtFromStreet").value == "" || document.getElementById("txtFromStreet").value == initFromStreet) &&
		(document.getElementById("txtFromSuburb").value == "" || document.getElementById("txtFromSuburb").value == initFromSuburb) &&
		(document.getElementById("txtFromLocation").value == "" ||document.getElementById("txtFromLocation").value == initFromLocation) &&
		(document.getElementById("txtFromStop").value == "" || document.getElementById("txtFromStop").value == initFromStop) )
	{
		alert('You must enter a \"From\" location.');
		return false;
	}
	if ((document.getElementById("txtToStreet").value == "" || document.getElementById("txtToStreet").value == initToStreet) &&
		(document.getElementById("txtToSuburb").value == "" || document.getElementById("txtToSuburb").value == initToSuburb) &&
		(document.getElementById("txtToLocation").value == "" || document.getElementById("txtToLocation").value == initToLocation) &&
		(document.getElementById("txtToStop").value == "" || document.getElementById("txtToStop").value == initToStop) )
	{
		alert('You must enter a \"To\" location.');
		return false;
	}
	var txtFromStopElement = document.getElementById("txtFromStop");
	var objRegExp =/^\d{5}$/ //Regular expression for 5 digit number
	if (txtFromStopElement != null)
	{
		if (txtFromStopElement.value != "" && txtFromStopElement.value != initFromStop)
		{
			if(objRegExp.test(txtFromStopElement.value)!=true)
			{
				alert('The \"From\" Stop number must be 5 digit numbers.');
				return false;				
			}
		}
	}
	var txtToStopElement = document.getElementById("txtToStop");
	if (txtToStopElement != null)
	{
		if (txtToStopElement.value != "" && txtToStopElement.value != initToStop)
		{
			if(objRegExp.test(txtToStopElement.value)!=true)
			{
				alert('The \"To\" Stop number must be 5 digit numbers.');
				return false;				
			}
		}
	}
	var checkCount = 0;
	for (j=0; j<document.forms[0].ckMode.length; j++)
	{
		if (document.forms[0].ckMode[j].checked == true)
		{
			checkCount = checkCount + 1;
		}
	}
	if (checkCount == 0)
	{
		alert('Please check at least one mode of transport.');
		return false;
	}
	return true;
}

//Main function called when user press Search to start journey planner process.
function planJourney()
{
	var fr = "";
	var nt = "";
	var frStreet = "";
	var frSuburb = "";
	var frLocation = "";
	var frStop = "";
	var toStreet = "";
	var toSuburb = "";
	var toLocation = "";
	var toStop = "";
	var jpDate = "";
	var jpDirection = "";
	var jpAMPM = "";
	var jpHour = "";
	var jpMinute = "";
	var bus = "true";
	var sBus = "false";
	var train = "true";
	var ferry = "true";
	var transportMode = "";
	var jpnMaxJourneys="";

	//Validate
	if(JP_valForm() == true)
	{
		document.getElementById("btnJourney").disabled = true;
		document.getElementById("msgPlanning").innerHTML = "&nbsp;<img src=\"" + getRootPath() + "/DesktopModules/JourneyPlanner/images/loading.gif\"> Loading.&nbsp;";
		//From
		var ddlFromTypeElement = document.getElementById('ddlFromType');
		if(ddlFromTypeElement != null)
		{
			//Read value from drop down list
			var sItem = ddlFromTypeElement.selectedIndex;
			fr = ddlFromTypeElement.options[sItem].value;
		}
		switch(fr)
		{
			case "Street":
				frStreet = document.getElementById("txtFromStreet").value;
				if (frStreet == initFromStreet)
				{
					frStreet = "";
				}
				frSuburb = document.getElementById("txtFromSuburb").value;
				if (frSuburb == initFromSuburb)
				{
					frSuburb = "";
				}
				break;
			case "Location":
				frLocation = document.getElementById("txtFromLocation").value;
				if (frLocation == initFromLocation)
				{
					frLocation = "";
				}
				break;
			case "Stop":
				frStop = document.getElementById("txtFromStop").value;
				if (frStop == initFromStop)
				{
					frStop = "";
				}
				break;
		}
	
		//To
		var ddlToTypeElement = document.getElementById('ddlToType');
		if(ddlToTypeElement != null)
		{
			//Read value from drop down list
			var sItem = ddlToTypeElement.selectedIndex;
			nt = ddlToTypeElement.options[sItem].value;
		}
		switch(nt)
		{
			case "Street":
				toStreet = document.getElementById("txtToStreet").value;
				if (toStreet == initToStreet)
				{
					toStreet = "";
				}
				toSuburb = document.getElementById("txtToSuburb").value;
				if (toSuburb == initToSuburb)
				{
					toSuburb = "";
				}
				break;
			case "Location":
				toLocation = document.getElementById("txtToLocation").value;
				if (toLocation == initToLocation)
				{
					toLocation = "";
				}
				break;
			case "Stop":
				toStop = document.getElementById("txtToStop").value;
				if (toStop == initToStop)
				{
					toStop = "";
				}
				break;
		}

		//Date and Time
		jpDate = getDateField().options[getDateField().selectedIndex].value;
		//Search the radio buttons to see which direction is set
		for(k=0; k<document.forms[0].directionType.length; k++)
		{
			if(document.forms[0].directionType[k].checked == true)
			{
				jpDirection = document.forms[0].directionType[k].value;
			}
		}
		ddlMid = document.getElementById("ddlAMPM");
		jpAMPM = ddlMid.options[ddlMid.selectedIndex].value;
		ddlH = document.getElementById("ddlHour");
		jpHour = ddlH.options[ddlH.selectedIndex].value;
		ddlM = document.getElementById("ddlMinute");
		jpMinute = ddlM.options[ddlM.selectedIndex].value;
		ddlnMaxJourneys=document.getElementById("ddlnMaxJourneys");
		if (ddlnMaxJourneys!=null)
		{
		    jpnMaxJourneys=ddlnMaxJourneys.options[ddlnMaxJourneys.selectedIndex].value;
		}
		else
		{
		    jpnMaxJourneys=3;
		}
		//Set Hidden Field Values (Date, Time, Direction)	
		document.getElementById("hDate").value = jpDate;
		document.getElementById("hTime").value = jpHour + ':' + jpMinute + jpAMPM;
		document.getElementById("hDirection").value = jpDirection;
		
		
	
		//Transport Mode
		for (j=0; j<document.forms[0].ckMode.length; j++)
		{
			modeChar="";
			switch(document.forms[0].ckMode[j].value)
			{
				case "train":
					modeChar = "t";
					break;
				case "bus":
					modeChar = ",b";
					break;
				case "ferry":
					modeChar = ",f";
					break;
				case "schoolbus":
					modeChar = ",s";
					break;
			}
			modeValue="";
			if (document.forms[0].ckMode[j].checked == true)
			{
				modeValue="1"; //1 represent checked
			}
			else
			{
				modeValue="0"; //0 represent uncheck 
			}
			transportMode = transportMode + modeChar + modeValue;
		}	
	
		//Make Request
		//alert("fr:"+fr+" frStreet:"+frStreet+" frSuburb:"+frSuburb+" frLocation:"+frLocation+" frStop:"+frStop);
		//alert("nt:"+nt+" toStreet:"+toStreet+" toSuburb:"+toSuburb+" toLocation:"+toLocation+" toStop:"+toStop);
		//alert("jpDate:"+jpDate+" jpDirection:"+jpDirection+" jpAMPM:"+jpAMPM+" jpHour:"+jpHour+" jpMinute:"+jpMinute+" Mode: "+transportMode);
		getOptions(fr, nt, frStreet, frSuburb, frLocation, frStop, toStreet, toSuburb, toLocation, toStop, jpDate, jpDirection, jpAMPM, jpHour, jpMinute, getFrameset(), getGadget(), transportMode,jpnMaxJourneys);
		
		JP_CreateCookies(fr, nt, frStreet, frSuburb, frLocation, frStop, toStreet, toSuburb, toLocation, toStop, jpDate, jpDirection, jpAMPM, jpHour, jpMinute, transportMode,jpnMaxJourneys);
		
		
	}
}
/* End Journey Plan Functions */

/* Journey Planner Enquiry (intermediate screen) Fucntion */

//Function that call pouplate From/To location drop down list from XML or show the Plase Wait intermediate message.
function JP_ClearAndSetLocations(locationNode)
{
	//Hide the Journey Planner Form and show locations	
	selF = document.getElementById("JPEForm");
	selF.style.display = "block";
	selQ = document.getElementById("JPForm");
	selQ.style.display = "none";

	fromControl = document.getElementById("selFrom");
	toControl = document.getElementById("selTo");	
		
    //Clears the From list box contents.
	for (var count = fromControl.options.length-1; count >-1; count--)
	{
		fromControl.options[count] = null;
	}
	//Clears the To list box contents.
	for (var icount = toControl.options.length-1; icount >-1; icount--)
	{
		toControl.options[icount] = null;
	}
	
	//alert('OK');
	try
	{
		var locs = locationNode.getElementsByTagName('address');
	}
	catch(t)
	{
		//alert(t);
		selF.style.display = "none";
		document.getElementById("msgRedirection").innerHTML = "<br/>There are no locations matching your query.<br/> Refresh the page to try again.<br/> Please check the spelling.<br/><br/>";
		return;		
	}
	//alert(locs.length);
	//alert('OK1');
	
	if (locs.length > 0)
	{
		selF.style.display = "none";
		document.getElementById("msgRedirection").innerHTML = "<br/><img src=\"" + getRootPath() + "/DesktopModules/JourneyPlanner/images/loading.gif\"><span class=\"TP_M_TextInfo\">&nbsp;Please wait ..</span><span class=\"TP_M_TextBold\"><br/><br/>Loading the results of the Journey Planner search.</span>";
		document.location.href = "http://" + document.domain + JP_URLDecode(locs[0].firstChild.nodeValue);
	}
	else
	{	
		//alert('OK1');
		//Seperate the To and From Locations
		toLocation = locationNode.getElementsByTagName('ToLocation');
		fromLocation = locationNode.getElementsByTagName('FromLocation');
		
		var textValue;
		var textDisplay;
		var optionItem;
		
		//Get the From Values and Display text
		try
		{
			if(fromLocation.length > 0)
			{			
				for (var i = 0; i < fromLocation.length; i++)
				{
					var fLoc = fromLocation[i];
					var fdisp = fLoc.getElementsByTagName("Display")[0].firstChild.nodeValue;
					var fval = fLoc.getElementsByTagName("Value")[0].firstChild.nodeValue;
					//alert(disp + ', ' + val);
					textValue = fdisp;
					textDisplay = fval;
					optionItem = new Option(textValue , textDisplay,  false, false);
					fromControl.options[fromControl.length] = optionItem;
				}
				//Show the list box and hide the message
				fromControl.style.display = "block";
				document.getElementById("msgNoFrom").style.display = "none";
			}
			else
			{
				//Show and set the message, also hide the list box
				fromControl.style.display = "none";
				document.getElementById("msgNoFrom").style.display = "block";
				document.getElementById("msgNoFrom").innerHTML = "<br/>There are no locations matching your query.<br/> Use the \"Back\" button to try again.<br/> Please check the spelling.<br/><br/>";
			}
		}
		catch(e)
		{
			alert(e);
		}
		
		//Get the To Values and Display text
		try
		{
			if (toLocation.length > 0)
			{
				for (var i = 0; i < toLocation.length; i++)
				{
					var tLoc = toLocation[i];
					var disp = tLoc.getElementsByTagName("Display")[0].firstChild.nodeValue;
					var val = tLoc.getElementsByTagName("Value")[0].firstChild.nodeValue;
					//alert(disp + ', ' + val);
					textValue = disp;
					textDisplay = val;
					optionItem = new Option( textValue, textDisplay,  false, false);
					toControl.options[toControl.length] = optionItem;
				}
				//Show the list box and hide the message
				toControl.style.display = "block";
				document.getElementById("msgNoTo").style.display = "none";				
			}
			else
			{
				//Show and set the message, also hide the list box			
				toControl.style.display = "none";
				document.getElementById("msgNoTo").style.display = "block";
				document.getElementById("msgNoTo").innerHTML = "<br/>There are no locations matching your query.<br/> Use the \"Back\" button to try again.<br/> Please check the spelling.<br/><br/>";
			}
		}
		catch(e)
		{
			alert(e);
		}
	}
	
	//
	//alert(cities.length);
	/*for (var count = 0; count < varToDisplay.length; count++)
	{
		textValue = GetInnerText(varToValue[count]);
		textDisplay = GetInnerText(varToDisplay[count]);
		optionItem = new Option( textValue, textValue,  false, false);
		toControl.options[toControl.length] = optionItem;
	}
	toControl.disabled = false;*/
}

function JP_GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function JP_hideResults()
{
	selF = document.getElementById("JPEForm");
	selF.style.display = "none";
	selQ = document.getElementById("JPForm");
	selQ.style.display = "block";
	document.getElementById("btnJourney").disabled = false;
	document.getElementById("msgPlanning").innerHTML = "";
}

//Function for intermediate screen validation
function JP_valLocations()
{
	continuePlanning = document.getElementById("msgContinuePlanning");
	continuePlanning.innerHTML = "&nbsp;<img src=\"" + getRootPath() + "/DesktopModules/JourneyPlanner/images/loading.gif\">&nbsp;Loading.";
	continuePlanning.style.display = "inline";
	fromControl = document.getElementById("selFrom");
	toControl = document.getElementById("selTo");
	
	var fromSel = 0;
	var toSel = 0;
	
	for (var i=0; i < fromControl.length; i++)
	{
		 if (fromControl.options[i].selected)
		 {
			fromSel++;
		 }
	}
	
	for (var j=0; j < toControl.length; j++)
	{
		 if (toControl.options[j].selected)
		 {
			toSel++;
		 }
	}
	if (fromSel == 0)
	{
		confirm('Please select an item from the \"From\" listbox.');
		continuePlanning.style.display = "none";
		return false;
	}
	else if (toSel == 0)
	{
		alert('Please select an item from the \"To\" listbox.');
		continuePlanning.style.display = "none";
		return false;
	}
	else
	{
		return true;
	}
}
/* End of Journey Planner Enquiry functions */

/* Functions for Interaction with Journey Planner Map */
// Called from Map Frame Clicks
function SetToStreet(street, suburb)
{
	var ddlToTypeElement = document.getElementById('ddlToType');
	if(ddlToTypeElement != null)
	{
		//set drop down list to Street
		ddlToTypeElement.selectedIndex = 0
	}
	
	setFormView("To");
	document.getElementById('txtToStreet').value = street;
	document.getElementById('txtToSuburb').value = suburb;
	document.getElementById('txtToSuburb.ClientID').focus();
}
function SetToSuburb(suburb)
{
	var ddlToTypeElement = document.getElementById('ddlToType');
	if(ddlToTypeElement != null)
	{
		//set drop down list to Street, because Suburb exist under street mode
		ddlToTypeElement.selectedIndex = 0
	}
	
	setFormView("To");
	document.getElementById('txtToSuburb').value = suburb;
	document.getElementById('txtToSuburb').focus();
}
function SetToStop(stopno)
{
	var ddlToTypeElement = document.getElementById('ddlToType');
	if(ddlToTypeElement != null)
	{
		//set drop down list to Stop
		ddlToTypeElement.selectedIndex = 2
	}

	setFormView("To");
	document.getElementById('txtToStop').value = stopno;
	document.getElementById('txtToStop').focus();
}
function SetFromStreet(street, suburb)
{
	var ddlFromTypeElement = document.getElementById('ddlFromType');
	if(ddlFromTypeElement != null)
	{
		//set drop down list to Street
		ddlFromTypeElement.selectedIndex = 0
	}

	setFormView("From");
	document.getElementById('txtFromStreet').value = street;
	document.getElementById('txtFromSuburb').value = suburb;
	document.getElementById('txtFromSuburb').focus();
}
function SetFromSuburb(suburb)
{
	var ddlFromTypeElement = document.getElementById('ddlFromType');
	if(ddlFromTypeElement != null)
	{
		//set drop down list to Street
		ddlFromTypeElement.selectedIndex = 0
	}
	
	setFormView("From");
	document.getElementById('txtFromSuburb').value = suburb;
	document.getElementById('txtFromSuburb').focus();
}
function SetFromStop(stopno)
{
	var ddlFromTypeElement = document.getElementById('ddlFromType');
	if(ddlFromTypeElement != null)
	{
		//set drop down list to Street
		ddlFromTypeElement.selectedIndex = 2
	}

	setFormView("From");
	document.getElementById('txtFromStop').value = stopno;
	document.getElementById('txtFromStop').focus();
}
/* End of Map Functions */

/* URL encode/decode functions */ 
function JP_URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   //var encoded = document.URLForm.F2.value;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   //document.URLForm.F1.value = plaintext;
   return plaintext;
};

function JP_URLEncode(decoded)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = decoded;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}
/* End of URL encode/decode functions */ 