﻿/* Common Functions */

function openWin(url, w, h) {
    var height = screen.availHeight / 2;
    var width = screen.availWidth / 2;
    if (w) width = w;
    if (h) height = h;
    var options = "width=" + width + ",height=" + height + ",scrollbars=yes,resizable=yes,toolbar=no";
    popupwindow = window.open(url, 'popupwindow', options);
    popupwindow.focus();
}

function openStationList() {
    stations = window.open('http://www.amtrak.com/html/stations_A.html', '0', 'directories=0,dependent=1,menubar=0,top=20,left=20,width=530,height=450,scrollbars=1');
    if (window.focus) {
        stations.focus();
    }
}

function numDays(y, m) {

    return (m == 3 || m == 5 || m == 8 || m == 10) ? 30 : ((m == 1) && (((y % 4 == 0) && y % 100 != 0) || y % 400 == 0)) ? 29 : (m == 1) ? 28 : 31;
}

function updateField(fieldValue1, fieldValue2, fieldValue3) {
    var fieldValue = fieldValue1 + ", " + fieldValue2 + " (" + fieldValue3 + ")";
    document[formname][fieldName].value = fieldValue;
}

/* FareFinder Functions */

function resetFareFinderDays() {

    var formMonthDropdown = document.getElementById("_farefinderdepmonthyear");
    var optionValue = "";
    if (formMonthDropdown[0].selected) {

        //optionValue = formMonthDropdown.options[0].value;
        optionValue = null;
    } else {

        optionValue = formMonthDropdown.options[1].value;
    }

    setFareFinderDate(optionValue);
}

function setFareFinderDate(ddValue, calday) {

    monthsShort = new Array;
    monthsShort[0] = 'Jan';
    monthsShort[1] = 'Feb';
    monthsShort[2] = 'Mar';
    monthsShort[3] = 'Apr';
    monthsShort[4] = 'May';
    monthsShort[5] = 'Jun';
    monthsShort[6] = 'Jul';
    monthsShort[7] = 'Aug';
    monthsShort[8] = 'Sep';
    monthsShort[9] = 'Oct';
    monthsShort[10] = 'Nov';
    monthsShort[11] = 'Dec';

    var now = new Date();
    var curMonth = now.getMonth();
    var curYear = now.getYear();
    var curDate = now.getDate();
    var dispMonth = curMonth;
    var dispYear = curYear;
    var monthPos = 0;
    var dayPos = 0;
    var firstMonth = curMonth;
    var lastMonth = curMonth;
    var origFirstMonth = curMonth;
    var origLastMonth = curMonth;
    var newMonth = 0;

    var formMonthDropdown = document.getElementById("_farefinderdepmonthyear").options;
    var formDayDropdown = document.getElementById("_farefinderdepday").options;

    if (formMonthDropdown.selectedIndex != 0) {

        curMonth = formMonthDropdown.selectedIndex - 1;
    }

    if (curYear < 1000) {
        curYear += 1900;
    }
    var firstMonthYear = curYear;
    var lastMonthYear = curYear;
    var dayOffset = 31;
    var firstDay = curDate - dayOffset;
    var lastDay = curDate + dayOffset;
    if (ddValue != null) {

        var monthYear = new Array();
        monthYear = ddValue.split("-");
        dispMonth = (monthYear.pop() - 0) - 1;
        dispYear = monthYear.pop();
    }
    if (curDate < dayOffset) {

        monthPos = 1;
        firstMonth = curMonth - 1;

        if (firstMonth < 0) {
            firstMonth = 11; firstMonthYear--;
        }
        origFirstMonth = firstMonth;
    }
    if (curDate > (numDays(curYear, curMonth) - dayOffset)) {

        monthPos = 0;
        lastMonth = curMonth + 1;

        if (lastMonth > 11) {
            lastMonth = 0;
            lastMonthYear++;
        }
        origLastMonth = lastMonth;
    }

    for (i = 30; i >= 0; i--) {

        formDayDropdown[i] = null;
    }

    if (dispMonth == null || dispMonth == curMonth) {

        if (firstDay < 0) firstDay = 1;
        if (lastDay > numDays(curYear, curMonth)) lastDay = numDays(curYear, curMonth);
    } else {

        var futureMonth = false;
        var pastMonth = false;

        if (dispYear > curYear) {

            futureMonth = true;
        } else {

            if (dispYear < curYear) {

                pastMonth = true;
            } else {

                if (dispMonth > curMonth) {
                    futureMonth = true;
                } else pastMonth = true;
            }
        }

        if (futureMonth) {

            monthPos = 1;
            firstDay = 1;
            lastDay = numDays(curYear, curMonth);
        } else if (pastMonth) {

            monthPos = 0;
            firstDay += numDays(dispYear, dispMonth);
            lastDay = numDays(dispYear, dispMonth);
        }
    }

    for (i = 0; i < lastDay - firstDay + 1; i++) {

        day = firstDay + i;
        if (calday) {

            if (day == calday) dayPos = i;
        } else {

            if (day == curDate) {
                dayPos = i;
            }
        }

        document.getElementById("_farefinderdepday").options[i] = new Option(day, day);
    }

    formMonthDropdown[curMonth + 1].selected = true;

    document.getElementById("_farefinderdepday").options[dayPos].selected = true;
}

