
if (typeof(String.trim)=='undefined') String.prototype.trim=function(){a=this.replace(/^\s+/,'');return a.replace(/\s+$/,'')}

function subsc() {
	this.nm = document.getElementById('name');
	this.em = document.getElementById('email');
	this.msg = document.getElementById('msg');
	this.auth = 'AF07AE24';

	var tt = document.getElementById('btn').value;
	switch (tt) {
	case 'subscribe': this.type = 'subscribe'; break;
	case 'unsubscribe': this.type = 'unsubscribe'; break;
	case 'send copy to my friend': this.type = 'share'; break;
	}

	this.em.onkeydown = function() {mySubsc.onEmailKey()};

	if (this.nm) {
		this.nm.focus();
		this.nm.onblur = function(e) {mySubsc.onNameBlur(e)};
		this.nm.onkeydown = function(e) {mySubsc.onNameKey(e)};
	} else {
		this.em.focus();
	}
}

subsc.prototype.pageAction = function() {
	var el = this.em;
	var en = this.nm;
	if (el) {
		var ee = el.value;
		var rx = /^([A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6})$/i;
		if (!rx.test(ee)) {
			this.showMsg('please enter a valid email address');
			el.focus();
			return;
		}
		var u = new bldurl();
		u.file('http://www.lauranidra.com/news/subscriber.html');
		if (en) u.add('name',en.value.trim());
		u.add('email',el.value.trim());
		u.add('type',this.type);
		u.add('auth',this.auth);
		u.unique();
		window.location = u.url();
	}
}

subsc.prototype.showMsg = function(msg) {
	var el = this.msg;
	if (el) {
		if (msg.length) {
			el.innerHTML = msg;
			el.style.visibility = 'visible';;
		}
		else {
			el.innerHTML = '&nbsp;';
			el.style.visibility = 'hidden';
		}
	}
}

subsc.prototype.endEvent = function(e) {
	e.returnValue = false;
	e.cancelBubble = true;
	e.cancel = true;
	if (e.preventDefault) e.preventDefault();
	if (e.stopPropagation) e.stopPropagation();
	return false;
}

subsc.prototype.onEmailKey = function(e) {
	e = e || event;
	if (e.keyCode != 13) return this.showMsg('');
	this.endEvent(e);
	this.pageAction();
}

subsc.prototype.onNameKey = function(e,id) {
	e = e || event;
	if (e.keyCode != 13) return this.showMsg('');
	this.endEvent(e);
	var el = document.getElementById('email');
	if (el) el.focus();
}

subsc.prototype.onNameBlur = function(e) {
	if (!this.nm.value.trim().length) {
		this.showMsg('please enter your name');
		this.nm.focus();
	}
}

if (!window.mySubsc) window.mySubsc = new subsc();

