// Title: Tigra Calendar PRO (customized version)
// URL: http://www.softcomplex.com/products/tigra_calendar_pro/
// Version: 1.0 (customized)
var STR_ICONSPATH = '/spsys/';
// in year selection box how many years to list relative to current year
var NUM_YEARSBEFORE = 4;
var NUM_YEARSAFTER  = 4;
var NUM_COMBOS=6;
// --------------------------------------------------------------------------------
var calendars = [];
var ONOFF=0;
var OUT_ARR=[];

// Constructor
function calendar (DATE, str_ctrl_name, str_weekdays, str_months,desktop) {
	// assign methods
	//DATE is the date format from HireSpider by Boris Kolesnikov modification
	this.dateformat=DATE;
	this.NUM_WEEKSTART =(DATE==1)?1:0;
	this.ARR_WEEKDAYS=new Array();
	ARR_WEEKDAYS=str_weekdays.split(':');
	this.ARR_MONTHS=new Array();
	ARR_MONTHS=str_months.split(':');
	this.get_html  = cal_get_html;
	this.repos = cal_repos;
	this.get_abspos = cal_get_abspos;
	this.get_body  = cal_get_body;
	this.set_year  = cal_set_year;
	this.set_month = cal_set_month;
	this.set_day   = cal_set_day;
	this.fill_years= cal_fill_years;
	this.validate  = cal_validate;
	this.DESKTOP=desktop || false;
	this.show = cal_show;
	this.update = cal_update;
	this.FIELDNAME=str_ctrl_name;

	// register in global collections
	this.id = calendars.length;
	calendars[this.id] = this;

	// process input parameters
	var re_url = new RegExp('cal' + this.id + '_val=(\\d+)');
	var dt_params = cal_date_only();
	this.dt_current = (re_url.exec(String(window.location))
		? new Date(new Number(RegExp.$1)) : dt_params);

	this.form_name = 0;
	this.control_name = (str_ctrl_name ? str_ctrl_name : 'datetime_' + this.id);

	var b_ok = (document.body && document.body.innerHTML);
	document.write('</td>' + (b_ok ? '<td>&nbsp;</td><td><a href="javascript:calendars[' + this.id + '].show();"><img src="'+ STR_ICONSPATH + 'cal.gif" width="16" height="16" border="0" alt="click to select date from the calendar" name="calicon' + this.id + '" id="calicon' + this.id + '"></a></td></tr><tr><td colspan="3"><img src="'+ STR_ICONSPATH + 'pixel.gif" width="1" height="1" border="0" name="calpos' + this.id + '" id="calpos' + this.id + '"></td>' : '') + '</tr></table>');
	if (!b_ok) return;
	
	this.posit = document.all ? document.all['calpos' + this.id] :
				document.images ? document.images['calpos' + this.id] :
				document.getElementById('calpos' + this.id);
				
	this.icon = document.all ? document.all['calicon' + this.id] :
				document.images ? document.images['calicon' + this.id] :
				document.getElementById('calicon' + this.id);

	document.write('<div id="caldiv' + this.id + '" style="position: absolute; left:0; top:0; width:170; height:170; visibility:hidden; z-index: ' + this.id + '">' + this.get_html() + '</div>');
	this.box_style = document.all
	//this.form_kal=document.forms[this.form_name]
		? document.all['caldiv' + this.id].style
		: document.getElementById('caldiv' + this.id).style;
	this.mon_ctrl    = document.forms[this.form_name].elements['calmon'  + this.id];
	this.year_ctrl   = document.forms[this.form_name].elements['calyear' + this.id];
	this.fill_years(this.dt_current.getFullYear());

	this.control_obj = document.forms[this.form_name].elements[this.control_name];
	this.repos();
}
function cal_get_html () {
	var dt_datetime = this.dt_current;
	var str_buffer =	
		'<table cellspacing="0" style="border: 1px solid Gray; width: 170px;" border="0" width="100%">'+
		'<tr><td class=HS1082><table cellspacing="0" width="100%" border="0">'+
		'<tr><td><select name="calmon' + this.id + '" class="HS1047" onchange="return calendars[' + this.id + '].set_month();">';
	for (i = 0; i < 12; i++)
		str_buffer += '<option value="' + i + '"' + (i == dt_datetime.getMonth() ? ' selected' : '') + '>' + ARR_MONTHS[i] + '</option>';

	str_buffer += '</select></td><td align="right">'+
	'<select name="calyear' + this.id + '" class="HS1047" onchange="return calendars[' + this.id + '].set_year();">';
	str_buffer += '</select></td></tr></table></td></tr>'+
	'<tr><td><div id="calbody' + this.id + '">' + this.get_body(dt_datetime) + '</div></td></tr></table>';

	return (str_buffer);
}
function cal_repos () {
	this.box_style.left = (this.posit.x ? this.posit.x : this.get_abspos('Left')) + 'px';
	this.box_style.top  = (this.posit.y ? this.posit.y : this.get_abspos('Top')) + 'px';
}
function cal_get_abspos (s_coord) {
	var n_pos = 0,
		o_elem = this.posit;
	while (o_elem)	{
		n_pos += o_elem["offset" + s_coord];
		o_elem = o_elem.offsetParent;
	}
	return n_pos;
}

