﻿
SelectedFromStation =
            {

                weeklyopentime: "",
                weeklyclosetime: "",
                satopentime: "",
                satclosetime: "",
                sunopentime: "",
                sunclosetime: "",
                Weekly24: "", 
                Sat24: "", 
                Sun24: "", 
                WeeklyIsOpen: "", 
                SatIsOpen: "", 
                SunIsOpen: "",
                latitude: "",
                longitude:""

            };

SelectedToStation =
            {

                weeklyopentime: "",
                weeklyclosetime: "",
                satopentime: "",
                satclosetime: "",
                sunopentime: "",
                sunclosetime: "",
                Weekly24: "",
                Sat24: "",
                Sun24: "",
                WeeklyIsOpen: "",
                SatIsOpen: "",
                SunIsOpen: "",
                latitude: "",
                longitude: ""

            };



var dtDefaultMinDate = '1/1/1900';

function qqq() {
    //alert();
}


//// Callback function invoked on successful 
//// completion of the page method.
//function OnSucceeded(result, userContext, methodName) {
//alert('dddss')
//    if (methodName == "GetValue") {
//        displayElement.innerHTML = "Current session state value: " +
//                result;
//    }
//}

//// Callback function invoked on failure 
//// of the page method.
//function OnFailed(error, userContext, methodName) {
//    if (error !== null) {
//        displayElement.innerHTML = "An error occurred: " +
//                error.get_message();
//    }
//}


function ToStationComboBox_SelectedIndexChanged(sender, e) {
    ///nnn
    if (e.get_item().get_text() != '') {
        FillComboBoxToolTip(SelectedToStation, e, 'ToCar');
    }

}

function FillComboBoxToolTip(p_objSelectedStation, p_e, p_strContext) {
    
    var weeklyopentime = GetRadComboBoxItemAttribute(p_e, 'weeklyopentime');
    var weeklyclosetime = GetRadComboBoxItemAttribute(p_e, 'weeklyclosetime');
    var satopentime = GetRadComboBoxItemAttribute(p_e, 'satopentime');
    var satclosetime = GetRadComboBoxItemAttribute(p_e, 'satclosetime');
    var sunopentime = GetRadComboBoxItemAttribute(p_e, 'sunopentime');
    var sunclosetime = GetRadComboBoxItemAttribute(p_e, 'sunclosetime');
    ///open all day
    var Weekly24 = eval(GetRadComboBoxItemAttribute(p_e, 'Weekly24'));
    var Sat24 = eval(GetRadComboBoxItemAttribute(p_e, 'Sat24'));
    var Sun24 = eval(GetRadComboBoxItemAttribute(p_e, 'Sun24'));
    ///closed all together
    var WeeklyIsOpen = eval(GetRadComboBoxItemAttribute(p_e, 'WeeklyIsOpen'));
    var SatIsOpen = eval(GetRadComboBoxItemAttribute(p_e, 'SatIsOpen'));
    var SunIsOpen = eval(GetRadComboBoxItemAttribute(p_e, 'SunIsOpen'));
    var strStationIsClosedText = 'סגורה';
    var strStationIsOpenedAllDayText = 'כל היום';
    
    var strStationWeeklyHours = weeklyopentime + '-' + weeklyclosetime;
    var strStationSatHours = satopentime + '-' + satclosetime;
    var strStationSunHours = sunopentime + '-' + sunclosetime;

    ///If stations are closed
    if (!WeeklyIsOpen) {
        strStationWeeklyHours = strStationIsClosedText;
    }

    if (!SatIsOpen) {
        strStationSatHours = strStationIsClosedText;
    }
    //debugger
    //alert(SunIsOpen == 'false')

    if (!SunIsOpen) {
        strStationSunHours = strStationIsClosedText;
        //alert(strStationSunHours)
    }
    
    ///is stations are opend 24 hors
    if (Weekly24) {
        strStationWeeklyHours = strStationIsOpenedAllDayText;
    }

    if (Sat24) {
        strStationSatHours = strStationIsOpenedAllDayText;
    }

    if (Sun24) {
        strStationSunHours = strStationIsOpenedAllDayText;
    }
    //alert(strStationSunHours)
    
    
    var latitude = GetRadComboBoxItemAttribute(p_e, 'latitude');
    var longitude = GetRadComboBoxItemAttribute(p_e, 'longitude');
    //debugger
    if ((typeof (latitude) != 'undefined') && (typeof (longitude) != 'undefined') && (latitude != 0) && (longitude != 0)) 
    {
        if ($get('Map' + p_strContext + 'Station'))
            document.getElementById('Map' + p_strContext + 'Station').style.display = "block";
    }
    else {
        if ($get('Map' + p_strContext + 'Station'))
            document.getElementById('Map' + p_strContext + 'Station').style.display = "none";
    }
    
    //        alert(weeklyopentime)
    //        alert(weeklyclosetime)
    //        alert(satopentime)
    //        alert(satclosetime)
    //        alert(sunopentime)
    //        alert(sunclosetime)
    
    
//            alert(WeeklyIsOpen);
//            alert(SatIsOpen);
            //alert(SunIsOpen);
//            alert(Weekly24);
//            alert(Sat24);
            //alert(Sun24);


   //alert(latitude+"js" );
    //alert(longitude+"js");
    if ($get('spnToolTip' + p_strContext + 'Station') == null)
    {
        if ( p_strContext == 'ToCar')
            p_strContext = 'To';
        //else if (p_strContext == 'To')
    }
    
    $get('spnToolTip' + p_strContext + 'Station').style.display = 'block';
    $get('tdToolTip' + p_strContext + 'StationName').innerHTML = p_e.get_item().get_text();
    $get('tdToolTip' + p_strContext + 'StationAddress').innerHTML = GetRadComboBoxItemAttribute(p_e, 'address');

    $get('tdToolTip' + p_strContext + 'StationWeeklyHours').innerHTML = strStationWeeklyHours;
    $get('tdToolTip' + p_strContext + 'StationSatHours').innerHTML = strStationSatHours;
    $get('tdToolTip' + p_strContext + 'StationSunHours').innerHTML = strStationSunHours;
    
    //alert(document.getElementById('hidden' + p_strContext + 'StationLat'));
    //document.getElementById('hidden' + p_strContext + 'StationLat').value = latitude;
    //alert(document.getElementById('hidden' + p_strContext + 'StationLat').value);
//    $get('hidden' + p_strContext + 'StationLong').value = longitude;
   
    try {
            p_objSelectedStation.weeklyopentime = new Date(dtDefaultMinDate + ' ' + weeklyopentime),
            p_objSelectedStation.weeklyclosetime = new Date(dtDefaultMinDate + ' ' + weeklyclosetime),
            p_objSelectedStation.satopentime = new Date(dtDefaultMinDate + ' ' + satopentime),
            p_objSelectedStation.satclosetime = new Date(dtDefaultMinDate + ' ' + satclosetime),
            p_objSelectedStation.sunopentime = new Date(dtDefaultMinDate + ' ' + sunopentime),
            p_objSelectedStation.sunclosetime = new Date(dtDefaultMinDate + ' ' + sunclosetime),
            p_objSelectedStation.Weekly24 = Weekly24,
            p_objSelectedStation.Sat24 = Sat24,
            p_objSelectedStation.Sun24 = Sun24,
            p_objSelectedStation.WeeklyIsOpen = WeeklyIsOpen,
            p_objSelectedStation.SatIsOpen = SatIsOpen,
            p_objSelectedStation.SunIsOpen = SunIsOpen,
            p_objSelectedStation.latitude = latitude,
            p_objSelectedStation.longitude = longitude
    }
    catch (e) {

    }


    //alert(p_objSelectedStation.weeklyopentime);
}


