﻿

/*

Der Name in der Parameter-Liste muss die parameter der C# Funktion im WS matchen:
z.B.: "mailAdress"

WebService("AddToMailingList", ["mailAdress",tb.val()], MailingListSuccess, MailingListContentError);

[WebMethod(EnableSession = true)]
    public void AddToMailingList(string mailAdress)
    {
        string a = "";        
    }

*/

function WebService(fn, paramArray, successFn, errorFn)   
{   
    var pagePath = window.location.pathname;   
    //Create list of parameters in the form:   
    //{"paramName1":"paramValue1","paramName2":"paramValue2"}   
    var paramList = '';   
    if (paramArray.length > 0)   
    {   
        for (var i=0; i<paramArray.length; i+=2)   
        {   
            if (paramList.length > 0) paramList += ',';   
                paramList += '"' + paramArray[i] + '":"' + paramArray[i+1] + '"';   
        }   
    }   
    paramList = '{' + paramList + '}';   
    //Call the page method   
    $.ajax({   
        type: "POST",               
        url: fn,   
        contentType: "application/json; charset=utf-8",   
        data: paramList,   
        dataType: "json",   
        success: successFn,   
        error: errorFn   
    });
}

//                                                                                                    
// From: http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/  
//                                                                                                    
function WebServiceStringList(fn, list, successFn, errorFn)   
{   
    var jsonText = JSON.stringify({ list: list });    // requires json2.js or json2.min.js
    $.ajax({   
        type: "POST",               
        url: fn,
        contentType: "application/json; charset=utf-8",   
        data: jsonText,   
        dataType: "json",   
        success: successFn,   
        error: errorFn   
    });    
} 


function trim (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}



