﻿
function OffsetPoster() {
    this.Quantity = null;
    this.Size = null;
    this.Proof = null;
    this.Stock = null;
    this.NumColors = null;
    this.FoldingType = null;
    this.Turnaround = null;
    this.ErrMessage = null;
    this.PriceGrid = [['11x17', 277, 560, 660, 750, 999, 1400, 79, 90, 25],
                        ['12x18', 277, 560, 660, 750, 999, 1400, 79, 90, 25],
                        ['18x24', 380, 745, 820, 960, 1290, 1999, 131, 130, 40],
                        ['24x36', 551, 999, 1165, 1495, 2399, 3399, 180, 180, 55],
                        ['25x38', 551, 999, 1165, 1495, 2399, 3399, 180, 180, 55],
                        ['27x39', 551, 999, 1165, 1495, 2399, 3399, 180, 180, 55],
                     ];

    this.SizeType = function(h, w) {
        this.height = h;
        this.width = w;
        this.smalldim = Math.min(h, w);
        this.largedim = Math.max(h, w);
        this.string = this.smalldim + 'x' + this.largedim;
    }

    this.SizeRowIndex = function(Size) {
        var i = 0;
        while ((this.PriceGrid[i][0] != Size.string) && (i < this.PriceGrid.length))
            i++;
        if (i < this.PriceGrid.length)
            return i;
        else {
            alert('price grid row not found.');
            return -1;
        }
    }

    this.PrintFees = function (Size, Quantity, NumColors) {
        var pmrFee; // plate and make ready fee
        var spotFee; // per Thousand
        var i = this.SizeRowIndex(Size);
        if (i < 0)
            return;
        var p;
        var pg = this.PriceGrid;
        switch (Quantity) {
            case 250: p = pg[i][1]; break;
            case 500: p = pg[i][2]; break;
            case 1000: p = pg[i][3]; break;
            case 2000: p = pg[i][4]; break;
            case 5000: p = pg[i][5]; break;
            case 10000: p = pg[i][6]; break;
            default:
                {
                    if (Quantity > 10000) {
                        p = (pg[i][5] + ((Quantity - 10000) * pg[i][6]));
                    }
                }
        }

        if (NumColors == 5)
            p += pg[i][8] + (pg[i][9] * (Quantity / 1000));
        return p;
    }

    this.FinishingFees = function (Size, Quantity, FoldType) {
        if (FoldType == 'none')
            return 0;
        if (((Size.smalldim == 11) || (Size.smalldim == 12)) &&
            ((Size.largedim == 17) || (Size.largedim == 18))) {
            if (FoldType == 'half') { // half fold
                switch (Quantity) {
                    case 250: return 45; break;
                    case 500: return 90; break;
                    case 1000: return 120; break;
                    case 2000: return 180; break;
                    case 5000: return 300; break;
                    case 10000: return 500; break;
                    default: return -1;
                }
            }
            else { // quarter fold
                switch (Quantity) {
                    case 250: return 65; break;
                    case 500: return 130; break;
                    case 1000: return 150; break;
                    case 2000: return 190; break;
                    case 5000: return 320; break;
                    case 10000: return 520; break;
                    default: return -1;
                }
            }
        }
        else
            alert('Folding is only available for 11x17 to 12x18. Fold type=' + FoldType);
    }

    this.TurnaroundFees = function(Days) {
        switch (Days) {
            case 6: return 0; break;
            case 4: return 300; break;
            case 3: return 500; break;
            case 2: return 700; break;
            case 1: return 1000; break;
        }
    }

    this.StockFees = function(Size, Quantity, Stock) {
        if ((Size.smalldim != 25) || (Size.largedim != 38))
            return 0;
        var thous = Quantity / 1000;
        switch (Stock) {
            case '10 Pt. Gloss Cover': return (thous * 350); break;
            default: return 0;
        }
    }

    this.Price = function(Size, Quantity, NumColors, FoldType, Turnaround) {
        var p;
        p = this.PrintFees(Size, Quantity, NumColors);
        p += this.FinishingFees(Size, Quantity, FoldType);
        p += this.TurnaroundFees(Turnaround);
        //        alert('PrintFees=' + this.PrintFees(Size, Quantity, NumColors) + '\r\nFinishingFees=' + this.FinishingFees(Size, Quantity, FoldType)
        //            + '\r\nTurnaroundFees=' + this.TurnaroundFees(Turnaround));
        p = Math.ceil(p);
        return p;
    }
}
