// $Log$   
/*
jsCalendar.js
created by: Mohamed Fathy
*/


/****** Globals ********/
var OpenedCalendars=new Array();
var OpenedCalsCount=0;


/****** Globals for customizing the calendar colors ********/
//You can change these values
var font_face	= "verdana";
var font_size	= 12;
var font_color  = "#FFFFFF";
var border_size	= 0;

var background_color	  = "#808080";
var topbar_bg_color	  = "#000000";
var topbar_font_color     = "#FFFFFF";

var weekdays_font_color   = "#C4AC64";
var weekdays_bg_color	  = "#696969";

var cell_bg_color         = "#C0C0C0";
var cell_mover_color      = "#FFFFFF";
var cell_mover_font_color = "#000000";
var selected_bg_color	  = "#C4AC64"; 
var selected_font_color	  = "#FFFFFF";
var cell_font_color		  = "#FFFFFF"; 



var rsDaysNames;
var rsMonthsNamesAbbrev;
var rsMonthsNames;

isNativeLoaded=false;
isNavigator = (navigator.appName == 'Netscape');
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;


//rsDaysNames = "Sat;Sun;Mon;Tues;Wed;Thur;Fri";
//rsMonthsNamesAbbrev = "Jan;Feb;March;Apr;May;June;Jully;Aug;Sep;Oct;Nov;Dec";
//rsMonthsNames = "Jan;Feb;March;Apr;May;June;Jully;Aug;Sep;Oct;Nov;Dec";
rsDaysNames = "Sat;Sun;Mon;Tues;Wed;Thur;Fri";
rsMonthsNamesAbbrev = "Jan;Feb;March;Apr;May;June;Jully;Aug;Sep;Oct;Nov;Dec";
rsMonthsNames = "January;February;March;April;May;June;July;August;September;October;November;December";


/*************************************************************************************************/
// the constructor

function Calendar(calname)
{
this.OutItem=null;
this.CalendarDivId=null;
this.LocaleDateReturn=null;
this.GregDay_Return=null;
//this.GregMonth_Return=null;
//this.GregYear_Return=null;
this.CalOCX=null
this.formIndex=null;
this.name = calname;

this.type = "YearMonthDays";

this.isNavigator=false;
this.isNS4=false;
this.isIE4=false;
this.isIE5=false;
this.isNS6=false;

this.currentDay  = null;
this.currentMonth= null;
this.currentYear = null;
this.currentEra  = null;
this.currentEraNo= null;

this.initGregDay  = null;
this.initGregMonth= null;
this.initGregYear = null;

this.ArrayEra     =null;
this.ArrayEraStart=null;
this.ArrayEraEnd  =null;
this.ArrayEraLocaleEndYear =null;
this.ArrayEraLocaleEndMonth=null;
this.ArrayEraLocaleEndDay  =null;

this.monthsOnly=null;
this.CalendarOpened = false;

//critical Dates
this.criticalStartDay=null;
this.criticalStartMonth=null;
this.criticalStartYear=null;
this.criticalEndDay=null;
this.criticalEndMonth=null;
this.criticalEndYear=null;

this.getWeekDays = getWeekDays;
this.getMonth	 = getMonth;
this.GetDaysNoInMonth = GetDaysNoInMonth;
this.getDayNoInWeekAfterOffset = getDayNoInWeekAfterOffset;
this.GregTimeToSystemTime = GregTimeToSystemTime;
this.convertToGregorian = convertToGregorian;
this.formatDate = formatDate;
this.RetreiveEras = RetreiveEras;
this.assertInEra = assertInEra;
this.getEraNo = getEraNo;

this.setCriticalDates = setCriticalDates;

//Events
this.MouseOverCell = MouseOverCell;
this.MouseOutCell = MouseOutCell;
this.MouseClickCell = MouseClickCell;
this.clickCloseButton = clickCloseButton;

this.roll = roll;
this.drawCalendar = drawCalendar;
this.setOutItem =setOutItem;
this.wwrite = wwrite;
this.freeCalendar = freeCalendar;
this.initializeDate = initializeDate;
this.srvInitializeDate = srvInitializeDate;
this.viewCalendar = viewCalendar;
this.initailize = initailize;
this.show_calendar = show_calendar;
this.A = A;

this.openNewWin = openNewWin;
}

