/*
script name: loadCalendar.js

description:

this script is:
- changing dates, months and years...
- ... and finally writing to the wanted field

functions:

loadCalendar() - it executes every time when something is changed
changeDate(whatToChange) - when is selected another month or year 
	this function is executed

todayDate() - it return today date
changeYear(oYear) - when is year typed this function is executed 
returnValue(oDate) - when date is chosen this function is executed 
hideMe() - this function hides calendar

dateOnMouseOver() - special effect that changes button style
			it executes when mouse is point a date
dateOnMouseOut() - it restores button style
*/

var cDateType;
var dDate,dTodayDate;

bIsToday=false;
dDate=new Date();
dTodayDate=new Date();

function loadCalendar()
{
	var iYear,iMonth,iCounter,iBuffer;
	var dLastDate;
	var bIsNotAllowed,bIsTodayDate,bIsChosenDate;

	dTodayDate=new Date();	
	//dLastDate=new Date();
	

	if(document.forms["oCalendar"].txtYear.value!="")
	{
		// get year and month that is selected
		iYear=document.forms["oCalendar"].txtYear.value;
		iMonth=document.forms["oCalendar"].cboMonth.selectedIndex;
	
		
		// set date of current month
		dDate.setYear(iYear);
		dDate.setMonth(iMonth);
		dDate.setDate(1);

		// set last date of current month
		/*dLastDate.setYear(iYear);
		dLastDate.setMonth(iMonth+1);
		dLastDate.setDate(0);*/
	}
	else
	{
		
		/* oMyField is field in that will be written wanted date
		if it's empty today date is shown
		*/
		if(oMyField.value!="")
		{
			// get year and month
			iYear=parseInt(document.forms["oCalendar"].hdnYear.value,10);
			iMonth=parseInt(document.forms["oCalendar"].hdnMonth.value,10)-1;
			
			// if year and month are valid
			if((isFinite(parseInt(iYear)) 
			|| String(iYear).length<4) 
			|| isFinite(parseInt(iMonth)))
			{
				// set year and month
				dDate.setYear(iYear);
				dDate.setMonth(iMonth);
				dDate.setDate(1);
			}
		}
		else
		{
			dDate.setYear(dTodayDate.getFullYear());
			dDate.setMonth(dTodayDate.getMonth());
			dDate.setDate(1);
			
			iYear=dDate.getFullYear();
			iMonth=parseInt(dDate.getMonth(),10);
			
		}
		
		/* set last date of month
		*/
		/*dLastDate.setYear(dDate.getFullYear());
		dLastDate.setMonth(dDate.getMonth()+1);
		dLastDate.setDate(0);
		
		dLastDate.setMonth(dDate.getMonth()+1);*/
		
		document.forms["oCalendar"].txtYear.value=dDate.getFullYear();
		document.forms["oCalendar"].cboMonth.selectedIndex=dDate.getMonth();
	
	}

	// days of previous month
	dPreviousDays=new Date(iYear,iMonth,0);
	

	dLastDate=new Date(dDate.getFullYear(),dDate.getMonth()+1,0);
	
	// set first day of month and last date
	iStart=dDate.getDay();
	iEnd=dLastDate.getDate();
	
	
	// if it's zero it's sunday
	if(iStart==0) iStart=7;
	
	for(iCounter=1;iCounter<43;iCounter++)
	{
		bIsNotAllowed=false;
		bIsTodayDate=false;
		bIsChosenDate=false;
		
		if(iStart<=iCounter && (iCounter-iStart+1)<=iEnd)
		{			
			document.forms["oCalendar"].elements["txt"+ iCounter].value=iCounter-iStart+1;
			document.forms["oCalendar"].elements["txt"+ iCounter].disabled=0;
			
			/* checks if is today date
			*/
			if(iCounter-(iStart-1)==dTodayDate.getDate() && iMonth==dTodayDate.getMonth() && iYear==dTodayDate.getFullYear())
				bIsTodayDate=true;

			/* checks if is chosen date
			*/			
			if(document.forms["oCalendar"].hdnDate.value==iCounter-iStart+1)
				bIsChosenDate=true;

			/* checks if is normal date
			*/						
			if(!(bIsTodayDate && bIsChosenDate))
				document.forms["oCalendar"].elements["txt"+ iCounter].className="date";			
			
			/* this will disable dates that are not allowed	
			first 'if' for min allowed date second for max allowed.
			*/
				
			if((((iCounter-(iStart-1))<document.forms["oCalendar"].hdnMinDate.value
			&& (iMonth+1)<=document.forms["oCalendar"].hdnMinMonth.value
			&& iYear<=document.forms["oCalendar"].hdnMinYear.value) 
			|| ((iMonth+1)<document.forms["oCalendar"].hdnMinMonth.value 
			&& iYear==document.forms["oCalendar"].hdnMinYear.value) 
			|| (iYear<document.forms["oCalendar"].hdnMinYear.value))
			&& document.forms["oCalendar"].hdnMinYear.value!="")
			{
				bIsChosenDate=false;
				bIsNotAllowed=true;
			}
			else if((((iCounter-(iStart-1))>document.forms["oCalendar"].hdnMaxDate.value 
			&& (iMonth+1)>=document.forms["oCalendar"].hdnMaxMonth.value 
			&& iYear>=document.forms["oCalendar"].hdnMaxYear.value) 
			|| ((iMonth+1)>document.forms["oCalendar"].hdnMaxMonth.value 
			&& iYear==document.forms["oCalendar"].hdnMaxYear.value) 
			|| (iYear>document.forms["oCalendar"].hdnMaxYear.value))
			&& document.forms["oCalendar"].hdnMaxYear.value!="")
			{
				bIsChosenDate=false;
				bIsNotAllowed=true;
			}

		/* here may be 5 solution:
			1. not allowed date but isn't neither today date nor chosen date
			2. today date but isn't neither chosen date nor not allowed date
			3. chosen date but isn't neither today date nor not allowed date
			4. combination of today date and chosen date but isn't not allowed date
			5. combination of today date and not allowed date but isn't chosen
		*/
			if(bIsNotAllowed && (!bIsTodayDate && !bIsChosenDate))
				document.forms["oCalendar"].elements["txt"+ iCounter].className="notAllowedDate";
			else if(bIsTodayDate && (!bIsChosenDate && !bIsNotAllowed))
				document.forms["oCalendar"].elements["txt"+ iCounter].className="todayDate";
			else if(bIsChosenDate && (!bIsTodayDate && !bIsNotAllowed))
				document.forms["oCalendar"].elements["txt"+ iCounter].className="chosenDate";
			else if(bIsTodayDate && bIsChosenDate && !bIsNotAllowed)
				document.forms["oCalendar"].elements["txt"+ iCounter].className="chosenAndToday";
			else if((bIsTodayDate && bIsNotAllowed) && !bIsChosenDate)
				document.forms["oCalendar"].elements["txt"+ iCounter].className="todayAndNotAllowed";
		}
		else
		{
			/* it prints dates for previous and next month.
		
			*/
			if(iCounter<7 && iStart!=1) // set days of previous month before
				iBuffer=(parseInt(dPreviousDays.getDate())-(iStart-1))+iCounter;
			else // set days of next month
				iBuffer=(iCounter-(iStart-1))-iEnd;
			
			document.forms["oCalendar"].elements["txt"+ iCounter].value=iBuffer;
			document.forms["oCalendar"].elements["txt"+ iCounter].className="disabledDate";
			document.forms["oCalendar"].elements["txt"+ iCounter].disabled=1;
		}

	}
	
	var oPleaseWait;
	oPleaseWait=document.getElementById("pleaseWait");
	oPleaseWait.style.display="none";
}


