//*********************** ES COPYRIGHT START  *********************************
// @copyright(disclaimer) 
// Licensed Materials - Property of IBM
// IBM OmniFind Enterprise Edition V8.4 (Program Number:  5724-C74)
// (c ) Copyright IBM Corp. 2003, 2006.  All Rights Reserved.
// 
// US Government Users Restricted Rights                                  
// Use, duplication or disclosure restricted by GSA ADP Schedule          
// Contract with IBM Corp.     
// 
// DISCLAIMER OF WARRANTIES :                                             
//                                                                        
// Permission is granted to copy and modify this  Sample code, and to           
// distribute modified versions provided that both the copyright        
// notice,- and this permission notice and warranty disclaimer  appear in all copies and modified  
// versions. 
// 
// THIS SAMPLE CODE IS LICENSED TO YOU AS-IS. 
// IBM  AND ITS SUPPLIERS AND LICENSORS  DISCLAIM     
// ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, IN SUCH SAMPLE CODE, INCLUDING THE WARRANTY OF NON-INFRINGEMENT AND THE IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL IBM OR ITS LICENSORS OR SUPPLIERS BE LIABLE FOR ANY DAMAGES ARISING OUT OF THE USE OF  OR INABILITY TO USE THE SAMPLE CODE, DISTRIBUTION OF THE SAMPLE CODE, OR COMBINATION OF THE SAMPLE CODE WITH ANY OTHER CODE. IN NO EVENT SHALL IBM OR ITS LICENSORS AND SUPPLIERS BE LIABLE FOR ANY LOST REVENUE, LOST PROFITS OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,-, EVEN IF IBM OR ITS LICENSORS OR SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.                                     
//                                                                  
// @endCopyright       
//*********************** ES COPYRIGHT END  ***********************************

//var THE_DATE_FORMAT = "m/d/Y";
var THE_DATE_FORMAT = "Y/m/d";
var CALENDAR_HEADING = makeHTMLHeading();
var TODAYS_DATE = new Date();

var calendarTimeout = false;
var textElement = null;
var daysInMonthList = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var calendarDate = null;
var calendarSelectedCell = null;

function getCalendarFor(event, targetId) {
	//alert(FIRST_DAY_OF_WEEK);
	textElement = document.getElementById(targetId);
	var inputDate = new Date(textElement.value);
	if (isFinite(inputDate)) {
		//alert('Yes finite. y='+getFullYear(inputDate)+' m='+inputDate.getMonth()+' d='+inputDate.getDate());
		setCalendar(getFullYear(inputDate), inputDate.getMonth());
		var cellElement = document.getElementById('cell'+inputDate.getDate());
		if (cellElement) {
			cellElement.className = 'wpsPortletAccentArea';
			cellElement.isSelected = true;
			calendarSelectedCell = cellElement;
		}	
	} else {
		if (calendarDate == null) {
			setCalendar();
		}	
	}
	// Display popup
	var calendarObj = document.getElementById('PopUpCalendar');
	calendarObj.style.top = "-1000px";
	calendarObj.style.display = "block";
	var scrollTop = getDocumentElement().scrollTop;
	var scrollLeft = getDocumentElement().scrollLeft;
	var posY = event.clientY; 
	var posX = event.clientX; 
	if (isRTL()) {  
	    if (window.event) { //IE
	     	calendarObj.style.left = (posX - calendarObj.offsetWidth)+"px";
	    } else { 
	        calendarObj.style.left = (posX + scrollLeft - calendarObj.offsetWidth) + "px";
	    } 	
	} else {
	    calendarObj.style.left = eval(posX + scrollLeft)+"px";
	}
	calendarObj.style.top = eval(posY + scrollTop)+"px";
	var frame = document.getElementById("calendarIFrame");
	frame.style.top = calendarObj.style.top;
	frame.style.left = calendarObj.style.left;
	frame.style.width = calendarObj.offsetWidth;
	frame.style.height = calendarObj.offsetHeight;
	frame.style.display = "inline"; 
	// Set month in select
	var monthSelect = document.getElementById('calendarMonthSelect');
	monthSelect.selectedIndex = calendarDate.getMonth();
}

function switchMonth(month) {
	setCalendar(getFullYear(calendarDate), month);
}

