_cfscriptLocation = "/ajax/functions/functions.cfm";

function isEmail(s) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(s)){ return true; }
	return false;
}

var queued_addresses = 0;

function forwardtofriend() {
	mystorybox.start();
}

function addtoemailaddressline() {
	$('emailtolines').value = parseInt($('emailtolines').value) + 1;
	var emailToLines = $('emailtolines').value;
	var row = emailToLines % 2 == 0 ? 1 : 2;
	var str = '';
    str += '<tr class="rowcolor' + row + '">';
    str += '<td><label for="name'+ emailToLines + '">Their Name:</label>';
    str += ' <input name="name'+ emailToLines + '" type="text" id="name'+ emailToLines + '" size="30" />';
    str += '</td>';
    str += '<td><label for="email'+ emailToLines + '">Their Email:</label>';
    str += ' <input name="email'+ emailToLines + '" type="text" id="email'+ emailToLines + '" size="30" />';
    str += '</td>';
    str += '</tr>';
	new Insertion.Before($('addanotherfriend'), str);
}

function validateForwardToFriendForm() {
	var valid_emails = 0;
	var toaddresses = '';
	
	if( $('emailfromname').value == '' ) {
		alert('Please enter your name');
		$('emailfromname').focus();
		return false;
	}
	if( ! isEmail($('emailfromaddress').value) ) {
		alert('Please enter a valid from email address');
		$('emailfromaddress').focus();
		return false;
	}

	for( var i=1; i <= $('emailtolines').value; i++ ) {
		var toaddress = $('email'+i).value;
		if( toaddress != '' && !isEmail(toaddress) ) {
			alert('Your friend\'s email address is not formatted properly');
			$('email'+i).focus();
			return false;
		}
		else if ( toaddress != '' ) { 
			valid_emails = valid_emails + 1; 
			if( valid_emails > 1 ) {
				toaddresses = toaddresses + ',' + toaddress;
			}
			else {
				toaddresses = toaddresses + toaddress;		
			}
		}
	}
	if( valid_emails == 0 ) {
		alert('Please enter one or more email addresses for your friends.');
		$('email1').focus();
		return false;
	}
	return true;
}

function sendToFriends() {
	
	$('forwardstatus').innerHTML = 'Status: Sending your message...';	
	$('forwardstatus').style.display = 'block';
	
	var emailToLines = $('emailtolines').value;
	
	for( var i=1; i <= emailToLines; i++ ) {
		var toname = $('name'+i).value;
		var toaddress = $('email'+i).value;
		if ( toaddress != '' ) { 
			queued_addresses = queued_addresses + 1;
			DWREngine._execute(_cfscriptLocation, null, 'sendToFriend', 
					$('linkurl').value,
					$('emailfromname').value, 
					$('emailfromaddress').value, 
					$('emailmessage').value, 
					toname, 
					toaddress, 
					handleSentToFriend);	 	
		}
	}		
}

function handleSentToFriend(val) {
	queued_addresses = queued_addresses - 1;
	if( queued_addresses == 0 ) { notifyAllSent(); }
}
function notifyAllSent() {
	$('forwardstatus').innerHTML = '<h3>Your message has been sent!</h3><p>Thank you for sharing our website with your friends.</p>';
	pageTracker._trackPageview("/forwardtofriend-completed");
}

var Storybox = Class.create();

Storybox.prototype = {

	// initialize()
	// Constructor runs on completion of the DOM loading. Loops through anchor tags looking for 
	// 'lightbox' references and applies onclick events to appropriate links. The 2nd section of
	// the function inserts html at the bottom of the page which is used to display the shadow 
	// overlay and the image container.
	//
	initialize: function() {	

	},
	//
	//	start()
	//	Display overlay
	//
	start: function() {	

//		hideSelectBoxes();

		// stretch overlay to fill page and fade in
		var arrayPageSize = getPageSize();
		Element.setHeight('overlay', arrayPageSize[1]);
		new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.8 });

		// calculate top offset for the lightbox and display 
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		var storyboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);

		Element.setTop('storybox', storyboxTop);
		Element.show('storybox');
		Element.setOpacity('storybox',1);

	},

	//
	//	end()
	//
	end: function() {
		Element.hide('storybox');
		new Effect.Fade('overlay', { duration: 0.2});
	}
}
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}


function initStorybox() { mystorybox = new Storybox(); }
Event.observe(window, 'load', initStorybox, false);

Object.extend(Element, {
	getWidth: function(element) {
	   	element = $(element);
	   	return element.offsetWidth; 
	},
	setWidth: function(element,w) {
	   	element = $(element);
    	element.style.width = w +"px";
	},
	setHeight: function(element,h) {
   		element = $(element);
    	element.style.height = h +"px";
	},
	setTop: function(element,t) {
	   	element = $(element);
    	element.style.top = t +"px";
	},
	setSrc: function(element,src) {
    	element = $(element);
    	element.src = src; 
	},
	setHref: function(element,href) {
    	element = $(element);
    	element.href = href; 
	},
	setInnerHTML: function(element,content) {
		element = $(element);
		element.innerHTML = content;
	}
});