function OpenPopUpCarMap(appPath, Dir) {
    //alert(document.getElementById("hiddenFromStationLat"));
    //alert(document.getElementById("hiddenFromStationLat").value);
    //alert(document.getElementsByName("hiddenFromStationLat"));
    //         alert(document.getElementById("hiddenFromStationLong"));
    //alert(SelectedFromStation.latitude);
    //alert(SelectedFromStation.longitude);
    //debugger
    windowAttribs = "width=400px,height=400px,left=\'+((screen.width -400) / 2)+\',top=\'+ (screen.height - 400)/ 2+\'";
    if (Dir == 'To') {
        path = appPath + "/Cars/CarsGMap.aspx?lan=" + SelectedToStation.latitude + "&long=" + SelectedToStation.longitude + "&car=1";
    }
    else 
    {
        path = appPath + "/Cars/CarsGMap.aspx?lan=" + SelectedFromStation.latitude + "&long=" + SelectedFromStation.longitude + "&car=1";
    }
    
    //alert(path);
    
    //***Building the client script- window.open, with additional parameters***///
    window.open(path, "Map", windowAttribs);
    return false;
    
}

function FromStationComboBox_OnClientItemsRequesting(sender, e) {
    //alert('req');
    //e.set_cancel(false);
}



function jjj(sender, e) {
    if (sender.get_text().length < 2)
    //return false if the number of the typed characters is less than 3
        e.set_cancel(true);
    else
        e.set_cancel(false);
}



/// End events

/// Start methods
function GetRadComboBoxItemAttribute(p_e, p_strAttributeKey) {
    //alert(p_strAttributeKey)
    //alert(p_e.get_item().get_attributes().getAttribute(p_strAttributeKey));
    return p_e.get_item().get_attributes().getAttribute(p_strAttributeKey);
}