function moveMonth(dir) {
	var monthSelectElement = document.getElementById('calendarMonthSelect');
	var year = getFullYear(calendarDate);
	var month;
	if (monthSelectElement != null) {
		if (dir.toLowerCase() == "back") {
			if (monthSelectElement.selectedIndex > 0) 
				monthSelectElement.selectedIndex--;
			else {				
				monthSelectElement.selectedIndex = 11;
				year--;
			}
		} else if (dir.toLowerCase() == "forward") {
			if (monthSelectElement.selectedIndex < 11)
				monthSelectElement.selectedIndex++;
			else {		
				monthSelectElement.selectedIndex = 0;
				year++;
			}
		} 
		month = monthSelectElement.selectedIndex;
	}
	setCalendar(year, month);
}

function moveYear(dir) {
	var curYear = getFullYear(calendarDate);
	if ((dir.toLowerCase() == "back")) {
		curYear--;
	} else if ((dir.toLowerCase() == "forward")) {
		curYear++;
	}
	setCalendar(curYear, calendarDate.getMonth());
}

function selectDay(selectedDay, cellElement) {
	if (calendarSelectedCell) {
		calendarSelectedCell.className = '';
		calendarSelectedCell.isSelected=false;
	}
	cellElement.className = 'wpsPortletAccentArea';
	cellElement.isSelected=true;
	calendarSelectedCell = cellElement;
	
	var year  = getFullYear(calendarDate);
	var month = calendarDate.getMonth();
	var day  = selectedDay;
	var dayInt = parseInt(day);
	calendarDate.setDate(dayInt);
	textElement.value = dateFormat(year, month, day);
	hideCalendar();
}

