// HDCalendar 1.0

String.PAD_LEFT = 0;
String.PAD_RIGHT = 1;

String.prototype.pad = function(len, char, align) {
	
	char = char || ' ';
	align = align || String.PAD_LEFT;
	
	var diff = len - this.length;
	if (diff <= 0) return this;
		
	var left = 0, right = 0;
	
	switch (align) {
		case String.PAD_LEFT:
			left = diff;
			break;
		case String.PAD_RIGHT:		
		default:
			right = diff;
			
	}
	
	var res = '';		
	for (var i = 0; i < left; i++) res += char;
	res += this;
	for (var i = 0; i < right; i++) res += char;
	
	return res;
}
Date.prototype.add = function (interval, n) {

	switch (interval) {
		case "yyyy": 
			this.setFullYear(this.getFullYear() + n);			
			break;
		case "q": 
			this.setMonth(this.getMonth() + (n*3));
			break;
		case "m": 
			this.setMonth(this.getMonth() + n);
			break;
		case "y":
		case "d":
		case "w":
			this.setDate(this.getDate() + n);
			break;
		case "ww": 
			this.setDate(this.getDate() + (n*7));
			break;
		case "h": 
			this.setHours(this.getHours() + n);
			break;
		case "n": 
			this.setMinutes(this.getMinutes() + n);
			break;
		case "s": 
			this.setSeconds(this.getSeconds() + n);
			break;
		case "ms":
			this.setMilliseconds(this.getMilliseconds() + n);
			break;
	}
	
	return this;
}

Date.prototype.format = function(format) {
	
	var s = '';
	
	var d = ('' + this.getDate()).pad(2,'0');
	var m = ('' + (this.getMonth() + 1)).pad(2,'0');
	var y = ('' + (this.getFullYear() >= 2000 ? this.getFullYear() - 2000 : this.getFullYear())).pad(2, '0');
	
	
	s = d + '/' + m + '/' + y;
		
	return s;
}

function dateAdd(d, interval, n) {	
	var dObj = parseDate(d);
	dObj.add(interval, n);
	return dObj.format();	
}

function findPos(obj, parnt) {
	
	
	if (!parnt) parnt = document.body;

	var curleft = curtop = 0;
	
	var curwidth = curheight = 0;	
	
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		curwidth = obj.offsetWidth;
		curheight = obj.offsetHeight;
		
		
		while ((obj = obj.offsetParent) && (obj != parnt)) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	
	return {x:curleft,y:curtop, width:curwidth, height:curheight};
}


function attachEvent(obj, ev, func, b) {
	
	b = b || false;
	
	if (window.addEventListener) {
		obj.addEventListener(ev, func, b);		
	} else {
		switch (ev) {
			case 'click':
				obj.onclick = func;
				break;
			case 'mousedown':
				obj.onmousedown = func;
		}
	}
}


function getCalendar(obj) {
	
	var el = obj;
	
	while (el && el.nodeName.toLowerCase() != 'div') el = el.parentNode;		
	
	if (cal.div == el) return cal;

	return false;
}


function parseDate(str) {				
	
	try {
		var o = str.split(/\-|\/|\./);
		if (o.length < 3) return false;
		var dObj = new Date;
		o[2] = parseInt(o[2], 10);
		if (o[2] < 100) o[2] += 2000;
		if (dObj.setFullYear(o[2],o[1] - 1,o[0])) {
			return dObj;
		} else {
			return false;
		}
	} catch(e) {
		return false;
	}
		
}



function Calendar_hide () {

	if (this.ifrm) {
		document.body.removeChild(this.ifrm);
		this.visible = false;
		this.ifrm = null;

	}
}

function Calendar_apply (d) {
	
	if (d) {
		
		this.date = parseDate(d);
		
		if (this.target) {
			
			if (!this.callback || (this.callback(d) !== false)) {
				
				this.target.value= d;
				
			}
			
			if (this.autoHide) this.hide();
			
		}
		
	}
}

