/* email-a-friend.js 
 * 
 * Jim Cradock <jim@yellahoose.com> and 
 * Gordon Holman <gordon@slickfishstudios.com> 
 * 
 * JavaScript for Email-A-Friend, written by Jim at Yellahoose and Gordon at 
 * Slickfish Studios. The functionality, but not a lot of the programming, is 
 * based on Lightbox (http://www.huddletogether.com/projects/lightbox/). 
 */ 

// trim() 
// Simple JavaScript function to remove whitespace, e.g., spaces, tab stops, 
// newline character, and carriage returns, from a given string. 
function trim(string) { 
	return string.replace(/^\s+|\s+$/g, ""); 
} 

// validEmail() 
// Simple JavaScript function to determine whether or not a given email address 
// is a valid email address. 
function validEmail(email) { 
	if (email.match(/^[A-Za-z0-9_\+-]+(\.[A-Za-z0-9_\+-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.([A-Za-z]{2,4})$/)) { 
		return true; 
	} else { 
		return false; 
	} 
} 

// pageHeight() 
// Simple JavaScript function to get user agent's window height. 
function pageHeight() { 
	return navigator.appName.indexOf("Microsoft") == -1 ? window.innerHeight : document.body.offsetHeight; 
} 

// pageWidth() 
// Simple JavaScript function to get user agent's window width. 
function pageWidth() { 
	return navigator.appName.indexOf("Microsoft") == -1 ? window.innerWidth : document.body.offsetWidth; 
} 

// scrollHeight() 
// Simple JavaScript function to get user agent's vertical window scroll. 
function scrollHeight() { 
	return navigator.appName.indexOf("Microsoft") == -1 ? window.pageYOffset : document.body.scrollTop; 
} 

// scrollWidth() 
// Simple JavaScript function to get user agent's horizontal window scroll. 
function scrollWidth() { 
	return navigator.appName.indexOf("Microsoft") == -1 ? window.pageXOffset : document.body.scrollLeft; 
} 

// centerPage() 
// Simple JavaScript function to center DIV. 
function centerPage() { 
	return navigator.appName.indexOf("Microsoft") == -1 ? (window.innerWidth / 2) - 200 : (document.body.offsetWidth / 2) - 200; 
} 

// showDiv() 
// Simple JavaScript to show a DIV, identified by the given ID. 
function showDiv(which_div) { 
	showOverlay(); 
 	if (document.getElementById) {
 		document.getElementById(which_div).style.left = centerPage() +'px'; 
 		document.getElementById(which_div).style.display = 'block'; 
 	}
	return; 
} 

// hideDiv() 
// Simple JavaScript function to hide a DIV, identified by the given ID. 
function hideDiv(which_div) { 
	hideOverlay(); 
	if (document.getElementById) {
		document.getElementById(which_div).style.display = 'none'; 
	}	
	return; 
} 

// showOverlay() 
// Simple JavaScript function to show the overlay. 
function showOverlay() {
	if (document.getElementById) {
		if (document.getElementById('EAFOverlay')) { 
			var oOverlay = document.getElementById('EAFOverlay'); 		
			oOverlay.style.width = parseInt(pageWidth() + scrollWidth()) +'px';
			oOverlay.style.height = parseInt(pageHeight() + scrollHeight()) +'px';
			oOverlay.style.display = 'block'; 
		} else { 
			var oOverlay = document.createElement('div'); 
			oOverlay.setAttribute('id', 'EAFOverlay'); 		
			oOverlay.style.width = parseInt(pageWidth() + scrollWidth()) +'px';
			oOverlay.style.height = parseInt(pageHeight() + scrollHeight()) +'px';
			oOverlay.style.position = 'absolute';
			oOverlay.style.top = '0px';
			oOverlay.style.left = '0px';
			oOverlay.style.zIndex = '100';
			oOverlay.style.display = 'block';
			document.getElementsByTagName("body").item(0).insertBefore(oOverlay, document.getElementsByTagName("body").item(0).firstChild); 
		} 
	} 
} 

// hideOverlay() 
// Simple JavaScript to hide the overlay. 
function hideOverlay() { 
	if (document.getElementById) { 
		if (document.getElementById('EAFOverlay')) {			
			document.getElementById('EAFOverlay').style.display = 'none'; 
		} 
	} 
} 

// initPage() 
// Simple JavaScript function to initialize the page, including setting the 
// email-a-friend form in its default state, which is hidden. 
function initPage(which_div) { 
	hideDiv(which_div); 
	setInterval("resizePage('EAFBox')", 1500); 
	return; 
} 

// resizePage() 
// Simple JavaScript function primarily to re-position the email-a-friend for 
// when the web browser's been resized. 
function resizePage(which_div) { 
	hideOverlay(); 
	if (document.getElementById) {
		if (document.getElementById(which_div).style.display == 'block') { 
			showDiv(which_div); 
		} 
		return; 
	}	
}  

// emailAFriend() 
// Simple JavaScript function to show form box with title and link of item in 
// page. 
function emailAFriend(which_div, what_title, what_link) { 
	showDiv(which_div); 
	if (document.getElementById) { 
		document.getElementById('EAFForm').name.value = what_title; 
		document.getElementById('EAFForm').link.value = what_link; 
		document.getElementById('EAFNameAndLink').innerHTML = '<strong><em>'+ what_title +'<\/em><\/strong><br>'+"\n"+ what_link +'<br>'+"\n"+'<br>'+"\n"; 
	}
} 

// sendEmail() 
// Simple JavaScript function to validate required data and send e-mail. 
function sendEmail(oForm) { 
	if (trim(oForm.name.value) == '' || trim(oForm.link.value) == '') { 
		alert('This page is not configured properly. Please contact the site\'s webmaster.'); 
		return; 
	} 
	if (trim(oForm.recipients_email.value) == '' || trim(oForm.your_email.value) == '') { 
		alert('Not all required data was entered. Please enter your e-mail address and the recipient\'s e-mail address.'); 
		return; 
	} 
	if (! validEmail(trim(oForm.recipients_email.value))) { 
		alert('The receipient\'s e-mail is not valid. Please enter a valid e-mail address.'); 
		return; 
	} 
	if (! validEmail(trim(oForm.your_email.value))) { 
		alert('Your e-mail is not valid. Please enter a valid e-mail address.'); 
		return; 
	} 
	oForm.submit(); 
	return; 
} 
