/*
 * 2006, Raw Ideas Pty Ltd
 * 
 * based on Lokesh Dhakar's Lightbox 2 (http://huddletogether.com/projects/lightbox2)
 * 
 * requires Scriptaculous & Prototype (script.aculo.us) as well as Peter-Paul Koch's getPageSize() & getPageScroll() (quirksmode.org)
 * 
 * Easy as pie. No documentation. Not very original code, so rip. And yes, it's all pretty hardcoded. Don't try this at home, kids!
 */

var TellAFriend = {
	formTarget: rootPath.replace(/\/$/, '') +"/tellAFriend/tell", 

	about: function(thisURL) {
			var hBody = document.getElementsByTagName("body").item(0);
			
			// create the basic tellAFriend frame:
			//	<div id="tAFOVerlay"></div>
			//	<div id="tellAFriend">
			//		<div id="tAFOuter">
			//			<div id="tAFInner">[this is where the content will go]</div>
			//		</div>
			//	</div>
			//
			// (we don't do this onload as most likely there will be far more views without invocation of this
			//  feature than people complaining that it takes 0.02 seconds for the form to load after clicking
			//  on "tell a friend"..)	
			var hOVerlay = document.createElement("div");
			hOVerlay.setAttribute('id','tAFOverlay');
			hOVerlay.style.display = 'none';
			hOVerlay.onclick = function() { TellAFriend.noDice(); return false; }
			hBody.appendChild(hOVerlay);
			
			var hTAF = document.createElement("div");
			hTAF.setAttribute('id','tellAFriend');
			hTAF.style.display = 'none';
			hBody.appendChild(hTAF);
		
			var hOuter = document.createElement("div");
			hOuter.setAttribute('id','tAFOuter');
			hTAF.appendChild(hOuter);
	
			var hInner = document.createElement("div");
			hInner.setAttribute('id','tAFInner');
			hOuter.appendChild(hInner);
			hInner.innerHTML =	"<h2>Tell a friend</h2><p>Fill out the form below to send this page to a friend.</p>"
										+"<form id='TAF' action='"+ TellAFriend.formTarget +"' method='post' onsubmit='return TellAFriend.goDo(this)')><dl>"
										+"<dt>Your name</dt><dd><input type='text' name='fromName' onkeyup=\"$('tAFFromName').innerHTML = this.value\" /></dd>"
										+"<dt>Your Email address</dt><dd><input type='text' name='fromEmail' /></dd>"
										+"<dt>Your friend's name</dt><dd><input type='text' name='toName' onkeyup=\"$('tAFToName').innerHTML = this.value\"></dd>"
										+"<dt>Your friend's Email address</dt><dd><input type='text' name='toEmail' /></dd>"
										+"<dt>Additional Text</dt><dd><textarea name='additionalText' onkeyup=\"$('tAFAdditionalText').innerHTML = this.value\"></textarea></dd></dl>"
										+"<h3>The Email</h3>"
										+"<div class='email'><p>Hey <span id='tAFToName'>[your friend's name]</span>,</p><p>I thought you might be interested in this page from the St Andrew's Cathedral School website: "
										+"<a href='"+ thisURL +"'>"+ thisURL.href.replace(/^(.{0,50}).*$/, '$1...') +"</a></p>"
										+"<pre id='tAFAdditionalText'>[additional text]</pre>"
										+"<p>Cheers,<br /><span id='tAFFromName'>[your name]</span></p></div>"
										+"<input type='hidden' name='location' value='"+ thisURL +"' />"
										+"<input type='submit' name='tellAFriend' value='Send &amp; Close' class='button' /> <input type='reset' value='Discard &amp; Close' class='button' onclick='TellAFriend.noDice(); return false;' />"
										+"</form><br class='clear' /><br class='clear' />";
		
			// hide all select boxes as they can't be "overlayed" in IE
			selects = document.getElementsByTagName("select");
			for (i = 0; i != selects.length; i++) {
				selects[i].style.visibility = "hidden";
			}
	
			// stretch overlay and fade in
			var pageSize = getPageSize();
			var pageScroll = getPageScroll();
			Element.setHeight('tAFOverlay', pageSize[1]);
			new Effect.Appear('tAFOverlay', { duration: 0.2, from: 0.0, to: 0.8 });
	
			// calculate top offset and display 
			var topOffset = pageScroll[1] + (pageSize[3] / 15);
			Element.setTop('tellAFriend', topOffset);
			Element.show('tellAFriend');
	},
	
	noDice: function() {
		Element.hide('tellAFriend');
		new Effect.Fade('tAFOverlay', { duration: 0.2});
		
		// show selectboxes again
		selects = document.getElementsByTagName("select");
		for (i = 0; i != selects.length; i++) {
			selects[i].style.visibility = "visible";
		}
	},
	
	goDo: function(form) {
		errors = Array();
		if (form.fromEmail.value.toLowerCase().match(/.+@.+\.[a-z]+/) == undefined) {
			errors.push("Please enter your email address.");
		}
		if (form.fromName.value == "") {
			errors.push("Please enter your name.");
		}
		if (form.toEmail.value.toLowerCase().match(/.+@.+\.[a-z]+/) == undefined) {
			errors.push("Please enter the email address of your friend.");
		}
		if (form.toName.value == "") {
			errors.push("Please enter your friend's name.");
		}
		
		if (errors.length == 0) {
			form.submit();
		} else {
			alert(errors.join("\n"));
			return false;
		}	
	}
};
