
function addNewDepartment()
{
	
	var deptVal = $('newDepartment').value;

	if( deptVal.length > 0 )
	{
		// save the new value
        var ajax = new Ajax.Updater(
            'divDepartment',
			'/src/department.php',
			{
				method: 'get',
				parameters: 'dept=' + deptVal
			});
		Element.toggle('hlAddDepartment');
		Element.toggle('departmentForm');
	}
	else
	{
		alert('Please enter a Department to Add.');
	} // end if
} // end addNewDepartment

function redrawDepartments(originalRequest)
{
	var elements = originalRequest.responseText;

	var sel = $('fldDepartment');
	
    sel.innerHTML = elements;
}

function addNewCategory()
{
	
	var catVal = $('newCategory').value;

	if( catVal.length > 0 )
	{
		
		// save the new value
        var ajax = new Ajax.Updater(
            'divCategory',
			'/src/category.php',
			{
				method: 'get',
				parameters: 'cat=' + catVal
			});
			Element.toggle('hlAddCategory');
		Element.toggle('categoryForm');
	}
	else
	{
		alert('Please enter a Category to Add.');
	} // end if
} // end addNewDepartment

function redrawCategories(originalRequest)
{
	var elements = originalRequest.responseText;
	
	var sel = $('category');

	sel.innerHTML = elements;
}

function addResearchArea()
{
	
var rVal = $('newResearchArea').value;

	if( rVal.length > 0 )
	{
		// save the new value
		var ajax = new Ajax.Updater(
            'divResearchAreas',
			'/src/researcharea.php',
			{
				method: 'get',
				parameters: 'area=' + rVal
			});

		Element.toggle('hlAddResearch');
		Element.toggle('researchForm');
	}
	else
	{
		alert('Please enter a Research Area to Add.');
	} // end if
} // end addResearchArea

function redrawResearchArea(originalRequest)
{
	var elements = originalRequest.responseText;
	
	var sel = $('fldResearchAreas');

	sel.innerHTML = elements;
}

function insertDegreeForm()
{
        /*
		var degreeTable = $('degreeTable');
		var rowCount = degreeTable.rows.length-1;
		var newRow = document.createElement('tr');//degreeTable.insertRow(rowCount);
		degreeTable.tBodies[0].appendChild(newRow);


		var ajax = new Ajax.Request(
			'/src/degreeform.php',
			{
				method: 'get',
				parameters: 'renderDegreeForm=' + rowCount,
				onComplete:	function (o){
                    alert('asd' + o.responseText);
                    //newRow.outerHTML =  o.responseText;
                    degreeTable.innerHTML = o.responseText;
                }
			});

            */
        var degreeContainer = $('divDegrees');
        var degreeCount = parseInt($('degreeCount').value);

        var newId = parseInt(degreeCount) + 1;

        var ajax = new Ajax.Request(
			'/src/degreeform.php',
			{
				method: 'get',
				parameters: 'renderDegreeForm=' + newId,
				onComplete:	function (o){
                    var degreeContainer = $('divDegrees');
                    var newDegree = document.createElement('div');

                    newDegree.innerHTML = o.responseText;
                    degreeContainer.appendChild(newDegree);
                    degreeCount++;
                    $('degreeCount').value = degreeCount;
                }
			});

} // end insertNewDegreeForm()


function drawForm(originalRequest)
{
	alert(originalRequest.responseText);
}

function displayLogin()
{
	if( $('hasLoginFalse').checked )
	{
		Element.hide('loginForm');
	}
	else if( $('hasLoginTrue').checked )
	{
		Element.show('loginForm');
	}
} // end displayLogin()

function displayChangeLogin()
{
	if( $('changeLoginFalse').checked )
	{
		Element.hide('loginForm');
	}
	else if( $('changeLoginTrue').checked )
	{
		Element.show('loginForm');
	}
} // end displayChangeLogin()





function TR_OnMouseOver(srcEle) {
if (srcEle.style.backgroundColor != "highlight")
srcEle.style.backgroundColor = "BlanchedAlmond";
}

