function validEmail(str) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return filter.test(str);
}


if (!nu) {
	var nu = new Object();
}

nu.Recommend = Class.create();
nu.Recommend.prototype = {
	
	saveUrl: "/__Ajax/Recommend.aspx",
	fromName: "",
	fromAddress: "",
	toName: "",
	toAddress: "",
	pageUrl: "",
	message: "",
	el: null,
	x: "500px", // default position
	y: "65px",
	
	initialize: function(url) {
		
		this.pageUrl = url;
		// create html element for note
		this.el = document.createElement('div');		
		this.el.className = "note";
		
        document.body.appendChild(this.el);		
        
		this.el.style.zIndex = highestDepth;
		this.el.style.left = this.x;
		this.el.style.top = this.y;
		
		// make element draggable 
		new Draggable(this.el, {starteffect:false, endeffect:false});	
		
		this.edit();
	},
		
	edit: function() {
		// insert input elements
		this.el.innerHTML ="<div class=\"inner\">&nbsp;</div>"
		var editHtml = '<h4 class="grey">Send to a friend</h4>';
		editHtml += '<p class="mandatory">To send this page to a friend, please fill in all the details.</p>';
		editHtml += '<dl class="grey">';
		editHtml += '<dt><label for="fromName">Your name</label></dt><dd><input id="fromName" name="fromName" type="text" value="' + this.fromName + '" /></dd>';
		editHtml += '<dt><label for="fromAddress">Your email</label></dt><dd><input id="fromAddress" name="fromAddress" type="text" value="' + this.fromAddress + '" /></dd>';
		editHtml += '<dt><label for="toName">Your friend\'s name</label></dt><dd><input id="toName" name="toName" type="text" value="' + this.toName + '" /></dd>';
		editHtml += '<dt><label for="toAddress">Your friend\'s email</label></dt><dd><input id="toAddress" name="toAddress" type="text" value="' + this.toAddress + '" /></dd>';
		editHtml += '</dl>';
		editHtml += '<div class="noteButtons"><button class="noteSendBt">&nbsp;</button><button class="noteCancelBt">&nbsp;</button></div>';
		
		this.el.firstChild.innerHTML = editHtml;

		// add button behaviors
		var buttons = this.el.getElementsByTagName('button');
		buttons[0].onclick = this.updateData.bind(this);
		buttons[1].onclick = this.cancel.bind(this);
	},
	
	remove: function() {
	
        // remove element from document
        this.el.parentNode.removeChild(this.el);
	},
	
	updateData: function() {
		this.fromName = this.el.getElementsByTagName('input')[0].value;
		this.fromAddress = this.el.getElementsByTagName('input')[1].value;
		this.toName = this.el.getElementsByTagName('input')[2].value;
		this.toAddress = this.el.getElementsByTagName('input')[3].value;
		
		// TODO - validate emails
		if (!this.fromName || !this.fromAddress || !this.toName || !this.toAddress) {
			alert("Please enter all the details.");		
		} else if (!validEmail(this.fromAddress)) {
			alert("Your email address is invalid.");
		} else if (!validEmail(this.toAddress)) {
			alert("Your friend\'s email address is invalid. ");
		} else {
			this.save();
		}
	},
	
	cancel: function() {
		this.remove();
	},
	
	save: function() {
		var updateRequest = new Ajax.Request(
			this.saveUrl, {
				method: 'post', 
				parameters: 'from_name=' + this.fromName + '&from_address=' + this.fromAddress + '&to_name=' + this.toName + '&to_address=' + this.toAddress + '&url=' + this.pageUrl + '&message=' + this.message, 
				onComplete: function(req) {
			        if (req.responseText == 'True') {
			           this.el.firstChild.innerHTML = "<h4 class=\"grey\">Send to a friend</h4><p>Your email has been sent.</p><div class=\"noteButtons\"><button id=\"noteOkBt\">&nbsp;</button></div>";
						var okButton = $('noteOkBt');
						okButton.onclick = this.remove.bind(this);
					} else {
						this.el.firstChild.innerHTML = "<h4 class=\"grey\">Send to a friend</h4><p>An error occured while sending your message.</p><div class=\"noteButtons\"><button id=\"noteOkBt\">&nbsp;</button></div>";
						var okButton = $('noteOkBt');
						okButton.onclick = this.remove.bind(this);
					}
				}.bind(this)	
			});
	}
}

function SendFriend(pageUrl) 
{
    new nu.Recommend(pageUrl);
}