/****************************************************************************************************/



function getWeekDays()
{
	if(this.CalOCX != null)
		DaysNames = this.CalOCX.jsRetreiveWeakDaysNames(0);
	else
		DaysNames = rsDaysNames;

	return tokenize(DaysNames);
}

function getMonth(n, apprev)
{
	if(!apprev)
		MonthsNames = rsMonthsNamesAbbrev;
	else
		MonthsNames = rsMonthsNames;

	if(this.CalOCX != null)
		MonthsNames = this.CalOCX.jsRetrieveMonthsNames(apprev);

	arr = tokenize(MonthsNames);
	if(n<0 || n>13)
		return arr;
	else
		return arr[n];

}

function GetDaysNoInMonth(day, month, year, era)
{
	ocx = arguments[4];
	if(ocx==null)
		ocx=this.CalOCX;

	if(ocx != null) 
	{
		y = year;
		e = era;
		values = null;
		if(era!=null && era!="" && era!="undefined")
		{
			vals = this.convertToGregorian(day, month, year, era);
			date = new Date(vals[2], vals[1], vals[0]);
			date.setDate(date.getDate()-day+1);

			{
				values = this.assertInEra(date.getDate(), date.getMonth(), date.getFullYear(), era);
				if(values!=null && values[0] != -1) { //no change
					y = values[0];
					e = values[1];
				}
			}
		}
		m = tryGetDaysNoInMonth(month, parseInt(y), e,ocx);
	}
	else
	{
		DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
		m = DOMonth[month];
		if((year%4)==0 && month==1)
			m++;
	}
	return m;
}

function getDayNoInWeekAfterOffset(day, currentDNW)
{
	var dayNo=-1;
	offset = day-this.currentDay;

	if(offset>=0)
		dayNo = (offset+currentDNW)%7;
	else
		dayNo = (7+offset%7+currentDNW)%7;

	return dayNo;
}

function GregTimeToSystemTime(day, month, year, ocx)
{ 
	if(ocx == null)
		ocx = this.CalOCX;

	if(ocx != null)
	{
		str = tryGregTimeToSystemTime(day, month, year, ocx);
	}
	else
	{
		d = new Date(year, month, day);
		dnw = d.getDay();
		dnm = this.GetDaysNoInMonth(day, month, year, "");
		str = ""+day+";"+month+";"+year+";"+dnw+";"+dnm;
	}

	return tokenize(str);
}

function convertToGregorian(day, month, year, era)
{
	if(this.CalOCX != null)
	{
		//str = this.CalOCX.jsConvertToGregorean(day, month, year, era);
		
		str = tryConvertToGregorean(Number(day), month, year, era, this.CalOCX);
	}
	else
	{
		str = ""+day+";"+month+";"+year;
	}

	return tokenize(str);
}


function formatDate()
{
	day     = arguments[0];
	month   = arguments[1];
	year    = arguments[2];
	era     = arguments[3];
	inclDay = arguments[4];
	ocx     = arguments[5];

	if(ocx != null) {
		ret = tryFormat(day, month, year, era,Number(inclDay),ocx);
	}
	else
	{
		if(inclDay)
			ret = day+" "+this.getMonth(month, 1)+" "+year;
		else
			ret = this.getMonth(month, 1)+" "+year;
	}
	return ret;
}

function RetreiveEras()
{
	if(this.CalOCX != null)
	{
		str = this.CalOCX.jsRetreiveEras();
	}
	else
	{
		str = "";
	}
	return tokenize(str);
}


function assertInEra(day, month, year, inEra)
{
	//coming is greg date/year/month,  and current era

	var outDay=day, outMonth=month, outYear=-1; outEra="";

	if(inEra != "")
	{
		//get era no in the array
		eraNumb = this.getEraNo(inEra);
		
		if( (year==this.ArrayEraEnd[eraNumb].getFullYear()) && (month==this.ArrayEraEnd[eraNumb].getMonth()) && (day>this.ArrayEraEnd[eraNumb].getDate()) ) {
			outEra  = this.ArrayEra[eraNumb+1];
			outYear = 1;
		}
		else if( (year==this.ArrayEraStart[eraNumb].getFullYear()) && (month==this.ArrayEraStart[eraNumb].getMonth()) && (day<this.ArrayEraStart[eraNumb].getDate()) ) {
			outEra   = this.ArrayEra[eraNumb-1];
			outYear  = this.ArrayEraLocaleEndYear[eraNumb-1];
		}
	}

	return tokenize(outYear+";"+outEra);
}