function getLastDate(iYear,iMonth)
{
	/*if(iMonth==1)
	{*/

	//}
	
	switch(parseInt(iMonth))
	{
		case(0):
		case(2):
		case(4):
		case(6):
		case(7):
		case(9):
		case(11):
			return(31);
		
		case(3):
		case(5):
		case(8):
		case(10):
			return(30);
		
		case(1):
			return(28);
	
	}
	
}


function changeDate(whatToChange)
{
	/* changing months and years
	*/
	switch(whatToChange)
	{
		// executes when is previous month requested
		case("monthPrevious"):
			/* in case that is first month of year 
			*/
			if(document.forms["oCalendar"].cboMonth.selectedIndex==0)
			{
				document.forms["oCalendar"].cboMonth.selectedIndex=11;
				document.forms["oCalendar"].txtYear.value=parseInt(document.forms["oCalendar"].txtYear.value)-1;
			}
			// otherwise, it decrements
			else
				document.forms["oCalendar"].cboMonth.selectedIndex--;
			
			break;
		// executes when is next month requested
		case("monthNext"):
			/* in case that is last month of year 
			*/
			if(document.forms["oCalendar"].cboMonth.selectedIndex==11)
			{
				document.forms["oCalendar"].cboMonth.selectedIndex=0;
				document.forms["oCalendar"].txtYear.value=parseInt(document.forms["oCalendar"].txtYear.value)+1;
			}
			// otherwise, it increments
			else
				document.forms["oCalendar"].cboMonth.selectedIndex++;
			
			break;
		// executes when is previous year requested
		case("yearPrevious"):
			document.forms["oCalendar"].txtYear.value--;
			break;
		// executes when is next year requested
		case("yearNext"):
			document.forms["oCalendar"].txtYear.value++;
			
			break;	
	}
	
	loadCalendar();
}