function setCalendar(year, month) {
	if (year == null) {
		year = getFullYear(TODAYS_DATE);
	}
	if (month == null) {
		month = TODAYS_DATE.getMonth();
	}
	if (month == 1) {
		daysInMonthList[1] = (isLeap(year)) ? 29 : 28;
	}
	if (calendarDate == null) {
	   calendarDate = new Date();
	}   
	calendarDate.setYear(year);
	calendarDate.setMonth(month);
	calendarDate.setDate(1);
	updateCalendarHTML();
}
function updateCalendarHTML() {
	// Set year
	document.getElementById('year').innerHTML = getFullYear(calendarDate);
	
	//Set monthDays
	var year  = getFullYear(calendarDate);
	var month = calendarDate.getMonth();
	var dayOfWeekNumber = calendarDate.getDay();
	var daysInMonth = daysInMonthList[month];
	var date = 1;
	var tableData="";
	for (var row = 0; row < 7; ++row) {
		if (date > daysInMonth) {
			break;
		}
		var rowdata = "";
		for (var col = 0; col < 7; ++col) {
		    var isEmptyCell = false;
			if (date > daysInMonth) {
			    isEmptyCell = true;
			} else if (row == 0) {
			    if (FIRST_DAY_OF_WEEK == 7) { //if Saturday is start of calendar
			        if (dayOfWeekNumber != 6) { //if not Saturday
			           if (col < (dayOfWeekNumber + 1)) {
			               isEmptyCell = true;
			           }
			        }        
			    } else if (FIRST_DAY_OF_WEEK == 2) { //if Monday is start of calendar
			        if (dayOfWeekNumber == 0) { //if Sunday
			            if (col < 6) { 
			               isEmptyCell = true;
				        } else if (col < (dayOfWeekNumber - FIRST_DAY_OF_WEEK + 1)) {
			               isEmptyCell = true;
			            }
			        } else if (col < (dayOfWeekNumber - FIRST_DAY_OF_WEEK + 1)) {
			            isEmptyCell = true;
			        }   
			    } else {      
			       if (col < (dayOfWeekNumber - FIRST_DAY_OF_WEEK + 1)) {
			           isEmptyCell = true;
			       }    
			    }  
			}
			if (isEmptyCell) {
				rowdata += makeCellHTML(year, month, 0);
			} else {
				rowdata += makeCellHTML(year, month, date);
				++date;
			}
		}
		tableData += "<tr align=\"center\">\n" + rowdata + "</tr>\n";
	}
	var tableStart = "<table border=\"1\" cellspacing=\"0\" width=\"100%\">";
	var tableEnd = "</table>";
	document.getElementById('monthDays').innerHTML = tableStart + CALENDAR_HEADING + tableData + tableEnd;
}
function makeHTMLHeading() {
   var heading = "<tr align=\"center\" class=\"wpsPortletToolbarButton\">";
   if (FIRST_DAY_OF_WEEK == 1)
      heading += "<td width=\"20\"><b>"+SUNDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK <= 2)
      heading += "<td width=\"20\"><b>"+MONDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK <= 3)
      heading += "<td width=\"20\"><b>"+TUESDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK <= 4)
      heading += "<td width=\"20\"><b>"+WEDNESDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK <= 5)
      heading += "<td width=\"20\"><b>"+THURSDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK <= 6)
      heading += "<td width=\"20\"><b>"+FRIDAY+"</b></td>";
   heading += "<td width=\"20\"><b>"+SATURDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK > 1)
      heading += "<td width=\"20\"><b>"+SUNDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK > 2)
      heading += "<td width=\"20\"><b>"+MONDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK > 3)
      heading += "<td width=\"20\"><b>"+TUESDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK > 4)
      heading += "<td width=\"20\"><b>"+WEDNESDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK > 5)
      heading += "<td width=\"20\"><b>"+THURSDAY+"</b></td>";
   if (FIRST_DAY_OF_WEEK == 7)
      heading += "<td width=\"20\"><b>"+FRIDAY+"</b></td>";
   heading += "</tr>";
   return heading;
}
function makeCellHTML(year, month, day) {
	var CELL_START = "<td width=\"20\" ";
	var CELL_END = "</span></td>\n";
	var EVENTS = "onMouseOver=\"this.className='wpsPortletAccentArea'\" onMouseOut=\"if(!this.isSelected) this.className=''\" onMouseUp=\"selectDay('"+day+"', this);\" ";
	var spanStyle = "";
	if (GetBrowserType() == 0) {
	   spanStyle = "style=\"cursor: hand\"";
	} else {
	   spanStyle = "style=\"cursor: default\"";
	}
	var HAND_CURSOR = "<span " + spanStyle + ">";
	var DEFAULT_CURSOR = "<span style=\"cursor: default\">";
	if (day == 0) {
		return CELL_START + ">" + DEFAULT_CURSOR + "&nbsp;" + CELL_END;
	} else {
	    var dayString = day+"";
		var cellValue;
		if ((TODAYS_DATE.getDate() == day) && (TODAYS_DATE.getMonth() == month) && (getFullYear(TODAYS_DATE) == year)) {
			cellValue = "<b>"+dayString+"</b>";
		} else {
		    cellValue = dayString;
		}
		return CELL_START + "id=\"cell"+dayString+"\" " + EVENTS + ">" + HAND_CURSOR + cellValue + CELL_END;
	}
}

function hideCalendar() {
	var calendarObj = document.getElementById('PopUpCalendar');
	calendarObj.style.display = "none";
	var frame = document.getElementById("calendarIFrame");
	frame.style.display = "none";
	calendarTimeout = false;
	textElement = null;
	var monthSelect = document.getElementById('calendarMonthSelect');
	monthSelect.selectedIndex = 0;
}

function isLeap(year) {
	if ((year%400==0)||((year%4==0)&&(year%100!=0))) {
		return true;
	} else {
		return false;
	}
}
  
function getFullYear(dateobj) {
    if (!dateobj) return "";
	var y = dateobj.getYear();
	if (GetBrowserType() != 0 || y < 2000) {
		return y + 1900;
	} else {
		return y;
	}
}

function dateFormat(year, month, day) {
	var crt = "";
	var str = "";
	var chars = THE_DATE_FORMAT.length;
	for (var i = 0; i < chars; ++i) {
		crt = THE_DATE_FORMAT.charAt(i);
		switch (crt) {
		  case "m": str += (month<9) ? ("0"+(++month)) : ++month; break;
		  case "Y": str += year; break;
		  case "y": str += year.substring(2); break;
		  case "d": str += ((THE_DATE_FORMAT.indexOf("m")!=-1)&&(day<10)) ? ("0"+day) : day; break;
		  default: str += crt;
		}
	}
	return unescape(str);
}

