function AutoEmailForm(tableID, hdnContactsCountID, btnNewContactID, ownFirstNameID, ownLastNameID, ownEmailID, formID, addContactImg, addContactOverImg, deleteContactImg, deleteContactOverImg, startRowId, defaultMessageText)
{
	this._el_AutoEmailFormTable = document.getElementById(tableID);
	this._el_StartRow = document.getElementById(startRowId);
	this._el_NewContactButton = document.getElementById(btnNewContactID);
	this._el_ContactsCount = document.getElementById(hdnContactsCountID);
	this._el_OwnFirstName = document.getElementById(ownFirstNameID);
	this._el_OwnLastName = document.getElementById(ownLastNameID);
	this._el_OwnEmail = document.getElementById(ownEmailID);
	this._form = document.getElementById(formID);
	this._addContactImage = addContactImg;
	this._addContactOverImage = addContactOverImg;
	this._deleteContactImage = deleteContactImg;
	this._deleteContactOverImage = deleteContactOverImg;

	this._c_FirstNameText = "txtFirstName";
	this._c_LastNameText = "txtLastName";
	this._c_EmailText = "txtEmail";
	this._c_MessageText = "txtMsg";
	this._c_MessageTextValue = defaultMessageText;
	this._c_DeleteButton = "btnDelete";

	this.initialize();
}

