var script = document.createElement('script');
script.src = '/js/jquery-1.6.4.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);

var blockUiScript = document.createElement('script');
blockUiScript.src = '/js/jQueryBlock/jquery.blockUI.js';
blockUiScript.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(blockUiScript);

google = "true";
function load() {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        var geocoder = new GClientGeocoder();
        map.addControl(new GSmallMapControl());
        var address = "2900 Percy Machin Dr., North Little Rock, AR, 72114";
        geocoder.getLatLng(
            address,
            function (point) {
                if (!point) {
                    alert(address + " not found");
                } else {
                    map.setCenter(point, 15);
                    var icon = new GIcon();
                    icon.image = "images/agi_balloon.png";
                    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
                    icon.iconSize = new GSize(72, 45);
                    icon.shadowSize = new GSize(72, 45);
                    icon.iconAnchor = new GPoint(6, 20);
                    var marker = new GMarker(point, icon);
                    map.addOverlay(marker);
                    marker.openInfoWindowHtml(address);
                }
            }
               );
    }
}

function ResetDefault() {
    __defaultFired = false;
}
var win = null;
function NewWindow(mypage, myname, w, h, pos, infocus) {

    if (pos == "random") { myleft = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100; mytop = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100; }
    if (pos == "center") { myleft = (screen.width) ? (screen.width - w) / 2 : 100; mytop = (screen.height) ? (screen.height - h) / 2 : 100; }
    else if ((pos != 'center' && pos != "random") || pos == null) {
        myleft = 0; mytop = 20;
    }
    settings = "width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=1,location=1,directories=no,status=no,menubar=no,toolbar=no,resizable=1"; win = window.open(mypage, myname, settings);
    win.focus();
}

function NewHelpWindow(mypage, myname, w, h, pos, infocus) {
    if (pos == "random") { myleft = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100; mytop = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100; }
    if (pos == "center") { myleft = (screen.width) ? (screen.width - w) / 2 : 100; mytop = (screen.height) ? (screen.height - h) / 2 : 100; }
    else if ((pos != 'center' && pos != "random") || pos == null) {
        myleft = 0; mytop = 20;
    }
    settings = "width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=1,location=1,directories=no,status=no,menubar=no,toolbar=no,resizable=1"; win = window.open(mypage, myname, settings);
    win.focus();
}

function NewWindowLandingPage(mypage, myname, w, h, pos, infocus) {

    if (pos == "random") { myleft = (screen.width) ? Math.floor(Math.random() * (screen.width - w)) : 100; mytop = (screen.height) ? Math.floor(Math.random() * ((screen.height - h) - 75)) : 100; }
    if (pos == "center") { myleft = (screen.width) ? (screen.width - w) / 2 : 100; mytop = (screen.height) ? (screen.height - h) / 2 : 100; }
    else if ((pos != 'center' && pos != "random") || pos == null) {
        myleft = 0; mytop = 20;
    }
    settings = "width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=1,location=1,directories=no,status=no,menubar=no,toolbar=yes,resizable=1"; win = window.open(mypage, myname, settings);
    win.focus();
}

function ChangeCheckBoxState(id, checkState) {
    var cb = document.getElementById(id);
    if (cb != null)
        cb.checked = checkState;
}

function ChangeAllCheckBoxStates(checkState) {
    // Toggles through all of the checkboxes defined in the CheckBoxIDs array
    // and updates their value to the checkState input parameter
    if (CheckBoxIDs != null) {
        for (var i = 0; i < CheckBoxIDs.length; i++)
            ChangeCheckBoxState(CheckBoxIDs[i], checkState);
    }
}

function ChangeHeaderAsNeeded() {
    // Whenever a checkbox in the GridView is toggled, we need to
    // check the Header checkbox if ALL of the GridView checkboxes are
    // checked, and uncheck it otherwise
    if (CheckBoxIDs != null) {
        // check to see if all other checkboxes are checked
        for (var i = 1; i < CheckBoxIDs.length; i++) {
            var cb = document.getElementById(CheckBoxIDs[i]);
            if (!cb.checked) {
                // Whoops, there is an unchecked checkbox, make sure
                // that the header checkbox is unchecked
                ChangeCheckBoxState(CheckBoxIDs[0], false);
                return;
            }
        }

        // If we reach here, ALL GridView checkboxes are checked
        ChangeCheckBoxState(CheckBoxIDs[0], true);
    }
}

