var formSubmitted = 'false';

function he(form, event)
{
	var vError ='';
	var vState ='return';

	if (event == 'SelectStartType')
	{		
		form.FormState.value = event;
		form.SelectStartType.value = form.SelectStartType[form.SelectStartType.selectedIndex].value;
		//alert(form.SelectStartType.value);
		form.Start.value = '';
		form.StartDetail.value = '';
		vState = 'submit';
	}

	if (event == 'SelectEndType')
	{		
		form.FormState.value = event;
		form.SelectEndType.value = form.SelectEndType[form.SelectEndType.selectedIndex].value;
		//alert(form.SelectEndType.value);
		form.End.value = '';
		form.EndDetail.value = '';
		vState = 'submit';
	}
 
	if (event == 'SelectStart')
	{		
		form.StartDetail.value = form.SelectStart[form.SelectStart.selectedIndex].text;
		form.StartGeo.value = form.SelectStart[form.SelectStart.selectedIndex].value;

		
		var indexBrackBegin = form.StartDetail.value.search(/\(/);
		var indexBrackEnd = form.StartDetail.value.search(/\)/);
		
		if( indexBrackBegin >=0 && indexBrackEnd >=0)
		{
		 
		  form.Start.value = form.StartDetail.value.substring(indexBrackBegin+1, indexBrackEnd);
		  if(form.Start.value == '' || form.Start.value == ' ')
			form.Start.value = form.StartDetail.value;
			
		  //alert(form.Start.value);
		
		}
		else
		  form.Start.value = form.StartDetail.value;

		
		vState = 'return';
	}

	if (event == 'SelectEnd')
	{		
		form.EndDetail.value = form.SelectEnd[form.SelectEnd.selectedIndex].text;
		form.EndGeo.value = form.SelectEnd[form.SelectEnd.selectedIndex].value;
		
		var indexBrackBegin = form.EndDetail.value.search(/\(/);
		var indexBrackEnd = form.EndDetail.value.search(/\)/);
		
		if( indexBrackBegin >=0 && indexBrackEnd >=0)
		{
		 
		  form.End.value = form.EndDetail.value.substring(indexBrackBegin+1, indexBrackEnd);
		 // alert(form.End.value);
		  if(form.End.value == '' || form.End.value == ' ')
			form.End.value = form.EndDetail.value;
		
		}
		else
		  form.End.value = form.EndDetail.value;
		vState = 'return';
	}
	if (event == 'MatchNextBus')
	{		
		form.StopId.value = form.MatchNextBus[form.MatchNextBus.selectedIndex].value;
	}
	if (event == 'MatchOrigin')
	{		
		if(form.MatchOrigin[form.MatchOrigin.selectedIndex].value == form.MatchDestination[form.MatchDestination.selectedIndex].value)
		{
			alert('You can not select a location that is the same as the Destination');
			return;
		}
		
		form.StartDetail.value = form.MatchOrigin[form.MatchOrigin.selectedIndex].text;
		form.Start.value = form.StartDetail.value;
		form.StartGeo.value = form.MatchOrigin[form.MatchOrigin.selectedIndex].value;
		document.getElementById('start-detail').innerHTML = form.MatchOrigin[form.MatchOrigin.selectedIndex].text;
	}

	if (event == 'MatchDestination')
	{		
		if(form.MatchOrigin[form.MatchOrigin.selectedIndex].value == form.MatchDestination[form.MatchDestination.selectedIndex].value)
		{
			alert('You can not select a location that is the same as the Starting Point');
			return;
		}
		form.EndDetail.value = form.MatchDestination[form.MatchDestination.selectedIndex].text;
		form.End.value = form.EndDetail.value;
		form.EndGeo.value = form.MatchDestination[form.MatchDestination.selectedIndex].value;
		document.getElementById('end-detail').innerHTML = form.MatchDestination[form.MatchDestination.selectedIndex].text;
	}
	
	if (event == 'SetLocation')
	{		
		form.FormState.value = event;
		vState = 'submit';
	}

	if (event == 'SetMorningTimes')
	{		
	 	form.FromTime.value = '5:00';
		form.FromMeridian[0].checked = true;
		form.ToTime.value = '12:00';
		form.ToMeridian[1].checked = true;
	}

	if (event == 'SetAfternoonTimes')
	{		
	 	form.FromTime.value = '12:00';
		form.FromMeridian[1].checked = true;
		form.ToTime.value = '6:00';
		form.ToMeridian[1].checked = true;
	}

	if (event == 'SetEveningTimes')
	{		
	 	form.FromTime.value = '6:00';
		form.FromMeridian[1].checked = true;
		form.ToTime.value = '5:00';
		form.ToMeridian[0].checked = true;
	}

	if (event == 'SetDayTimes')
	{		
	 	form.FromTime.value = '5:00';
		form.FromMeridian[0].checked = true;
		form.ToTime.value = '4:59';
		form.ToMeridian[0].checked = true;
	}

	if (event == 'SetStops')
	{		
		form.StopCategory[2].checked = true;
	}

	if (event == 'ScheduleLookupSearch')
	{
 		if (form.LineName.value=='')
		{
			vError += 'Please enter a Route or pick from the list';
		}
	}
  
	if (event == 'HeadwaySheet')
	{
 		if (form.LineName.value=='')
		{
			vError += 'Please enter a Route or pick from the list';
		}
		form.FormState.value = event;
	}

	if (event == 'HeadwayRefresh')
	{
		vState = 'return';
	}
	
	if (event == 'HeadwayPrint')
	{
		vState = 'return';
	}

	if (event == 'NextBus')
	{
		vState = 'submit';
	}
	
	if (event == 'GetSchedule')
	{
		vState = 'submit';
	}

	if (event == 'TripPlanningSched')
	{
 		if (form.LineName.value=='')
		{
  			vError += 'Please enter a Route';
		}
	}

	 if (event == 'iLocationLookup')
	 {
		if (form.StartDetail.value=='')
		{
			vError += 'Please enter a location';
		}
	}
 
	 if (event == 'iStopLookup')
	 {
 		if (form.StopNumber.value=='')
		{
			vError += 'Please enter a stop number';
		}
	}

	 if (event == 'iNextBusFind' || event == 'iNextBus' )
	{
 		if (form.StopNumber.value=='')
		{
			vError += 'Please enter a stop number, or use the location form below to find your closest stop';
		}
	}

	if (event == 'TripPlan')
	{
		if (form.StartDetail.value=='')
		{
			/* vError += 'Your trip needs an origin. Check the from.\n'; */
			vError += 'Every trip needs a starting point. Check the "From" address.\n';
		}
		if (form.EndDetail.value=='') 
		{
			/* vError += 'Your trip needs a destination. Check the to.\n'; */
			vError += 'Every trip needs a destination. Check the "To" address.\n';
		}
	
		if (form.EndDetail.value.toUpperCase()==form.StartDetail.value.toUpperCase() && form.StartDetail.value!='') 
		{
			if((form.StartGeo.value != "" && form.StartGeo.value == form.EndGeo.value))		
				vError += "The origin and destination (\"From\" and \"To\" addresses) can't be the same.\n";
		}
		
		
		var bchecked=false;
		
		if (form.Optionsubway.checked == true || form.Optionbus.checked == true || form.Optionstreetcar.checked == true) 
		{
			bchecked=true;
		}
	
		if (bchecked == false) 
		{
			vError += 'Choose at least one method of transport.\n';
		}
	
		if (vError == '')
		{
			form.FormState.value = 'Valid';
		}
		else if(form.EndDetail.value.toUpperCase()==form.StartDetail.value.toUpperCase() && form.StartDetail.value!='')
			vError = vError + '\n\nPlease go back and check your trip.\n\n';
		else
			vError = 'Some parts of the form were left blank.\n\n' + vError + '\n\nPlease go back and check your trip.\n\n';
	
		vState = 'return';
	}
	
	if (vState == 'submit' && vError == '')
	{
		// block the duplicate submits
		if( formSubmitted == 'false')
		{
			formSubmitted = 'true';
			form.submit();
		}
		else
		{
			 return true;
		}
	}
	else if (vState == 'return' && vError == '')
	{ 
		return true;
	}
	else
	{
		 alert(vError);
		 return false;
	}
	
}

