// FRANKIE'S JAVASCRIPT LIBRARY
// for use by the Center for Talented Youth
// http://cty.jhu.edu
// Last updated by Frank Uy 2004/03/20

// 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 Popup = window.open(page, 'popwin', 'width=600,height=400,scrollbars,resizable,location,menubar');
	Popup.focus();
	return false;
} // end popwin()

// popbox() a little square popup with the chrome removed.
function popbox(page)	{
	var Popup = window.open(page, 'popbox', 'width=400,height=400,resizable,scrollbars');
	Popup.focus();
	return false;
} // 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();
} // end if
return false;
} // end poptext()

// cdeframe() Feel the sticky power of an external site frameloader.
function cdeframe(page)	{
	top.location.href = 'http://cty.jhu.edu/ctyonline/frame.html?' + encodeURI(page);
	return false;
} // end cdeframe()

// 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.focus();
		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.focus();
		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.focus();
		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)	{
	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 **********

// ********** 1k DHTML API from dithered.com **********
// ********** heavily edited for readability **********

function gE(i,f)	{
	if(document.layers)	{
		f=(f)?f:window;
		V=f.document.layers;
		if(V[i])return V[i];
		for(W=0;W<V.length;)t=(gE(i,V[W++]));
		return t;
	}
	else if(document.all)return document.all[i];
	else return document.getElementById(i);}
function sE(e)	{
	if(document.layers)e.visibility='show';
	else e.style.visibility='visible';}
function hE(e)	{
	if(document.layers)e.visibility='hide';
	else e.style.visibility='hidden';}
function sZ(e,z)	{
	if(document.layers)e.zIndex=z;
	else e.style.zIndex=z;}
function wH(e,h)	{
	if(document.layers)	{
		e.document.write(h);e.document.close();}
	else if(e.innerHTML)e.innerHTML=h;
	else alert(h);}

// ********** END OF 1k DHTML API **********
