function Ginger_DateObj()
{
	this.startDateObj = new Date();
	this.currentDate = this.dateToString(this.startDateObj);
	this.dateInput;
	this.keepDateValuesArr = new Array();
	this.tempDateObj = new Date();
	this.monthFactor = 0;
	this.saveDocumentEvent = document.onmouseup;
	this.tempDateObj.setDate(1);
	this.currentDay = this.tempDateObj.getDay();
	this.currentMonth = this.tempDateObj.getMonth();
	this.tempDateObj.setDate(this.tempDateObj.getDate()-this.currentDay);
	this.selectedDateObj = this.startDateObj;
	this.endDateObj
	this.setEndDate = function (dateValue) {
		this.endDateObj = new Date(parseInt(dateValue));
	}
	
	this.restoreDocumentEvent = function () {
		var oParent;
		try {
			//if (parent.length)
				//if (parent.length > 1)
					oParent = parent[parent.length-1];
				//else
				//	oParent = parent;
			//else
				//oParent = parent;
			if (Ginger_DateObj.prototype.isDateObjOn && (oParent.event.srcElement.id.toString().indexOf("daysSpan") == -1))
			{
				try {
					parent.document.onmouseup = Ginger_DateObj.prototype.currentObj.saveDocumentEvent;
				} catch (e) {
				}
				Ginger_DateObj.prototype.currentObj.hideDateObj();
			}
		} catch(e) {
			//if (parent.length)
				//if (parent.length > 1)
					//oParent = parent[parent.length-1];
				//else
				//	oParent = parent;
			//else
				oParent = parent;
			if (Ginger_DateObj.prototype.isDateObjOn && (oParent.event.srcElement.id.toString().indexOf("daysSpan") == -1))
			{
				parent.document.onmouseup = Ginger_DateObj.prototype.currentObj.saveDocumentEvent;
				Ginger_DateObj.prototype.currentObj.hideDateObj();
			}
		}
	}
	
	this.hideDateObj = function () {
		this.monthFactor = 0;
		if (Ginger_DateObj.prototype.isDateObjOn)
		{
			document.all.dateSpan.style.visibility='hidden';
			document.all.dateSpan.style.pixelTop = -200;
			document.all.dateSpan.style.pixelLeft = -200;
		}
		else
		{
			document.all.dateSpan.style.visibility='visible';
			var nLeftPosition = this.dateInput.offsetLeft + this.dateInput.offsetWidth - document.all.dateSpan.offsetWidth + 15;
			//alert(nLeftPosition)
			//if (nLeftPosition-document.all.dateSpan.offsetWidth < 0)
				//nLeftPosition = 0;
			this.getInputDate();
			document.all.dateSpan.style.pixelLeft = nLeftPosition;
			document.all.dateSpan.style.pixelTop = (this.dateInput.offsetTop+this.dateInput.offsetHeight);
			document.onmouseup = Ginger_DateObj.prototype.currentObj.restoreDocumentEvent;
		}
		Ginger_DateObj.prototype.isDateObjOn = !Ginger_DateObj.prototype.isDateObjOn;
	} 
	
	this.onSelection = function () {
		this.tempSelectedValue = ""+window.CMS_DateFrame.event.srcElement.id
		this.selectedDateValue = parseInt(this.keepDateValuesArr[this.tempSelectedValue]);
		this.selectedDateObj = new Date(this.selectedDateValue);
		this.dateInput.value = this.dateToPrint(this.selectedDateObj);
		this.redrawCalender();
		this.hideDateObj(); 
	}
	
	this.getInputDate = function() {
		if (this.isValidDate(this.dateInput.value))
		{
			var DateStrArr = this.dateInput.value.split("/");
			var tempDateStr = DateStrArr[1]+"/"+DateStrArr[0]+"/"+DateStrArr[2];
			this.selectedDateObj = new Date(tempDateStr);
		}
		else
			this.selectedDateObj = new Date();
		this.redrawCalender();
	}
}

