var webClose = {

    handleSuccess:function(o)
    {
        //alert("webClose.handleSuccess: " + o.responseText);
     },

    handleFailure:function(o){
        //alert("webClose.handleFailure: " + o.statusText);
    }
};

var thirdPartyCall = {

    handleSuccess:function(o)
    {
        if(o.responseText == "")          
            return;
        var response = YAHOO.lang.JSON.parse(o.responseText);
        this.callID = response.CallID;
        
        if ( response.StatusCode < 300 && this.ActionID == response.ActionID )
            this.queryState();
        
        if ( response.StatusCode != 101 )
            this.onMessage(response);
    },

    handleFailure:function(o){
        //alert("thirdPartyCall.handleFailure: " + o.statusText);
    },

    placeCall:function(SubscriberID, ContactID, VisitorContact, Language, Referral, VisitorID, VisitorCookie, Referer, MD5Hash, MD5TS, onMessage) {
        
        var jReq = new Object();
        jReq.Command = "place_call";
        jReq.SubscriberID = SubscriberID;
        jReq.ContactID = new Number(ContactID);
        jReq.VisitorContact = VisitorContact;
        jReq.Language = Language;
        jReq.Referral = Referral;
        jReq.Referer = Referer;
        jReq.VisitorID = VisitorID;
        jReq.VisitorCookie = VisitorCookie;
        jReq.ActionID = Math.random().toString();
                
        this.onMessage = onMessage;
        this.ActionID = jReq.ActionID;

        this.MD5Hash = MD5Hash;
        this.MD5TS = new Number(MD5TS);
        this.SubscriberID = SubscriberID;
        this.ContactID = new Number(ContactID);

        jReq.MD5 = this.MD5Hash;
        jReq.Timestamp = this.MD5TS;
        
        var srequest = YAHOO.lang.JSON.stringify(jReq);
                
        YAHOO.util.Connect.setDefaultPostHeader( false );
        YAHOO.util.Connect.initHeader("Content-Type", "application/json", true);
        YAHOO.util.Connect.initHeader("Accept", "application/json", true);

        YAHOO.util.Connect.initHeader("X-AbbeySDKVersion", "1.3", true);
        
        this.request = YAHOO.util.Connect.asyncRequest('POST', 'ThirdPartyCall', tpcCallback, srequest);
    },
    
    queryState: function() {
        var jReq = new Object();
        jReq.CallID = this.callID;
        jReq.Command = "query_state";
        jReq.ActionID = Math.random().toString();
        
        this.ActionID = jReq.ActionID;
               
        var srequest = YAHOO.lang.JSON.stringify(jReq);
        
        YAHOO.util.Connect.setDefaultPostHeader( false );
        YAHOO.util.Connect.initHeader("Content-Type", "application/json", true);
        YAHOO.util.Connect.initHeader("Accept", "application/json", true);

        YAHOO.util.Connect.initHeader("X-AbbeySDKVersion", "1.3", true);
        
        this.request = YAHOO.util.Connect.asyncRequest('POST', 'ThirdPartyCall', tpcCallback, srequest);        
    },
    
     closeCall: function(){
        var jReq = new Object();
        jReq.Command = "close_call";
        jReq.CallID = this.callID;
        jReq.ActionID = Math.random().toString();

        jReq.SubscriberID = this.SubscriberID;
        jReq.ContactID = this.ContactID;
        jReq.MD5 = this.MD5Hash;
        jReq.Timestamp = this.MD5TS;
        
        var srequest = YAHOO.lang.JSON.stringify(jReq);
            
        YAHOO.util.Connect.setDefaultPostHeader( false );
        YAHOO.util.Connect.initHeader("Content-Type", "application/json", true);
        YAHOO.util.Connect.initHeader("Accept", "application/json", true);

        YAHOO.util.Connect.initHeader("X-AbbeySDKVersion", "1.3", true);

        YAHOO.util.Connect.asyncRequest('POST', 'ThirdPartyCall', webCloseCallback, srequest);
    },

    closeAllCalls: function(callIDList){         
        for(var i=0; i<callIDList.length; ++i){
            this.callID = callIDList[i];
            this.closeCall();
        }
        callIDList.splice(0, callIDList.length);
    }//end
};

var tpcCallback =
{
    success: thirdPartyCall.handleSuccess,
    failure: thirdPartyCall.handleFailure,
    scope: thirdPartyCall
};

var webCloseCallback =
{
    success: webClose.handleSuccess,
    failure: webClose.handleFailure,
    scope: webClose
};

function getEndpointString(endpoint) 
{
    var endpoint = statusMap[endpoint.toString()];
    if ( typeof(status) == 'undefined' )
        return "";
    else
        return endpoint;
}

function getStatusString(message) 
{
    var code = message.StatusCode;
    var status;
    if ( code < 100 || code >= 800 )
        status = statusMap["XXX"];
    else if(code == 250){
        status = statusMap["250"];
    }
    else {
        status = statusMap[code.toString()];
        if (typeof(status) == 'undefined') {            
            code = Math.round(code / 100 - 0.5);
            //alert(code);
            if ( code != message.StatusCode ) {
                message.StatusCode = code * 100;
                return getStatusString(message);
            }
            else
                status = statusMap["XXX"];                
        }
    }   
    status = status.replace("%s", getEndpointString(message.Source));
    return status.replace("%d", getEndpointString(message.Destination));
}



