﻿var win = null;
function NewWindow(mypage, myname) {
    var w = screen.width * 0.8;
    var h = screen.height * 0.8;

    LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100; 
    TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;

    settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=yes';
    win = window.open(mypage, myname, settings);
}


function BusSearch() {
    var From = document.getElementById('txtFrom').value;
    var To = document.getElementById('txtTo').value;
    var url = "http://www.stagecoachbus.com/timetables.aspx?serviceid=&locationId=0&from=" + From + "&to=" + To
    var w  = screen.width * 0.8;
    var h = screen.height * 0.8;

    LeftPosition = (screen.width) ? (screen.width - w) / 2 : 100;
    TopPosition = (screen.height) ? (screen.height - h) / 2 : 100;

    settings = 'width=' + w + ',height=' + h + ',top=' + TopPosition + ',left=' + LeftPosition + ',location=yes';
    win = window.open(url, 'Bus Route Search', settings);
}

function c1s() {
    onCalendarShown("c1");
}
function c1h() {
    onCalendarHidden("c1");
}
function c2s() {
    onCalendarShown("c2");
}
function c2h() {
    onCalendarHidden("c2");
}
function c3s() {
    onCalendarShown("c3");
}
function c3h() {
    onCalendarHidden("c3");
}
function c4s() {
    onCalendarShown("c4");
}
function c4h() {
    onCalendarHidden("c4");
}

function onCalendarShown(calendar) {
    var cal = $find(calendar);
        //Setting the default mode to month
        cal._switchMode("months", true);

        //Iterate every month Item and attach click event to it
        if (cal[i]._monthsBody) {
            for (var i = 0; i < cal[i]._monthsBody.rows.length; i++) {
                var row = cal[i]._monthsBody.rows[i];
                for (var j = 0; j < row.cells.length; j++) {
                    Sys.UI.DomEvent.addHandler(row.cells[j].firstChild, "click", call(calendar));
                }
            }
        }    
}

function onCalendarHidden(calendar) {
    var cal = $find(calendar);
    //Iterate every month Item and remove click event from it
    if (cal._monthsBody) {
        for (var i = 0; i < cal._monthsBody.rows.length; i++) {
            var row = cal._monthsBody.rows[i];
            for (var j = 0; j < row.cells.length; j++) {
                Sys.UI.DomEvent.removeHandler(row.cells[j].firstChild, "click", call(calendar));
            }
        }
    }
}

function call(eventElement, calendar) {
    var target = eventElement.target;
    switch (target.mode) {
        case "month":
            var cal = $find(calendar);
            cal._visibleDate = target.date;
            cal.set_selectedDate(target.date);
            cal._switchMonth(target.date);
            cal._blur.post(true);
            cal.raiseDateSelectionChanged();
            break;
    }
}
