WindAndWaves wrote:
[...]
Thank you, that makes more sense now.
- Nicolaas
I posted a reply to Dr. J that seems to have never made it to the page.
It included some updates, below is just the last script, including most
of his recommendations.
I've dropped a lot of comments to reduce the size.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>Visit Properties</TITLE>
<script type="text/javascript">
// Make month and day arrays global
var monthsA = ['Jan','Feb','Mar','Apr',
'May','Jun','Jul','Aug',
'Sep','Oct','Nov','Dec'];
var daysA = ['Sun','Mon','Tue','Wed',
'Thu','Fri','Sat'];
function formatDate(d) {
var s = daysA[d.getDay()]
+ ' ' + d.getDate()
+ ' ' + monthsA[d.getMonth()]
+ ' ' + d.getFullYear();
return s;
}
function calc(f){
// Setup variables - msg is for debug
var ele, x, n, numNights, gapNights,
depDate, nField, gField;
var digitErr = 'You must enter a whole number from zero to 99';
// Substitute doc.all function to allow for older IEs
if (document.all && !document.getElementById) {
document.getElementById = function(id) {
return document.all[id] }
}
ele = f.elements;
// Check month adjustment, keep aligned with input
var aa = f.elements['startdate'].value.split('-');
// var aa = f.elements['startdate'].value.split(/\D+/);
var sd = new Date(aa[2],aa[1]-1,aa[0]);
for (var i=0; i<ele.length; i++) {
if (ele
.name) {
if (ele.name.split('-')[0] == 'v') {
// Get the element id extension
n = ele.name.split('-')[1];
if (ele.checked) {
document.getElementById('a-' + n).innerHTML
= formatDate(sd);
nField = f.elements['n-' + n];
if ( !(numNights = nField.value)
|| !checkDigit2(numNights)) {
alert(digitErr);
// Blank field
nField.value = '';
// Put focus on it if you like
nField.focus();
return false;
}
// Add the number of nights to get depature date
sd.setDate(sd.getDate() + +numNights);
// Write dep date to form
document.getElementById('d-' + n).innerHTML
= formatDate(sd);
// Get gap nights
gField = f.elements['g-' + n];
if ( gField
&& !(gapNights = gField.value)
|| !checkDigit(gapNights)) {
alert(digitErr);
gField.value = '';
gField.focus();
return false;
}
sd.setDate(sd.getDate() + +gapNights);
} else {
document.getElementById('a-' + n).innerHTML = '---';
f.elements['n-' + n].value = '';
document.getElementById('d-' + n).innerHTML = '---';
if (f.elements['g-' + n])
f.elements['g-' + n].value = '';
}
}
}
}
}
function checkDigit(x) {
var dayRegX = /^\d{1,2}$/;
// var dayRegX = /^(0|[1-9])\d*$/
// It's easier to check 1/2 digit fields with a RegExp -
// OK = /^\d?\d$/.test(field)
// and if OK it can safely be converted to Number with a unary +.
if (!dayRegX.test(x)) {
return false;
}
return true;
}
function checkDigit2(x) {
return x == parseInt(x,10) && x == parseFloat(x);
}
</script>
<style type="text/css">
body, th, td {font-family: sans-serif; font-size: 90%;}
th {background-color: #dfdfdf; padding: 0 8 0 8;}
td {text-align: center;}
..sg {width: 10em;}
..xs {width: 2em;}
..hint {color: #333366; font-weight: normal; font-size: 90%;}
..dateP, .dateQ, dateO
{position: absolute; left:0px; top:0px; width: 100px; height: 100px;
background-color: blue; border: thin solid red; z-index: 100;}
..dateQ
{background-color: red; left: 0px; top: 0px; z-index: 50;}
..dateO
{position: relative; display: inline;}
</style>
</HEAD>
<BODY>
<FORM METHOD="POST" NAME="basket" ID="basket" action="">
<!-- This field is just to provide a start date -->
<label for="startdate">Start Date
<input type="text" value="05-12-2004" name="startdate"></label>
<table border="0" cellspacing="0">
<thead>
<tr>
<th>Property Name</th>
<th style="width: 10em;">Will you visit<br>this
property?</th>
<th style="width: 12em;">Date of arrival<br><span
class="hint">after 1pm</span></th>
<th>How many nights<br>you will stay?</th>
<th style="width: 12em;">Date of departure<br><span
class="hint">before 10am</span></th>
<th>Nights gap to<br>next property</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">
<INPUT TYPE="reset" VALUE="reset" onclick="this.blur();"
ID="Reset1" NAME="Reset1"><br>
</td>
<td colspan="3">
<INPUT TYPE="button" VALUE="calculate" ONCLICK="
this.blur();
return calc(this.form);
"
ID="calculate" NAME="calculate" tabindex="8">
</td>
</tr>
</tfoot>
<tbody>
<tr>
<td>property 1</td>
<td><input type="checkbox" id="v-1" name="v-1" checked></td>
<td><span class="sg" ID="a-1">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="1" ID="n-1" NAME="n-1"
tabindex="1"></td>
<td><span class="sg" ID="d-1">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="0" ID="g-1" NAME="g-1"
tabindex="2"></td>
</tr>
<tr>
<td>property 2</td>
<td><input type="checkbox" id="v-2" name="v-2" checked></td>
<td><span class="sg" ID="a-2">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="1" ID="n-2" NAME="n-2"
tabindex="3"></td>
<td><span class="sg" ID="d-2">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="0" ID="g-2" NAME="g-2"
tabindex="4"></td>
</tr>
<tr>
<td>property 3</td>
<td><input type="checkbox" id="v-3" name="v-3" checked></td>
<td><span class="sg" ID="a-3">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="1" ID="n-3" NAME="n-3"
tabindex="5"></td>
<td><span class="sg" ID="d-3">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="0" ID="g-3" NAME="g-3"
tabindex="6"></td>
</tr>
<tr>
<td>property 4</td>
<td><input type="checkbox" id="v-4" name="v-4" checked></td>
<td><span class="sg" ID="a-4">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="1" ID="n-4" NAME="n-4"
tabindex="7"></td>
<td><span class="sg" ID="d-4">---</span></td>
<td><INPUT type="text" CLASS="xs" VALUE="0" ID="g-4" NAME="g-4"
style="display: none;"></td>
</tr>
</tbody>
</table>
</form>
</BODY>
</HTML>