Forum Moderators: open
I have been trawling the internet for answers to my 'else if' problem but can't find anything.
A friend of mine made a little quick quote field for my website last year but it isn't working properly and being all but completely html/ javascript illiterate I cannot see why. It did work for a little while which just adds to my confusion. If anyone would be kind enough to have a quick look at the javascript and see if there is a glaring error I would be very grateful.
The script is as follows:
<SCRIPT language="JavaScript">
<!--
function getQuote(form){
// These are the lists of prices in pounds for each of the seasons
var low=["10","12","15","18","22","24","28","28","28","35","40","43","46","49","49","49","52","55","58","61","64"]
var high=["10","20","25","25","30","35","39","39","45","50","55","60","65","69","69","80","85","90","95","95","100"]
var medium=["10","12","15","18","22","24","28","28","28","35","40","43","46","49","49","49","52","55","58","61","64"]
// This is the text that precedes the quote
var text="Your quote is: £"
var day = form.ddfrom.value;
if (day.length == 1) day = "0" + day
var month = form.mmfrom.value;
if (month.length == 1) month = "0" + month
var date = parseInt(month + day);
var days = parseInt(form.NoDays.value);
// the dates here are in the format MMDD. If you need to change the date ranges, eg 14th Jan becomes 3rd Feb, then 0114 becomes 0203
if (date < 0501)
{
alert(text + low[(days-1)]);
}
else if (date < 1031)
{
alert(text + high[(days-1)]);
}
else if (date < 1231)
{
alert(text + medium[(days-1)]);
{
else
{
alert("Please check the date range and try again. The date format is DD/MM");
}
}
//-->
</SCRIPT>
The script for the corresponding form, just in case that is the problem is as follows:
<fieldset>
<legend>
<span class="legendsub">Quick Quote </span></legend>
<form name="QQuote" id="chooseDateForm" action=""><center>
<p>Parking From:<br /><input type="Text" name="ddfrom" id="doody" maxlength="2" size="1">/<input type="Text" name="mmfrom" id="doody" maxlength="2" size="1"><br />
<b>Valid date format:</b> DD/MM<br>
Number of Days:<br /><select name="NoDays">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
</select> <br /><br />
<input type="button" value="Get Quote" onclick="getQuote(this.form)">
</center>
</form>
</fieldset>
Any help that anyone coulkd give would be much appreciated.
Alex
I have discovered that the javascript seems to have issues with number 8 and number 9!
For example if I put in the date as the 8th May or 5th August (5/8) it gives a low season price, even though they are both high season date ranges. Same if the date has a 9 anywhere in it. I have racked my brains and allmy limited know how and the mind boggles!
Note that whenever you convert back and forth between and compare dates and strings, you're asking for trouble. It's almost always better to convert to actual dates and do true date comparisons.
Thank you so much for taking the time to help!
I have tried out your suggestion and changed 'date' to 'arr' but still just doesn't like 8's and 9's. If the date has an 8 or 9 anywhere parseint allocates a really low integer (hence going to low season automatically)... I'm using your previous suggestion to check for changes but still no joy.
I will keep you updated though and let you know if I make any breakthroughs.
Thank you again!
[edited by: Fotiman at 8:38 pm (utc) on Feb. 26, 2009]