function TR_OnMouseOut(srcEle) {
if (srcEle.style.backgroundColor == "BlanchedAlmond")
srcEle.style.backgroundColor = "";
}

function confirmDeleteGeneric()
{
	var agree = confirm("Are you sure you want to permanently remove this item?");
	if( agree )
	{
		return true;
	}
	else
	{
		return false;
	}
} // end function confirmDeleteGeneric()

function testUsername(name, userid)
{

	var ajax = new Ajax.Request(
	'/src/misc.php',
	{
		method: 'get',
		parameters: 'action=validuser&userid=' + userid + '&username=' + name,
		onComplete:	function (o){
				
			var field = $('username');
			
			if( o.responseText != 0 )
			{				
				field.value = '';
				alert('The username is already in use. Please try again.');
				field.focus();
				field.style.border = '2px solid red;';
			}
			else
			{
				field.style.border = '';
			}
		} 		
	});	
} // end validateuniqueUsername()

function validateStudentForm()
{
	var errMessage = "";
	var department = $('fldDepartment');
	
	if( document.studentForm.fldFirstName.value.length == 0 )
	{
		showError(document.studentForm.fldFirstName);
		errMessage += "<li>Please enter the First Name.";
	}

	if( document.studentForm.fldLastName.value.length == 0 )
	{
		showError(document.studentForm.fldLastName);
		errMessage += "<li>Please enter the Last Name</li>";
	}

	if( document.studentForm.fldEmail.value.length == 0 )
	{
		showError(document.studentForm.fldEmail);
		errMessage += "<li>Please enter the E-mail Address.</li>";
	}
	else
	{
		// make sure the email is properly formatted
		var emailCheckStatus = emailCheck(document.studentForm.fldEmail.value);
		if( !(emailCheckStatus === true) )
		{
			showError(document.studentForm.fldEmail);
			errMessage += "<li>" + emailCheckStatus + "</li>";
		}
		
	} // end email check
	
	if( !(department.selectedIndex > 0) )
	{
		showError(department);
		errMessage += "<li>Please select a Department.</li>";
	}

	if( errMessage.length > 0 )
	{
		var messageDiv = $('serverMessage');
		
		messageDiv.innerHTML = 'The following errors were found:<br /><ul>' + errMessage + '</ul>';
		Element.scrollTo(messageDiv);
		return false;
	} // end 

	document.studentForm.submit();
	return true;
}

function validateLinkForm()
{
	var errMessage = "";
	var category = $('category');
	var department = $('fldDepartment');

	// get all of the required fields
	if( document.linkForm.link.value.length == 0 )
	{
		showError(document.linkForm.link);
		errMessage += "<li>Please enter the Title of the Link.";
	}
	
	// get all of the required fields
	if( document.linkForm.url.value.length == 0 )
	{
		showError(document.linkForm.url);
		errMessage += "<li>Please enter the URL of the Link.";
	}
	else
	{
		// make sure the url is in the proper format
		if( !isUrl(document.linkForm.url.value) )
		{
			showError(document.linkForm.url);
			errMessage += "<li>The URL is not in the correct format. (ex. http://www.google.com)";
		}
	} 
	
	if( !(category.selectedIndex > -1) )
	{
		showError(category);
		errMessage += "<li>Please select a Category.</li>";
	}
	
	if( !(department.selectedIndex > -1) )
	{
		showError(department);
		errMessage += "<li>Please select a Department.</li>";
	}
	
	if( errMessage.length > 0 )
	{
		var messageDiv = $('serverMessage');
		
		messageDiv.innerHTML = 'The following errors were found:<br /><ul>' + errMessage + '</ul>';
		Element.scrollTo(messageDiv);
		return false;
	} // end 

	document.linkForm.submit();
	return true;

} // end validateLinkForm()

