Recently, I’ve started working with javascript since my group has decided we are going to web enable a bunch of our apps. To cut my teeth, I’ve been working on a simple application that will allow our users to submit their vacation requests via the web so that they can be e-mailed to our payroll admin. However, in order to create a dynamic field that will generate the appropriate amount of days, i wrote a javascript (based loosely on tips from Scott Good in his July 2003 Lotus Advisor article) to do this. unfortunately, it doesn’t work.
i believe my issues are due to the fact that i am not quote certain how to handle arrays in javascript. basically, i’ve tried to break up the code into two pieces–one piece that when given the month value determines how many days that month has and sets the field accordingly. the second bit of code actually generates the number list accordingly. It is called by the first bit of code once the first bit determines how long the month actually is.
here’s the code as contained in my JSHeader:
var doc = document.forms[0]; //set doc variable equal to current form
var today = new Date(); //get today’s date
function genList (num){
//this function generates a list based on the number passed to it
alert ("genList routine has begun");
var numlist = new Array(); //number list array container
for (i = 0; i<num; i++){ //loop through the numbers
numlist[i] = i++; //set current array position equal to position + 1
}
return numlist;
alert ("genList has concluded");
}
function setDayChoices (month) {
//this function will create a list of date choices based on month and year selection
alert ("The month value is " + month);
alert ("setDayChoicesFunction has begun");
var daylist = new Array (); //set daylist container for month choices
var year = doc.StDateYr.options.selectedIndex; //find out the current year selected
var leap = doc.StDateYr.options[year].value; //find the year value
switch (month){
case "1"||"3"||"5"||"7"||"8"||"10"||"12": //if a 31 day month
daylist = genList(31); //call genList function, set it equal to daylist array
break;
case "4"||"6"||"9"||"11": //if 30 day month
daylist = genList(30); //call genList function
break;
case "2" && ((leap%4!=0) || ((leap%100==0) && (leap%400!=0))): //if a non leap year feb
daylist = genList(28); //call genList function
break;
case "2" && ((leap%4)==0) : //if a leap year feb
daylist = genList(29); //call genList function
break;
default: //default to 31 days
daylist = genList(31);
break;
}
switch (this.Name){ //dependent on start date or end date, set the appropriate field with day listing
case "StDateMo":
for (i = 0;i<daylist.length;i++){
doc.StDateDay.options[i] = daylist[i];
}
break;
case "EndDateMo":
for (i = 0;i<daylist.length;i++){
doc.EndDateDay.options[i] = daylist[i];
}
break;
}
alert ("setDayChoices has ended");
}
I call setDayChoices in one of two places–first, in the onLoad event of the form and secondly in the onChange event of the field that contains the month (“StDateMo”) I know for a fact that it at least calls setDayChoices in both events, because i received the alert–however, the genList function has never executed.
can someone show me where I am going wrong here? I’m almost certain it is due to some fundamental misunderstanding i have of the language, but i am too ignorant to see it myself.
thanks.
brandt