// Title: Tigra Calendar
// URL: http://www.softcomplex.com/products/tigra_calendar/
// Version: 3.3 (European date format)
// Date: 09/01/2005 (mm/dd/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

// 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+$/;
var RE_NUM_SIGN = /^(-\d|\d)\d*$/;
var callingWindow = null;
var dateErrors = "";

function calendar(obj_target) {
	
	// 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.popup    = cal_popup;
	callingWindow = obj_target.id;

	// validate input parameters
	if (!obj_target){
		alert(errorConstructer(2, null));
		return null;
	}
	if (obj_target.value == null){
		alert(errorConstructer(3, 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;
}

function cal_popup (str_datetime, cotaInf, cotaSup) {
    
	if (str_datetime) {
		this.dt_current = this.prs_tsmp(str_datetime);
	}
	else {
		this.dt_current = this.prs_tsmp(this.target.value, true);
		this.dt_selected = this.dt_current;
	}
	if (!this.dt_current) return;
	var obj_calwindow = window.open('calendario.htm?datetime=' + this.dt_current.valueOf()+ 
		'&id=' + this.id + '&cWindow=' + callingWindow + '&cotaInf=' + cotaInf + '&cotaSup=' + cotaSup,'Calendar','width=200,height='+
		(this.time_comp ? 235 : 210)+ 
		',status=no,resizable=yes,top=250,left=250,dependent=yes,alwaysRaised=yes');
	obj_calwindow.opener = window;
	obj_calwindow.focus();
}

// timestamp generating function
function cal_gen_tsmp (dt_datetime) {
	return(this.gen_date(dt_datetime) + ' ' + this.gen_time(dt_datetime));
}

// date generating function
function cal_gen_date (dt_datetime) {
	return (
		(dt_datetime.getDate() < 10 ? '0' : '') + dt_datetime.getDate() + "/"
		+ (dt_datetime.getMonth() < 9 ? '0' : '') + (dt_datetime.getMonth() + 1) + "/"
		+ dt_datetime.getFullYear()
	);
}
// time generating function
function cal_gen_time (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 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_SIGN.exec(str_datetime))
		return new Date(str_datetime);

	// else treat as date in string format
	return this.prs_time(null , this.prs_date(str_datetime, true));
}

// date parsing function
function cal_prs_date (str_date, aBool, txtName) {
	dateErrors = "";
	var str_date_copia = str_date;
	var arr_date = str_date.split('/');
	if (arr_date.length == 1){
		arr_date = null;
		arr_date = str_date.split(' ');
	}
	if (arr_date.length == 2 || arr_date.length == 3){
		if (!arr_date[0]){ 
			if (aBool == false) cal_error(errorConstructer(13, new Array(txtName, str_date)));
			else cal_error(errorConstructer(4, new Array(str_date)), aBool);
			return null;
		}
		if (!RE_NUM.exec(arr_date[0])){ 
			if (aBool == false) cal_error(errorConstructer(14, new Array(txtName, arr_date[0])));
			else cal_error(errorConstructer(5, new Array(arr_date[0])), aBool);
			return null;	
		}
		if (!arr_date[1]){
			if (aBool == false) cal_error(errorConstructer(15, new Array(txtName, str_date)));
			else cal_error(errorConstructer(6, new Array(str_date)), aBool);
			return null;
		}
		if (!RE_NUM.exec(arr_date[1])){ 
			if (aBool == false) cal_error(errorConstructer(16, new Array(txtName, arr_date[1])));
			else cal_error(errorConstructer(7, new Array(arr_date[1])), aBool);
			return null;
		}
		if (arr_date.length == 3){
			if (!arr_date[2]){ 
				if (aBool == false) cal_error(errorConstructer(17, new Array(txtName, str_date)));
				else cal_error(errorConstructer(8, new Array(str_date)), aBool);
				return null;
			}
			if (!RE_NUM.exec(arr_date[2])){ 
				if (aBool == false) cal_error(errorConstructer(18, new Array(txtName, arr_date[2])));
				else cal_error(errorConstructer(9, new Array(arr_date[2])), aBool);
				return null;
			}
		}
	}else{
		if (aBool == false) cal_error(errorConstructer(19, new Array(txtName, str_date)));
		else cal_error(errorConstructer(10, new Array(str_date)), aBool);
		return null;
	}
	
	var dt_date = new Date();
	dt_date.setDate(1);

	if (arr_date[1] < 1 || arr_date[1] > 12){
		if (aBool == false) cal_error(errorConstructer(20, new Array(txtName, arr_date[1]))); 
		else cal_error(errorConstructer(11, new Array(arr_date[1])), aBool);
		return null;
	}
	dt_date.setMonth(arr_date[1]-1);
	if (arr_date.length > 2){
		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]);
	}else{
		var _auxDate = new Date();
		dt_date.setFullYear(_auxDate.getFullYear());
	}

	var dt_numdays = new Date(arr_date[2], arr_date[1], 0);
	dt_date.setDate(arr_date[0]);
	if ((dt_date.getMonth()) != (arr_date[1]-1)){
		if (aBool == false) cal_error(errorConstructer(21, new Array(txtName, dt_numdays.getDate())));
		else cal_error(errorConstructer(12, new Array(arr_date[0], dt_numdays.getDate())), aBool);
		return null;
	}
	return (dt_date);
}
//-time parsing function

function cal_prs_time (str_time, dt_date) {
	if (!dt_date) return null;
	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 return cal_error ("Invalid hours value: '" + arr_time[0] + "'. Allowed range is 00-23.", true);
	else return cal_error ("Invalid hours value: '" + arr_time[0] + "'. Allowed values are unsigned integers.", true);

	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 return cal_error ("Invalid minutes value: '" + arr_time[1] + "'. Allowed range is 00-59.", true);
	else return cal_error ("Invalid minutes value: '" + arr_time[1] + "'. Allowed values are unsigned integers.", true);

	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 return cal_error ("Invalid seconds value: '" + arr_time[2] + "'. Allowed range is 00-59.", true);
	else return cal_error ("Invalid seconds value: '" + arr_time[2] + "'. Allowed values are unsigned integers.", true);

	dt_date.setMilliseconds(0);
	return dt_date;
}

//Thomas - Burke
//función que comprueba la fecha onSubmit
function checkDate(elem, aBool){
	var fecha = elem.value;
	fecha = fecha.replace(/^\s+|\s+$/, '');
	if (fecha != ''){
		var auxFecha = cal_prs_date(fecha, aBool, getElemName(elem));
	 	if (auxFecha != null){
	 		elem.value = 
	 			(auxFecha.getDate() + "/" + (auxFecha.getMonth()+1) + "/" + auxFecha.getFullYear());
	 			return true;
		}
		else {
			return false;
		} 	
	}	
	return true;
}

//Thomas - Burke
//función que lanza un alert con un mensaje de error
function cal_error (str_message, aBool) {
	if (aBool == true)
		 alert(str_message);
	else dateErrors += str_message + '<BR>';
	
}