function setFareFinderRoundTrip(fieldName) {

    if (fieldName == "_farefinderdestination") {

        if (document.forms[1]._farefinderdestination.value != "") {

            document.forms[1]._farefindertripType[1].checked = true;
        } else {

            if (document.forms[1]._farefinderretmonthyear.selectedIndex == 0 && document.forms[1]._farefinderretday.selectedIndex == 0 && document.forms[1]._farefinderrethourmin.selectedIndex == 0) {

                document.forms[1]._farefindertripType[0].checked = true;
            }
        }
    } else {

        if (document.forms[1][fieldName].selectedIndex > 0) {

            document.forms[1]._farefindertripType[1].checked = true;
        } else {

            if (document.forms[1]._farefinderretmonthyear.selectedIndex == 0 && document.forms[1]._farefinderretday.selectedIndex == 0 && document.forms[1]._farefinderrethourmin.selectedIndex == 0) {

                document.forms[1]._farefindertripType[0].checked = true;
            }
        }
    }
}

function setFareFinderReturnDate() {

    if (document.forms[1]._farefinderretmonthyear && document.forms[1]._farefinderretmonthyear.selectedIndex <= 0) {

        var now = new Date();
        document.forms[1]._farefinderretmonthyear.options[now.getMonth() + 1].selected = true;
        document.forms[1]._farefinderretday.options[now.getDate()].selected = true;
    }
}

function clearFareFinderReturnDate() {

    document.forms[1]._farefinderretmonthyear.options[0].selected = true;
    document.forms[1]._farefinderretday.options[0].selected = true;
}

