// take day and month and return year for month+day comboboxes (no year combo)
// if the month+day is in the past, assume next year, else current year.
function CalcTheYearByMonthDay(year, month, day)
{
	var now = new Date();
	if (((now.getMonth() > month) || (now.getMonth() == month && now.getDate() > day)) && now.getFullYear() >= year) {
		year = now.getFullYear();	
		year++;
	}		
	return year;
}

//The function appearing after this one is a more general version of this function. 
function OnEndDateChange(servType)
{
	var	MS_PER_DAY = 24*60*60*1000;
	
	if (form.numDays) {
		var dd,mm,yy;
		var start_dt, end_dt;
		var numDays;
		yy = form.start_dt_yrmn.value / 100;
		mm = form.start_dt_yrmn.value % 100;
		dd = form.start_dt_dy.value;
		yy = CalcTheYearByMonthDay(yy, mm, dd)
		start_dt = new Date(yy,mm-1,dd);
		yy = form.end_dt_yrmn.value / 100;
		mm = form.end_dt_yrmn.value % 100;
		dd = form.end_dt_dy.value;
		yy = CalcTheYearByMonthDay(yy, mm, dd)
		end_dt = new Date(yy,mm-1,dd);
		
		//Update the number of days combo box
		numDays = Math.round((end_dt - start_dt) / MS_PER_DAY);
		numDays += ExtraDays(servType, startTime, endTime, start_dt, end_dt);
		form.numDays.value = numDays;
	}
}

//The function appearing after this one is a more general version of this function. 
//dontAutoCalcYear - set this optional var to true if you dont want to call the function
//CalcTheYearByMonthDay (if no parameter is passed it's value is undefined and the 
//CalcTheYearByMonthDay function is executed)
function OnNumDaysChange(servType, dontAutoCalcYear)
{
	if (form == null || form.start_dt_yrmn == null) //prevent run time error - happens when there is session timeout
		return false;
	
	var numDays = 1;	// Default 1 day
	var dd,mm,yy, rc;
	var start_dt, end_dt;
	var startTime = 0, endTime = 0;  //Has no affect since we want the start or end date only without relative ref to the service
	yy = form.start_dt_yrmn.value / 100;
	if (!dontAutoCalcYear)
		yy = CalcTheYearByMonthDay(yy, mm, dd);
	mm = form.start_dt_yrmn.value % 100;
	dd = form.start_dt_dy.value;
	var d = new Date();
	start_dt = new Date(yy,mm-1,dd,d.getUTCHours(),d.getUTCMinutes(),d.getUTCMilliseconds());
	var	msPerDay = 24*60*60*1000;
	
	if (form.numDays) {
		if (form.numDays.value == "") {
			yy = form.end_dt_yrmn.value / 100;
			if (!dontAutoCalcYear)
				yy = CalcTheYearByMonthDay(yy, mm, dd);
			mm = form.end_dt_yrmn.value % 100;
			
			dd = form.end_dt_dy.value;
			end_dt = new Date(yy,mm-1,dd,d.getUTCHours(),d.getUTCMinutes(),d.getUTCMilliseconds());
			numDays = (end_dt.valueOf()-start_dt.valueOf())/msPerDay;
			if (numDays <= form.numDays.length)
				form.numDays.value = numDays;				
			else
				return false;
		}
		else
			numDays = parseInt(form.numDays.value,10);
	}
	end_dt = new Date(start_dt.getTime() + msPerDay * (numDays-ExtraDays(servType, startTime, endTime, start_dt, end_dt)));
	SelComboByValue(form.end_dt_yrmn,buildMYItemValueByDate(end_dt));
	if (servType != "7")
		SelComboByIndex(form.end_dt_dy, end_dt.getDate()); 				
	updateDaysDesc();
}

//The selected index will be set only if it there is an item to set it to it.
function SelComboByIndex(combo, newSelectedIndex)
{
	if (combo.length < newSelectedIndex) {
		return false;
	}
	combo.selectedIndex = newSelectedIndex - 1;
	return true;
}

//Select combo item by given required value
function SelComboByValue(combo,value)
{
	//Look for the item
	for( var i = 0 ; i < combo.length ; i++ )
	{
		if( combo[i].value == value )
		{
			//Select the item
			combo.selectedIndex = i;
			break;
		}
	}
	if (form.end_dt_yrmn)	
		updateDaysCombo(form.end_dt_yrmn,form.end_dt_dy);
}

//function to return the day's number according to a specific date
function getDayDesc(year, month, day)
{
	var date = new Date(year, month-1 , day);
	return date.getDay();
}

//Get the selected month in the month + year combo
function getMYMonth(combo)
{
	return (combo.value % 100);
}

//Get the selected year in the month + year combo
function getMYYear(combo)
{
	return Math.floor(combo.value / 100);
}

//Build key value to be saved in the combobox item value
//KeyValue = year *  100 + month by given date object
function buildMYItemValueByDate(date)
{
	return (date.getYear() * 100 + (date.getMonth() + 1));	
}

//Build key value to be saved in the combobox item value by given month and year
//KeyValue = year *  100 + month
function buildMYItemValue(month,year)
{
	return (year*100 + month);
}

