﻿
var currentLocation = "";


function submit_CustomPlanner(browserType,
                              startAddr,
                              endAddr,
                              Arrdep,
                              hour,
                              minute,
                              ampm,
                              selectedDate,
                              minimize,
                              walkdistance,
                              mode,
                              lineStart,
                              lineEnd,
                              accessible,
                              OriginCoors,
                              DestinationCoors,
                              startServiceType,
                              startTrainType,
                              startBorough,
                              endServiceType,
                              endTrainType,
                              endBorough,
                              walkincrease,
                              maxinitialwait) {
    var objJSON = createJSONObj(browserType,
                                  startAddr,
                                  endAddr,
                                  Arrdep,
                                  hour,
                                  minute,
                                  ampm,
                                  selectedDate,
                                  minimize,
                                  walkdistance,
                                  mode,
                                  lineStart,
                                  lineEnd,
                                  accessible,
                                  OriginCoors,
                                  DestinationCoors,
                                  startServiceType,
                                  startTrainType,
                                  startBorough,
                                  endServiceType,
                                  endTrainType,
                                  endBorough,
                                  walkincrease,
                                  maxinitialwait);

    var stringifyJSON = JSON.stringify(objJSON);
    sendCustomPlannerRequest(stringifyJSON);
}



function submit_Point2Point(browserType,
                              startAddr,
                              endAddr,
                              Arrdep,
                              hour,
                              minute,
                              ampm,
                              selectedDate,
                              minimize,
                              walkdistance,
                              mode,
                              lineStart,
                              lineEnd,
                              accessible,
                              OriginCoors,
                          DestinationCoors) {


    var objJSON = createJSONObj(browserType,
                                  startAddr,
                                  endAddr,
                                  Arrdep,
                                  hour,
                                  minute,
                                  ampm,
                                  selectedDate,
                                  minimize,
                                  walkdistance,
                                  mode,
                                  lineStart,
                                  lineEnd,
                                  accessible,
                                  OriginCoors,
                          DestinationCoors);

    var stringifyJSON = JSON.stringify(objJSON);
    sendPoint2PointRequest(stringifyJSON);
}

function submit_ServiceintheArea(browserType,
                              startAddr,
                              endAddr,
                              Arrdep,
                              hour,
                              minute,
                              ampm,
                              selectedDate,
                              minimize,
                              walkdistance,
                              mode,
                              lineStart,
                              lineEnd,
                              accessible,
                              OriginCoors,
                          DestinationCoors) {


    var objJSON = createJSONObj(browserType,
                                  startAddr,
                                  endAddr,
                                  Arrdep,
                                  hour,
                                  minute,
                                  ampm,
                                  selectedDate,
                                  minimize,
                                  walkdistance,
                                  mode,
                                  lineStart,
                                  lineEnd,
                                  accessible,
                                  OriginCoors,
                          DestinationCoors);

    var stringifyJSON = JSON.stringify(objJSON);
    sendServiceintheAreaRequest(stringifyJSON);
}



function createJSONObj(browserType,
                          startAddr,
                          endAddr,
                          Arrdep,
                          hour,
                          minute,
                          ampm,
                          selectedDate,
                          minimize,
                          walkdistance,
                          mode,
                          lineStart,
                          lineEnd,
                          accessible,
                          OriginCoors,
                          DestinationCoors,
                          startServiceType,
                          startTrainType,
                          startBorough,
                          endServiceType,
                          endTrainType,
                          endBorough,
                          walkincrease,
                          maxinitialwait) {

    var JOSONCustObj = {
        RequestDevicename: browserType,
        OriginInput: startAddr,
        DestinationInput: endAddr,
        Arrdep: Arrdep,
        Hour: hour,
        Minute: minute,
        Ampm: ampm,
        InputDate: selectedDate,
        Minimize: minimize,
        Walkdist: walkdistance,
        Mode: mode,
        LineStart: lineStart,
        LineEnd: lineEnd,
        Accessible: accessible,
        OriginCoordinates: OriginCoors,
        DestinationCoordinates: DestinationCoors,
        LocationType: "",
        StartServiceType: startServiceType,
        StartTrainType: startTrainType,
        StartBorough: startBorough,
        EndServiceType: endServiceType,
        EndTrainType: endTrainType,
        EndBorough: endBorough,
        Walkincrease: walkincrease,
        Maxinitialwait: maxinitialwait
    };

    return JOSONCustObj;
}


