//////////////////////////////////////////////////////////////////////////////////////////////////////////
// NOTE:   This demo will be simplified and bought into line with the new SOAPjr lite jQuery plugin API
//////////////////////////////////////////////////////////////////////////////////////////////////////////

// This is just used for the demo so we can change the script called easily
var script = "SOAPjr.pl";

// This is a simple demonstration method
function demo() {
	/*
		Common SETUP design pattern:
		----------------------------
		- create a SOAPjr_request object
		- set the HEAD and BODY values
		- then call send()

		The real work that handles the response is done in the callback below
	*/


	// USE JQUERY to create a vanilla SOAP-jr request object
	var url = "http://"+document.location.host+"/demos/"+script;
	var SOAPjr_request = $.SOAPjr.create_request({
		"ENVELOPE" : {
			"url" : url
		},
		"OPTIONS" : {
			"callback" : "my_callback"
		}
	});

	// or you could USE THAT OBJECT's set() method to set your own callback
	//SOAPjr_request.OPTIONS.set({
	//	"callback" : "my_callback"
	//});

	// USE THAT OBJECT's set() method to set a value in HEAD
	SOAPjr_request.set({
        "HEAD" : {
		    "service_type" : "customer",
		    "action_type" : "view",
		    "sid" : "1ec33acfd0ddb7ce0e2015e7f649624e"
        },
        "BODY" : {
		    "cust_id" : "23427771"
        }
	});

	// USE THAT OBJECT to send() itself
	var result = SOAPjr_request.send();

	// NOTE: result is the result of the send() call and NOT the response object itself as AJAX calls are asynchronous
	// 	 the response object is handed back to the callback (along with the request object and xhr that was used)
	$("#send").html("<h2>Message Info:</h2>URL: "+url+"<br>Send result: "+JSON.stringify(result.get()));
}

// This is the callback method you setup to handle the response object you get back
function my_callback(SOAPjr_response, SOAPjr_request, xhr) {
        /*
        	Common CALLBACK design pattern:
        	-------------------------------
		- get the response object's HEAD (for convenience)
		- if the result was successful process the BODY
		- else get the errors object and process any errors you find

		NOTE:	Now all transport, application and validation errors can be handled in a common way.
			Also, multiple errors can easily be returned and common or domain specific error codes can also be standardised.
        */


        // Get the HEAD of the response object
        var resp_head = SOAPjr_response.get("HEAD");

        // If the request was successful then get the BODY and process it
        if (resp_head.result == 1) {
                var body = JSON.stringify(SOAPjr_response.get("BODY"));
                $("#output").html("<h2>Now you can process the BODY:</h2>"+body+"<br>");	// do whatever you need to here with the data you successfully got back

        // Else see what errors we had
        } else {
                // We'll create a string to dump the results into for this demonstration
                var results = "";

                // Let's see if the request HEAD generated any errors
                results += "<h2>Errors in HEAD;</h2>";
                var h = resp_head.errors.get("HEAD");
                for (prop in h) {
                        var code = h[prop].code;
                        var msg= h[prop].message;
                        results += "Error type:"+prop+"<br>Error code: "+code+"<br>Error message: "+msg+"<br>";			// do whatever you need to here to handle the errors
                }
                results += "<br>";

                // Let's see if the request BODY generated any errors
                results += "<h2>Errors in BODY;</h2>";
                var b = resp_head.errors.get("BODY");
                for (prop in b) {
                        var code = b[prop].code;
                        var msg= b[prop].message;
                        results += "Error code ("+prop+"):"+code+"<br>Error message: "+msg+"<br>";			// do whatever you need to here to handle the errors
                }
                results += "<br>";

                // You could repeat this for ENVELOPE and OPTIONS too

		// Here's what a non-SOAPjr request would get back
		results += "<h2>Here's the same NON-SOAPjr response :</h2><pre>"+xhr.responseText+"</pre>";

                // For this demonstration we'll just alert the errors
                $("#output").html(results);
        }

        $("#sent").html(
                "<h2>Here's the request you created:</h2>"+
                JSON.stringify(SOAPjr_request.get())+"<br>"
        );
        $("#received").html(
                "<h2>Here's the response you got back:</h2>"+
                JSON.stringify(SOAPjr_response.get())+""
        );

}
