var LNScal_ie = false;
if (document.all) { LNScal_ie = true; }

var calendars = Array();
var LNScal_months = Array('Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic');
var LNScal_days = Array('D', 'L', 'M', 'M', 'J', 'V', 'S');
var LNScal_openCal;

var LNScal_calCount = 0;

function getCalendar(fieldId) {
	return calendars[fieldId];
}

function dLNSCalendF(fieldId, THEForm) {
	var formElement = LNScal_getObj(fieldId);
	displayCalendar(formElement, THEForm);
}

function displayCalendar(formElement,LNSform) {
	if (!formElement.id) {
		formElement.id = LNScal_calCount++;
	}
	var cal = calendars[formElement.id];
	if (typeof(cal) == 'undefined') {
		cal = new LnsXCalendar();
		cal.setElement(formElement,LNSform);
		calendars[formElement.id] = cal;
	}
	if (cal.shown) {
		cal.hide();
	} else {
		cal.show();
	}
}

function LnsXCalendar() {
	this.setElement = LNScal_setElement;
	this.parseDate = LNScal_parseDate;
	this.generateHTML = LNScal_generateHTML;
	this.show = LNScal_show;
	this.hide = LNScal_hide;
	this.moveMonth = LNScal_moveMonth;
	this.setDate = LNScal_setDate;
	this.formatDate = LNScal_formatDate;
	this.setDateFields = LNScal_setDateFields;

	this.shown = false;
}
	
function LNScal_setElement(formElement,elform) {
	this.elform = elform;
	this.element = formElement;
	this.format = 'YYYY-MM-DD';
	this.value = this.element.value;
	this.id = this.element.id;
	this.mode = 1;
}

function LNScal_parseDate() {
	if (this.element.value) {
		this.date = new Date();
		var out = '';
		var token = '';
		var lastCh, ch;
		var start = 0;
		lastCh = this.format.substring(0, 1);
		for (i = 0; i < this.format.length; i++) {
			ch = this.format.substring(i, i+1);
			if (ch == lastCh) {
				token += ch;
			} else {
				LNScal_parseToken(this.date, token, this.element.value, start);
				start += token.length;
				token = ch;
			}
			lastCh = ch;
		}
		LNScal_parseToken(this.date, token, this.element.value, start);
	} else {
		this.date = new Date();
	}
	if ('' + this.date.getMonth() == 'NaN') {
		this.date = new Date();
	}
}


function LNScal_setDate(d, m, y) {
	this.date.setYear(y);
	this.date.setMonth(m);
	this.date.setDate(d);
	if (this.mode == 1) {
		this.element.value = this.formatDate();
	} else {
		this.setDateFields();
	}
	this.hide();
}

function LNScal_setDateFields() {
	LNScal_setFieldValue(this.mElement, LNScal_zeroPad(this.date.getMonth() + 1));
	LNScal_setFieldValue(this.dElement, LNScal_zeroPad(this.date.getDate()));
	LNScal_setFieldValue(this.yElement, this.date.getFullYear());
}

function LNScal_formatDate() {
	var out = '';
	var token = '';
	var lastCh, ch;
	lastCh = this.format.substring(0, 1);
	for (i = 0; i < this.format.length; i++) {
		ch = this.format.substring(i, i+1);
		if (ch == lastCh) {
			token += ch;
		} else {
			out += LNScal_formatToken(this.date, token);
			token = ch;
		}
		lastCh = ch;
	}
	out += LNScal_formatToken(this.date, token);
	return out;
}

function LNScal_show() {
	if (typeof(LNScal_openCal) != 'undefined') { LNScal_openCal.hide(); }

	this.parseDate();
	this.showDate = new Date(this.date.getTime());
	if (typeof(this.div) != 'undefined') {
		this.div.innerHTML = this.generateHTML();
	}

	if (typeof(this.div) == 'undefined') {
		this.div = document.createElement('div');
		this.div.style.position = 'absolute';
		/*this.div.style.zIndex=1010;*/
		var coors = LNSfindPos(this.element);
		this.div.style.left = coors[0] + 'px';
		this.div.style.top = coors[1] + 'px';
/*
		this.div.style.left = LNScal_absoluteOffsetLeft(this.element) + 'px';
		this.div.style.top = (LNScal_absoluteOffsetTop(this.element) + this.element.offsetHeight + 1) + 'px';
		this.div.style.top = (LNScal_absoluteOffsetTop(this.element)+1) + 'px';
		this.div.style.top = (LNScal_absoluteOffsetTop(this.element)+1) + 'px';
*/
		this.div.className = 'LNScal';
		this.div.style.zIndex=0;
		this.div.style.display = 'none';
		this.div.innerHTML = this.generateHTML();
		document.body.appendChild(this.div);
	}
	LNShideshowselect(this.elform, true, this.element.name);
	this.div.style.display = 'block';
	this.shown = true;
	LNScal_openCal = this;
}