function sendCustomPlannerRequest(stringifiedJSONString) {

    // Request String
    var randomnumber = Math.floor(Math.random() * 11);

    //The encodeURIComponent() function encodes a URI component.This function encodes special characters. 
    //In addition, it encodes the following characters: , / ? : @ & = + $ #
    var reqStr = "?jsonpacket=" + encodeURIComponent(stringifiedJSONString) + "&rand=" + randomnumber;
    //Handler URL
    var handlerURL = "../../handler/customplannerHandler.ashx";

    var callbackFn = ShowServerResultsCust;

    var ajax_caller = new AjaxCaller();
    ajax_caller.postDataReturnText(handlerURL + reqStr, '', callbackFn);
}

function sendPoint2PointRequest(stringifiedJSONString) {//debugger;
    // Request String
    var randomnumber = Math.floor(Math.random() * 11);

    //The encodeURIComponent() function encodes a URI component.This function encodes special characters. 
    //In addition, it encodes the following characters: , / ? : @ & = + $ #
    var reqStr = "?jsonpacket=" + encodeURIComponent(stringifiedJSONString) + "&rand=" + randomnumber;
    //Handler URL
    var handlerURL = "../../handler/Point2Point.ashx";

    var callbackFn = ShowServerResultsCust;

    var ajax_caller = new AjaxCaller();
    ajax_caller.postDataReturnText(handlerURL + reqStr, '', callbackFn);


}


function sendServiceintheAreaRequest(stringifiedJSONString) {//debugger;
    // Request String
    var randomnumber = Math.floor(Math.random() * 11);

    //The encodeURIComponent() function encodes a URI component.This function encodes special characters. 
    //In addition, it encodes the following characters: , / ? : @ & = + $ #
    var reqStr = "?jsonpacket=" + encodeURIComponent(stringifiedJSONString) + "&rand=" + randomnumber;
    //Handler URL

    var handlerURL = "../../handler/ServiceintheArea.ashx";

    var callbackFn = ShowServerResultsCust;

    var ajax_caller = new AjaxCaller();
    ajax_caller.postDataReturnText(handlerURL + reqStr, '', callbackFn);

}




//call back function of custom planner
function ShowServerResultsCust(response) {
    if (document.getElementById("ajaxLoadingDiv")) {
        document.getElementById("ajaxLoadingDiv").style.display = "none"
    }

    if (g_BrowserType == "MODIFY_TRIP") {

        try {
            eval(response);
        }
        catch (err) {
            //to do
        }

        if (jsonObj) {

            document.getElementById('ModifyTripDiv').innerHTML = jsonObj.OutHTML;
            document.getElementById('DIVWalkingDirections').innerHTML = jsonObj.MapLegDirections;

        }
        showSubmitButton();

    }
    else {
        window.location = response;
    }
}




