// confirm delete
function confirmSubmit() {
 if (confirm("Do you really want to do this?")) return true;
 else return false;
}

// confirm delete
function confirmDelete(URL) {
	var submitValue = confirmSubmit();
	if (submitValue) {
		window.location=URL;
	}
}

// load states/provinces based on requestor's country selection
function StuStateLoad(country) {
	document.student.action="index.cfm/do/my.edit_student/country/" + country.options[country.selectedIndex].value;
	document.student.submit();
}

// load states/provinces based on requestor's country selection
function ContactStateLoad(country) {
	document.contact.action="index.cfm/do/my.edit_contact/country/" + country.options[country.selectedIndex].value;
	document.contact.submit();
}

// checks for length on the password retrieval page
function validateRetrievePassword() {
	if (document.retrievePassword.username.value=="") {
		document.retrievePassword.username.focus();
		alert("Please enter a username.");
		return false;
	} 
}

// calls the IsNumeric function to validate that the student id is numeric on the username retrieval page
function validateRetrieveUsername() {
	if (document.retrieveUsername.SyStudentID.value=="") {
		document.retrieveUsername.SyStudentID.focus();
		alert("Please enter your Student ID Number.");
		return false;
	} 
	else if (IsNumeric(document.retrieveUsername.SyStudentID.value) == false) {
		document.retrieveUsername.SyStudentID.focus();
		alert("Your Student ID must be numeric");
		return false;
	}
}

//  check for valid numeric strings
function IsNumeric(strString) {
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1) {
				blnResult = false;
		}
	}
	return blnResult;
}

// student info validation
function validateStudent(){
	var emailID=document.student.email;
	var invalid = " "; // Invalid character is a space
	var minLength = 5; // Minimum length
	var pw1 = document.student.password.value;
	var pw2 = document.student.verifyPassword.value;
	
	// first name	
	if (document.student.firstName.value=="") {
		document.student.firstName.focus();
		alert('Please enter your first name.');
	  return false; 
	}
	// last name	
	if (document.student.lastName.value=="") {
		document.student.lastName.focus();
		alert('Please enter your last name.');
	  return false; 
	}
	// email
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address.")
		emailID.focus()
		return false;
	}
	// user name	
	if (document.student.userName.value=="") {
		document.student.userName.focus();
		alert('Please enter a username.');
	  return false; 
	}
	// validate email structure
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false;
	}
	// password
	if (document.student.password.value=="") {
		document.student.password.focus();
		alert('Please enter a password.');
	  return false; 
	}
	// check for a value in both fields.
	if (pw1 == '' || pw2 == '') {
		document.student.verifyPassword.focus();
		alert('Please enter your password twice.');
		return false;
	}
	// check for minimum length
	if (document.student.password.value.length < minLength) {
		document.student.password.focus();
		alert('Your password must be at least ' + minLength + ' characters long. Try again.');
		return false;
	}
	// check for a match
	if (pw1 != pw2) {
		document.student.verifyPassword.focus();
		alert ("Your password verification did not match. Please re-enter your password.");
		return false;
	}
	// addr1
	if (document.student.addr1.value=="") {
		document.student.addr1.focus();
		alert('Please enter an address.');
	  return false; 
	}
	// city
	if (document.student.city.value=="") {
		document.student.city.focus();
		alert('Please enter a city.');
	  return false; 
	}
	// zip
	if (document.student.zip.value=="") {
		document.student.zip.focus();
		alert('Please enter a zip code.');
	  return false; 
	}
	// country
	if (document.student.country.value=="") {
		document.student.country.focus();
		alert('Please enter a country.');
	  return false; 
	}
	// phone
	if (document.student.phone.value=="") {
		document.student.phone.focus();
		alert('Please enter a phone number.');
	  return false; 
	}
}

// contact info validation
function validateContact(){
	var emailID=document.contact.email;
	
	// first name	
	if (document.contact.firstName.value=="") {
		document.contact.firstName.focus();
		alert('Please enter contact\'s first name.');
	  return false; 
	}
	// last name	
	if (document.contact.lastName.value=="") {
		document.contact.lastName.focus();
		alert('Please enter contact\'s last name.');
	  return false; 
	}
	// email
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter contact\'s email address.")
		emailID.focus()
		return false;
	}
	// validate email structure
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false;
	}
	// addr1
	if (document.contact.addr1.value=="") {
		document.contact.addr1.focus();
		alert('Please enter an address.');
	  return false; 
	}
	// city
	if (document.contact.city.value=="") {
		document.contact.city.focus();
		alert('Please enter a city.');
	  return false; 
	}
	// zip
	if (document.contact.zip.value=="") {
		document.contact.zip.focus();
		alert('Please enter a zip code.');
	  return false; 
	}
	// country
	if (document.contact.country.value=="") {
		document.contact.country.focus();
		alert('Please enter a country.');
	  return false; 
	}
	// phone
	if (document.contact.phone.value=="") {
		document.contact.phone.focus();
		alert('Please enter a phone number.');
	  return false; 
	}
}

// email validation
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail")
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail")
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Invalid E-mail")
	    return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
	   alert("Invalid E-mail")
	   return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   alert("Invalid E-mail")
	   return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
	   alert("Invalid E-mail")
	   return false
	}
	if (str.indexOf(" ")!=-1){
	   alert("Invalid E-mail")
	   return false
	}
	 return true					
}

// validates account creation
function validateInitLogin(){
	var minLength = 5; // Minimum length
	var pw1 = document.initLogin.password.value;	
	var pw2 = document.initLogin.verifyPassword.value;
	
	// user name	
	if (document.initLogin.userName.value=="") {
		document.initLogin.userName.focus();
		alert('Please enter a username.');
	  return false; 
	}
	// password
	if (document.initLogin.password.value=="") {
		document.initLogin.password.focus();
		alert('Please enter a password.');
	  return false; 
	}
	// password
	if (document.initLogin.password.value=="changeme") {
		document.initLogin.password.focus();
		alert('You must select a different password.');
	  return false; 
	}
	// check for a value in both fields.
	if (pw1 == '' || pw2 == '') {
		document.initLogin.verifyPassword.focus();
		alert('Please enter your password twice.');
		return false;
	}
	// check for minimum length
	if (document.initLogin.password.value.length < minLength) {
		document.initLogin.password.focus();
		alert('Your password must be at least ' + minLength + ' characters long. Try again.');
		return false;
	}
	// check for a match
	if (pw1 != pw2) {
		document.initLogin.verifyPassword.focus();
		alert ("Your password verification did not match. Please re-enter your password.");
		return false;
	}
}