﻿/******
* You may use and/or modify this script as long as you:
* 1. Keep my name & webpage mentioned
* 2. Don't use it for commercial purposes
*
* If you want to use this script without complying to the rules above, please contact me first at: marty@excudo.net
*
* Please be aware that this is only the core source-code of a full script and that it may not work (for you) without the rest.
* The full script can be downloaded from my website. A working example is provided there as well.
* 
* Author: Martijn Korse
* Website: http://devshed.excudo.net
*
* Date: 2006-09-07 07:53:21
***/

 // Some global arrays
monthMaxDays	= [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
monthMaxDaysLeap= [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var currentMonth=0;
var currentYear=0;
hideSelectTags = [];

function getRealYear(dateObj)
{
	return (dateObj.getYear() % 100) + (((dateObj.getYear() % 100) < 39) ? 2000 : 1900);
}

function getDaysPerMonth(month, year)
{
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((year % 4) == 0)
	{
		if ((year % 100) == 0 && (year % 400) != 0)
			return monthMaxDays[month];
	
		return monthMaxDaysLeap[month];
	}
	else
		return monthMaxDays[month];
}

function GetMonth()
{
    return currentMonth;

}
function GetYear()
{
    return currentYear;

}
function ShowMonths(direction)
{
    var a = document.getElementById(RightCalendar);
    
    var month=GetMonth();
    var year=GetYear();
    

    var d = new Date();
    

    if(direction=='left') {
       month=month+1;
       a.style.display = "inline";
    }
    if(direction=='right')
    {
        if(d.getMonth() < month)//You can not go a month old
        {
          month=month-1;
          a.style.display = "inline";
        }
        else
        {
            a.style.display = "none";
        }
       
    }
    
    if(month==12)
    {
        //month=d.getMonth();//Back to current month
        month=0;
        year++; //Current year only
        a.style.display = "none";
    }
    else if(month == d.getMonth())
    {
        a.style.display = "none";
    }
    else if(month==-1)
    {
        month=11;
        year--;
    }
    
    
    showCalenderBody(createCalender(year, month, false));
    MonthName.innerHTML=GetMonthName(currentMonth);
    

}
function GetMonthName(month)
{
    
    switch(month)
    {
        case 0 : return 'ינואר';
                 break;
        case 1 : return 'פברואר';
                 break;
        case 2 : return 'מרץ';
                 break;
        case 3 : return 'אפריל';
                 break;
        case 4 : return 'מאי';
                 break;
        case 5 : return 'יוני';
                 break;
        case 6 : return 'יולי';
                 break;
        case 7 : return 'אוגוסט';
                 break;
        case 8 : return 'ספטמבר';
                 break;
        case 9 : return 'אוקטובר';
                 break;
        case 10 : return 'נובמבר';
                 break;
        case 11 : return 'דצמבר';
                 break;
    }


}
function createCalender(year, month, day)
{
	 // current Date
	var curDate = new Date();
	var curDay = curDate.getDate();
	var curMonth = curDate.getMonth();
	var curYear = getRealYear(curDate)

	 // if a date already exists, we calculate some values here
	if (!year)
	{
		var year = curYear;
		var month = curMonth;
	}
    currentMonth=month;
    document.getElementById('MonthName').innerHTML=GetMonthName(currentMonth);
	currentYear=year;

	 // first day of the month is a ....
	var fristDayOfMonthObj = new Date(year, month, 1);
	var firstDayOfMonth = fristDayOfMonthObj.getDay();

	continu		= true;
	firstRow	= true;
	var x	= 0;
	var d	= 0;
	var trs = []
	var ti = 0;
	while (d <= getDaysPerMonth(month, year))
	{
		if (firstRow)
		{
			trs[ti] = document.createElement("TR");
			if (firstDayOfMonth > 0)
			{
				while (x < firstDayOfMonth)
				{
					trs[ti].appendChild(document.createElement("TD"));
					x++;
				}
			}
			firstRow = false;
			var d = 1;
		}
		if (x % 7 == 0)
		{
			ti++;
			trs[ti] = document.createElement("TR");
		}
		if (day && d == day) {
		    var setID = 'calenderChoosenDay';
		    var styleClass = 'choosenDay';
		    var setTitle = 'this day is currently selected';
		}
		else if (d == curDay && month == curMonth && year == curYear) {
		    var setID = 'calenderToDay';
		    var styleClass = 'toDay';
		    var setTitle = 'this day today';
		}
		else if (curDay > d && month == curMonth && year == curYear) 
		{
		    var setID = false;
		    var styleClass = 'td_blank';
		    var setTitle = false;
		}
		else {
		    var setID = false;
		    var styleClass = 'normalDay';
		    var setTitle = false;
		}
		var td = document.createElement("TD");
		td.className = styleClass;
		if (setID)
		{
			td.id = setID;
		}
		if (setTitle)
		{
			td.title = setTitle;
		}
		td.onmouseover = new Function('highLiteDay(this)');
		td.onmouseout = new Function('deHighLiteDay(this)');
		if (targetEl)
			td.onclick = new Function('pickDate('+year+', '+month+', '+d+')');
		else
			td.style.cursor = 'pointer';
		td.appendChild(document.createTextNode(d));
		trs[ti].appendChild(td);
		x++;
		d++;
	}
	return trs;
}

function showCalender(elPos, tgtEl,ta,tb)
{
	targetEl = true;
	
	var calTable = document.getElementById('calenderTable');

	var positions = [0,0];
	var positions = getParentOffset(elPos, positions);	// nieuw
	calTable.style.left = positions[0]+'px';		// nieuw
	calTable.style.top = positions[1]+'px';			// nieuw

	calTable.style.display='block';
	

	
		trs = createCalender(false, false, false);
		showCalenderBody(trs);
	

	hideSelect(document.body, 1);
}
function showCalenderBody(trs)
{
	var calTBody = document.getElementById('calender');
	while (calTBody.childNodes[0])
	{
		calTBody.removeChild(calTBody.childNodes[0]);
	}
	for (var i in trs)
	{
		calTBody.appendChild(trs[i]);
	}
}
function setYears(sy, ey)
{
	 // current Date
	var curDate = new Date();
	var curYear = getRealYear(curDate);
	if (sy)
		startYear = curYear;
	if (ey)
		endYear = curYear;
	
}
function hideSelect(el, superTotal)
{
	if (superTotal >= 100)
	{
		return;
	}

	var totalChilds = el.childNodes.length;
	for (var c=0; c<totalChilds; c++)
	{
		var thisTag = el.childNodes[c];
		if (thisTag.tagName == 'SELECT')
		{
			if (thisTag.id != 'selectMonth' && thisTag.id != 'selectYear')
			{
				var calenderEl = document.getElementById('calenderTable');
				var positions = [0,0];
				var positions = getParentOffset(thisTag, positions);	// nieuw
				var thisLeft	= positions[0];
				var thisRight	= positions[0] + thisTag.offsetWidth;
				var thisTop	= positions[1];
				var thisBottom	= positions[1] + thisTag.offsetHeight;
				var calLeft	= calenderEl.offsetLeft;
				var calRight	= calenderEl.offsetLeft + calenderEl.offsetWidth;
				var calTop	= calenderEl.offsetTop;
				var calBottom	= calenderEl.offsetTop + calenderEl.offsetHeight;

				if (
					(
						/* seeing if it overlaps horizontally */
						(thisLeft >= calLeft && thisLeft <= calRight)
							||
						(thisRight <= calRight && thisRight >= calLeft)
							||
						(thisLeft <= calLeft && thisRight >= calRight)
					)
						&&
					(
						/* seeing if it overlaps vertically */
						(thisTop >= calTop && thisTop <= calBottom)
							||
						(thisBottom <= calBottom && thisBottom >= calTop)
							||
						(thisTop <= calTop && thisBottom >= calBottom)
					)
				)
				{
					hideSelectTags[hideSelectTags.length] = thisTag;
					thisTag.style.display = 'none';
				}
			}

		}
		else if(thisTag.childNodes.length > 0)
		{
			hideSelect(thisTag, (superTotal+1));
		}
	}
}
function closeCalender()
{
	for (var i=0; i<hideSelectTags.length; i++)
	{
		hideSelectTags[i].style.display = 'block';
	}
	hideSelectTags.length = 0;
	document.getElementById('calenderTable').style.display='none';
}
function highLiteDay(el) {
    /*Moshe Najar : Change to Red Color */

	el.className = 'hlDay';
}
function deHighLiteDay(el)
{
	if (el.id == 'calenderToDay')
		el.className = 'toDay';
	else if (el.id == 'calenderChoosenDay')
		el.className = 'choosenDay';
	else
		el.className = 'normalDay';
}
function pickDate(year, month, day) 
{

    var curDate = new Date();
    var curDay = curDate.getDate();
    var curMonth = curDate.getMonth() + 1;
    var curYear = getRealYear(curDate);
    
    
	month++;
	day	= day < 10 ? '0'+day : day;
	month	= month < 10 ? '0'+month : month;
	if (!targetEl)
	{
		alert('target for date is not set yet');
	}
	else
	{
		targetEl.value= day+'-'+month+'-'+year;
	
		//Moshe Najar - Change Path
		//alert(day+'-'+month+'-'+year);

		

		//alert(curDay + ">" + day + "  " + month + "==" +  curMonth + "  " +  year + "==" +  curYear);
		if (curDay > day && month == curMonth && year == curYear) {
		}
		else {
		    window.location = "/NewChannels/searchevents.aspx?ChannelId=2&Year=" + year + "&Month=" + month + "&Day=" + day;
		}
	}
}
function querySt(ji) 
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) 
    {
        ft = gy[i].split("=");
        if (ft[0] == ji)
        {
            return ft[1];
        }
    }
}

function getParentOffset(el, positions)
{
	
	positions[0]=screen.width/2 ;
	positions[1]=screen.height/2 ;
	return positions;
}
