﻿/// <reference path="scripts/jquery-1.3.2-vsdoc.js" />
/// <reference path="scripts/jquery.validate.js" />
(function($) {

    $.goTo = function(url) {
        top.location.href = url;
    }

    $.definedOrDefault = function(value, ifUndefined)
    {
        if (typeof value === "undefined")
            return ifUndefined;
        return value;
    }

    //Similar to $.ajax, but has a few extras:
    //1.  Meant for calling an asp.net web service passing JSON data
    //2.  Adds a default error message for the user
    //3.  Hides the "result.d" parameter that asp.net passes back in web services
    //4.  Introduces a "returned()" function in the opts that occurs before either error() or success()
    //5.  Automatically serializes the data (NOTE: opts.data should be a function so it gets computed after teh form submits, otherwise form elements will be wrong)
    $.wsAjax = function(opts, form) {
        if (form)
            form = $(form);
        //if the form is already being submitted (user pressed enter twice or hit the button twice), we ignore this submission
        if (form && form.data("submitting")) {
            return false;
        }

        var ajaxOps = jQuery.extend({}, opts); //make a shallow copy of opts because it can be used for multiple form submissions, and overwriting data the first time would screw things up the second time

        //data needs to be a fucntion so it gets evaluated as the form is validated, not during page load
        if (opts.data)
            ajaxOps.data = $.toJSON(opts.data());
        ajaxOps.type = "POST";
        ajaxOps.contentType = "application/json; charset=utf-8";
        ajaxOps.dataType = "json";
        if (form && !opts.url)
            ajaxOps.url = form[0].action;

        ajaxOps.error = function(XMLHttpRequest, textStatus, errorThrown) {
            if (opts.returned)
                opts.returned(false);
            if (form)
                form.data("submitting", false);
            //execute the specified 
            if (opts.error) {
                opts.error(XMLHttpRequest, textStatus, errorThrown);
            }
            else {
                alert('Uh oh!  There was an error while communicating with our server.  ' + XMLHttpRequest.statusText + XMLHttpRequest.responseText);
            }
        };

        ajaxOps.success = function(result) {
            if (opts.returned)
                opts.returned(true); //call our custom "returned" function and indicate success

            if (form)
                form.data("submitting", false);

            if (opts.success) {
                //asp.net returns a result.d that contains the actual results.  it's some security measure.  hide that little fact from our normal code
                opts.success(result.d);
            }
        };

        if (form)
            form.data("submitting", true);
        $.ajax(ajaxOps);

    };


    //Creates the submitHandler for submitting a form to a web service.  Just saves a few lines of code.
    $.wsFormSubmitHandler = function(opts) {
        return function(form) {
            $.wsAjax(opts, form);
            return false;
        }
    };
})(jQuery);

