// This function initiailizes the form when it is loaded.
// Set focus to Student ID Field
function focus(){
	var studentID = document.borderCountyForm.StudentID;
	studentID.focus();
}
// Set focus to Student ID2 Field
function focus2(){
	var studentID2 = document.borderCountyQueryForm.StudentID2;
	studentID2.focus();
}

//Alpha and Numeric functions
var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) {
	if (parm == "") {
		return true;
	}
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1) {
			return false;
		}
	}
	return true;
}

function isNumber(parm) {
	return isValid(parm,numb);
}
function isLower(parm) {
	return isValid(parm,lwr);
}
function isUpper(parm) {
	return isValid(parm,upr);
}
function isAlpha(parm) {
	return isValid(parm,lwr+upr);
}
function isAlphanum(parm) {
	return isValid(parm,lwr+upr+numb);
}
function isStudentID(parm) {
	return isValid(parm,'@' + numb);
} 

//Strip blank spaces from fields
function stripBlanks(fld) {
	var result = "";
	var c = 0;
	for (i=0; i<fld.length; i++) {
		if (fld.charAt(i) != " " || c > 0) {
			result += fld.charAt(i);
		}
		if (fld.charAt(i) != " ") {
			c = result.length;
		}
	}
	return result.substr(0,c);
}

//Only One @ Symbol
function oneOnly(parm,chr,must) {
	var atPos = parm.indexOf(chr,0);
	if (atPos == -1) {
		return !must;
	}
	if (parm.indexOf(chr, atPos + 1) > - 1) {
		return false;
	}
	return true; 
} 

//function to validate @ symbol position
function studentIDSymbol(parm, AT) {
	var atPosition = parm.indexOf(AT);
	if (atPosition != 0) {
		return false; // @ symbol not in first position
	}
	return true;
}

//Validate the Fields
function validateInfo(){
	
//Variables
	var studentID = document.borderCountyForm.StudentID;
	var semester = document.borderCountyForm.Semester;
	
//Student ID Validation
	studentID.value = stripBlanks(studentID.value);
	if (studentID.value == "") {
		alert("Please insert the student's ID#.");
		studentID.focus();
		return false; // Student ID field must not be empty
	}
	if (!isStudentID(studentID.value)) {
		alert("Please insert a valid Student ID# which contains an @ symbol and numbers.");
		studentID.focus();
		return false; // Test Student ID @ and Numeric
	}
	if (!oneOnly(studentID.value,'@',true)) {
		alert("Please insert a valid Student ID# which contains an @ symbol and only one @ symbol.");
		studentID.focus();
		return false; // Student ID must contain an @ symbol and only one
	}
	if (!studentIDSymbol(studentID.value,'@')) {
		alert("Please insert a valid Student ID# which contains an @ symbol in the first position.");
		studentID.focus();
		return false; // @ symbol must be in first position
	}
	if (studentID.value.length != 9) {
		alert("The Student ID must begin with an @ symbol and must be 9 characters in length including the @ symbol.");
		studentID.focus();
		return false; // Student ID Length Validation
	}
	
//Semester Validation
	if (semester.value == "") {
		alert("Please choose a semester.");
		semester.focus();
		return false;
	}
}

