Calendar = function(form, params) {
params || (params = {});
params.name || (params.name = "datepicker");
this.ajxcheck = params.ajxcheck;
this.on = params.on;
this.actives = [];
this.date = (params.date ? params.date : new Date());
this.form = form;
this.input = document.createElement("input");
this.input.type = "hidden";
this.input.name = params.name;
this.input.value = this.getDate();
this.form.appendChild(this.input);
this.table = this.form.appendChild(document.createElement("table"));
this.table.classList.add("calendar");
this.check();
}
Calendar.prototype = {
"i18n" : {
"months" : ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"],
"days" : ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"]
},
"check" : function() {
if(this.ajxcheck)
this.ajax(this.ajxcheck, {
"success" : this.draw.bind(this),
"error" : function(e) { console.log(e); }
});
else
this.draw([]);
},
"draw" : function(check) {
var row, cell, firstDay, countDays, init = true, residue = 0, currentDay, thisDay;
while (this.table.firstChild) this.table.removeChild(this.table.lastChild);
row = this.table.appendChild(document.createElement("tr"));
cell = row.appendChild(document.createElement("th"));
cell.classList.add("action");
cell.innerHTML = "<";
cell.addEventListener("click", this.change_month.bind(this, -1));
cell = row.appendChild(document.createElement("th"));
cell.colSpan = 5;
cell.innerHTML = this.i18n.months[this.date.getMonth()] + " " + this.date.getFullYear();
cell.addEventListener("click", this.change_month.bind(this, -1));
cell = row.appendChild(document.createElement("th"));
cell.classList.add("action");
cell.innerHTML = ">";
cell.addEventListener("click", this.change_month.bind(this, 1));
row = this.table.appendChild(document.createElement("tr"));
for(var day=0; day<this.i18n.days.length; day++)
row.appendChild(document.createElement("th")).innerHTML = this.i18n.days[day].substring(0, 3);
firstDay = (new Date(this.date.getFullYear(), this.date.getMonth(), 1)).getDay();
countDays = (new Date(this.date.getFullYear(), this.date.getMonth() + 1, 0)).getDate();
for(currentDay=0; currentDay < countDays+firstDay; currentDay++) {
if(currentDay % 7 == 0) {
row = this.table.appendChild(document.createElement("tr"));
residue = 7;
}
if(init) {
if(currentDay < firstDay)
row.appendChild(document.createElement("td"));
else
init = false;
}
if(!init) {
thisDay = (currentDay - firstDay + 1);
cell = row.appendChild(document.createElement("td"));
cell.innerHTML = thisDay;
if(!check[currentDay - firstDay]) {
cell.classList.add("valid");
cell.addEventListener("click", function(cell, thisDay) {
var active = this.table.getElementsByClassName("active");
while(active.length) active[0].classList.remove("active");
cell.classList.add("active");
this.actives.push(cell);
this.date.setDate(thisDay);
this.input.value = this.getDate();
if(this.on && this.on.change)
this.on.change(this.getDate());
}.bind(this, cell, thisDay));
if(this.date.getDate() == thisDay) {
cell.classList.add("active");
this.actives.push(cell);
}
} else {
cell.classList.add("invalid");
}
}
residue--;
}
for(currentDay=0; currentDay<residue; currentDay++)
row.appendChild(document.createElement("td"));
},
"change_month" : function(inc) {
var newMonth = this.date.getMonth() + inc,
newYear = this.date.getFullYear() + (newMonth < 0 ? -1 : (newMonth > 11 ? 1 : 0));
newMonth = (newMonth < 0 ? 11 : (newMonth > 11 ? 0 : newMonth));
this.date = (new Date(newYear, newMonth, this.date.getDate()));
this.input.value = this.getDate();
this.check();
if(this.on && this.on.change)
this.on.change(this.getDate());
},
"getDate" : function() {
var month = (this.date.getMonth() + 1),
day = this.date.getDate();
return this.date.getFullYear() + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ? "0" : "") + day;
},
"ajax" : function(url, callbacks) {
var request = new XMLHttpRequest();
request.open("post", url);
request.onreadystatechange = function(callbacks) {
if(this.readyState === 4) {
var response = this.responseText,
success = false;
if(this.status == 200) {
try {
response = JSON.parse(response);
success = true;
} catch(e) { }
}
if(success)
{
if(callbacks.success)
callbacks.success(response);
}
else if(callbacks.error)
callbacks.error({"response" : response});
}
}.bind(request, callbacks);
request.send(new FormData(this.form));
}
}