﻿
//控制图片等比缩放
function DrawImage(ImgD, FitWidth, FitHeight) {
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= FitWidth / FitHeight) {
            if (image.width > FitWidth) {
                ImgD.width = FitWidth;
                ImgD.height = (image.height * FitWidth) / image.width;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        } else {
            if (image.height > FitHeight) {
                ImgD.height = FitHeight;
                ImgD.width = (image.width * FitHeight) / image.height;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
    }
}


//检验尺寸
function CheckJobSize(hiddenzize, tbText) {
    var hdsize = document.getElementById(hiddenzize);
    var tbcont = document.getElementById(tbText)

    var hidzide = hdsize.value;
    var tempsize = tbcont.value;
    if (hidzide != "-1" && hidzide != "0" && hdsize!="-2") 
    {
        if (tempsize == '' || tempsize == 0) {
            alert("成品实际尺寸不能为空或者小于0");
            tbcont.value = hidzide;
        }
        else {
            var ddl = document.getElementById("ddlPageType")
            var index = ddl.selectedIndex;
            var Value = ddl.options[index].value;
            if (Value != "0") {

                if (!isNaN(tempsize)) {
                    var maxSize = toFixed(parseFloat(hidzide) + parseFloat(hidzide) * 0.06, 0);
                    var smallSize = toFixed(parseFloat(hidzide) - parseFloat(hidzide) * 0.3, 0);
                    if (toFixed(parseFloat(tempsize), 2) > maxSize) {

                        alert("成品实际尺寸不能大于选择尺寸的标准尺寸:" + maxSize + ",选择范围是:" + smallSize + "-" + maxSize + "！");
                        tbcont.value = toFixed(parseFloat(hidzide), 2);
                    }
                    if (toFixed(parseFloat(tempsize), 2) < smallSize) {

                        alert("成品实际尺寸不能小于选择尺寸的标准尺寸:" + smallSize + ",选择范围是:" + smallSize + "-" + maxSize + "！");
                        tbcont.value = toFixed(parseFloat(hidzide), 2);
                    }
                }
                else {

                    tbcont.value = hidzide;
                }
            }
        }
    } 
    //书籍自定义时，产品尺寸不能大于100000
    else if (hidzide=="0")
    {
        if (!isNaN(tempsize)) {
            if (toFixed(parseFloat(tempsize), 2) > 100000) {

                alert("成品尺寸不能大于100000！");
                tbcont.value = toFixed(parseFloat(1), 2);
            }
        }
    }
    else
     {
        if (tempsize == '' || tempsize == 0) {
            alert("成品实际尺寸不能为空或者小于0");
            tbcont.value = '1';
        }
    }
}
function toFixed(num, d) {
    with (Math) num = round(num * pow(10, d)) / pow(10, d);
    //num: 帶小數的數值
    //d: 小數點後的位數
    return num;
}
