var xmlHttp;
var xmlHTTP_VERSIONS = new Array('MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHttp');
var E_ERROR = false; // Email error flag
var G_ERROR = false; // Gloval error flag
var C_ERROR = false; // Captcha error flag

// Initializes XMLHttpRequest Object ###################################################################################################
function GetXmlHttpObject()
{
	try
	 {
	        xmlHttp = new XMLHttpRequest();
	 }

	catch(e)
	 {
	        for(var i=0; i<xmlHTTP_VERSIONS.length && !xmlHttp; ++i)
		 {
		        try
		        {
		        	xmlHttp = new ActiveXObject(xmlHTTP_VERSIONS[i]);
		        }
                        catch(e) {}
		 }
	 }
	 
	 if(!xmlHttp)
	  {
	        alert('Error creating the XMLHttpRequest Object');
			G_ERROR = true;
	        return;
	  }
	 else
	  {
	        return xmlHttp;
	  }
}
// ..................................................................................................................................................................
    function AJAX_ValidateEmail(_email)
     {
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
	  	{
	  		alert ("Your browser does not support AJAX!");
	  		return;
	  	}

		var url="bin/ajax_validate_email.php";
		url=url+"?email="+_email;
		url=url+"&sid="+Math.random();

		xmlHttp.onreadystatechange=stateValidateEmail;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
     }

     function stateValidateEmail()
      {
		  
		switch(xmlHttp.readyState)
		 {
			case 1:
			case 2:
			case 3:
				document.getElementById('img_send').style.visibility='hidden';
				document.getElementById('email_stat').innerHTML= '<img src="images/validate_email1.gif" border="0" title="Validating Email" style="vertical-align: bottom" /> Validating ... ';	
			break;
			
			case 4:
	       		xml_text = xmlHttp.responseText;
	       		xml_text_parsed = xml_text.indexOf('_');
	       		if(xml_text_parsed >= 0)
	       		 {
                		document.getElementById('img_send').style.visibility='hidden';
						document.getElementById('email_stat').innerHTML=' Invalid Email Address';
						E_ERROR = true;
           		 }
           		else
           		 {
					 	AJAX_CheckDupPetitions(document.getElementById('email').value);
                		document.getElementById('img_send').style.visibility='visible';
						document.getElementById('email_stat').innerHTML= '<img src="images/valid.gif" border="0" title="Valid" alt="Valid" style="vertical-align: bottom" />';
						E_ERROR = false;
           		 }
			break;
		 }
		  
		  
      }
// ..................................................................................................................................................................
    function AJAX_CheckDupPetitions(_email)
     {
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
	  	{
	  		alert ("Your browser does not support AJAX!");
	  		return;
	  	}

		var url="bin/ajax_check_duplicate_petition.php";
		url=url+"?email="+_email;
		url=url+"&sid="+Math.random();

		xmlHttp.onreadystatechange=stateCheckDupPetitions;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
     }

     function stateCheckDupPetitions()
      {
		  
		switch(xmlHttp.readyState)
		 {
			case 1:
			case 2:
			case 3:
				document.getElementById('img_send').style.visibility='hidden';
				document.getElementById('email_stat').innerHTML='?';	
			break;
			
			case 4:
	       		xml_text = xmlHttp.responseText;
	       		if(xml_text == "D")
	       		 {
                		document.getElementById('img_send').style.visibility='hidden';
						document.getElementById('email_stat').innerHTML='Sorry, but it seems that you have already submitted this petition once. Only one petition per email address.';
						E_ERROR = true;
           		 }
           		else
           		 {
                		document.getElementById('img_send').style.visibility='visible';
						document.getElementById('email_stat').innerHTML= '<img src="images/valid.gif" border="0" title="Valid" alt="Valid" style="vertical-align: bottom" />';
						E_ERROR = false;
           		 }
			break;
		 }
		  
		  
      }
