﻿// IYM FUNCTIONS //

var bSubmitButtonId = false;

$(document).ready(function()
{
    $("input[@name='cartNotes']").click(function()
    {
        bSubmitButtonId = true
    });
    $("input[name='/asset/attribute[id=#valueOfGoods#]']").change(function()
    {
        var consignmentValue = $(this).val();
        if (isNaN(consignmentValue))
        {
            alert('Please enter only numeric characters');
            $(this).val('');
            $(this).focus(true);
        }
    });
});




/* ============================== FORM CHECK JS FOR NOTES FORM ============================== */

function form_check_quote_notes(oForm, bTermsTrigger)
{

    var oElem, i, bValid, cCheckRadio, cTestName, oOptions, bSelected, nElemTypeGroup, cId, cName;
    var aRadioCheck = new Array();
    var bHasDisabledButton = false;
    bValid = true;

    if (bSubmitButtonId == true)
    {
        if (bValid)
        {
            bValid = form_check_itemHasName(oForm);
        }

        if (bValid)
        {
            bValid = form_check_specifiedItems(oForm);
        }
    }

    if (bTermsTrigger == 'termsAgree')
    {
        bValid = form_check_terms_all_agreed(oForm);
    }

    for (i = 0; i < oForm.length; i++)
    {
        oElem = oForm.elements[i]

        switch (oElem.type)
        {
            case "text": nElemTypeGroup = 1; break;
            case "textarea": nElemTypeGroup = 1; break;
            case "checkbox": nElemTypeGroup = 3; break;
            case "radio": nElemTypeGroup = 3; break;
            case "select-one": nElemTypeGroup = 2; break;
            case "select-multiple": nElemTypeGroup = 2; break;
            default: nElemTypeGroup = 0; break;
        }

        // Check if the element is required
        if (oElem.className.toLowerCase().indexOf("required") >= 0)
        {
            switch (nElemTypeGroup)
            {

                case 1:
                    if (form_check_value(oElem.value))
                    {
                        form_alert("required", oElem)
                        bValid = false
                    }
                    break;
                case 2:
                    if (oElem.selectedIndex < 0)
                    {
                        form_alert("required", oElem)
                        bValid = false
                    }
                    else if (form_check_value(oElem.options[oElem.selectedIndex].text))
                    {
                        form_alert("required", oElem)
                        bValid = false
                    }
                    break;
                case 3:
                    // Check if the checkbox group has already been checked through
                    cCheckRadio = "," + aRadioCheck.join(",") + ","
                    cTestName = "," + oElem.name + ","
                    if (cCheckRadio.indexOf(cTestName) < 0)
                    {

                        // Not found - let's do the checks
                        aRadioCheck.push(oElem.name)
                        oOptions = document.getElementsByName(oElem.name)
                        bSelected = false;
                        if (oOptions.length > 1)
                        {
                            for (i = 0; i < oOptions.length; i++) { if (oOptions[i].checked) { bSelected = true; break } }
                        }
                        if (!bSelected && oOptions.length > 1)
                        {
                            form_alert("required", oElem)
                            bValid = false
                        }
                    }
                    break;
            }
            if (!bValid) { break }
        }
    }

    if (bValid)
    {
        for (i = 0; i < oForm.length; i++)
        {
            oElem = oForm.elements[i]
            if (oElem.type == 'text' || oElem.type == 'textarea')
            {
                if (form_check_value(oElem.value)) { oElem.value = ''; }
            }
        }
    }

    if (bValid)
    {
        // Check for Disabled Buttons
        for (i = 0; i < oForm.length; i++)
        {
            oElem = oForm.elements[i];
            if (oElem.type == "hidden" && oElem.id.indexOf("ewSubmitClone_") == 0)
            {
                cId = oElem.id.replace(/ewSubmitClone_/, "")
                cName = oElem.name.replace(/ewSubmitClone_/, "")
                form_disable_button(oForm, cId, cName)
                oElem.id = cId
                oElem.name = cName
                bHasDisabledButton = true;
            }
        }

        if (bHasDisabledButton)
        {
            for (i = 0; i < oForm.length; i++)
            {
                oElem = oForm.elements[i];
                if (oElem.type == "submit" || oElem.type == "button") { oElem.disabled = true; }
            }
        }
    }

    bSubmitButtonId = false;
    return bValid;
}


function totalValueOperation()
{
    var runningTotal = 0;
    $("input[@class='currency additionalItemCost']").each(function()
    {
        var inputValue = $(this).val();
        if (!isNaN(inputValue))
        {
            runningTotal = runningTotal + inputValue * 1;
        }
    });
    var remainingItems = $("input[@id='cValueOfGoods']").val() - runningTotal * 1;
    $("input[@id='nValueOfRemaining']").val(remainingItems);
    $("input[@id='nValueOfSpecified']").val(runningTotal);
}

function form_check_itemHasName()
{
    
    var isValid = true;
    var errorMessage = "You must add a description for every item specified";
    /* */
    $("input[@class='currency additionalItemCost']").each(function()
    {
        var inputValue = $(this).val();
        /* CHECK ITEM HAS NAME */
        if (inputValue > 0)
        {
            /*Build description Id for selecting*/
            var inputDescriptionId = '#cDescription' + this.id.substring(13);
            $(inputDescriptionId).each(function()
            {
                if ($(this).val().length == 0 || $(this).val().lastIndexOf('Please enter') == 0)
                {
                    isValid = false;
                }
            });
        }
        /* CHECK ITEM PRICE IS A NUMBER */

        if (isValid)
        {

            if (isNaN(inputValue))
            {
                errorMessage = "The value of each item must be numeric only.\n\If you have entered a currency symbol please remove it and re-submit.";
                isValid = false;
            }
        }

    });

    if (isValid == false) { alert(errorMessage) }
    return isValid;
}

function form_check_specifiedItems()
{
    var isValid = true;

    $("input[@id = 'nValueOfRemaining']").each(function()
    {
        if ($(this).val() < 0)
        {
            alert('The value of your specified items is greater than your total sum insured.\n\nPlease adjust your specified items, or click re-quote to increase sum of cover.');
            isValid = false;
        }
    });

    return isValid;

}

function form_check_terms_all_agreed()
{
    var bValid = true;

    $("#optionsForm input[@name='cKeyFacts']").each(function()
    {

        if (this.checked == false)
        {
            bValid = false;
        }

    })
    if (!bValid)
    {
        alert("You must agree to all the Terms and Conditions before you can make a payment");
    }
    return bValid;
}