

/* Filename: Calendar.js */

/*

How to use:       

<input type="text" name="Year" value=""\/>
<input type="text" name="Year" value=""\/>
<input type="text" name="Year" value=""\/>
<a name="StartDate" id="StartDate" style="cursor:pointer">
	<img src="[G=/calendar.gif]" onclick="selectDate(document.submitForm.elements['StartDate'], 'StartDate' );" \/>
<\/a>

*/

var months=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var days=new Array("S","M","T","W","T","F","S");
var calendar = new CalendarPopup("Calendar", months, days);

function selectDate( fieldYear, fieldMonth, fieldDay, anchor ) {
    calendar.select( fieldYear, fieldMonth, fieldDay, anchor );
    return false;
}

function CalendarPopup( title, months, days ) {
	var c=new PopupWindow();
	c.returnFunction = "CalendarPopup_tmpReturnFunction";
	c.weekStartDay = 0;
	c.currentDate = null;
	window.CalendarPopup_targetYear=null;
	window.CalendarPopup_targetMonth=null;
	window.CalendarPopup_targetDay=null;
	c.setReturnFunction = CalendarPopup_setReturnFunction;
	c.setMonthNames = CalendarPopup_setMonthNames;
	c.setDayHeaders = CalendarPopup_setDayHeaders;
	c.setWeekStartDay = CalendarPopup_setWeekStartDay;
	c.showCalendar = CalendarPopup_showCalendar;
	c.hideCalendar = CalendarPopup_hideCalendar;
	c.getStyles = CalendarPopup_getStyles;
	c.refreshCalendar = CalendarPopup_refreshCalendar;
	c.getCalendar = CalendarPopup_getCalendar;
	c.select = CalendarPopup_select;
	c.popupTitle = new String( title );
	c.monthNames=new Array( months.length );
	c.setMonthNames( months );
	c.dayHeaders=new Array( days.length );
	c.setDayHeaders( days );
	return c;
}

function CalendarPopup_tmpReturnFunction(y,m,d) {
	window.CalendarPopup_targetYear.value=y;
	if( (""+m).length==1 ) {
		m = "0"+m; 
	}
	window.CalendarPopup_targetMonth.value=m;
	if( (""+d).length==1 ) { 
		d = "0"+d; 
	}
	window.CalendarPopup_targetDay.value=d;
}

function CalendarPopup_setReturnFunction(name) { 
	this.returnFunction = name; 
}

function CalendarPopup_setMonthNames( months ) {	
	for (i=0; i < months.length; i++){
		this.monthNames[i] = months[i]; 
	}			  		
}

function CalendarPopup_setDayHeaders( days ) {
	for (var i=0; i<days.length; i++) {
		this.dayHeaders[i] = days[i]; 
	}
}

function CalendarPopup_setWeekStartDay(day) { 
	this.weekStartDay = day; 
}

function CalendarPopup_hideCalendar() {
	if (arguments.length > 0) {
		window.pWO[arguments[0]].hidePopup(); 
	} else { 
		this.hidePopup(); 
	}
}

function CalendarPopup_refreshCalendar(index) {
	var calObject = window.pWO[index];
	if (arguments.length>1) {
		calObject.populate(calObject.getCalendar(arguments[1],arguments[2],arguments[3],arguments[4],arguments[5]));
	} else {
		calObject.populate(calObject.getCalendar());
	}
	calObject.refresh();
}

function CalendarPopup_showCalendar(a) {
	this.populate(this.getCalendar());
	this.showPopup(a);
}

function CalendarPopup_select(inputDate, linkname) {
	window.CalendarPopup_targetYear="";
	window.CalendarPopup_targetMonth="";
	window.CalendarPopup_targetDay="";
	window.CalendarPopup_targetDate=inputDate;
	var actualDate = "";
	if(inputDate.value!="") {
		actualDate+=inputDate.value;
	}
	if(actualDate!="") {
		var time=getDateFromFormat(actualDate,'y/M/d');
		if(time==0){
			this.currentDate=null; 
		} else{ 
			this.currentDate=new Date(time); 
		}
	} else{ this.currentDate=null; }
	this.showCalendar(linkname);
}

