/* Copyright 2009 Palm, Inc.  All rights reserved. */

var PhoneNumber = Class.create({
    initialize: function(number){
        if (number) {
            this.value = number;
            this.displayValue = PhoneNumber.format(number);
        }
        this.size = "unassigned";
    },
    
    isPhoneNumber: true,
    getHashKey: function(){
        return PhoneNumber.getHashKey(this);
    },
    
    getRating: function(contact){
        var rating = PhoneNumber.strip(this.value).length;
        return rating;
    }
    
});

PhoneNumber.strip = function(numberStr){
    if (typeof numberStr === 'number') {
        numberStr = "" + numberStr;
    }
    return numberStr.replace(/[^A-Za-z0-9+*#]+/g, "");
};

// US phone number formatter
PhoneNumber.format = function(numberStr){
    var prev = PhoneNumber.strip(numberStr);
    if (prev.length < 7) {
        return numberStr;
    }
    return Mojo.Format.formatPhoneNumber(prev);
};

PhoneNumber.getHashKey = function(phoneNumber){
    var stripped = PhoneNumber.strip(phoneNumber.value);
    if (stripped.length > 7) {
        stripped = stripped.slice(stripped.length - 7);
    }
    return stripped;
};

PhoneNumber.HOME = 0;
PhoneNumber.WORK = 1;
PhoneNumber.OTHER = 2;
PhoneNumber.MOBILE = 3;
PhoneNumber.PAGER = 4;
PhoneNumber.PERSONAL_FAX = 5;
PhoneNumber.FAX = 6;

PhoneNumber.popupLabels = [{
    value: 3,
    label: $L("Mobile"),
    command: 3
}, {
    value: 0,
    label: $L("Home"),
    command: 0
}, {
    value: 1,
    label: $L("Work"),
    command: 1
}, {
    value: 6,
    label: $L("Fax"),
    command: 6
}, {
    value: 2,
    label: $L("Other"),
    command: 2
}];

PhoneNumber.labels = {
    3: $L('Mobile'),
    0: $L('Home'),
    1: $L('Work'),
    7: $L('Main'),
    6: $L('Fax'),
    5: $L('Fax'),
    4: $L('Pager'),
    2: $L('Other')
};
