/**
 * @author Michal Donat, based on Steven Rowe's version.
 */

	var Callback = new Object();
	
	/**
	 * Starts the callback
	 * 
	 * @param string user_cli - users phone number
	 * @param string phone_country - users/number country
	 * @param bool is_mobile - mobile/landline indicator
	 * @param string service_name - name of the service (AMPAYBILL, PHONEVER etc)
	 */
	Callback.startCallback = function(user_cli , phone_country , is_mobile , service_name, minutes) 
	{
		if(typeof(minutes) == 'undefined') minutes = null;

		var add = '';
		
		if(minutes != null) {
			var add = "&minutes="+minutes;
		} 

		if( Callback.busy() == 0 )
		{
			Callback.busy( 1 );
			
			var Ajax = new AjaxRequest();
			if( Ajax.mRequest )
			{			
				Ajax.mCli = user_cli;
				Ajax.mCountry = phone_country;
				Ajax.mType = is_mobile;
				Ajax.mServiceName = service_name;
				
				Ajax.mActionMethod = 'checkStatus';
				
				setTimeout(
					function() { 
						Callback.checkStatus();
					} , 60000
				);
					
				Ajax.addEventListener( REQ_COMPLETE, Callback.processStartCallback );
				Ajax.mRequest.open( "POST", "/ajax/getCallback.php?cli="+ Ajax.mCli +"&country="+ Ajax.mCountry +"&cli_type="+ Ajax.mType +"&service_name="+ Ajax.mServiceName+add  , true );
				Ajax.mRequest.send( '1' );		
			}
		}
	}
	
	Callback.startConferenceCallback = function(user_cli, destination_pn, phone_country , is_mobile , service_name) 
	{
		
		if( Callback.busy() == 0 )
		{
			Callback.busy( 1 );
			
			var Ajax = new AjaxRequest();
			if( Ajax.mRequest )
			{			
				Ajax.mCli = user_cli;
				Ajax.mCountry = phone_country;
				Ajax.mType = is_mobile;
				Ajax.mServiceName = service_name;
				Ajax.mDestinationPN = destination_pn;
				Ajax.mActionMethod = 'checkStatus';
				
				setTimeout(
					function() { 
						Callback.checkStatus();
					} , 60000
				);
					
				Ajax.addEventListener( REQ_COMPLETE, Callback.processStartCallback );
				Ajax.mRequest.open( "POST", "/ajax/getCallback.php?cli="+ Ajax.mCli +"&country="+ Ajax.mCountry +"&cli_type="+ Ajax.mType +"&service_name="+ Ajax.mServiceName+"&conference="+Ajax.mDestinationPN  , true );
				Ajax.mRequest.send( '1' );		
			}
		}
	}

	/**
	 * Processes the callback response and calls the Callback.checkStatus method
	 * 
	 * @param AjaxRequest pAjax - ajax object
	 */
	Callback.processStartCallback = function(pAjax)
	{	
		if( pAjax.mRequest.status == 200 )
		{
			var data = eval( pAjax.mRequest.responseText );
			
			var mReturnCode = ( typeof data.callback.returnCode == 'undefined' ) ? null : data.callback.returnCode;
			
			var mResultMessage = ( typeof data.callback.resultMessage == 'undefined' ) ? null : data.callback.resultMessage;
					
			if( pAjax.mActionMethod )
			{
				var newArgs = [];
				newArgs.push(mReturnCode);
				newArgs.push(mResultMessage);
								
				if( Callback[ pAjax.mActionMethod ] ) Callback[ pAjax.mActionMethod ].apply( this, newArgs );
			}
		} 
	}


	/**
	 * Checks for any technical error
	 * 
	 * @param int status_id - status_id returned by ajax, error if -1
	 * @param string message - additional error message returned by ajax
	 */
	Callback.checkStatus = function(status_id, message)
	{
		switch( status_id )
		{
			case '0':
				Micropayments.FrontController.ShowStartNotice();
				break;
			default:
				if(Micropayments.Verified === false)
				{
					// For phone verificaiton when we getting a timeout. Showing error message.
					Micropayments.FrontController.ShowTryAgain("Your callback was not successful. Please check your number and try again.");
					Callback.busy( 0 );
				}
		}
	}

	
	/**
	 * Use to get/set the status of a call. If the user has already started a call then mark them as busy and vice versa.
	 */
	Callback.Status = false;
	
	Callback.busy = function( status )
	{
		if( typeof( status ) != 'undefined' )
			Callback.Status = status;
		else
			return Callback.Status;
			
	}
	

		