function InitializeTripplanner() {//debugger;
   

    if (document.getElementById('trainStart')) {
        document.getElementById('trainStart').disabled = false;
        document.getElementById('trainEnd').disabled = false;
        document.getElementById('trainStart').checked = true;
        document.getElementById('trainEnd').checked = true;
    }


    if (jsonObj != null && jsonObj != "") {

        if (jsonObj.LineStart != "") {
            switch (jsonObj.StartServiceType) {
                case "train":
                    {
                        document.getElementById('trainStart').checked = true;
                        document.getElementById('SelectStartTrainType').value = jsonObj.StartTrainType;
                        switch (jsonObj.StartTrainType) {
                            case "subway":
                                document.getElementById('SelectStartTrainType').selectedIndex = 0; break;
                            case "LIRR":
                                document.getElementById('SelectStartTrainType').selectedIndex = 1; break;
                            case "MNR":
                                document.getElementById('SelectStartTrainType').selectedIndex = 2; break;
                        }
                        document.getElementById('tdStartTrainTypeSelection').style.display = "block";
                        document.getElementById('tdStartBoroSelection').style.display = "none";
                        break
                    }
                case "bus":
                    {
                        document.getElementById('busStart').checked = true;
                        document.getElementById('SelectStartBoro').value = jsonObj.StartBorough;
                        switch (jsonObj.StartBorough) {
                            case "MN":
                                document.getElementById('SelectStartBoro').selectedIndex = 0; break;
                            case "BK":
                                document.getElementById('SelectStartBoro').selectedIndex = 1; break;
                            case "BX":
                                document.getElementById('SelectStartBoro').selectedIndex = 2; break;
                            case "SI":
                                document.getElementById('SelectStartBoro').selectedIndex = 3; break;
                            case "LI":
                                document.getElementById('SelectStartBoro').selectedIndex = 4; break;
                        }
                        document.getElementById('tdStartTrainTypeSelection').style.display = "none";
                        document.getElementById('tdStartBoroSelection').style.display = "block";
                        break
                    }
                case "Xbus":
                    {
                        document.getElementById('XbusStart').checked = true;
                        document.getElementById('SelectStartBoro').value = jsonObj.StartBorough;
                        switch (jsonObj.StartBorough) {
                            case "MN":
                                document.getElementById('SelectStartBoro').selectedIndex = 0; break;
                            case "BK":
                                document.getElementById('SelectStartBoro').selectedIndex = 1; break;
                            case "BX":
                                document.getElementById('SelectStartBoro').selectedIndex = 2; break;
                            case "SI":
                                document.getElementById('SelectStartBoro').selectedIndex = 3; break;
                            case "LI":
                                document.getElementById('SelectStartBoro').selectedIndex = 4; break;
                        }

                        document.getElementById('tdStartTrainTypeSelection').style.display = "none";
                        document.getElementById('tdStartBoroSelection').style.display = "block";
                        break
                    }
            } //end switch

        }
        if (jsonObj.LineEnd != "") {
            switch (jsonObj.EndServiceType) {
                case "train":
                    {
                        document.getElementById('trainEnd').checked = true;
                        document.getElementById('SelectEndTrainType').value = jsonObj.EndTrainType;
                        switch (jsonObj.EndTrainType) {
                            case "subway":
                                document.getElementById('SelectEndTrainType').selectedIndex = 0; break;
                            case "LIRR":
                                document.getElementById('SelectEndTrainType').selectedIndex = 1; break;
                            case "MNR":
                                document.getElementById('SelectEndTrainType').selectedIndex = 2; break;
                        }

                        document.getElementById('tdEndTrainTypeSelection').style.display = "block";
                        document.getElementById('tdEndBoroSelection').style.display = "none";
                        break
                    }
                case "bus":
                    {
                        document.getElementById('busEnd').checked = true;
                        document.getElementById('SelectEndBoro').value = jsonObj.EndBorough;
                        switch (jsonObj.EndBorough) {
                            case "MN":
                                document.getElementById('SelectEndBoro').selectedIndex = 0; break;
                            case "BK":
                                document.getElementById('SelectEndBoro').selectedIndex = 1; break;
                            case "BX":
                                document.getElementById('SelectEndBoro').selectedIndex = 2; break;
                            case "QN":
                                document.getElementById('SelectEndBoro').selectedIndex = 3; break;
                            case "SI":
                                document.getElementById('SelectEndBoro').selectedIndex = 4; break;
                            case "LI":
                                document.getElementById('SelectEndBoro').selectedIndex = 5; break;
                        }
                        document.getElementById('tdEndTrainTypeSelection').style.display = "none";
                        document.getElementById('tdEndBoroSelection').style.display = "block";
                        break
                    }
                case "Xbus":
                    {
                        document.getElementById('XbusEnd').checked = true;
                        document.getElementById('SelectEndBoro').value = jsonObj.EndBorough;
                        switch (jsonObj.EndBorough) {
                            case "MN":
                                document.getElementById('SelectEndBoro').selectedIndex = 0; break;
                            case "BK":
                                document.getElementById('SelectEndBoro').selectedIndex = 1; break;
                            case "BX":
                                document.getElementById('SelectEndBoro').selectedIndex = 2; break;
                            case "QN":
                                document.getElementById('SelectEndBoro').selectedIndex = 3; break;
                            case "SI":
                                document.getElementById('SelectEndBoro').selectedIndex = 4; break;
                            case "LI":
                                document.getElementById('SelectEndBoro').selectedIndex = 5; break;
                        }
                        document.getElementById('tdEndTrainTypeSelection').style.display = "none";
                        document.getElementById('tdEndBoroSelection').style.display = "block";
                        break
                    }
            } //end switch

        }
    } //end if jason == null

    if (document.getElementById('trainStart')) {

        FillTrainOrBusRoutes('s');
        if (jsonObj != null && jsonObj != "" && jsonObj.LineStart != "") {
            document.getElementById('lineStart').value = jsonObj.LineStart;

        }
        FillTrainOrBusRoutes('e');
        if (jsonObj != null && jsonObj != "" && jsonObj.LineEnd != "") {
            document.getElementById('lineEnd').value = jsonObj.LineEnd;
        }
    }

}


