function writeDropDown(selname, optionvalues, optionnames, seloptions) {
    var writeval = "<select name='"+selname+"' "+seloptions+">";
    //writeval += "<option value=''>Select Station</option>" ;
    for (var i = 0; i < optionvalues.length; i++) {
	writeval += "<option value='" + optionvalues[i] + "'>" + optionnames[i] + "</option>";
    }
    writeval += "</select>";
    document.writeln(writeval);
}

/*
    Writes an entire select element containing values from the minimum up to (including)
    the maximum.  Put style attribute or other code to pass through in the "passthrough" variable.
*/
function writeSelect(selname, minval, maxval, stepsize, tabind, passthrough) {
    var writeval = "<select name='"+selname+"' tabindex="+tabind+" ";
    if (passthrough != null) writeval += passthrough;
    writeval += ">";
    for (var i = minval; i <= maxval; i += stepsize) {
	writeval += (i < 10) ? "<option value='0"+i+"'>0" : "<option value='"+i+"'>";
      writeval += i;
    }
    writeval += "</select>";
    document.writeln(writeval);

}

// Used for controlling input via forms.
// Sets the specified select element's selection to the specified value
function setSelection(el, val) {
    for (var i = 0; i < el.options.length; i++) {
        if (el.options[i].value == val) {
            el.selectedIndex = i;
            return;
        }
    }
 }


/*
  Ensures that people can't select an invalid day for the specified month/year.
  Call this when the month or year change.
*/

function arry() {}
var x_days_per_month = new arry();
x_days_per_month[1] = 31; x_days_per_month[2] = 29; x_days_per_month[3] = 31;
x_days_per_month[4] = 30; x_days_per_month[5] = 31; x_days_per_month[6] = 30;
x_days_per_month[7] = 31; x_days_per_month[8] = 31; x_days_per_month[9] = 30;
x_days_per_month[10] = 31; x_days_per_month[11] = 30; x_days_per_month[12] = 31;

// returns days in month
function daysInMonth(m, y) {
  if (m == 2) return getFebDays(y);
  else return x_days_per_month[m];
}

function repairDays(mm, dd, yyyy) {
  var m = parseInt(mm.options[mm.selectedIndex].value);
  var y = parseInt(yyyy.options[yyyy.selectedIndex].value);
  //alert("Month : "+m +" Year : "+y);
  if ((m <= 0) || (y <= 0)) return;
  var d_sel = dd.selectedIndex;
  var d_max_valid = daysInMonth(m, y);
  var offset = 1;
  if (parseInt(dd.options[0].value) <= 0) offset = 0;
  for (var i = 31; i >= 28; i--) {
    if (i > d_max_valid) {
      dd.options[i-offset] = null;
    } else 
      dd.options[i-offset] = new Option(""+i, ""+i);
  }
  if (dd.options[d_sel] != null)
    dd.selectedIndex = d_sel;
  else
    dd.selectedIndex = dd.options.length - 1;
  
}


function initNoCookie() {
    var today = new Date();
    
    var soon = new Date(today.getTime()+ (4.5)*60*1000); // round to nearest 5 >= now
    var hrs = soon.getHours();
    var mins = soon.getMinutes();
    mins = mins - (mins % 5);
    
    document.frmBusSchedulesP2P.Suffix[1].selected = (hrs >= 12);
    document.frmBusSchedulesP2P.Suffix[0].selected = (hrs < 12);

    setSelection(document.frmBusSchedulesP2P.Hour, ((hrs != 0 && hrs != 12 && hrs != 24) ? hrs%12 : 12));
    setSelection(document.frmBusSchedulesP2P.Minute, mins);

    setSelection(document.frmBusSchedulesP2P.Month, (1+today.getMonth()));
    setSelection(document.frmBusSchedulesP2P.Day, today.getDate());
    setSelection(document.frmBusSchedulesP2P.Year, today.getYear());
    repairDays(document.frmBusSchedulesP2P.Month, document.frmBusSchedulesP2P.Day, document.frmBusSchedulesP2P.Year);
}

function doSubmit() {
   if(document.frmBusSchedulesP2P.selOrigin.selectedIndex == document.frmBusSchedulesP2P.selDestination.selectedIndex){
   	alert("Please select different Origin and Destination points to view bus schedules");
   	return false;
   }
   if(document.frmBusSchedulesP2P.Time.value == '' || !isValidTime(document.frmBusSchedulesP2P.Time.value)){
   	alert("Please enter the time of travel in the format of HH:MM");
   	return false;
   }
   var timeParts = document.frmBusSchedulesP2P.Time.value.split(":");
   document.frmBusSchedulesP2P.Hour.value = timeParts[0];
   document.frmBusSchedulesP2P.Minute.value = timeParts[1];

   //document.frmBusSchedulesP2P.Center.value = (document.frmBusSchedulesP2P.center[0].checked? document.frmBusSchedulesP2P.center[0].value : document.frmBusSchedulesP2P.center[1].value);
   document.frmBusSchedulesP2P.OriginDescription.value = document.frmBusSchedulesP2P.selOrigin[document.frmBusSchedulesP2P.selOrigin.selectedIndex].text;
   document.frmBusSchedulesP2P.DestDescription.value = document.frmBusSchedulesP2P.selDestination[document.frmBusSchedulesP2P.selDestination.selectedIndex].text;
}

  var form = $("#frm_bus_schedules");
  
    //disable sumbit on enter 
  form.keypress(function (evt) { 
     if (evt.keyCode == 13) {
     	return false;
     } 
  });

   function initialize() {
	$('.datepicker').dpSetSelected(new Date().asString()); //select current date
  }

  function isValidTime(time) {
	var pattern = new RegExp(/^(0[1-9]|1[012]|[0-9]):([0-5][0-9])$/);
	return pattern.test(time);
  }

  $(document).ready(function() {	
	initialize();
	setTime();  
 	form.submit(function(e){  
		var selectedBusRoute = $("select#selBusRoute").val();
		if(selectedBusRoute == ""){
				alert("Please select Bus Route(s).");
				return false;
		}
		
	});
 
 });
 
 
   function getBusSchedules(){
  	 var selectedBusRoute = $("select#selBusRoute").val();
  	 if(selectedBusRoute == "" || selectedBusRoute == null){
		alert("Please select Bus Route(s).");
		return false;
	}
  	 
  	 $.ajax({
		  url: "/sf/sf_servlet.srv?hdnPageAction=BusSchedulesLookupTo&selBusRoute="+selectedBusRoute,
		  cache: true,
		  success: function(html){
		    $("#BusSchedulesData").empty();
		    $("#BusSchedulesData").append("<a href='#' onClick='$(\"#BusSchedulesData\").empty();'>close</a>");
		    $("#BusSchedulesData").append(html);
		    $("#BusSchedulesData").append("<a href='#' onClick='$(\"#BusSchedulesData\").empty();'>close</a>");		    
		    $("#BusSchedulesData").append(" <p>* Upcoming schedules are provided when new schedules will take effect on the bus route. </p>");
		    $("#BusSchedulesData").append(" <p>View <a href='/sa/sa_servlet.srv?hdnPageAction=TravelAlertsTo&rel=Bus#BusTab'>bus alerts and advisories</a> that may affect your trip. </p>");
		    //$("#BusSchedulesData").append(" <p>* To view a bus timetable in PDF format, select the route number above. </p>");
		  }
		});
	return;
  }
