/* Use and distribute Freely as long as following messages are intact:
 *--------------------------------------------------------------*
 * fieldedit, v 1.0:						*
 *  library of Javascript Functions for checking entry fields	*
 *    Initial release                                           *
 * This script written and © 2004 Christine Jamison             *
 * ALL RIGHTS RESERVED.                                         *
 * Created on : 02-Oct-05                                       *
 * Modified on : 15-Oct-05                                      *
 * Mail : tech-info@nwmagic.net                                 *
 * v 1.0 - Original Release                                     *
 *--------------------------------------------------------------*
 */

//  work1 = document.forms.register.name.value;
//  work1 = this.form.name.value;

/*
 *  Purpose: return true if the string is non-null and digits-only, false
 *	     otherwise.
 *	     False always displays an alert message, explaining why the
 *	     data is not valid.
 *
 *  Arguments:	digits in object (use "this" for current field);
 *		Minimum length of string;
 *		Maximum length of string;
 *		bool defining if a null string is valid.
 *
 *  Variables:
 *	Valid_char: a list of valid characters.
 */

function digit_chk(pass_str, min_len, max_len, allow_null)
  { var exit_sts=true;
    var test_str='';			// WARNING: Without this, test_str can
					// CHANGE without warning in IE!
    var valid_char="0123456789";
    var test_char;

    test_str=pass_str.value;
//    alert("DIGIT CHECK!! ["+test_str+':'+test_str.length+':'+allow_null+']');

    if (test_str.length == 0)
      { if (allow_null == false)
	  { alert("You must enter at least "+min_len.toString()+" digit(s). ("+
		  pass_str.name+")");
	    return false;
	  }
     else { pass_str.value='0';
	    return true;
	  }
      }
 else { if (test_str.length < min_len || test_str.length > max_len)
	  { alert("You must enter between "+min_len+" and "+max_len +
		  " digits. ("+pass_str.name+")");
	    return false;
	  }
      }

    for (ind=0; ind < test_str.length && exit_sts == true; ind++)
      { test_char=test_str.charAt(ind);
	if (valid_char.indexOf(test_char) == -1)
	  { alert("You must enter digits ONLY. ("+pass_str.name+")");
	    exit_sts=false;
	  }
      }
    return exit_sts;
  }

/*
 *  Purpose: return true if the string is valid (as defined by the rule
 *	     arg's), false otherwise. False always displays an alert
 *	     message, explaining why the data is not valid.
 *
 *  Arguments:	a string in an object;
 *		Minimum length of string;
 *		Maximum length of string;
 *		bool defining if null string is valid.
 *
 *  Variables:
 *	None.
 */
function string_chk(pass_str, min_len, max_len, allow_null)
  { var exit_sts = true;
    var test_str='';			// See WARNING, above.

    test_str=pass_str.value;
//    alert("STRING CHECK!! ["+test_str+':'+test_str.length+']');
    if (test_str.length == 0)
      { if (allow_null == false)
	  { alert("You may not enter an empty value. ("+pass_str.name+")");
	    return false;
	  }
      }
 else { if (test_str.length < min_len || test_str.length > max_len)
	  { alert("You must enter between "+min_len+" and "+max_len +
	    " characters. ("+pass_str.name+")");
	    return false;
	  }
      }

    return true;
  }

/*
 *  Purpose: return true if the string is a valid number, false otherwise.
 *	     False always displays an alert message, explaining why the data
 *	     is not valid.
 *
 *  Arguments:	A number in an object;
 *		Minimum numeric value;
 *		Maximum numeric value;
 *		A bool defining if zero-value string is valid;
 *		The number at which (or above) a warning msg is displayed;
 *		Maximum number of decimal places allowed.
 *
 *  Variables:
 *	bad_char: a list of characters which are NOT acceptable.
 */
function number_chk(pass_str, min_val, max_val, allow_zero, warn_val, num_dec)
  { var exit_sts=true;
    var test_str='';			// See WARNING, above.
    var test_num=0;
    var bad_char="+ ";
    var test_char;

    test_str=pass_str.value;
    test_num=test_str*1;
//    alert("NUMBER CHECK!! ["+test_str+':'+test_num.toString()+']');
    if (test_str.length == 0)
      { if (allow_zero == false)
	  { alert("You may not enter a zero (null) value. ("+pass_str.name+")");
	    return false;
	  }
     else { pass_str.value='0';
	    test_num=0;
	    return true;
	  }
      }
    if (isNaN(test_num))
      { alert("You MUST enter a number. ("+pass_str.name+")");
	return false;
      }
    if (test_num == 0 && allow_zero == false)
      { alert("You may not enter a zero value. ("+pass_str.name+")");
	return false;
      }

    test_ptr=-1;
    for (ind=0; ind < test_str.length && exit_sts == true; ind++)
      { test_char=test_str.charAt(ind);
	if (bad_char.indexOf(test_char) > -1)
	  { alert("You MUST enter a valid number. ("+pass_str.name+")");
	    return false;
	  }
	if (test_char == '.')
	  { test_ptr=ind;
	  }
      }
    if (test_ptr > -1 && test_str.length-test_ptr-1 > num_dec)
      { alert("You MUST enter no more than "+num_dec.toString() +
	      " decimal places. ("+pass_str.name+")");
	return false;
      }
    if (test_num < min_val || test_num > max_val)
      { alert("You MUST enter a value between "+min_val.toString() +
	      " and "+max_val.toString()+". ("+pass_str.name+")");
	return false;
      }
    if (warn_val > 0 && test_num > warn_val)
      { alert("The value you entered looks a bit large. " +
	      "Please be sure you have entered the correct value. (" +
	      pass_str.name+")");
      }
    return exit_sts;
  }

