//================================================================
//  libHVal.js
//      2009-01-15 - JC - RFC5274
//      This file also requires data structures defined in libHValAddressTokens.js
//      so the page including libHVal.js (/h/homevalues) must also include libHValAddressTokens.js
//================================================================
//================================================================
//  Classes/functions
//================================================================
function AddressParser() {
    this.suffixSet;
    this.unitDesignatorSet;
}

AddressParser.prototype.parse_unit = function ( addr ) {
        /* Ported from ~/src/services/home_valuation/HValAddressing.py

        Parse the unit number from an address string.
        
        Assumes that a unit designator exists and that the unit number
        contains no spaces. Returns None if no unit number is found.
        */
        var chain;
        chain = addr.replace(/[\.,]/g, "");
        chain = chain.replace('#', 'UNIT ');
        chain = chain.toUpperCase().split(' ');
        
        var oAddrPartSet = new Set(chain);

        oAddrPartSet.init();
        if ( !this.unitDesignatorSet.hasIntersection(oAddrPartSet) ) {
            return "";
        }
        
        var j = 0;

        for ( var i = chain.length - 1; i >= 0; i-- ) {
            if ( !this.unitDesignatorSet.propsObj.hasOwnProperty(chain[i]) ) {
                // Continue
            }
            else if ( i == 0 ) {
                return "";
            }
            else {
                unit = chain[chain.length - j];

                if ( this.suffixSet.propsObj.hasOwnProperty(unit) ) {
                    return "";
                }
                else {
                    return unit;
                }
            }
            j++;
        }
}
//================================================================
function Set(list) {
    this.elements = list;
    this.propsObj;
}

Set.prototype.init = function () {
    this.propsObj = this.createProps();
}

Set.prototype.numProperties = function () {
    return this.elements.length;
}
Set.prototype.createProps = function () {
    var oTmp = new Object();

    for ( i in this.elements ) {
        oTmp[this.elements[i]] = '';
    }

    return oTmp;
}
Set.prototype.hasIntersection = function(obj_set) {
    // For efficiency we'll select the smaller (prop-length-wise) of the two "sets" to iterate over.
    var objSm = ( this.numProperties() <= obj_set.numProperties() ? this.propsObj : obj_set.propsObj );
    var objLg = ( this.numProperties() > obj_set.numProperties() ? this.propsObj : obj_set.propsObj );
    
    for ( prop in objSm ) {
        if ( objLg.hasOwnProperty(prop) ) {
            return true;
            break;
        }
    }
    return false;
}
//================================================================
var oAddrParser = new AddressParser();
var oSuffixSet = new Set(arrSuffixes);
var oUnitDesignatorSet = new Set(arrUnitDesignators);

oSuffixSet.init();
oUnitDesignatorSet.init();

oAddrParser.suffixSet = oSuffixSet;
oAddrParser.unitDesignatorSet = oUnitDesignatorSet;

//alert(oAddrParser.parse_unit('123 Main St. Apt. 6 Los Angeles, CA 94857'));
//alert(oAddrParser.parse_unit('975 church st unit 4 San Francisco CA'));
//alert(oAddrParser.parse_unit('975 church st #4 San Francisco CA'));
//var oAddrPartSet = oAddrParser.parse_unit('123 Main St. Apt. 6 Los Angeles, CA 94857');

//bIntersection = oSuffixSet.hasIntersection(oAddrPartSet);

//alert(bIntersection);
//var oSuffixSet = new Set(arrSuffixes);
//var oUnitDesigSet = new Set(arrUnitDesignators);

//oUnitDesigSet.intersection(oSuffixSet);

//AddressParser.prototype.parse_unit('123 Main St. Apt. 6 Los Angeles, CA 94857');
//AddressParser.prototype.parse_unit('975 church st Apt 4 San Francisco CA');
//================================================================
