// JavaScript Document
// 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 (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.<br>";
}

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

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.<br>";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.<br>";
    } 
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.<br>";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 8) || (strng.length > 10)) {
       error = "The password is the wrong length.<br>";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.<br>";
    } 
    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.<br>";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng,l)
{
var error = "";
if (strng == "") {
   error = "<br>-You didn't enter a "+l+".";
}


    //var illegalChars =/\W/; // allow letters, numbers, and underscores
    /*if (strng.length < 4) {
       error = "<br>-The "+l+" should have atleast 4 chars.";
    }*/
   /* if (illegalChars.test(strng)) {
    error = "<br>-The "+l+" contains illegal characters.";
    }*/ 
return error;
}       


// non-empty textbox
/*
function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "The mandatory text area has not been filled in.<br>"
  }
return error;	  
}
*/
// was textbox altered

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

// exactly one radio button is chosen

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

// valid selector from dropdown list

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

function checkAddress(str){
  var error = "";
  if (str.length < 5 ) {
     error = "Address should have atleast 5 characters.<br>"
  }
return error;	  
}

function isEmpty(str,l){
	var error='';
	if(str.length == 0){
			error = l+" should be entered.<br>";
	}
		return error;	
}

//match password
function matchPassword(strng1, strng2){
	var error ="";
	if(strng1 != strng2){
			error = 'Passwords mismatch.<br>';
	}
	return error;
}