function ChangeHeaderAsNeededSetLabel() {
    SetSelected();
    //FindLabel
    var labelSelectedNotice = document.getElementById("lblSelectedNotice");
    var NumberChecked = 0;
    var NumberUnChecked = 0;
    // Whenever a checkbox in the GridView is toggled, we need to
    // check the Header checkbox if ALL of the GridView checkboxes are
    // checked, and uncheck it otherwise
    if (CheckBoxIDs != null) {
        // check to see if all other checkboxes are checked
        for (var i = 1; i < CheckBoxIDs.length; i++) {
            var cb = document.getElementById(CheckBoxIDs[i]);
            if (!cb.checked) {
                // Whoops, there is an unchecked checkbox, make sure
                // that the header checkbox is unchecked
                ChangeCheckBoxState(CheckBoxIDs[0], false);
                NumberUnChecked++;
            }

            if (cb.checked) {
                NumberChecked++;
            }
        }

        if (NumberUnChecked == (CheckBoxIDs.length - 1)) {

            labelSelectedNotice.innerHTML = 'Using All Results';
            return;
        }

        if (NumberChecked != (CheckBoxIDs.length - 1)) {
            labelSelectedNotice.innerHTML = "Using Selected Results";
            return;
        }

        // If we reach here, ALL GridView checkboxes are checked
        ChangeCheckBoxState(CheckBoxIDs[0], true);
        labelSelectedNotice.innerHTML = 'Using All Results';
    }
}

function SetCallbackSelected(state) {
    window.selected = "";
    if (state) {
        var checkBoxen = $('[id$="CheckedReportRow"]');
        $(checkBoxen).each(function () {
            var rpid = $(this).parent().attr("Rpid");
            var countyId = $(this).parent().attr("CountyId");
            window.selected += ":{" + rpid + "," + countyId + "}:";
        });
    }
    else {
        window.selected = "";
    }

    var searchId = getParameterByName("sid");
    var listId = getParameterByName("lid");
    $.ajax({
        type: "POST",
        url: "/Results.asmx/SetSelectedValue",
        data: '{listId: "' + listId + '", searchId: "' + searchId + '", values: "' + window.selected + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: function () {
           blockUi();
        },
        complete: function () {
            unBlockUi();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            // alert(xhr.status);
            // alert(thrownError);
        }
    });

}

function SetSelected() {
    window.selected = "";
    var checkBoxen = $('[id$="CheckedReportRow"]');
    $(checkBoxen).each(function () {
        var isChecked = $(this).is(':checked');
        if (isChecked) {
            var parent = $(this).closest('span');
            var rpid = $(parent).attr("Rpid");
            var countyId = $(parent).attr("CountyId");
            window.selected += ":{" + rpid + "," + countyId + "}:";
        }
    });

    var searchId = getParameterByName("sid");
    var listId = getParameterByName("lid");

    $.ajax({
        type: "POST",
        url: "/Results.asmx/SetSelectedValue",
        data: '{listId: "' + listId + '", searchId: "' + searchId + '", values: "' + window.selected + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: function () {
            blockUi();
        },
        complete: function () {
            unBlockUi();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            // alert(xhr.status);
            // alert(thrownError);
        }
    });
}

function blockUi() {
    $.blockUI({
        message: '<div style="margin-top: 5px;"><h1><img src="/Images/busy.gif" /> Page loading.....Please wait</h1></div>'
    });
}

function unBlockUi() {
    $.unblockUI();
}


function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}



function SetAllCheckBoxStatesSetLabel(checkState) {
    SetCallbackSelected(checkState);
    var labelSelectedNotice = document.getElementById("lblSelectedNotice");

    labelSelectedNotice.innerHTML = 'Using All Results';

    // Toggles through all of the checkboxes defined in the CheckBoxIDs array
    // and updates their value to the checkState input parameter
    if (CheckBoxIDs != null) {
        for (var i = 0; i < CheckBoxIDs.length; i++)
            ChangeCheckBoxState(CheckBoxIDs[i], checkState);
    }
}

