var user, domain, regex, match;

window.onload=function() {
	document.forms[0].onsubmit=function() {
		checkAddress(this.email.value);
		return false;
	};
};

function Email(e){
	this.emailAddress=e;
	this.message="";
	this.valid=false;
}

function validateEmail(){
	if (this.emailAddress == null 			||
		this.emailAddress.length == 0
	){
		this.message="This email address is blank. You need to enter your email address.";
		this.valid = false;
		return;
	}

	if (this.emailAddress.indexOf(".") == -1){
		this.message="This email address doesn't have a dot (like yourname@hello.com).";
		this.valid = false;
		return;
	}

	if (this.emailAddress.indexOf("@") == -1){
		this.message="This email address doesn't have an 'at' sign (like yourname@hello.com).";
		this.valid = false;
		return;
	}

	if (this.emailAddress.indexOf(" ") != -1){
		this.message="This email address has a space, which is not allowed.";
		this.valid = false;
		return;
	}

	//parse username
	// this means 2 "word" characters, optional period, 2 more "word" characters.
	regex=/(^\w{2,}\.?\w{2,})@/;
	match=regex.exec(this.emailAddress);

	if (match){
		user=RegExp.$1
	} else {
		this.message="The username in this email address doesn't seem valid.";
		this.valid = false;
		return;
	}

	//parse domain
	//accept IP addresses if they want to use them.
	regex=/@(\[\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}\])$/;
	match=regex.exec(this.emailAddress);

	if (match){
		domain=RegExp.$1;
		this.valid=true;
	} else {
		//check for non-IP address domains
		regex=/@(\w{2,}\.(\w{2,}\.)?[a-zA-Z]{2,3})$/;
		match=regex.exec(this.emailAddress);

		if (match) {
			domain=RegExp.$1;
			this.valid=true;
			return;
		} else {
			//This was showing many domains to be invalid, so I disabled the check.
			//this.message="The domain in this email address doesn't seem valid.";
			domain=RegExp.$1;
			this.valid = true;
			return;
		}
	}

	this.valid=true;
}

// Hook the custom validateEmail function to the Email object
Email.prototype.validate=validateEmail;

function errorMsg(msg, sColor){
	var mydiv=document.getElementById("results");
	mydiv.style.color=sColor;
	mydiv.style.fontSize="0.9em";

	if (mydiv.hasChildNodes()){
		//remove them
		mydiv.removeChild(mydiv.firstChild);
	}

	mydiv.appendChild(document.createTextNode(msg));
}

function checkAddress(val){
	var myemail=new Email(val);
	var url;
	myemail.validate();
	if (! email.valid) {
		errorMsg(myemail.message, "red")
	} else {
		return;
	}
}

function handleResponse(){
	var usedTag,answer,xmlReturnVal;
	var failText="We're sorry, a problem occurred and we can't send this request. Please try again later.";

	try {
		if(request.readyState == 4){
			if(request.status == 200){
				xmlReturnVal=request.responseXML;

				if(xmlReturnVal != null){
					outCome=xmlReturnVal.getElementsByTagName("results")[0];

					if (outCome.childNodes[0].nodeValue == "true"){
						showMsg(document.getElementById("results"), "Your email has been sent");
					} else {
						showMsg(document.getElementById("results"), failText);
					}
				} else {
					showMsg(document.getElementById("results"), failText);
				}
			} else {
				showMsg(document.getElementById("results"), failText);
			}
		} else {
			showMsg(document.getElementById("results"), failText);
		}
	} catch(err) {
		showMsg(document.getElementById("results"), "I couldn't communicate with our server right now. Please try again later.");
	}
}

function setQueryString() {
	queryString="";
	var myform=document.forms[0];
	var elementsCount=myform.elements.length;
	for(var loop=0; loop < elementsCount; loop++) {
		if (myform.elements[loop].type == "checkbox") {
			queryString += myform.elements[loop].name+"="+encodeURIComponent(myform.elements[loop].checked);
		} else {
			queryString += myform.elements[loop].id+"="+encodeURIComponent(myform.elements[loop].value);
		}

		if (loop < elementsCount-1) {
			queryString += "&";
		}
	}
	//alert(queryString);
}
