// JavaScript Document

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

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateLoginPassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
   
    if (fld.value == "" ) {
        fld.style.background = 'Yellow';
        error = "Please enter your password.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 15)) {
        error = "The password is the wrong length. Please enter a password 6 to 15 characters.\n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

function validatePassword(fld, fld2) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
   
    if (fld.value == "" || fld2.value == "") {
        fld.style.background = 'Yellow';
        error = "Please enter a password and confirm it.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 15)) {
        error = "The password is the wrong length. Please enter a password 6 to 15 characters.\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 if (fld.value != fld2.value) {
		error = "The passwords do not match. Please try again.\n";
		fld.sytle.background = 'Yellow';
		fld2.sytle.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   

function validateName(fld) {
	var error ="";
	if (fld.value =="") {
		fld.style.background = 'Yellow';
		error = "Please enter your name.\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

function validateFillOut (fld) {
	var error = "";
	if (fld.value == "") {
		fld.style.background = 'Yellow';
		error = "All fill in all the indicated fields.\n";
	} else {
		fld.style.background = 'White';
	}
	return error = "";
}

function validateDate(m,d,y) {
  var dateNow = new Date();
  var yearNow = dateNow.getFullYear();
  var error = "";
  if ((m.value < 1) || (m.value > 12)) {
	  m.style.background = 'Yellow';
	  error = "Please enter a valid month.\n";
  } else if ((d.value < 1) || (d.value > 31)) {
	 d.style.background = 'Yellow';
	 error = "Please enter a valid day.\n";
  } else if ((y.value < (yearNow - 100)) || (y.value > (yearNow) - 5)) {
	  y.style.background = 'Yellow';
	  error = "Please enter a valid year.\n";
  } else {
	 y.style.background = 'White';
  }
  return error; 
}

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

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateName(theForm.fullName);
  reason += validateEmail(theForm.eMail);
  reason += validateDate(theForm.birthMonth, theForm.birthDay, theForm.birthYear);
  reason += validatePassword(theForm.pwd, theForm.pwd2);
  //reason += validatePhone(theForm.phone);
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

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

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  //reason += validateName(theForm.fullName);
  reason += validateEmail(theForm.eMail);
  //reason += validateDate(theForm.birthMonth, theForm.birthDay, theForm.birthYear);
  reason += validateLoginPassword(theForm.pwd);
  //reason += validatePhone(theForm.phone);
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

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

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  //reason += validateName(theForm.fullName);
  reason += validateEmail(theForm.eMail);
  //reason += validateDate(theForm.birthMonth, theForm.birthDay, theForm.birthYear);
  // reason += validateLoginPassword(theForm.pwd);
  //reason += validatePhone(theForm.phone);
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

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

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  //reason += validateName(theForm.fullName);
  // reason += validateEmail(theForm.eMail);
  reason += validateFilledOut(theForm.venuename);
  reason += validateFilledOut(theForm.eventname);
  reason += validateFilledOut(theForm.songs);
  reason += validateFilledOut(theForm.contactname);
  reason += validateFilledOut(theForm.contactphone);
  reason += validateDate(theForm.eventMonth, theForm.eventDay, theForm.eventYear);
  // reason += validateLoginPassword(theForm.pwd);
  //reason += validatePhone(theForm.phone);
  
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

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

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  //reason += validateName(theForm.fullName);
  //reason += validateEmail(theForm.eMail);
  //reason += validateDate(theForm.birthMonth, theForm.birthDay, theForm.birthYear);
  reason += validateLoginPassword(theForm.pwd);
  //reason += validatePhone(theForm.phone);
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

