// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

// email

function checkEmail (str) {
var error="";
if (str == "") {
   error = "You didn't enter an email address.<br><br>";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(str))) { 
       error = "Please enter a valid email address.<br><br>";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (str.match(illegalChars)) {
          error = "The email address contains illegal characters.<br><br>";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (str) {
var error = "";
if (str == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = str.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// password - between 6-14 chars, uppercase, lowercase, and numeral

function checkPassword1 (str) {
var error = "";
if (str == "") {
   error = "You didn't enter a password.<br><br>";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((str.length < 6) || (str.length > 14)) {
       error = "The password must be between 6 to 14 letters long.<br><br>";
    }
    else if (illegalChars.test(str)) {
      error = "The password contains illegal characters.<br><br>";
    } 
    else if (!((str.search(/(a-z)+/)) && (str.search(/(A-Z)+/)) && (str.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.<br><br>";
    }  
return error;    
}  
  
function checkPassword2 (str) {
var error = "";
if (str == "") {
   error = "You didn't enter a password.<br><br>";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((str.length < 6) || (str.length > 14)) {
       error = "The confirm password must be between 6 to 14 letters long.<br><br>";
    }
    else if (illegalChars.test(str)) {
      error = "The confirm password contains illegal characters.<br><br>";
    } 
    else if (!((str.search(/(a-z)+/)) && (str.search(/(A-Z)+/)) && (str.search(/(0-9)+/)))) {
       error = "The confirm password must contain at least one uppercase letter, one lowercase letter, and one numeral.<br><br>";
    }  
return error;    
} 
   
function checkFName (str){
var error = "";
if (str == "") {
   error = "Your First Name is a required field.<br><br>";
}
    var legalChars =new RegExp(/^[a-z ]+$/i);
    if ((str.length < 2) || (str.length > 20)) {
       error = "First Name must be between 2-20 letters long.<br><br>";
    }
    else if (legalChars.test(str)) {
    }
    else
    {
		error = "First Name contains illegal characters. You may only use letters in your name.<br><br>";
    } 
return error;
}

function checkLName (str){
var error = "";
if (str == "") {
   error = "Your Last Name is a required field.<br><br>";
}
    var legalChars =new RegExp(/^[a-z ]+$/i);
    if ((str.length < 2) || (str.length > 20)) {
       error = "Last Name must be between 2-20 letters long.<br><br>";
    }
    else if (legalChars.test(str)) {
    }
    else
    {
		error = "Last Name contains illegal characters. You may only use letters in your name.<br><br>";
    } 
return error;
}

function checkZip (str){
var error = "";
if (str == "") {
   error = "Your zipcode is a required field.<br><br>";
}
    var legalChars =new RegExp(/^[0-9 ]+$/i);
    if (str.length < 5) {
       error = "Zipcode must be 5 numbers long. If less than 5 numbers...lead with zeros<br><br>";
    }
    else if (legalChars.test(str)) {
    }
    else
    {
		error = "Zipcode contains illegal characters. You may only use numbers in your zipcode.<br><br>";
    } 
    if (str.length > 5) {
       error = "Zipcode has too many numbers.  Zipcode must be 5 numbers long.<br><br>";
    }
return error;
}

function checkUsername (str) {
var error = "";
if (str == "") {
   error = "You didn't enter a username.<br><br>";
}
    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((str.length < 4) || (str.length > 15)) {
       error = "Your username must be between 4-15 letters long.<br><br>";
    }
    else if (illegalChars.test(str)) {
    error = "The username contains illegal characters. You may only use letters, numbers, and underscores in your username.<br><br>";
    } 
return error;
}       


// non-empty textbox

function isEmpty(str) {
var error = "";
  if (str.length == 0) {
     error = "The mandatory text area has not been filled in.<br><br>"
  }
return error;	  
}

// was textbox altered

function isDifferent(str) {
var error = ""; 
  if (str != "Can\'t touch this!") {
     error = "You altered the inviolate text area.<br><br>";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.<br><br>";
    }
return error;
}

// valid selector from dropdown list

function checkDropdownM(choice) {
var error = "";
    if (choice == "--") {
    error = "You didn't choose a Year option from the drop-down list.<br><br>";
    }    
return error;
}    

function checkDropdownD(choice) {
var error = "";
    if (choice == "--") {
    error = "You didn't choose a Day option from the drop-down list.<br><br>";
    }    
return error;
}    

function checkDropdownY(choice) {
var error = "";
    if (choice == "--") {
    error = "You didn't choose a Month option from the drop-down list.<br><br>";
    }    
return error;
}    