// TITLE: Order Form with Checkbox and Radio Button Item Selection // An order form which utilizes checkboxes AND radio buttons for making a selection. The // checkbox only allows one quantity of any item to be ordered, while the radio button // only allows for one item within a defined group to be ordered. // Script updates the order total as the visitor makes his/her selections. // Also note that the order total can not be manually altered by the visitor. // Custom scripting by Paul DeBrino of Infinity Research and Development, Inc. // We also have a netscrape version for purchase, which works with both NS and IE. // For other scripts, or to place an order for customized scripts such as // on-line form scripting, please contact the owner via the contact form at infinity-rd.com // If you wish to use this script, kindly keep these comments and (C)opyright within the // source code. Permission granted for http://javascript.internet.com to publish this script. //Define function to manipulate the form total per item selected/deselected: function CheckChoice(whichbox) { with (whichbox.form) { { //If box was checked, accumulate the checkbox value as the form total, //Otherwise, reduce the form total by the checkbox value: if (whichbox.checked == false) { hiddentotal.value = eval(hiddentotal.value) - eval(whichbox.value); } else { hiddentotal.value = eval(hiddentotal.value) + eval(whichbox.value); } } //Ensure the total never goes negative (some browsers allow radiobutton to be deselected): if (hiddentotal.value < 0) { InitForm(); } //Now, return with formatted total: return(formatCurrency(hiddentotal.value)); } } //Define function to format a value as currency: function formatCurrency(num) { // Courtesy of http://www7.brinkster.com/cyanide7/ num = num.toString().replace(/\$|\,/g,''); if(isNaN(num)) num = "0"; sign = (num == (num = Math.abs(num))); num = Math.floor(num*100+0.50000000001); cents = num%100; num = Math.floor(num/100).toString(); if(cents<10) cents = "0" + cents; for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3)); return (((sign)?'':'-') + '£' + num + '.' + cents); } //Define function to init the form on reload: function InitForm() { //Reset the displayed total on form: document.myForm.total.value='£0'; document.myForm.hiddentotal.value=0; //Set all checkboxes on form to unchecked: for (xx=0; xx < document.myForm.elements.length; xx++) { if (document.myForm.elements[xx].type == 'checkbox') { document.myForm.elements[xx].checked = false; } } }