Well I have a somewhat lengthy Javascript that I’ve written myself, with one component that I borrowed from somewhere else online, and all is well except for one thing: at the top of the script I define a variable named “glitch” to 0. Throughout the script, as it checks the form, if an error occurs it sets the variable glitch to 1 and throws an alert. This works. At the very bottom of the script I want to know if a glitch was found so that I can or cannot set a cookie, and it doesn’t work.
In fact, unless I comment out portions of the code I can’t get the if statement OR the alert(glitch) above it at the very bottom to work. It just ignores it. Why does this happen? Here’s my code. I’m sorry, this is probably a beast to look at. It seems that the IF statement at the bottom is totally neglected. It never returns “true” but the form can still be submitted so long as all the fields are filled out properly.
[CODE]<script language=”JavaScript” type=”text/javascript”>
function checkForm() {
var glitch = 0;
var div1 = document.getElementById(“divStartTime”);
var div2 = document.getElementById(“divEndTime”);
var div3 = document.getElementById(“divFirstName”);
var div4 = document.getElementById(“divLastName”);
var div5 = document.getElementById(“divPhoneNumber”);
var div6 = document.getElementById(“divEmailAddress”);
div1.style.color = “#000000”;
div2.style.color = “#000000”;
div3.style.color = “#000000”;
div4.style.color = “#000000”;
div5.style.color = “#000000”;
div6.style.color = “#000000”;
var thisLocation = <cfoutput>#id#</cfoutput>;
var timeAvailable = 1;
var thisDay = “<cfoutput>#DateFormat(today, “DDDD”)#</cfoutput>”;
var startTime = document.forms[“calendarmain”][“startTime”].value;
var endTime = document.forms[“calendarmain”][“endTime”].value;
if (eval(startTime) > 0 && eval(endTime) > 0) {
if (eval(startTime) >= eval(endTime)) {
alert(“The Start time must be earlier than the End time.”);
div1.style.color = “red”;
div2.style.color = “red”;
glitch = 1;
return false;
}
} else {
if (! eval(startTime) && eval(endTime) > 0) {
div1.style.color = “red”;
alert(“You must choose a Start time for your lesson.”);
glitch = 1;
return false;
} else if (! eval(endTime) && eval(startTime) > 0) {
div2.style.color = “red”;
alert(“You must choose an End time for your lesson.”);
glitch = 1;
return false;
} else {
div1.style.color = “red”;
div2.style.color = “red”;
alert(“You must choose a value for both Start and End time of your lesson.”);
glitch = 1;
return false;
}
}
<cfoutput query=”getSchedule”>
<cfset xTime = xTime + 1>
var xTime#xTime#A = #starttime#;
var xTime#xTime#B = #endTime#;
</cfoutput>
<cfset xTime = xTime + 1>
<cfif xTime eq 1>
var xTime1A = 0;
var xTime1B = 0;
</cfif>
if (thisLocation == 2 && thisDay == “Thursday” || thisLocation == 1) {
for (x = 1; x < <cfoutput> #xTime#</cfoutput>; x++) {
if (eval(startTime) < eval(“xTime” + x + “A”) && eval(endTime) < eval(“xTime” + x + “A”) || eval(startTime) > eval(“xTime” + x + “B”) && eval(endTime) > eval(“xTime” + x + “B”) || eval(startTime) < eval(“xTime” + x + “A”) && eval(endTime) == eval(“xTime” + x + “A”) || eval(startTime) == eval(“xTime” + x + “B”) && eval(endTime) > eval(“xTime” + x + “B”)) {
timeAvailable = 1;
} else {
timeAvailable = 0;
alert(“Sorry, but the time you selected is already reserved. Please choose a time that is not blocked out in the schedule on the right.”);
glitch = 1;
return false;
break;
}
}
} else {
timeAvailble = 0;
alert(“Lessons in New York can only be booked on Thursdays.”);
glitch = 1;
return false;
}
var startValue = “<cfoutput>#DateFormat(today, “dd.mm.yyyy”)#</cfoutput>, ” + startTime.replace(/.25/g, “”).replace(/.5/g, “”).replace(/.75/g, “”) + “:00:00”;
var re = /[.:]|, /,
date1 = startValue.split(re).map(Number),
date2 = “<cfoutput>#DateFormat(Now(), “dd.mm.yyyy”)#</cfoutput>, <cfoutput>#TimeFormat(Now(), “HH”)#</cfoutput>:00:00″.split(re).map(Number);
var diffMs = Date.UTC( date1[2], date1[1] – 1, date1[0], date1[3], date1[4], date1[5] ) – Date.UTC( date2[2], date2[1] – 1, date2[0], date2[3], date2[4], date2[5] );
var hours = diffMs / (3600 * 1000);
if (hours < 24) {
alert(“You must book your lesson at least 24 hours in advance.”);
glitch = 1;
return false;
} else if (hours > 1080) {
alert(“Please call our school to book a lesson more than 6 weeks in advance. The phone number is (XXX)-XXX-XXXX. Thank you.”);
glitch = 1;
return false;
}
var nameFirst = document.forms[“calendarmain”][“firstName”].value;
nameFirst = nameFirst.replace(/s/g, “”)
if (! nameFirst.length) {
document.forms[“calendarmain”][“firstName”].value = “”;
div3.style.color = “red”;
alert(“Please enter your First Name.”);
glitch = 1;
return false;
}
var nameLast = document.forms[“calendarmain”][“lastName”].value;
nameLast = nameLast.replace(/s/g, “”);
if (! nameLast.length) {
document.forms[“calendarmain”][“lastName”].value = “”;
div4.style.color = “red”;
alert(“Please enter your Last Name.”);
glitch = 1;
return false;
}
var phone = document.forms[“calendarmain”][“phoneNumber”].value;
phone = phone.replace(/s/g, “”);
if (! phone.length) {
document.forms[“calendarmain”][“phoneNumber”].value = “”;
div5.style.color = “red”;
alert(“Please enter your Phone Number.”);
glitch = 1;
return false;
}
var email = document.forms[“calendarmain”][“emailAddress”].value;
var atpos = email.indexOf(“@”);
var point = email.lastIndexOf(“.”);
if (atpos < 1 || point < atpos + 2 || dotpos + 2 >= email.length) {
document.forms[“calendarmain”][“emailAddress”].value = “”;
div6.style.color = “red”;
alert(“Please enter your Email Address.”);
glitch = 1;
return false;
}
alert(glitch);
if (glitch) {
return false;
} else {
document.cookie = ‘JSTEST=1; path=/;’;
return true;
}
}
</script>
There’s a little bit of ColdFusion in there which is run server-side. Essentially though, the very last IF statement above is totally ignored, along with the alert(glitch) above it. However, the script, otherwise, performs perfectly and runs very smoothly.