function InitializeModifyTrip() {

    useFromSuggestion = false
    useToSuggestion = false

    document.getElementById('xmodeR').checked = true
    document.getElementById('xmodeB').checked = true
    document.getElementById('xmodeX').checked = false
    document.getElementById('xmodeCT').checked = true

    if (jsonObj) {
        fromCoordinates = jsonObj.OriginCoordinates;
        toCoordinates = jsonObj.DestinationCoordinates;
        //To do
        document.getElementById('ddlHour').selectedIndex = jsonObj.Hour - 1
        document.getElementById('ddlMinute').selectedIndex = jsonObj.Minute

        if (jsonObj.Ampm == "am") {
            document.getElementById('ddlampm').selectedIndex = 0
        }
        else {
            document.getElementById('ddlampm').selectedIndex = 1
        }


        document.getElementById(calDateID).value = jsonObj.InputDate

        if (document.getElementById('ddlminimize')) {
            document.getElementById('ddlminimize').value = jsonObj.Minimize
        }
        if (document.getElementById('ddlWalkdistance')) {
            document.getElementById('ddlWalkdistance').value = jsonObj.Walkdist
        }

        if (jsonObj.Arrdep != null && jsonObj.Arrdep != "") {
            document.getElementById('DepId').checked = false;
            document.getElementById('ArrId').checked = false;
            if (jsonObj.Arrdep == "D") {
                document.getElementById('DepId').checked = true;
            }
            if (jsonObj.Arrdep == "A") {
                document.getElementById('ArrId').checked = true;
            }
        }

        if (jsonObj.Mode != null && jsonObj.Mode != "") {
            document.getElementById('xmodeR').checked = false;
            document.getElementById('xmodeB').checked = false;
            document.getElementById('xmodeX').checked = false;
            document.getElementById('xmodeCT').checked = false;

            if (jsonObj.Mode.indexOf("R") > -1) {
                document.getElementById('xmodeR').checked = true;
            }
            if (jsonObj.Mode.indexOf("B") > -1) {
                document.getElementById('xmodeB').checked = true;
            }
            if (jsonObj.Mode.indexOf("X") > -1) {
                document.getElementById('xmodeX').checked = true;
            }
            if (jsonObj.Mode.indexOf("C") > -1) {
                document.getElementById('xmodeCT').checked = true;
            }
        }
        if (jsonObj.Accessible == "Y") {
            document.getElementById('accessibleChkbox').checked = true;
        }


        //start map calls clearForm that clears these values so set these after calling startmap()
        useFromSuggestion = false;
        useToSuggestion = false;

        if (jsonObj.OriginCoordinates != "" && (jsonObj.OriginCoordinates).split("$")[2] == "Current Location") {
            jsonObj.OriginCoordinates = ""
            jsonObj.OriginInput = ""
        }

        if (jsonObj.OriginCoordinates != "") {
            useFromSuggestion = true;
        }
        document.getElementById('txtOriginInput').value = jsonObj.OriginInput

        if (jsonObj.DestinationCoordinates != "")
            useToSuggestion = true;
        if (jsonObj.DestinationInput != null && jsonObj.DestinationInput != "") {
            document.getElementById('txtDestinationInput').value = jsonObj.DestinationInput
        }
        // When request is coming from MTA home page we fill result page and check if cordinate empty
        // then only call submitaddress
        if (jsonObj.RequestDevicename == "EXTERNAL-PAGE") {
            if (jsonObj.OriginCoordinates == "" || jsonObj.DestinationCoordinates == "") {
                submitAddresses();
            }
        }
        //calling OpenClose after the page loads so that it closes the modify trip box
        //if there is exception then do not close, so do not call this function. Default is open
        if (jsonObj.RequestDevicename != "IPHONE") {
            if (jsonObj.TpException != "YES") {
                OpenClose('ModifyTripContent', 'ModifyTripText');
            }
        }
    }

    //if currentLocation is set from iphone use it as start address
    if (currentLocation != "") {
        useFromSuggestion = true;
        document.getElementById('txtOriginInput').value = 'current Location'
        fromCoordinates = currentLocation;
    }
}

