$.ajaxSetup({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    timeout: 5000
});


function wishlist(event, esroid, show, hall, date, returnURL) {

     hidetip();
     //showtip(event, '<img src="../images/spinner.gif" />');

    //sendMail('hans@lepepproductions.com', 'ks check functie : 1', ' - esroid : ' + esroid + ' - show : ' + show + ' - hall : ' + hall + ' - returnURL : ' + returnURL)

    $.ajax({
        url: "/WishList.asmx/DoAuthenticationCheck",
        data: "{}",
        success: function (msg) {
            if (msg != null) {
                var results;
                if (msg != null) {
                    results = msg.d;
                }
            }
            if (results == true) {
                getClientCode(event, esroid, show, hall, date);              
            }
            else {
                showtip(event, 'Om deze functie te kunnen gebruiken dient u <a href="/mijn-ks/login?ReturnUrl=' + returnURL + '">in te loggen</a> of zich <a href="/mijn-ks/Aanmelden">aan</a> te melden. <br><br><span style="margin-left:90px">[<a onclick="hidetip();" style="cursor:pointer;text-decoration:underline;color:#666666;" >sluiten</a>]</span>');
            }
        },
        error: function (xhr, desc, exceptionobj) {
            // alert('[error] : authenticationCheck | desc: ' + desc + '|  exception obj: ' + exceptionobj);
        }
    });

}


function getClientCode(event, esroid, show, hall, date) {

    //sendMail('hans@lepepproductions.com', 'ks check functie : 2', '- esroid : ' + esroid + ' - show : ' + show + ' - hall : ' + hall + ' - date : ' + date);

    $.ajax({
        url: "/WishList.asmx/DoGetClientCode",
        data: "{}",
        success: function (msg) {
            if (msg != null) {
                var clientcode;
                if (msg != null) {
                    clientcode = msg.d;
                } 
            }  
            if(clientcode != -1)
            {                   
                checkAlreadyAdded(event, esroid, show, hall, date, clientcode);
            }
        },
        error: function (xhr, desc, exceptionobj) {
            // alert('[error] : getClientCode | desc: ' + desc + '|  exception obj: ' + exceptionobj);
        }
    });

}


function checkAlreadyAdded(event, esroid, show, hall, date, clientcode) {

    //sendMail('hans@lepepproductions.com', 'ks check functie : 3', '- esroid : ' + esroid + ' - show : ' + show + ' - hall : ' + hall + ' - date : ' + date + ' - clientcode : ' + clientcode);
    
     $.ajax({
        url: "/WishList.asmx/DoCheckAlreadyAdded",
        data: "{'genericEventID': '" + esroid + "', 'clientCode': '" + clientcode + "'}",
        success: function (msg) {
            if (msg != null) {
                var results;
                if (msg != null) {
                    results = msg.d;
                }
            }                   
            if (results == true) {
                showtip(event, 'Deze voorstelling is reeds toegevoegd aan uw verlanglijst. <br><br><span style="margin-left:90px">[<a onclick="hidetip();" style="cursor:pointer;text-decoration:underline;color:#666666;" >sluiten</a>]</span>');       
            }
            else{
                showtip(event, 'Deze voorstelling <a onclick="wishlistAddRecord(\'' + clientcode + '\', \'' + show + '\', \'' + hall + '\', \'' + date + '\', \'' + date + '\', \'' + date + '\', \'' + esroid + '\'); hidetip(); wishlistUpdateCounter();" style="cursor:pointer;text-decoration:underline;color:#666666;" >toevoegen</a> aan uw verlanglijst. <br><br><span style="margin-left:90px">[<a onclick="hidetip();" style="cursor:pointer;text-decoration:underline;color:#666666;" >sluiten</a>]</span>');         
            }

        },
        error: function (xhr, desc, exceptionobj) {
            // alert('[error] : checkAlreadyAdded | desc: ' + desc + '|  exception obj: ' + exceptionobj);
        }
    });

}


function wishlistUpdateCounter() {

    var aantal = $('.wishlistammount').text();
    aantal = jQuery.trim(aantal);
    aantal = aantal.replace("(", "");
    aantal = aantal.replace(")", "");
    aantal = aantal.replace("-", "");
    aantal = trim(aantal);
    var newAantal = parseInt(aantal);
    newAantal++;

    $('.wishlistammount').text(" (" + newAantal + ") - ");
}


