﻿var creditorCount = 0;
var incomeTotalBalance = 0;
var expenditureTotalBalance = 0;

// Used by ctvPhone_ClientValidate.
var homePhoneID = '';
var mobilePhoneID = '';

// Used by ctvDateOfBirth_ClientValidate on Step3-1.aspx.
var dayID = '';
var monthID = '';
var yearID = '';
var dateOfBirthValidatorID = '';

// Used by ctvPhone_ClientValidate on Step3-1.aspx.
var homePhoneID = '';
var mobilePhoneID = '';

// Used by ctvTermsAndConditionsConfirm_ClientValidate on Step3-1.aspx.
var tsAndCsConfirmationID = '';

function updateTotalByContainer(containerName, totalName, nextButtonID) {
    var inputs;
    var input;
    var index;
    var total = 0;
    var mdi;
    var flooredMDI;

    inputs = document.getElementById(containerName).getElementsByTagName('input');

    for (index = 0; index < inputs.length; ++index) {
        input = inputs[index];

        if (input.getAttribute('type') == 'text')
            total += input.value * 1;
    }

    document.getElementById(totalName).innerHTML = '£' + total;

    if (containerName == 'Income')
        incomeTotalBalance = total;
    else
        expenditureTotalBalance = total;

    mdi = incomeTotalBalance - expenditureTotalBalance;

    if (mdi > 0)
        flooredMDI = mdi
    else
        flooredMDI = 0

    document.getElementById('MDI').innerHTML = '£' + flooredMDI;

    if (mdi >= 100) {
        document.getElementById('Message').innerHTML = '';
        document.getElementById(nextButtonID).disabled = false;
    }
    else {
        document.getElementById(nextButtonID).disabled = true;

        if (expenditureTotalBalance > 0)
            document.getElementById('Message').innerHTML = 'Unfortunately, based on the details that you have entered, we\'re not able to offer you a solution. However, one of our partners may be able to. Please call 0800 933 6666.';
        else
            document.getElementById('Message').innerHTML = '';
    }
}

function updateTotalByElement(elementName, totalName) {
    var inputs;
    var input;
    var index;
    var total = 0;

    inputs = document.getElementsByTagName('input');

    for (index = 0; index < inputs.length; ++index) {
        input = inputs[index];

        if (input.getAttribute('type') == 'text' && Right(input.name, elementName.length) == elementName)
            total += input.value * 1;
    }

    document.getElementById(totalName).innerHTML = total;
}

function Right(str, n) {
    if (n <= 0)     // Invalid bound, return blank string
        return "";
    else if (n > String(str).length)   // Invalid bound, return
        return str;                     // entire string
    else { // Valid bound, return appropriate substring
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

function filterNumber(input, event) {
    var key;
    var keyChar;

    if (window.event)
        key = window.event.keyCode;
    else if (event)
        key = event.which;
    else
        return true;

    keyChar = String.fromCharCode(key);

    // Control keys.
    if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27))
        return true;
    // Numbers
    else if (("0123456789").indexOf(keyChar) >= 0)
        return true;
    else
        return false;
}

function checkValue(input, defaultToValue) {
    if (input.value.length == 0 || input.value == 0)
        input.value = defaultToValue;
    else
        input.value = input.value * 1;
}

function creditorClicked(checkBox, nextButtonID) {
    if (checkBox.checked)
        creditorCount += 1;
    else
        creditorCount -= 1;

    document.getElementById(nextButtonID).disabled = (creditorCount == 0);
}

function showHelp(caller, helpDivName, placement) {
    var helpDiv = document.getElementById(helpDivName)

    with (helpDiv.style) {
        display = 'block';
        top = findPosY(caller) + 20 + 'px';

        if (placement == 'Left')
            left = findPosX(caller) - 330 + 'px';
        else
            left = findPosX(caller) + 20 + 'px';
    }
}

function hideHelp(helpDivName) {
    document.getElementById(helpDivName).style.display = 'none';
}

function showTermsAndConditions(caller) {
    var tsandCsDiv = document.getElementById('TermsAndConditions');

    with (tsandCsDiv.style) {
        display = 'block';
        top = findPosY(caller) - 490 + 'px';
        left = findPosX(document.getElementById('Body')) + 50 + 'px';
    }
}

function hideTermsAndConditions() {
    document.getElementById('TermsAndConditions').style.display = 'none';
}

function findPosX(obj) {
    var curleft = 0;

    if (obj.offsetParent) {
        while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    } else if (obj.x)
        curleft += obj.x;

    return curleft;
}

function findPosY(obj) {
    var curtop = 0;

    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent)
                break;
            obj = obj.offsetParent;
        }
    } else if (obj.y)
        curtop += obj.y;

    return curtop;
}

function isDate(day, month, year) {
    var date = new Date(year, month - 1, day);

    return (date.getDate() == day && date.getMonth() == month - 1 && date.getFullYear() == year);
}

function ctvDateOfBirth_ClientValidate(source, args) {
    var day = document.getElementById(dayID).value;
    var month;
    var year = document.getElementById(yearID).value;
    var monthControl;
    var monthName;
    var errorMessage;

    monthControl = document.getElementById(monthID);
    month = monthControl.value;

    if (Right(day, 1) == ']' || Right(month, 1) == ']' || Right(year, 1) == ']') {
        errorMessage = 'Enter your date of birth'
        document.getElementById(dateOfBirthValidatorID).errormessage = errorMessage;
        args.IsValid = false;
    }
    else if (isDate(day, month, year))
        args.IsValid = true;
    else {
        monthName = monthControl.options[monthControl.selectedIndex].text;
        errorMessage = 'Check the date of birth you have entered as ' + day + ' ' + monthName + ' ' + year + ' is not a valid date'
        document.getElementById(dateOfBirthValidatorID).errormessage = errorMessage;
        args.IsValid = false;
    }
}

function ctvPhone_ClientValidate(source, args) {
    if (document.getElementById(homePhoneID).value != '' || document.getElementById(mobilePhoneID).value != '')
        args.IsValid = true;
    else
        args.IsValid = false;
}

function ctvTermsAndConditionsConfirm_ClientValidate(source, args) {
    if (document.getElementById(tsAndCsConfirmationID).checked == true)
        args.IsValid = true;
    else
        args.IsValid = false;
}

function DoProcessing() {
    setTimeout("document.getElementById('ProcessingStep1').className = ''; document.getElementById('ProcessingStep2').className = 'Current'", 3000);
    setTimeout("document.getElementById('ProcessingStep2').className = ''; document.getElementById('ProcessingStep3').className = 'Current'", 6000);
    setTimeout("window.location.href = 'http://www.debtline-direct.com/Plan/Complete.aspx'", 9000)
}

function ctvPhone_ClientValidate(source, args) {
    if (document.getElementById(homePhoneID).value != '' || document.getElementById(mobilePhoneID).value != '')
        args.IsValid = true;
    else
        args.IsValid = false;
}