function InitializePoint2Point() {
    //startMap();
    if (jsonObj != null && jsonObj != "") {
        //startmap calls clearForm that clears these values so set these after calling startmap()

        fromCoordinates = jsonObj.OriginCoordinates;
        toCoordinates = jsonObj.DestinationCoordinates;

        document.getElementById('ddlHour').selectedIndex = jsonObj.Hour - 1
        document.getElementById('ddlMinute').selectedIndex = jsonObj.Minute

        if (jsonObj.Ampm == "am") {
            document.getElementById('ddlampm').selectedIndex = 0
        }
        else {
            document.getElementById('ddlampm').selectedIndex = 1
        }
        document.getElementById(calDateID).value = jsonObj.InputDate

        if (jsonObj.Accessible == "Y") {
            document.getElementById('accessibleChkbox').checked = true;
        }
        useFromSuggestion = true;
        useToSuggestion = true;
        document.getElementById('txtOriginInput').value = jsonObj.OriginInput
        if (jsonObj.DestinationInput != null && jsonObj.DestinationInput != "") {
            document.getElementById('txtDestinationInput').value = jsonObj.DestinationInput
        }
    }

}

function InitializeServiceintheArea() {
    //startMap();
    if (jsonObj != null && jsonObj != "") {
        //startmap calls clearForm that clears these values so set these after calling startmap()

        fromCoordinates = jsonObj.OriginCoordinates;
        toCoordinates = jsonObj.DestinationCoordinates;

        document.getElementById('ddlHour').selectedIndex = jsonObj.Hour - 1
        document.getElementById('ddlMinute').selectedIndex = jsonObj.Minute

        if (jsonObj.Ampm == "am") {
            document.getElementById('ddlampm').selectedIndex = 0
        }
        else {
            document.getElementById('ddlampm').selectedIndex = 1
        }
        document.getElementById(calDateID).value = jsonObj.InputDate

        if (jsonObj.Accessible == "Y") {
            document.getElementById('accessibleChkbox').checked = true;
        }
        if (jsonObj.Walkdist != null && jsonObj.Walkdist != "") {
            document.getElementById('ddlWalkdistance').value = jsonObj.Walkdist
        }
        useFromSuggestion = true;
        useToSuggestion = true;
        document.getElementById('txtOriginInput').value = jsonObj.OriginInput
    }
}

function reverseTrip() {
    var startAddress = document.getElementById('txtOriginInput').value;
    var endAddress = document.getElementById('txtDestinationInput').value;
    document.getElementById('txtOriginInput').value = endAddress;
    document.getElementById('txtDestinationInput').value = startAddress;

    var fromCoors = fromCoordinates;
    var ToCoors = toCoordinates;

    fromCoordinates = ToCoors;
    toCoordinates = fromCoors;

    g_BrowserType = "MODIFY_TRIP";
    submitAddresses();

}

function OpenEmailPopUp() {
    window.open("../EmailTrip.aspx", "mywindow", "left=250,top=450,toolbar=no,scrollbars=no,width=550,height=370");
    //window.open('~/ui_web/Email.aspx' , 'Check Email','left=100,top=100,toolbar=no,scrollbars=yes,width=680,height=350');
}

function OpenCommentsPopUp() {
    window.open("../Comment.aspx?from=tp", "mywindow", "left=250,top=450,toolbar=no,scrollbars=no,width=550,height=370");
    //window.open('~/ui_web/Email.aspx' , 'Check Email','left=100,top=100,toolbar=no,scrollbars=yes,width=680,height=350');
}

function addOption(selectbox, text, value) {
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}