function getEraNo(era)
{
	j=-1;
	if(this.ArrayEra != null) {
		for(i=0, j=0; i<this.ArrayEra.length; i++, j++) {
			if(era == this.ArrayEra[i])   break;
		}
		this.currentEraNo = j;
	}
	return j;
}


function setCriticalDates(EraNum)
{
		vals = this.GregTimeToSystemTime(this.ArrayEraStart[EraNum].getDate(), this.ArrayEraStart[EraNum].getMonth(), this.ArrayEraStart[EraNum].getFullYear());
		this.criticalStartDay = vals[0]; this.criticalStartMonth = vals[1]; this.criticalStartYear = vals[2];

		if(EraNum < this.ArrayEra.length-1 ) {
			vals = this.GregTimeToSystemTime(this.ArrayEraStart[EraNum+1].getDate(), this.ArrayEraStart[EraNum+1].getMonth(), this.ArrayEraStart[EraNum+1].getFullYear());
			this.criticalEndDay = vals[0]; this.criticalEndMonth = vals[1]; this.criticalEndYear = vals[2];
		}
		else {
			this.criticalEndDay=31; this.criticalEndMonth=12; this.criticalEndYear=2019;
		}
}


/*--------------------------------------------------------------------------------*/
// events
/*--------------------------------------------------------------------------------*/
function MouseOverCell(cellid, cellfontid, TopbarTD)
{
	var oYear=this.currentYear, oEra=this.currentEra;
	if(!(this.isNavigator) || this.isNS6)
	{
		cellid.bgColor = cell_mover_color;
		if(!this.isNS6) cellfontid.color=cell_mover_font_color;
		if(this.currentEra != null)
		{
			if( (this.currentYear==this.criticalStartYear) && (this.currentMonth==this.criticalStartMonth) ) {
				if(parseInt(cellid.outerText) < this.criticalStartDay) {
					oYear=this.ArrayEraLocaleEndYear[this.currentEraNo-1];  oEra=this.ArrayEra[this.currentEraNo-1];
				}
			}
			TopbarTD.innerHTML = "<font color='"+topbar_font_color+"'>"+this.getMonth(parseInt(this.currentMonth), 0)+" "+oYear+" "+oEra+"</font>";
		}
	}
}

function MouseOutCell(cellid, cellfontid)
{
	if(!this.isNavigator || this.isNS6)
	{
		if(cellid.outerText==this.currentDay) {
			cellid.bgColor = selected_bg_color;
			if(!this.isNS6) cellfontid.color = selected_font_color;
		}
		else {
			cellid.bgColor = cell_bg_color;
			if(!this.isNS6) cellfontid.color = cell_font_color;
		}
	}
}

function MouseClickCell(id, caller)
{
	if(caller == "CloseBut")
	{
		if(this.initGregDay == null)
			day = 1;
		else
			day = this.initGregDay;

	}
	else if(this.isNavigator)
	{
		if(!this.isNS6)
		{
			day = this.OutItem.document.anchors[id].text;
		}
		else
		{
			str = id.toString();
			if(str.substring(0,4)=="link")
				day = document.anchors[id].text;
			else
				return false;
		}
	}
	else
	{
		day  = parseInt(id.outerText);
	}

	if(day.toString() == "NaN") 
	{
		day = 1;
	}

	oYear= this.currentYear; 
	oEra = this.currentEra;
	if(this.currentEra != null)
	{
		if( (this.currentYear==this.criticalStartYear) && (this.currentMonth==this.criticalStartMonth) ) {
			if(day < this.criticalStartDay) {
				oYear=this.ArrayEraLocaleEndYear[this.currentEraNo-1];  oEra=this.ArrayEra[this.currentEraNo-1];
			}
		}
	}
	this.currentDay  = Number(day);
	this.currentYear = Number(oYear);
	this.currentEra  = oEra;
	//refresh the calendar
	this.roll(0);

	//display the locale date
	this.LocaleDateReturn.value = this.formatDate(this.currentDay, this.currentMonth, this.currentYear, this.currentEra, (!this.monthsOnly), this.CalOCX);

	//put the greg date to the control required
	vals =this.convertToGregorian(this.currentDay, this.currentMonth, this.currentYear, this.currentEra);
	
	monstr = "" + (parseInt(vals[1])+1);
	if(monstr.length < 2)
		monstr = "0"+monstr;
		
	datestr = "" + (vals[0]);
	if(datestr.length < 2)
		datestr = "0"+datestr;

	this.GregDay_Return.value   = "" + vals[2] + monstr + datestr;
	//this.GregMonth_Return.value = parseInt(vals[1])+1;
	//this.GregYear_Return.value  = vals[2];

	this.initGregDay   = vals[0];
	this.initGregMonth = parseInt(vals[1])+1;
	this.initGregYear  = vals[2];

	this.freeCalendar();
}

