
var cDate = new Date();
var cblCal;
Cal.isGlobalYear =false ;
Cal.isGlobalMonth = true;
Cal.irregularMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
Cal.Months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
Cal.regularMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

var weekend = [0,6];
var ffont = "Verdana";
var fontsize = 2;

var currentDay = cDate.getDate();
var currentMonth = cDate.getMonth();
var currentYear = cDate.getFullYear();
var initialDate=currentMonth+"/"+currentDay+"/"+currentYear;

function Cal(dField, winName, dMonthName, dYearName, beginMonthName, beginDayName, beginYearName) {
	 ;
	if ((dMonthName == null) && (dYearName == null))	return;

	if (winName == null)
		this.gWinCal = cblCal;
	else
		this.gWinCal = winName;
	
	if (dMonthName == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else {
		this.gMonthName = Cal.get_month(dMonthName);
		this.gMonth = new Number(dMonthName);
		this.gYearly = false;
	}

	this.gYear = dYearName;
	this.gStartMonth = beginMonthName;
	this.gStartDay = beginDayName;
	this.gStartYear = beginYearName;
	this.gBGColor = "#e1e1e1";
	this.gFGColor = "black";
	this.gTextColor = "black";
	this.gHeaderColor = "black";
	this.gReturnItem = dField;
}

Cal.get_month = Cal_get_month;
Cal.get_daysofmonth = Cal_get_daysofmonth;
Cal.calc_month_year = Cal_calc_month_year;
Cal.print = Cal_print;

function Cal_get_month(monthNo) {
	return Cal.Months[monthNo];
}

function Cal_get_daysofmonth(monthNo, dYearName) {
	if ((dYearName % 4) == 0) {
		if ((dYearName % 100) == 0 && (dYearName % 400) != 0)
			return Cal.regularMonth[monthNo];
	
		return Cal.irregularMonth[monthNo];
	} else
		return Cal.regularMonth[monthNo];
}

function Cal_calc_month_year(dMonthName, dYearName, incr) {
	var ret_arr = new Array();
	
	if (incr == -2) {
		if (dMonthName == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(dYearName) - 1;
		}
		else {
			ret_arr[0] = parseInt(dMonthName) - 1;
			ret_arr[1] = parseInt(dYearName);
		}
	} else if (incr == 1) {
		if (dMonthName == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(dYearName) + 1;
		}
		else {
			ret_arr[0] = parseInt(dMonthName) + 1;
			ret_arr[1] = parseInt(dYearName);
		}
	}
	
	return ret_arr;
}

function Cal_print() {
	cblCal.print();
}

function Cal_calc_month_year(dMonthName, dYearName, incr) {
	var ret_arr = new Array();
	
	if (incr == -2) {
		if (dMonthName == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(dYearName) - 1;
		}
		else {
			ret_arr[0] = parseInt(dMonthName) - 1;
			ret_arr[1] = parseInt(dYearName);
		}
	} else if (incr == 1) {
		if (dMonthName == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(dYearName) + 1;
		}
		else {
			ret_arr[0] = parseInt(dMonthName) + 1;
			ret_arr[1] = parseInt(dYearName);
		}
	}
	
	return ret_arr;
}


new Cal();

Cal.prototype.prototypeMonthCode = function() {
	var displayText = "";
	var vHeader_Code = "";
	var vData_Code = "";
	
	displayText = displayText + "<TABLE BORDER=1 BGCOLOR=\"" + this.gBGColor + "\">";
	
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	displayText = displayText + vHeader_Code + vData_Code;
	
	displayText = displayText + "</TABLE>";
	
	return displayText;
}
        Cal.prototype.show = function() {
	var displayText = "";
	this.gWinCal.document.open();
	this.textWrite("<html>");
	this.textWrite("<head><title>Calendar</title>");
	this.textWrite("</head>");
	this.textWrite("<body " + "link=\"" + this.gLinkColor + "\" " + "vlink=\"" + this.gLinkColor + "\" " +"alink=\"" + this.gLinkColor + "\" " +"text=\"" + this.gTextColor + "\">");
	var lastMoYEAR = Cal.calc_month_year(this.gMonth, this.gYear, -2);
	var prevMM = lastMoYEAR[0];
	var prevYYYY = lastMoYEAR[1];

	var nextMMYYYY = Cal.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
	
	this.textWrite("<TABLE WIDTH='225' BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR='#e0e0e0'><TR><TD ALIGN=center>");
		this.textWrite("<A HREF=\"" +
			"javascript:window.opener.fnCreateNew(" + 
			"'" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gStartMonth + "', '" + this.gStartDay + "', '" + this.gStartYear + "'" +
			");" +
			"\"><IMG SRC=\"/crmspeed/images/back.gif\" border=\"0\"></A></TD><TD ALIGN=center>");

	this.textWriteNext("<FONT FACE='" + ffont + "' SIZE=-2><B>");
	this.textWriteNext(this.gMonthName + " " + this.gYear);
	this.textWriteNext("</B>");

	if (this.gMonth >= currentMonth && this.gYear > currentYear){
	//alert("MONTH:::"+this.gMonth);
	//alert(" YEAR " +this.gYear);
		//var nextMMYYYY = Cal.calc_month_year(this.gMonth, this.gYear, 1);
		//var nextMM = nextMMYYYY[0];
		//var nextYYYY = nextMMYYYY[1];
		//this.textWrite("</TD><TD ALIGN=center>&nbsp;</TR></TABLE>");
			this.textWrite("</TD><TD ALIGN=center><A HREF=\"" +
			"javascript:window.opener.fnCreateNew(" + 
			"'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gStartMonth + "', '" + this.gStartDay + "', '" + this.gStartYear + "'" +
			");" +
			"\"><IMG SRC=\"/crmspeed/images/forward.gif\" border=\"0\"></A></TD></TR></TABLE>");		
	}else{
		this.textWrite("</TD><TD ALIGN=center><A HREF=\"" +
			"javascript:window.opener.fnCreateNew(" + 
			"'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gStartMonth + "', '" + this.gStartDay + "', '" + this.gStartYear + "'" +
			");" +
			"\"><IMG SRC=\"/crmspeed/images/forward.gif\" border=\"0\"></A></TD></TR></TABLE>");
        }
	displayText = this.prototypeMonthCode();
	this.textWrite(displayText);

	this.textWrite("</font></body></html>");
	this.gWinCal.document.close();
}


Cal.prototype.textWrite = function(wtext) {
	this.gWinCal.document.writeln(wtext);
}

Cal.prototype.textWriteNext = function(wtext) {
	this.gWinCal.document.write(wtext);
}

Cal.prototype.cal_header = function() {
	var displayText = "";
	
	displayText = displayText + "<TR bgcolor'#2C84B2'>";
	displayText = displayText + "<TD WIDTH='14%' bgcolor='#9FB3CD'><FONT SIZE='-2' FACE='" + ffont + "' COLOR='" + this.gHeaderColor + "'><B>Sun</B></FONT></TD>";
	displayText = displayText + "<TD WIDTH='14%' bgcolor='#9FB3CD'><FONT SIZE='-2' FACE='" + ffont + "' COLOR='" + this.gHeaderColor + "'><B>Mon</B></FONT></TD>";
	displayText = displayText + "<TD WIDTH='14%' bgcolor='#9FB3CD'><FONT SIZE='-2' FACE='" + ffont + "' COLOR='" + this.gHeaderColor + "'><B>Tue</B></FONT></TD>";
	displayText = displayText + "<TD WIDTH='14%' bgcolor='#9FB3CD'><FONT SIZE='-2' FACE='" + ffont + "' COLOR='" + this.gHeaderColor + "'><B>Wed</B></FONT></TD>";
	displayText = displayText + "<TD WIDTH='14%' bgcolor='#9FB3CD'><FONT SIZE='-2' FACE='" + ffont + "' COLOR='" + this.gHeaderColor + "'><B>Thu</B></FONT></TD>";
	displayText = displayText + "<TD WIDTH='14%' bgcolor='#9FB3CD'><FONT SIZE='-2' FACE='" + ffont + "' COLOR='" + this.gHeaderColor + "'><B>Fri</B></FONT></TD>";
	displayText = displayText + "<TD WIDTH='16%' bgcolor='#9FB3CD'><FONT SIZE='-2' FACE='" + ffont + "' COLOR='" + this.gHeaderColor + "'><B>Sat</B></FONT></TD>";
	displayText = displayText + "</TR>";
	
	return displayText;
}

Cal.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay=vDate.getDay();
	var vDay=1;
	var vLastDay=Cal.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var displayText = "";
	displayText = displayText + "<TR>";
	for (i=0; i<vFirstDay; i++) {
		displayText = displayText + "<TD WIDTH='14%' ALIGN=CENTER bgcolor='#eeeeee'><FONT SIZE='-2' FACE='" + ffont + "'> </FONT>&nbsp;</TD>";
	}

	for (j=vFirstDay; j<7; j++) {
		
		if (this.format_href(vDay)){
		        displayText = displayText + "<TD WIDTH='14%' ALIGN=CENTER bgcolor='#eeeecc'><FONT SIZE='-2' FACE='" + ffont;
			displayText = displayText + "'>" +
				"<A HREF='#' " + 
				"onClick=\"self.opener.document." + this.gReturnItem + ".value='" + 
				this.format_data(vDay) + 
				"';window.close();\">" + 
				this.format_day(vDay) + 
				"</A>" + 
				"</FONT></TD>";
		}else{
		        displayText = displayText + "<TD WIDTH='14%' ALIGN=CENTER bgcolor='#eeeeee'><FONT SIZE='-2' FACE='" + ffont;
			displayText = displayText + "'><font COLOR='gray'>" +
					//"<A HREF='#' " + 
						//"onClick=\"self.opener.document." + this.gReturnItem + ".value='" + 
						//this.format_data(vDay) + 
						//"';window.close();\">" + 
						this.format_day(vDay) + 
						//"</A>" + 
						"</font></FONT></TD>";

		}
		vDay=vDay + 1;
	}
	displayText = displayText + "</TR>";

	for (k=2; k<7; k++) {
		displayText = displayText + "<TR>";

		for (j=0; j<7; j++) {
			
			if (this.format_href(vDay)){
			  displayText = displayText + "<TD WIDTH='14%' ALIGN=CENTER bgcolor='#eeeecc'><FONT SIZE='-2' FACE='" + ffont;			
			  displayText = displayText + "'>" +
					"<A HREF='#' " + 
						"onClick=\"self.opener.document." + this.gReturnItem + ".value='" + 
						this.format_data(vDay) + 
						"';window.close();\">" + 
						this.format_day(vDay) + 
						"</A>" + 
						"</FONT></TD>";
			}else{
			  displayText = displayText + "<TD WIDTH='14%' ALIGN=CENTER bgcolor='#eeeeee'><FONT SIZE='-2' FACE='" + ffont;			
			  displayText = displayText + "'><font COLOR='gray'>" +
					//"<A HREF='#' " + 
						//"onClick=\"self.opener.document." + this.gReturnItem + ".value='" + 
						//this.format_data(vDay) + 
						//"';window.close();\">" + 
						this.format_day(vDay) + 
						//"</A>" + 
						"</font></FONT></TD>";
                          }
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			displayText = displayText + "</TR>";
		if (vOnLastDay == 1)
			break;
	}
	
	for (m=1; m<(7-j); m++) {
		if (this.gYearly)
			displayText = displayText + "<TD WIDTH='14%' ALIGN=CENTER bgcolor='#eeeeee'><FONT SIZE='-2' FACE='" + ffont + "' COLOR='gray'> </FONT></TD>";
		else
			displayText = displayText + "<TD WIDTH='14%' ALIGN=CENTER bgcolor='#eeeeee'><FONT SIZE='-2' FACE='" + ffont + "' COLOR='gray'>" + m + "</FONT></TD>";
	}
	
	return displayText;
}

Cal.prototype.format_href = function(vday) {
var cDateObj = new Date();
diff  = new Date();
var currentDay = cDateObj.getDate();
var currentMonth = cDateObj.getMonth();
var currentYear = cDateObj.getFullYear();
//alert(this.gMonth+ " - " + vday + " - " + this.gYear);
//alert(currentMonth+ " - " + currentDay + " - " + currentYear);
var testDate = new Date(this.gMonth+1 +"/"+vday+"/"+this.gYear);

var date1temp =cDateObj.getTime(); 
var date2temp =testDate.getTime();

if(date2temp>date1temp){

}else{
	//alert("Test Date=="+testDate + " todays is :::"+cDateObj);
	
	if(vday==currentDay && this.gMonth == currentMonth && this.gYear>=currentYear){
	 //do nothing
	}else{
	    return 0;
	  }
}



        if(vday < currentDay && this.gMonth <= currentMonth && this.gYear <= currentYear)
        {
          //alert("inn");
          return 1
        
        }

	if (vday < this.gStartDay  && this.gMonth == this.gStartMonth  && this.gYear == this.gStartYear )
		return 0;
	else
		//if (this.gMonth > this.gStartMonth  && this.gYear > this.gStartYear )
			//return 0;
		//else
			//if (vday >= this.gStartDay && this.gMonth == this.gStartMonth && this.gYear > this.gStartYear)
				//return 0;
			//else
				return 1;

}

Cal.prototype.format_day = function(vday) {

	if (vday == this.gStartDay && this.gMonth == this.gStartMonth && this.gYear == this.gStartYear)
		return ("<FONT COLOR=\"RED\"><B>" + vday + "</B></FONT>");
	else
		return (vday);
}

//ADDED on 8/1/2004
Cal.prototype.past_days = function(vday) {

	if (vday < this.gStartDay && this.gMonth < this.gStartMonth && this.gYear <= this.gStartYear)
		return "true";
	else
		return "false";
}
//end of addition

Cal.prototype.write_weekend_string = function(vday) {
	var i;
	for (i=0; i<weekend.length; i++) {
		if (vday == weekend[i])
			return (" BGCOLOR=\"#000000\"");
	}
	
	return "";
}

Cal.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vY4 = new String(this.gYear);
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	vData = vMonth + "\/" + vDD + "\/" + vY4;
        //alert(vData);
	return vData;
}