function todayDate()
{
	document.forms["oCalendar"].txtYear.value=dTodayDate.getFullYear();
	document.forms["oCalendar"].cboMonth.selectedIndex=dTodayDate.getMonth();
	
	loadCalendar();
	
}

function changeYear(oYear)
{
	if(oYear.value.length==4)
	{	
		document.forms["oCalendar"].txtYear.value=oYear.value;
			
		loadCalendar();
	}
}

function returnValue(oDate)
{
	var cDateSep;
	var iDate,iMonth;
	
	cDateSep=document.forms["oCalendar"].hdnDateSep.value;
	
	/* if returned value has only 1 character
	it return 0 before value (for exp. 3 will be 03 ...)
	*/
	if(oDate.value.length==1)
		iDate="0"+ oDate.value;
	else if(oDate.value.substring(0,1)==" ")
		iDate="0"+ oDate.value.substring(1,2);
	else
		iDate=oDate.value;

	if(String(dDate.getMonth()+1).length==1)
		iMonth="0"+ (dDate.getMonth()+1);
	else
		iMonth=dDate.getMonth()+1;

	/* dates that have class notAllowedDate cannot be
	chosen.
	*/
	if(oDate.className!="notAllowedDate" && oDate.className!="todayAndNotAllowed")
	{
		oMyField.value=iDate + cDateSep + iMonth + cDateSep + dDate.getFullYear();
		
		hideMe();
	}
}

function hideMe()
{	
	var oCalendar;
	oCalendar=checkMe();
	
	// show selects
	showHideSelects();
		
	// hide calendar
	oCalendar.style.display="none";
	
	/* clear txtYear field for every case
	*/
	document.forms["oCalendar"].txtYear.value="";
}


/* special effects (onMouseOver and onMouseOut)
*/

function dateOnMouseOver(oDate)
{
	/* set value for variable cDateType
	it will be used for onMouseOut event
	*/
	switch(oDate.className)
	{
		case("todayDate"):
			cDateType="todayDate";
			
			break;
		case("chosenDate"):
			cDateType="chosenDate";
			
			break;
		case("date"):
			cDateType="date";
			
			break;
		default:
			cDateType="notAllowedDate";
	}
		
	if(cDateType!="notAllowedDate")
		oDate.className="onMouseOverDate";
}


function dateOnMouseOut(oDate)
{	
	/* get value from cDateType and 
	put className value
	*/
	switch(cDateType)
	{
		case("todayDate"):
			oDate.className="todayDate";
				
			break;
		case("chosenDate"):
			oDate.className="chosenDate"
			
			break;
		case("date"):
			oDate.className="date";
				
			break;
			
	}
}
//
//
//
//