 function checkBasket() {
   if(checkForm('cardholdersname', 'cardholdersemail', 'cardholderaddr1', 'cardholdercity', 'cardholderstate', 'cardholderpostcode', 'cardholdertelephonenumber')) {
     document.getElementById('securehost').submit();
     return false;
   }
   return false;
 }
 
// Form to be checked
// Usually the first form element on the document
  if (typeof formCheckNum == 'undefined') { var formCheckNum = 0; }

 function setColor(el, bg) {
   if (el.style) el.style.backgroundColor = bg;
 }

 function resetcolor(el) {
   if ( (el.style) && (el.value != '') ) el.style.backgroundColor = '';
 }

 function checkForm() {
   var nFields = checkForm.arguments;  // All the inputs to verify
   var bgBad = "#99ccff";    // Colour of unfilled input
   var fm = document.forms[formCheckNum];
   var el = fm.elements;    // Get all form elements
   var inValid = false;
   var firstInvalid;
   var emailUsed = false;
   var emailalert = '';
   
   for(i=0;i<el.length;i++) {  // Loop through all form elements
     // Cycle through text-based field inputs to check for non-blank values
     for(j=0;j<nFields.length;j++) {   // Loop through all required names
       if( nFields[j]=="email" ) emailUsed = true;  // Check if there is an email field, used later
       if( (el[i].type != "radio") && (el[i].type != "checkbox") ) {
         if( (el[i].name==nFields[j]) && (el[i].value == "") ) {
           setColor(el[i], bgBad);    // Set the hightlight colour
           if(!inValid) {
             inValid = true;
             firstInvalid = el[i];
           }
           break;        
         }
         else setColor(el[i], "");
       } // Text based inputs
       else {  // Input is either a radio or checkbox
         if(el[i].name==nFields[j]) { // Check input name is same as required name
           var radiogroup = fm.elements[el[i].name];   // Get the whole set of radio/check buttons.
           if( !(radiogroup.length > 1) ) {    // Radio/check option on own (code falls over)
             radiogroup = new Array(el[i],el[i]) ;      // So pretend that there are two. He he :)
           }
           var itemchecked = false;
           for(r=0;r<radiogroup.length;r++) {  // Loop through entire group
             if( radiogroup[r].checked ) {   // Find if at least one is checked
               itemchecked = true;
               break;        // If one found break
             }
           }
           if(!itemchecked) {      // No radios/checks checked
             for(r=0;r<radiogroup.length;r++) { // Loop through entire group
               setColor(radiogroup[r], bgBad);  // Set the hightlight colour
               if(!inValid) {
                 inValid = true;
                 firstInvalid = radiogroup[r];
               }
             }
           } else {        // At least one checked
             for(r=0;r<radiogroup.length;r++) {
               setColor(radiogroup[r], "");  // Unset the hightlight colour
             }
           }
           if(itemchecked) break;
         }
       }
     }
   }  // Loop all form elements

   if(emailUsed) {
     // Checks Email has basic valid structure, could be much more complicated
     if(fm.email.value.match("^[^@ ]+@[^@ ]{2,}\\.[^@ \\.]{2,}$") == null) {
       setColor(fm.email, bgBad);
       emailalert = "\nPlease also check your email address, it appears to be malformed.";
       if(!inValid) {
         inValid = true;
         firstInvalid = fm.email;
       }
//       fm.email.focus();
//       return false;
     }
   }
   if(inValid) {
     // Display error message that text elements need filling
     alert("Please check and fill out all the required fields." + emailalert );
     firstInvalid.focus();
     return false;
   }
   // Everything correct submit form
   return true;
   //fm.submit();
 }