function CalendarPopup_select(inputYear, inputMonth, inputDay, linkname) {
    window.CalendarPopup_targetYear=inputYear;
    window.CalendarPopup_targetMonth=inputMonth;
    window.CalendarPopup_targetDay=inputDay;
    var actualDate = "";
    if(inputYear.value!="") {
        actualDate+=inputYear.value;
        if(inputMonth.value!="") {
            actualDate+='/'+inputMonth.value;
            if(inputDay.value!="") {
                actualDate+='/'+inputDay.value;
            } else {
                actualDate+='/01';
            }
        } else {
            actualDate+='/01/01';
        }
    }
    if(actualDate!="") {
        var time=getDateFromFormat(actualDate,'y/M/d');
        if(time==0) { 
            this.currentDate=null; 
        } else { 
            this.currentDate=new Date(time); 
        }
    } else { 
        this.currentDate=null; 
    }
    this.showCalendar(linkname);
}

function CalendarPopup_getStyles() {
	var result = "";
	result += "\<STYLE>\n";
	result += "TABLE.CalendarTable { border: 1px solid #666666; }\n";
	result += "TD.cal { font-family:verdana,arial; font-size: 8pt; font-weight: bold; }\n";
	result += "TD.calmonth { font-family:verdana,arial; font-size: 8pt; text-align: right;}\n";
	result += "TD.caltoday { font-family:verdana,arial; font-size: 8pt; text-align: right; color: white; background-color:#B1CAFF; border-width:1; border-type:solid; border-color:#800000; }\n";
	result += "A.textlink { font-family:verdana,arial; font-size: 8pt; height: 20px; color: black; }\n";
	result += ".disabledtextlink { font-family:verdana,arial; font-size: 8pt; height: 20px; color: #808080; }\n";
	result += "A.cal { text-decoration:none; color:#000000; }\n";
	result += "A.calthismonth { text-decoration:none; color:#000000; }\n";
	result += "A.calothermonth { text-decoration:none; color:#808080; }\n";
	result += ".calnotclickable { color:#808080; }\n";
	result += "<\/STYLE>\n";
	return result;
}
function CalendarPopup_getCalendar() {
	var now=new Date();
	var daysinmonth=new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	var windowref="window.opener.";
	var result="";			  		

	result += "<HTML><HEAD><TITLE>"+this.popupTitle+"<\/TITLE>"+this.getStyles()+"<\/HEAD><BODY MARGINWIDTH=0 MARGINHEIGHT=0 TOPMARGIN=0 RIGHTMARGIN=0 LEFTMARGIN=0>\n";
	result += '<CENTER><TABLE WIDTH=100% BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0>\n';
	if(this.currentDate==null){ 
		this.currentDate = now; 
	}
	if(arguments.length > 0){ 
		var month = arguments[0]; 
	}else{ 
		var month = this.currentDate.getMonth()+1; 
	}
	if(arguments.length > 1){ 
		var year = arguments[1]; 
	} else{ 
		var year = this.currentDate.getFullYear(); 
	}	

	var current_month = new Date(year,month-1,1);
	var display_year = year;
	var display_month = month;
	var display_date = 1;
	var weekday= current_month.getDay();
	var offset = 0;

	if(((year%4 == 0)&&(year%100 != 0)) || (year%400 == 0)){ 
		daysinmonth[2] = 29;
	}			  
	if(weekday >= this.weekStartDay){ 
		offset = weekday - this.weekStartDay;
	} else{ 
		offset=7-this.weekStartDay+weekday; 
	}
	if(offset > 0) {
		display_month = display_month - 1;
		if(display_month < 1){ 
			display_month = 12; 
			display_year = display_year - 1; 
		}
		display_date = daysinmonth[display_month]-offset+1;
	}

	var next_month=month+1;
	var next_month_year=year;

	if(next_month > 12){ 
		next_month=1; 
		next_month_year = next_month_year + 1; 
	}

	var last_month=month-1;
	var last_month_year=year;

	if(last_month < 1) { 
		last_month=12; 
		last_month_year = last_month_year - 1; 
	}

	var date_class;
	result += '<TABLE WIDTH=144 BORDER=0 BORDERWIDTH=0 CELLSPACING=0 CELLPADDING=0 class="CalendarTable">\n';
	result += '<TR BGCOLOR="##B1CAFF">\n';

	var refresh = 'javascript:'+windowref+'CalendarPopup_refreshCalendar';
	var td = '<TD BGCOLOR="#B1CAFF" CLASS="cal" ALIGN=CENTER VALIGN=MIDDLE ';

	result += td + ' WIDTH=10><B><A CLASS="cal" HREF="'+refresh+'('+this.i+','+last_month+','+last_month_year+');">&lt;<\/A><\/B><\/TD>';
	result += td + ' WIDTH=58>'+this.monthNames[month-1]+'<\/TD>';
	result += td + ' WIDTH=10><B><A CLASS="cal" HREF="'+refresh+'('+this.i+','+next_month+','+next_month_year+');">&gt;<\/A><\/B><\/TD>';
	result += td + ' WIDTH=10>&nbsp;<\/TD>';
	result += td + ' WIDTH=10><B><A CLASS="cal" HREF="'+refresh+'('+this.i+','+month+','+(year-1)+');">&lt;<\/A><\/B><\/TD>';
	result += td + ' WIDTH=36>'+year+'<\/TD>';
	result += td + ' WIDTH=10><B><A CLASS="cal" HREF="'+refresh+'('+this.i+','+month+','+(year+1)+');">&gt;<\/A><\/B><\/TD>';
	result += '<\/TR><\/TABLE>\n';
	result += '<TABLE WIDTH=120 BORDER=0 CELLSPACING=1 CELLPADDING=0 ALIGN=CENTER>\n';
	result += '<TR>\n';

	var td = '	<TD CLASS="cal" ALIGN=RIGHT WIDTH=14%>';

	for(var j=0; j<7; j++){
		result += td+this.dayHeaders[(this.weekStartDay+j)%7]+'<\/TD>\n';
	}
	result += '<\/TR>\n';
	result += '<TR><TD COLSPAN=7 ALIGN=CENTER><IMG SRC="graypixel.gif" WIDTH=120 HEIGHT=1><\/TD><\/TR>\n';
	for(var row=1; row<=6; row++) {
		result += '<TR>\n';
		for(var col=1; col<=7; col++) {
			if(display_month==month){ 
				date_class="calthismonth";
			}else{ 
				date_class="calothermonth";
			}
			if((display_month==this.currentDate.getMonth()+1) && (display_date==this.currentDate.getDate()) && (display_year==this.currentDate.getFullYear())){
				td_class="caltoday";
			} else { 
				td_class="calmonth"; 
			}
			result += '	<TD CLASS="'+td_class+'"><A HREF="javascript:'+windowref+this.returnFunction+'('+display_year+','+display_month+','+display_date+');'+windowref+'CalendarPopup_hideCalendar(\''+this.i+'\');" CLASS="'+date_class+'">'+display_date+'<\/A><\/TD>\n';
			display_date++;
			if(display_date > daysinmonth[display_month]){ 
				display_date=1; display_month++; 
			}
			if(display_month > 12){ 
				display_month=1; display_year++; 
			}
		}
		result += '<\/TR>';
	}
	result += '<\/TABLE><\/CENTER><\/TD><\/TR><\/TABLE>\n';
	result += "<\/BODY><\/HTML>\n";
	return result;
}

