﻿var callbackError = false;
var extendedTimeoutId;
function openDialog(url, width, height) {
    var features = "center:yes;status:no;resizable:false;dialogWidth:" + width + "px;dialogHeight:" + height + "px;";
    return window.showModalDialog(url, '', features);
}
function openDialogWithRefresh(url, width, height) {
	var returnValue = openDialog(url, width, height);
	if (returnValue != undefined) {
		if (returnValue[0] == 'parentNavigate') {
			window.location.href = returnValue[1];
		}
		else {
			refresh();
		}
	}
	else {
		refresh();
	}
}
function openDialogWithPostBack(url, width, height, ctl) {
    openDialog(url, width, height);
    __doPostBack(document.all.item(ctl), '');
}
function showTextAreaPopupDialog(url, txtNameId, width, height) {
    var txtName = document.getElementById(txtNameId);
    var returnValue = openDialog(appendUrl(url), width, height);
    if (returnValue != undefined) {
        if (returnValue[0] == 'refresh') {
            refresh();
        }
        else if (returnValue[0] == 'parentNavigate') {
            window.location.href = returnValue[1];
        }
        else {
            txtName.value = txtName.value + unescape(returnValue[0]);
        }
    }
}
function showPopupDialogWithPostBack(url, txtNameId, txtValueId, width, height) {
    var txtName = document.getElementById(txtNameId);
    var returnValue = openDialog(appendUrl(url) + "Search=" + escape(txtName.value), width, height);
    if (returnValue != undefined) {
        txtName.value = unescape(returnValue[0]);
        document.getElementById(txtValueId).value = returnValue[1];
        __doPostBack();
    }
}
function showPopupDialog(url, txtNameId, txtValueId, width, height) {
    var txtName = document.getElementById(txtNameId);
    var txtValue = document.getElementById(txtValueId);
    var returnValue = openDialog(appendUrl(url) + "Search=" + escape(txtName.value) + "&SearchId=" + escape(txtValue.value), width, height);
    if (returnValue != undefined) {
        if (returnValue[0] == 'refresh') {
            refresh();
        }
        else if (returnValue[0] == 'parentNavigate') {
            window.location.href = returnValue[1];
        }
        else {
            txtName.value = unescape(returnValue[0]);
            document.getElementById(txtValueId).value = returnValue[1];
        }
    }
}
function returnTextAreaPopupDialogValue(text) {
    window.returnValue = new Array(text);
    window.close();
}
function returnPopupDialogValue(text, value) {
    window.returnValue = new Array(text, value);
    window.close();
}
function showPopupCalendar(url, txtDateId) {
    var txtDate = document.getElementById(txtDateId);
    var returnValue = openDialog(appendUrl(url) + "Date=" + txtDate.value, 349, 311);
    if (returnValue != undefined) {
        txtDate.value = returnValue;
    }
}
function returnPopupCalendarValue(calCalendarId) {
    var calendar = eval(calCalendarId);
    window.returnValue = calendar.formatDate(calendar.getSelectedDate(), "M/d/yyyy");
    window.close();
}
function appendUrl(url) {
    return url + (url.toString().lastIndexOf("?") > -1 ? "&" : "?").toString();
}
function toggleButton(elementId, isVisible) {
    document.getElementById(elementId).className = isVisible ? 'formButton' : 'formButtonHidden';
    return true;
}
function toggle(elementId, isVisible) {
    document.getElementById(elementId).style.display = isVisible ? '' : 'none';
    return true;
}
function toggleEnabled(elementId, isEnabled) {
    document.getElementById(elementId).disabled = !isEnabled;
    return true;
}
function extendedWait(messageClientId, timout) {
    extendedTimeoutId = setTimeout("showMessage('" + messageClientId + "', 'The selected operation is taking longer than expected to complete.  Please be patient and allow the operation to finish before taking any additional action.','msgInfo')", timout);
    return true;
}
function cancelExtendedWait(messageClientId) {
    clearTimeout(extendedTimeoutId);
    hideMessage(messageClientId);
    return true;
}
function showMessage(messageClientId, message, cssClass) {
    var span = document.getElementById(messageClientId);
    span.className = cssClass;
    span.innerHTML = unescape(message) + "&nbsp;&nbsp;[<a href='#' onclick=hideMessage('" + messageClientId + "')>Clear this Message</a>]";
}
function hideMessage(messageClientId) {
    try {
        var span = document.getElementById(messageClientId);
        span.className = '';
        span.innerHTML = '';
    } catch (Error) {
    }
}
function refresh() {
    window.location.href = window.location.href;
}
function grid_OnItemBeforeSelect(sender, eventArgs) {
    eventArgs.set_cancel(true);
}
function grid_OnCallbackComplete(sender, eventArgs) {
    if (CheckBoxIDs != null) {
        CheckBoxIDs = null;
        if (sender.get_callbackParameter() != null) {
            CheckBoxIDs = sender.get_callbackParameter().split(',');
        }
    }
}
function grid_refresh(sender, eventArgs) {
    cancelExtendedWait();
    if (!callbackError) eval(sender.get_parameter()).callback();
    callbackError = false;
}
function callback_error(sender, eventArgs) {
    cancelExtendedWait();
    callbackError = true;
    alert(eventArgs.get_message());
}
function changeHeaderAsNeeded() {
    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 != null && !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 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 getSelectedCheckboxIds() {
    // Toggles through all of the checkboxes defined in the CheckBoxIDs array
    // and updates their value to the checkState input parameter
    var items = new Array();
    var counter = 0;
    if (CheckBoxIDs != null) {
        for (var i = 1; i < CheckBoxIDs.length; i++) {
            var cb = document.getElementById(CheckBoxIDs[i]);
            if (cb.checked) {
                items[counter] = cb.value;
                counter += 1;
            }
        }
    }
    return items.join(",");
}

function CommaFormatted(amount) {
    var delimiter = ","; // replace comma if desired
    var a = amount.split('.', 2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { return ''; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    return amount;
}