function clickCloseButton(cel, item)
{
	this.MouseClickCell(cel, "CloseBut");
	this.freeCalendar();
}


/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/

function roll(n)
{
	arr = this.convertToGregorian(this.currentDay, this.currentMonth, this.currentYear, this.currentEra);
	mDate = new Date(Number(arr[2]+".0"),Number(arr[1]+".0"),Number(arr[0]+".0"));
	if( ((mDate.getFullYear()<2019) && n>0) || 
		((mDate.getFullYear()>1971) && n<0) || 
		((mDate.getFullYear()==2019) && (mDate.getMonth()<11) && n<2) ||
		((mDate.getFullYear()==1971) && (mDate.getMonth()>0) && n>-2)  )
	{
		switch(n)
		{
		case -1:
			mDate.setMonth(mDate.getMonth()-1, 1);
			break;
		case -2:
			mDate.setFullYear(mDate.getFullYear()-1);
			break;
		case 1:
			mDate.setMonth(mDate.getMonth()+1, 1);
			break;
		case 2:
			mDate.setFullYear(mDate.getFullYear()+1);
			break;
		}
		
		
		var values = new Array(6);
		values = this.GregTimeToSystemTime(mDate.getDate(), mDate.getMonth(), mDate.getFullYear());
		
		this.currentDay   = parseInt(values[0]);
		this.currentMonth = parseInt(values[1]);
		this.currentYear  = parseInt(values[2]);
		this.currentEra   = values[5];
		dnw				  = parseInt(values[3]);

		if(this.currentEra!="" && this.currentEra!=null)
			this.setCriticalDates(this.getEraNo(this.currentEra));
		
		if(this.currentDay > (i=this.GetDaysNoInMonth(1, this.currentMonth, this.currentYear, this.currentEra)))
		{
			this.currentDay   = 1;
		}

		this.drawCalendar(this.currentDay , this.currentMonth, this.currentYear, values[5], dnw);
	}
}

function tokenize(str)
{
	var values = new Array(15);

	for(i=0; i<15; i++)
	{	
		index     = str.indexOf(';', 0);
		if(index > 0)
		{
			values[i] = str.substring(0,index);
			str	  = str.substring(index+1);
		}
		else
		{
			values[i] = str;
			break;
		}
	}
	return values;
}


function getArrayLength(arr)
{
	len=0;
	for(; len<arr.length; len++) {
		if(arr[len]==null)
			break;
	}

	return len;
}


/*--------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------*/

