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

Utilities = {};

Utilities.iconClassFromAccountName = function(name){
	switch(name){
		case "GTalk":
			return "googletalk"
		case "Google":
			return "google";
		case "Facebook":
			return "facebook";
		case "Exchange":
			return "eas";
		case "Microsoft Exchange":
			return "eas";
		case "AIM":
			return "aim";
		case "Palm Profile":
			return "local";
		case "SIM":
			return "sim";
	}
	return;
}

Utilities.populateIconClasses = function(accountList, propertyname){
	for(var i = 0; i < accountList.length; i++){
		accountList[i].iconClass = Utilities.iconClassFromAccountName(accountList[i][propertyname || "accountDisplayName"]);
	}
}
Utilities.parseBirthday = function(unformatted){
  var date = parseInt(unformatted, 10);
	if (date == "NaN") {
		Mojo.Log.info("The birthday %s is not in the localdate int format", unformatted)
  	return unformatted;
  }
	date = Utilities.localDateToDate(date);
	var format = (date.getYear() > 0) ? "long" : $L("MMMM dd");
	return Mojo.Format.formatDate(
		date, {
			date: format
		})
}

Utilities.localDateToDate = function(date){
	if(!date) return null;
	// easier to do this numerically
	var day = date % 100;
	date = Math.floor(date / 100);
	var month = date % 100;
	month = month -1;
	date = Math.floor(date / 100);
	var year = date;  // possibly 0. this means it's a yearless birthday and not a birthdate
	var returnDate = new Date(year, month, day);
	return returnDate;
}



Utilities.iconJSONToIconPath = function(jsonString){
	if (!jsonString)
		return "";
	
	var json = jsonString.evalJSON(true)
	var icon = json['32x32CONTACTS'] || json['32x32'];
	if(!icon){
		return "";
	}
	if(icon.charAt(0) == "/" || icon.indexOf("file:///") != -1){
		return icon;
	} else {
		return Mojo.Config.MOJO_FRAMEWORK_HOME + "/" + (icon || "images/accounts/envelope-closed-32x32.png");
	}
	
}

Utilities.dateToLocalDate = function(date){
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		if(month < 10) month = "0" + month;
		var day = date.getDate();
		if(day < 10) day = "0" + day;
		var dateString = year + "" + month + "" + day;
		return dateString;
}

Utilities.setCursorToEnd = function(textfield){
	    var range = txt.createTextRange();
        range.collapse(true);
        range.moveStart('character', textfield.value.length);
        range.moveEnd('character', textfield.value.length);
        range.select();
}

Utilities.removeFromArray = function(array, item){
	var index = array.indexOf(item);
	if(index == -1){
		return array;
	}
	array.splice(index, 1);
	return array;
}
