var lastRkey = "";

function DisableControls(sender, args)
{
    $('#loadingpanel').show();
}

function EnableControls(sender, args)
{
    $('#loadingpanel').hide();
}

$.ajaxSetup({
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    beforeSend: function(xhr) {
        xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
    }
});

function ToggleHelp(rKey) {
    if (lastRkey != rKey) {
        GetResource("Help", rKey, $('#HelpContent'));
        $('#HelpPanel').show('slow');
        lastRkey = rKey;
    } else {
        lastRkey = "";
        $('#HelpPanel').hide('slow');
    }
}
function ShowSmallPanel(panel) {
    $('#' + panel).show(); 
    return false;
}
function ShowPanel(panel) {
    $('#ActionPanels .ActionPane').hide();
    $('#' + panel).show(); 
    
    return false;
}
function HidePanel(panel) {
    $('#' + panel).hide();
    return false; 
}

// -- Added by Dim 20091228 (#581)
function HideActionPanes(){
    $('#ActionPanels .ActionPane').hide();

}//HideWebpagePanels

function TogglePanel(panel) {
    var isVisible = $('#' + panel).is(':visible'); 
    $('#ActionPanels .ActionPane').hide();
    if (!isVisible) {
        $('#' + panel).show(); 
    }
    
    return false;
}
function ShowFeedBack(text, type) {
    $('#feedbackpanel').show();
    $('#feedbackpanel .progressBackgroundFilter').animate({opacity: 0.5}, 1);
    
    GetResource("FeedBack", text, $('#FeedbackContent'));
    if (type != 1)
    {
        $('#FeedbackPane h3').html("Backoffice Foutmelding");
        $('#FeedbackPane').addClass("red").removeClass("green");
    }
    else
    {
        $('#FeedbackPane h3').html("Backoffice Bericht");
        $('#FeedbackPane').addClass("green").removeClass("red");
    }
        
    $('#feedbackpanel').click(function() {
        $('#feedbackpanel').hide();
        $('#feedbackpanel .progressBackgroundFilter').animate({opacity: 0.0}, 1);
    });
    return false;
}

function ShowFeedBack2(text, type) {
    $('#feedbackpanel').show();
    $('#feedbackpanel .progressBackgroundFilter').animate({opacity: 0.5}, 1);
    $('#FeedbackContent').html(text);
    if (type != 1)
    {
        $('#FeedbackPane h3').html("Backoffice Foutmelding");
        $('#FeedbackPane').addClass("red").removeClass("green");
    }
    else
    {
        $('#FeedbackPane h3').html("Backoffice Bericht");
        $('#FeedbackPane').addClass("green").removeClass("red");
    }
        
    $('#feedbackpanel').click(function() {
        $('#feedbackpanel').hide();
        $('#feedbackpanel .progressBackgroundFilter').animate({opacity: 0.0}, 1);
    });
    return false;
}

function GetResource(name, rkey, outputDiv) {
    AjaxCall("/webservices/BackOfficeService.asmx/GetResource",
        "{'Name':'" + name + "', 'rKey':'" + rkey + "'}",
        function(msg) {
            outputDiv.html(msg);
        }
    );
}

function ValidateForm() {
    $("#aspnetForm").validate();
    if($('#aspnetForm').valid()) {
        $(".errorSummary").hide('slow');
        return true;
    } else {
        $(".errorSummary").show('slow');
        return false;
    }
}

function StoreSettings(func) {
    $("#aspnetForm").validate();
    if($('#aspnetForm').valid()) {
        func();
        $(".errorSummary").hide('slow');
    } else {
        $(".errorSummary").show('slow');
        ShowFeedBack("FormNotValid", 2);
    }
}

function AjaxCall(url, data, success) {
    $.ajax({
        url: url,
        data: data,
        success: success,
        error: function(xhr, msg) {
            alert(msg);
        }
    });
}



function AddItemToWishList(sourceId, wishGroup, entityID, currenthref, refresh, datasource) {
    
    params = "{'sourceId':" + sourceId + " , 'wishGroup':'" + wishGroup + "', 'entityID':" + entityID + ", 'refresh':" + refresh + ", 'datasourceName':'" + datasource +"'}";

    AjaxCall("/webservices/EventListener.asmx/AddItemToWishList2", params, function(msg) {
        if (msg) {
            window.location.reload();
        }
     });
    $(currenthref).addClass("added");
    return false;
}

$.fn.clearForm = function() {
  return this.each(function() {
    if (this.tagName.toLowerCase() == "form" || this.tagName.toLowerCase() == "div") {
      return $(":input",this).clearForm();
    }
    switch (this.type) {
      case "password":
      case "textarea":
      case "text":
        this.value = "";
        break;
      case "select-multiple":
      case "select-one":
        this.selectedIndex = 0;
        break;
      case "radio":
      case "checkbox":
        this.checked = false;
        break;
    }
  });
};