/*
drawCalendar()
- 
- 
- 
- 
*/
function drawCalendar(Day, Month, Year, Era, dayNoNWeek)
{
	wDays  = this.getWeekDays();
	wMonth = this.getMonth(Month, -1);

	if(Day > this.GetDaysNoInMonth(Day, Month, Year, Era))
	{
		this.currentDay = Day = 1;
	}

	nDays	   = getArrayLength(wDays);
	dayNoNWeek = this.getDayNoInWeekAfterOffset(1, dayNoNWeek);
	nMonthDays = this.GetDaysNoInMonth(Day, Month, Year, Era);

	//free the calendar and refresh
	if(this.CalendarOpened)
		this.freeCalendar();
	
	if(Era == null)
		Era="";

	//------------------------------------- drawing the calendar ----------------------------------------
	//add the top bar and the buttons

	if(this.isNavigator)
	{
		//OutHtml = "<FORM name='inForm'>";
		OutHtml ="";
		A1 = "</A>";
	}
	else
	{
		A1="";
		OutHtml = "";
	}
	OutHtml += "<TABLE dir='ltr' border='"+border_size+"' bgcolor="+background_color+" cellspacing=0'><tr align='center'><td id=TopbarTD name=TopbarTD colspan=3 bgcolor='"+topbar_bg_color+"'>"+"<font color='"+topbar_font_color+"'>"+wMonth+" "+Year+" "+Era+"</font>";
	OutHtml += "<td bgcolor=#000000 align=right valign=top><input name='hid"+this.name+"' type='hidden' value=1><input type='button' onclick='"+this.name+".clickCloseButton( hid"+this.name+", this.OutItem); return true;' value='  x  ' id=closeButton name=closeButton></td>";
	OutHtml += "</td></tr>";
	OutHtml += "<tr align='center'><td colspan=4>"+"<input type='button' onclick='"+this.name+".roll(-2); return true;' value='  << '>";
	OutHtml += "<input type='button' onclick='"+this.name+".roll(-1); return true;' value='  <  '>";
	OutHtml += "<input type='button' onclick='"+this.name+".roll(+1); return true;' value='  >  '>";
	OutHtml += "<input type='button' onclick='"+this.name+".roll(+2); return true;' value='  >> '>"+"</tr>";
	
	if(this.monthsOnly == false) 
	{
		OutHtml += "<tr><td colspan=4>";
		OutHtml += "<TABLE cellpadding=0 cellspacing=1 align='center' border=0> <tr align='center'>";
		//add week days names
		for(i=0; i<nDays; i++) {
			OutHtml = OutHtml +"<th bgcolor='"+weekdays_bg_color+"'>"+"<font color='"+weekdays_font_color+"'>"+wDays[i]+"</font></th>";
		}
		
		//add the month cells
		OutHtml = OutHtml +"</tr><tr align='center'>";
		for(i=0; i<nMonthDays+dayNoNWeek; i++) {
			if(i%7==0 && i!=0)
				OutHtml = OutHtml +"</tr><tr align='center'>"
				if(i<dayNoNWeek)
					OutHtml = OutHtml +"<td>&nbsp;</td>";
				else {
					bcol = cell_bg_color;
					col  = cell_font_color;
					if(i-dayNoNWeek+1 == this.currentDay) { 
						bcol = selected_bg_color;
						col  = selected_font_color;
					}
					OutHtml = OutHtml +"<td bgcolor='"+bcol+"' id='cell"+this.name+i+"' name='cell"+this.name+i+"' onMouseOut='"+this.name+".MouseOutCell(this, cell"+this.name+i+"font);' onMouseOver='"+this.name+".MouseOverCell(cell"+this.name+i+", cell"+this.name+i+"font, TopbarTD);' onclick='"+this.name+".MouseClickCell(cell"+this.name+i+"); return false;'>";
					OutHtml += "<font id='cell"+this.name+i+"font' name='cell"+this.name+i+"font' color='"+col+"'>";
					OutHtml += this.A(i);
					OutHtml += (i-dayNoNWeek+1);
					OutHtml += A1 + "</font></td>";
				}
				
		}
	}//end of if(monthsOnly = false)

	OutHtml = OutHtml+"</tr></TABLE></td></tr></TABLE>";
	if(this.isNavigator)
	{
		//OutHtml += "</FORM>";
	}
	
	this.wwrite(OutHtml);
	this.OutItem.style.visibility = "visible";
	
}

function A(i)
{
	//add the top bar and the buttons
	if(this.isNavigator)
	{
		a  = "<A href='' name='link"+this.name+i+"' onmouseout=\""+this.name+".MouseOutCell('link"+this.name+i+"', "+this.name+".OutItem.link"+i+"font);\" onMouseOver=\""+this.name+".MouseOverCell('link"+this.name+i+"', 'link"+i+"font', 'TopbarTD');\" onclick=\""+this.name+".MouseClickCell('link"+this.name+i+"'); return false;\" >";
	}
	else
	{
		a = "";
	}
	return a;
}

function setOutItem(id1, id2)
{
	if(this.isNS4)
	{
		this.OutItem = document.layers[id1].document.layers[0];
	}
	else
	{
		this.OutItem = window.document.getElementById(id2);
	}
	return this.OutItem;
}



