function writeadjustwidth(html, fracscreen){
    var w;
    
    if (screen.width >= 1024 && fracscreen == 0.32) {
        //alert(fracscreen+" "+screen.width);
        html = html.replace('width', '');
    }
    else {
        w = screen.width * fracscreen;
        html = html.replace('width', ' width=' + w);
    }
    document.write(html);
}

function cssubmit(form){
    form.submit();
}

function reveal(elem, setting){
    elem.style.display = "";
    elem.style.visibility = "visible";
}

function hide(elem, setting){
    if (setting == 1) {
        elem.style.display = "none";
    }
    else {
        elem.style.visibility = "hidden";
    }
    
}

function vistoggle(elem, setting){
    if ((setting === 0 && elem.style.visibility == "visible") || (setting == 1 && elem.style.display === "")) {
        hide(elem, setting);
    }
    else {
        reveal(elem, setting);
    }
}

function fabdetail(fabno, dpi, tape){
    var s, ht;
    s = '';
    s = s + '<TABLE style="layout:fixed;width:100%;border:none;padding:0;margin:0;cellspacing:0;";><COL style="width:50%"><COL style="width:50%">';
    s = s + '<TR><TD colspan=2 style="vertical-align:top" align=left><img class="display" src="http://www.romansdirect.co.uk/fabrics/fab' + fabno + 'b.jpg">';
    if (tape == 1) {
        ht = dpi * 4.33;
        wt = 70;
        s = s + '<img class="display" height=' + ht.toString() + ' width=' + wt.toString() + ' src="http://www.curtainshack.co.uk/images/ruler.gif">';
    }
    s = s + '<TR><TD style="font-size:8pt;font-weight:bold;text-align:left;">' + fabname(fabno);
    s = s + '<TD align=left><BUTTON style="width:50pt" OnClick="javascript:window.close()";>Close</BUTTON>';
    s = s + '</TABLE>';
    return s;
}

function popfab(fabno, w, h, dpi, tape){
    var z, wb, hb;
    if (isNaN(w)) {
        w = 840;
    }
    if (isNaN(h)) {
        h = 640;
    }
    //wb=w+75;
    wb = w + 95;
    hb = h + 35;
    //hb=h+135;
    rh = dpi * 4.33 + 60;
    if (dpi > 0 && hb < rh) {
        hb = rh;
    }
    var feat = 'toolbar=no,resizable=yes,menubar=no,width=' + wb.toString() + ',height=' + hb.toString() + ',status=no,left=0,screenX=0,top=0,screenY=0,location=no,scrollbars=yes,directories=no';
    var pfab = null;
    pfab = window.open('', 'popfab', feat);
    pfab.document.write('<HTML>');
    pfab.document.write('<HEAD>');
    pfab.document.write('<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">');
    pfab.document.write('<LINK REL="STYLESHEET" TYPE="text/css" HREF="softfurn.css">');
    pfab.document.write('<TITLE>Fabric ' + fabname(fabno) + '</TITLE>');
    pfab.document.write('<BODY style="margin:0em;padding:0em;border: none;" onload=window.focus()>');
    pfab.document.write(fabdetail(fabno, dpi, tape));
    pfab.document.write('</BODY></HTML>');
    pfab.document.close();
}

function padnum(intnumber, width){
    var s = intnumber.toString();
    n = s.length;
    for (i = 0; i < width - n; i++) {
        s = '0' + s;
    }
    return s;
}

function nr(num, decplaces){
    if (decplaces === 0) {
        return num;
    }
    else {
        return Math.round(num * Math.pow(10, decplaces)) / Math.pow(10, decplaces);
    }
}

function sround(num, nearest){
    if (nearest === 0) {
        return num;
    }
    else 
        if (nearest == 0.1) {
            return nr(num, 1);
        }
        else 
            if (nearest == 0.001) {
                return nr(num, 3);
            }
            else 
                if (nearest == 0.0001) {
                    return nr(num, 4);
                }
                else 
                    if (nearest == 0.01) {
                        return nr(num, 2);
                    }
                    else {
                        return Math.round(num / nearest) * nearest;
                    }
}

function sceiling(num, nearest){
    return Math.ceil(num / nearest) * nearest;
    //return Math.floor((num+nearest-0.000000001)/nearest)*nearest;
}

function num(str){
    if (str === '' || str === null) {
        return 0;
    }
    else {
        return parseFloat(str);
    }
}

function numstr(num, showzero){
    if (num === null || (num === 0 && showzero === false)) {
        return "";
    }
    num = Math.round(num * 100) / 100;
    var s = num.toString();
    if (s.charAt(s.length - 2) == ".") {
        s = s + "0";
    }
    else 
        if (s.indexOf('.') == -1 && s.length > 0) {
            s = s + ".00";
        }
    return s;
}

function intstr(intnum, showzero){
    if (intnum === null || (intnum === 0 && showzero === false)) {
        return "";
    }
    return intnum.toString();
}

function report_error(msg, url, line){
}

function tomet(form, setting){
    var yards = 0;
    var centimeters = 0;
    feet = parseFloat(form.feet.value);
    inches = parseFloat(form.inches.value);
    if (setting == 1) {
        yards = parseFloat(form.yards.value);
        if (isNaN(yards)) {
            form.yards.value = 0;
            yards = 0;
        }
    }
    if (isNaN(feet)) {
        form.feet.value = 0;
        feet = 0;
    }
    if (isNaN(inches)) {
        form.inches.value = 0;
        inches = 0;
    }
    //prove of inputs
    form.feet.value = intstr(feet, false);
    form.inches.value = numstr(inches, false);
    if (setting == 1) {
        form.yards.value = numstr(yards, false);
    }
    //do calcs
    centimeters = (yards * 3 * 12 + feet * 12 + inches) * 2.54;
    //output
    if (setting == 1) {
        form.metres.value = numstr(centimeters / 100, false);
    }
    else {
        form.centimeters.value = numstr(centimeters, false);
    }
}

function metclear(form, setting){
    if (setting == 1) {
        form.metres.value = "";
        form.yards.value = "";
    }
    else {
        form.centimeters.value = "";
    }
    form.feet.value = "";
    form.inches.value = "";
}

function toimp(form, setting){
    var yards = 0;
    if (setting == 1) {
        centimeters = parseFloat(form.metres.value) * 100;
        if (isNaN(centimeters)) {
            form.metres.value = 0;
            centimeters = 0;
        }
    }
    else {
        centimeters = parseFloat(form.centimeters.value);
        if (isNaN(centimeters)) {
            form.centimeters.value = 0;
            centimeters = 0;
        }
    }
    //prove inputs
    if (setting == 1) {
        form.metres.value = numstr(centimeters / 100, false);
    }
    else {
        form.centimeters.value = numstr(centimeters, false);
    }
    //do calcs
    feet = Math.floor(centimeters / 2.54 / 12);
    if (setting == 1) {
        yards = Math.floor(feet / 3);
        feet = feet - yards * 3;
        form.yards.value = intstr(yards, false);
    }
    inches = (centimeters / 2.54) % 12;
    //output
    if (setting == 1) {
        form.feet.value = intstr(feet, true);
    }
    else {
        form.feet.value = intstr(feet, true);
    }
    form.inches.value = numstr(inches, true);
}
