var html = '<div id="send_container" style="display:none;">	<div class="email_frame">		<div class="email_form section">		    <h2>Email This Page</h2>    		<form class="block" action="/email_page.form" onsubmit="return(SendPopup.Submit())">    		    <div class="field">    			    <label for="send_to">To:     			        <span class="error" id="send_to_required">*Required</span>    			        <span class="error" id="send_to_invalid">*Invalid Email Address</span>    			    </label>     			    <input type="text" id="send_to" value="" />    			</div>    			<div class="field">    			    <label for="send_from">From:     			        <span class="error" id="send_from_required">*Required</span>    			        <span class="error" id="send_from_invalid">*Invalid Email Address</span>    			    </label>    			    <input type="text" id="send_from" value="" />    			</div>    			<div class="field">        			<label for="send_subject">Subject: <em>(Optional)</em></label>         			<input type="text" id="send_subject" value="" />        		</div>        		<div class="field">        			<label for="send_message">Message: <em>(Optional)</em></label>        			<textarea id="send_message"></textarea>        		</div>        		<div class="buttons">        		    <input type="submit" value="Send" />        		    <input type="submit" value="Cancel" onclick="return(SendPopup.Close());" />        		</div>    		</form>    	</div>		<div class="thank_you section">			<h2>Thank You</h2>			<div class="block">				<p>Your message has been sent.</p>				<p><input type="submit" value="  Done  " onclick="return(SendPopup.Close());" /></p>			</div>		</div>			<div class="mail_failed section">			<h2>Sorry</h2>			<div class="block">				<p>Your message could not be sent.</p>				<p><input type="submit" value="  Done  " onclick="return(SendPopup.Close());" /></p>			</div>		</div>	</div></div>';
document.write(html);

var SendPopup = { };

SendPopup.Open = function()
{
	$("#send_container .section").css('display', 'none');
	$("#send_container .email_form").css('display', 'block');
	$("#send_container .error").css('display', 'none');
	$("#send_container").css('display', 'block');
	// return false;
}

SendPopup.Close = function()
{
	$("#send_container").css('display', 'none');
	return false;
}

SendPopup.Submit = function()
{
	var emailPattern = /^([a-zA-Z0-9_+]|\-|\.)+@(([a-zA-Z0-9_]|\-)+\.)+[a-zA-Z]{2,4}$/;	
	var from = $("#send_container #send_from").get(0).value;
	var to = $("#send_container #send_to").get(0).value;
	var subject = $("#send_container #send_subject").get(0).value;
	var message = $("#send_container #send_message").get(0).value;
	
	$("#send_container .error").css('display', 'none');	
	var errors = false;
	
	// check for missing <from> field
	if(from.match(/^\s*$/))
	{
		errors = true;
		$("#send_container #send_from_required").css('display', 'inline');
	}
	else
	{
		// check for invalid <from> field
		if(!from.match(emailPattern)) 
		{
			errors = true;
			$("#send_container #send_from_invalid").css('display', 'inline');
		}
	}
	
	// check for missing <to> field
	if(to.match(/^\s*$/))
	{
		errors = true;
		$("#send_container #send_to_required").css('display', 'inline');
	}
	else
	{
		// check for invalid <to> field
		if(!to.match(emailPattern)) 
		{
			errors = true;
			$("#send_container #send_to_invalid").css('display', 'inline');
		}
	}
	
	// submit it!
	if(!errors)
	{
		var URL = $("#send_container form").get(0).action;
		var args = 
		{
			url: window.location.href,
			from: from,
			to: to,
			subject: subject,
			message: message
		};
		
		$.post(URL, args, function(data)
		{
			if(data && data.result == 0)
			{
				$("#send_container .section").css('display', 'none');
				$("#send_container .thank_you").css('display', 'block');
			}
			else
			{
				$("#send_container .section").css('display', 'none');
				$("#send_container .mail_failed").css('display', 'block');
			}
		}, "json");
	}
	
	return false;
}