// ..................................................................................................................................................................
	function AJAX_CastPetition()
	  {
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
	  	{
	  		alert ("Your browser does not support AJAX!");
	  		return;
	  	}


	    var f_name = document.getElementById('fname').value;
	    var l_name = document.getElementById('lname').value;
	    var _email = document.getElementById('email').value;
		var _code = document.getElementById('code').value;
		var _comments = document.getElementById('comments').value;

		if(f_name.length == 0 || l_name.length == 0 || _email.length == 0 || _code.length == 0)
		 {
			G_ERROR = true;	
			document.getElementById('cast_error').innerHTML = "Please make sure you have entered all the required fields under My Information."; 
			return;
		 }
		else
		 {
			G_ERROR = false;		 
		 }

		document.getElementById('img_send').style.visibility='hidden';
		//document.getElementById('success').innerHTML = '<h2>Sending ...</h2>';
		

		if(G_ERROR || E_ERROR || C_ERROR)
		 {
			document.getElementById('cast_error').innerHTML = "We apologize; but there seem to be an error or more in your entries. Please check your entries and try again."; 
			return;
		 }


	    var params = "email=" + _email;
	        params += "&f_name=" + f_name;
	        params += "&l_name=" + l_name;
			params += "&_comments=" + _comments;


			var url="bin/ajax_cast_petition.php";
			url=url+"?sid="+Math.random();
			xmlHttp.open("POST", url, true);
			xmlHttp.onreadystatechange=stateCastPetition;

			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", params.length);
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(params);
	  }

	 function stateCastPetition()
	  {
		
		switch(xmlHttp.readyState)
		 {
			case 1:
			case 2:
			case 3:
				document.getElementById('cast_error').innerHTML = "Connecting to Mail Server.";
			break;
			
			case 4:
				xml_text = xmlHttp.responseText;
				switch(xml_text)
				 {
					case "OK":
						document.getElementById('success').innerHTML = "<h1>Thank you for taking the time to fill out this petition. Your petition was successfully sent.</h1>";
						setTimeout('window.location.reload()', 3000);
					break;
	
					case "OK_NOT_SENT":
						document.getElementById('success').innerHTML = "<h1>Thank you. Your peition was received but failed to send.</h1>" + xml_text;
						setTimeout('window.location.reload()', 10000);
					break;
	
					case "DUP":
						document.getElementById('cast_error').innerHTML = "Sorry, but it seems that you have already submitted this petition once. Only one petition per email address.";
					break;
					 
					case "ERR":
						document.getElementById('cast_error').innerHTML = "We apologize; but the server encountered an unexcpected error. Please try agin";
					break;
	
			break;
		 }
		
		
		
		if (xmlHttp.readyState==4)
		{
			}
		}
	  }
// ..................................................................................................................................................................
	function AJAX_CountPetitions()
	{
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null)
			{
				alert ("Your browser does not support AJAX!");
				return;
			}
	
	

			var url="bin/ajax_count_petitions.php";
			url=url+"?sid="+Math.random();
	
			xmlHttp.onreadystatechange=stateCountPetitions;
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
	}

	function stateCountPetitions()
	{
		if (xmlHttp.readyState==4)
		{
			var _now = new Date();
			xml_text = xmlHttp.responseText;
			if(document.getElementById('petitions_count'))
			 {
				 document.getElementById('petitions_count').innerHTML = xml_text;
				 document.getElementById('petitions_count_aux').innerHTML = 'PETITIONS SIGNED AS OF ' + fnFormatTime(_now.getHours(),'val') + ":" + _now.getMinutes() + ":" + _now.getSeconds() + " " + fnFormatTime(_now.getHours(),'ap');
				 
			 }
			
		}
	}

// ..................................................................................................................................................................
	function AJAX_ValidateCaptcha(_captcha)
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
	  	{
	  		alert ("Your browser does not support AJAX!");
	  		return;
	  	}

		var h_captcha = document.getElementById('h_captcha').value;

		var url="bin/ajax_validate_captcha.php";
		url=url+"?captcha="+_captcha + "&h_captcha=" + h_captcha;
		url=url+"&sid="+Math.random();

		xmlHttp.onreadystatechange=stateValidateCaptcha;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		
		
	}

	function stateValidateCaptcha()
	{
		if (xmlHttp.readyState==4)
		{
			xml_text = xmlHttp.responseText;
			switch(xml_text)
			 {
				case "OK":
                		document.getElementById('img_send').style.visibility='visible';
						document.getElementById('captcha_stat').innerHTML='';
				break;
				 
				case "ERR":
                		document.getElementById('img_send').style.visibility='hidden';
						document.getElementById('captcha_stat').innerHTML='Invalid Security Code';
						setTimeout('window.location.reload()', 1000);
				break;

			}
		}
	}