AutoEmailForm.prototype = 
{
	initialize : function()
	{
		var t = this;
		if (typeof(this._el_NewContactButton) != "undefined")
		{
			this._el_NewContactButton.onclick = function(event) { t.NewContact(event); return false; };
			this._el_NewContactButton.onmouseover = function(event) { t._ChageButtonImg(event, t._addContactOverImage); };
			this._el_NewContactButton.onmouseout = function(event) { t._ChageButtonImg(event, t._addContactImage); };
		}

		if (typeof(this._el_OwnFirstName) != "undefined")
		{
			this._el_OwnFirstName.onblur = function(event) { var el = window.event ? event.SrcElement : event.target; return t._ValidateRequiredText(el); };
		}

		if (typeof(this._el_OwnLastName) != "undefined")
		{
			this._el_OwnLastName.onblur = function(event) { var el = window.event ? event.SrcElement : event.target; return t._ValidateRequiredText(el); };
		}

		if (typeof(this._el_OwnEmail) != "undefined")
		{
			this._el_OwnEmail.onblur = function(event) { var el = window.event ? event.SrcElement : event.target; return t._ValidateRequiredText(el) && t._ValidateEmail(el); };
		}
	},

	NewContact : function(evt)
	{
		this._AddNewContact("", "", "", "");
	},

	_ChageButtonImg : function(evt, imageSrc)
	{
		var button = window.event ? window.event.srcElement : evt.target;
		eval("var el = document.getElementById(\"" + button.id + "\"); if (el != null) el.src = '" + imageSrc + "';");
	},

	_AddNewContact : function(firstName, lastName, email, message)
	{
		var mainRow = this._el_AutoEmailFormTable.insertRow(this._el_StartRow.rowIndex);
		mainRow.className = "ae_trData";
		var t = this;
		var ind = parseInt(this._el_ContactsCount.value, 10);
		var newContactInd = "" + ind;

		var firstNameCell = mainRow.insertCell(mainRow.cells.length);

		var lastNameCell = mainRow.insertCell(mainRow.cells.length);

		var emailCell = mainRow.insertCell(mainRow.cells.length);

		var additionalCell = mainRow.insertCell(mainRow.cells.length);
		/* Create First Name textbox */
		var firstNameTxt = document.createElement("INPUT");
		firstNameTxt.id = this._c_FirstNameText + newContactInd;
		firstNameTxt.name = this._c_FirstNameText + newContactInd;
		firstNameTxt.className = "ae_txt";
		firstNameTxt.type = "text";
		firstNameTxt.value = firstName;
		firstNameCell.appendChild(firstNameTxt);
		/* Create Last Name textbox */
		var lastNameTxt = document.createElement("INPUT");
		lastNameTxt.id = this._c_LastNameText + newContactInd;
		lastNameTxt.name = this._c_LastNameText + newContactInd;
		lastNameTxt.className = "ae_txt";
		lastNameTxt.type = "text";
		lastNameTxt.value = lastName;
		lastNameCell.appendChild(lastNameTxt);
		/* Create E-mail textbox */
		var emailTxt = document.createElement("INPUT");
		emailTxt.id = this._c_EmailText + newContactInd;
		emailTxt.name = this._c_EmailText + newContactInd;
		emailTxt.className = "ae_txt";
		emailTxt.type = "text";
		emailTxt.value = email;
		emailCell.appendChild(emailTxt);
		/* Create Delete button */
		var btnDelete = document.createElement("INPUT");
		btnDelete.type = "image";
		btnDelete.id = this._c_DeleteButton + newContactInd;
		btnDelete.alt = "Delete Contact";
		btnDelete.title = "Delete Contact";
		btnDelete.src = this._deleteContactImage;
		btnDelete.onclick = function(event){ return t._DeleteContact(event); return false; };
		btnDelete.onmouseover = function(event) { t._ChageButtonImg(event, t._deleteContactOverImage); };
		btnDelete.onmouseout = function(event) { t._ChageButtonImg(event, t._deleteContactImage); };
		additionalCell.appendChild(btnDelete);

		var messageRow = this._el_AutoEmailFormTable.insertRow(this._el_StartRow.rowIndex);
		messageRow.className = "ae_trData";
		var messageCell = messageRow.insertCell(messageRow.cells.length);
		messageCell.colSpan = 3;

		var messageAdditionalCell = messageRow.insertCell(messageRow.cells.length);
		
		/* Create Message textbox */
		var msgTxt = document.createElement("TEXTAREA");
		msgTxt.id = this._c_MessageText + newContactInd;
		msgTxt.name = this._c_MessageText + newContactInd;
		message = message.replace(/^\s+/,"").replace(/\s+$/,"");
		if (message == "" || message == this._c_MessageTextValue)
		{
			msgTxt.className = "ae_txt";
			msgTxt.value = this._c_MessageTextValue;
		}
		else
		{
			msgTxt.className = "ae_txt";
			msgTxt.value = message;
		}
		msgTxt.rows = 4;
		msgTxt.onblur = function(event) { return t._MessageBlur(event); };
		msgTxt.onfocus = function(event) { return t._MessageFocus(event); };
		messageCell.appendChild(msgTxt);
		
		var hrRow = this._el_AutoEmailFormTable.insertRow(this._el_StartRow.rowIndex);
		hrRow.className = "ae_trData";
		var hrCell = hrRow.insertCell(hrRow.cells.length);
		hrCell.colSpan = 4;
		var hr = document.createElement("HR");
		hrCell.appendChild(hr);
		//hrRow.insertCell(hrRow.cells.length);
		
		this._el_ContactsCount.value = parseInt(this._el_ContactsCount.value, 10) + 1;
	},
	
	_MessageBlur : function(evt)
	{
		var el = window.event ? window.event.srcElement : evt.target;
		var elVal = el.value.replace(/^\s+/,"").replace(/\s+$/,"");
		if (elVal == "" || elVal == this._c_MessageTextValue)
		{
			el.value = this._c_MessageTextValue;
		}
	},
	
	_MessageFocus : function(evt)
	{
		var el = window.event ? window.event.srcElement : evt.target;
		if (el.value == this._c_MessageTextValue)
		{
			el.value = "";
		}
	},
	
	_DeleteContact : function(evt)
	{
		var t = this;
		var tr = window.event ? window.event.srcElement : evt.target;
		while (tr != null && tr.tagName != "TR")
		{
			tr = tr.parentNode;
		}

		if (tr != null)
		{
			this._el_StartRow
			var contactsCount = parseInt(this._el_ContactsCount.value, 10);
			var contactIndex = contactsCount + (tr.rowIndex - this._el_StartRow.rowIndex) / 3;

			var ind = tr.rowIndex;
			this._el_AutoEmailFormTable.deleteRow(ind);
			this._el_AutoEmailFormTable.deleteRow(ind);
			this._el_AutoEmailFormTable.deleteRow(ind);

			while (contactIndex < contactsCount)
			{
				var nextInd = "" + (contactIndex + 1);

				var txtFirstName = document.getElementById(this._c_FirstNameText + nextInd);
				if (txtFirstName != null) txtFirstName.id = this._c_FirstNameText + contactIndex;

				var txtLastName = document.getElementById(this._c_LastNameText + nextInd);
				if (txtLastName != null) txtLastName.id = this._c_LastNameText + contactIndex;

				var txtEmail = document.getElementById(this._c_EmailText + nextInd);
				if (txtEmail != null) txtEmail.id = this._c_EmailText + contactIndex;

				var btnDelete = document.getElementById(this._c_DeleteButton + nextInd);
				if (btnDelete != null) btnDelete.id = this._c_DeleteButton + contactIndex;

				contactIndex++;
			}

			this._el_ContactsCount.value = parseInt(this._el_ContactsCount.value, 10) - 1;
		}

		if (window.event)
			window.event.cancelBubble = true;
		else
			evt.stopPropagation();
	},

	DataBind : function(data)
	{
		for(var i = 0; i < data.length; i++)
		{
			var dataItem = data[i];
			this._AddNewContact(dataItem.FirstName, dataItem.LastName, dataItem.Email, dataItem.Message);
		}
	},

	_ValidateRequiredText : function (textEl, showAlert)
	{
		var res = true;
		if (typeof(showAlert) == "undefined") showAlert = true;
		if (textEl != null && textEl.value.replace(/^\s+/,"").replace(/\s+$/,"") == "")
		{
			if (showAlert)
			{
				alert("Value is required.");
				textEl.focus();
			}
			res = false;
		}
		textEl.className = res ? "ae_txt" : "ae_invalidtxt";
		return res;
	},

	_ValidateEmail : function (emailEl, showAlert)
	{
		var res = false;
		if (typeof(showAlert) == "undefined") showAlert = true;
		if (this._ValidateRequiredText(emailEl))
		{
			var val = emailEl.value.replace(/^\s+/,"").replace(/\s+$/,"");
			var regExp = /^[a-zA-Z_0-9-'\+~]+(\.[a-zA-Z_0-9-'\+~]+)*@([a-zA-Z_0-9-]+\.)+[a-zA-Z]{2,7}$/;
			res = regExp.test(val);
			if (!res && showAlert)
			{
				alert("Email value has incorrect format.");
				emailEl.focus();
			}
		}
		return res;
	},

	ValidateForm : function(event)
	{
		var res = true;

		res = this._ValidateRequiredText(this._el_OwnFirstName, false) && res;
		res = this._ValidateRequiredText(this._el_OwnLastName, false) && res;
		res = this._ValidateRequiredText(this._el_OwnEmail, false) && this._ValidateEmail(this._el_OwnEmail, false) && res;

		var count = parseInt(this._el_ContactsCount.value, 10);
		for (var i=0; i<count; i++)
		{
			var ind = "" + i;
			var firstNameEl = document.getElementById(this._c_FirstNameText + ind);
			var lastNameEl = document.getElementById(this._c_LastNameText + ind);
			var emailEl = document.getElementById(this._c_EmailText + ind);
			var msgEl = document.getElementById(this._c_MessageText + ind);
			var msgElVal = msgEl.value.replace(/^\s+/,"").replace(/\s+$/,"");
			msgElVal = msgElVal == this._c_MessageTextValue ? "" : msgElVal;
			if (firstNameEl.value.replace(/^\s+/,"").replace(/\s+$/,"").length > 0 ||
				lastNameEl.value.replace(/^\s+/,"").replace(/\s+$/,"").length > 0 ||
				emailEl.value.replace(/^\s+/,"").replace(/\s+$/,"").length > 0 || 
				msgElVal.length > 0)
			{
				res = this._ValidateRequiredText(firstNameEl, false) && res;
				res = this._ValidateRequiredText(lastNameEl, false) && res;
				res = this._ValidateRequiredText(emailEl, false) && this._ValidateEmail(emailEl, false) && res;
			}
		}
		
		if (!res) alert("Some fields have incorrect values");
		
		return res;
	}
}

