function validateEnotificationForm(theForm) {
var reason = "";
var i;
var selected = "no";
var ans;


  reason += validateEmpty(theForm.FirstName);
  reason += validateEmpty(theForm.LastName);
  reason += validateEmpty(theForm.Barcode);
  reason += validateEmail(theForm.email1,theForm.email2);
  
  if (!(theForm.request[0].checked) && !(theForm.request[1].checked) && !(theForm.request[2].checked)) {
   reason+="Are you starting or stopping email requests or updating your email address?";
  }
  
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
	
  return true;
}

function validateRoomresForm(theForm) {
var reason = "";

	reason += validateDate(theForm.programdate);
	reason += validateTime(theForm.programtime);
	reason += validateTime(theForm.starttime);
	reason += validateTime(theForm.endtime);
	reason += validateEmpty(theForm.progdesc);
	reason += validateEmpty(theForm.orgname);
	reason += validateEmpty(theForm.addr);
	reason += validateEmpty(theForm.contact);
	reason += validateEmpty(theForm.layout);

	if (validatePhone(theForm.dayphone) && validatePhone(theForm.evephone)) {
		
		reason += "You must enter either a day or evening phone number.\n";
	}

	if ( theForm.rules.checked == false ) {
		reason += "You must agree to the terms of use specified in Library Policy 3.5 in order to submit your request.\n";
	} 
	
	if (reason != "") {
      alert("Some fields need correction:\n\n" + reason);
    return false;
    }
	
  return true;
}

function checkRadio(checkvalue) {
var error = "";
  if (!(checkvalue)) {
       error = "Please check a radio button.\n";    
	}  
  if (error != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n";
    } else {
        fld.style.background = 'White';
    }
   return error;      
}

function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld1,fld2) {
    var error="";
    var tfld1 = trim(fld1.value);                        // value of field with whitespace trimmed off
    var tfld2 = trim(fld2.value);                        // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\] ]/ ;
    
    if (!(tfld1 == tfld2)) {
			error = "Two different email addresses were entered.\n";
	}
	else if (tfld1 == "") {
        fld1.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(fld1.value)) {              //test email for illegal characters
        fld1.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld1.value.match(illegalChars)) {
        fld1.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld1.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10) && !(stripped.length == 7)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
	
    } else {
		error = "";
		fld.style.background = 'White';
	}
    return error;
}

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please choose whether your want to start or stop email notifications.\n";
    }
return error;    
}

function confirmstart() {
		msg="By making this choice, I am authorizing the Clifton Park-Halfmoon Public Library to send notices via email instead of by telephone. This includes notices for items requested and items that are due soon.\n\n" +
			  "I understand that I may receive informational messages from the Clifton Park-Halfmoon Public Library, such as changes in our hours or operation or special library programs.";
	
	alert(msg);
}
function confirmstop() {
		msg="By making this choice, I am authorizing the Clifton Park-Halfmoon Public Library to stop sending notices via email.  I wish to receive requested item notifications by telephone.";
	
	alert(msg);
}

function validateDate(fld) {
	var error="";
    // regular expression to match required date format
    //re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	var re = /^[01]?[0-9]\/[0-3]?[0-9]\/[12][90][0-9][0-9]$/;
    if(fld.value != '' && !fld.value.match(re)) {
      error = "The program date is not in the proper format.\n";
      fld.style.background = 'Yellow';
	}
	else {
		error="";
		fld.style.background = 'White';
	}
    return error;
  }
  
 function validateTime(fld) {
	 // regular expression to match required time format
    var re = /^\d{1,2}:\d{2}([ ][apAP][mM])$/;

    if(fld.value != '' && !fld.value.match(re)) {
      error = "The time is not in the proper format.\n";
	  fld.style.background = 'Yellow';
    }
	else if  (trim(fld.value) == "" ) {
		error = "Time is required.\n";
		fld.style.background = 'Yellow';
	}
	else {
		error="";
		fld.style.background = 'White';
	}
	
	return error;
 }