/*-----------------------------------------------------------

Project Name		:	FourChain
File Name			:	calendar.js
Description			:	Contains the calendar related code (JavaScript Implementation)
Author				:	http://www.softcomplex.com/products/tigra_calendar/ (Tigra Calendar)
Creation Date		:	September 1, 2005
Included in Files	:	CustPage.asp

Modified By			:	Yasir Amin
Modified on			:	October 11, 2005
Modi. Comments		:	Customized the "Tigra Calendar" to support functionality related to the one required in FourChain
						
Modified By			:	Yasir Amin
Modified on			:	October 14, 2005
Modi. Comments		:	Updating the calendar so that is becomes Mozilla Compatible

-----------------------------------------------------------*/

// if two digit year input dates after this year considered 20 century.
var NUM_CENTYEAR = 30;
// is time input control required by default
var BUL_TIMECOMPONENT = false;
// are year scrolling buttons required by default
var BUL_YEARSCROLL = true;

var calendars = [];
var RE_NUM = /^\-?\d+$/;

function Calendar(obj_target)
{
	// assigning methods
	this.gen_date					= fnGenerateDate;
	this.gen_time					= fnGenerateTime;
	this.gen_tsmp					= fnGenerateTimeStamp;
	this.prs_date					= fnParseDate;
	this.prs_time					= fnParseTime;
	this.prs_tsmp					= fnParseTimeStamp;
	this.Popup						= fnPopup;
	this.GenerateCurrentDateTime	= fnGenerateCurrentDateTime;

	// validate input parameters
	if (!obj_target)
	{
		// Display a message
		alert("Error calling the calendar: no target control specified");
		
		// return null
		return null;
	}
	
	if (obj_target.value == null)
	{
		// Display a message
		alert("Error calling the calendar: parameter specified is not valid target control");
		
		// return null
		return null;
	}
	
	this.target = obj_target;
	this.time_comp = BUL_TIMECOMPONENT;
	this.year_scroll = BUL_YEARSCROLL;
	
	// register in global collections
	this.id = calendars.length;
	calendars[this.id] = this;
}

// This will popup the calendar window to the user
function fnPopup (str_datetime)
{
	if (str_datetime)
	{
		this.dt_current = this.prs_tsmp(str_datetime);
	}
	else
	{
		this.dt_current = this.prs_tsmp(this.target.value);
		this.dt_selected = this.dt_current;
	}
	
	if (!this.dt_current)
	{
		return;
	}
	
	/// This will contain the window height currently set to 0
	var intWindowHeight=265;
	
	// NOTE: Make sure that that uyou have included the Validation.js file in your HTML main file because
	// fnGetBrowser() function is defined in that file
	var strBrowserName=fnGetBrowser();
	
	// Both MS Internet Explorer and Mozilla(Netscape) have different height so we will check the browser and then set the
	// height
	
	// Check if IE then
	if (strBrowserName == "MSIE")
	{
		// If the time component is enabled
		if (this.time_comp == true)
		{
			intWindowHeight=215;
		}
		else
		{
			intWindowHeight=185;
		}
	}
	// If the browser is Netscape or Mozilla
	else if (strBrowserName == "NETSCAPE")
	{
		// If the time component is enabled
		if (this.time_comp == true)
		{
			intWindowHeight=200;
		}
		else
		{
			intWindowHeight=190;
		}
	}

	var obj_calwindow = window.open(
		'calendar.htm?datetime=' + this.dt_current.valueOf()+ '&id=' + this.id,
		'Calendar', 'width=200,height='+ intWindowHeight +
		',status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes'
	);
	
	obj_calwindow.opener = window;
	obj_calwindow.focus();
}

// Timestamp generating function
function fnGenerateTimeStamp (dt_datetime)
{
	return(this.gen_date(dt_datetime) + ' ' + this.gen_time(dt_datetime));
}