function PopupWindow() {
	if(!window.pWI){ window.pWI=0; }
	if(!window.pWO){ window.pWO=new Array(); }
	if(!window.lA){
		window.lA=true;
		PopupWindow_attachListener();
	}
	this.i=pWI++;
	pWO[this.i]=this;
	this.pW=null;
	this.w=150;
	this.h=150;
	this.pop=false;
	this.html="";
	this.offX=-152;
	this.offY=25;
	this.getXYPosition=PopupWindow_getXYPosition;
	this.populate=PopupWindow_populate;
	this.refresh=PopupWindow_refresh;
	this.showPopup=PopupWindow_showPopup;
	this.hidePopup=PopupWindow_hidePopup;
}

function PopupWindow_getXYPosition(a) {
	var coor=getAnchorWindowPosition(a);
	this.x = coor.x;
	this.y = coor.y;
}

function PopupWindow_populate(html) {
	this.html = html; this.pop = false;
}

function PopupWindow_refresh() {
	if (this.pW!=null && !this.pW.closed) {
		this.pW.document.open();
		this.pW.document.writeln(this.html);
		this.pW.document.close();
		this.pW.focus();
	}
}

function PopupWindow_showPopup(a) {
	this.getXYPosition(a);
	this.x += this.offX;
	this.y += this.offY;
	if(!this.pop && (this.html != "")) {
		this.pop = true;
		this.refresh();
	}
	if (screen && screen.availHeight) {
		if ((this.y + this.h) > screen.availHeight) {
			this.y = screen.availHeight - this.h;
		}
	}
	if (screen && screen.availWidth) {
		if ((this.x + this.w) > screen.availWidth) {
			this.x = screen.availWidth - this.width;
		}
	}
	this.pW=window.open("/WXUtil/WXBlank.htm","window_"+a,"toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable,dependent,width="+this.w+",height="+this.h+",screenX="+this.x+",left="+this.x+",screenY="+this.y+",top="+this.y+"");
	this.refresh();
}