function Calendar_move(interval, n, doc) {

	switch (interval) {
		case 'Y':
			this.date.setFullYear(this.date.getFullYear() + n);
			break;
		case 'm':
			this.date.setFullYear(this.date.getFullYear(), this.date.getMonth() + n, 1);
			break;			
		case 'd':
		default:
			this.date.setDate(this.date.getDate() + n);		
	}	

	cal.refresh(doc);	
	
}


function drawCalendar(doc) {
	cal.refresh(doc);
}


function Calendar_refresh(doc) {

	var div = doc.getElementById('calendar');
	div.innerHTML = ' ';

	var _monthName = function(m) {
		var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
		return months[m];
	}
	
	var d = new Date; // Counter
	
	d.setFullYear(this.date.getFullYear(), this.date.getMonth(), 1);
	
	var endDate = new Date;
	endDate.setFullYear(this.date.getFullYear(), this.date.getMonth() + 1, 0);	
	
	var t = doc.createElement('table');				
	
	var c = doc.createElement('caption');
	
	
	// Controles
	if (!this.minDate || this.minDate < d) {	
		var a = doc.createElement('a');
		a.className = 'calendar-rw';			
		attachEvent(a, 'click', function() {					
			parent.cal.move('m', -1, this.ownerDocument);
			return false;
			}
		);
		a.appendChild(doc.createTextNode('<<'));			
		c.appendChild(a);	
	}

	var m = doc.createElement('span');	
	m.className = 'calendar-m';	
	m.appendChild(doc.createTextNode(_monthName(this.date.getMonth())));

	var y = doc.createElement('span');	
	y.className = 'calendar-y';
	y.appendChild(doc.createTextNode(this.date.getFullYear()));

	c.appendChild(m);	
	c.appendChild(y);
	
	
	if (!this.maxDate || this.maxDate > endDate) {
		var a = doc.createElement('a');
		a.className = 'calendar-ff';
	
		attachEvent(a, 'click', function(ev) {					
			parent.cal.move('m', 1, this.ownerDocument);
			return false;
		});
		
		a.appendChild(doc.createTextNode('>>'));
		
		c.appendChild(a);	
	}

	t.appendChild(c);	
	
	var tb = doc.createElement('tbody');		
	
	var r = doc.createElement('tr');	
	
	while (d.getUTCDay() != 1) {		
		d.add('d', -1);	
	}		

	while (d <= endDate) {
		
		var c = doc.createElement('td');
		c.className = 'cal-day';		
		
		if (d.getYear() == this.date.getYear() && d.getMonth() == this.date.getMonth()) {												
		
			c.title = d.format(this.format);		
			c.appendChild(doc.createTextNode(d.getDate()));
			
			if ((!this.minDate || this.minDate <= d) && (!this.maxDate || this.maxDate >= d)) {							
				attachEvent(c, 'click', function () {			
					parent.cal.apply(this.title);
					return true;
				});
				
				if (d.getDate() == this.date.getDate()) {
					c.className = 'cal-day-sel';
					
					c.onmouseover = function() {
						this.className
					};
				} else {
					c.onmouseover = function() {
						this.className = 'cal-day-over';
					};	
					
					c.onmouseout = function() {
						this.className = 'cal-day';
					}
				}
				
								
				
			} else {				
				c.className =  'cal-day-dis';
			}
			
			
		} else {
			c.className = 'cal-day-white';
		}
		
		r.appendChild(c);
		
		if (d.getUTCDay() == 0) {
			tb.appendChild(r);
			r = doc.createElement('tr');			
		}
		
		d.add('d', 1);
	}

	if (d.getUTCDay() != 1) {	
		while (d.getUTCDay() != 0) {
			var c = doc.createElement('td');
			c.className = 'cal-day-white'
			r.appendChild(c);
			d.add('d', 1);
		}
	}
	
	tb.appendChild(r);
	t.appendChild(tb);	
	
	//attachEvent(document, 'mousedown', pres);
	div.appendChild(t);
	
}

