/// <reference path="../javascript/jquery-1.3-vsdoc.js" />

//  Disable flash portion of addthis buttons on the site
//  This config may be modified for further customizations of buttons
var addthis_config = {
    data_use_flash: false,
    data_use_cookies: false
}

$().ready(function() {
    if($('#marked').size() > 0) { //we'd like to keep track of marked records on this page
        $('.check input[type="checkbox"]').click(individualRecord_Click);

        //  Recheck all records that have been previously checked and are in the marked collection
        $('.check input[type="checkbox"]').each(function() {
            if (($('#marked').val().indexOf($(this).attr("value") + '|') != -1) &&
                ($('#marked').val().indexOf('-' + $(this).attr("value") + '|') == -1)) {
                this.checked = true;
            }
        });
    }
});

/*** Mark records functions ***/
//  Handles individual result checkbox clicks
function individualRecord_Click(oElem) {
    if(oElem.id == null)
        oElem = this;

    var resultID = $(oElem).attr("value");
    var myregexp = new RegExp('(?:-)?' + resultID + '\\|');
    var mymatch = myregexp.exec($("#marked").val());

    if(oElem.checked) { //we marked the record
        if(mymatch != null && mymatch.length > 0) {
            if(mymatch[0].substring(0, 1) == '-') { //  if a -id was found, remove it and add +
                $("#marked").val($("#marked").val().replace(myregexp, ''));
                $("#marked").val($("#marked").val() + resultID + '|');
                //$("#mCount").val(parseInt($("#mCount").val()) + 1);
            }
        }
        else { //add to marked
            $("#marked").val($("#marked").val() + resultID + '|');
            //$("#mCount").val(parseInt($("#mCount").val()) + 1);
        }

        $(oElem).parent().parent().addClass("marked");
    }
    else { //we unmarked the record
        if(mymatch != null && mymatch.length > 0) { //delete from marked
            if(mymatch[0].substring(0, 1) != '-') { //  if a +id was found, remove it and add -
                $("#marked").val($("#marked").val().replace(myregexp, '-' + resultID + '|'));
                //$("#mCount").val(parseInt($("#mCount").val()) - 1);
            }
        }
        else { //delete from marked
            $("#marked").val($("#marked").val() + '-' + resultID + '|');
            //$("#mCount").val(parseInt($("#mCount").val()) - 1);
        }

        $(oElem).parent().parent().removeClass("marked");
    }
}

//  Handles mark all link
function markAll_Click() {
    $('.check input[type="checkbox"]').each(function() { this.checked = true; });
    $('.check input[type="checkbox"]:first').click();
    $('.check input[type="checkbox"]').each(function() { this.checked = true; });
    
    return false;
}

//  Handles clear all link
function clearAll_Click() {
    $('.check input[type="checkbox"]').each(function() { this.checked = false; });
    $('.check input[type="checkbox"]:first').click();
    $('.check input[type="checkbox"]').each(function() { this.checked = false; });
    
    return false;
}

//  Re-mark any previously marked records after returning to a page
function ReinitChecks() {
    var rowsToCheck;

    if(pagePendingAction.indexOf('s=viewmarked') >= 0) { //mark all page items
        rowsToCheck = $("input[class='recordcheck']").get();

        for(var i = 0; i < rowsToCheck.length; ++i) {
            rowsToCheck[i].checked = true;
            $(rowsToCheck[i]).parent().parent().addClass("marked");
        }
    }
    else { //mark those listed in the client field
        rowsToCheck = $("#cm").val().split('|');

        for(var i = 0; i < rowsToCheck.length; ++i) {
            if(rowsToCheck[i].length > 0) {
                var oElem = $("#chk" + rowsToCheck[i]).get(0);
                if(oElem) {
                    oElem.checked = true;
                    $(oElem).parent().parent().addClass("marked");
                }
            }
        }
    }

    //are all records on this pages checked?  If so, put a check in the mark all box
    if($('input.recordcheck').size() != 0) {
        if($('input.recordcheck:checked').size() == $('#resultset tbody input[type="checkbox"]').size()) {
            $("input#pageCheck").get(0).checked = true;
            $('#markAllCell p.checkCellLabel').html('Clear: ' +
              '<a id="lnkAll" href="#">All</a> | ' +
              '<a href="/results.aspx?c=viewmarked">Show</a>');
        }
    }
}
/*** END Mark records functions ***/


function Lookup(searchType, controlName, IDField) {
    if (document.getElementById(controlName) && document.getElementById(controlName).value == '')
        return false;

    window.open('/utils/lookup/lookupmain.asp?t=' + escape(searchType) + '&c=' + escape(controlName) +
        '&cid=' + escape(IDField) + '&s=' + escape(document.getElementById(controlName).value) +
        '&st=s','','directories=no,fullscreen=no,location=no,menubar=no,resizable=no,status=no,toolbar=no,scrollbars=yes,width=600,height=400');
    return false;
}

function Check(type) {
    switch(type) {
        case 'all':
            markAll_Click();
            break;
        case 'allpage':
            markAll_Click();
            break;
        case 'clearpage':
            clearAll_Click();
            break;
        case 'none':
            clearAll_Click();
            $("#marked").val('');
            break;
        default:
            break;
    }

    return false;
}

function ViewChecked() {
    var qstring = $("#marked").val();
    qstring = qstring.replace(/-\d+\|/g, '');

    if(qstring != '')
        window.location = '/browse/printview.asp?ids=' + qstring;
    else
        alert('Select at least one record by marking the checkboxes to the left of each result.');

    return false;
}

function ViewSummary() {
    var qstring = $("#marked").val();
    qstring = qstring.replace(/-\d+\|/g, '');

    if (qstring != '')
        window.location = '/utils/printSummaryResults.aspx?f1=&ids=' + qstring;
    else
        alert('Select at least one record by marking the checkboxes to the left of each result.');

    return false;
}

function Email() {
    var qstring = $("#marked").val();
    qstring = qstring.replace(/-\d+\|/g, '');

    if(qstring != '')
        window.open('/utils/email.asp?recordlist=' + qstring, '', 'directories=no,fullscreen=no,location=no,menubar=no,resizable=no,status=no,toolbar=no,scrollbars=yes,width=450,height=350');
    else
        alert('Select at least one record by marking the checkboxes to the left of each result.');

    return false;
}