﻿//var propTransRecordPrice = new Array(0.75,3,2.00);
//var propCharRecordPrice = new Array(0.35,3,1.00);

function GetPropTranRecordsPrice(quantity)
{
    if (quantity > propTransRecordPrice[1])
        return ((quantity - propTransRecordPrice[1]) * propTransRecordPrice[0]) + propTransRecordPrice[2];
    else
    return propTransRecordPrice[2];
        
}

function GetPropCharRecordsPrice(quantity)
{
    if (quantity > propCharRecordPrice[1])
        return ((quantity - propCharRecordPrice[1]) * propCharRecordPrice[0]) + propCharRecordPrice[2];
    else
    return propCharRecordPrice[2];
}

function NumtoCurrencyFormat(inVal) 
{
	inVal -= 0;
	inVal = (Math.round(inVal*100))/100;
	return (inVal == Math.floor(inVal)) ? inVal + '.00' 
     : ( (inVal*10 == Math.floor(inVal*10)) ? 
              inVal + '0' : inVal);
}

function WritepanSelectedProperties(hfInterestedLvidRecKeysName,
    hfInterestedPropCharRecKeysName, panSelectedPropertiesName)
{
    var hfInterestedLvidRecKeys = document.getElementById(hfInterestedLvidRecKeysName);
    var hfInterestedPropCharRecKeys = document.getElementById(hfInterestedPropCharRecKeysName);
    var panSelectedProperties = document.getElementById(panSelectedPropertiesName);
    
    if ((parseInt(hfInterestedLvidRecKeys.value) > 0)
        && (parseInt(hfInterestedPropCharRecKeys.value) > 0))
    {
        panSelectedProperties.innerText = 'You have selected ' +
            hfInterestedLvidRecKeys.value +
            ' transaction records which will cost you £' + 
            NumtoCurrencyFormat(GetPropTranRecordsPrice(hfInterestedLvidRecKeys.value)) +
            ' and ' + 
            hfInterestedPropCharRecKeys.value + 
            ' property characteristics records which will cost you £' +
            NumtoCurrencyFormat(GetPropCharRecordsPrice(hfInterestedPropCharRecKeys.value)) +
            '';
    }
    else
    {
        if (parseInt(hfInterestedLvidRecKeys.value) > 0)
        {
            panSelectedProperties.innerText = 'You have selected ' +
            hfInterestedLvidRecKeys.value +
            ' transaction records which will cost you £' + 
            NumtoCurrencyFormat(GetPropTranRecordsPrice(hfInterestedLvidRecKeys.value)) +
            '';
        }
        else
        {
            if (parseInt(hfInterestedPropCharRecKeys.value) > 0)
            {
                panSelectedProperties.innerText = 'You have selected ' +
                hfInterestedPropCharRecKeys.value +
                ' property characteristics records which will cost you £' +
                NumtoCurrencyFormat(GetPropCharRecordsPrice(hfInterestedPropCharRecKeys.value)) +
                '';
            }
            else
            {
                panSelectedProperties.innerText = 'You have selected no records';
            }
        }
    }
}

//

function UpdateCheckBoxState(id, checkState)
{
    var cb = document.getElementById(id);
    if (cb != null)
        cb.checked = checkState;
}

function HandleCheckBoxClick(id, valueFieldId)
{
    var valueField = document.getElementById(valueFieldId);
    var cb = document.getElementById(id);
    if (cb != null)
    {
        var intVal = parseInt(valueField.value);
        if (cb.checked)
            intVal++;
        else
            intVal--;
        valueField.value = intVal;
    }
}

function ChangeCheckBoxState(id, checkState, valueFieldId, isHandleNoOfRecords)
{
    var valueField = document.getElementById(valueFieldId);
    var cb = document.getElementById(id);
    if (cb != null)
    {
        if ((cb.checked != checkState) && isHandleNoOfRecords)
        {
            var intVal = parseInt(valueField.value);
            if (checkState)
                intVal++;
            else
                intVal--;
            valueField.value = intVal;
        }
        cb.checked = checkState;
    }
}

function ChangeAllCheckBoxStates(CheckBoxIDs, checkState, valueFieldId)
{
    if (CheckBoxIDs != null)
    {
        for (var i = 0; i < CheckBoxIDs.length; i++)
        {
            if (i > 0)
                ChangeCheckBoxState(CheckBoxIDs[i], checkState, valueFieldId, true);
            else
                ChangeCheckBoxState(CheckBoxIDs[i], checkState, valueFieldId, false);
        }
    }
}

function ChangeHeaderAsNeeded(CheckBoxIDs)
{
    if (CheckBoxIDs != null)
    {
        for (var i = 1; i < CheckBoxIDs.length; i++)
        {
            var cb = document.getElementById(CheckBoxIDs[i]);
            if (!cb.checked)
            {
                UpdateCheckBoxState(CheckBoxIDs[0], false);
                return;
            }
        }
        UpdateCheckBoxState(CheckBoxIDs[0], true);
    }
}