function PopupWindow_hidePopup() {
	if (this.pW && !this.pW.closed) {
		this.pW.close();
		this.pW=null;
	}
}

function PopupWindow_hidePopupWindows(e) {
	for (var i=0; i<pWO.length; i++) {
		if(pWO[i] != null) {
			var p=pWO[i];
			p.hidePopup();
		}
	}
}

function PopupWindow_attachListener() {
	if(document.layers){ 
		document.captureEvents(Event.MOUSEUP); 
	}
	window.pWOEL=document.onmouseup;
	if(window.pWOEL != null){
		document.onmouseup = new Function("window.pWOEL(); PopupWindow_hidePopupWindows();");
	} else {
		document.onmouseup = PopupWindow_hidePopupWindows;
	}
}

function getAnchorWindowPosition(a) {
	var coor=getAnchorPosition(a);
	var x=0;
	var y=0;
	if(document.getElementById){
		if(isNaN(window.screenX)){
			x=coor.x-document.body.scrollLeft+window.screenLeft;
			y=coor.y-document.body.scrollTop+window.screenTop;
		} else {
			x=coor.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
			y=coor.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	} else if (document.all) {
		x=coor.x-document.body.scrollLeft+window.screenLeft;
		y=coor.y-document.body.scrollTop+window.screenTop;
	} else if (document.layers) {
		x=coor.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		y=coor.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
	}
	coor.x=x;
	coor.y=y;
	return coor;
}

function getAnchorPosition(a) {
	var coordinates=new Object();
	var x=0,y=0;
	if(document.all) {
		x=getPageOffsetLeft(document.all[a]);
		y=getPageOffsetTop(document.all[a]);
	} else if(document.getElementById) {
		var o=document.getElementById(a);
		x=o.offsetLeft; y=o.offsetTop;
	} else if (document.layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==a) { found=1; break; }
		}
		if (found==0) { coordinates.x=0; coordinates.y=0; return coordinates; }
		x=document.anchors[i].x;
		y=document.anchors[i].y;
	} else {
		coordinates.x=0; coordinates.y=0; return coordinates;
	}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
}

function getPageOffsetLeft(el) {
	var ol=el.offsetLeft;
	while((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
}

function getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
}

function _isInteger(val) {
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
	}
	return true;
}

function _getInt(str,i,minlength,maxlength) {
	for (var x=maxlength; x>=minlength; x=x-1) {
		var token=str.substring(i,i+x);
		if (token.length < minlength) { return null; }
		if (_isInteger(token)) { return token; }
	}
	return null;
}

function getDateFromFormat(val,format) {
	val=val+"";
	format=format+"";
	var i_val=0;
	var i_format=0;
	var c="";
	var token="";
	var token2="";
	var x,y;
	var now=new Date();
	var year=now.getYear();
	var month=now.getMonth()+1;
	var date=now.getDate();

	while (i_format < format.length) {
		c=format.charAt(i_format);
		token="";
		while((format.charAt(i_format)==c) && (i_format < format.length)) {
			token += format.charAt(i_format++);
		}
		if(token=="y") {
			x=2;y=4;
			year=_getInt(val,i_val,x,y);
			if(year==null) { 
				return 0; 
			}
			i_val += year.length;
			if (year.length==2) {
				if(year > 70) { 
					year=1900+(year-0); 
				}else{ 
					year=2000+(year-0); 
				}
			}
		} else if(token=="M") {
			month=_getInt(val,i_val,token.length,2);
			if(month==null||(month<1)||(month>12)){
				return 0;
			}
			i_val+=month.length;
		} else if(token=="d") {
			date=_getInt(val,i_val,token.length,2);
			if(date==null||(date<1)||(date>31)){
				return 0;
			}
			i_val+=date.length;
		} else {
			if (val.substring(i_val,i_val+token.length)!=token) {
				return 0;
			}
			else {
				i_val+=token.length;
			}
		}
	}
	if (month==2) {
		if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) {
			if (date > 29){ 
				return false; 
			}
		} else { 
			if (date > 28) { 
				return false; 
			} 
		}
	}
	if ((month==4)||(month==6)||(month==9)||(month==11)) {
		if (date > 30) { return false; }
	}
	var newdate=new Date(year,month-1,date,'01','00','00');
	return newdate.getTime();
}