function wwrite(text)
{
	if(this.isNavigator) 
	{
		if(!this.isNS6){
			//Worked will using the div with absoulte position style 
			this.OutItem.document.open();
			this.OutItem.document.write(text);
			this.OutItem.document.close();
			
			window.resizeBy(0,0);			
		}
		else
			this.OutItem.innerHTML = text;
	}
	else 
	{
		this.OutItem.insertAdjacentHTML("beforeEnd", text);
		//this.OutItem.innerHTML = text;
	}
}


function freeCalendar()
{
	if(this.isNS6)
	{
		//this.OutItem.innerHTML = ""; //WORKS WILL
		//this.OutItem.style.visibility = "hidden";
	}
	else
	{
		this.OutItem.innerHTML = "";
		this.OutItem.style.visibility = "hidden";
	}

}

function initializeDate(d, m, y, inclDay, obj,ocx)
{
	v = this.GregTimeToSystemTime(d, m-1, y, ocx);
	//Initialize the text box
	obj.value = this.formatDate(parseInt(v[0]), parseInt(v[1]), parseInt(v[2]), v[3], inclDay, ocx);
}

function srvInitializeDate(d, m, y, inclDay, ocx)
{
	v = this.GregTimeToSystemTime(d, m-1, y, ocx);
	return this.formatDate(parseInt(v[0]), parseInt(v[1]), parseInt(v[2]), v[3], inclDay, ocx);
}



/*
viewCalendar()
- 
- 
- 
- 
*/
function viewCalendar()
{
	today	   = new Date();
	var values = new Array(6);

	if(arguments[0]!=null && arguments[1]!=null && arguments[2]!=null) {
		today.setFullYear(arguments[2]);
		today.setMonth(arguments[1]-1);
		today.setDate(arguments[0]);
	}

	values = this.GregTimeToSystemTime(today.getDate(), today.getMonth(), today.getFullYear());
	this.currentDay   = parseInt(values[0]);
	this.currentMonth = parseInt(values[1]);
	this.currentYear  = parseInt(values[2]);
	this.currentEra	 = values[5];
	dnw			 = parseInt(values[3]);

	if(this.currentEra!="" && this.currentEra!=null)
		this.setCriticalDates(this.getEraNo(this.currentEra));

	this.drawCalendar(this.currentDay, this.currentMonth, this.currentYear, this.currentEra, dnw);
}


/*
initailize()
- 
- 
- 
- 
*/
function initailize()
{
	vals  = this.RetreiveEras();
	count = parseInt(vals[0]);
	if(! isNaN(count))
	{
		this.ArrayEra     = new Array(count);
		this.ArrayEraStart= new Array(count);
		this.ArrayEraEnd	 = new Array(count);
		
		this.ArrayEraLocaleEndYear  = new Array(count);
		this.ArrayEraLocaleEndMonth = new Array(count);
		this.ArrayEraLocaleEndDay   = new Array(count);
		
		for(k=0, j=1; k<count; k++, j+=7)
		{
			this.ArrayEra[k] = vals[j];
			this.ArrayEraStart[k] = new Date(parseInt(vals[j+3]), parseInt(vals[j+2])-1, parseInt(vals[j+1]));
			this.ArrayEraEnd[k]   = new Date(parseInt(vals[j+6]), parseInt(vals[j+5])-1, parseInt(vals[j+4]));
			
			ret = this.GregTimeToSystemTime(parseInt(vals[j+4]), parseInt(vals[j+5])-1, parseInt(vals[j+6]));
			this.ArrayEraLocaleEndDay[k]   = ret[0];
			this.ArrayEraLocaleEndMonth[k] = ret[1];
			this.ArrayEraLocaleEndYear[k]  = ret[2];
		}
	}
}