function setStartAddOpt(form, addrDetail, addrGeo)
{
	addrDetail = addrDetail.replace("+", "'");	
	addrGeo = addrGeo.replace("+", "'");
	if(addrDetail == document.TripPlan.EndDetail.value)
	{
		alert('You can not select a location that is the same as the Destination');
		return;
	}
	
	document.TripPlan.StartDetail.value = addrDetail;
	document.TripPlan.Start.value = addrDetail;
	document.TripPlan.StartGeo.value = addrGeo;
	document.getElementById('start-detail').innerHTML = addrDetail;
	return true;
}

function setEndAddOpt(form, addrDetail, addrGeo)
{
	addrDetail = addrDetail.replace("+", "'");	
	addrGeo = addrGeo.replace("+", "'");
	
	if(addrDetail == document.TripPlan.StartDetail.value)
	{
		alert('You can not select a location that is the same as the Staring Point');
		return;
	}
	document.TripPlan.EndDetail.value = addrDetail;
	document.TripPlan.End.value = addrDetail;
	document.TripPlan.EndGeo.value = addrGeo;
	document.getElementById('end-detail').innerHTML = addrDetail;
	return true;
}
function setScheduleFormState(form,state)
{
	form.FormState.value = state;
	if (state == 'iHeadwaySheet')
	{
		form.StopHeadersOnly.value = '0';
	}
	else
	{
		form.StopHeadersOnly.value = '1';
	}
}