//Validate the Query Fields
function validateInfoQuery(){
	
//Variables
	var studentID = document.borderCountyQueryForm.StudentID2;
	var firstName = document.borderCountyQueryForm.FirstName2;
	var lastName = document.borderCountyQueryForm.LastName2;

//Student ID Validation
	studentID.value = stripBlanks(studentID.value);
	if (studentID.value != "" && !isStudentID(studentID.value)) {
		alert("Please insert a valid Student ID# which contains an @ symbol and numbers.");
		studentID.focus();
		return false; // Test Student ID @ and Numeric
	}
	if (studentID.value != "" && !oneOnly(studentID.value,'@',true)) {
		alert("Please insert a valid Student ID# which contains an @ symbol and only one @ symbol.");
		studentID.focus();
		return false; // Student ID must contain an @ symbol and only one
	}
	if (studentID.value != "" && !studentIDSymbol(studentID.value,'@')) {
		alert("Please insert a valid Student ID# which contains an @ symbol in the first position.");
		studentID.focus();
		return false; // @ symbol must be in first position
	}
	if (studentID.value != "" && studentID.value.length != 9) {
		alert("The Student ID must begin with an @ symbol and must be 9 characters in length including the @ symbol.");
		studentID.focus();
		return false; // Student ID Length Validation
	}
	
//First Name Validation
	firstName.value = stripBlanks(firstName.value);
	if (firstName.value != "" && !isAlpha(firstName.value)){
		alert("The student's first name cannot contain numbers.");
		firstName.focus();
		return false;
	}
	
//Last Name Validation
	lastName.value = stripBlanks(lastName.value);
	if (lastName.value != "" && !isAlpha(lastName.value)){
		alert("The student's last name cannot contain numbers.");
		lastName.focus();
		return false;
	}
}



//Validate the Insert Fields
function validateInfoInsert(){
	
//Variables
	var studentID = document.borderCountyForm.StudentID;
	var firstName = document.borderCountyForm.FirstName;
	var lastName = document.borderCountyForm.LastName;
	var semester = document.borderCountyForm.Semester;
	var approvedForSemesters = document.borderCountyForm.ApprovedForSemesters;
	var newRecertify = document.borderCountyForm.NewRecertify;
	var decision = document.borderCountyForm.Decision;
	var references1 = document.borderCountyForm.References1;

//Student ID Validation
	studentID.value = stripBlanks(studentID.value);
	if (studentID.value == "") {
		alert("Please insert the student's ID#.");
		studentID.focus();
		return false; // Student ID field must not be empty
	}
	if (!isStudentID(studentID.value)) {
		alert("Please insert a valid Student ID# which contains an @ symbol and numbers.");
		studentID.focus();
		return false; // Test Student ID @ and Numeric
	}
	if (!oneOnly(studentID.value,'@',true)) {
		alert("Please insert a valid Student ID# which contains an @ symbol and only one @ symbol.");
		studentID.focus();
		return false; // Student ID must contain an @ symbol and only one
	}
	if (!studentIDSymbol(studentID.value,'@')) {
		alert("Please insert a valid Student ID# which contains an @ symbol in the first position.");
		studentID.focus();
		return false; // @ symbol must be in first position
	}
	if (studentID.value.length != 9) {
		alert("The Student ID must begin with an @ symbol and must be 9 characters in length including the @ symbol.");
		studentID.focus();
		return false; // Student ID Length Validation
	}
	
//First Name Validation
	firstName.value = stripBlanks(firstName.value);
	if (firstName.value == "") {
		alert("Please the student's first name.");
		firstName.focus();
		return false;
	}
	if (!isAlpha(firstName.value)){
		alert("The student's first name cannot contain numbers.");
		firstName.focus();
		return false;
	}
	
//Last Name Validation
	lastName.value = stripBlanks(lastName.value);
	if (lastName.value == "") {
		alert("Please the student's last name.");
		lastName.focus();
		return false;
	}
	if (!isAlpha(lastName.value)){
		alert("The student's last name cannot contain numbers.");
		lastName.focus();
		return false;
	}
	
//Semester Validation
	if (semester.value == "") {
		alert("Please choose a semester.");
		semester.focus();
		return false;
	}
	
//Approved for Semesters Validation
	if (approvedForSemesters.value == "") {
		alert("Please choose the semester(s) for which the student is approved.");
		approvedForSemesters.focus();
		return false;
	}
	
//New Recertify Status Validation
	if (newRecertify.value == "") {
		alert("Please choose the student's status.");
		newRecertify.focus();
		return false;
	}
	
//Decision Validation
	if (decision.value == "") {
		alert("Please choose a decision.");
		decision.focus();
		return false;
	}
	
//References1 Validation
	references1.value = stripBlanks(references1.value);
	if (references1.value == "") {
		alert("Please insert notes into the references section.");
		references1.focus();
		return false;
	}
}
