﻿    var strErrorColor = 'Red';
    var strBGErrorColor = '#ffe6e6';
    var strNormalColor = '#000000';
    
    function validateInsurance() {
        if (!document.getElementById(prefix + 'lstOtherInsuranceTypes_0').checked &&
       !document.getElementById(prefix+'lstOtherInsuranceTypes_1').checked &&
       !document.getElementById(prefix + 'lstOtherInsuranceTypes_2').checked &&
       !document.getElementById(prefix + 'lstOtherInsuranceTypes_3').checked) {
            alert('Please select at least one insurance type to receive a free quote.');
            return false;
        }
    }

    function validateForm() {
        var blnError = false;
        if (document.getElementById(prefix + 'txtFirstName').value <= '') {
            document.getElementById(prefix + 'txtFirstName').style.backgroundColor = strBGErrorColor;
            blnError = true;
        }
        else
            document.getElementById(prefix + 'txtFirstName').style.color = strNormalColor;

        if (document.getElementById(prefix + 'txtLastName').value <= '') {
            document.getElementById(prefix + 'txtLastName').style.backgroundColor = strBGErrorColor;
            blnError = true;
        }
        else
            document.getElementById(prefix + 'txtLastName').style.color = strNormalColor;

        if (document.getElementById(prefix + 'txtEmail').value <= '') {
            document.getElementById(prefix + 'txtEmail').style.backgroundColor = strBGErrorColor;
            blnError = true;
        }
        else {
            if (!isEmail(document.getElementById(prefix + 'txtEmail').value)) {
                document.getElementById(prefix + 'txtEmail').style.backgroundColor = strBGErrorColor;
                blnError = true;
            }
            else
                document.getElementById(prefix + 'txtEmail').style.color = strNormalColor;
        }

        if (document.getElementById(prefix + 'txtContactPhone').value <= '') {
            document.getElementById(prefix + 'txtContactPhone').style.backgroundColor = strBGErrorColor;
            blnError = true;
        }
        else {
            if (replaceAll(document.getElementById(prefix + 'txtContactPhone').value).search("^[0-9]{10}$|^\(0[1-9]{1}\)[0-9]{8}$|^[0-9]{8}$|^[0-9]{4}[ ][0-9]{3}[ ][0-9]{3}$|^\(0[1-9]{1}\)[ ][0-9]{4}[ ][0-9]{4}$|^[0-9]{4}[ ][0-9]{4}$") == -1) {
                document.getElementById(prefix + 'txtContactPhone').style.backgroundColor = strBGErrorColor;
                blnError = true;
            }
            else
                document.getElementById(prefix + 'txtContactPhone').style.color = strNormalColor;
        }

        if (document.getElementById(prefix + 'txtPostcode').value <= '') {
            document.getElementById(prefix + 'txtPostcode').style.backgroundColor = strBGErrorColor;
            blnError = true;
        }
        else {
            document.getElementById(prefix + 'txtPostcode').style.color = strNormalColor;
        }
        
        /*
        if (document.getElementById(prefix + 'lstSuburb').value <= '') {
            document.getElementById(prefix + 'lstSuburb').style.color = strErrorColor;
            blnError = true;
        }
        else
            document.getElementById(prefix + 'lstSuburb').style.color = strNormalColor;
        */

        if (document.getElementById(prefix + 'lstLoanPurpose').selectedIndex <= 0) {
            document.getElementById(prefix + 'lstLoanPurpose').style.backgroundColor = strBGErrorColor;
            blnError = true;
        }
        else
            document.getElementById(prefix + 'lstLoanPurpose').style.color = strNormalColor;

        if (document.getElementById(prefix +'lstLoanPurpose').value == 'First Home Buyer') {
            if (document.getElementById(prefix + 'lstDeposit').value <= '') {
                document.getElementById(prefix + 'lstDeposit').style.backgroundColor = strBGErrorColor;
                blnError = true;
            }
            else {
                document.getElementById(prefix + 'lstDeposit').style.color = strNormalColor;
            }
            if (document.getElementById(prefix + 'lstDeposit').value == 'Yes' && (document.getElementById(prefix + 'lstSixMonths').value <= "")) {
                document.getElementById(prefix + 'lstSixMonths').style.backgroundColor = strBGErrorColor;
                blnError = true;
            }
            else {
                document.getElementById(prefix + 'lstSixMonths').style.color = strNormalColor;
            }
        }
        if (document.getElementById(prefix + 'lstHouseholdIncome').value <= '') {
            document.getElementById(prefix + 'lstHouseholdIncome').style.backgroundColor = strBGErrorColor;
            blnError = true;
        }
        else {
            document.getElementById(prefix + 'lstHouseholdIncome').style.color = strNormalColor;
        }
        if (document.getElementById(prefix + 'lstCreditHistory').value <= '') {
            document.getElementById(prefix + 'lstCreditHistory').style.backgroundColor = strBGErrorColor;
            blnError = true;
        }
        else {
            document.getElementById(prefix + 'lstCreditHistory').style.color = strNormalColor;
        }

        if (document.getElementById(prefix + 'txtLoanAmount').value <= '') {
            document.getElementById(prefix + 'txtLoanAmount').style.backgroundColor = strBGErrorColor;
            blnError = true;
        }
        else {
            var fltLoanAmount = parseFloat(replaceAll(document.getElementById(prefix + 'txtLoanAmount').value, ',', ''));
            if (fltLoanAmount < (dblMinLoanAmount-1)) {
                document.getElementById(prefix + 'txtLoanAmount').style.backgroundColor = strBGErrorColor;
                blnError = true;
            }
            else
                document.getElementById(prefix + 'txtLoanAmount').style.color = strNormalColor;
        }
        if (!blnError) {
            document.getElementById('divError').style.visibility = 'hidden';
            return true;
        }
        else {
            document.getElementById('divError').style.visibility = 'visible';
            return false;
        }
    }

    function checkLP(val) {
        if (val == "First Home Buyer" || val == "Pre Approval" || val == "Building") {
            document.getElementById('trDeposit').style.display = '';
        }
        else {
            document.getElementById('trDeposit').style.display = 'none';
        }
    }


    function checkDeposit(val) {
        if (val == "Yes") {
            document.getElementById('trSixMonths').style.display = '';
        }
        else {
            document.getElementById('trSixMonths').style.display = 'none';
        }
    }

    function populate(obj, val) {
        if (obj.value.length <= 0)
            obj.value = val;
    }

    function clearField(obj, val) {
        if (obj.value == val) {
            obj.value = '';
        }
    }

    function changeFont(obj) {
        obj.style.color = strNormalColor;
    }

    function updateSuburbs(val) {
        var objSelect = document.getElementById(prefix + 'lstSuburb');
        if (val.length >= 4) {
            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) {
                    objSelect.remove(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;
        }
    }


    function isEmail(argvalue) {

        if (argvalue.indexOf(" ") != -1)
            return false;
        else if (argvalue.indexOf("@") == -1)
            return false;
        else if (argvalue.indexOf("@") == 0)
            return false;
        else if (argvalue.indexOf("@") == (argvalue.length - 1))
            return false;

        // arrayString = argvalue.split("@"); (works only in netscape3 and above.)
        var retSize = customSplit(argvalue, "@", "arrayString");

        if (arrayString[1].indexOf(".") == -1)
            return false;
        else if (arrayString[1].indexOf(".") == 0)
            return false;
        else if (arrayString[1].charAt(arrayString[1].length - 1) == ".") {
            return false;
        }

        return true;

    }

    function customSplit(strvalue, separator, arrayName) {
        var n = 0;

        if (separator.length != 0) {
            while (strvalue.indexOf(separator) != -1) {
                eval("arr" + n + " = strvalue.substring(0, strvalue.indexOf(separator));");
                strvalue = strvalue.substring(strvalue.indexOf(separator) + separator.length,
          strvalue.length + 1);
                n++;
            }
            eval("arr" + n + " = strvalue;");
            arraySize = n + 1;
        }
        else {
            for (var x = 0; x < strvalue.length; x++) {
                eval("arr" + n + " = \"" + strvalue.substring(x, x + 1) + "\";");
                n++;
            }
            arraySize = n;
        }

        eval(arrayName + " = new makeArray(arraySize);");

        for (var i = 0; i < arraySize; i++)
            eval(arrayName + "[" + i + "] = arr" + i + ";");

        return arraySize;
    }

    function makeArray(IntarrSize) {

        for (var n = 0; n < IntarrSize; n++)
            this[n] = "";

        return this;

    }
    function changeFont(obj) {
        obj.style.color = strNormalColor;
    }

    function changeBG(obj) {
        obj.style.backgroundColor = bgChangeColor;
    }

