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

var Contact = Class.create({ 
	
	nameCharacterBlacklist: "[]!@#$^&*()+-%1234567890=*/.",
	
	initialize: function() {
	},
	
	parseName: function(name) {
 		this.prefix = null;
		this.suffix = null
	    this.firstName = null;
	    this.middleName = null;
	    this.lastName = null;
	    if (!name) return; //There's no way to delete someones name so i took this out so we could
	
	    var result = name.strip().split(' ');
			var actualParts = [];
			var blacklist = this.nameCharacterBlacklist;
	
			result.each(function(part){
				if(blacklist.indexOf(part.charAt(0)) == -1){
					actualParts.push(part);
				} else {
					if(actualParts[actualParts.length -1]){
						actualParts[actualParts.length -1] = actualParts[actualParts.length -1] + " " + part;
					} else {
						actualParts.push(part);
					}
				}
			})
			result = actualParts;
	
			var that = this;
			if(result.length > 1){
				Contact.PREFIXES.each(function(prefix){
					var lowered = result[0].toLowerCase();
					if(lowered == prefix || lowered == prefix + "."){
						that.prefix = result.shift();
						throw $break;
					}
				});
				Contact.SUFFIXES.each(function(suffix){
					var lastItem = result.last();
					lastItem = lastItem.strip().gsub(/\./, "").toLowerCase();
					if(suffix == lastItem){
						that.suffix = result.pop();
						throw $break;
					}
				})
			}
	    if (result.length == 1) {
	      this.firstName = result[0];
	    } else if (result.length == 2) {
	      this.firstName = result[0];
	      this.lastName = result[1];
	    } else if (result.length >= 3) {
	      this.firstName = result[0];
	      this.lastName = result[result.length -1];
	
	      this.middleName = "";
	      for (var i = 1; i < result.length -1; i++) {
	        this.middleName += result[i] + " ";
	      }
	      this.middleName.strip();
	    }
  	},
	
	getFirstName: function () {
		return this.firstName;
	},
	
	getMiddleName: function() {
		return this.middleName;
	},
	
	getLastName: function() {
		return this.lastName;
	},
	
	getSuffix: function() {
		return this.suffix;
	}
	
});

Contact.PREFIXES = ["mr", "miss", "mrs", "ms", "dr"];
Contact.SUFFIXES = ["jr", "sr", "i", "ii", "iii", "iv", "v", "vi", "phd", "md"];