function submitFareFinderForm() {

    var submitForm = document.forms["roundTripInputForm"];
    if (document.forms[1]._farefindertripType[0].checked) {

        submitForm._tripType[1].checked = false;
        submitForm._tripType[0].checked = true;
    } else {

        submitForm._tripType[0].checked = false;
        submitForm._tripType[1].checked = true;
    }

    submitForm._origin.value = document.forms[1]._farefinderorigin.value;

    var depmonthyear = document.forms[1]._farefinderdepmonthyear.value;
    for (i = 0; i < submitForm._depmonthyear.length; i++) {

        if (submitForm._depmonthyear[i].value == depmonthyear) {

            submitForm._depmonthyear[i].selected = true;
        }
    }

    var depday = document.forms[1]._farefinderdepday.value;
    if (depday < 10 && depday.length < 2) {
        depday = "0" + depday;
    }
    for (i = 0; i < submitForm._depday.length; i++) {

        if (submitForm._depday[i].value == depday) {

            submitForm._depday[i].selected = true;
        }
    }

    var dephourmin = document.forms[1]._farefinderdephourmin.value;
    for (i = 0; i < submitForm._dephourmin.length; i++) {

        if (submitForm._dephourmin[i].value == dephourmin) {

            submitForm._dephourmin[i].selected = true;
        }
    }

    submitForm._destination.value = document.forms[1]._farefinderdestination.value;

    var retmonthyear = document.forms[1]._farefinderretmonthyear.value;
    for (i = 0; i < submitForm._retmonthyear.length; i++) {

        if (submitForm._retmonthyear[i].value == retmonthyear) {

            submitForm._retmonthyear[i].selected = true;
        }
    }

    var retday = document.forms[1]._farefinderretday.value;
    if (retday < 10 && retday.length < 2) {
        retday = "0" + retday;
    }
    for (i = 0; i < submitForm._retday.length; i++) {

        if (submitForm._retday[i].value == retday) {

            submitForm._retday[i].selected = true;
        }
    }

    var rethourmin = document.forms[1]._farefinderrethourmin.value;
    for (i = 0; i < submitForm._rethourmin.length; i++) {

        if (submitForm._rethourmin[i].value == rethourmin) {

            submitForm._rethourmin[i].selected = true;
        }
    }

    submitForm._adults.value = document.getElementById("_farefinderadults").value;
    submitForm._children.value = document.getElementById("_farefinderchildren").value;
    submitForm._infants.value = document.getElementById("_farefinderinfants").value;
    submitForm.submit();
}

/* Train Status Functions */
function resetTrainStatusDays() {

    var formMonthDropdown = document.getElementById("_trainstatusdepmonthyear");
    var optionValue = "";
    if (formMonthDropdown[0].selected) {

        optionValue = null;
    } else {

        optionValue = formMonthDropdown.options[1].value;
    }

    setTrainStatusDate(optionValue);
}

