// -----------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// -----------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};

// -----------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// -----------------------------------------

function commonCheck    (vfld,   // element to be validated
                         low,	//min number of characters
						 high	//max number of characters
						 )   
{
  var emptyString = /^\s*$/;
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  if (emptyString.test(vfld.value)){
	  return false;
  }
  if(vfld.value.length < low)
  	return false;
  if(vfld.value.length > high)
  	return false;
	
  return true;
}


// -----------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// -----------------------------------------

function validateEmail  (vfld   // element to be validated
							)
{
  var stat = commonCheck (vfld);
  if (!stat) return false;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
  if (!email.test(tfld)) {
    return false;
  }
  return true;
}

function validateOnSubmitDownload() {
	var elem;
	var errs=0;
	var message="";

	if (!commonCheck(document.forms.info.name, 3, 40)){
		errs += 1;
		message += "Invalid Name\n";
	}
	if (!validateEmail(document.forms.info.from)){
		errs += 1;
		message += "Invalid Email\n";
	}


	if (errs>=1)  alert("Please check your info:\n\n"+message);

	return (errs==0);
}


function validateOnSubmit() {
	var elem;
	var errs=0;
	var message="";

	if (!commonCheck(document.forms.info.name, 3, 40)){
		errs += 1;
		message += "Invalid Name\n";
	}
	if (!validateEmail(document.forms.info.from)){
		errs += 1;
		message += "Invalid Email\n";
	}
	if(document.forms.info.from.value != document.forms.info.email2.value){
		errs += 1;
		message += "Email does not match Confirm Email.  Please retype.\n";
	}


	if (errs>=1)  alert("Please check your info:\n\n"+message);

	return (errs==0);
}

function validateOnSubmit2() {
	var elem;
	var errs=0;
	var message="";

	if (!commonCheck(document.forms.info.username, 6, 15)){
		errs += 1;
		message += "Invalid Username (Must be 6-15 characters)\n";
	}
	if (!commonCheck(document.forms.info.password, 6, 15)){
		errs += 1;
		message += "Invalid Password  (Must be 6-15 characters)\n";
	}
	if(document.forms.info.password.value != document.forms.info.password2.value){
		errs += 1;
		message += "Password does not match Confirm Password\n";
	}

	if (errs>=1)  alert("Please check your info:\n\n"+message);

	return (errs==0);
}