So, basically I'm building a rental form where you have three basic dropdowns for Month, Day, Year for a "rent from" and then another three for "rent till."
That's easy enough and I've even got decent leap year support and such. The issue comes with generating the "rent till" based on the "rent from." I'd like the "rent till" to show only the next five days in a dropdown afterwords and I'm having serious difficulty working out logic to do it efficiently.
Also, the range is variable, so I'm trying to work a function in that takes a 'length of rental' parameter to effect the changes. The code also uses reference points. So that the pick up dropdowns are "pick_up_day", "pick_up_month" etc. This is to try to make the code portable to multiple projects with consistently ID'd HTML elements.
Here's my basic logic and what I've got so far:
This is the code that sets up the from-dates and such.
function populate_dates(identifier) {
"use strict";
var i, currentday, node, textbox, maxdays;
while (document.getElementById(identifier + "days").hasChildNodes()) {
document.getElementById(identifier + "days").removeChild(document.getElementById(identifier + "days").firstChild);
}
currentday = new Date();
switch (parseInt(document.getElementById(identifier + "month").value, 10)) {
case 2:
if (new Date(currentday.getFullYear(), 1, 29).getDate() === 29) {
maxdays = 29;
} else {
maxdays = 28;
}
break;
case 9:
case 4:
case 6:
case 11:
maxdays = 30;
break;
default:
maxdays = 31;
break;
}
for (i = 1; i <= maxdays; i = i + 1) {
node = document.createElement("option");
textbox = document.createTextNode(i);
node.value = i;
node.appendChild(textbox);
document.getElementById(identifier + "days").appendChild(node);
}
}
window.onload = function() {
"use strict";
var currentday, i, node, textbox;
currentday = new Date();
for (i = 0; i <= 1; i = i + 1) {
node = document.createElement("option");
textbox = document.createTextNode(currentday.getFullYear() + i);
node.value = currentday.getFullYear() + i;
node.appendChild(textbox);
document.getElementById("pick_up_year").appendChild(node);
}
document.getElementById("pick_up_month").onchange = function () {
populate_dates("pick_up_");
};
populate_dates("pick_up_");
};
Now for the logic regarding the 'till' date I have worked out some pseudocode like this, but it seems overly complex for such a simple task:
function a(referencer, ref1, ref2, rentlength) {
//Make datetime object out of ref1's current date;
datetime.setDays(datetime.getDate() + rentlength)
for (i = 0; i <= rentlength; i = i + 1) {
datetime.setDays(datetime.getDate() + i);
outmatrix[outmatrix.length] = [getDates, getMonth, getYear];
}
outmatrix.removeduplicates;
date_generator(ref2, outmatrix)
}
function date_generator(referencer, inputmatrix) {
Clean up referencer
Loop through, create nodes based on array[y+1][x];
}
Is there a better way to do this that I'm just not seeing? What is a good way to turn a Datetime object into three dropdowns while removing duplicates? Is there a better way to handle this than the way I am currently?