﻿function Shared() {
    // all static members, no instance implementation
}

String.prototype.trim =
function() {
    return this.replace(/^\s*/, '').replace(/\s*$/, '');
}

Shared.encodeString =
function(str) {
    // replace ampersands and spaces with escaped equivalents
    var encoded = '';

    for (idx = 0; str.length; i++) {
        switch (str.charAt(idx)) {
            case '&':
                encoded += '%26';
                break;
            case ' ':
                encoded += '+';
                break;
            default:
                encoded += str.charAt(idx);
                break;
        }
    }
}

// Size a dialog relative to the position of the OK button
Shared.resizeDialog = 
function() {
    // Set dialog height/width to be 40 pixels bigger than the document body
    window.dialogHeight = String(document.body.scrollHeight + 40) + "px";
    window.dialogWidth = String(document.body.scrollWidth + 40) + "px";

    // If dialog is taller/wider than the screen, reduce it a tad
    if (window.dialogHeight > screen.availHeight) {
        window.dialogHeight = screen.availHeight - 50;
    }
    if (window.dialogWidth > screen.availWidth) {
        window.dialogWidth = screen.availWidth - 50;
    }

    // Center the dialog
    window.dialogLeft = (screen.availWidth - parseInt(window.dialogWidth, 10)) / 2;
    window.dialogTop = (screen.availHeight - parseInt(window.dialogHeight, 10)) / 2;
}

// Close a dialog and return true to cause client postback 
// or false so client doesn't postback
Shared.closeDialog =
function(refresh) {
    var retVal = new Object();
    retVal.refresh = refresh;
    window.returnValue = retVal;
    window.close();
}

Shared.checkout =
function() {
    var tbl = document.getElementById('tblBasket');
    if (tbl != null) {
        if (tbl.rows.length > 3) {
            try {
                hWnd.focus();
            }
            catch (Exception) {
                hWnd = window.showModalDialog('Checkout.aspx?', window,
                    'dialogHeight:550px;dialogWidth:700px;center:yes;resizable:no;scroll:yes;status:no;unadorned:yes');
            }
            return true;
        }
        else {
            alert('You must have items in your basket before proceeding to the checkout.');
            return false;
        }
    }
    else {
        alert('Unable to find your shopping basket.\n\nPlease restart your browser.');
        return false;
    }

}
