W
WindAndWaves
Hi Gurus
Below you can find a function and the accompagnying html. I am keen to make
it work, but it seems to be riddled with errors. I basically can not get it
to work.
The idea is that the user completes the startdate field and that from there
on, the arrival dates and departure dates for the properties are calculated,
using the number of nights and the number of days (gap) between each
property. While the number of nights and the gap are entered as 1 and 0
respectively, the user should be able (once the function is working) to
change those values and the arrival and departure date should be
recalculated.
Any hints greatly appreciated (this is one of my first javascripts).
TIA
- Nicolaas
//this function fills a form (f), WHERE i is the section number (e.g. 0, 1,
2 or 3)
//each section of the form has the following fields:
//arrival: <INPUT CLASS="sg" ID="ai" NAME="ai">
//nights: <INPUT CLASS="xs" VALUE="1" ID="ni" NAME="ni">
//departure: <INPUT CLASS="sg" ID="di" NAME="di">
//gap between this and next stay: <INPUT CLASS="xs" VALUE="1" ID="gi"
NAME="gi">
//hidden actual arrival date: <INPUT TYPE="hidden" ID="hi" NAME="hi">
//fill form with data, f = Form, s = the number of sections (see above, e.g.
3)
function main(f, n) {
sd = new Date;
sd = f.startdate;
// need to put a check here to make sure we have a valid start date
return go(f, sd, n);
// for (i = 0; i < f.length; i++) {
// }
}
// set the elements in the form
function go(f, sd, n) {
for (i = 0; i < n; i++) { //start loop
if (i==0) { //first loop
writeD(false,f, 'h0', sd); //write hidden field (h0) with the date
} else { //other loops
writeD(false,f, 'h' + i, adf(getD(f, 'h' + (i-1)), getN(f, 'n' (i-1)) +
getN(f, 'g' (i-1)))); //set hidden field (previous hidden field + the number
of nights + the number of nights between (gap (g))
}
writeD(true, f, 'a' + i, getD(f, 'h' + i)); //write arrival date
writeD(true, f, 'd' + i, adf(getD(f, 'h' + i), getN(f, 'n' + i))); //write
departure date
return true;
}
}
//returns the number of seconds (number)
function pd(d) {
x = new Date(d);
return Date.parse(x); //parse provides the number of milli seconds since
1970
}
//add values to a date; sd = start date, ad = number of days to add
function adf(sd, ad){
var d = 0;
d = pd(sd) + (ad*86400000); //change number of days into number of
milliseconds
return new Date(d);
}
//write element with a date
function writeD(ft, f, el,d){
var bRef = null;
if (document.getElementById) {
bRef = document.getElementById(el);
} else {
bRef = document.all[el];
}
if (ft == true) {
d = fmt(d);
}
bRef.value = d;
}
// get date from an element
function getD (f,el){
var bRef = null;
var nv = new Date(); //nv = new value
if (document.getElementById) {
bRef = document.getElementById(el);
} else {
bRef = document.all[el];
}
nv = bRef.value;
return Date(nv); //change it into a date
}
// get number from an element
function getN (f,el){
var bRef = null;
var nv = null; //nv = new value
if (document.getElementById) {
bRef = document.getElementById(el);
} else {
bRef = document.all[el];
}
nv = bRef.value;
return nv;
}
// format DD-MMM-YYYY
function fmt(d) {
d = Date(d);
return (d.getDate() < 10 ? "0" : "") + d.getDate() + "-" +
(["JAN","FEB","MAR","APR",
"MAY","JUN","JUL","AUG",
"SEP","OCT","NOV","DEC"])[d.getMonth()] + "-" +
d.getFullYear();
}
The relevant HMTL is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>test me</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" NAME="basket" ID="basket">
<INPUT TYPE="button" VALUE="calculate" ONCLICK="return main(this.form,
3);" ID="calculate" NAME="calculate">
<INPUT TYPE="reset" VALUE="reset" ID="Reset1" NAME="Reset1">
property1
arrival<INPUT CLASS="sg" ID="a0" NAME="a0">
nights<INPUT CLASS="xs" VALUE="1" ID="n0" NAME="n0">
departure<INPUT CLASS="sg" ID="d0" NAME="d0">
gap<INPUT CLASS="xs" VALUE="0" ID="g0" NAME="g0">
<BR><INPUT TYPE="hidden" ID="h0" NAME="h0">
property 2
arrival<INPUT CLASS="sg" ID="a1" NAME="a1">
nights<INPUT CLASS="xs" VALUE="1" ID="n1" NAME="n1">
departure<INPUT CLASS="sg" ID="d1" NAME="d1">
gap<INPUT CLASS="xs" VALUE="0" ID="g1" NAME="g1">
<BR><INPUT TYPE="hidden" ID="h1" NAME="h1">
property 3
arrival<INPUT CLASS="sg" ID="a2" NAME="a2">
nights<INPUT CLASS="xs" VALUE="1" ID="n2" NAME="n2">
departure<INPUT CLASS="sg" ID="d2" NAME="d2">
gap<INPUT CLASS="xs" VALUE="0" ID="g2" NAME="g2">
<BR><INPUT TYPE="hidden" ID="h2" NAME="h2">
property 4
arrival<INPUT CLASS="sg" ID="a3" NAME="a3">
nights<INPUT CLASS="xs" VALUE="1" ID="n3" NAME="n3">
departure<INPUT CLASS="sg" ID="d3" NAME="d3">
gap<INPUT CLASS="xs" VALUE="0" ID="g3" NAME="g3">
<BR><INPUT TYPE="hidden" ID="h3" NAME="h3">
</BODY>
</HTML>
Below you can find a function and the accompagnying html. I am keen to make
it work, but it seems to be riddled with errors. I basically can not get it
to work.
The idea is that the user completes the startdate field and that from there
on, the arrival dates and departure dates for the properties are calculated,
using the number of nights and the number of days (gap) between each
property. While the number of nights and the gap are entered as 1 and 0
respectively, the user should be able (once the function is working) to
change those values and the arrival and departure date should be
recalculated.
Any hints greatly appreciated (this is one of my first javascripts).
TIA
- Nicolaas
//this function fills a form (f), WHERE i is the section number (e.g. 0, 1,
2 or 3)
//each section of the form has the following fields:
//arrival: <INPUT CLASS="sg" ID="ai" NAME="ai">
//nights: <INPUT CLASS="xs" VALUE="1" ID="ni" NAME="ni">
//departure: <INPUT CLASS="sg" ID="di" NAME="di">
//gap between this and next stay: <INPUT CLASS="xs" VALUE="1" ID="gi"
NAME="gi">
//hidden actual arrival date: <INPUT TYPE="hidden" ID="hi" NAME="hi">
//fill form with data, f = Form, s = the number of sections (see above, e.g.
3)
function main(f, n) {
sd = new Date;
sd = f.startdate;
// need to put a check here to make sure we have a valid start date
return go(f, sd, n);
// for (i = 0; i < f.length; i++) {
// }
}
// set the elements in the form
function go(f, sd, n) {
for (i = 0; i < n; i++) { //start loop
if (i==0) { //first loop
writeD(false,f, 'h0', sd); //write hidden field (h0) with the date
} else { //other loops
writeD(false,f, 'h' + i, adf(getD(f, 'h' + (i-1)), getN(f, 'n' (i-1)) +
getN(f, 'g' (i-1)))); //set hidden field (previous hidden field + the number
of nights + the number of nights between (gap (g))
}
writeD(true, f, 'a' + i, getD(f, 'h' + i)); //write arrival date
writeD(true, f, 'd' + i, adf(getD(f, 'h' + i), getN(f, 'n' + i))); //write
departure date
return true;
}
}
//returns the number of seconds (number)
function pd(d) {
x = new Date(d);
return Date.parse(x); //parse provides the number of milli seconds since
1970
}
//add values to a date; sd = start date, ad = number of days to add
function adf(sd, ad){
var d = 0;
d = pd(sd) + (ad*86400000); //change number of days into number of
milliseconds
return new Date(d);
}
//write element with a date
function writeD(ft, f, el,d){
var bRef = null;
if (document.getElementById) {
bRef = document.getElementById(el);
} else {
bRef = document.all[el];
}
if (ft == true) {
d = fmt(d);
}
bRef.value = d;
}
// get date from an element
function getD (f,el){
var bRef = null;
var nv = new Date(); //nv = new value
if (document.getElementById) {
bRef = document.getElementById(el);
} else {
bRef = document.all[el];
}
nv = bRef.value;
return Date(nv); //change it into a date
}
// get number from an element
function getN (f,el){
var bRef = null;
var nv = null; //nv = new value
if (document.getElementById) {
bRef = document.getElementById(el);
} else {
bRef = document.all[el];
}
nv = bRef.value;
return nv;
}
// format DD-MMM-YYYY
function fmt(d) {
d = Date(d);
return (d.getDate() < 10 ? "0" : "") + d.getDate() + "-" +
(["JAN","FEB","MAR","APR",
"MAY","JUN","JUL","AUG",
"SEP","OCT","NOV","DEC"])[d.getMonth()] + "-" +
d.getFullYear();
}
The relevant HMTL is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>test me</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" NAME="basket" ID="basket">
<INPUT TYPE="button" VALUE="calculate" ONCLICK="return main(this.form,
3);" ID="calculate" NAME="calculate">
<INPUT TYPE="reset" VALUE="reset" ID="Reset1" NAME="Reset1">
property1
arrival<INPUT CLASS="sg" ID="a0" NAME="a0">
nights<INPUT CLASS="xs" VALUE="1" ID="n0" NAME="n0">
departure<INPUT CLASS="sg" ID="d0" NAME="d0">
gap<INPUT CLASS="xs" VALUE="0" ID="g0" NAME="g0">
<BR><INPUT TYPE="hidden" ID="h0" NAME="h0">
property 2
arrival<INPUT CLASS="sg" ID="a1" NAME="a1">
nights<INPUT CLASS="xs" VALUE="1" ID="n1" NAME="n1">
departure<INPUT CLASS="sg" ID="d1" NAME="d1">
gap<INPUT CLASS="xs" VALUE="0" ID="g1" NAME="g1">
<BR><INPUT TYPE="hidden" ID="h1" NAME="h1">
property 3
arrival<INPUT CLASS="sg" ID="a2" NAME="a2">
nights<INPUT CLASS="xs" VALUE="1" ID="n2" NAME="n2">
departure<INPUT CLASS="sg" ID="d2" NAME="d2">
gap<INPUT CLASS="xs" VALUE="0" ID="g2" NAME="g2">
<BR><INPUT TYPE="hidden" ID="h2" NAME="h2">
property 4
arrival<INPUT CLASS="sg" ID="a3" NAME="a3">
nights<INPUT CLASS="xs" VALUE="1" ID="n3" NAME="n3">
departure<INPUT CLASS="sg" ID="d3" NAME="d3">
gap<INPUT CLASS="xs" VALUE="0" ID="g3" NAME="g3">
<BR><INPUT TYPE="hidden" ID="h3" NAME="h3">
</BODY>
</HTML>