function validateUserForm()
{
	var errMessage = "";
	
	// get all of the required fields
	if( document.userForm.fldFirstName.value.length == 0 )
	{
		showError(document.userForm.fldFirstName);
		errMessage += "<li>Please enter the First Name.";
	}

	if( document.userForm.fldLastName.value.length == 0 )
	{
		showError(document.userForm.fldLastName);
		errMessage += "<li>Please enter the Last Name</li>";
	}

	if( document.userForm.fldEmail.value.length == 0 )
	{
		showError(document.userForm.fldEmail);
		errMessage += "<li>Please enter the E-mail Address.</li>";
	}
	else
	{
		// make sure the email is properly formatted
		var emailCheckStatus = emailCheck(document.userForm.fldEmail.value);
		if( !(emailCheckStatus === true) )
		{
			showError(document.userForm.fldEmail);
			errMessage += "<li>" + emailCheckStatus + "</li>";
		}
		
	} // end email check
	
	if( errMessage.length > 0 )
	{
		var messageDiv = $('serverMessage');
		
		messageDiv.innerHTML = 'The following errors were found:<br /><ul>' + errMessage + '</ul>';
		Element.scrollTo(messageDiv);
		return false;
	} // end 

	document.userForm.submit();
	return true;
}

function showError(el)
{
	Element.addClassName(el,'errorField');
} // end showError()

function clean(el)
{
	Element.removeClassName(el,'errorField');
} // end clean()

function isUrl(item) 
{
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(item)) 
    {
        return false;
    }
    else
    {
    	return true;
    }
} // isUrl



function emailCheck (emailStr) {
	
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	
	var checkTLD=1;
	
	/* The following is the list of known TLDs that an e-mail address must end with. */
	
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	
	var emailPat=/^(.+)@(.+)$/;
	
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address.
	These characters include ( ) < > @ , ; : \ " . [ ] */
	
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	/* The following string represents the range of characters allowed in a
	username or domainname.  It really states which chars aren't allowed.*/
	
	var validChars="\[^\\s" + specialChars + "\]";
	
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	
	var quotedUser="(\"[^\"]*\")";
	
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	/* The following string represents an atom (basically a series of non-special characters.) */
	
	var atom=validChars + '+';
	
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	
	var word="(" + atom + "|" + quotedUser + ")";
	
	// The following pattern describes the structure of the user
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	
	/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */
	
	// alert("Email address seems incorrect (check @ and .'s)");
	// return false;
	return "The Email address is incorrect (check @ and .'s).";
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	
	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	//alert("Ths username contains invalid characters.");
	//return false;
	return "The username part of the Email address contains invalid characters.";
	   }
		 }
		 for (i=0; i<domain.length; i++) {
		 if (domain.charCodeAt(i)>127) {
		// alert("Ths domain name contains invalid characters.");
	 	// return false;
		return "The domain name of the Email address contains invalid characters.";
	}
	}
	
	// See if "user" is valid
	
	if (user.match(userPat)==null) {
	
	// user is not valid
	
	// alert("The username doesn't seem to be valid.");
	// return false;
	return "The username is not valid for the Email Address.";
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	
	// this is an IP address
	
	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	//alert("Destination IP address is invalid!");
	//return false;
	return "The Destination IP for the Email Address is invalid.";
	   }
	 }
	 return true;
	 }
	
	 // Domain is symbolic name.  Check if it's valid.
	
	 var atomPat=new RegExp("^" + atom + "$");
	 var domArr=domain.split(".");
	 var len=domArr.length;
	 for (i=0;i<len;i++) {
	 if (domArr[i].search(atomPat)==-1) {
		//alert("The domain name does not seem to be valid.");
	 //return false;
	return "The Domain for the Email Address is invalid.";
	}
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding
	the domain or country. */
	
	if (checkTLD && domArr[domArr.length-1].length!=2 &&
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	return "The address must end in a well-known domain or two letter " + "country.";
	// alert("The address must end in a well-known domain or two letter " + "country.");
	// return false;
	}
	
	// Make sure there's a host name preceding the domain.
	
	if (len<2) {
	//alert("This address is missing a hostname!");
	//return false;
	return "The Email Address is missing a hostname.";
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}