function FillTrainOrBusRoutes(startEnd) {
    var ddlRouteStartEnd;
    var radioStartEnd;
    var boro;
    var trainType;
    var trainStartEnd;
    var BusStartEnd;
    var XbusstartEnd;
    var trStartEndBoro
    var trStartEndtrainType
    var i;
    if (startEnd == "s") {
        ddlRouteStartEnd = "lineStart";
        boro = "SelectStartBoro";
        trainType = "SelectStartTrainType";
        trainStartEnd = "trainStart";
        BusStartEnd = "busStart";
        XbusstartEnd = "XbusStart";

        //To show hide td
        trStartEndBoro = "tdStartBoroSelection";
        trStartEndtrainType = "tdStartTrainTypeSelection";

    }
    else if (startEnd == "e") {
        ddlRouteStartEnd = "lineEnd";
        boro = "SelectEndBoro"
        trainType = "SelectEndTrainType";
        trainStartEnd = "trainEnd";
        BusStartEnd = "busEnd";
        XbusstartEnd = "XbusEnd";

        //to show hide td
        trStartEndBoro = "tdEndBoroSelection";
        trStartEndtrainType = "tdEndTrainTypeSelection";

    }

    if (document.getElementById(trainStartEnd).checked == 1) {
        document.getElementById(boro).selectedIndex = 0;
        document.getElementById(trStartEndBoro).style.display = "none";
        document.getElementById(trStartEndtrainType).style.display = "block";
    }
    else {
        document.getElementById(trainType).selectedIndex = 0;
        document.getElementById(trStartEndtrainType).style.display = "none";
        document.getElementById(trStartEndBoro).style.display = "block";
    }
    document.getElementById(ddlRouteStartEnd).length = 0;
    addOption(document.getElementById(ddlRouteStartEnd), 'select', '0');

    if (document.getElementById(trainStartEnd).checked == 1) {

        var TrainType = document.getElementById(trainType)[document.getElementById(trainType).selectedIndex].value;
        if (TrainType == "subway") {
            for (i = 1; i < subwayLines.length; i++) {
                if (subwayLines[i] == "Shuttle-Franklin") {
                    addOption(document.getElementById(ddlRouteStartEnd), subwayLines[i], "FS");
                }
                else if (subwayLines[i] == "Shuttle-Rockaway") {
                    addOption(document.getElementById(ddlRouteStartEnd), subwayLines[i], "H");
                }
                else if (subwayLines[i] == "Shuttle-Times Sq") {
                    addOption(document.getElementById(ddlRouteStartEnd), subwayLines[i], "GS");
                }
                else {
                    addOption(document.getElementById(ddlRouteStartEnd), subwayLines[i], subwayLines[i]);
                }
            }
        }
        else if (TrainType == "LIRR") {
            for (i = 1; i < LIRRLines.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), LIRRLines[i], LIRRLines[i]);
            }
        }
        else if (TrainType == "MNR") {
            for (i = 1; i < MNRLines.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), MNRLines[i], MNRLines[i]);
            }
        }
    }
    else if (document.getElementById(BusStartEnd).checked == 1) {
        var boroIndex = document.getElementById(boro).selectedIndex;
        var boroValue = document.getElementById(boro)[boroIndex].value;
        if (boroValue == "MN") {
            for (i = 1; i < BusLinesManhattan.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), BusLinesManhattan[i], BusLinesManhattan[i]);
            }
        }
        else if (boroValue == "BK") {
            for (i = 1; i < BusLinesBrooklyn.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), BusLinesBrooklyn[i], BusLinesBrooklyn[i]);
            }
        }
        else if (boroValue == "QN") {
            for (i = 1; i < BusLinesQueens.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), BusLinesQueens[i], BusLinesQueens[i]);
            }
        }
        else if (boroValue == "BX") {
            for (i = 1; i < BusLinesBronx.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), BusLinesBronx[i], BusLinesBronx[i]);
            }
        }
        else if (boroValue == "SI") {
            for (i = 1; i < BusLinesStatenIsland.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), BusLinesStatenIsland[i], BusLinesStatenIsland[i]);
            }
        }
        else if (boroValue == "LI") {
            for (i = 1; i < BusLinesLongIsland.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), BusLinesLongIsland[i], BusLinesLongIsland[i]);
            }
        }

    }
    else {
        var XBusLines;
        var boroIndex = document.getElementById(boro).selectedIndex;
        var boroValue = document.getElementById(boro)[boroIndex].value;
        if (boroValue == "MN") {
            for (i = 1; i < XBusLinesManhattan.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), XBusLinesManhattan[i], XBusLinesManhattan[i]);
            }
        }
        else if (boroValue == "BK") {
            for (i = 1; i < XBusLinesBrooklyn.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), XBusLinesBrooklyn[i], XBusLinesBrooklyn[i]);
            }
        }
        else if (boroValue == "QN") {
            for (i = 1; i < XBusLinesQueens.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), XBusLinesQueens[i], XBusLinesQueens[i]);
            }
        }
        else if (boroValue == "BX") {
            for (i = 1; i < XBusLinesBronx.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), XBusLinesBronx[i], XBusLinesBronx[i]);
            }
        }
        else if (boroValue == "SI") {
            for (i = 1; i < XBusLinesStatenIsland.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), XBusLinesStatenIsland[i], XBusLinesStatenIsland[i]);
            }
        }
    }
}