function cal_show() {
	var s_visib = String(this.box_style.visibility).toLowerCase();

	if (s_visib == 'visible' || s_visib == 'show') {
		this.box_style.visibility = 'hidden';
		this.icon.src = STR_ICONSPATH + 'cal.gif';
		ONOFF--;

 	}
	else {
		this.repos();
		var dt_ctrl = cal_date_only();
		if (this.dt_current.valueOf() != dt_ctrl.valueOf()) 
		{
			this.dt_current = dt_ctrl;
			this.update(this.dt_current);
		}
		this.box_style.visibility = 'visible';
		this.icon.src = STR_ICONSPATH + 'no_cal.gif';
			if (!opera) {
						if (this.DESKTOP==false) {
               		Outers(this.form_name,this.FIELDNAME);
        				hide(this.form_name,this.FIELDNAME);
               		} else {
               		Outers_desktop();
        				hide_on_desktop();
        				}
			ONOFF++;
			}
	}
	//alert(ONOFF);
		if (!opera && ONOFF <= 0) {
			if (this.DESKTOP==false)
			unhide(this.form_name);
			else
			unhide_on_desktop();
		ONOFF=0;
		}
}

function cal_update (dt_datetime) {
	this.dt_current = dt_datetime;
	var obj_container = (document.all ?
		document.all['calbody' + this.id] :
		document.getElementById('calbody' + this.id)
	);
	obj_container.innerHTML =  this.get_body(dt_datetime);
	this.mon_ctrl.selectedIndex = dt_datetime.getMonth(); 
	for (var i = 0; i < this.year_ctrl.options.length; i++)
		this.year_ctrl.options[i].selected = (this.year_ctrl.options[i].text == dt_datetime.getFullYear());
}
function cal_set_day (num_datetime) {
	var dt_newdate = new Date(num_datetime);
	if (this.validate(dt_newdate)) {
		this.control_obj.value = cal_generate_date(dt_newdate,this.dateformat);
		this.show();
	}
	else
		this.update(this.dt_current);
}
function cal_set_month () {
	var dt_newdate = new Date(this.dt_current);
	var num_month = this.mon_ctrl.options[this.mon_ctrl.selectedIndex].value;
	
	dt_newdate.setMonth(num_month);
	if (num_month != dt_newdate.getMonth())
		dt_newdate.setDate(0);
	
	if (this.validate(dt_newdate))
		this.update(dt_newdate);
	else
		this.mon_ctrl.selectedIndex = this.dt_current.getMonth();
}
function cal_set_year () {
	var dt_newdate = new Date(this.dt_current);
	var str_year = this.year_ctrl.options[this.year_ctrl.selectedIndex].text;
	var str_scroll = this.year_ctrl.options[this.year_ctrl.selectedIndex].value;

	var num_year;
	if (str_scroll) {
		num_year = (str_scroll == '+'
			? this.dt_current.getFullYear() + NUM_YEARSAFTER
			: this.dt_current.getFullYear() - NUM_YEARSBEFORE
		);
	}
	else
		num_year = new Number(str_year);
	dt_newdate.setFullYear(num_year);
	var num_month = this.mon_ctrl.options[this.mon_ctrl.selectedIndex].value;
	if (num_month != dt_newdate.getMonth())
		dt_newdate.setDate(0);

	if (!this.validate(dt_newdate)) {
		this.year_ctrl.selectedIndex =
			this.dt_current.getFullYear() - Number(this.year_ctrl.options[1].text) + 1;
		return;
	}

	if (str_scroll)
		this.fill_years(num_year);
	this.update(dt_newdate);
}

