function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

/*  I'm leaving this here for now to see if any problems arise from the new version directly below
I guess this version will swap any number of images with one function call, the only use I can see for this
is if I was using slices.  Obviously, I won't use that technique as CSS has made them obsolete.
function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}*/

function changeImages(name, value) {
	if (document.images && (preloadFlag == true))
		document.getElementsByName(name)[0].src = value;
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		applynow_down = newImage("gfx/layout/applynow_down.jpg");
		preloadFlag = true;
	}
}

function toggle(id)
{
	element = document.getElementById(id);
	
	if (element.style.display == 'none')
		element.style.display = 'block';
	else
		element.style.display = 'none';
}

function togglemenus(num)
{
	menu = document.getElementById('menu'+num);
	submenu = document.getElementById('submenu'+num);
	
	
	if (submenu.style.display == 'block') {
		submenu.style.display = 'none';
		jscss('add', menu, 'arrowup');
		activemenu = '';
		openedmenu('0');
	}
	else {
		if (activemenu) {
			document.getElementById('submenu'+activemenu).style.display = 'none';
			jscss('add', document.getElementById('menu'+activemenu), 'arrowup');
		}
		submenu.style.display = 'block';
		jscss('remove', menu, 'arrowup');
		activemenu = num;
		openedmenu(num);
	}
}

function collapsemenu(num)
{
	menu = document.getElementById('menu'+num);
	submenu = document.getElementById('submenu'+num);
	
	jscss('add', menu, 'arrowup');
	submenu.style.display = 'none';
}

function showmenu(num)
{
	menu = document.getElementById('menu'+num);
	submenu = document.getElementById('submenu'+num);
	
	jscss('remove', menu, 'arrowup');
	submenu.style.display = 'block';
}

// Initializes the X images on forms correctly if CSS is not available
// At least I think it will do that, as I'm not certain that the style attribute will be available without CSS
// Of course, if that is the case, this function does not cause any harm and introduces negligible server load
// as it's executed client side
function initFormXs()
{
	var classes = getElementsByClass("x");
	for (var i=0; i<classes.length; i++)
		if (jscss("check", classes[i], "valid"))
			classes[i].style.display = "none";
}


// Gathers all elements of a particular class name into a return array
function getElementsByClass(className)
{
	var container = [];
	var i=0;
	var j=0;

	var tags = document.getElementsByTagName("*");
	for (i=0; i<tags.length; i++)
		if (jscss("check", tags[i], className))
			container[j++] = tags[i];
	
	return container;
}

/*
	This function manages the application of classes to objects without overwriting any other classes that happen to be there
	Source: http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
*/
function jscss(a,o,c1,c2)
{
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}

function addOption(selectbox,text,value,selected)
{
var option = document.createElement("option");
option.text = text;
option.value = value;
option.selected = selected;
//selectbox.options.add(option);
selectbox.options[selectbox.options.length] = option;
}

function currentPage()
{
	var a = document.URL.split("//");
	a = (a[1] ? a[1] : a[0]).split("/");
	a = a[a.length-1].split(".");
	return a[0];
}

function adjustDays(monthID, dayID)
{
	var month = document.getElementById(monthID);
	var day = document.getElementById(dayID);
	var selected_day = day.value;
	var numdays=0;
	
	switch (month.value) {
		case "":
		case "1":
		case "3":
		case "5":
		case "7":
		case "8":
		case "10":
		case "12":	numdays = 31; break;
		case "4":
		case "6":
		case "9":
		case "11": numdays = 30; break;
		case "2": numdays = 29; break;
	}
	
	var i;
	for(i=day.options.length-1;i>=0;i--) {
		day.remove(i);
	}

	var selected = (selected_day == '');
	addOption(day,"--","",selected);
	for(i=1; i<=numdays; i++) {
		selected = (selected_day == i);
		addOption(day, i, i, selected);
	}
}

function showhide(id, show) {
	var field = document.getElementById(id);
	if (show)
		field.style.display = "inline";
	else
		field.style.display = "none";
}


/*********************************************************************************************
************************************  Mayle_bocks ********************************************
*********************************************************************************************/

// The purpose of this script is to protect e#m#ai#l addresses from harvesters. For that 
// reason, keywords are misspelled or separated with pound signs (#) to disguise them

// Takes a us#er#na#me (and an optional do#m#a#in), and it writes a "m#ai#lt#o" link
function imail(apples,doughmane,linktext){
	// if do#m#a#in is not given, it assigns it a value of "odu#dot#edu"
	doughmane = (doughmane == void 0 || doughmane == "") ? "o" + "du" + "." + "e" + "d" + "u":doughmane;
	apples = (apples == void 0 || apples == "") ? "admi" + "t":apples;
	var attt = "@";
	// A default e#m#ai#l address is admit
	if (apples == ""){
		apples = "adm" + "it";
	}
	if (!linktext)
		linktext = apples + attt + doughmane;
		
	document.write('<a href="ma' + 'il' + 'to:' + apples + attt + doughmane + '">' + linktext + '</a>');
}

window.onload = function () {
	preloadImages();
};