/*
Asterop GeoIntelligence Intranet/Internet Framework

(c) Asterop 1999-2007

CTransact / CTransactParam : Client-Server transaction
*/

//*********** Transact Utilities

function PadIntFor2DigitsWith0(iValue)
{
    if (iValue<10)
        return ('0' + iValue);
    else
        return (iValue);
}

function TimeStamp()
{
	var date = new Date();
				
	return date.getFullYear() + PadIntFor2DigitsWith0(1+date.getMonth()) + PadIntFor2DigitsWith0(date.getDate()) + PadIntFor2DigitsWith0(date.getHours()) + PadIntFor2DigitsWith0(date.getMinutes()) + PadIntFor2DigitsWith0(date.getSeconds()) + date.getMilliseconds() + Math.floor(7*Math.random()+1)
}

function ajaxCallEx(url, strHttpMethod, strPostBody, bAsync, onsuccess, onfailure)
{
    //var url = encodeURIComponent('http://www.google.com/search?q=Prototype'); 
    // notice the use of a proxy to circumvent the Same Origin Policy. 
    
    var myAjax = new Ajax.Request(url, 
                            {
                               method: strHttpMethod,
                               asynchronous: bAsync,
                               postBody: strPostBody,                               
                               onSuccess: onsuccess,
                               onFailure: onfailure
                            } 
                     );
    return myAjax;                 
}

//*********** CTransactParam

var CTransactParam = Class.create();

CTransactParam.prototype = {
	initialize: function(name, value)
	{
		this.m_name = name;
		this.m_value = value;		
	}
};

//*********** CTransactParams

var CTransactParams = Class.create();

CTransactParams.prototype = {
	initialize: function()
	{
		this.m_params = new Array();
		this.m_children = null;
	},
	
	AddParam: function(name, value)
	{
		if (this.m_params === null)
	        this.m_params = new Array();       
	    this.m_params.push(new CTransactParam(name, value));
	    return true;
	},
	
	Clear: function()
	{	
		this.m_params = new Array();	    
	},
	
	GetXML: function()
	{
	    var strXML = '';
        var i;
       
        for (i = 0; i < this.m_params.length; i++)
        {
            strXML = strXML + '<'+this.m_params[i].m_name+'>'+this.m_params[i].m_value+'</'+this.m_params[i].m_name+'>';
        }
        
	    if (this.m_children != null)
	    {
            for (i = 0; i < this.m_children.length; i++)
		        strXML = strXML + this.m_children[i].GetXML();
	    }
	    return strXML;
	},

	GetURL: function()
	{
	    var strURL = '';
        var i;
       
        if (this.m_params != null)
        {    
            for (i = 0; i < this.m_params.length; i++)
            {
                if (i>0)
                    strURL = strURL + '&';
                strURL = strURL + this.m_params[i].m_name+'='+ encodeURIComponent(this.m_params[i].m_value);
            }
        }
                
	    if (this.m_children != null)
	    {
            for (i = 0; i < this.m_children.length; i++)
		        strURL = strURL + this.m_children[i].GetURL();
	    }
	    return strURL;
	}
		
};

//*********** CTransact

var c_strTransactFlowType_XML = 'XML';
var c_strTransactFlowType_URL = 'URL';

// Cache configuration management : Default is no caching
var c_iCACHECFG_iNoCache				= 0; // No cache use (specific URL each time => no internet browser or proxy cache use possible)
var c_iCACHECFG_CacheSession			= 1; // Potentially use the caches (internet browser or proxy) within a same user session : same URL for a given identifier and a given user session
var c_iCACHECFG_Cache					= 2; // Always use the potential caches (internet browser or proxy) : always use the same URL for a given identifier

var CTransact = Class.create();

CTransact.prototype = Object.extend(new CTransactResult(),
{
	initialize: function()
	{
	    CTransactResult.prototype.initialize.call();
	    
		this.m_session = new CSession();
		this.m_pTransactparams = new CTransactParams();
		this.m_type = c_strTransactFlowType_XML;
		this.m_bAsynchronous = false;
		this.m_iCacheMode = c_iCACHECFG_iNoCache;
	},

	SetSessionId: function(sessionId)
	{
		this.m_session.SetId(sessionId);
	},

	GetSessionId: function(sessionId)
	{
		return this.m_session.GetId();
	},

    GetParamURL: function()
    {
        return this.m_pTransactparams.GetURL();
    },
   
    GetURL: function()
    {
        var strURL = this.GetServiceDispatcherURL();
        var strURLParams = '';
        
        if (this.GetSessionId() != '')
        {
            strURLParams = "sid=" + this.GetSessionId();
        }
	    switch (this.m_iCacheMode)
	    {
	    case c_iCACHECFG_iNoCache:
			strURLParams += (strURLParams == ''?'':'&') + "t=" + TimeStamp();
	        break;
	    case c_iCACHECFG_CacheSession:
	    case c_iCACHECFG_Cache:
	        // Do not add anything to URL (in the case of a session, the session id has been added)
	        break;
	    }
	            
        if (this.m_type == c_strTransactFlowType_URL)
        {
			strURLParams += (strURLParams == ''?'':'&') + this.GetParamURL();
        }
        
        strURL += (strURL.indexOf('?')!=-1?'&':'?') + strURLParams;
        return strURL;         
    },
    
    // PUBLIC functions
	SetParams: function(params, values)
	{
		this.ClearParams();
		
		if (params.length == values.length)
		{
            for (var i = 0; i < params.length; i++)
		        AddParam(param[i], values[i]);
		    return true;
		}
		else
		    return false;		
	},

	AddParam: function(param, value)
	{
		return this.m_pTransactparams.AddParam(param, value);	    
	},
	
	ClearParams: function()
	{
		if (this.m_pTransactparams)
			this.m_pTransactparams.Clear();	    
	},

    SetType: function(iFlowType)
    {
        this.m_type = iFlowType;
    },

    SetMode: function(bAsync)
    {
        this.m_bAsynchronous = bAsync;
    },
    
	Call: function()
	{
	    var strXML;
	    var strURL = this.GetURL();	       
	    
	    switch (this.m_type)
	    {
	    case c_strTransactFlowType_XML:
	        strXML = this.m_pTransactparams.GetXML();
	        this.m_result = ajaxCallEx(strURL, 'post', strXML, this.m_bAsynchronous, null, null);
	        break;
	    case c_strTransactFlowType_URL:
	        this.m_result = ajaxCallEx(strURL, 'get', null, this.m_bAsynchronous, null, null);
	        break;
	    }
	    return this.GetResult();
	},
	
	// VIRTUAL functions to be overridden by custom code
	GetServiceDispatcherURL: function()
	{
	    return 'http://localhost/MyApp/DispatchService.aspx';	    
	}
});

