// JScript File
/// <reference path="jquery-1.4.4-vsdoc.js" />

function retrieveUrlAJAX(url, stringIdElm, Params, showLoading, completeFunc) {

    $(document).ready(function () {
        $.ajax({
            url: url,
            type: "GET",
            data: Params,
            cache: false,                //Do not cache the page
            async: false,
            contentType: "text/html; charset=utf-8",
            dataType: "html",
            beforeSend: function () {

                if (showLoading != false) {
                    ajax_loading(stringIdElm);
                }
            },
            success: function (response) {

                var headHtml = getScripts(response)
                response = removeFormHtml(response);
                response = removeViewstateHtml(response);
                response = headHtml + response;

                var test = $("#" + stringIdElm).html();

                $("#" + stringIdElm).html(response);
            },
            complete: function (response) {

                if (completeFunc != undefined) {
                    eval(completeFunc);
                }
            },

            error: function (xhr, ajaxOptions, thrownError) {
                //alert("error: " + xhr.status);
                //$.ajax({
                //url: CB_PathAjaxPages + "Error.aspx",
                //success: function(response) {
                //    $("#" + stringIdElm).html(response);
                //}
                //});

                //alert(thrownError); 
            }

        });
    });
}

function retrieveParentUrlAJAX(url, stringIdElm, Params, showLoading, completeFunc) {

    $.ajax({
        url: url,
        type: "GET",
        data: Params,
        cache: false,                //Do not cache the page
        async: false,
        contentType: "text/html; charset=utf-8",
        dataType: "html",

        beforeSend: function() {
            if (showLoading != false) {
                ajax_parentLoading(stringIdElm);
            }
        },

        success: function(response) {
            var headHtml = getScripts(response)
            //response = removeFormHtml(response);
            //response = removeViewstateHtml(response);
            $(window.parent.document).find("#" + stringIdElm).html(response);
        },

        complete: function() {
            if (completeFunc != undefined) {
                eval(completeFunc);
            }
            self.parent.tb_remove();
        },

        error: function(xhr, ajaxOptions, thrownError) {
            //alert(xhr.status); 
            alert(thrownError);
        }
    });
}

function getScripts(sResponseText) {
    var sResponseHtml = sResponseText;

    var nIndexFormOpen = sResponseHtml.indexOf('<head');
    //if (nIndexFormOpen != -1) { //if head exist
        sResponseHtml = sResponseHtml.substring(nIndexFormOpen, sResponseHtml.length);

        nIndexFormOpen = sResponseHtml.indexOf('>');

        var nIndexFormClose = sResponseHtml.lastIndexOf("</head>");

        sResponseHtml = sResponseHtml.substring(nIndexFormOpen + 1, nIndexFormClose);
    /*}
    else {
        sResponseHtml = "";
    }*/
    return sResponseHtml;
}

function removeFormHtml(sResponseText) {
    var sResponseHtml = sResponseText;

    var nIndexFormOpen = sResponseHtml.indexOf('<form');

    sResponseHtml = sResponseHtml.substring(nIndexFormOpen, sResponseHtml.length);

    nIndexFormOpen = sResponseHtml.indexOf('>');

    var nIndexFormClose = sResponseHtml.lastIndexOf("</form>");

    sResponseHtml = sResponseHtml.substring(nIndexFormOpen + 1, nIndexFormClose);

    return sResponseHtml;
}

function removeViewstateHtml(sResponseText) {
    var sResponseHtml = sResponseText;

    var nIndexViewstateOpen = sResponseHtml.toUpperCase().indexOf("<DIV>");

    var nIndexViewstateClose = sResponseHtml.toUpperCase().indexOf("</DIV>");

    sResponseHtml = sResponseHtml.substring(nIndexViewstateClose + 6, sResponseHtml.length);

    return sResponseHtml;
}


function trim(s) {
    return s.replace(/^\s+|\s+$/g, "");
}

function ajax_loading(divLoading) {

    if (divLoading == "" || divLoading == undefined) {
        divLoading = "divLoading";
    }

    var nHeight = $("div#" + divLoading).height();
    var nWidth = $("div#" + divLoading).width();

    if (nHeight >= 30) {
        var imgLoading = " <div style='height: " + nHeight + "px; width: " + nWidth + "px;'><img class='master_loading' src='" + strPathJQuery + "Popup/Images/Loading.gif'  /></div>";
        $("div#" + divLoading).html(imgLoading);
    }
}

function ajax_parentLoading(divLoading) {

    if (divLoading == "" || divLoading == undefined) {
        divLoading = "divLoading";
    }

    var nHeight = $(window.parent.document).find("div#" + divLoading).height();
    var nWidth = $(window.parent.document).find("div#" + divLoading).width();

    if (nHeight >= 30) {
        var imgLoading = " <div style='height: " + nHeight + "px; width: " + nWidth + "px;'><img class='master_loading' src='" + strPathJQuery + "Popup/Images/Loading.gif' /></div>";
        $(window.parent.document).find("div#" + divLoading).html(imgLoading);
    }
}



