﻿/**
 @author: Jeremy Manoto
 @description: Asp.NET Web Service Javascript handler
**/


var WSHandler = base2.Base.extend({
    constructor: function(options) {
        this.SetOptions(options);
		
		if (this.Options.Auto) {
			return this.Execute();
		}

    },

    Methods: { POST: "POST", GET: "GET", DELETE: "DELETE", PUT: "PUT" },

    Defaults: {
        ServiceUrl:         "",                     // Full url of the asmx web service
        Method:             "POST",      			// Default method to communicate with Web Service
        Data:               "{}",                   // Data to send to the web service
        Async:              false,                  // Execute the request asynchronously.
        Timeout:            10000,                  // Amount of time to wait for the response (ms)
        Callback:           null,                   // Callback function to execute after submission
        Auto:               true,                   // Automatically make the call to the web service
        onBefore:           null,                   // Function to execute before calling the web service
        onComplete:         null,                   // Function to execute after calling the web service
        onSuccess:          null,                   // Function to execute on successfully execution
        onError:            null,                   // Function to execute when an error occurs
        UseCacheBuster:     false                   // Turn on/off cache busting (useful for GET requests

    },

    Options: {},
	
	Response: null,

    SetOptions: function(options) {
        this.Options = jQuery.extend({}, this.Defaults, options);
    },

    Execute: function() {

		var url = this.Options.Url

        this.XHR = jQuery.ajax({
			url:			url,
            type:			this.Options.Method,
			data:			this.Options.Data,
			dataType:		"json",
			contentType:	"application/json; charset=utf-8",
			timeout:		this.Options.Timeout,
			async:			this.Options.Async,
			processData:	true,
			
			dataFilter:		jQuery.proxy(function(data, type) {
								/*var response = (typeof data) == 'string' ? JSON.parse(data) : data.d;
								response = (typeof response.d) == 'string' ? JSON.parse(response.d) : response.d;
								this.Response = response;*/
			
								var response = eval('(' + data + ')');								
								return response.d;
							}, this),
			
			beforeSend:		jQuery.proxy(function() {
								if (this.Options.onBefore != null) {
									this.Options.onBefore();
								}
							}, this),
			success:		jQuery.proxy(function(response) {
								try {
									// REDUNDANT with dataFilter():  var data = (typeof response.d) == 'string' ? JSON.parse(response.d) : response.d;
									
									if (this.Options.onSuccess != null) {
										this.Options.onSuccess(response);
									}
								} catch (exception) {
									// Possibly Could not parse response
								}
								
							}, this),
			error: 			jQuery.proxy(function(xhr, status, ex) {
								try {
									
								} catch (exception) {
									
								}	
								
								if (this.Options.onError != null) {
									this.Options.onError(xhr);
								}
								
								return;
							}, this),
							
			complete:		jQuery.proxy(function(xhr, status) {
								if (this.Options.onComplete != null) {
									this.Options.onComplete(xhr, status);
								}
							}, this)			
			
        });

    }

})