function cal_fill_years (num_year) {
		this.year_ctrl.length = 0;

		var num_begin_year = num_year - NUM_YEARSBEFORE,
			b_topscroll = true;
		if (this.min_date && num_begin_year <= this.min_date.getFullYear()) {
			num_begin_year = this.min_date.getFullYear();
			b_topscroll = false;
		}
		var num_end_year = num_year + NUM_YEARSAFTER,
			b_bottomscroll = true;
		if (this.max_date && num_end_year >= this.max_date.getFullYear()) {
			num_end_year = this.max_date.getFullYear();
			b_bottomscroll = false;
		}

		if (b_topscroll)
			this.year_ctrl.options[0] = new Option('<< ' + num_begin_year, '-');
		var num_idex = b_topscroll ? 1 : 0;
		for (var i = num_begin_year + num_idex; i < num_end_year + (b_bottomscroll ? 0 : 1); i++) {
			this.year_ctrl.options[num_idex] = new Option(i);
			this.year_ctrl.options[num_idex].selected = (i == num_year);
			num_idex++;
		}
		if (b_bottomscroll)
			this.year_ctrl.options[NUM_YEARSBEFORE + NUM_YEARSAFTER] = new Option((num_end_year) + ' >>', '+');
}

function cal_get_body (dt_datetime) {

	var dt_firstday = new Date(dt_datetime);
	dt_firstday.setDate(1);
	dt_firstday.setDate(1 - (7 + dt_firstday.getDay() - this.NUM_WEEKSTART) % 7);

	var str_buffer = new String (
	'<table cellspacing="0" cellpadding="2" width="100%">');

	str_buffer += '<tr>';
	for (var n = 0; n < 7; n++)
		str_buffer += '<td style="vertical-align: baseline; text-align: center;" class="HS1024" align=center><strong>' + ARR_WEEKDAYS[(this.NUM_WEEKSTART + n) % 7] + '</strong></td>';
	str_buffer += "</tr>\n";

	var dt_current_day = new Date(dt_firstday);
	while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
		dt_current_day.getMonth() == dt_firstday.getMonth()) {
		str_buffer += '<tr>';
		var ul=['',''];
		for (var n_current_wday = 0; n_current_wday < 7; n_current_wday++) {
			if (dt_current_day.getMonth() == dt_datetime.getMonth()) {
			ul=['<u>','</u>'];
			} else {
			ul=['',''];
			}

				if (dt_current_day.getDate() == dt_datetime.getDate() &&
					dt_current_day.getMonth() == dt_datetime.getMonth()) {
					str_buffer += '<td class="HS1024" style="vertical-align: baseline; text-align: center;" align="center" valign="middle">';
					str_buffer += '<a href="javascript:calendars[' + this.id +
						'].set_day(' + dt_current_day.valueOf() + ');" class="HS1024">'
						+ul[0]+ dt_current_day.getDate() + ul[1]+'</a></td>';
				} else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6) {
					str_buffer += '<td class="HS1011" style="vertical-align: baseline; text-align: center;" align="center" valign="middle">';
					str_buffer += '<a href="javascript:calendars[' + this.id +
						'].set_day(' + dt_current_day.valueOf() + ');" class="HS1011">'
						+ul[0]+ dt_current_day.getDate() + ul[1]+'</a></td>';
				} else {
					str_buffer += '<td class="HS1082" style="vertical-align: baseline; text-align: center;" align="center" valign="middle">';
					str_buffer += '<a href="javascript:calendars[' + this.id +
						'].set_day(' + dt_current_day.valueOf() + ');" class="HS1082">'
						+ul[0]+ dt_current_day.getDate() + ul[1]+'</a></td>';
				}

				dt_current_day.setDate(dt_current_day.getDate() + 1);
		}
		str_buffer += "</tr>\n";
	}
	str_buffer +=
		'</table>';

	return (str_buffer);
}
function cal_date_only () {
	var dt_datetime = new Date();
	dt_datetime.setHours(0);
	dt_datetime.setMinutes(0);
	dt_datetime.setSeconds(0);
	dt_datetime.setMilliseconds(0);
	return dt_datetime;
}

function cal_validate (dt_datetime, b_silent) {
return true;
}


function cal_pre () {
document.write('<table cellpadding="0" cellspacing="0" border="0"><tr><td class="HS1001">');
}

