   window.onload = function() {
     document.getElementById("contact").onsubmit = function() {
       return checkRegex();
     }
   }

   function checkRegex() {
      var isValid = true;
      var thisValid = new Array();
      var ids = new Array();
      var texts = new Array();
      var regex = new Array();
      var errorMsg = new Array();
      ids[0] = new Array("Name","MAddress","DAddress","Phone","Email","Account","MCity","MZip","DCity","DZip");
      texts[0] = new Array("Name","Mailing Address","Delivery Address","Phone","E-Mail","Account Number","Mailing Address City","Mailing Address ZIP", "Delivery Address City","Delivery Address ZIP");
      regex[0] = /^[^/\s].*/;
      errorMsg[0] = "[label] cannot be blank";
      for (var i = 0; i < 1; i++)
        thisValid[i] = applyRegex(regex[i], ids[i], texts[i], errorMsg[i]);
      for (i = 0; i < thisValid.length; i++)
        isValid = isValid && thisValid[i];
        
      var form = document.getElementById("contact");
        
      // remove error divisions for non-standard validation
      var customErrors = new Array("OrderType","OilType","OilQuantity","oilgallons","GasType","GasQuantity","gasgallons","DieselType1","DieselType2");
      for (i = 0; i < customErrors.length; i++)
        removeErrorDiv(customErrors[i]);  

      // validate order type
      var orderType = 0;
      orderType = document.getElementById("OrderType").selectedIndex;
      if (orderType == 0) {
        addErrorDiv('OrderType', "<b>Note:</b> You must select an Order Type", "98%");
        isValid = false;
      }
      // add validation depending on order type selected
      // VALIDATION FOR HEATING OIL
      else if (orderType == 1) {
      	var chosenType = '';
        for (i = 0; i < form.OilType.length; i++) {
          if (form.OilType[i].checked)
            chosenType = form.OilType[i].value;	
        }
        if (chosenType == '') {
          addErrorDiv('OilType', "<b>Note:</b> You must select an Oil Type", "98%");
          isValid = false;
        }
        
      	var chosenQuantity = '';
        for (i = 0; i < form.OilQuantity.length; i++) {
          if (form.OilQuantity[i].checked)
            chosenQuantity = form.OilQuantity[i].value;	
        }
        
        if (chosenQuantity == '') {
          addErrorDiv('OilQuantity', "<b>Note:</b> You must select an Oil Quantity", "98%");
          isValid = false;
        }

        if (chosenQuantity == "Gallons") {
        	var gal = document.getElementById("oilgallons").value;
          if (gal == '' || gal < 150 || isNaN(gal)) {
            addErrorDiv('oilgallons', "<b>Note:</b> You must order a minimum delivery of 150 gallons", "98%");
            isValid = false;
          }
        }
      }

      // VALIDATION FOR GASOLINE
      else if (orderType == 2) {
      	var chosenType = '';
        for (i = 0; i < form.GasType.length; i++) {
          if (form.GasType[i].checked)
            chosenType = form.GasType[i].value;	
        }
        if (chosenType == '') {
          addErrorDiv('GasType', "<b>Note:</b> You must select a Gas Type", "98%");
          isValid = false;
        }
        
      	var chosenQuantity = '';
        for (i = 0; i < form.GasQuantity.length; i++) {
          if (form.GasQuantity[i].checked)
            chosenQuantity = form.GasQuantity[i].value;	
        }
        
        if (chosenQuantity == '') {
          addErrorDiv('GasQuantity', "<b>Note:</b> You must select a Gas Quantity", "98%");
          isValid = false;
        }

        if (chosenQuantity == "Gallons") {
        	var gal = document.getElementById("gasgallons").value;
          if (gal == '' || gal < 150 || isNaN(gal)) {
            addErrorDiv('gasgallons', "<b>Note:</b> You must order a minimum delivery of 150 gallons", "98%");
            isValid = false;
          }
        }
      }
        
      // VALIDATION FOR DIESEL
      else if (orderType == 3) {
      	var chosenType1 = '';
      	var chosenType2 = '';
        for (i = 0; i < form.DieselType1.length; i++) {
          if (form.DieselType1[i].checked)
            chosenType1 = form.DieselType1[i].value;	
        }
        if (chosenType1 == '') {
          addErrorDiv('DieselType1', "<b>Note:</b> You must select a Diesel Type (On Road / Off Road)", "98%");
          isValid = false;
        }
        
        for (i = 0; i < form.DieselType2.length; i++) {
          if (form.DieselType2[i].checked)
            chosenType2 = form.DieselType2[i].value;	
        }
        if (chosenType2 == '') {
          addErrorDiv('DieselType2', "<b>Note:</b> You must select a Diesel Type (No. 2 / Super)", "98%");
          isValid = false;
        }

        var chosenQuantity = '';
        for (i = 0; i < form.GasQuantity.length; i++) {
          if (form.GasQuantity[i].checked)
            chosenQuantity = form.GasQuantity[i].value;	
        }
        
        if (chosenQuantity == '') {
          addErrorDiv('GasQuantity', "<b>Note:</b> You must select a Gas Quantity", "98%");
          isValid = false;
        }

        if (chosenQuantity == "Gallons") {
        	var gal = document.getElementById("gasgallons").value;
          if (gal == '' || gal < 150 || isNaN(gal)) {
            addErrorDiv('gasgallons', "<b>Note:</b> You must order a minimum delivery of 150 gallons", "98%");
            isValid = false;
          }
        }
      }
      return isValid;
    }

    function applyRegex(regex, ids, texts, errorMsg) {
      var obj, thisValid = true;
      for (var i = 0; i < ids.length; i++) {
        obj = document.getElementById(ids[i]);
        removeErrorDiv(ids[i]);
        if (obj.value.match(regex) == null) {
          var errorStr = "<b>Note:</b> " + errorMsg.replace("[label]", texts[i]);
          addErrorDiv(ids[i], errorStr, "98%");
          thisValid = false;
        }
      }
      return thisValid;
    }

    function addErrorDiv(domID, msg, width) {
     var add = true;
     var target = document.getElementById("errors");
     var errorDiv = document.createElement("div");
     var errorDivID = "error_" + domID;
     if (document.getElementById(errorDivID))
       return;
     errorDiv.setAttribute("id", errorDivID);
     errorDiv.className = "validation_error";
     errorDiv.style.width = width;
     errorDiv.style.padding = "1%";
     errorDiv.innerHTML = msg;
     target.appendChild(errorDiv);
   }

   function removeErrorDiv(domID) {
     var errorDivID = "error_" + domID;
     if (document.getElementById(errorDivID)) {
       var child = document.getElementById(errorDivID);
       var parent = document.getElementById("errors"); 
       parent.removeChild(child);
     }
   }