//animatedcollapse.addDiv('contactForm', 'hide=0,fade=1');
//animatedcollapse.addDiv('contactFormButtonSend', 'hide=0,fade=0');
//animatedcollapse.addDiv('contactFormButtonSending', 'hide=1,fade=0');
//animatedcollapse.addDiv('contactFormDone', 'hide=1,fade=1');
//animatedcollapse.addDiv('contactFormError', 'hide=1,fade=1');

function checkContactForm()
{
  if (document.getElementById("name").value == "")
  {
    alert ('Please enter your Name.');
    document.getElementById("name").focus();
	return false;
  }
  if (document.getElementById("gender").value == "-")
  {
    alert ('Please enter your Gender.');
    document.getElementById("gender").focus();
	return false;
  }
  if (document.getElementById("email").value == "")
  {
	  alert ('Please enter your Email address.');
      document.getElementById("email").focus();
	  return false;
  }
  if (checkEmailAddress(document.getElementById("email").value)==false)
  {
    alert ('Please enter a valid Email address.');
    document.getElementById("email").focus();
	return false;
  }
  if (!document.getElementById("iagree").checked)
  {
    alert ('You must agree to the Terms & Conditions before you can enter the draw');
	return false;
  }
    return true;
}

function checkEmailAddress(emailAddress)
{
  var allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.@-_";
  var emailLength = emailAddress.length;
  var atSignPosition = 0;
  var finalDotPosition = 0;
  var numberOfDots = 0;
  var numberOfAtSigns = 0;

  if (emailLength < 5)
  {
    return false;
  }

  for (i = 0; i < emailLength ;i ++)
  {
    if (allowed.indexOf(emailAddress.charAt(i))<0)
    {
      return (false);
    }
    if ((emailAddress.charAt(i)) == "@")
    {
      numberOfAtSigns ++;
      atSignPosition = i;
    }
    else
    {
      if ((emailAddress.charAt(i)) == ".")
      {
        numberOfDots ++;
        finalDotPosition = i;
      }
    }
  }
  if (numberOfDots == 0)
  {
    return (false);
  }
  if (numberOfAtSigns != 1)
  {
    return (false);
  }
  if (atSignPosition > finalDotPosition)
  {
    return (false);
  }
  if (finalDotPosition == (emailLength-1))
  {
    return (false);
  }
  return (true);
}

function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i])
   }
   catch(e){
    //suppress error
   }
  }
 }
 else if (window.XMLHttpRequest) // if Mozilla, Safari etc
  return new XMLHttpRequest()
 else
  return false
}

function submitForm(){
	
document.getElementById("result").innerHTML = "<i>Checking For Errors...Please Wait</i>";
	
if ( checkContactForm() )
{
	document.getElementById("result").innerHTML = "<i>Processing...Please Wait</i>";
	
	//animatedcollapse.hide('contactFormButtonSend');
	//animatedcollapse.show('contactFormButtonSending');
	
	var mypostrequest=new ajaxRequest()
	mypostrequest.onreadystatechange=function(){
	 if (mypostrequest.readyState==4){
	  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
		   //alert("WIN");
		   //animatedcollapse.hide('contactForm');
		   //animatedcollapse.show('contactFormDone');
		   document.getElementById("result").innerHTML = mypostrequest.responseText;
	  }
	  else{
	   alert("An error has occured making the request");
	   //animatedcollapse.hide('contactForm');
	   //animatedcollapse.show('contactFormError');
	  }
	 }
	}
	
	var parameters = "name=" + encodeURIComponent( document.getElementById("name").value ) + 
					"&gender=" + encodeURIComponent( document.getElementById("gender").value ) +
					"&email=" + encodeURIComponent( document.getElementById("email").value );
			
	
	//alert(parameters);			
	
	mypostrequest.open("POST", "/draw_add.php", true)
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	mypostrequest.send(parameters)
	
	}
	else
	{
		document.getElementById("result").innerHTML = "Fix Errors and Submit Again";
	}
}