/*
 *  Purpose: return true if the date is valid, false otherwise. False
 *	     always displays an alert message, explaining why the data
 *	     is not valid.
 *
 *  Arguments:	 7-, 9-, or 10-byte date object (i.e., with dashes).
 *
 *  Variables:
 *	date_val: date object.
 */
function date_chk(pass_date, with_day)
  { var date_val;
    var test_date='';			// See WARNING, above.

    test_date=pass_date.value;
//    alert("DATE CHECK!! ["+test_date+':'+test_date.length+']');
    if (with_day == false)
      { work1=test_date.indexOf('-');
	if (work1 > 0)
	  { test_date=test_date.substr(0, work1)+'-01'+
		      test_date.substr(work1, 5);
	  }
      }

    if (test_date.length == 7 || test_date.length == 9)
      { test_date='0'+test_date;
	if (with_day == true)
	  {pass_date.value=test_date;}
      }
    if (test_date.length == 8)
      { test_date=test_date.substr(0,6)+'20'+test_date.substr(6,2);
	if (with_day == true)
	  {pass_date.value=test_date;}
      }
//    alert("Date Chk1 ["+test_date+':'+test_date.length+']');
    if (test_date.length != 10)
      { if (with_day == false)
	  { work1="MM-CCYY.";
	  }
     else { work1="MM-DD-CCYY.";
	  }
	alert("You must enter a properly formatted date, "+work1+" (" +
	      pass_str.name+")");
	return false;
      }

    if (test_date.substr(6,4) < "1900")
      { alert("You must enter a date AFTER 1899. ("+pass_str.name+")");
	return false;
      }
//set up a Date object based on the passed argument.
//Note that javascript months start at 0 (0-11 instead of 1-12)
    date_val = new Date(test_date.substr(6,4), test_date.substr(0,2)-1,
		       test_date.substr(3,2));

// The javascript date function is too forgiving and will generate
// its best guess date if the passed date is invalid. We'll use this
// to our advantage by creating a date object and then comparing it
// to the passed parameters. If the date object is different, then it
// must have been an invalid date to start with...

//alert("A new date: "+date_val+", broken down as: "+date_val.getYear()+"+"+
//	date_val.getMonth()+"+"+date_val.getDate()+", "+test_date+".");
    work1=date_val.getYear();	// WHY can't NS and IE agree on how date info
    if (work1 < 1900)		// will be returned? Sheesh....
      { work1 +=1900;
      }
    if (test_date.substr(6,4) != work1 ||
	test_date.substr(0,2) != date_val.getMonth() + 1 ||
	test_date.substr(3,2) != date_val.getDate())
      { alert("You must enter a valid date. ("+ pass_str.name+")");
	return false;
      }
return true;
  }

/*
 *  Purpose: return true if test_str is a valid email addr, false otherwise.
 *	     False always displays an alert message, explaining why the
 *	     data is not valid.
 *
 *  Arguments:	an e-mail address in string format.
 *
 *  Variables:
 *	None.
 */
function email_chk(pass_str, allow_null)
  { var exit_sts=true;
    var test_str='';			// See WARNING, above.

    test_str=pass_str.value;
//    alert("EMAIL CHECK!! ["+test_str+':'+test_str.length+']');
    if (test_str.length == 0)
      { if (allow_null == false)
	  { alert("You must enter a valid e-mail address. ("+pass_str.name+")");
	    return false;
	  }
     else { return true;
	  }
      }

    work1=test_str.indexOf('@');
    work2=test_str.lastIndexOf('.');
    work3=test_str.length-1;
    work4=0;
    for (work5=0; work5 <= work3; work5++)
      { if (test_str.substr(work5,1) < '-')
	  { work4++;
	  }
      }
//alert("E-mail testB: "+test_str+":"+work1+"/"+work2+"/"+work3+"/"+work4+"/"+work5);
    if (work1 < 2 || work2 < 2 || work1 > work2 ||
	work3-work2 < 2 || work3-work2 > 3 || work4 > 0)
      { alert("You must enter a valid e-mail address format, " +
	      "name@domain.com. ("+pass_str.name+")");
	return false;
      }
    return exit_sts;
  }

/*
 *  Purpose: return true if test_str is a valid phone #, false otherwise.
 *	     False always displays an alert message, explaining why the
 *	     data is not valid.
 *
 *  Arguments:	a Phone Number in string format.
 *
 *  Variables:
 *	None.
 */
function phone_chk(pass_str, allow_null)
  { var exit_sts=true;
    var test_str='';			// See WARNING, above.
    var valid_char="0123456789";
    var test_char;

    test_str=pass_str.value;
//    alert("PHONE CHECK! ["+test_str+':'+test_str.length+']');
    if (test_str.length == 0)
      { if (allow_null == false)
	  { alert("You must enter a valid phone number. ("+pass_str.name+")");
	    return false;
	  }
     else { return true;
	  }
      }

    if (test_str.length == 10)
      { test_str=test_str.substr(0, 3)+'-'+test_str.substr(3, 3)+"-"+
		 test_str.substr(6, 4);
	pass_str.value=test_str;
      }
    if (test_str.length != 12)
      { alert("You must enter a valid 10- or 12-character phone number. (" +
	      pass_str.name+")");
	return false;
      }
    for (ind=0; ind < test_str.length && exit_sts == true; ind++)
      { test_char=test_str.charAt(ind);
	if (ind == 3 || ind == 7)
	  { if (test_char != '-')
	      { alert("You must enter dashes in the usual places. (" +
		      pass_str.name+")");
		exit_sts=false;
	      }
	  }
     else { if (valid_char.indexOf(test_char) == -1)
	      { alert("You must enter digits in the usual places. (" +
		      pass_str.name+")");
		exit_sts=false;
	      }
	  }
      }
    return exit_sts;
  }

