// 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

//THIS FUNCTION, WITH ONLY THE NECESSARY WHY += SECTIONS, 
//SHOULD BE PASTED AT THE TOP OF THE FILE WITH THE FORM THAT NEEDS VALIDATION
/*
function checkWholeForm(theForm) {
    var why = "";
    why += checkID(theForm.customer_id.value);
    why += checkEmail(theForm.e_mail.value);
    why += checkDropdown(theForm.pub1_status.selectedIndex);
//    why += checkPhone(theForm.phone.value);
//    why += checkPassword(theForm.password.value);
//    why += isEmpty(theForm.notempty.value);
//    why += isDifferent(theForm.different.value);
    for (i=0, n=theForm.radios.length; i<n; i++) {
        if (theForm.radios[i].checked) {
            var checkvalue = theForm.radios[i].value;
            break;
        } 
    }
    why += checkRadio(checkvalue);
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}
*/ 


function checkEmail (strng) {
    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "The e-mail address you entered is not valid.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The e-mail address you entered contains illegal characters.\n";
       }
    }
	var error="";
	if (strng == "") {
	   error = "A valid e-mail address is required.\n";
	}

return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.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-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[^a-zA-Z0-9_]/; // allow only letters and numbers
    
    if ((strng.length < 1) || (strng.length > 20)) {
       error = "The password must be between 1 and 20 characters long.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters. Please use only letters, numbers and underscores (_).\n";
    } 
/*    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }*/  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkID (strng) {
var error = "";
if (strng == "") {
   error = "The Customer ID is required.\n";
}


//    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if (strng.length > 7) {
       error = "The Customer ID can be no longer than 7 numerical characters.\n";
    }
//    else if (illegalChars.test(strng)) {
//allow only a number
    else if (isNaN(strng)) {
	    error = "The Customer ID must be a number only.\n";
    } 
return error;
}       

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "You must enter a username.\n";
}


    var illegalChars = /[^a-zA-Z0-9_@.-]/; // allow letters, numbers, and underscores
    if (strng.length > 75) {
       error = "The username can be no longer than 75 characters.\n";
    }
    else if (illegalChars.test(strng)) {
		error = "The username contains illegal characters.\nPlease enter only letters, numbers, dashes (-) and underscores (_).";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng, field_name) {
var error = "";
  if (strng.length == 0) {
     error = "The ";
	 error += field_name;
	 error += " is required.\n";
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice, drop_name) {
var error = "";
    if (choice == 0) {
    error = "The ";
	error += drop_name;
	error += " is required.\n";
    }    
return error;
}    

