//Dynamic Function to manage AJAX Requests
function Ajaxer()
{
	var InParameters = new Array();
	var ScriptableURL = "";
	var TransportMethod = "GET";
	var FunctionObjLoading = null;
	var FunctionObjLoaded = null;
	var FunctionObjInteractive = null;
	var FunctionObjComplete = null;
	var TransportObj = null;	
	var Asynchronous_Mode = true;
	
  	//HTTP-HTTPS method: POST/GET, recommendation, use GET to displaying info, use POST when you are saving info 
	this.setMethod = function(methodName)
	{
		methodName = methodName.toUpperCase();
		if ( methodName=="POST" )
			TransportMethod="POST";
		else
			TransportMethod="GET";		
	} // setMethod
	
	//Sets vars to send during the reques
	this.setParameter = function(VarName,VarValue)
	{
		if (InParameters==null)
			InParameters = new Array();
		InParameters.push(VarName+"="+escape(VarValue));
	} // setParameter	
	
  	//Sets an asynchronous function during execution state
	this.setAsynchronous = function(sync)
	{
		Asynchronous_Mode = sync;
	} // setAsynchronous_Mode
	
	//Function during the loading state
	this.setLoading = function(FunctionObj)
	{
		if((FunctionObj!=null)&& (typeof FunctionObj=="function" ))
			FunctionObjLoading = FunctionObj;
	} // setLoading	

  	//Returns the response text for the last execution.
	this.getResponseText = function()
	{
		if (TransportObj!=null )
		{
			if((TransportObj.readyState==4)||(TransportObj.readyState=="complete"))
				return TransportObj.responseText;
			else
				return '';
		}
		else
		{
			return '';
		}
	} // getResponseText
	
	//Sets the function to be used when loaded state*/
	this.setLoaded = function(FunctionObj)
	{
		if (FunctionObj!=null && (typeof FunctionObj=="function" ))
			FunctionObjLoaded = FunctionObj;
	} // setLoaded
	
	//Sets the function to be used when interactive state
	this.setInteractive = function(FunctionObj)
	{
		if (FunctionObj!=null && (typeof FunctionObj=="function" ))
			FunctionObjInteractive = FunctionObj;
	} // setInteractive

	//Sets the function to be used when complete state
	this.setComplete = function(FunctionObj)
	{
		if (FunctionObj!=null &&(typeof FunctionObj=="function"))
			FunctionObjComplete=FunctionObj;
	} // setComplete

  	//Sets the request application URL.
	this.setURL = function(scripturl)
	{
		if((scripturl!=null)&&(scripturl.length>0))
			ScriptableURL = scripturl;
	} // setURL
	
	//	Sets the function to be used when complete state
	this.getHeader = function( headerName )
	{
		if ( headerName!=null && headerName.length>0 && TransportObj!=null )
			return TransportObj.getResponseHeader( headerName );
		else
			return '';
	} // getHeader

  	//Returns the response XML for the last execution.
	this.getResponseXML = function()
	{
		if ( TransportObj!=null )
		{
			if ( TransportObj.readyState==4 || TransportObj.readyState=="complete" )
				return TransportObj.responseXML;
			else
				return null;
		}
		else
		{
			return null;
		}
	} // getResponseXML
	
	//Eval mode if the response contains a javascript function to eval
	this.evalReponseText = function()
	{
		var responseText = this.getResponseText();
		if (responseText!=null && responseText.length>0 )
		{
			try
			{
				return eval(responseText);
			}
			catch(e)
			{
			}
		}
	} // evalReponseText

	//Handles the states for the last request
	this.catchCallBack = function()
	{
		if ( TransportObj!=null )
		{
			switch(TransportObj.readyState)
			{
				// loading object
				case 1:
					if (FunctionObjLoading!=null)
						FunctionObjLoading.call();
				break;
				//loaded
				case 2:
					if (FunctionObjLoaded!=null)
						FunctionObjLoaded.call();
				break;
				//interactive
				case 3:
					if (FunctionObjInteractive!=null)
						FunctionObjInteractive.call();
				break;
				//complete
				case 4:
					var contentType = TransportObj.getResponseHeader('Content-type').toLowerCase();
					var isJavaScript = ( contentType.indexOf("javascript")>0 );
					if(isJavaScript==true)
					{
						try
						{
							eval(TransportObj.responseText);
						}
						catch(e)
						{
							alert("Cannot execute the AJAX response.\n[ " + e.toString() + " ]");
						}
					}
					else
					{
						FunctionObjComplete.call();
					}
				break;
			}
		}
	}
  	/*Prepare transport method*/
	this.setTransportMethod = function()
	{
		var objRequest = null;
		if ( objRequest==null )
		{
			// First attemp, IE
			if ( window.ActiveXObject )
			{	
				var MSTransportProtocols = new Array('Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
				for ( var i=0; i<MSTransportProtocols.length && objRequest==null; i++ )
				{
					try
					{
						objRequest = new ActiveXObject(MSTransportProtocols[i]);
						break;
					}
					catch(e)
					{
					}
				}		
			} 	
			//Standar way: FF, Safari, Chrome and Opera
			if(objRequest==null && (window.XMLHttpRequest))
			{
				objRequest = new XMLHttpRequest();
			}
		}
		if (objRequest==null)
			throw new Error("Your browser does not support AJAX, please use Firefox, IE 7 or upper, Safari, Opera or Chrome.");
		else
			TransportObj = objRequest;
	} // setTransportMethod
	
  	//Prepare the XMLHTTPRequest headers.
	this.setObjHeaders = function()
	{
		if ( TransportObj!=null )
		{
			TransportObj.setRequestHeader("X-Requested-With","XMLHttpRequest");
			TransportObj.setRequestHeader("If-Modified-Since","Sat, 1 Jan 2000 00:00:00 GMT");
			if ( TransportMethod=="POST" )
				TransportObj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			if ( TransportObj.overrideMimeType )
				TransportObj.setRequestHeader("Connection","close");
		}
	} // setObjHeaders
  	//Executes the request.
	this.execute = function()
	{
		try
		{
			this.setTransportMethod();
			this.setParameter("FA_AJAXID",Math.random());
			if (TransportMethod=="GET")
				ScriptableURL = ScriptableURL+"?"+InParameters.join("&");
			TransportObj.open( TransportMethod,ScriptableURL, Asynchronous_Mode );	
			TransportObj.onreadystatechange=this.catchCallBack;				
			this.setObjHeaders();	
			TransportObj.send((TransportMethod=="POST"?InParameters.join("&"):null));
			InParameters = new Array();
		}
		catch(e)
		{
			alert("Exception during the execution of the ajax request.\n[ " + e.toString() + " ]");
		}
	} // execute
} //end of Ajaxer function
//user controller function: process all the ajax request into a centralized function
function EmailActivationControl(email)
{
	var EmailAjax = new Ajaxer(); //AccountAjax - Ajaxer Module
	var InputsArray = new Array();
	var LoadingMessage ="";

	var FixedUrl = fixAjaxURL();	
	EmailAjax.setMethod("post");
	EmailAjax.setURL(FixedUrl+'phpajax/emailactivation.ajax.php');
	
	////////
	EmailAjax.setParameter('email',email);
	EmailAjax.setComplete(UserControlResponse);
	EmailAjax.execute();
}
function UserControlResponse()
{
	var EmailAjax = new Ajaxer(); //AccountAjax - Ajaxer Module
	if((EmailAjax.getResponseText()==1)||(EmailAjax.getResponseText()=='1'))
	{
		alert('Your email activation has been sent');
	}
	else
	{
		alert('Your email activation already sent');
	}
}

