////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////
//////////////////				Here's the AJAX code to check for the Captcha's
//////////////////
//////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var xmlHttp;

function checkCaptcha(code)
{
		xmlHttp=GetXmlHttpObject();
		
		 if (xmlHttp==null)
		 {
		 	alert ("Browser does not support HTTP Request");
		 	return;
		 }
		 var url="/scripts/validate.php?code=" + code;
		 
		 xmlHttp.onreadystatechange=stateChanged;
	 	 xmlHttp.open("GET",url,true);
	 	 xmlHttp.send(null);
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==1)
	{
		
	}
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		xmlDoc=xmlHttp.responseText;
		
		if (xmlDoc == "true")
		{
			forma.submit();
		}
		
		else
		{
			document.getElementById("captcha").innerHTML = '<img src="/scripts/securimage/securimage_show.php?sid=<?php echo md5(uniqid(time())); ?>">';
			forma.code.focus();
			alert("The code is incorrect!");
		}
	}
} 

function GetXmlHttpObject()
 { 
 var objXMLHttp=null;
 if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest();
  }
 else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 return objXMLHttp;
 }


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////
//////////////////				Here's the code to make the form validations
//////////////////
//////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 

enviar = document.getElementsByName("imageField");
forma = document.getElementsByName("form1");
forma = forma[0];

function check()
{ 
	 if (validateForm(forma))
	 {
		checkCaptcha(forma.code.value);	 
	 }
}

function validateForm(frm)
{
	var error = false;
	var errorResults = "";
	var fieldFocus = null;
	
	var textName = frm.name.value;
	var textEmail = frm.email.value;
	var code = frm.code.value;
	
	//First we check that there are not empty fields
	if (textName == "" || textEmail == "" || code == "")
	{
		error = true;
		
		if (textName == "")
		{
		 	errorResults += "The \"Name\" field cannot be empty";
		 	fieldFocus = frm.name;
		}
	
		if (textEmail == "")
		{
		 	if (errorResults != "")
		 	{
				errorResults +=	"\r\n";	
			}
			
			errorResults += "The \"Email\" field cannot be empty";
			
		 	if (fieldFocus == null)
		 	{
		 		fieldFocus = frm.email;
	 		}
		}
		
		if (code == "")
		{
		 	if (errorResults != "")
		 	{
				errorResults +=	"\r\n";	
			}
			
			errorResults += "The \"Code\" field cannot be empty";
			
		 	if (fieldFocus == null)
		 	{
		 		fieldFocus = frm.code;
	 		}
		}
	}
	
	//Nao that we finished checking for error we make the necessary arangements
	if (error)
	{
		fieldFocus.focus();
		
		alert("There are some missing information on the form you are submitting to us please check the following results: \r\n\r\n" + errorResults);
		
		return false;
	}
	
	else
	{
		return true;
	}	
}