
// ---------------------------------------- //

// Return the item that does not pass validation.
function ValidateEarlyAlert( form )
{
	// Required Fields
	if( !checkInput( form.StudentName, "Please enter the Student's Name." ) ) return form.StudentName;
	if( !checkInput( form.StudentID, "Please enter the Student Identification Number." ) ) return form.StudentID;
	if( !checkInteger( form.StudentID, "Please enter the Student Identification Number. (only 0-9 allowed)" ) ) return form.StudentID;
	if( form.StudentPhone.value.trim() != "" && !checkPhone( form.StudentPhone, "Please enter the Student Phone in the correct format. (i.e. xxx-xxx-xxxx)" ) ) return form.StudentPhone;
	if( !checkInput( form.Course, "Please enter the Course Title and Section Number." ) ) return form.Course;
	if( !checkInput( form.Referrer, "Please enter the Referring Faculty Member." ) ) return form.Referrer;
	if( !checkInput( form.FacultyPhone, "Please enter the Faculty's Phone Extension." ) ) return form.FacultyPhone;
	if( !checkMinLength( form.FacultyPhone, "Please enter the Faculty's 4-digit Phone Extension." ) ) return form.FacultyPhone;
	if( !checkInteger( form.FacultyPhone, "Please enter the Faculty's Phone Extension." ) ) return form.FacultyPhone;
	if( !checkInput( form.FacultyEmail, "Please enter the Faculty's Email Address." ) ) return form.FacultyEmail;
	if( !checkEmail( form.FacultyEmail, "Please enter a valid email address for Faculty's Email Address." ) ) return form.FacultyEmail;
	
	return null;
}

// ---------------------------------------- //

function checkInput( obj, msg )
{
	if( obj.value.trim() == "" ) {
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

// ---------------------------------------- //

function checkEmail( obj, msg )
{
	var value = obj.value.trim();
	if (!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test( value ) ) {
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

// ---------------------------------------- //

function checkPhone( obj, msg ) { 
	var phoneNo = obj.value.trim();

	if (/^\(?\d{3}\)?([ -\/\.])\d{3}([ -\/\.])\d{4}$/.test( phoneNo ) ) {   
		return true; 
	} else {
		alert(msg);
		obj.focus();
		return false; 
	} 
}

// ---------------------------------------- //

function checkLength( obj, len, msg ) {
	if( obj.value.length > len ) {
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

// ---------------------------------------- //

function checkMinLength( obj, len, msg ) {
	if( obj.value.length < len ) {
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

// ---------------------------------------- //

function checkSelect( obj, def, msg )
{ // def is the default selected item of the Select box
	if( obj.selectedIndex == -1 ) {
		alert(msg);
		return false;
	}
	if( obj.options[obj.selectedIndex].value.trim() == def ) {
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

// ---------------------------------------- //

function checkPassword( obj, msg )
{	var i;
	for (i = 0; i < obj.value.length; i++)
	{
		// Check that current character is a number or letter.
		var c = obj.value.charAt(i);
		if ( ( (c >= "0") && (c <= "9") ) ||
			 ( (c >= "a") && (c <= "z") ) ||
			 ( (c >= "A") && (c <= "Z") ) ) {
			 	// This character is a valid password character
		} else {
			alert(msg);
			obj.focus();
			return false;
		}
	}
	// All characters are a number or letter.
	return true;
}

// ---------------------------------------- //

function checkInteger( obj, msg )
{   var i;
    for (i = 0; i < obj.value.length; i++)
    {   
        // Check that current character is number.
        var c = obj.value.charAt(i);
        if (((c < "0") || (c > "9"))) {
			alert(msg);
			obj.focus();
			return false;
		}
    }
    // All characters are numbers.
    return true;
}

// ---------------------------------------- //

function checkMinInteger( obj, i, msg ) {
	if( parseInt(obj.value) < i ) {
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

// ---------------------------------------- //

function compareStrings( obj1, obj2, msg ) {
	if( obj1.value !== obj2.value ) {
		alert(msg);
		obj1.focus();
		return false;
	}
	return true;
}

// ---------------------------------------- //

function checkValidDate(obj, msg) {
	var dateStr = obj.value.trim();
	var valid = true;
	
    // dateStr must be of format month day year with slashes
    // separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { valid = false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { valid = false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { valid = false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { valid = false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { valid = false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { valid = false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { valid = false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { valid = false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { valid = false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { valid = false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( dateYear.length != 4 ) { valid = false; }
    
    if ((numYear <= 0) || (numYear > 9999)) { valid = false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { valid = false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { valid = false; }
    }
	
	if(!valid) {
		alert(msg);
		obj.focus();
		return false;
	}
	
    // date is valid
    return true;
}

// ---------------------------------------- //

function checkMinDate(obj, days, msg) {
	// Extract month, day, and year from obj.value
	var date = obj.value;
	var slash1 = date.indexOf("/");
	var month = date.substring(0, slash1)
	var dateAndYear = date.substring(slash1+1, date.length);
	var slash2 = dateAndYear.indexOf("/");
	var day = dateAndYear.substring(0, slash2);
	var year = dateAndYear.substring(slash2+1, dateAndYear.length);
	
	// Assign minDate and the newDate
	var curDate = new Date();
	var minDate = new Date();
	minDate.setDate(curDate.getDate() + days);
	var newDate = new Date();
	newDate.setFullYear(year, month - 1, day);
	
	if(newDate < minDate) {
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

// ---------------------------------------- //

function checkValidTime(obj, msg){
	var time = obj.value.trim();
	
	// /^\(?\d{3}\)?([ -\/\.])\d{3}([ -\/\.])\d{4}$/.test( phoneNo )
	if (!/^(0?[1-9]|10|11|12):[0-5][0-9]\s*[a|A|p|P][m|M]$/.test( time ) ) {
		alert(msg);
		obj.focus();
		return false;
	}
	return true;
}

// ---------------------------------------- //

String.prototype.trim = function() {
	// skip leading and trailing whitespace
	// and return everything in between
	var x=this;
	x=x.replace(/^\s*(.*)/, "$1");
	x=x.replace(/(.*?)\s*$/, "$1");
	return x;
}