function emailPopObj(popid, bodyid, toid, fromid, subjectid, messageid, errorsid, sendstatusid) {
	this._popid = popid;
	this._popup = null;

	this._bodyid = bodyid;
	this._body = null;

	this._toid = toid;
	this._to = null;

	this._fromid = fromid;
	this._from = null;

	this._subjectid = subjectid;
	this._subject = null;

	this._messageid = messageid;
	this._message = null;

	this._errorsid = errorsid;
	this._errors = null;

	this._sendstatusid = sendstatusid;
	this._sendstatus = null;

	this._defaultTextArr = null;
	this._defaultBodyText = null;

	this._invokingObject = null;

	this._XMLHttpRequestObject = null;

	this._trimString = function(str) {
		return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
	}

	this._validateEmailAddrs = function(addrs, field) {
		var emailReg = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$";
		var regex = new RegExp(emailReg);		
		var addrsArr = addrs.split(',');
		if (addrsArr.length < 1) { return false; }
		if (field == "to" && addrsArr.length > 5) { return false; }
		for (var i = 0; i < addrsArr.length; i++) {
			if (!regex.test(this._trimString(addrsArr[i]))) { return false; }
		}
		return true;
	}

	this._findPos = function() {
		var curleft = 0;
		var curtop = 0;
		var obj = this._invokingObject;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return [curleft, curtop];
	}

	this.open = function(invokingObject) {
		this._defaultTextArr = new Array(3);
		this._defaultTextArr[0] = "Enter e-mail addresses separated by comma";
		this._defaultTextArr[1] = "Enter your e-mail address";
		this._defaultTextArr[2] = "I thought you'd be interested in this item I found on the NEI website.";

		this._popup = document.getElementById(this._popid);
		this._body = document.getElementById(this._bodyid);
		this._to = document.getElementById(this._toid);
		this._from = document.getElementById(this._fromid);
		this._subject = document.getElementById(this._subjectid);
		this._message = document.getElementById(this._messageid);
		this._errors = document.getElementById(this._errorsid);
		this._sendstatus = document.getElementById(this._sendstatusid);
		this._invokingObject = invokingObject;

		this._popup.style.left = this._findPos()[0] + "px";
		this._popup.style.top = this._findPos()[1] + this._invokingObject.clientHeight + 3 + "px";
		this._popup.style.display = "block";
		this._defaultBodyText = this._body.innerHTML;
		this._to.value = this._defaultTextArr[0];
		this._to.className = "";
		this._from.value = this._defaultTextArr[1];
		this._from.className = "";
		this._message.value = this._defaultTextArr[2];

		this._XMLHttpRequestObject = false;
		if (window.XMLHttpRequest) {
			this._XMLHttpRequestObject = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			this._XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
		}

	} // end of open

	this.close = function() {
		this._sendstatus.style.display = "none"
		this._popup.style.display = "none";
		this._errors.innerHTML = "";
		this._body.innerHTML = this._defaultBodyText; // reset email popup text to original format
	} // end of close

	this.defaultTextCheck = function(id, e) {
		var field = document.getElementById(id);
		var value = this._trimString(field.value);
		if (e == "onfocus") {
			switch(id) {
				case "emailTo":
					if (value == this._defaultTextArr[0]) { field.value = ""; }
					break;
				case "emailFrom":
					if (value == this._defaultTextArr[1]) { field.value = ""; }
					break;
				case "emailMessage":
					if (value == this._defaultTextArr[2]) { field.value = ""; }
					break;
			}
		} else {
			switch(id) {
				case "emailTo":
					if (value == "") { field.value = this._defaultTextArr[0]; }
					break;
				case "emailFrom":
					if (value == "") { field.value = this._defaultTextArr[1]; }
					break;
				case "emailMessage":
					if (value == "") { field.value = this._defaultTextArr[2]; }
					break;
			}
		}
	} // end of defaultTextCheck

	this.send = function() {
		var emailMessage = "";
		var me = this;
		var errmess = "";
		var htmlText = "";
		var trimmedHref = window.location.href.length-1;
		var newHref = escape(window.location.href.slice(0,trimmedHref));
		var location = "http://" + window.location.host + "/referPage";

		if (this._validateEmailAddrs(this._trimString(this._to.value), "to") && this._validateEmailAddrs(this._trimString(this._from.value), "from")) {
			// valid email addresses
			this._errors.innerHTML = "";
			if (this._XMLHttpRequestObject) {
				this._XMLHttpRequestObject.open("POST", location);
				this._XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				
				this._XMLHttpRequestObject.onreadystatechange = function() {
					if (me._XMLHttpRequestObject.readyState == 4 && me._XMLHttpRequestObject.status == 200) {
						me._sendstatus.style.display = "none";
						htmlText = me._XMLHttpRequestObject.responseText;
						me._body.innerHTML = htmlText;
					}
				}
				emailMessage = "emailTo=" + this._trimString(this._to.value) + "&emailFrom=" + this._trimString(this._from.value) + "&emailSubject=" + 
					this._subject.value + "&emailMessage=The following web page has been sent to you by " + this._from.value + ":\n" + newHref + "\n\nFrom:\n" + this._from.value + "\n\nMessage From Sender:\n" + this._message.value + "\n\n";
				this._XMLHttpRequestObject.send(emailMessage);
				// center and display sending mail status message
				this._sendstatus.style.left = this._findPos()[0] + ((this._popup.clientWidth - this._sendstatus.clientWidth) / 6) + "px";
				this._sendstatus.style.top = this._findPos()[1] + this._invokingObject.clientHeight + 3 + ((this._popup.clientHeight - this._sendstatus.clientHeight) / 2) + "px";
			} // end of inner if
		} else {
			// not valid email addresses
			if (!this._validateEmailAddrs(this._trimString(this._to.value), "to")) {
				this._to.className = "errors";
				errmess += "<li>To: invalid email(s) or too many emails entered</li>";
			} else { this._to.className = ""; }
			if (!this._validateEmailAddrs(this._trimString(this._from.value), "to")) {
				this._from.className = "errors";
				errmess += "<li>From: invalid email(s)</li>";
			} else { this._from.className = ""; }
			this._errors.innerHTML = errmess;
			
		} // end of if else
	} // end of send
} // end of emailPopObj

emailPop = new emailPopObj("emailPop", "emailPopBody", "emailTo", "emailFrom", "emailSubject", "emailMessage", "emailErrors", "emailSendStatus");
