﻿

function CallJSONWebService(targetElement, url, parameters, successCallBack, failureCallback) {
    var passed = false;
    var count = 0;

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: url,
        data: parameters,
        cache: false,

        success: function (response) {

            if (successCallBack)
                successCallBack(response, targetElement);
            else {
                if (targetElement)
                    $(targetElement).html(response);
            }


        },
        error: function (response) { successCallBack(response); }
    });
}

function DisplayWindow(pageUrl, dialogTitle, width, height, dialogCallBack, autoSizeDialog, pageParameters) {
    var fullUrl = pageUrl;
    if (pageParameters)
        fullUrl += '?' + pageParameters;

    var options = {
        // url: SP.Utilities.Utility.getLayoutsPageUrl('slm?Source=' + location.href),
        url: fullUrl,
        title: dialogTitle,
        allowMaximize: true,
        showClose: true,
        width: width,
        showMaximized: !width && !height && !autoSizeDialog,
        height: height,
        autoSize: autoSizeDialog,
        dialogReturnValueCallback: Function.createDelegate(null, dialogCallBack)
    };


    var dialog = SP.UI.ModalDialog.showModalDialog(options);
    return dialog;
}

function DisplayWindowHTML(html, dialogTitle, width, height, dialogCallBack, autoSizeDialog) {

    var options = {
        html: html,
        title: dialogTitle,
        allowMaximize: true,
        showClose: true,
        width: width,
        showMaximized: !width && !height && !autoSizeDialog,
        height: height,
        autoSize: autoSizeDialog,
        dialogReturnValueCallback: Function.createDelegate(null, dialogCallBack)
    };


    var dialog = SP.UI.ModalDialog.showModalDialog(options);
    return dialog;
}


function CloseDialogWindow(resultArgs) {
    SP.UI.ModalDialog.commonModalDialogClose(1, resultArgs);
    //RefreshInterface();
    //   SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
}

function ShowDialog() {
    var options = {
        url: 'http://www.google.com',
        autoSize: true,
        allowMaximize: true,
        title: 'Test dialog',
        showClose: true
    };
    var dialog = SP.UI.ModalDialog.showModalDialog(options);
}


function LoadRemotePage(targetElement, sourcePage, parameters, callback) {
    var passed = false;
    var count = 0;

    $.ajax({
        type: "POST",
        url: sourcePage,
        data: parameters,
        cache: false,
        success: function (html) {
            if (html.indexOf('Não é possível exibir esta Web Part') == -1) {
                if (callback)
                    callback(targetElement, html);
                else {
                    if (targetElement)
                        $(targetElement).html(html);
                    // chamando callback
                    //callback;
                }
            }
            else LoadRemotePage(targetElement, sourcePage, parameters);


        }
    });
    // }

}

function PagesCallBack(targetArea, callResult) {
    var contentArea;
    if ($(callResult).find('#slm_partialPageContent').length > 0)
        contentArea = callResult;
    else contentArea = $(callResult).find('#ContentArea').html();

    if (contentArea && contentArea != '') {
        $(targetArea).show();
        (targetArea).html(contentArea);

        $(targetArea).find('.embeddedArea').each(
                function () {
                    $(this).html($(this).html($(this).attr('code')).text());
                });
        if (contentArea.toLowerCase().match('slm_transitions'))
            if (CallContentTransition) CallContentTransition(targetArea);
    }
    else {
        $(targetArea).hide();
    }
}

// Error handler
function ErrorHandler(message) {
    alert(message);
}


function ResizeParentFrame(url,height,width)
{

url= url.replace(/%20/g,' ');
	$("iframe", window.document).each(function()
			{
				var src = $(this).attr('src');
				src = src.replace(/%20/g,' ');

				if (url.indexOf(src.split('?')[0]) > -1)
				{
				if (height && height !='')
					setHeight($(this),height);
				if (width&& width!='')					
					setWidth($(this),width);
		        }
	        });

}

function setHeight(element,height)
{
$(element).css("height",height);
}

function setWidth(element,width)
{
$(element).css("width",width);
}


