﻿/********************************************************************/
/*       Nastaveni data                                */
/********************************************************************/

var refJourneyDate;
var refJourneyDateID;

function MSIEevents()
{
  return (! document.body.addEventListener);
}

function CancelBubbling(Ev)
{
    if (MSIEevents()) {
        Ev.cancelBubble = true;
        Ev.returnValue = false;
    } else {
        Ev.stopPropagation();
        Ev.preventDefault();
    }
}

/* onLoad stranky Conn.aspx */
function loadSearch(DateSelect, DateMin, DateMax, id){
        refJourneyDateID = id;        
        refJourneyDate = document.getElementById(refJourneyDateID);       
        OurDate = new Date(DateSelect);
        MinValidDate = new Date(DateMin);
        var today;
        today = new Date();
        MaxValidDate = new Date(DateMax);
}

/*       Reference na prvky stranky a promenne */
var OurDate = new Date();
var MinValidDate;
var MaxValidDate;

//function refJourneyDate()
//{
//    return document.getElementById("txtDate");
//}

/* zorazeni kalendare */
function ShowCalendar(){
        document.getElementById("DateInputRow").style.display = "none";
        cal.setDate(OurDate); // go to OurDate
        document.getElementById("CalendarRow").style.display = "block";
}

/* Kontrola vlozeneho data */

function JourneyDateCheck() {
    OurDate = CheckAndSetDate(OurDate, refJourneyDate.value);
    refJourneyDate.value = OurDate2String();
    return false;
}

// extern var [OurDate]
// [JourneyDate]
function JourneyDateBack() {
    if (MinValidDate >= OurDate) return false;
    OurDate.setTime(OurDate.getTime() - 1*24*60*60*1000);
    refJourneyDate.value = OurDate2String();
    return false;
}

// $[OurDate]
// [JourneyDate]
function JourneyDateFore() {
    if (MaxValidDate <= OurDate) return false;
    OurDate.setTime(OurDate.getTime() + 1*24*60*60*1000);
    refJourneyDate.value = OurDate2String();
    return false;
}

function GetNearestDay(DayOfWeek) {
    var D = new Date();
    while (true)
    {
        if (D.getDay() == DayOfWeek) return D;
        D.setTime(D.getTime() + 1*24*60*60*1000);
    }
}

function DaysInMonth(Month, Year) {
    if (Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12) return 31;
    if (Month == 4 || Month == 6 || Month == 9 || Month == 11) return 30;
    if (Month == 2) return (Year % 4 ? 28 : 29);
    return 0;
}

function CheckAndSetDate(XDate, Input) {
    var LastDate = XDate;

	var delimiters = new RegExp('[ .,/-]');
	Input = Input.toLowerCase();

	//Nastaveni podle dne v tydnu
	if (Input == 'po') { XDate = GetNearestDay(1); return XDate; }
	if (Input == 'ut' || Input == 'út') { XDate = GetNearestDay(2); return XDate; }
	if (Input == 'st' ) { XDate = GetNearestDay(3); return XDate; }
	if (Input == 'ct' || Input == 'čt') { XDate = GetNearestDay(4); return XDate; }
	if (Input == 'pa' || Input == 'pá') { XDate = GetNearestDay(5); return XDate; }
	if (Input == 'so') { XDate = GetNearestDay(6); return XDate; }
	if (Input == 'ne') { XDate = GetNearestDay(0); return XDate; }

	var Split = Input.split(delimiters, 3);
	var SplitLength = Split.length;

	if (isNaN(Split[SplitLength-1])) SplitLength--;

	if (SplitLength < 1 || SplitLength > 3) return LastDate;

	for (i = 0; i < SplitLength; i++)
		if (isNaN(Split[i])) return LastDate;

	if (SplitLength == 3)
	{
		//rok
		if (Split[2].length != 2 && Split[2].length != 4) return LastDate;
		if (Split[2].length == 2){Split[2] = '20' + Split[2]}

		//mesic
		if (Split[1] < 1 || Split[1] > 12) return LastDate;

		//den
		if (Split[0] < 0 || Split[0] > DaysInMonth(Split[1], Split[2])) return LastDate;

		XDate = new Date();
		XDate.setFullYear(Split[2]);
		XDate.setMonth(Split[1] - 1);
		XDate.setDate(Split[0]);
		return XDate;
	}

	if (SplitLength == 2)
	{
		//mesic
		if (Split[1] < 1 || Split[1] > 12) return LastDate;

		//den
		if (Split[0] < 0 || Split[0] > DaysInMonth(Split[1], new Date().getFullYear())) return LastDate;

		XDate = new Date();
		XDate.setMonth(Split[1] - 1);
		XDate.setDate(Split[0]);
		return XDate;
	}

	if (SplitLength == 1)
	{
		//den
		if (Split[0] < 0 || Split[0] > DaysInMonth(new Date().getMonth() + 1, new Date().getFullYear())) return LastDate;

		XDate = new Date();
		XDate.setDate(Split[0]);
		return XDate;
	}

    } // CheckAndSetDate



/* deklarace  OurDate2String */

function OurDate2String()
{
    return Date2String(OurDate) + ' ' + GetDayName(OurDate.getDay());
}
function Date2String(D)
{
    return D.getDate() + '.' + (D.getMonth() + 1) + '.' + D.getFullYear();
}
function GetDayName(DOW) {    
    if (DOW == 0) return Calendar._SDN[0];
    if (DOW == 1) return Calendar._SDN[1];
    if (DOW == 2) return Calendar._SDN[2];
    if (DOW == 3) return Calendar._SDN[3];
    if (DOW == 4) return Calendar._SDN[4];
    if (DOW == 5) return Calendar._SDN[5];
    if (DOW == 6) return Calendar._SDN[6];
    return 'Error';
}


/* handle sipek pro datum */
function onDateKeydown(button, event)
{
    try {
        var ikeyCode;
        /* get keyCode */
        ikeyCode = 0;
        if (event.which) {
            ikeyCode = event.which;
        }
        else if (event.keyCode){
            ikeyCode = event.keyCode;
        }
        /* akce */
	    if (ikeyCode == 13) {
		    button.click();
		    event.returnValue=false;
		    event.cancel = true;
		    return false;
	    } else if (ikeyCode == 38){
            JourneyDateFore();
            refJourneyDate.select();
		    event.returnValue=false;
		    event.cancel = true;
		    return false;
	    } else if(ikeyCode == 40) {
            JourneyDateBack();
            refJourneyDate.select();
		    event.returnValue=false;
		    event.cancel = true;
            return false;
	    }
	    else {
		    return true;
	    }
    } catch(e) { }
}