function setNextStop(id,lonlat)
{
 document.RF.SN.value = id;
 document.RF.GEO.value = lonlat;
 document.RF.submit();
	if (event == "SetNextStop")
	{		
		form.StopId.value = id;
		form.StopGeo.value = lonlat;
		document.RF.submit();
	}
}


function showDate() 
{

var d=new Date()
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var monthname=new Array("Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec.")

document.write(weekday[d.getDay()] + " - ")
document.write(monthname[d.getMonth()])
document.write(" " + d.getDate() + ", ")
document.write(d.getFullYear())
}

function printPage()
{
	if (navigator.appName == "Netscape") 
	{
       	window.print();
	}
	else
	{
	 if (confirm("Print this page?")) window.print();
	}
}


function dayChange(form)
{
	var month = form.month.options[form.month.selectedIndex].value;
	var today= new Date();
	
	var thismonth= today.getMonth();
	var thisyear= today.getFullYear();
	
	if(month <= thismonth)
		thisyear += 1;
	
	form.Date.value = month + '-' + form.Day.options[form.Day.selectedIndex].value + '-' + thisyear;

}

function dateChange(form)
{
	var date = form.Date.value;
	form.month.selectedIndex = date.substring(0,2) - 1;
	form.Day.selectedIndex = date.substring(3,5) - 1;
}


function AccessWCChange()
{
	if(document.getElementById('AccessibilitySel1').checked)
	{
		document.getElementById('Optionstreetcar').checked = false; 
		document.getElementById('Optionstreetcar').disabled = true;
	}
	else
	{
		document.getElementById('Optionstreetcar').disabled = false;
		document.getElementById('Optionstreetcar').checked = true; 
	}

}

function AdjustDayByMonth(form)
{
	var month = form.month.options[form.month.selectedIndex].value;
	var daysel = form.Day.selectedIndex;
	var today= new Date();
	var thisday= today.getDate();
	var thismonth= today.getMonth();
	var thisyear= today.getFullYear();
	
	if(month <= thismonth)
		thisyear += 1;
	
	var dayNum = 0;
	if(month == 4 || month == 6 || month == 9 || month == 11)
	{
		dayNum = 30;
		//alert(form.Day[form.dayNum.selectedIndex].value);
	}
	else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
	{
		dayNum = 31;
	}
	else if (month == 2)
	{
		
		if ( ( (thisyear%4==0)&&(thisyear%100 != 0) ) || (thisyear%400==0) )  // leap year
		{
			dayNum = 29;
		}
		else
			dayNum = 28; 
			
		dayNum = 28;		
	}
	
	for(i=0;i<31;i++){
		form.Day.options[i]=null;
	}

	for (var i=0; i <dayNum ; i++)
	{
		var x= String(i+1);
		if(i < 9)
			x = '0' + x;
		form.Day.options[i] = new Option(x, x);	
	}
	if(daysel < dayNum)
		form.Day.selectedIndex = daysel;
	else
		form.Day.selectedIndex = dayNum-1;
		
	form.Date.value = month + '-' + form.Day.options[form.Day.selectedIndex].value + '-' + thisyear;
}

function expandFirst() {
document.getElementById(expandFirst.arguments[0]).style.display = "inline";
for (var i=1; i<expandFirst.arguments.length; i++) {
document.getElementById(expandFirst.arguments[i]).style.display = "none";
}
}
function expandCollapse() {
for (var i=0; i<expandCollapse.arguments.length; i++) {
var element = document.getElementById(expandCollapse.arguments[i]);
element.style.display = (element.style.display == "none") ? "inline" : "none";
}
}

function show_all_remark(LegNum)
{
	document.getElementById('remark-toggle' + LegNum).className='hide';
	document.getElementById("all-remark" + LegNum).className='no-js-hide';
}

function hide_all_remark(LegNum)
{
	document.getElementById('remark-toggle' + LegNum).className='no-js-hide';
	document.getElementById("all-remark" + LegNum).className='hide';
}

 											 


