var theForm = document.getElementById('sendemail');

function check() {
	  // Make sure they fill in the from address
	  if (theForm.email.value.length<1) {
		theForm.email.select();
		theForm.email.focus();
		alert('Please enter your email address.');
		return false;
	  }
	  if (emailCheck(theForm.email.value) == null || emailCheck(theForm.email.value) == 'undefined') {
	  	theForm.email.select();
		theForm.email.focus();
		alert('The sender\'s email address you entered is not valid.  Please enter a valid email address');
		return false;
	  }
		
	 // Make sure they fill in the to address
	 if (theForm.friend_email.value.length<1)  {
		theForm.friend_email.select();
		theForm.friend_email.focus();
		alert('Please a recipient\'s email address.');
		return false;
	 }
	 var toAddress = theForm.friend_email.value;
	 var atSignOccurance = toAddress.indexOf('@');
	 var periodOccurance = toAddress.indexOf('.');
	 if (parseInt(atSignOccurance) < 0 || parseInt(periodOccurance) < 0 ) {
		theForm.friend_email.select();
		theForm.friend_email.focus();
	 	alert ('The recipient\'s email address is not valid');
		return false;
	 }
	 checkCaptcha();
}
 //This checks for bots
function hiddenCheck() {
		 if(theForm.magicfield.value.length > 0) {
		 alert ('No bots allowed!');
		 return false;
		 }
}
hiddenCheck();

	//  This method checks the email address they entered to make sure it's valid.
	//  It will return true if everything works right and false if it doesn't.
function emailCheck (emailStr) {
	var emailPat=/^(.+)hotmail@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	if (user.match(userPat)==null) {
		return false;
	}	

	var IPArray = domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true;
	}
	
	var domainArray=domain.match(domainPat);
	if (domainArray==null) {
		return false;
	}

	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		return false;
	}
	if (len<2) {
		//alert(errStr)
		return false;
	}
	// If we've gotten this far, everything's valid!
	return true;
}

//captcha data
var qList = ["What color is the sky&blue|cyan|orange|black|yellow|red|gray|grey","How many letters are in the word 'Novell'&six|6","What is the sum of one plus ten&eleven|11","What color was John Wayne\'s brown horse&brown"];
var captchaOK = false;
var x = (qList.length);
var i = Math.floor(Math.random()*x);
var getQ = qList[i].split("&");
var question = getQ[0];
var aList = new RegExp(getQ[1]);

function checkCaptcha() {
	var answer = $("input[name='q_answer']").val().toLowerCase();
	if(answer.search(aList) >= 0) {
		$("input[name='nugget']").val("true");
	}
}
$(document).ready(function(){
	$("#question").html("<p>"+getQ[0]+"?<\/p>");
});