function LNScal_generateHTML() {
	var mydiv='<div class="LNScald">';
	var mydivc='</div>';
	var mylnk='<a href="javascript: void(0);"';
	var mylnkc='</a>';
	var myul='<ul class="LNScalL">';
	var myulc='</ul>';
	var myli='<li>';
	var mylic='</li>';
	var myp='<p>';
	var mypc='</p>';
	var mypT='<p class="LNScalT">';
	var mypX='<p class="LNScalX">';
	var mypMY='<p class="LNScalMY">';
	var mypDT='<p class="LNScalDT">';
	var mypDTF='<p class="LNScalDTF">';
	var mypDF='<p class="LNScalDF">';
	var mypDS='<p class="LNScalDS">';
	var mypDH='<p class="LNScalDH">';
	var dow = 0;
	var col = 3;
	var i, estilo;
	var html = mydiv + myul + myli;
	html += mypT + 'Seleccione Fecha' + mypc;
	html += mypX + mylnk + ' onClick="getCalendar(\'' + this.id + '\').hide();">X' + mylnkc + mypc;
	html += mylic + myulc + myul + myli;
	html += mypX + mylnk + ' onClick="getCalendar(\'' + this.id + '\').moveMonth(-12);">&laquo;' + mylnkc + mypc;
	html += mypX + mylnk + ' onClick="getCalendar(\'' + this.id + '\').moveMonth(-1);">&#8249;' + mylnkc + mypc;
	html += mypMY + LNScal_months[this.showDate.getMonth()] + '&nbsp;' + LNScal_getYear(this.showDate) + mypc;
	html += mypX + mylnk + ' onClick="getCalendar(\'' + this.id + '\').moveMonth(1);">&#8250;' + mylnkc + mypc;
	html += mypX + mylnk + ' onClick="getCalendar(\'' + this.id + '\').moveMonth(12);">&raquo;' + mylnkc + mypc;
	html += mylic + myulc + myul + myli;
	html += mypDTF + LNScal_days[0] + mypc;
	for (i = 1; i < 7; i++) {
		html += mypDT + LNScal_days[i] + mypc;
	}
	html += mylic + myulc ;
	var totaldias = LNScal_monthLength(this.showDate);
	var pridia =LNScal_firstDOW(this.showDate);
	var dia=1;
	for (i=0; i<6; i++) {
		html +=  myul + myli;
		for (j=0;j<7; j++) {
			if ((i==0) &&(j<pridia)) {
				html += myp + '&nbsp;' + mypc;
			} else {
				if (dia <= totaldias) {
					if (this.showDate.getMonth() == this.date.getMonth() && this.showDate.getYear() == this.date.getYear() && this.date.getDate() == dia ) html += mypDH;
					else {
						if (j==0) html += mypDF;
						else html += mypDS;
					}
					html += mylnk + ' onClick="getCalendar(\'' + this.id + '\').setDate(' + dia + ', ' + this.showDate.getMonth() + ', ' + this.showDate.getFullYear() + ');">' + dia + mylnkc + mypc;
					dia++;
				} else {
					html += myp + '&nbsp;' + mypc;
				}
			}
		}
		html += mylic + myulc ;
	}
	html += mydivc;
	return html;
}

function LNScal_hide() {
	if (this.div != false) {
		LNShideshowselect(this.elform, false, this.element.name);
		this.div.style.display = 'none';
	}
	this.shown = false;
	LNScal_openCal = undefined;
}