function setTrainStatusDate(ddValue, calday) {

    monthsShort = new Array;
    monthsShort[0] = 'Jan';
    monthsShort[1] = 'Feb';
    monthsShort[2] = 'Mar';
    monthsShort[3] = 'Apr';
    monthsShort[4] = 'May';
    monthsShort[5] = 'Jun';
    monthsShort[6] = 'Jul';
    monthsShort[7] = 'Aug';
    monthsShort[8] = 'Sep';
    monthsShort[9] = 'Oct';
    monthsShort[10] = 'Nov';
    monthsShort[11] = 'Dec';

    var now = new Date();
    var curMonth = now.getMonth();
    var curYear = now.getYear();
    var curDate = now.getDate();
    var dispMonth = curMonth;
    var dispYear = curYear;
    var monthPos = 0;
    var dayPos = 0;
    var firstMonth = curMonth;
    var lastMonth = curMonth;
    var origFirstMonth = curMonth;
    var origLastMonth = curMonth;
    var newMonth = 0;

    var formMonthDropdown = document.getElementById("_trainstatusdepmonthyear").options;
    var formDayDropdown = document.getElementById("_trainstatusdepday").options;

    if (curYear < 1000) {
        curYear += 1900;
    }
    var firstMonthYear = curYear;
    var lastMonthYear = curYear;
    var dayOffset = 5;
    var firstDay = curDate - dayOffset;
    var lastDay = curDate + dayOffset;
    if (ddValue != null) {


        var monthYear = new Array();
        monthYear = ddValue.split("-");
        dispMonth = (monthYear.pop() - 0) - 1;
        dispYear = monthYear.pop();
    }
    if (curDate < dayOffset) {

        monthPos = 1;
        firstMonth = curMonth - 1;

        if (firstMonth < 0) {
            firstMonth = 11; firstMonthYear--;
        }
        origFirstMonth = firstMonth;
    }
    if (curDate > (numDays(curYear, curMonth) - dayOffset)) {

        monthPos = 0;
        lastMonth = curMonth + 1;

        if (lastMonth > 11) {
            lastMonth = 0;
            lastMonthYear++;
        }
        origLastMonth = lastMonth;
    }

    for (var i = 11; i >= 0; i--) {

        formMonthDropdown[i] = null;
    }

    var addZero = "";

    if (origFirstMonth + 1 < 10) addZero = "0";

    var optionValue = firstMonthYear + "-" + addZero + (origFirstMonth + 1);
    var optionText = monthsShort[origFirstMonth];
    document.getElementById("_trainstatusdepmonthyear").options[0] = new Option(optionText, optionValue);

    if (firstMonth != lastMonth) {

        addZero = "";
        if (origLastMonth + 1 < 10) addZero = "0";
        optionValue = lastMonthYear + "-" + addZero + (origLastMonth + 1);
        optionText = monthsShort[origLastMonth];
        document.getElementById("_trainstatusdepmonthyear").options[1] = new Option(optionText, optionValue);
    }

    for (i = 30; i >= 0; i--) {

        formDayDropdown[i] = null;
    }

    if (dispMonth == null || dispMonth == curMonth) {

        if (firstDay < 0) firstDay = 1;
        if (lastDay > numDays(curYear, curMonth)) lastDay = numDays(curYear, curMonth);
    } else {

        var futureMonth = false;
        var pastMonth = false;

        if (dispYear > curYear) {

            futureMonth = true;
        } else {

            if (dispYear < curYear) {

                pastMonth = true;
            } else {

                if (dispMonth > curMonth) {
                    futureMonth = true;
                } else pastMonth = true;
            }
        }

        if (futureMonth) {

            monthPos = 1;
            firstDay = 1;
            lastDay -= numDays(curYear, curMonth);
        } else if (pastMonth) {

            monthPos = 0;
            firstDay += numDays(dispYear, dispMonth);
            lastDay = numDays(dispYear, dispMonth);
        }
    }

    for (i = 0; i < lastDay - firstDay + 1; i++) {

        day = firstDay + i;
        if (calday) {

            if (day == calday) dayPos = i;
        } else {

            if (day == curDate) {
                dayPos = i;
            }
        }

        document.getElementById("_trainstatusdepday").options[i] = new Option(day, day);
    }

    formMonthDropdown[monthPos].selected = true;

    document.getElementById("_trainstatusdepday").options[dayPos].selected = true;
}

function submitTrainStatusForm() {

    var submitForm = document.forms["trainStatusForm"];

    submitForm._origin.value = document.getElementById("_trainstatusorigin").value;
    submitForm._destination.value = document.getElementById("_trainstatusdestination").value;
    submitForm._trainNumber.value = document.getElementById("_trainstatustrainNumber").value;

    var depmonthyear = document.forms[1]._trainstatusdepmonthyear.value;
    for (i = 0; i < submitForm._depmonthyear.length; i++) {

        if (submitForm._depmonthyear[i].value == depmonthyear) {

            submitForm._depmonthyear[i].selected = true;
        }
    }

    var depday = document.forms[1]._trainstatusdepday.value;
    if (depday < 10 && depday.length < 2) {
        depday = "0" + depday;
    }
    for (i = 0; i < submitForm._depday.length; i++) {

        if (submitForm._depday[i].value == depday) {

            submitForm._depday[i].selected = true;
        }
    }

    var dephourmin = document.forms[1]._trainstatusdephourmin.value;
    for (i = 0; i < submitForm._dephourmin.length; i++) {

        if (submitForm._dephourmin[i].value == dephourmin) {

            submitForm._dephourmin[i].selected = true;
        }
    }

    if (document.forms[1]._trainstatussearchType[0].checked) {

        submitForm._searchType[1].checked = false;
        submitForm._searchType[0].checked = true;
    } else {

        submitForm._searchType[0].checked = false;
        submitForm._searchType[1].checked = true;
    }

    submitForm.submit();
}