//formats modified by Boris Kolesnikov
//to be competible with the existing code
// date object to string converter, output format is dd-mm-yyyy
// this method may be customized - string date format
function cal_generate_date (dt_date,dateformat) {
	if (dateformat==1)
	return (
		new String (
			(dt_date.getDate() < 10 ? '0' : '') + dt_date.getDate() + "-"
			+ (dt_date.getMonth() < 9 ? '0' : '') + (dt_date.getMonth() + 1) + "-"
			+ dt_date.getFullYear()
		)
	);

	else if (dateformat==2)
	return (
		new String (
			(dt_date.getMonth() < 9 ? '0' : '') + (dt_date.getMonth() + 1) + '-'
			+ (dt_date.getDate() < 10 ? '0' : '') + dt_date.getDate() + '-'
			+ dt_date.getFullYear()
		)
	);

	else if (dateformat==3)
	return (
		new String (
			dt_date.getFullYear() + "-"
			+ (dt_date.getMonth() < 9 ? '0' : '') + (dt_date.getMonth() + 1) + "-"
			+ (dt_date.getDate() < 10 ? '0' : '') + dt_date.getDate()
		)
	);

	else if (dateformat==4)
	return (
		new String (
			dt_date.getFullYear() + "-"
			+ (dt_date.getDate() < 10 ? '0' : '') + dt_date.getDate() + "-"
			+ (dt_date.getMonth() < 9 ? '0' : '') + (dt_date.getMonth() + 1)
		)
	);

}
//section by Boris Kolesnikov that hides comboboxes
function hide(form_name,field_name) {
var thisform=document.forms[form_name];
var select=/select/i; var iskal=/^cal/; var flag=0;
var count=0;
	for (var i=0; i<thisform.elements.length;i++)
	{
	  if (thisform.elements[i].name==field_name) flag=1;

	   	if (flag && !iskal.exec(thisform.elements[i].name) 
	   	&& select.exec(thisform.elements[i].type))
	   	{
                  thisform.elements[i].outerHTML='<input type=hidden name='
                  +thisform.elements[i].name+'>';
                  ++count;
                  if (count==NUM_COMBOS) break;
	   	}
	}
}

function hide_on_desktop() {
var firstform=document.forms[0];
	if (!document.forms[1]) return true;
var secondform=document.forms[1];
var select=/select/i; var iskal=/^cal/; var flag=0;
var count=0;
	for (var i=0; i<secondform.elements.length;i++)
	{

	   	if (!iskal.exec(firstform.elements[i].name) 
	   	&& select.exec(secondform.elements[i].type))
	   	{
                  secondform.elements[i].outerHTML='<input type=hidden name='
                  +secondform.elements[i].name+'>';
                  ++count;
                  if (count==NUM_COMBOS) break;
	   	}
	}
}


function unhide(form_name) {
var thisform=document.forms[form_name];
	
	for (var i in this.OUT_ARR)
	{
			if (thisform.elements[i])
          	thisform.elements[i].outerHTML=this.OUT_ARR[i];
	}

}

function unhide_on_desktop() {
	if (!document.forms[1]) return true;
var secondform=document.forms[1];
	for (var i in this.OUT_ARR)
	{
			if (secondform.elements[i])
	          secondform.elements[i].outerHTML=this.OUT_ARR[i];
	}

}


function Outers(form_name,field_name) {
var thisform=document.forms[form_name];
var select=/select/i; var iskal=/^cal/;
var flag=0;
	for (var i=0; i<thisform.elements.length;i++)
	{
		if (thisform.elements[i].name==field_name) flag=1; 
   	  if (flag && !iskal.exec(thisform.elements[i].name) && 
   	  select.exec(thisform.elements[i].type)) 	  {
   	     OUT_ARR[thisform.elements[i].name]=thisform.elements[i].outerHTML;
           }
	}
}

function Outers_desktop() {
	if (!document.forms[1]) return true;
var firstform=document.forms[0];
var secondform=document.forms[1];
var select=/select/i; var iskal=/^cal/; var flag=0;
	for (var i=0; i<secondform.elements.length;i++)
	{
   	  if (!iskal.exec(firstform.elements[i].name) && 
   	  select.exec(secondform.elements[i].type)) 	  {
   	     OUT_ARR[secondform.elements[i].name]=secondform.elements[i].outerHTML;
           }
	}
}