function LNScal_moveMonth(amount) {
	var m = this.showDate.getMonth();
	var y = LNScal_getYear(this.showDate);
	if (amount == 1)  {
		if (m == 11)  {
			this.showDate.setMonth(0);
			this.showDate.setYear(y + 1);
		} else {
			this.showDate.setMonth(m + 1);
		}
	} else if (amount == -1)  {
		if (m == 0)  {
			this.showDate.setMonth(11);
			this.showDate.setYear(y - 1);
		} else {
			this.showDate.setMonth(m - 1);
		}
	} else if (amount == 12) {
		this.showDate.setYear(y + 1);
	} else if (amount == -12) {
		this.showDate.setYear(y - 1);
	}
	this.div.innerHTML = this.generateHTML();
}

function LNScal_firstDOW(date) {
	var xday = date;
	xday.setDate(1);
	var dow = date.getDay();
	return dow;
}
		
function LNScal_getYear(date) {
	var y = date.getYear();
	if (y > 1900) return y;
	return 1900 + y;
}
		
function LNScal_monthLength(date) {
	var month = date.getMonth();
	var totald = 30;
	if (month == 0
		|| month == 2
		|| month == 4
		|| month == 6
		|| month == 7
		|| month == 9
		|| month == 11) totald = 31;
	if (month == 1) {
		var year = date.getYear();
		if (year % 4 == 0 && (year % 400 == 0 || year % 100 != 0))
			totald = 29;
		else
			totald = 28;
	}
	return totald;
		}
		
		function LNScal_formatToken(date, token) {
	var command = token.substring(0, 1);
	if (command == 'y' || command == 'Y') {
		if (token.length == 2) { return LNScal_zeroPad(date.getFullYear() % 100); }
		if (token.length == 4) { return date.getFullYear(); }
	}
	if (command == 'd' || command == 'D') {
		if (token.length == 2) { return LNScal_zeroPad(date.getDate()); }
	}
	if (command == 'm' || command == 'M') {
		if (token.length == 2) { return LNScal_zeroPad(date.getMonth() + 1); }
		if (token.length == 3) { return LNScal_months[date.getMonth()]; }
	}
	return token;
		}
		
		function LNScal_parseToken(date, token, value, start) {
	var command = token.substring(0, 1);
	var v;
	if (command == 'y' || command == 'Y') {
		if (token.length == 2) {
			v = value.substring(start, start + 2);
			if (v < 70) { date.setFullYear(2000 + parseInt(v)); } else { date.setFullYear(1900 + parseInt(v)); }
		}
		if (token.length == 4) { v = value.substring(start, start + 4); date.setFullYear(v);}
	}
	if (command == 'd' || command == 'D') {
		if (token.length == 2) { v = value.substring(start, start + 2); date.setDate(v); }
	}
	if (command == 'm' || command == 'M') {
		if (token.length == 2) { v = value.substring(start, start + 2); date.setMonth(v - 1); }
		if (token.length == 3) {
			v = value.substring(start, start + 3);
			var i;
			for (i = 0; i < LNScal_months.length; i++) {
				if (LNScal_months[i].toUpperCase() == v.toUpperCase()) { date.setMonth(i); }
			}
		}
	}
		}
		
function LNScal_zeroPad(num) {
	if (num < 10) { return '0' + num; }
	return num;
}

function LNScal_getObj(id) {
	if (LNScal_ie) { return document.all[id]; }
	else { return document.getElementById(id);	}
}

function LNScal_setFieldValue(field, value) {
	if (field.type.substring(0,6) == 'select') {
		var i;
		for (i = 0; i < field.options.length; i++) {
			if (LNScal_equals(field.options[i].value, value)) {
							field.selectedIndex = i;
			}
		}
	} else {
		field.value = value;
	}
}

function LNScal_getFieldValue(field) {
	if (field.type.substring(0,6) == 'select') {
		return field.options[field.selectedIndex].value;
	} else {
		return field.value;
	}
}

function LNScal_equals(val1, val2) {
	if (val1 == val2) return true;
	if (1 * val1 == 1 * val2) return true;
	return false;
}

function LNShideshowselect(tf, t, ne) {
	if (LNScal_ie) {
		var xx=(!t) ? 'visible' : 'hidden';
		var xx2=false;
		for (var i = 0; i < tf.elements.length; i++) {
			if (tf.elements[i].name==ne) xx2=true;
			if ((tf.elements[i].type=='select-one') && (xx2)) tf.elements[i].style.visibility=xx;
		}
	}
}

function LNSfindPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}
