// Is it a valid email address? // Assume valid if got the @ // in it function isEmailAddr(email) { var result = false; var theStr = new String(email); var index = theStr.indexOf("@"); if (index > 0) { var pindex = theStr.indexOf(".",index); if ((pindex > index+1) && (theStr.length > pindex+1)) result = true; } return result; } // Return true if the required field is // not empty function validRequired(formField,fieldLabel) { var result = true; if (formField.value == "") { alert('Please enter a value for the "' + fieldLabel +'" field.'); formField.focus(); result = false; } return result; } // Make sure that a valid selection has been // made, one without an empty value function validSelection(formField,fieldLabel) { var result = true; if (formField.value == "") { alert('Please choose a value for the "' + fieldLabel +'" field.'); result = false; } return result; } // Returns true if there are all digits in the string function allDigits(str) { return inValidCharSet(str,"0123456789"); } function inValidCharSet(str,charset) { var result = true; // Note: doesn't use regular expressions to avoid early Mac browser bugs for (var i=0;i max){ alert('Please enter a whole number no more than '+max+' for the "' + fieldLabel +'" field.'); formField.focus(); result = false; } } return result; } function validDate(formField,fieldLabel,required) { var result = true; if (required && !validRequired(formField,fieldLabel)) result = false; if (result) { var elems = formField.value.split("/"); result = (elems.length == 3); // should be three components if (result) { var month = parseInt(elems[0]); var day = parseInt(elems[1]); var year = parseInt(elems[2]); result = allDigits(elems[0]) && (month > 0) && (month < 13) && allDigits(elems[1]) && (day > 0) && (day < 32) && allDigits(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4)); } if (!result) { alert('Please enter a date in the format MM/DD/YYYY for the "' + fieldLabel +'" field.'); formField.focus(); } } return result; } function populateHiddenFields(theForm){ //alert("populating hiddenFields"); if (elementExistsInForm(theForm, "scent")){ theForm.scentVal.value = theForm.scent.options[theForm.scent.selectedIndex].text; } if (elementExistsInForm(theForm, "colour")){ theForm.colourVal.value = theForm.colour.options[theForm.colour.selectedIndex].text; } if (elementExistsInForm(theForm, "design")){ theForm.designVal.value = theForm.design.options[theForm.design.selectedIndex].text; } if (elementExistsInForm(theForm,"contents")){ var contents = ""; if (elementExistsInForm(theForm,"description")){ contents += theForm.description.value; contents += ". "; } var theElements = theForm.elements; for(var j = 0; j < theElements.length; j++) { if (theElements[j].name != "minQuantity" && theElements[j].name != "price" && theElements[j].name != "quantity" && theElements[j].name != "contents" && theElements[j].name != "productType" && theElements[j].name != "description" && theElements[j].name != "personalDetails"){ //alert(theElements[j].name+" is "+theElements[j].type); if (theElements[j].type == "select-one"){ //alert("select element"); contents += theElements[j].name; contents += " = " contents += theElements[j].options[theElements[j].selectedIndex].value; contents += ". " } if (theElements[j].type == "text"){ //alert("text element"); contents += theElements[j].name; contents += " = " contents += theElements[j].value; contents += ". " } } } theForm.contents.value = contents; } } function selectsHaveNonEmptyValueSelected(theForm){ var theElements = theForm.elements; for(var j = 0; j < theElements.length; j++) { if (theElements[j].type == "select-one"){ //alert("checking valid select-one "+theElements[j].options[theElements[j].selectedIndex]+" "+theElements[j].options[0].text); if (!validSelection(theElements[j].options[theElements[j].selectedIndex],theElements[j].options[0].text)){ return false; } } } return true; } function elementExistsInForm(theForm, elementName){ var theElements = theForm.elements; for(var j = 0; j < theElements.length; j++) { if (theElements[j].name == elementName){ return true; } } return false; } function validateForm(theForm) { //alert("In validateForm"); if (!cookiesAllowed()){ return false; } //alert("1"); if (elementExistsInForm(theForm,"scent")){ if (!validSelection(theForm.scent.options[theForm.scent.selectedIndex],"Scent")) return false; } //alert("2"); if (elementExistsInForm(theForm,"colour")){ if (!validSelection(theForm.colour.options[theForm.colour.selectedIndex],"Colour")) return false; } //alert("3"); if (elementExistsInForm(theForm,"design")){ if (!validSelection(theForm.design.options[theForm.design.selectedIndex],"Design")) return false; } //alert("4"); if (!selectsHaveNonEmptyValueSelected(theForm)){ return false; } //alert("5"); if (!(elementExistsInForm(theForm,"minQuantity"))){ //alert('minQuantity not in form'); if (!intInBound(theForm.quantity,"Quantity", true,1,1000)){ return false; } } else { //alert('minQuantity IN form'); //alert('minQuantity IN form'+theForm.minQuantity.value); if (!intInBound(theForm.quantity,"Quantity", true,theForm.minQuantity.value,1000)){ return false; } } //alert("In validateForm - about to populateHiddenFields"); populateHiddenFields(theForm); //alert("In validateForm - done populateHiddenFields"); return true; } function cookiesAllowed() { /* check for a cookie */ if (document.cookie == "") { /* if a cookie is not found - alert user - change cookieexists field value to false */ alert("COOKIES need to be enabled on your browser to use the shopping basket!"); return false; } else { return true; } } document.cookie = "This is a test cookie to ensure one exists"