

var isIE = navigator.appName == 'Microsoft Internet Explorer' ? 1 : 0;
function checkCountry() {
  if (!document.getElementById) return true;  /* Fallback to true */
  var country = document.getElementById("countrychoice");
  if (!country) return true;  /* Fallback to true */
  switch (country.value) {
    case '' : /* Other country */
      var err_mess = nodeValue(document.getElementById("limitedcountries"));
      alert(err_mess);
      return false;
    break;
    case 'GB':
    default:
      return true;
    break;
  }
}
function submitCountry() {
  if (checkCountry()) {
    if (document.getElementById) {
      var form = document.getElementById("frmcart");
      if (form) form.submit();
    }
    return false;
  }
  return false;
}
function nodeValue(node) {
  var result = "";
  if (isIE) {
    result = node.innerHTML;
  }
  else {
    if (node.nodeType == 1) {
      result = nodeValue(node.firstChild);
    }
    else if (node.nodeType == 3) {
      result = node.nodeValue;
    }
  }
  return(result);
}
function checkBasket() {
   if(checkForm('securehost', 'cardholdersname', 'cardholderaddr1', 'cardholdercity', 'cardholderstate', 'cardholderpostcode', 'cardholdertelephonenumber', 'cardholdersemail', 'confirmemail')) {
     var terms = document.forms['securehost']['terms_acceptance'];
     var termschecked = false;
     if(terms.checked ) {
       termschecked = true;
     }
     if(!termschecked) {
       alert("You MUST confirm that you agree to the terms and conditions of Page Communication Ltd before continuing.");
       terms.focus();
       return false;
     }
     if(termschecked)
     {
       document.getElementById('securehost').submit();
     }
     return false;
   }
   return false;
 }
 
 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[nFields[0]];
   var el = fm.elements;    // Get all form elements
   var inValid = false;
   var firstInvalid;
   var emailUsed = '';
   var emailalert = '';
   
   if(!checkCountry()) return;
   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=1;j<nFields.length;j++) {   // Loop through all required names
       if( nFields[j]=="email" || nFields[j]=="cardholdersemail") emailUsed = nFields[j];  // 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[emailUsed].value.match("^[^@ ]+@[^@ ]{2,}\\.[^@ \\.]{2,}$") == null) {
       setColor(fm[emailUsed], bgBad);
       emailalert = "\nYou didn't enter a valid email address (for example bob@smith.com). Please try again.";
       if(!inValid) {
         inValid = true;
         firstInvalid = fm[emailUsed];
       }
//       fm.emailUsed.focus();
//       return false;
     }
   }
   if(inValid) {
     // Display error message that text elements need filling
     alert("Please check that you have filled in all the required fields." + emailalert );
     firstInvalid.focus();
     return false;
   }
   // Everything correct submit form
   return true;
   //fm.submit();
 }