function Calendar_HTML() {
	
	
	var _monthName = function(m) {
		var months = ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
		return months[m];
	}
	
	var d = new Date; // Counter
	
	d.setFullYear(this.date.getFullYear(), this.date.getMonth(), 1);
	
	var endDate = new Date;
	endDate.setFullYear(this.date.getFullYear(), this.date.getMonth() + 1, 0);	
	
	var html = '';
	
	html += "<html><head><title></title>";
	html += '<link media="all" rel="stylesheet" type="text/css" href="http://www.hoteldescriptor.com/css/calendar.css" />';
	html += "</head><body onload=\"parent.drawCalendar(document)\"><div class=\"calendar\" id=\"calendar\">";	
	
	html += "</div></body></html>";	
	
	return html;
	

	
}

function Calendar_load () {	


	var ifrm = document.createElement('iframe');	
	

	c = this.HTML();

	ifrm.name = ifrm.id = 'calendar_278627';
	ifrm.className = 'calendar';
	ifrm.frameBorder = '0';
	ifrm.style.display = '';		
	ifrm.style.zIndex = '999';
	ifrm.src = "javascript:'" + c + "'";
	ifrm.allowTransparency = 'true';
	ifrm.scrolling = 'no';
													
	var pos = findPos(this.target);
	
	ifrm.style.position = 'absolute';
	ifrm.style.left = pos.x + 'px';
	ifrm.style.top = pos.y + pos.height + 'px';
	ifrm.style.zIndex= '999';
	
	this.ifrm = document.body.appendChild(ifrm);
	this.ifrm.doc = this.ifrm.contentDocument || this.ifrm.document;
	
	attachEvent(document, 'mousedown', function(e) {
		e = e || window.event;		
		t = e.target || e.srcElement		
		if (cal && (t != cal.target)) {
			cal.hide();
			if (document.removeEventListener) {
				document.removeEventListener('mousedown', arguments.callee, false);
			} else {
				document.onmousedown = null;
			}
		} 
		
		return false;
	});

	//div.style.MozUserSelect = 'none';
	//div.className = 'calendar';
	//div.style.backgroundColor = 'yellow';
	//this.ifrm.doc.body.appendChild(this.div);
	
	//this.div = doc.body.appendChild(div, true);
	
	
	//this.refresh();	
	
	
}


function Calendar(target) {
	
	// Propiedades
	this.target 	= target;
	this.date 		= new Date;
	this.format 	= 'dd/mm/YY';
	this.minDate 	= null;
	this.maxDate 	= null;
	this.autoHide 	= true;
	
	// Privadas
	this.format 	= null;
	this.visible 	= false;
	
	// Métodos
	this.hide 		= Calendar_hide;
	this.load 		= Calendar_load;
	this.apply 		= Calendar_apply;
	this.refresh	= Calendar_refresh;
	this.HTML 		= Calendar_HTML;
	this.move 		= Calendar_move;	
	this.callback 	= null;
	
}

cal = null;

function popupCalendar(target, format, date, minDate, maxDate, callback) {				

	if (!cal) {
		cal = new Calendar(target);
	} else if (cal.target !== target) {
		cal.hide();
		cal = new Calendar(target);
	}
		
	if (format) cal.format = format;
	if (date) cal.date = parseDate(date) || new Date();
	
	if (minDate) cal.minDate = parseDate(minDate) || null;	
	if (maxDate) cal.maxDate = parseDate(maxDate) || null;		
	
	if (callback) cal.callback = callback;	
	
	if (cal.minDate && cal.minDate > cal.date) cal.date = cal.minDate;
	if (cal.maxDate && cal.maxDate < cal.date) cal.date = cal.maxDate;
	
	cal.load();	
}