// FRANKIE'S JAVASCRIPT LIBRARY
// for use by the Center for Talented Youth
// http://ctyjhu.org
// Last updated by Frank Uy 2003/06/02
// http://spamreaper.org/frankie
// if you borrow this code, please credit me

// I use the current date so often, it may as well be global.
var now = new Date();

// popwin() opens a small new window. Note: NO spaces in the window specs
function popwin(page)	{
	var Popwin = window.open(page, 'popwin', 'width=600,height=400,scrollbars,resizable,location,menubar');
	Popwin.focus();
	return Popwin;
} // end popwin()

function manageEnrollmentLinkSubmit(subName)
{
//	document.forms[0].frmLinkClicked.value = subName;
	document.forms["mainform"].frmLinkClicked.value = subName;
//	alert(document.forms[0].frmLinkClicked.value);
	document.forms["mainform"].submit();
}

// popbox() a little square popup with the chrome removed.
function popbox(page)	{
	var Popbox = window.open(page, 'popbox', 'width=400,height=400,resizable,scrollbars');
	Popbox.focus();
	return Popbox;
} // end popbox()

// poptext() opens a little popbox with aribtrary html of your choice.
function poptext(which) {
if (which) {
	var Popup = window.open('', 'popbox', 'width=400,height=200,resizable');
	Popup.document.open();
	Popup.document.write(which);
	Popup.document.close();
	Popup.focus();
//	return Popup;
} // end if
} // end poptext()

// jumpto() select box navigation system
function jumpto(selector)	{
	var the_url = selector.options[selector.selectedIndex].value;
	selector.selectedIndex = 0;
	if (the_url)	window.location.href = the_url;
} // end jumpto()

// checkEadd() checks a form field for name@domain.suffix
// If e_add is bad, focuses on field and returns false
function checkEadd(e_add)	{
if (e_add.value.length) 	{
	var is_there_at = e_add.value.indexOf('@');
	var find_period = e_add.value.lastIndexOf('.');
	var find_suffix = e_add.value.length - find_period;
	if (is_there_at < 2)	{
		alert("Your address is incomplete. \n A COMPLETE internet e-mail address \n has a user name, an @ symbol, \n and a service provider, \n such as \'thx1138@aol.com\' ");
		e_add.select();
		return false;
	}	// end if(is_there_at)
	else if (find_period < is_there_at + 3)	{
		alert("Your address is incomplete. \n A COMPLETE internet e-mail address \n includes a host name \n such as \'jhu.edu\' or \'aol.com\' ");
		e_add.select();
		return false;
	}	// end if()
	else if (find_suffix < 3)	{
		alert("Your address is incomplete. \n A COMPLETE internet e-mail address \n ends in a suffix \n such as \'.com\' or \'.edu\' ");
		e_add.select();
		return false;
	}	// end if()
	else	return true;
} // end if(length)
else	return null;
} //end checkEadd()

// isNum() checks a TEXT field for numeric input, alerts if not. Custom message optional.
function isNum(which, message)	{
if (!message)
	message = 'You must enter a number value in this field.';
if (which.value.length)	{
//	which.value = parseInt(which.value, 10);
	if (isNaN(which.value))	{
		alert(message);
		which.focus();
		return false;
	} // end if(isNaN)
	else return true;
} // end if(length)
else return false;
} // end isNum()

// isYear() forces a Y2K compliant four digit year in a field. Should work up to year 9999.
// relies on now
function isYear(which)	{
if (isNum(which))	{
	nowyear = now.getFullYear();
	var century = nowyear - (nowyear%100);
	if ((which.value < 100) || (which.value > nowyear+1))	{
		var yy = Math.abs(which.value%100);
		which.value = (yy <= nowyear%100) ? (century+yy) : (century-100+yy);
	} // end if(century)
	return true;
} // end if(isNum)
	else return false;
} // end isYear()

// TwoDigit() convert the last two digits of an integer to a string
function TwoDigit(number)	{
	if (number)	{
		var result = (Math.abs(number) % 100).toString();
		if (result.length==1)	result = "0" + result;
		return result;
	}
	else	return null;
} // end TwoDigit()

// uncheck() go through a list of buttons and uncheck them
function uncheck(mylist)	{
	for (var i=0; i<mylist.length; i++)
		mylist[i].checked = false;			
} // end uncheck()

// makeMMDDYY() converts 3 numerical values into old style non-compliant date string.
// returns MMYY if day is omitted, returns null if others fail.
function makeMMDDYY(year, month, day)	{
	var yy = TwoDigit(year);
	var mm = TwoDigit(month);
	var dd = TwoDigit(day);
	if (month && day && year)
		return (mm + dd + yy);
	else if (month && year)
		return (mm + yy);
	else
		return '';
} // end makeMMDDYY()

// buildDate() converts yyyy mm dd into a javascript Date, mm and dd are optional
function buildDate(yyyy, mm, dd)	{
	var month = (isNaN(mm) || (mm<1) || (mm>12)) ? 11 : (mm - 1);
	var day = (isNaN(dd) || (dd<1) || (dd>31)) ? 31 : dd;
	return new Date(yyyy, month, day, 20, 0, 0, 0);
} // end buildDate()

// coppa() verifies parental permission for applicants age 12 or less
// relies on now
function coppa(bdate)	{
	var cutoff = 13 * 1000 * 3600 * 24 * 365.25;
	if (now.getTime() - bdate.getTime() <= cutoff)	{
		if (!confirm('According to US law, information about young children\nmay only be collected with parental permission.\nThe parent or guardian must click OK to continue this form.'))
			location.replace('./');
	} // end if()
} // end coppa()

// ********** END OF cde-library.js **********
