var _newsLetter = null;

function NewsLetter(sInputControlID, sLoadingDivID, sButtonID, sMessageDiv)
{
	this.InputControl = $("#" + sInputControlID);
	this.Email = "";
	this.ButtonControl = $("#" + sButtonID);
	this.LoadingDiv = $("#" + sLoadingDivID);
	this.MessageDiv = $("#" + sMessageDiv); 
}

/***********************************Login Methods*************************************/
NewsLetter.prototype.SignUp = function()
{
	this.ClearErrorMessage();
	//Validate that the email is correct
	if(this.InputControl != null)
	{
		this.Email = Trim(this.InputControl.val());
		if(this.Email == "")
		{
			this.ShowErrorMessage("Please enter a valid email address.");
			txtUsername.style.backgroundColor = "#ecb9af";
			txtUsername.focus();
			return;
		}
		else if(!ValidateEmailNoMessage(this.Email))
		{
			this.ShowErrorMessage("Please enter a valid email address.");
			this.InputControl.css("backgroundColor","#ecb9af");
			this.InputControl.focus();
			return;
		}

		this.ShowLoading();
		//Package up the data and create the account.
		var sURL = "newsletter/newsletterws.php?action=signup&email=" + this.Email;
		//Post the ajax call
		$.ajax({ 
			   type: "POST", 
			   url: sURL,  
			   success: _newsLetter.SignUp_onSuccess} 
			 );
		_newsLetter = this;
	}
}

NewsLetter.prototype.SignUp_onSuccess = function(result)
{
	switch(result)
	{
		case "accepted":
			_newsLetter.ShowInfoMessage("Thank you for signing up for the Tailwind newsletter.");
			break;
		default:
			_newsLetter.ShowErrorMessage(result);
			break;
	}
}

NewsLetter.prototype.ShowLoading = function()
{
	this.LoadingDiv.css("display","block");
	this.ButtonControl.css("display","none");
}

NewsLetter.prototype.ShowErrorMessage = function(sMsg)
{
	this.LoadingDiv.css("display","none");
	this.MessageDiv.css("display","block");
	this.MessageDiv.css("color","red");
	this.MessageDiv.text(sMsg);
	this.ButtonControl.css("display","block");
}

NewsLetter.prototype.ClearErrorMessage = function(sMsg)
{
	this.LoadingDiv.css("display","none");
	this.MessageDiv.textContent = sMsg;
	this.MessageDiv.css("display","block");
}

NewsLetter.prototype.ShowInfoMessage = function(sMsg)
{
	this.LoadingDiv.css("display","none");
	this.MessageDiv.css("display","block");
	this.MessageDiv.css("color","green");
	this.MessageDiv.text(sMsg);
	this.ButtonControl.css("display","block");
}
