﻿//
// Declare constants for the user control prefixes. This allows us to more easily update 
// the javascript when the user control instances are renamed
var ubBI1 = 'ctl00_cphMain_wzQuickQuote_UcBorrowerIncome1_';
var ubBI2 = 'ctl00_cphMain_wzQuickQuote_UcBorrowerIncome2_';
////set page event handlers
//if (window.attachEvent) {
//    window.attachEvent("onload", Resize);
//    window.attachEvent("onresize", Resize);
//} else {
//    window.addEventListener("DOMContentLoaded", Resize, false);
//}

// -----------------------------------------------------------------------------------------------
//
// HIDE AND DISPLAY ELEMENTS AND SECTIONS
//
// -----------------------------------------------------------------------------------------------
function showListings() {
    var c = document.getElementById(ucCreditImpairment + 'txtListings');
    if (IsNumeric(c.value)) {
        if (c.value > 0) {
            c = document.getElementById('rDefaultsUnpaid');
            if (c.style.visibility == 'hidden') {
                c.style.visibility = 'visible';
                c = document.getElementById(ucCreditImpairment + 'txtDefaultsUnpaid');
                c.value = '0';
            }
            c = document.getElementById('rDefaultsPaid');
            if (c.style.visibility == 'hidden') {
                c.style.visibility = 'visible';
                c = document.getElementById(ucCreditImpairment + 'txtDefaultsPaid');
                c.value = '0';
            }
        }
        else {
            c = document.getElementById('rDefaultsUnpaid');
            if (c.style.visibility == 'visible') {
                c.style.visibility = 'hidden';
                c = document.getElementById(ucCreditImpairment + 'txtDefaultsUnpaid');
                if (c != null) {
                    c.value = '0';
                }
            }

            c = document.getElementById('rDefaultsPaid');
            if (c.style.visibility == 'visible') {
                c.style.visibility = 'hidden';
                c = document.getElementById(ucCreditImpairment + 'txtDefaultsPaid');
                if (c != null) {
                    c.value = '0';
                }
            }
        }
    }

}




// Move an element directly on top of another element (and optionally
// make it the same size)
function Cover(bottom, top, ignoreSize) {
    var location = Sys.UI.DomElement.getLocation(bottom);
    top.style.position = 'absolute';
    top.style.top = location.y + 'px';
    top.style.left = location.x + 'px';
    if (!ignoreSize) {
        top.style.height = bottom.offsetHeight + 'px';
        top.style.width = bottom.offsetWidth + 'px';
    }
}

function toggleVisibility(cn) {
    var c = document.getElementById(cn);
    if (c != null) {
        if (c.style.display == 'none') {
            c.style.display = 'block';
        }
        else {
            c.style.display = 'none';
        }
    }
}

function hideControl(cn) {
    //debugger;
    //Find the control
    var c = document.getElementById(cn);
    if (c != null) {
        c.style.visibility = 'hidden';
    }
}
function showControl(cn) {
    //debugger;
    //Find the control
    var c = document.getElementById(cn);
    if (c != null) {
        c.style.visibility = 'visible';
    }
}

function enableControl(cn) {
    var c = document.getElementById(cn);
    if (c != null) {
        c.style.visibility = 'hidden';
    }
}

function disableControl(cn) {
    //Find the control
    var c = document.getElementById(cn);

    //If the control is already disabled get out of here.
    if (c.disabled == true) { return; }

    //Remove all child entries & disable the control
    removeChildren(c);
    c.disabled = true;

    //Create a new generic option
    myOption = new Option('select one...', '', true, true);
    c.options[c.options.length] = myOption;

}

function removeChildren(s) {
    while (s.hasChildNodes())
        s.removeChild(s.childNodes[0]);
}





// -----------------------------------------------------------------------------------------------
//
// CALCULATIONS
//
// -----------------------------------------------------------------------------------------------
function validateListings(source, arguments) {
    //Find the total control, paid control & unpaid control
    var t = document.getElementById(ucCreditImpairment + 'txtListings');
    var p = document.getElementById(ucCreditImpairment + 'txtDefaultsPaid');
    var u = document.getElementById(ucCreditImpairment + 'txtDefaultsUnpaid');

    var paid = 0;
    var unpaid = 0;
    var total = 0;

    if (!isNaN(parseInt(p.value))) { paid = p.value; }
    if (!isNaN(parseInt(u.value))) { unpaid = u.value; }
    if (!isNaN(parseInt(t.value))) { total = t.value; }


    if (parseInt(paid) + parseInt(unpaid) > parseInt(total)) {
        //debugger;
        arguments.IsValid = false;
    }
    else {
        arguments.IsValid = true;
    }
}

function CalculateRentalPercent() {
    //Find the total control
    var c = document.getElementById(ubBI1 + 'txtRentalIncome');
    var t = document.getElementById(ubBI1 + 'lblRental80');

    if (c == null) {
        //Get the Borrower2 control
        c = document.getElementById(ubBI2 + 'txtRentalIncome');
        t = document.getElementById(ubBI2 + 'lblRental80');
    }

    if (c != null && t != null) {
        var value = c.value * assessment_RentalIncome; //.80;
        t.innerHTML = value.toFixed(2);
    }
    else {
        t.innerHTML = 0;
    }
}

function CalculateCreditCard() {
    //Find the total control
    var c = document.getElementById(ubBI1 + 'txtCreditCards');
    var t = document.getElementById(ubBI1 + 'lblCreditCard25');

    if (c == null) {
        //Get the Borrower2 control
        c = document.getElementById(ubBI2 + 'txtCreditCards');
        t = document.getElementById(ubBI2 + 'lblCreditCard25');
    }

    if (c != null && t != null) {
        var value = c.value * assessment_CreditCard//;.025;
        t.innerHTML = value.toFixed(2);
    }
    else {
        t.innerHTML = 0;
    }
}


function CalculateServicability() {
    // 

}


// -----------------------------------------------------------------------------------------------
//
// UTILITIY FUNCTIONS
//
// -----------------------------------------------------------------------------------------------
function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber = true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) {
        Char = sText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
        }
    }
    return IsNumber;
}

function ValidatePostcode(oSrc, args) {
    //args.IsValid = false;
    //debugger
    //Find the suburb drop down control and ensure it is enabled
    var d = document.getElementById(ucAPL + 'ddSuburbs');
    //If the control is already disabled get out of here.
    if (d.disabled == true) { return false; }
    if (d.value == '(select one...)') { return false; }

    //Find the control
    var c = document.getElementById(ucAPL + 'txtPostcode');
    if (c.value == '') { return false; }
    //'reqvalPostcode.IsValid = false;
    args.IsValid = true;
}

function showPressCalculate() {
    var c = document.getElementById('ctl00_cphMain_wzQuickQuote_UcLoanDetails1_divPressCalculate');
    if (c != null) {
        c.style.visibility = 'visible';
    }

    c = document.getElementById('ctl00_cphMain_wzQuickQuote_UcLoanDetails1_divSuccessful');
    if (c != null) {
        c.style.visibility = 'hidden';
    }

    c = document.getElementById('ctl00_cphMain_wzQuickQuote_UcLoanDetails1_divUnsuccessul');
    if (c != null) {
        c.style.visibility = 'hidden';
    }
}


//function Resize() {
//    var footerDiv = document.getElementById("ctl00_footer");
//    var windowWidth = document.body.clientWidth;
//    var ieOffset = 10;
//    footerDiv.style.left = (windowWidth / 2) - (footerDiv.style.pixelWidth / 2) + ieOffset + "px";
//}
