App.Models.Contact = Backbone.Model.extend({

	defaults : {
		"UserEmail":"",
		"UserEmail2":"",
		"UserEmail3":"",
		"UserEmail4":"",
		"UserEmail5":"",
		"UserFirstName":"",
		"UserMiddleName":"",
		"UserLastName":"",
		"UserTitle":"",
		"UserGender":"",
		"UserDOB":"",
		"UserHomeAddress":"",
		"UserHomeCity":"",
		"UserHomeState":"",
		"UserHomeZip":"",
		"UserHomeCountry":"",
		"UserHomePhone":"",
		"UserHomeMobile":"",
		"UserHomeFax":"",
		"UserURL":"",
		"UserWorkCompany":"",
		"UserWorkTitle":"",
		"UserWorkDept":"",
		"UserWorkOffice":"",
		"UserWorkAddress":"",
		"UserWorkCity":"",
		"UserWorkState":"",
		"UserWorkZip":"",
		"UserWorkCountry":"",
		"UserWorkPhone":"",
		"UserWorkMobile":"",
		"UserWorkFax":"",
		"UserType":"",
		"UserInfo":"",
		"Account":"",
		"DateAdded":"",
		"DateModified":"",
		"EntryID":"",
		"UserPhoto":"",
		"UserFileAs":"",
		"Global":0,
		"Shared":0,
		"Favourite":0,
		"UsageCount":0,
		"VCardData":"",
		"CardDAVUrl":"",
		"SIP":"",
		"UserSuffix":"",
		"UserRelationship":"",
		"UserSignificantOther":"",
		"UserAnniversary":"",
		"UserFacebookProfile":"",
		"UserTwitterProfile":"",
		"UserLinkedInProfile":"",
		"UserIM":"",
		"UserNote":"",
		"UserHomeAddress2":"",
		"UserPrimaryEmail":'UserEmail',
		"UserPrimaryPhone":'UserHomePhone',
		"UserPrimaryAddress":'UserHomeAddress'
	},

	validate: function(attrs,options){
		if( !(attrs.UserFirstName||attrs.UserLastName) ){
			return "A valid contact must have a first or last name";
		}
	},
	
	initialize: function(attrs,options){
		this.set('Account',App.primaryAccount );
	}
});
App.Models.Contact.prototype.hasWorkAddressProperties = function(){
	return this.get('UserWorkAddress') &&
		   this.get('UserWorkCity')    &&
		   this.get('UserWorkState')   &&
		   this.get('UserWorkZip')     &&
		   this.get('UserWorkCountry');
}
App.Models.Contact.prototype.getPrimaryEmail = function(){ 
	var key = this.get('UserPrimaryEmail');
	return this.get(key); 
}
App.Models.Contact.prototype.isPrimaryEmail = function(key){
	return (this.get('UserPrimaryEmail') === key);
}
App.Models.Contact.prototype.getPrimaryPhone = function(){ 
	var key = this.get('UserPrimaryPhone');
	return this.get(key);
}
App.Models.Contact.prototype.getPrimaryPhoneFormatted = function(){
	return this.getPhoneFormatted( this.get('UserPrimaryPhone') );
}
App.Models.Contact.prototype.getPhoneFormatted = function(type){
	var ph = this.get(type);
	if(/^\d{10}$/.test(ph)){
		return "("+ph.substr(0,3)+") "+ph.substr(3,3)+"-"+ph.substr(6,4);
	} else {
		return ph;
	}
}
App.Models.Contact.prototype.isPrimaryPhone = function(key){
	return (this.get('UserPrimaryPhone')===key);
}
App.Models.Contact.prototype.getPrimaryAddress = function(){ 
	var key = this.get('UserPrimaryAddress');
	return this.get(key);
}
App.Models.Contact.prototype.isPrimaryAddress = function(key){
	return (this.get('UserPrimaryAddress')===key);
}
App.Models.Contact.prototype.getFullName = function(){
	var fname, lname;
	fname = this.get('UserFirstName');
	lname = this.get('UserLastName');

	if( fname && lname ){
		return (fname + " " + lname);
	}

	if( !fname && lname ){
		return lname;
	}

	if(fname && !lname ){
		return fname;
	}

	return "";
}

App.Models.Contact.prototype.getAllEmail = function(){
	var types = ['UserEmail','UserEmail2','UserEmail3','UserEmail4','UserEmail5'],
		all   = [];

	for(var i = 0; i < types.length; i++){
		all[i] = {
			key: types[i],
			text: ('Email ' + (i+1)),
			email: this.get(types[i]),
			primary:this.isPrimaryEmail(types[i])
		}
	}
	return all;
}

App.Models.Contact.prototype.getAllEmailNamed = function(){
	var email = this.getAllEmail(),
		name  = this.getFullName();

	_.each(email, function(em,i){
		if(em.email){
			email[i] = name + ' ' + em.email ;
		} else {
			email[i] = false;
		}
	});

	return _.compact(email);
}

App.Models.Contact.prototype.getAllPhoneNumbers = function(){
	var types = ['UserHomePhone','UserHomeMobile','UserWorkPhone'],
	    all   = [];

	for(var i = 0; i < types.length; i++){
		var lc = types[i].toLowerCase(),
			key = lc+'Phone';

		all[i] = { 
			key: key,
			text: types[i],
			phone: this.get(key),
			primary: this.isPrimaryPhone(key)
		}
	}

	return all;
}

App.Models.Contact.prototype.getDobDate = function(){
	var dob = this.get('UserDOB');
	if( dob && dob.length && dob.indexOf("0000-00-00") == -1){
		return new Date(dob);
	}
}

App.Models.Contact.prototype.getDobDateStr = function(){
	var date,y,m,d,fm,fd;

	date = this.getDobDate();
	//console.log(typeof date);
	if( typeof date !=='undefined' ){
		y = date.getFullYear();
		m = date.getMonth();
		d = date.getDate();
	}

	if( !(isNaN(y) && isNaN(m) && isNaN(d) ) ){
		fm   = (m<9) ? "0"+(m+1) : (m+1);
		fd   = (d<10) ? "0"+d : d;
		return y +"-"+ fm +"-"+ fd;
	} 
	
	return "";
}

App.Models.Contact.prototype.getFormattedDob = function(){
	var date,y,m,d;

	date = this.getDobDate();
	console.log( typeof date );
	if(typeof date !== 'undefined'){
		y    = date.getFullYear();
		m    = App.Calendar.month[ date.getMonth() ];
		d    = date.getDate();

		if( ! (isNaN(y) && isNaN(m) && isNaN(d)) ){
			return m+" "+d+", "+y;
		}
	}
		return "";
}

App.Models.Contact.prototype.getAllAddresses = function(){
	var types = ['Home','Work'],
	    all   = [];

	for(var i = 0; i < types.length; i++){
		var lc = types[i].toLowerCase(),
			key = lc+'Address';

		all[i] = { 
			key: key,
			text: types[i],
			address: this.get(key),
			primary: this.isPrimaryAddress(key)
		}
	}
	return all;
}
