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;
}
