var btxt;

document.observe ('dom:loaded', function() {
	// external links
	$$('a.ext').each (function (el) {
		el.observe ('click', extLinkHandler);
	});
	
	var theForm = document.getElementById ('ppform');
	var efield = document.getElementById ('email');
	var pfield = document.getElementById ('password');

	efield.focus();
	efield.onkeyup = efield.onblur = pfield.onkeyup = pfield.onblur = formEnable;

	// assign handler for form submission
	theForm.onsubmit = formHandler;
		
	// initialize button state
	formEnable();
});


// allow form to go or not
function formHandler() {
	var efield = document.getElementById ('email');
	var formId = this.id;

	if (btxt) {
		alert (btxt);
		efield.focus();
		return false;
	}
	return true;
}

// allow form to be submitted or not, depending on state of fields
// argument, returns: none
function formEnable() {
	var efield = document.getElementById ('email');
	var pfield = document.getElementById ('password');
	var gbtn = document.getElementById ('getbtn');

	// assume form's ready to go to start with
	var ok = true;
	
	if (!isEmail (efield.value)) {
		ok = false;
		btxt = 'Please enter a valid email address';
	}
	if (ok) {
		if (!pfield.value) {
			ok = false;
			btxt = 'Please enter the password';
		}
	}
	
	// set button accordingly
	if (ok) {
		gbtn.style.background = '#2b882d';
		gbtn.style.color = 'white';
		btxt = '';
	}
	else {
		gbtn.style.background = '#eee';
		gbtn.style.color = '#ccc';
	}
}


// external link handler
function extLinkHandler (event) {
	// stop link from being followed conventionally
	event.stop();
	// open a new window
	open (this.href);
}


// check whether email address is valid
// argument: email address
// returns: boolean
function isEmail (str) {
	// if the string's empty, no good
	if (!str) {
		return false;
	}

	// disallowed characters: if we find any of these, no good
	var iChars = ' *|,"<:>[]{}`\';()&$#%';
	for (var i = 0; i < str.length; i++) {
		if (iChars.indexOf(str.charAt(i)) != -1) {
			return false;
		}
	}
	
	// position in string of @
	var iAt = str.indexOf('@');
	// position in string of second @ (if any)
	var jAt = str.indexOf('@', iAt + 1);
	// position in string of last .
	var iDot = str.lastIndexOf('.');
	// if @ is absent or first character, or there's more than one @
	// or the last . is too near the end or too close to @,
	// no good
	if (iAt < 1 || jAt != -1 || iDot > str.length - 3 || iDot - iAt < 2) {
		return false;
	}
	
	// otherwise, fine!
	return true;
}


// mailto-hider
// arguments: text for link (or 'addr');
// number of elements before the @;
// title;
// elements separated by @ or .
// returns: nothing - writes the necessary into the page
function noSpam() {
	var link = noSpam.arguments[0];	// text for link
	var pre = noSpam.arguments[1];	// number of elements before the @
	var title = noSpam.arguments[2];	// title attribute for link

	var myat = String.fromCharCode(64);		// @
	var mydot = String.fromCharCode(46);	// .
	
	// initialise the address string
	var addr = '';
	
	// loop through the elements of the address (starting at arguments[3])
	for (var i = 3; i < noSpam.arguments.length; i++) {
		// if we've already started the string, put a separator before the next element
		if (addr) {
			// if we've reached the element numbered the same as 'pre' we need the @
			// otherwise it's a .
			addr += (i == pre + 3) ? myat : mydot;
		}
		// then add the element itself
		addr += noSpam.arguments[i];
	}
	
	// initialise a string for the whole tag (or just address)
	var str = '';
	// if the first argument isn't an empty string we do the tag
	if (link) {
		// start with the mailto: part
		str = '<a href="mailto:' + addr + '"';
		// add title if any
		if (title) {
			str += ' title="' + title + '"';
		}
		str += '>';
		// if the first argument is 'addr' we spell out the address verbatim
		// otherwise we put the text from the argument
		str += (link == 'addr') ? addr : link;
		// close the tag
		str += '<\/a>';
	}
	// first argument empty so we simply regurgitate the address as text
	else {
		str = addr;
	}
	
	// write whatever we've ended up with into the page
	document.write (str);
}


function showReel (h) {
	document.write ('<object id="NSPlay" width="240" height="' + h + '" border="0" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version%3d6,0,02,902" standby="Loading Microsoft Media Player components..." type="application/x-oleobject">\r');
	document.write ('<param name="FileName" value="http://www.the80minutemba.com/video/the_80min_mba.wmv">\r');
	document.write ('<param name="AnimationAtStart" value="true">\r');
	document.write ('<param name="TransparentAtStart" value="true">\r');
	document.write ('<param name="AutoStart" value="true">\r');
	document.write ('<param name="ShowControls" value="true">\r');
	document.write ('<param name="Volume" value="-100">\r');
	document.write ('<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" src="http://www.the80minutemba.com/video/the_80min_mba.wmv" name="NSPlay" width="240" height="' + h + '" border="0" AutoStart="true">\r');
	document.write ('<\/object>\r');
}

