// JavaScript Document

function getHttpRequest()
{
	try 
	{ 
	   http = new XMLHttpRequest(); /* e.g. Firefox */ 
	} 
	catch(e) 
	{ 
	   try 
	   { 
		   http = new ActiveXObject("Msxml2.XMLHTTP"); /* some versions IE */ 
		} 
		catch (e) 
		{ 
		   try 
			{ 
			   http  = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */ 
			} 
			catch (E) 
			{ 
			   http  = false; 
			} 
		} 
	} 
	
	return http;
}

function sendEmail(email) 
{
   http = getHttpRequest();
	
	var url = "email.php"; 
	var myRandom = parseInt(Math.random()*99999999); // cache buster 
	
	http.open("GET", url + "?email=" + escape(email) + "&rand=" + myRandom, true); 
	
	http.onreadystatechange = handleHttpResponse; // Call handleMemberHttpResponse if ready state changes
	
	http.send(null); 	
}

function handleHttpResponse()
{ 
   if (http.readyState == 4) 
	{
		/* Not sure why we aren't getting a status 200, but this seems to work
		
		if ( http.status == 200 )
		{	
         alert ( "Thanks for your interest in the Jackson County Brevet!" );
		}
      else
		{
			alert ( "There was a problem receiving the data: \n" + http.statusText + " - " + http.status );
		}
		*/
		
		alert ( "Thanks for your interest in the Jackson County Brevet!" );
   }
}				

/**
 * DHTML email validation. The following function (echeck, ValidateForm) courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
 
function echeck(str) 
{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please enter a valid email address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter a valid email address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter a valid email address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter a valid email address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter a valid email address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter a valid email address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please enter a valid email address")
		    return false
		 }

 		 return true					
	}

function ValidateForm()
{
	var emailID=document.email_updates.email
	
	if ((emailID.value==null)||(emailID.value==""))
	{
		alert("Please enter your email address")
		emailID.focus()
		return false
	}
	
	if (echeck(emailID.value)==false)
	{
		emailID.value=""
		emailID.focus()
		return false
	}
	
	sendEmail(emailID.value);
	
	return true
}

