// required field checking...
// note: no <script> tags here...

// add the following to the page:
// <script language="javascript1.2" src="reqcheck.js"></script>
// under the form tag:
// <form ...  onsubmit="show_req(this)">

//      // convert the required "long values" to an associative array
//      var req = form.required.value;
//      //var vals = req.split(',');
//      // javascript 1.2 - allow for optional spaces
//      var vals = req.split(/,\s*/);
//      if (get_elem(form, "required_long")) {
//          var req_long = form.required_long.value;
//          //var vals_long = req_long.split(',');
//          var vals_long = req_long.split(/,\s*/);
//      } else
//          var vals_long = vals;
//      var longtext = [];
//      for (var i=0; i<vals.length; i++) {
//          longtext[vals[i]] = vals_long[i];
//      }
  //var reqdebug = 1;
  var reqdebug = 0;

  function show_req(form) {
      //alert (form.required.value);
      //var req = form.required.value;
      var req = get_elem(form, "required");
      if (! req)
          return true;
      req = req.value;
      //var vals = req.split(',');
      // javascript 1.2 - allow for optional spaces
      var vals = req.split(/,\s*/);
      if (get_elem(form, "required_long")) {
          var req_long = form.required_long.value;
          //var vals_long = req_long.split(',');
          var vals_long = req_long.split(/,\s*/);
      } else
          var vals_long = vals;
      var missing = "";
      for (var i=0; i<vals.length; i++) {
          if (val_filled(form, vals[i]))
              continue;		// all set

          // not found...
          missing += " '" + vals_long[i] + "'\n";
      }
      if (missing.length > 0) {
          missing = "The following fields are required and \n need to be filled in: \n\n" + missing;
	  alert(missing);
	  return false;
      } else {
          return true;
          //form.submit();
	  //alert ('submit');
      }
  }

  
  // a simple function for checking where a value is "filled" or not
  function val_filled(form, name) {
      var elem = get_elem(form, name);
      if (! elem) {
          if (reqdebug)
              alert("can't find elem: '" + name + "'\n");
          return true;
      }
      var val = elem.value;

      var chkblank = /^\s*$/i;	// for testing only spaces
      var pattern = /select/i;
      //if (pattern.test(elem.type) && elem.selectedIndex>0) 
      if (pattern.test(elem.type)) {
	  var ind = elem.selectedIndex;
	  if (ind>0)
	      return true;
          // assume the first option is special - "select an option"
	  if (ind==0 && elem.options[ind].value != "")
	      return true;
          // if 2nd option has no value either, assume there are no values...
	  if (ind==0 && elem.options[ind].value == "" && elem.options[1].value == "")
	      return true;
          if (elem.options.length == 1)
              return true;     // only one choice.  no options.
      }
      //else if ( (elem.type=="checkbox" || elem.type=="radio") & 
      //   elem.checked==true)
      else if (elem.type=="checkbox" || elem.type=="radio") {
          if (elem.checked)
              return true;
	  // radio buttons an checkboxes imply more than 1 item
	  var count = 1;
	  var gotone = 0;
	  while (!gotone) {
	      elem = get_elem_num(form, name, count);
	      if (!elem)   // exit condition
	          break;
	      if (elem.checked) 
	          gotone = 1;
		  count++;
	  }
	  if (gotone == 1)
              return true;
      }
      else if ( (elem.type=="text" || elem.type=="textarea" || 
                 elem.type=="hidden" || elem.type=="file") && 
	  	  (! chkblank.test(val) ) ) 
      //	   	(val != "") ) 
          return true;

      // NOT FILLED!
      return false;
  }


  // make sure numeric fields (as listed in the hidden input "numeric")
  // are actually numeric.   Allow numbers of the form: $123,456.00
  function show_badnums(form) {
      //alert("show_bad_num");
      // convert the required "long values" to an associative array
      //var req = form.required.value;
      var req = get_elem(form, "required");
      if (! req)
          return true;
      req = req.value;
      //var vals = req.split(',');
      // javascript 1.2 - allow for optional spaces
      var vals = req.split(/,\s*/);
      if (get_elem(form, "required_long")) {
          var req_long = form.required_long.value;
          //var vals_long = req_long.split(',');
          var vals_long = req_long.split(/,\s*/);
      } else
          var vals_long = vals;
      var longtext = [];
      for (var i=0; i<vals.length; i++) {
          longtext[vals[i]] = vals_long[i];
      }

      var missing = "";
      var numeric = get_elem(form, "numeric");
      if (! numeric)
          return true;
      numeric = numeric.value;
      //var nums = numeric.split(',');
      var nums = numeric.split(/,\s*/);
      var chkblank = /^\s*$/;	// for testing only spaces

      for (var i=0; i<nums.length; i++) {
	  var elem = get_elem(form, nums[i]);
          if (! elem) {
              if (reqdebug)
                  missing += "can't find elem(badnums): '" + nums[i] + "'\n";
              continue;
          }
	  var val = elem.value;

          //if (val == "")
	  //    continue;
	  // allow for only spaces
	  if ( chkblank.test(val) ) 
	      continue;

          // get rid of $ and , and spaces before checking for a number
          val = val.replace(/,/g, "");
          val = val.replace(/ /g, "");
          val = val.replace(/^\$/, "");
          var numval = parseInt(val);
          if (isNaN(numval)) {
	      var longname = longtext[nums[i]];
	      if (!longtext[nums[i]])
	          longname = nums[i];
              missing += " '" + longname + "' value='" + elem.value + "'\n";
	  }
      }
      if (missing.length > 0) {
          missing = "The following fields should be numbers!: \n\n" + missing;
	  alert(missing);
	  return false;
      } else {
          return true;
      }

  }

  function show_all_req() {
      //alert("show all required!");
      len = window.document.forms.length
      for (var i=0; i<len; i++) {
          form = window.document.forms[i];
	  //alert("form " + i + " " + form);
	  //req = form.required;
	  //if (req) { req = req.value; }
	  //alert("req " + req);
          if (!show_req(form))
              return false;
          if (!show_badnums(form))
              return false;
      }
      //alert("OK!");
      return true;
  }



