var xmlhttp;
var showit;
var sawform = 0;

function processCookies() {
	var myCook = new Array();
	if(document.cookie != '') {
		var cook = document.cookie.split(";");
		for(var a = 0; a < cook.length; a++) {
			var c = cook[a].split("=");
			var key = trimAll(c[0]);
			var val = trimAll(c[1]);
			myCook[key] = val;
		}
	}
	return myCook;
}

var myCookies = processCookies();

if (!String.prototype.toDocumentFragment) {
	String.prototype.toDocumentFragment = function toDocumentFragment(context) {
		var aRange=document.createRange();
		context= context || document.getElementsByTagName("body")[0];
		aRange.selectNodeContents(context);
		return aRange.createContextualFragment(this);
	}
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function setSeatCookie(cookiename, cookievalue) {
	var d = new Date();
	var dms = d.getTime();
	d = new Date(dms + (36000 * 24 * 10));
	
	document.cookie= escape(cookiename)
		+ "=" 
		+ escape(cookievalue) 
		+ ";" 
		+ "expired="
		+ d.toGMTString();

		myCookies[cookiename] = cookievalue;
}

function echeck(string) {	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)		return true;	else		return false;}

function showmailform() {
	if(myCookies['sawform'] != 1) {
		var topPos;
		if(document.all) {
			topPos = document.body.scrollTop;
		} else {
			topPos = window.pageYOffset;
		}
		
		document.getElementById('maillist').style.top = (parseInt(topPos)+30)+'px';
		document.getElementById('maillist').style.display = 'block';
		window.clearTimeout(showit); 
	}
}

function hidemailform(close) {
	document.getElementById('maillist').style.display = 'none';
	if(close == 1) {
		setSeatCookie("sawform", 1);
	}
	return false;
}

function subtolist() {
	var goodemail = echeck(document.getElementById('list_email').value);
	var validate_error = '';
	document.getElementById('list_email').style.background = 'white';
	document.getElementById('list_name').style.background = 'white';
	if(document.getElementById('list_email').value == '' || !goodemail) {
		document.getElementById('list_email').style.background = 'yellow';
		document.getElementById('list_email').focus();
		validate_error = 'You must specify a valid email address.\n' + validate_error;
	}
	
	if(document.getElementById('list_name').value == '') {
		document.getElementById('list_name').style.background = 'yellow';
		document.getElementById('list_name').focus();
		validate_error = 'You must specify a name.\n' + validate_error;
	}
	
	if(validate_error != '') {
		alert(validate_error);
		return false;
	}
	var name = document.getElementById('list_name').value;
	var email = document.getElementById('list_email').value;
	var myurl = '/addlist.php?name=' + escape(name) + '&email='  + 
	escape(email);
	loadXMLDoc(myurl);
	document.getElementById('list_name').disabled = true;
	document.getElementById('list_email').disabled = true;
	document.getElementById('maillist_submit').disabled = true;
	document.getElementById('maillist_submit').value = 'Submitting Your Info';
	return false;
}

function loadXMLDoc(url) {
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttp) {
		xmlhttp.onreadystatechange=readXML;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}

function readXML() {
	var parser = null;
	var doc = null;
	if (xmlhttp.readyState==4) {
		if (xmlhttp.status==200) {
			var xmlstr = xmlhttp.responseText;	
			if(xmlstr == 'success') {
				document.getElementById('maillist_content').innerHTML = '<h3>Thans for your sumission. Your emails should arrive shortly.</h3>';
				showmailform();
				showit = window.setTimeout("hidemailform(1)", 5000);
				return true;
			}
		} 
		setmailformerror("There was an error please try again<br />");
	}
	return false;
}

function setmailformerror(errorStr) {
	document.getElementById('error').innerHTML = error;
	document.getElementById('list_name').disabled = false;
	document.getElementById('list_email').disabled = false;
	document.getElementById('maillist_submit').disabled = false;
	document.getElementById('maillist_submit').value = 'Sign me up';
}