/*
show_calendar()
- This function is used to view the calendar
- Notes:
-	To view the calendar you have to create a <DIV> object and initialize the CalendarOCX.ocx using <Object>
- 
- Parameters
-	OutItem: the div on which the calendar will be drawn
-	LocaleDateReturn: input of type text or any type to view the selected date of the calendar
-	GregDay_Return, GregMonth_Return, GregYear_Return: Handle to three controls (may be hidden) to take the Gregorian Date,month,year.
-	CalOCX: Handle to the CalendarOCX.cx <Object>.
-	type: Type of calendar to be displayed (YearMonthDays to display the whole calendar, MonthsOnly to display year-month part only)
-	arguments[6],arguments[7],arguments[8]: Gregorian Day/Month/Year to initialize the calendar.
- 
*/
function show_calendar()
{


	this.LocaleDateReturn= arguments[1];
	this.GregDay_Return  = arguments[2];
	//this.GregMonth_Return= arguments[3];
	//this.GregYear_Return = arguments[4];
	this.CalOCX = null;
	this.type	= arguments[4];
	this.formIndex 	    = arguments[5];

	
	this.isNavigator = (navigator.appName == 'Netscape');
	this.isNS4 = (document.layers) ? true : false;
	this.isIE4 = (document.all && !document.getElementById) ? true : false;
	this.isIE5 = (document.all && document.getElementById) ? true : false;
	this.isNS6 = (!document.all && document.getElementById) ? true : false;

	

	this.CalendarDivId		= arguments[0];

	OutHtm = "";
	if(this.type.toString() == "YearMonthDays")
		this.monthsOnly = false;
	else if(this.type.toString() == "MonthsOnly")
		this.monthsOnly = true;
	else
		this.monthsOnly = false;

	initializeOCX();

	this.initailize();

	if(!this.CalendarOpened)
	{
		if (!(this.isNavigator && !this.isNS6))
		{
			OutHtm  = "<DIV id=CalDiv"+this.CalendarDivId+"></div>";
			document.write(OutHtm);
			this.OutItem = this.setOutItem("", "CalDiv"+this.CalendarDivId );
		}

	}
	
	if (!(this.isNavigator && !this.isNS6))
	{
		if(isNS6)
			this.CalendarOpened= true;

		if(this.CalendarOpened)
		{ 
			if(this.initGregDay!=null && this.initGregMonth!=null && this.initGregYear!=null)
			{
				this.viewCalendar(this.initGregDay, this.initGregMonth , this.initGregYear);
			}
			else
			{
				this.viewCalendar(arguments[8], arguments[9], arguments[10]);
			}

		}
	}
	else
	{
		this.LocaleDateReturn.blur();
		this.openNewWin(arguments);
		return;
	}
	this.CalendarOpened = true;	
	
}

function getFormIndexOfControl(ControlName)
{
	frmCnt = window.document.forms.length;
	for(i=0; frmCnt>=0, i<=frmCnt; i++)
	{
		if(window.document.forms[i].elements[ControlName])
			{ return i; }
	}
	return -1;
}var timerID=null;
var count=0;
function reload()
{
	reloadPlugins();
	history.go(-2);		
	/*if(!pluginExist("emms/x-calendar"))
	{
		
		alert("Sorry,Plugin not exist");
		history.go(-2);		
	}

	else
	{
		alert("Installation Done Successfully.");
		history.go(-1);		
	}*/
}
function checkPlugin()
{
	reloadPlugins();
	if(!pluginExist("emms/x-calendar"))
	{
	   timerID=setTimeout("checkPlugin()",1000);
	   count++;
	   if(count>100)
	   {
		clearTimeout(timerID);
		alert("Installation Failed.");
		history.go(0);
	   }	
	}
	else
	{
		clearTimeout(timerID);
		alert("Installation Done Successfully.");
		history.go(0);
	}	
}
function download()
{
   var isNS4 = (document.layers) ? true : false;
   var isNS6 = (!document.all && document.getElementById) ? true : false;
   if(isNS4) 
   {
	//var ret=confirm("EMMS Calendar plugin is not installed\nPress OK to Download it now\nPress Cancel to cancel download");
	//if(ret)
	{
	  winx=open("/tar/NPCAL32.tar","main");
	  //timerID=setTimeout("checkPlugin()",1000);	
     	  count=0;
	}
   }
   else
   {
	//var ret=confirm("EMMS Calendar plugin is not installed\nPress OK to Download it now\nPress Cancel to cancel download");
	//if(ret)
	{
	  winx=open("/tar/npxcal32.tar","main");
          //setTimeout("winx.close();",1000);
	 // timerID=setTimeout("checkPlugin()",1000);	
     	  count=0;
     	

	}
	return false;
   }
}
function pluginExist(plg)
{
   plugins=navigator.plugins;
   for(i=0;i<plugins.length;i++)
   {
	if(plugins[i][0].type==plg)
		return true;
   }
   return false;
}
function reloadPlugins()
{
   navigator.plugins.refresh();
}