//Email functions
function showEmailDiv() {
    document.getElementById("EmailDiv").style.display = "inline"
    //document.getElementById("EmailDiv").style.height = (pageHeight() - 50) + "px"
    //document.getElementById("EmailIframe").style.height = (pageHeight() - 74) + "px"
}


function hideEmailDiv() {
    this.parent.document.getElementById("EmailDiv").style.display = "none"
}

//Comment functions
function showCommentDiv() {
    document.getElementById("CommentDiv").style.display = "inline"
    //document.getElementById("EmailDiv").style.height = (pageHeight() - 50) + "px"
    //document.getElementById("EmailIframe").style.height = (pageHeight() - 74) + "px"
}


function hideCommentDiv() {
    this.parent.document.getElementById("CommentDiv").style.display = "none"
}

function pageHeight() {
    if (window.innerHeight) {
        return window.innerHeight
    }
    else {
        return document.documentElement.clientHeight
    }
}


function addOptionByServiceType(trainORbusORXbus, traintype, borough, selectbox, text, value) {

    if (trainORbusORXbus == 'train') {
        if (TrainType == "subway") {
            for (var i = 1; i < subwayLines.length; i++) {
                if (subwayLines[i] == "Shuttle-Franklin") {
                    addOption(document.getElementById(ddlRouteStartEnd), subwayLines[i], "FS");
                }
                else if (subwayLines[i] == "Shuttle-Rockaway") {
                    addOption(document.getElementById(ddlRouteStartEnd), subwayLines[i], "H");
                }
                else if (subwayLines[i] == "Shuttle-Times Sq") {
                    addOption(document.getElementById(ddlRouteStartEnd), subwayLines[i], "GS");
                }
                else {
                    addOption(document.getElementById(ddlRouteStartEnd), subwayLines[i], subwayLines[i]);
                }
            }
        }
        else if (TrainType == "LIRR") {
            for (var i = 1; i < LIRRLines.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), LIRRLines[i], LIRRLines[i]);
            }
        }
        else if (TrainType == "MNR") {
            for (var i = 1; i < MNRLines.length; i++) {
                addOption(document.getElementById(ddlRouteStartEnd), MNRLines[i], MNRLines[i]);
            }
        }
    }
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    selectbox.options.add(optn);
}

function printItenery() {debugger;
    var printContent = document.getElementById("printDiv").innerHTML;
    printContent = printContent.replace(/Map/g, "");
    printContent = printContent.replace(/Collapse/g, "").replace(/[[\]]/g, '').replace(/\-/g, "");
    
//    window["iteneryPrintIframe"].document.body.innerHTML = printContent;
//    window.frames["iteneryPrintIframe"].focus();
//    window.frames["iteneryPrintIframe"].print();


    var myIFrame = document.getElementById("iteneryPrintIframe");
    myIFrame.contentWindow.document.body.innerHTML = printContent;
    myIFrame.contentWindow.focus();
    myIFrame.contentWindow.print();

}

function ToggleRoutePrefDiv() {
    if (document.getElementById('RoutePrefDiv').style.display == "inline") {
        document.getElementById('RoutePrefDiv').style.display = "none";
        document.getElementById('imgCollapseExpand').src = "../../images/Expand.gif"
        document.getElementById('imgCollapseExpand').alt = "Expand";
    }
    else if (document.getElementById('RoutePrefDiv').style.display == "none") {
        document.getElementById('RoutePrefDiv').style.display = "inline";
        document.getElementById('imgCollapseExpand').src = "../../images/Collapse.gif"
        document.getElementById('imgCollapseExpand').alt = "Collapse";
    }
}


