﻿
/*
serviceName: for example products
methodName: for example GetProduct
dataObject: a javascript object with properties corelating to the parameter names of the method you call, for example { productId : 123, marketId : 23, userName : 'John' }
onCompleteMethod: optional. A function to call when the ajax call has completed. It will received 2 parameters: the return value of the method you called and whatever you put in the context parameter
context: optional. an object that gets passed on to the onCompleteMethod. Good for reference and context
onFailMethod: optional. called if the ajax method fails (duh!)
*/
function MakeAjaxCall(serviceName, methodName, dataObject, onCompleteMethod, context, onFailMethod) {
    if (dataObject == null)
        dataObject = new Object();
    $.ajax({
        type: "POST",
        url: serviceName + ".svc/" + methodName,
        data: JSON.stringify(dataObject),
        contentType: "application/json; charset=utf-8",
        dataType: "json", //do not specify datatype. it will mess up the datafilter
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined" && msg.hasOwnProperty('d'))
                msg = msg.d;
            if (typeof onCompleteMethod === "function")
                onCompleteMethod(msg, context);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            if (onFailMethod != null) {
                onFailMethod(XMLHttpRequest, textStatus, errorThrown);
            }
            else
                OnAjaxFailed(XMLHttpRequest, textStatus, errorThrown);
        }
    });
}
function OnAjaxFailed(XMLHttpRequest, textStatus, errorThrown) {
    debugger;
    alert(XMLHttpRequest.responseText + "" + textStatus.responseText + "" + errorThrown.responseText);
}



function SendMail() {
    debugger;
    var tblHtml = $('#tblOmsattning').html();
    
    MakeAjaxCall("Service", "SendConfirmationMail", { html: tblHtml }, OnMailSent);
}


function OnMailSent() {
    alert('Mailet har skickats iväg.');
}
