// Title: Tigra Calendar
// URL: http://www.softcomplex.com/products/tigra_calendar/
// Version: 3.2 (European date format)
// Date: 14-10-2002 (dd-mm-yyyy)
// Note: Permission given to use this script in ANY kind of applications if
//    header lines are left unchanged.
// Note: Script consists of two files: calendar?.js and calendar.html
// Modified : 20 July 2005, BNP PAM


// 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;

// months as they appear in the calendar's title
// default values of months
var ARR_MONTHS = ["January", "February", "March", "April", "May", "June",
		"July", "August", "September", "October", "November", "December"];
// week day titles as they appear on the calendar
// default values of days
var ARR_WEEKDAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
// day week starts from (normally 0-Su or 1-Mo)
var NUM_WEEKSTART = 1;


var calendars = [];
var RE_NUM = /^\-?\d+$/;

function calendar(obj_target, obj_target_real, path) {

	// assigning methods
	this.gen_date = cal_gen_date;

	this.gen_time = cal_gen_time;

	this.gen_tsmp = cal_gen_tsmp;

	//this.prs_date = cal_prs_date;

	this.prs_time = cal_prs_time;

	this.prs_tsmp = cal_prs_tsmp;

	this.clear = cal_clear;
	this.popup    = cal_popup;
	this.path = path;	

	// validate input parameters
	if (!obj_target)
		return cal_error("Error calling the calendar: no target control specified");
	if (obj_target.value == null)
		return cal_error("Error calling the calendar: parameter specified is not valid target control");
	this.target = obj_target; //Readonly field date in string
	this.target_real = obj_target_real; // Hidden field date in long

	this.time_comp = BUL_TIMECOMPONENT;
	this.year_scroll = BUL_YEARSCROLL;
	if (!this.months) {
		this.months = ARR_MONTHS;
	}
	if (!this.days) {
		this.days = ARR_WEEKDAYS;
	}
	// register in global collections
	this.id = calendars.length;
	calendars[this.id] = this;

}

function cal_popup (str_datetime) {

	this.dt_current = this.prs_tsmp(str_datetime ? str_datetime : this.target_real.value);

	if (!this.dt_current) return;

	var obj_calwindow = window.open(
		'/portal/ep/calendar/calendar.jsp?datetime=' + this.dt_current.valueOf()+ '&id=' + this.id,
		'Calendar', 'width=220,height='+(this.time_comp ? 215 : 190)+
		',status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes'
	);
	obj_calwindow.opener = window;
	obj_calwindow.focus();
}

// timestamp generating function
function cal_gen_tsmp (dt_datetime) {
	return formatDate(dt_datetime, this.date_pattern + " hh:mm:ss");
}

// date generating function
function cal_gen_date (dt_datetime) {
	return formatDate(dt_datetime, this.date_pattern);
}
// time generating function
function cal_gen_time (dt_datetime) {
	return formatDate(dt_datetime, "hh:mm:ss");
}

// timestamp parsing function sert à initialiser le calendrier
function cal_prs_tsmp (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)) {
		lres = new Date();
		lres.setTime(str_datetime.valueOf());
		return lres;
	}
	// else treat as date in string format
	var arr_datetime = str_datetime.split(' ');
	return getDateFromFormat(str_datetime, this.date_pattern);

}

// time parsing function
function cal_prs_time (str_time, dt_date) {
	return formatDate(dt_date, this.date_pattern + " hh:mm:ss");
}

function cal_error (str_message) {
	
	return null;
}


function cal_clear() {
	this.target.value = "";
	this.target_real.value = "";
}


function cal_onchange(field_name_readOnly, str_datetime, date_pattern, err_message) {
	if (field_name_readOnly != null && field_name_readOnly != "") {
		var field_name = field_name_readOnly.split("Readonly");
		if (str_datetime != null && str_datetime != "") {	
			if (!isDate(stringTrim(str_datetime), date_pattern)) {
				alert(err_message);
				document.getElementsByName(field_name_readOnly)[0].value = "";
				document.getElementsByName(field_name[0])[0].value = "";
				return false;
			} else {
				document.getElementsByName(field_name_readOnly)[0].value = str_datetime;
				document.getElementsByName(field_name[0])[0].value
						= getDateFromFormat(str_datetime, date_pattern);
			}
		} else {
				document.getElementsByName(field_name_readOnly)[0].value = "";
				document.getElementsByName(field_name[0])[0].value = "";
		}
	}
	
	return true;
}



