
var aspectratio;
var pixwidth;
var pixheight;
var picname;
var dpival;

load();

function gup( 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 results[1];
}


function load()
{
        var box = document.pricebox;

        pixwidth = gup( "w" );
        pixheight = gup( "h" );
        var suggestw= gup( "sug" );
        dpival = gup("dpi");
        picname = gup("n");
        if( pixheight > 0 && pixwidth > 0 ) {
            aspectratio = pixwidth / pixheight;
            }
         else 
            aspectratio = 4;

        document.getElementById("aspectr").innerHTML = aspectratio.toPrecision(2) + " to 1";
        document.getElementById("picname").innerHTML = picname;

        if( suggestw < 1 )
            suggestw = 48;
        box.width.value = suggestw;
        setWidth();
}

function explaindpi(dp)
{
    if( dp > 300 )
        return "wastefully sharp";
    if (dp > 250 )
        return "very sharp";
    if( dp >= 180 )
        return "sharp";
    if( dp >= 150 )
        return "good";
    if( dp > 100 )
        return "less sharp up close, good at distance";
    if( dp > 50 )
        return "less sharp up close, good from a few feet away";
    if( dp > 30 )
        return "blurry except far away";
    return "billboard/banner";
}

function rollsize(box,toinches)
{
    var h = box.height.value * toinches;
    var w = box.width.value * toinches;
    if( h > 40.75 ) return "Too Tall to print";
    if( h > 23.6 ) return "Requires Outside Printer";
    if( h > 17.6 ) return "24 inch";
    if( h > 15.6 ) return "18 or 24 inch";
    if( h > 12.6 ) return "16 inch";
    if( h > 9.6 ) return "13 or 16 inch";
    if( h > 7.6 ) return "10, 13 or 16 inch";
    if( h < 6 && w < 72 ) return "Outside Printer or my smallest";
    return "my smallest";
    }

function setprice() {
    var box = document.pricebox;

    var uni=box.units;
    var toinches = uni.options[uni.selectedIndex].value;


    var sqf = box.width.value * box.height.value * toinches * toinches/144;
    var newcost = sqf * 11;
    if( newcost > 110 )
        newcost = 110 + (sqf-10)*7;
    var dpi = pixwidth / box.width.value / toinches;
    if( newcost < 25 ) 
        newcost = 25;
    box.cost.value = newcost.toFixed(2);

    if( dpival >= 0 ) 
        document.getElementById("notes").innerHTML = "At " + dpi.toFixed(0) + " PPI (" +
            explaindpi(dpi) + ") - Roll size: " + rollsize(box,toinches) + ".";
     else
        document.getElementById("notes").innerHTML = 
            "DPI: Unknown.  Roll size: " + rollsize(box,toinches) + ".";

    //document.getElementById("dpi").innerHTML = dpi.toFixed(0);
    //document.getElementById("dpix").innerHTML = explaindpi(dpi);
    //document.getElementById("rollsize").innerHTML = rollsize(box);
    }


function setWidth()
{
    var box = document.pricebox;
    box.height.value = (box.width.value / aspectratio).toPrecision(3);
    setprice();
}
function setHeight()
{
    var box = document.pricebox;
    var newh = box.height.value * aspectratio;
    box.width.value = newh.toPrecision(3);
    setprice();
}