Ginger_DateObj.prototype.redrawCalender = function () {
	this.tempDateObj = new Date(this.selectedDateObj.valueOf());
	this.tempDateObj.setDate(1);
	this.tempDateObj.setMonth(this.tempDateObj.getMonth()+this.monthFactor);
	this.currentDay = this.tempDateObj.getDay();
	this.currentMonth = this.tempDateObj.getMonth();
	window.CMS_DateFrame.document.all.daysSpanheadline.innerText = this.monthNamesArr[this.currentMonth]+" "+this.tempDateObj.getFullYear();
	this.tempDateObj.setDate(this.tempDateObj.getDate()-this.currentDay)
	
	for (weekCount=0; weekCount<6; weekCount++)
	{ 
		for (dayCount=0; dayCount<7; dayCount++)
		{	
			eval("window.CMS_DateFrame.document.all.daysSpan"+weekCount+"_"+dayCount+".innerText = this.tempDateObj.getDate()");
			if (this.currentDate == this.dateToString(this.tempDateObj))
			{
				if (this.currentDate == this.dateToString(this.selectedDateObj))
					eval("window.CMS_DateFrame.document.all.daysSpan"+weekCount+"_"+dayCount+".className='todaySelected'");
				else
					eval("window.CMS_DateFrame.document.all.daysSpan"+weekCount+"_"+dayCount+".className='today'");
			}
			else if (this.dateToString(this.selectedDateObj) == this.dateToString(this.tempDateObj))
				eval("window.CMS_DateFrame.document.all.daysSpan"+weekCount+"_"+dayCount+".className='selected'");
			else if (this.currentMonth == this.tempDateObj.getMonth())
				eval("window.CMS_DateFrame.document.all.daysSpan"+weekCount+"_"+dayCount+".className='inMonth'");
			else
				eval("window.CMS_DateFrame.document.all.daysSpan"+weekCount+"_"+dayCount+".className='outMonth'");
				
			this.keepDateValuesArr["daysSpan"+weekCount+"_"+dayCount] = this.tempDateObj.valueOf();
			this.tempDateObj.setDate(this.tempDateObj.getDate()+1)
		}
	}
}

Ginger_DateObj.prototype.monthNamesArr = new Array(
	"January",
	"Febuary",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"Septemper",
	"October",
	"November",
	"December"
)

Ginger_DateObj.prototype.dateToString = function (dateObj) {
	var dateToString = dateObj.getDate()+"/"+dateObj.getMonth()+"/"+dateObj.getFullYear();
	return dateToString;
}

Ginger_DateObj.prototype.dateToPrint = function (dateObj) {
	var dateToString = dateObj.getDate()+"/"+(dateObj.getMonth()+1)+"/"+dateObj.getFullYear();
	return dateToString;
}

Ginger_DateObj.prototype.isValidDate = function (DateStr){
	var DateStrArr = DateStr.split("/");
	for (i=0; i<DateStrArr.length-1; i++)
	{
		for (;DateStrArr[i].charAt(0) == " " || DateStrArr[i].charAt(0) == "0";)
		{
			DateStrArr[i] = DateStrArr[i].substring(1,DateStrArr[i].length)
		}
	}
	var tempDateStr = DateStrArr[1]+"/"+DateStrArr[0]+"/"+DateStrArr[2];
	var dateToCheckObj = new Date(tempDateStr);
	return (this.dateToPrint(dateToCheckObj)==DateStr && this.dateToPrint(dateToCheckObj).indexOf("NaN") == -1 );
}

Ginger_DateObj.prototype.isValidRange = function (startDateStr, endDateStr){
	var startDateObj = new Date (""+startDateStr);
	var endDateObj = new Date (""+endDateStr);
	return (startDateObj.valueOf() <= endDateObj.valueOf());
}

Ginger_DateObj.prototype.getValueFromStr = function (DateStr){
	var DateStrArr = DateStr.split("/");
	for (i=0; i<DateStrArr.length-1; i++)
	{
		for (;DateStrArr[i].charAt(0) == " " || DateStrArr[i].charAt(0) == "0";)
		{
			DateStrArr[i] = DateStrArr[i].substring(1,DateStrArr[i].length)
		}
	}
	DateStr = DateStrArr.join("/");
	var dateToCheckObj = new Date(DateStr);
	return (dateToCheckObj.valueOf());
}

Ginger_DateObj.prototype.daysNamesArr = new Array(
	"Sun",
	"Mon",
	"The",
	"Wed",
	"Thu",
	"Fri",
	"Sat"
);

Ginger_DateObj.prototype.isDateObjOn = false;

function drawCalenderScript() {
	var CalenderStr = "";
	for (weekCount=0; weekCount<6; weekCount++) 
	{
		CalenderStr += '<tr align=center height=16>';
		for (dayCount=0; dayCount<7; dayCount++) 
		{
			CalenderStr +='<td align=center class=';
			if (Ginger_DateObj.prototype.currentDate == myGinger_DateObj0.dateToString(myGinger_DateObj0.tempDateObj)) 
			{
				CalenderStr += 'todaySelected ';
				todayID = 'd'+weekCount+'_'+dayCount; 
			}
			else if (myGinger_DateObj0.currentMonth == myGinger_DateObj0.tempDateObj.getMonth())
				CalenderStr += 'inMonth ';
			else
				CalenderStr += 'outMonth ';
			CalenderStr += 'id=daysSpan'+weekCount+'_'+dayCount+'>'+myGinger_DateObj0.tempDateObj.getDate()+'</td>';
			myGinger_DateObj0.keepDateValuesArr['daysSpan'+weekCount+'_'+dayCount] = myGinger_DateObj0.tempDateObj.valueOf();
			myGinger_DateObj0.tempDateObj.setDate(myGinger_DateObj0.tempDateObj.getDate()+1) 
		}
		CalenderStr += '</tr>'; 
	}
	return CalenderStr;
}

var myGinger_DateObj0 = new Ginger_DateObj();
Ginger_DateObj.prototype.currentObj = myGinger_DateObj0;
