﻿var FormSubmit = true;
function jsCheckSpaces(eValue) {
    spaces_count = 0;
    for (count = 0; count < eValue.length; count++) {
        if ((eValue.charCodeAt(count) != 32)) {
            spaces_count = spaces_count + 1;
        }
    }
    return (spaces_count);
}
function isMoney(s) {

    if (s.search(/^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{3})?$/) == -1) {
        return false;
    }
}
function jsQuantity() {

    var eField = document.getElementById("Quantity").value;
    var eFieldValue = jsCheckSpaces(eField);
    if (eField == null || eField == "" || eField == 0 || isNaN(eField)) {
        FormSubmit = false;
        document.getElementById("spnQuantity").style.display = "inline";
    }
    else {
        document.getElementById("spnQuantity").style.display = "none";
    }
}

function jsRound4DecNo(fNo) {
    var result = Math.round(fNo * 10000) / 10000;
    return result;  
}

function jsRound3DecNo(fNo) {
    var result = Math.round(fNo * 1000) / 1000;
    return result;
}

function jsConvertKD(fNo) {
    var sNo = fNo.toString();
    var sNoArr = sNo.split(".");
    if (sNoArr.length > 1) {
        var sNoEnd = sNoArr[sNoArr.length - 1];
        if (sNoEnd.length == 1) {
            sNo += "00";
        }
        else if (sNoEnd.length == 2) {
            sNo += "0";
        }
    }
    else {
        sNo += ".000";
    }
    return sNo;
}

function jsPrice() {

    var eField = document.getElementById("Price").value;
    var eFieldValue = jsCheckSpaces(eField);
    if (eField == null || eField == "" || eFieldValue == 0 || isMoney(eField)==false) {
        document.getElementById("spnPrice").style.display = "inline";
        FormSubmit = false;
    }
    else {
        document.getElementById("spnPrice").style.display = "none";
    }
}

function jsGetInterest() {
    var iInt = document.getElementById("Period").value;
    document.getElementById("tdInt").innerHTML = iInt + " %";
    return iInt;
}

function jsGetForwardPrice(fPrice,fPercent) {
    var fFPrice = (fPrice * 0.6 * fPercent) + fPrice;
    fFPrice = jsRound4DecNo(fFPrice);
    document.getElementById("tdForwardPrice").innerHTML = "KWD " + jsConvertKD(fFPrice);
}

function jsGetTotalPrice(fPrice, fQuantity) {
    var fTPrice = (fPrice * fQuantity);
    fTPrice = jsRound3DecNo(fTPrice);
    document.getElementById("tdTotalPrice").innerHTML = "KWD " + jsConvertKD(fTPrice);  
    return fTPrice;
}

function jsGetFirstPayment(fTPrice) {
    var f1stPayment = fTPrice * 0.4;
    document.getElementById("td1stPayment").innerHTML = "KWD " + jsConvertKD(f1stPayment);
    f1stPayment = jsRound3DecNo(f1stPayment);
    return f1stPayment;
}

function jsGetInterest1(fPrice, fQuantity, fPercent) {
    var fInterest = fPrice * fQuantity * 0.6 * fPercent;
    fInterest = jsRound4DecNo(fInterest);
    document.getElementById("tdInterest").innerHTML = "KWD " + jsConvertKD(fInterest);
    return fInterest;
}

function jsGetMaturityBalance(fTotal) {
    var fMBalance = fTotal * 0.6;
    fMBalance = jsRound3DecNo(fMBalance);
    document.getElementById("tdMBalance").innerHTML = "KWD " + jsConvertKD(fMBalance);
    return fMBalance;
}

function jsGetTotalFCost(f1stPayment, fInterest, fMBalance) {
    var fTotalFCost = f1stPayment + fInterest + fMBalance;
    fTotalFCost = jsRound3DecNo(fTotalFCost);
    document.getElementById("tdTFCost").innerHTML = "KWD " + jsConvertKD(fTotalFCost);
    return fTotalFCost;
}

function jsGetFullPayment(f1stPayment, fInterest) {
    var fFullPayment = f1stPayment + fInterest;
    fFullPayment = jsRound3DecNo(fFullPayment);
    document.getElementById("tdFullPayment").innerHTML = "KWD " + jsConvertKD(fFullPayment);
    return fFullPayment;
}

function jsGetCutPrice(fPrice) {
    var fCutPrice = fPrice * 0.6;
    fCutPrice = jsRound3DecNo(fCutPrice);
    document.getElementById("tdCutPrice").innerHTML = "KWD " + jsConvertKD(fCutPrice);
}

function jsGetCharges(fTotalFCost) {
    var fCharges = fTotalFCost * 0.00125;
    fCharges = jsRound3DecNo(fCharges);
    document.getElementById("tdCharges").innerHTML = "KWD " + jsConvertKD(fCharges);
}


function js_Calculate() {
    FormSubmit = true;
    jsQuantity();
    jsPrice();
    if (FormSubmit == true) {
        var fPrice = parseFloat(document.getElementById("Price").value);
        var iQuantity = parseFloat(document.getElementById("Quantity").value);
        var fInt = jsGetInterest();
        var fPercent = (parseFloat(fInt))/100;
        jsGetForwardPrice(fPrice, fPercent);
        var fTotalPrice = jsGetTotalPrice(fPrice, iQuantity);
        var f1stPayment = jsGetFirstPayment(fTotalPrice);
        var fInterest = jsGetInterest1(fPrice, iQuantity, fPercent);
        var fMBalance = jsGetMaturityBalance(fTotalPrice);
        var fTotalFCost = jsGetTotalFCost(f1stPayment, fInterest, fMBalance);
        var fFullPayment = jsGetFullPayment(f1stPayment, fInterest);
        jsGetCutPrice(fPrice);
        jsGetCharges(fTotalFCost);
        document.getElementById("trCalResults").style.display = "inline";
    }
    else {
        document.getElementById("trCalResults").style.display = "none";
    } 
    
}

function js_Reset() {
    document.getElementById("Quantity").value = "";
    document.getElementById("Period").selectedIndex = 0;
    document.getElementById("Price").value = "";
    document.getElementById("trCalResults").style.display = "none";
    document.getElementById("spnQuantity").style.display = "none";
    document.getElementById("spnPrice").style.display = "none";
}