function fnCreateNew(dField, dMonthName, dYearName,  beginMonthName, beginDayName, beginYearName) {

	var winName = cblCal;
	gCal = new Cal(dField, winName, dMonthName, dYearName,  beginMonthName, beginDayName, beginYearName);
	gCal.gBGColor="#ffffff";
	gCal.gLinkColor="#003366";
	gCal.gTextColor="black";
	gCal.gHeaderColor="#ffffff";

	gCal.show();
}

function show_CalRelative(dField,dDateStart) {
	var selectDate = new Date(dDateStart);
		dMonthName = new String(selectDate.getMonth());
		dYearName = new String(selectDate.getFullYear().toString());
		beginMonthName = selectDate.getMonth();
		beginDayName  = new String(selectDate.getDate());
		beginYearName  = dYearName;	
		beginMonthName = dMonthName;
		beginYearName  = dYearName;			
	windowName = window.open("", "Calendar", "width=245,innerwidth=245,innerheight=175,height=175,status=no,resizable=no,left=230,screenx=230,top=230,screeny=230");
	windowName.opener = self;
	cblCal = windowName;
	fnCreateNew(dField, dMonthName, dYearName,  beginMonthName, beginDayName, beginYearName);
}


function show_Cal(dField) {
		dMonthName = new String(cDate.getMonth());
		dYearName = new String(cDate.getFullYear().toString());
		beginMonthName = cDate.getMonth();
		beginDayName  = new String(cDate.getDate());
		beginYearName  = dYearName;
	windowName = window.open("", "Calendar", "width=245,innerwidth=245,innerheight=175,height=175,status=no,resizable=no,left=230,screenx=230,top=230,screeny=230");
	windowName.opener = self;
	cblCal = windowName;	
	fnCreateNew(dField, dMonthName, dYearName,  beginMonthName, beginDayName, beginYearName);
}

//-->