function GetRouteSchedulesHTML(url) {
    // Request String
    document.getElementById('ajaxLoadingDiv').style.display = "inline";

    var randomnumber = Math.floor(Math.random() * 11);
    url = url + "&rand=" + randomnumber;
    var ajax_caller = new AjaxCaller();
    ajax_caller.postDataReturnText(url, '', CallBackRouteSchedules);
}

function CallBackRouteSchedules(resp) {
    document.getElementById('ajaxLoadingDiv').style.display = "none";

    Popup.hideAll();
    Popup.show(null, null, 'center center', { 'content': resp,
        'style': { 'border': '1px solid black', 'z-index': '20000', 'backgroundColor': 'white' }
    }, false); return false;
}

function GetRouteStatusHTML(url) {
    // Request String
    document.getElementById('ajaxLoadingDiv').style.display = "inline";

    var randomnumber = Math.floor(Math.random() * 11);

    url = url + "&rand=" + randomnumber;
    var ajax_caller = new AjaxCaller();
    ajax_caller.postDataReturnText(url, '', CallBackRouteStatus);
}

function CallBackRouteStatus(resp) {
    document.getElementById('ajaxLoadingDiv').style.display = "none";

    Popup.hideAll();
    Popup.show(null, null, null, { 'content': resp,
        'style': { 'border': '1px solid black', 'z-index': '20000', 'backgroundColor': 'white' }
    }, false); return false;
}



//called from results page




function OpenClose(divId, BtnId) {

//innerText does not work in firefox so used innerHTMLinstead
    if (document.getElementById(divId)) {
        if (document.getElementById(divId).style.display == "none") {
            document.getElementById(divId).style.display = "inline";
            document.getElementById(BtnId).innerHTML = "[-]Collapse"
        }
        else {
            document.getElementById(divId).style.display = "none";
            document.getElementById(BtnId).innerHTML = "[+]Expand"
        }
    }
}


var mapLoaded = "N"

function goToRouteMap(mapParameters, requestdevice) {

    document.getElementById("mapIframe").src = "../../maps/routeMap.aspx?walkandrideparams=" + mapParameters + "&&reqestDeviceName=" + requestdevice
    document.getElementById("mapIframe").style.display = "inline"
    document.getElementById("mapIframe").style.top = (getScrollY() + 0) + "px"
    mapLoaded = "Y"
}

function goToMap_phone(mapParameters) {

    window.location = "SelectStop.aspx?mapstopsphone=" + mapParameters

}

function closeMap() {
    document.getElementById("mapIframe").style.display = "none"
}



function getScrollY() {
    var scrOfX = 0, scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }

    return scrOfY;
}


//Called from kiosk results page in case nearest landmark page is shown
        function SetValuesFromJSON() {

            if (jsonObj) {
                Arrdep = jsonObj.Arrdep;
                AccessibleTrip = jsonObj.Accessible;
                hour = jsonObj.Hour;
                minute = jsonObj.Minute;
                ampm = jsonObj.Ampm;
                minimize = jsonObj.Minimize;
                walkdistance = jsonObj.Walkdist;
                mode = jsonObj.Mode;
                selectedDate = jsonObj.InputDate;
                originCoors = jsonObj.OriginCoordinates;
                destinationCoors = jsonObj.DestinationCoordinates;
                startAddr = jsonObj.OriginInput
                endAddr = jsonObj.DestinationInput
                walkincrease = "" //default
                maxinitialwait = "" //default
                lineStart = "";
                startServiceType = "train"; //default
                startTrainType = "subway" //default
                startBorough = "MN"; //default
                lineEnd = "";
                endServiceType = "train"; //default
                endTrainType = "subway" //default
                endBorough = "MN"; //default
            }
        }


        function SubmitTPFromNearLMKpage() {
            submit_CustomPlanner(g_BrowserType,
                              startAddr,
                              endAddr,
                              Arrdep,
                              hour,
                              minute,
                              ampm,
                              selectedDate,
                              minimize,
                              walkdistance,
                              mode,
                              lineStart,
                              lineEnd,
                              AccessibleTrip,
                              originCoors,
                              destinationCoors,
                              startServiceType,
                              startTrainType,
                              startBorough,
                              endServiceType,
                              endTrainType,
                              endBorough,
                              walkincrease,
                              maxinitialwait)
        }

