function setDays(month, day, year) {
if(month.constructor != Number) {
var date = month.options[month.selectedIndex].value;
var month = getMonth(date) - 1;
var year = getYear(date);
}
Date.DAYSINMONTH[1] = (year && !isLeapYear(year)) ? 28 : 29;
var index = day.length;
if(index == Date.DAYSINMONTH[month]) return;
else if(index < Date.DAYSINMONTH[month]) {
for(var i = index; i < Date.DAYSINMONTH[month]; i++) {
day[i] = new Option(i + 1);
}
}
else if(index > Date.DAYSINMONTH[month]) {
for(var i = index; i >= Date.DAYSINMONTH[month]; i--) {
day[i] = null;
}
}
if(day.selectedIndex > index) day.selectedIndex = 0;
}
function getDateArray(length) {
var arr = [];
var date = new Date();
var startYear = date.getFullYear();
var startMonth = date.getMonth();
for(var i = 0; i < 12; i++) {
var num = ((startMonth + i) >= Date.MONTHS.length) ? (startMonth + i) - Date.MONTHS.length : startMonth + i;
var year = ((startMonth + i) >= Date.MONTHS.length) ? startYear + 1 : startYear;
var month = (length) ? Date.MONTHS[num].substring(0, 3) : Date.MONTHS[num];
var value = String(num + 1);
if(value.length == 1) value = "0" + value;
arr[i] = [year + value, month + " " + year];
}
return arr;
}
function getMonth(date) {
return Number(date.substring(4, 6));
}
function getYear(date) {
return Number(date.substring(0, 4));
}
function isLeapYear(year) {
var d = new Date();
d.setYear(year);
d.setMonth(1);
d.setDate(29);
return (d.getMonth() == 1);
}
function isValidDate(type, startMonth, startDay, endMonth, endDay) {
var msg, start, end;
var now = new Date();
var valid = function(a, b, msg) {
if(a.getTime() > b.getTime()) {
alert(msg);
return false;
}
return true;
}
if(type == "creditcard") {
msg = "Your card valid from date must be before your card valid to date.";
start = getDate(startDay.options[startDay.selectedIndex].text, startMonth.selectedIndex, 0);
end = getDate(endDay.options[endDay.selectedIndex].text, endMonth.selectedIndex);
if(!valid(start, now, "Your card valid from date must be before the current date.")) return false;
now.setMonth(now.getMonth() - 1);
if(!valid(now, end, "Your card valid to date must be after the current date.")) return false;
}
else if(type == "departure") {
var sm = startMonth.options[startMonth.selectedIndex].value;
var month = getMonth(sm) - 1;
var sDay = (startDay.type == "select-one") ? startDay.options[startDay.selectedIndex].text : startDay.value;
if(sDay > Date.DAYSINMONTH[month]) {
alert("There are only " + Date.DAYSINMONTH[month] + " days in " + Date.MONTHS[month]);
startDay.value = 1;
return false;
}
msg = "Your departure date must be before your return date.";
start = getDate(getYear(sm), month, sDay);
if(!valid(now, start, "Your departure date must be after the current date.")) return false;
if(!endMonth) return true;
var em = endMonth.options[endMonth.selectedIndex].value;
month = getMonth(em) - 1;
var eDay = (endDay.type == "select-one") ? endDay.options[endDay.selectedIndex].text : endDay.value;
if(eDay > Date.DAYSINMONTH[month]) {
alert("There are only " + Date.DAYSINMONTH[month] + " days in " + Date.MONTHS[month]);
endDay.value = 1;
return false;
}
end = getDate(getYear(em), month, eDay);
}
else if (type == "switch") {
end = getDate(endDay.options[endDay.selectedIndex].text, endMonth.selectedIndex);
now.setMonth(now.getMonth() - 1);
if(!valid(now, end, "Your card valid to date must be after the current date.")) return false;
if (startDay.options[startDay.selectedIndex].text == "----" || startMonth.selectedIndex.text == "--") {
start = getDate(startDay.options[1].text, startMonth.options[1], 0) }
else {
start = getDate(startDay.options[startDay.selectedIndex].text, startMonth.selectedIndex, 0);
if(!valid(start, now, "Your card valid from date must be before the current date.")) return false;
}
}
return valid(start, end, msg);
}
function dateCompare(low, high, msg, field) {
if(low.getTime() > high.getTime()) {
alert(msg);
field.value = "";
field.focus();
}
}
function getDate(year, month, day) {
var d = new Date();
d.setDate(1);
if(typeof(year) != "undefined") d.setYear(year);
if(typeof(month) != "undefined") d.setMonth(month);
if(typeof(day) != "undefined") d.setDate(day);
return d;
}
if(!Date.MONTHS) {
Date.MONTHS = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
}
Date.DAYSINMONTH = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
