// JavaScript Document


//alert('test 1');

function clearTextfeild(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = "";
		theText.style.color = '#000000'
	}
}
function resetTextfeild(theText) {
	if (theText.value == "") {
		theText.value = theText.defaultValue;
		theText.style.color = '#888888'
	}
}

function isValidEmail(the_email) {
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalChars= /[\(\)\<\'\>\,\;\:\\\/\"\[\]]/;
	if (!emailFilter.test(the_email) || the_email.match(illegalChars)) {
		return false;
	}
	return true;
}

function initAjaxObj() {
	//alert('test 2');
	var ajax;
	try {
	// Firefox, Opera 8.0+, Safari
	ajax=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
		ajax=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				ajax=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return ajax;
}

function addToMailingList() {
	//alert('test 3');
	var email_elem = document.getElementById('emailsignup');
	var result_elem = document.getElementById('signupresult');
	var button_elem = document.getElementById('buttonsignup');
	
	if( !isValidEmail(email_elem.value) ) {
		email_elem.focus();
		result_elem.innerHTML='<span class="error"><strong>ERROR:</strong> Invalid E-mail Address.</span>';
		result_elem.style.display='block';
		return;
	}
	
	var xmlHttp = initAjaxObj();
	
	xmlHttp.open("POST","/inc/mailing-list.php",true);
	var post_vars = 'email='+email_elem.value;
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", post_vars.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange=function() {
		//alert('test 2');
		if(xmlHttp.readyState==4) {
			result_elem.innerHTML=xmlHttp.responseText;
			result_elem.style.display='block';
			button_elem.disabled=false;
		}
		else {
			button_elem.disabled=true;
		}
	};
	
	xmlHttp.send(post_vars);
	//alert('test 2');
}