// JavaScript Document
/*******************
UTILITY FUNCTIONS
********************/
// day of week of month's first day
function getFirstDay(theYear, theMonth){
var firstDate = new Date(theYear,theMonth,1)
return firstDate.getDay()
}
// number of days in the month
function getMonthLen(theYear, theMonth) {
var oneDay = 1000 * 60 * 60 * 24
var thisMonth = new Date(theYear, theMonth, 1)
var nextMonth = new Date(theYear, theMonth + 1, 1)
var len = Math.ceil((nextMonth.getTime() -
thisMonth.getTime())/oneDay)
return len
}
// create array of English month names
var theMonths =
['January','February','March','April','May','June','July','August',
'September','October','November','December']

// return IE4+ or W3C DOM reference for an ID
function getObject(obj) {
var theObj
if (document.all) {
if (typeof obj == 'string') {
return document.all(obj)
} else {
return obj.style
}
}
if (document.getElementById) {
if (typeof obj == 'string') {
return document.getElementById(obj)
} else {
return obj.style
}
}
return null
}

/************************
DRAW CALENDAR CONTENTS
*************************/
// clear and re-populate table based on form's selections
function populateTable(form) {
var theMonth = form.chooseMonth.selectedIndex
var theYear =
parseInt(form.chooseYear.options[form.chooseYear.selectedIndex].text)
// initialize date-dependent variables
var firstDay = getFirstDay(theYear, theMonth)
var howMany = getMonthLen(theYear, theMonth)
// fill in month/year in table header
getObject('tableHeader').innerHTML = theMonths[theMonth] +
' ' + theYear
// initialize vars for table creation
var dayCounter = 1
var TBody = getObject('tableBody')
// clear any existing rows
while (TBody.rows.length > 0) {
TBody.deleteRow(0)
}
var newR, newC
var done=false
while (!done) {
// create new row at end
newR = TBody.insertRow(TBody.rows.length)
for (var i = 0; i < 7; i++) {
// create new cell at end of row
newC = newR.insertCell(newR.cells.length)
if (TBody.rows.length == 1 && i < firstDay) {
// no content for boxes before first day
newC.innerHTML = ''
continue
}
if (dayCounter == howMany) {
// no more rows after this one
done = true
}
// plug in date (or empty for boxes after last day)
newC.innerHTML = (dayCounter <= howMany) ?
dayCounter++ : ''
}
}
}
/*******************
INITIALIZATIONS
********************/
// create dynamic list of year choices
function fillYears() {
var today = new Date()
var thisYear = today.getFullYear()
var yearChooser = document.dateChooser.chooseYear
for (i = thisYear; i < thisYear + 5; i++) {
yearChooser.options[yearChooser.options.length] = new Option(i, i)
}
setCurrMonth(today)
}
// set month choice to current month
function setCurrMonth(today) {
document.dateChooser.chooseMonth.selectedIndex = today.getMonth()
}

// validates that the entry is formatted as an email address
function isEMailAddr(elem) {
    var str = elem.value;
    str = str.toLowerCase( );
    if (str.indexOf('@') > 1) {
        var addr = str.substring(0, str.indexOf('@'));
        var domain = str.substring(str.indexOf('@') + 1, str.length);
        // at least one top level domain required
        if (domain.indexOf('.') == -1) {
            alert('Verify the domain portion of the email address.');
            return false;
        }
        // parse address portion first, character by character
        for (var i = 0; i < addr.length; i++) {
            oneChar = addr.charAt(i).charCodeAt(0);
            // dot or hyphen not allowed in first position; dot in last
            if ((i == 0 && (oneChar == 45 || oneChar == 46))  ||
                (i == addr.length - 1 && oneChar == 46)) {
                alert('Verify the user name portion of the email address.');
                return false;
            }
            // acceptable characters (- . _ 0-9 a-z)
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 ||
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert('Verify the user name portion of the email address.');
                return false;
            }
        }
        for (i = 0; i < domain.length; i++) {
            oneChar = domain.charAt(i).charCodeAt(0);
            if ((i == 0 && (oneChar == 45 || oneChar == 46)) ||
                ((i == domain.length - 1  || i == domain.length - 2) && oneChar == 46)) {
                alert('Verify the domain portion of the email address.');
                return false;
            }
            if (oneChar == 45 || oneChar == 46 || oneChar == 95 ||
                (oneChar > 47 && oneChar < 58) || (oneChar > 96 && oneChar < 123)) {
                continue;
            } else {
                alert('Verify the domain portion of the email address.');
                return false;
            }
        }
        return true;
    }
    alert('The email address may not be correct. Please verify.');
    return false;
}
// validates that the entry is a valid phone number
function isNumber(elem) {
    var str = elem.value;
    var oneDecimal = false;
    var oneChar = 0;
    // make sure value hasn't cast to a number data type
    str = str.toString( );
    for (var i = 0; i < str.length; i++) {
        oneChar = str.charAt(i).charCodeAt(0);
        // OK for plus or minus sign as first character
        if (oneChar == 43 || oneChar == 45) {
            if (i == 0) {
                continue;
            } else {
                alert('Only the first character may be a plus sign or minus sign.');
				elem.focus();
                return false;
            }
        }
		// OK for one decimal point
        if (oneChar == 46) {
            if (!oneDecimal) {
                oneDecimal = true;
                continue;
            } else {
                alert('Only one decimal is allowed in a number.');
				elem.focus();
                return false;
            }
        }
        // characters outside of 0 through 9 not OK
        if (oneChar < 48 || oneChar > 57) {
            alert('Enter only numbers into the field.');
			elem.focus();
            return false;
        }
    }
    return true;
}

// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
    var str = elem.value;
    if(str == null || str.length == 0) {
        alert('Please fill in all required fields.');
        return false;
    } else {
        return true;
    }
}

//validation fxn for login
function validatelogin(form){
    if(isNotEmpty(form.user)){
		if(isNotEmpty(form.pass)){
			return true;
		}
    }
return false;
}

function p_result(val){
	window.open('result.php?id='+val,'prt','height=600,width=740, scrollbars=1,resizable=no,dependent=1');
}

function alt_result(val){
	window.open('change.php?id='+val,'alt','height=600,width=740, scrollbars=1,resizable=no,dependent=1');
}

function askq(elem,yr){
	if(elem.options[elem.selectedIndex].value==yr){
	getObject('ask').innerHTML = "<div align='center'><select name='what' class='contols' id='what' style='width:100px'><option value='apply_yr'>Admission</option><option value='current'>Current</option></select></div>";
	}else{	getObject('ask').innerHTML ="&nbsp;";	}
}

function checkyr(form){
	if(form.year.selectedIndex==0){
		alert('Please select a year.');
		return false;
	}else return true;
}

function checkboth(form){
	if(form.course.selectedIndex!=0){
		if(checkyr(form)){
			return true;
		}
	}else alert('Please select a course.');
return false;
}