﻿function setShipAdd() {

    document.creditApp.ShippingAddress.value = document.creditApp.BillingAddress.value
    document.creditApp.ShippingCity.value = document.creditApp.BillingCity.value
    document.creditApp.ShippingState.value = document.creditApp.BillingState.value
    document.creditApp.ShippingZip.value = document.creditApp.BillingZip.value
    document.creditApp.ShippingCountry.value = document.creditApp.BillingCountry.value
}

function setCurDate() {
    var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    return month + "/" + day + "/" + year
}


function toggleBankruptcy(inVal) {
    vVal = inVal.value
    var ele1 = document.getElementById("divBr1");
    var ele2 = document.getElementById("divBr2");
    if (vVal == 'Yes') {
        ele1.style.visibility = 'visible';
        ele2.style.visibility = 'visible';
        ele1.style.display = 'block';
        ele2.style.display = 'block';
    } else {
        ele1.style.visibility = 'hidden';
        ele2.style.visibility = 'hidden';
        ele1.style.display = 'none';
        ele2.style.display = 'none';

    }
}

function doCalcsDirect() {
    vWsExtTot = 0
    vGhExtTot = 0
    for (i = 0; i < 6; i++) {
        vWsExt = parseFloat(document.getElementById(aFields[i] + "1").value) * parseFloat(gp_cleanNums(document.getElementById(aFields[i] + "2").value))
        if (gpf_isNumeric(vWsExt)) {
            document.getElementById(aFields[i] + "3").value = '$ ' + vWsExt.toFixed(2)
            vWsExtTot = vWsExtTot + vWsExt
        } else {
            document.getElementById(aFields[i] + "3").value = "-"
        }

        vGhExt = parseFloat(document.getElementById(aFields[i] + "1").value) * parseFloat(gp_cleanNums(document.getElementById(aFields[i] + "4").value))
        if (gpf_isNumeric(vGhExt)) {
            document.getElementById(aFields[i] + "5").value = '$ ' + vGhExt.toFixed(2)
            vGhExtTot = vGhExtTot + vGhExt
        } else {
            document.getElementById(aFields[i] + "5").value = "-"
        }

        if (gpf_isNumeric(vWsExtTot)) {
            document.getElementById("tot1").value = '$ ' + vWsExtTot.toFixed(2)
        } else {
            document.getElementById("tot1").value = "-"
        }

        if (gpf_isNumeric(vGhExtTot)) {
            document.getElementById("tot2").value = '$ ' + vGhExtTot.toFixed(2)
        } else {
            document.getElementById("tot2").value = "-"
        }

        if (gpf_isNumeric(vWsExtTot) && gpf_isNumeric(vGhExtTot)) {

            vDiff = vWsExtTot - vGhExtTot
            vPc = (vDiff / vWsExtTot) * 100


            document.getElementById("vMsg").innerHTML = "Buy Ghent direct and pocket <span style='color:#000000;font-weight:bold;'>$" + vDiff.toFixed(2) + "</span> or <span style='color:#000000;font-weight:bold;'>" + vPc.toFixed(0) + "%</span> more gross profit."
        }
    }
}


function gpf_isNumeric(str) {
    return (parseFloat(str, 10) == (str * 1));
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function gp_cleanNums(inNum) {
    vVal = inNum
    vVal = vVal.replace(",", "")
    vVal = vVal.replace("$", "")
    return vVal

}

function checkNumeric(objName, minval, maxval, comma, period, hyphen) {
    var numberfield = objName;
    if (chkNumeric(objName, minval, maxval, comma, period, hyphen) == false) {
        numberfield.select();
        numberfield.focus();
        return false;
    }
    else {
        return true;
    }
}

function chkNumeric(objName, minval, maxval, comma, period, hyphen) {
    // only allow 0-9 be entered, plus any values passed
    // (can be in any order, and don't have to be comma, period, or hyphen)
    // if all numbers allow commas, periods, hyphens or whatever,
    // just hard code it here and take out the passed parameters
    var checkOK = "0123456789" + comma + period + hyphen;
    var checkStr = objName;
    var allValid = true;
    var decPoints = 0;
    var allNum = "";

    for (i = 0; i < checkStr.value.length; i++) {
        ch = checkStr.value.charAt(i);
        for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break;
        if (j == checkOK.length) {
            allValid = false;
            break;
        }
        if (ch != ",")
            allNum += ch;
    }
    //alert(allNum)
    if (allNum.charAt(0) == '.') { allNum = '0' + allNum }
    if (!allValid) {
        alertsay = "Please enter only these values \""
        alertsay = alertsay + checkOK + "\" in this field."
        alert(alertsay);
        return (false);
    }

    // set the minimum and maximum
    var chkVal = allNum;
    var prsVal = parseInt(allNum);
    if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval)) {
        alertsay = "Please enter a value greater than or "
        alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
        alertsay = alertsay + "equal to \"" + maxval + "\" in this field."
        alert(alertsay);
        return (false);
    }
}
