<!--
//
// Validate.js
//
// @author      Michael Skolik
// @version     2.1
// @date        November  06, 1999
// @modified    November  06, 2000
//
// @purpose:	Collection of Validation functions
//
//


// Checks, if all required parameters are filled out.
// The list of required parameters is specified in a hidden
// variable called 'Required'.
//
// Arguments:
//	form	- Form name
//

function isRequired(aForm)
{
  var argv     = isRequired.arguments;
  var argc     = isRequired.arguments.length;
  var text     = (argc > 1) ? argv[1] : 'The following required fields are empty:';
  var str;
  var rc = true;
  var strArray = aForm.Required.value.split(",");
  text += "\n";
  for (var i=0; i < strArray.length; i++) {
     str = replace(trim(strArray[i]), "'", "");
     if (trim(getParameterValue(aForm, str)).length == 0) {
         text += "\n  - " + str;
	 rc = false;
     }
  }

  if (rc == false)  alert(text);
  return rc;
}


var LOWER	= "abcdefghijklmnopqrstuvwxyz";
var UPPER	= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var NUMBER	= "0123456789";
var SIGN	= "+-";
var DOT		= ".";
var SPACE	= " ";


// Checks, if the string contains valid characters.
//
// Arguments:
//	str		- string to check
//	ValidChars	- List of valid characters
//

function isValidString(str, ValidChars)
{
  for (var i=0; i < str.length; i++) {
     if (ValidChars.indexOf(str.charAt(i)) == -1)  return false;
  }
  return true;
}

function isValidInteger(str)
{
  return isValidString(str, SIGN+NUMBER);
}

function isValidFloat(str)
{
  return isValidString(str, NUMBER+SIGN+DOT);
}

// Checks, if the string contains a valid date.
//
// Arguments:
//	datein		- string that contains the date
//			  in the followin format:
//			  [YY]YY-MM-DD or [YY]YY/MM/DD
//

function isValidDate(datein){
        
  var indate = replace (datein, "-", "/");

  var sdate   = indate.split("/");
  var chkDate = new Date(Date.parse(indate));

  var cmpDate=(chkDate.getYear())+"/"+(chkDate.getMonth()+1)+"/"+(chkDate.getDate());
  var indate2=(Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2]));

  if (indate2!=cmpDate){         alert("Invalid date.");  return false; }
  else {
    if (cmpDate=="NaN/NaN/NaN"){ alert("Invalid date.");  return false; }
  return true;
  }

}


// Checks, if the string contains a valid email address.
//
// Arguments:
//	Email		- string that contains the Email
//

function isValidEmail(Email)
{
var EmailOk  = true
var Temp     = Email;
var AtSym    = Temp.value.indexOf('@')
var Period   = Temp.value.lastIndexOf('.')
var Space    = Temp.value.indexOf(' ')
var Length   = Temp.value.length - 1   // Array is from 0 to length-1

if ((AtSym < 1) ||                     // '@' cannot be in first position
    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    (Period == Length ) ||             // Must be atleast one valid char after '.'
    (Space  != -1))                    // No empty spaces permitted
   {  
      EmailOk = false
   }
return EmailOk
}


// Checks, if a password and its confirmation is valid
//
// Arguments:
//	pwd	- string that contains the password
//	cpwd	- string that contains the confirmed password
//

function isValidPwd(Pwd, cPwd)
{

  if (Pwd == ""  ||  cPwd == "") {
        alert("Password is missing.");
        return false;
   }

   if (Pwd.length < 4) {
     alert("Password must contain at least 4 characters.");
     return false;
   }

   if (Pwd != cPwd) {
      alert("Passwords don't match.");
      return false;
   }
   return true;
}



function getParameterValue(form, str)
{
  var Element, Type, Value;
  for (var i=0; i < form.length; i++) {
     if (form.elements[i].name == str) {
	Element = form.elements[i];
	Type    = Element.type;
	if (Type =="select-one") {
	  Value = Element.options[Element.options.selectedIndex].value; 
	} else {
	  Value = Element.value;
	}
	return Value;
     }
  }
  return ""
//  return "N/A"
}



function InputMakeArray(form, Prefix)
{
  var PreLen	= Prefix.length;
  var InputList	= new Array();
  var objCtrl;
  var objValue;
  var n = 0;
  var id;

  for (var i=0; i < form.length; i++) {
    objCtrl = form.elements[i];
    if (objCtrl.name.substring(0,PreLen) == Prefix) {
      objValue = trim(objCtrl.value);
      if (objValue.length > 0) {
	id = objCtrl.name.substring(PreLen);
	InputList[n] = new Option(id, objValue);
	n++;
      }
    }
  }
  return InputList;
}
// -->