// date generating function
function fnGenerateDate (dt_datetime)
{
	return (
		(dt_datetime.getMonth() < 9 ? '0' : '') + (dt_datetime.getMonth() + 1) + "/"
		+ (dt_datetime.getDate() < 10 ? '0' : '') + dt_datetime.getDate() + "/"
		+ dt_datetime.getFullYear()
	);
}
// time generating function
function fnGenerateTime (dt_datetime)
{
	return (
		(dt_datetime.getHours() < 10 ? '0' : '') + dt_datetime.getHours() + ":"
		+ (dt_datetime.getMinutes() < 10 ? '0' : '') + (dt_datetime.getMinutes()) + ":"
		+ (dt_datetime.getSeconds() < 10 ? '0' : '') + (dt_datetime.getSeconds())
	);
}

// timestamp parsing function
function fnParseTimeStamp (str_datetime)
{
	// if no parameter specified return current timestamp
	if (!str_datetime)
	{
		return (new Date());
	}

	// if positive integer treat as milliseconds from epoch
	if (RE_NUM.exec(str_datetime))
	{
		return new Date(str_datetime);
	}
		
	// else treat as date in string format
	var arr_datetime = str_datetime.split(' ');
	return this.prs_time(arr_datetime[1], this.prs_date(arr_datetime[0]));
}

// date parsing function
function fnParseDate (str_date)
{
	var arr_date = str_date.split('/');

	if (arr_date.length != 3)
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	if (!arr_date[1])
	{	
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	if (!RE_NUM.exec(arr_date[1]))
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	if (!arr_date[0])
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	if (!RE_NUM.exec(arr_date[0]))
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	if (!arr_date[2])
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	if (!RE_NUM.exec(arr_date[2]))
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}

	var dt_date = new Date();
	dt_date.setDate(1);

	if (arr_date[0] < 1 || arr_date[0] > 12)
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	dt_date.setMonth(arr_date[0]-1);
	 
	if (arr_date[2] < 100)
	{
		arr_date[2] = Number(arr_date[2]) + (arr_date[2] < NUM_CENTYEAR ? 2000 : 1900);
	}
	
	dt_date.setFullYear(arr_date[2]);

	var dt_numdays = new Date(arr_date[2], arr_date[0], 0);
	dt_date.setDate(arr_date[1]);
	if (dt_date.getMonth() != (arr_date[0]-1))
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	return (dt_date)
}

// time parsing function
function fnParseTime (str_time, dt_date)
{
	if (!dt_date)
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	var arr_time = String(str_time ? str_time : '').split(':');

	if (!arr_time[0])
	{
		dt_date.setHours(0);
	}
	else if (RE_NUM.exec(arr_time[0]))
	{
		if (arr_time[0] < 24)
		{
			dt_date.setHours(arr_time[0]);
		}
		else
		{
			// Generate the current date
			return this.GenerateCurrentDateTime();
		}
	}
	else
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}
	
	if (!arr_time[1])
	{
		dt_date.setMinutes(0);
	}
	else if (RE_NUM.exec(arr_time[1]))
	{
		if (arr_time[1] < 60)
		{
			dt_date.setMinutes(arr_time[1]);
		}
		else
		{
			// Generate the current date
			return this.GenerateCurrentDateTime();
		}
	}
	else
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}

	if (!arr_time[2])
	{
		dt_date.setSeconds(0);
	}
	else if (RE_NUM.exec(arr_time[2]))
	{
		if (arr_time[2] < 60)
		{
			dt_date.setSeconds(arr_time[2]);
		}
		else
		{
			// Generate the current date
			return this.GenerateCurrentDateTime();
		}
	}
	else
	{
		// Generate the current date
		return this.GenerateCurrentDateTime();
	}

	dt_date.setMilliseconds(0);
	
	// return the date
	return dt_date;
}

// Generates the current date and if required time
function fnGenerateCurrentDateTime()
{
	// Get the current date and time
	var dtCurrentDataTime=new Date();
			
	// The current date or date and timr
	var strCurrentDateTime="";
			
	// Check to see that the 
	if (this.time_comp == true)
	{
		// Generate the time stamp
		strCurrentDateTime=this.gen_tsmp(dtCurrentDataTime);
	}
	else
	{
		// Generate the time stamp
		strCurrentDateTime=fnGenerateDate(dtCurrentDataTime);
	}

	// Set the values
	this.target.value=strCurrentDateTime;
	this.dt_current=strCurrentDateTime;
			
	// Create a new object and
	return dtCurrentDataTime;
}
