﻿// JScript File

 var sfsIndex       = new Object;
    sfsIndex.populated = false;
    function sfsComplete () {
        if (!document.getElementById) return;
        
        var text   = document.getElementById('ctl00_ctPublic_CtlPropertyAlert1_tb');
        var select = document.getElementById('ctl00_ctPublic_CtlPropertyAlert1_lbSuburb');

        if (!sfsIndex.populated) sfsBuildIndex();

        var suburb = text.value.match(/,*([^,]+)$/);
        if (suburb) {
            var name = suburb[1].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
            for (var i = sfsIndex[name.charAt(0)]; i < select.options.length; i++) {
                if (select.options[i].text.toUpperCase().indexOf(name) == 0) {
                    select.selectedIndex = i;
                    break;
                }
                else {
                    select.selectedIndex = -1;
                }
            }
        }
    }
    function sfsInsert () {
        if (!document.getElementById) return;
        var text    = document.getElementById('ctl00_ctPublic_CtlPropertyAlert1_tb');
        var select  = document.getElementById('ctl00_ctPublic_CtlPropertyAlert1_lbSuburb');
        text.focus();
        var suburb = select.options[select.selectedIndex];
        if (select.selectedIndex == 0) {
            text.value = '';
            return true;
        }
        var textSuburbs = text.value.split(/\s*,\s*/);
        if (!textSuburbs) return;
        var pattern = new RegExp('\s*' + suburb.text + '\s*$');
        for (var i = 0; i < textSuburbs.length; i++) {
            if (pattern.exec(textSuburbs[i])) return;
        }
        var newvalue = text.value.replace(
            /(^|,)([^,]*)$/,
            "$1 " + suburb.text + ', '
        );
        text.value = newvalue;
    }
    function sfsBuildIndex () {
        if (!document.getElementById) return;
        var select = document.getElementById('ctl00_ctPublic_CtlPropertyAlert1_lbSuburb');
        for (var i = select.options.length; i--;) {
            sfsIndex[select.options[i].text.toUpperCase().charAt(0)] = i;
        }
        sfsIndex.populated = true;
    }