function ValidateStationWorkHours(p_objSelectedStation, p_objPicker, p_strStationHour) {
    if (p_objSelectedStation.weeklyopentime) {//alert(SelectedFromStation.context)
        var selectedDayOfWeek = p_objPicker.getSelectedDate().getDay();
        var tmpSelectedStationWorkOpenHour;
        var tmpSelectedStationWorkCloseHour;
        
        ///weekly
        if (selectedDayOfWeek > 0 && selectedDayOfWeek < 6) {
            tmpSelectedStationWorkOpenHour = p_objSelectedStation.weeklyopentime;
            tmpSelectedStationWorkCloseHour = p_objSelectedStation.weeklyclosetime;
            
            if (!p_objSelectedStation.WeeklyIsOpen)
            {
                return false;
            }
            
            if (p_objSelectedStation.Weekly24)
            {
                return true;
            }
        }

        ///sat
        if (selectedDayOfWeek == 6) {
            tmpSelectedStationWorkOpenHour = p_objSelectedStation.satopentime;
            tmpSelectedStationWorkCloseHour = p_objSelectedStation.satclosetime;
            
            if (!p_objSelectedStation.SatIsOpen)
            {
                return false;
            }
            
            if (p_objSelectedStation.Sat24)
            {
                return true;
            }
        }

        ///sun
        if (selectedDayOfWeek == 0) {
            tmpSelectedStationWorkOpenHour = p_objSelectedStation.sunopentime;
            tmpSelectedStationWorkCloseHour = p_objSelectedStation.sunclosetime;
            
            if (!p_objSelectedStation.SunIsOpen)
            {
                return false;
            }
            
            if (p_objSelectedStation.Sun24)
            {
                return true;
            }
        }

        //alert(tmpSelectedFromStationWorkOpenHour)
        //alert(strFromStationHour);
        try {
//            alert(tmpSelectedStationWorkOpenHour.getTime())
//            alert(new Date(dtDefaultMinDate + ' ' + p_strStationHour).getTime())
//            alert(tmpSelectedStationWorkCloseHour.getTime())
//            alert(new Date(dtDefaultMinDate + ' ' + p_strStationHour).getTime())
//            alert(tmpSelectedStationWorkOpenHour.getTime())
//            alert(tmpSelectedStationWorkCloseHour.getTime())

//            alert(tmpSelectedStationWorkOpenHour.getTime() > new Date(dtDefaultMinDate + ' ' + p_strStationHour).getTime())
//            alert(tmpSelectedStationWorkCloseHour.getTime() < new Date(dtDefaultMinDate + ' ' + p_strStationHour).getTime())
//            alert(tmpSelectedStationWorkOpenHour.getTime() == tmpSelectedStationWorkCloseHour.getTime())
            
            if (tmpSelectedStationWorkOpenHour.getTime() > new Date(dtDefaultMinDate + ' ' + p_strStationHour).getTime()
                        || tmpSelectedStationWorkCloseHour.getTime() < new Date(dtDefaultMinDate + ' ' + p_strStationHour).getTime()
                        || tmpSelectedStationWorkOpenHour.getTime() == tmpSelectedStationWorkCloseHour.getTime()
                        ) 
            {
                //alert(88)
                return false;
            }
        }
        catch (e) {
            //alert(e)
        }

        return true;

    }
}

function RenderStationWorkHours(p_objStation) {//return;
    dateFormat.masks.regularTime = 'HH:MM';
    var strMidNightHour = '00:00';
    var strStationNotOpenText = 'תחנה לא פעילה';
    var strStationIsClosedText = 'סגורה';
    var strStationIsOpenedAllDayText = 'כל היום';

    var strStationWeeklyHours = p_objStation.weeklyopentime.format("regularTime") + '-' + p_objStation.weeklyclosetime.format("regularTime");
    var strStationSatHours = p_objStation.satopentime.format("regularTime") + '-' + p_objStation.satclosetime.format("regularTime");
    var strStationSunHours = p_objStation.sunopentime.format("regularTime") + '-' + p_objStation.sunopentime.format("regularTime");

//    alert(p_objStation.Weekly24);
//    alert(p_objStation.Sat24);
//    alert(p_objStation.Sun24);
//    alert(p_objStation.WeeklyIsOpen);
//    alert(p_objStation.SatIsOpen);
//    alert(p_objStation.SunIsOpen);

    ///If stations are closed
    if (p_objStation.WeeklyIsOpen == false) {
        strStationWeeklyHours = strStationIsClosedText;
    }

    if (p_objStation.SatIsOpen == false) {
        strStationSatHours = strStationIsClosedText;
    }

    if (p_objStation.SunIsOpen == false) {
        strStationSunHours = strStationIsClosedText;
    }

    ///is stations are opend 24 hors
    if (p_objStation.Weekly24 == true) {
        strStationWeeklyHours = strStationIsOpenedAllDayText;
    }

    if (p_objStation.Sat24 == true) {
        strStationSatHours = strStationIsOpenedAllDayText;
    }

    if (p_objStation.Sun24 == true) {
        strStationSunHours = strStationIsOpenedAllDayText;
    }

    var result =
        'שני -שישי:' + strStationWeeklyHours + '\n' +
        'שבת: ' + strStationSatHours + '\n' +
        'ראשון:' + strStationSunHours + '\n'

    return result;
    //return eval('result.replace(/' + strMidNightHour + '-' + strMidNightHour + '/g, strStationNotOpenText)');
}