//Calculate the number of days in a given month and year
//year - requested year
//month - requested month
function getNumOfDays(year,month)
{
	var ONE_DAY_MILI = 3600*1000*24;
	var dateObj    = new Date(year,month - 1,1);
	var ret = 0;
	//Count the days while the month didn't change
	while( dateObj.getMonth() + 1 == month )
	{
		//move to the next day and increase the number of days
		dateObj.setTime(dateObj.getTime() + ONE_DAY_MILI );
		if( (dateObj.getDate() - 1) != ret )
			ret++;
	} 
	return ret;
}	

//Update the days combo by the givven month and year
//monthYearCombo - month year combo box
//daysCombo - days combo box to be updated 
function updateDaysCombo(monthYearCombo,daysCombo)
{
	if (monthYearCombo == null || monthYearCombo == null )
		return;
	//Get the year
	var month = getMYMonth(monthYearCombo);
	//Get the month
	var year  = getMYYear(monthYearCombo);
	//Get the number of days
	var numOfDays = getNumOfDays(year,month);
	//If there are extra items in the combo, remove them
	while( daysCombo.length > numOfDays )
		daysCombo.remove(daysCombo.length - 1);
	//If there are missing items in the combo, add them
	while( daysCombo.length < numOfDays )
		daysCombo[daysCombo.length] = new Option(daysCombo.length + 1,daysCombo.length + 1);		
}		

//Open calendar dialog
function openCalendarPage(comboMY,comboDay) 
{	 
    if (window.event.srcElement.disabled)
        return;
	//Get the selected month and year from the relevant combo box
	var startMonth = getMYMonth(comboMY);
	var startYear  = getMYYear(comboMY);
	var xPos      = window.event.clientX +20//window.screen.width - 350;
	var yPos	  = window.event.clientY +100//100;	
	if (rtlFlag)
		xPos      = 100;
	//Open the dialog
	var url = "calendar.asp?startFromMonth=" + startMonth + "&startFromYear=" + startYear + "&lastMonth=" + startMonth + "&lastYear=" + END_YEAR;
	var selDate = showModalDialog(url,null,"status:no;dialogWidth:250px;dialogHeight:230px;resizable:no;scroll:no;help:no;dialogLeft:" + xPos + ";dialogTop:" + yPos);	
	if( selDate )
	{
		//Update by the selected date
		SelComboByValue(comboMY , buildMYItemValueByDate(selDate));		
		comboDay.selectedIndex = selDate.getDate() - 1;
		comboMY.onchange();
		comboDay.onchange();		
	}
}

//Open calendar dialog connected to dd/mm/yyyy input text fields
function openCalendarPage2(dyFld,mnFld,yrFld,defDateFld,allowHistory) 
{	 
    if (window.event.srcElement.disabled)
        return;
	//Get the selected month and year from the relevant combo box
	var startMonth = mnFld.value;
	var startYear  = yrFld.value;
    var s = "2000";
    while (startYear.length < 4)
        startYear = s.substr(4-startYear.length-1,1)+startYear;
	if ((startMonth == 0 || startYear == 0) && defDateFld != "") {
	    var dt = StrToDate(defDateFld.value)
	    startMonth = dt.getMonth()+1;
	    startYear = dt.getFullYear();
	}
	var xPos      = window.screen.width - 350;
	var yPos	  = 100;	
	if (rtlFlag)
		xPos      = 100;
	//Open the dialog
	var url = "calendar.asp?startFromMonth=" + startMonth + "&startFromYear=" + startYear + "&lastMonth=" + startMonth + "&lastYear=" + END_YEAR;
	if (typeof(allowHistory) != "undefined")
	    url += "&allowHistory=1";
	var selDate = showModalDialog(url,null,"status:no;dialogWidth:250px;dialogHeight:230px;resizable:no;scroll:no;help:no;dialogLeft:" + xPos + ";dialogTop:" + yPos);	
	if( selDate )
	{
		//Update by the selected date
		dyFld.value = selDate.getDate();
		mnFld.value = selDate.getMonth()+1;
		yrFld.value = selDate.getFullYear();
		if (dyFld.value.length < 2) dyFld.value = "0" + dyFld.value;
		if (mnFld.value.length < 2) mnFld.value = "0" + mnFld.value;
		defDateFld.value = yrFld.value + mnFld.value + dyFld.value;
		try {
		    UpdateAfterSelectDate(defDateFld);
		}
		catch (e) {}
	}
}


//Open calendar dialog with history dates
function openCalendarPage_history(comboMY,comboDay,historyStartMonth,historyStartYear,historyEndMonth,historyEndYear) 
{	 
	//Get the selected month and year from the relevant combo box
	var startMonth = getMYMonth(comboMY);
	var startYear  = getMYYear(comboMY);
	var xPos      = window.screen.width - 350;
	var yPos	  = 100;	
	if (rtlFlag)
		xPos      = 100;
	//Open the dialog
	var url = "calendar.asp?startFromMonth=" + startMonth + "&startFromYear=" + startYear + "&lastMonth=" + historyEndMonth + "&lastYear=" + historyEndYear + "&historyStartMonth=" + historyStartMonth + "&historyStartYear=" + historyStartYear;
	var selDate = showModalDialog(url,null,"status:no;dialogWidth:250px;dialogHeight:230px;resizable:no;scroll:no;help:no;dialogLeft:" + xPos + ";dialogTop:" + yPos);	
	if( selDate )
	{
		//Update by the selected date
		SelComboByValue(comboMY , buildMYItemValueByDate(selDate));		
		comboDay.selectedIndex = selDate.getDate() - 1;
		comboMY.onchange();
		comboDay.onchange();		
	}
}
