// Taken from https://www.dpd.co.uk/js/Redback.js

function Redback ( operation ) {

this . opertaion = operation ;

this . async = false ;

this . fields = { } ;

}

Redback. prototype = {

setField : function ( name , value ) {

this . fields [ name ] = value ;

} ,

getField : function ( name ) {

return this . fields [ name ] ;

} ,

setAsync : function ( bool ) {

this . async = bool ;

} ,

toJSON : function ( ) {

var jfields = "" ;

var first = true ;

for ( var field in this . fields ) {

if ( first ) first = false ;

else jfields += "," ;

jfields += "{ \" name \" : \" " + field + " \" ," ;

jfields += " \" value \" : \" " + this . getField ( field ) + " \" }

"

}

return "{" +

" \" fields \" : [" + jfields + "]" +

"}" ;

} ,

sendToRedback : function ( successCallback , failureCallback ) {

jQuery. ajax ( {

url : "/redback/invoke/" + this . opertaion ,

accept : "application/json" ,

contentType : "application/json" ,

type : "POST" ,

processData : false ,

data : this . toJSON ( ) ,

dataType : "json" ,

success : function ( data , textStatus , jqXHR ) {

var rb = new Redback ( this . opertaion ) ;

for ( var i = 0 ; i < data. fields . length ; i ++ ) {

rb. setField ( data. fields [ i ] . name , data. fields [ i ] . value ) ;

}

successCallback ( rb , jqXHR. responseText ) ;

} ,

error : function ( jqXHR , textStatus , errorThrown ) {

failureCallback ( textStatus , errorThrown ) ;

} ,

async : this . async

} ) ;

}