var RATE = 0;
var PRICE = 1;

function validate()
{
    if (!isNaturalNumber(document.constant.d, " for the Current Year Dividend Payout."))
    {
        return false;
    }

    if (!isNaturalNumber(document.constant.g, " for the Constant Growth Rate."))
    {
        return false;
    }

    if ((!document.constant.choice[RATE].checked) && (!document.constant.choice[PRICE].checked))
    {
        alert ("Please Select Rate of Return or Current Price of Equity Share");
        document.constant.choice[RATE].focus();
        return false;
    }

    if (document.constant.choice[RATE].checked)
    {
        if (!isValueInRange(document.constant.v, 1, 100, " for the Rate of return of the Equity share."))
        {
            return false;
        }
    }

    if (document.constant.choice[PRICE].checked)
    {
        if (!isNaturalNumber(document.constant.v, " for the Current price of the Equity share."))
        {
            return false;
        }
    }

    return true;
}

function calc()
{
    var d =parseFloat(document.constant.d.value);
    var g =parseFloat(document.constant.g.value) / 100;

    var v = parseFloat(document.constant.v.value);
    if (document.constant.choice[RATE].checked)
    {
        if (parseFloat(document.constant.v.value) <= parseFloat(document.constant.g.value))
        {
            alert ("Rate of return must be greater than the Constant Growth Rate");
            document.constant.v.focus();
        }
        else
        {
            k1 = (d * (1 + g)) / ((v / 100) - g);
            document.constant.v.focus();
            document.constant.k2.value = "Current Price of the Equity Share should be Rs. " + round(k1, 2);
        }
    }
    else if (document.constant.choice[PRICE].checked)
    {
        k10 = (d * (1 + g) / v) + g;
        k3 = k10 * 100;
        document.constant.v.focus();
        document.constant.k2.value = "Rate of return on the Equity Share is " + round(k3, 2) + "%";
    }
}

function clean()
{
    document.constant.d.value = "";
    document.constant.g.value = "";
    document.constant.v.value = "";
    document.constant.choice[RATE].checked = false;
    document.constant.choice[PRICE].checked = false;
    document.constant.k2.value = "";
}

function checkValue()
{
    if (document.constant.choice[RATE].checked)
    {
        if (document.constant.v.value.length >= 3)
        {
            document.constant.v.value = document.constant.v.value.substr(0, 3);
        }
    }
}
