﻿    var avLockCheck;
    window.addEvent('domready', function() {
        avLockCheck = new FormCheck('aspnetForm'
        ,
        {
            display: {
                keepFocusOnError: 0,
                scrollToFirst: 1
            },
            alerts: {
                select: "Please select an option.",
                digit: "Please enter a valid number.",
                number: 'Please enter a $ amount.',
                alphanum: 'Please enter your full name.',
                required: 'This field is required.'
            }
        }
        );
        updateSuburbs(document.getElementById(prefix + 'txtPostcode').value);
        checkLoanPurpose(document.getElementById(prefix+'lstLoanPurpose').value);
        show15Percent();
        showSecurity();
    });
    var avlock = false;
    function checkval(el) {
        if (avlock) return true;
        avlock = true;
        var txtValue = replaceAll(el.value, ',', '');
        if (avLockCheck.validate(el) && parseInt(txtValue) < 49999) {
            var errorMsg = "Please enter an amount greater than or equal to $50,000.";
            el.errors.push(errorMsg);
            avlock = false;
            return false;
        }

        avlock = false;
        return true;
    }
    var avlock = false;
    function checkval2(el) {
        if (avlock) return true;
        avlock = true;
        var txtValue = replaceAll(el.value, ',', '');
        var dblLoanAmount = replaceAll(document.getElementById(prefix+'txtLoanAmount').value, ',', '');
        if (avLockCheck.validate(el) && parseInt(txtValue) < parseInt(dblLoanAmount)) {
            var errorMsg = "Please enter a house value greater than the loan amount.";
            el.errors.push(errorMsg);
            avlock = false;
            return false;
        }

        avlock = false;
        return true;
    }
    function checkname(el) {
        if (avlock) return true;
        avlock = true;
        var txtValue = replaceAll(el.value, ',', '');
        if (avLockCheck.validate(el) && (txtValue.length <= 1)) {
            var errorMsg = "Please enter your full name.";
            el.errors.push(errorMsg);
            avlock = false;
            return false;
        }

        avlock = false;
        return true;
    } 
        
    function show15Percent()
    {
        if(document.getElementById(prefix+'lstGenuineSavings').value =="No")
        {
            document.getElementById('tr15Percent').style.display='';
        }
        else
        {
            document.getElementById('tr15Percent').style.display='none';
        }
    }
    function showSecurity()
    {
        if(document.getElementById(prefix+'lst15PercentDeposit').value =="No")
        {
            document.getElementById('trSecurity').style.display='';
        }
        else
        {
            document.getElementById('trSecurity').style.display='none';
        }
    }
    
    function checkOtherFields()
    {
            if(document.getElementById(prefix+'lstSuburb').disabled==true)
            {
                alert('Please select a suburb.');
                document.getElementById(prefix+'txtPostcode').focus();
                return false;
            }
        if(blnDoValidation)
        {
            if(document.getElementById(prefix+'lstGenuineSavings').selectedIndex==0)
            {
                alert('Please select if you have a 5% genuine savings.');
                document.getElementById(prefix+'lstGenuineSavings').focus();
                return false;
            }
        
            if(document.getElementById(prefix+'lstGenuineSavings').value =='No' && document.getElementById(prefix+'lst15PercentDeposit').selectedIndex==0)
            {
                alert('Please select if you can get a 15% deposit elsewhere.');
                document.getElementById(prefix+'lst15PercentDeposit').focus();
                return false;
            }
            if(document.getElementById(prefix+'lst15PercentDeposit').value =='No' && document.getElementById(prefix+'lstSecurity').selectedIndex==0)
            {
                alert('Please select if you have a family member that can act as security for your loan.');
                document.getElementById(prefix+'lstSecurity').focus();
                return false;
            }
        }
        return true;
    }
    var blnDoValidation=true;
    function checkLoanPurpose(val)
    {
        if(val == "Refinancing Consolidation" || val=="Reverse Mortgage" || val=="Moving Or Renovating")
        {
            blnDoValidation=false;
            document.getElementById('tr5Percent').style.display='none';
        }
        else
        {
            blnDoValidation=true;
            document.getElementById('tr5Percent').style.display='';  
        }
    }
    
    function updateSuburbs(val)
    {
        var objSelect = document.getElementById(prefix+'lstSuburb');
        if(val.length>=3)
        {
            objSelect.length=0;
            objSelect.disabled=true;
            var randomnumber=Math.floor(Math.random()*99999999)
            var xmlObj = getXMLDocument('/ajax/suburb_lookup.aspx?input='+ val + '&rnd=' + randomnumber);
            if(xmlObj.childNodes.length>0)
            {
                var nodes = xmlObj.getElementsByTagName("rs");
                if(nodes.length >0)
                {
                    for (var i = 0; i < nodes.length; i++)
                    {
                        var opt = document.createElement('option');
                        opt.value = nodes[i].childNodes[0].nodeValue + ' - '  + nodes[i].getAttribute('state');
                        opt.text = nodes[i].childNodes[0].nodeValue + ' - '  + nodes[i].getAttribute('state')
                        
                        try 
                        {
                            objSelect.add(opt, null);
                        }
                        catch(ex) 
                        {
                            objSelect.add(opt); // IE only
                        }
                    }
                    objSelect.disabled=false;
                }
                else
                {
                    objSelect.length=0;
                    var opt = document.createElement('option');
                    opt.value = '';
                    opt.text = 'No match found';
                    try 
                    {
                        objSelect.add(opt, null);
                    }
                    catch(ex) 
                    {
                        objSelect.add(opt); // IE only
                    }
                    objSelect.disabled=true;
                }
            }
            else
            {
                objSelect.length=0;
                var opt = document.createElement('option');
                opt.value = '';
                opt.text = 'No match found';
                try 
                {
                    objSelect.add(opt, null);
                }
                catch(ex) 
                {
                    objSelect.add(opt); // IE only
                }
                objSelect.disabled=true;
            }
        }
        else
        {
            objSelect.length=0;
            objSelect.length=0;
                var opt = document.createElement('option');
                opt.value = '';
                opt.text = 'No match found';
                try 
                {
                    objSelect.add(opt, null);
                }
                catch(ex) 
                {
                    objSelect.add(opt); // IE only
                }
            objSelect.disabled=true;
        }
    }
