/**
 * 
 * All content on this website (including text, images, source
 * code and any other original works), unless otherwise noted,
 * is licensed under a Creative Commons License.
 * 
 * http://creativecommons.org/licenses/by-nc-sa/2.5/
 * 
 * Copyright (C) 2004-2010 Open-Xchange, Inc.
 * Mail: info@open-xchange.com 
 * 
 * @author Ben Pahne <ben.pahne@open-xchange.com>
 * @author Stefan Preuss <stefan.preuss@open-xchange.com>
 * 
 */

//menuhasRightandLoad([{ folder_id: 6, id: configGetKey("contact_id")}], "WRITE", ct_userdata_Init, function() { return false; });

function plugin_userData_init() {
	var old_user_data;

	// we have to check if parent node configuration/user already exists
	// @todo: This shouldn't be done by using private methods. VP has to add 
	// some function to check for existing nodes!
	var innerNodeFound = false;
	if (ox.Configuration && ox.Configuration.nodes) {
	    for (i in ox.Configuration.nodes) {
	      if (ox.Configuration.nodes[i].id == "configuration/user") {
	        innerNodeFound = true;
	      }
	    }
	}
	 
	// parent node doesn't exists, so we create it
	if (!innerNodeFound)
	   new ox.Configuration.InnerNode("configuration/user", _("User"));
	
	var node2 = new ox.Configuration.LeafNode("configuration/user/personal",_("Personal Data"));
	var page2 = new ox.Configuration.Page(node2, _("Change Personal Contact Information"))
	  
	var intro2 = new ox.UI.Text(_("Modify your account information."));
	page2.addWidget(intro2);
	
	var display_name = new ox.UI.Input(format(noI18n("%s *"),
	                                          _("Display name")));
	page2.addWidget(display_name, "display_name");
	  
	var first_name = new ox.UI.Input(_("First name"));
	page2.addWidget(first_name, "first_name");
	
	var second_name = new ox.UI.Input(_("Middle name"));
	page2.addWidget(second_name, "second_name");
	
	var last_name = new ox.UI.Input(_("Last name"));
	page2.addWidget(last_name, "last_name");
	
	// var suffix = new ox.UI.Input(_("Suffix"));
	// page2.addWidget(suffix , "suffix");
	
	
	// var title = new ox.UI.Input(_("Title"));
	//  page2.addWidget(title , "title");
	
	var street_home = new ox.UI.Input(_("Street (private)"));
	page2.addWidget(street_home , "street_home");
	
	var postal_code_home = new ox.UI.Input(_("ZIP (private)"));
	page2.addWidget(postal_code_home , "postal_code_home");
	
	var city_home = new ox.UI.Input(_("City (private)"));
	page2.addWidget(city_home , "city_home");
	
	var state_home = new ox.UI.Input(_("State (private)"));
	page2.addWidget(state_home , "state_home");
	
	// var country_home = new ox.UI.Input(_("Country (Home)"));
	// page2.addWidget(country_home , "country_home");
	
	var birthday = new ox.UI.Input(_("Date of birth"));
	page2.addWidget(birthday , {
		get:function(data, value) {
			var longTime = parseDateString(value);
			if (longTime == null) {
				data.birthday = null;
			} else {
				data.birthday = longTime.getTime();
			}
		},
		set: function(data){
			return data.birthday ? formatDate(new Date(data.birthday), "date") : "";
		}
	});
	
	//  var marital_status = new ox.UI.Input(_("Marital status"));
	//  page2.addWidget(marital_status , "marital_status");
	
	var company = new ox.UI.Input(_("Company"));
	page2.addWidget(company , "company");
	
	//  var department = new ox.UI.Input(_("Department"));
	//  page2.addWidget(department , "department");
	
	
	//  var position = new ox.UI.Input(_("Position"));
	//  page2.addWidget(position , "position");
	
	var street_business = new ox.UI.Input(_("Street (business)"));
	page2.addWidget(street_business , "street_business");
	
	var postal_code_business = new ox.UI.Input(_("ZIP (business)"));
	page2.addWidget(postal_code_business , "postal_code_business");
	
	var city_business = new ox.UI.Input(_("City (business)"));
	page2.addWidget(city_business , "city_business");
	
	var state_business = new ox.UI.Input(_("State (business)"));
	page2.addWidget(state_business , "state_business");
	
	// var country_business = new ox.UI.Input(_("Country (business)"));
	// page2.addWidget(country_business , "country_business");
	
	var telephone_business1 = new ox.UI.Input(_("Phone (business)"));
	page2.addWidget(telephone_business1 , "telephone_business1");
	
	var fax_business = new ox.UI.Input(_("Fax (business)"));
	page2.addWidget(fax_business , "fax_business");
	 
	// var telephone_company = new ox.UI.Input(_("Telephone (company)"));
	// page2.addWidget(telephone_company , "telephone_company");
	 
	var telephone_home1 = new ox.UI.Input(_("Phone (private)"));
	page2.addWidget(telephone_home1 , "telephone_home1");
	 
	// var fax_home = new ox.UI.Input(_("Fax (Home)"));
	// page2.addWidget(fax_home , "fax_home");
	
	var cellular_telephone1 = new ox.UI.Input(_("Mobile"));
	page2.addWidget(cellular_telephone1 , "cellular_telephone1");
	
	//var mandatory_fields = new ox.UI.Text(_("(Fields marked with a * must be filled)"));
	//page2.addWidget(mandatory_fields);
	 
	var last_mod;
	page2.load = function (cont) {
        ox.JSON.get(AjaxRoot + "/user?action=get&id=" +
            configGetKey("identifier") + "&session=" + session,
            function(reply) {
                old_user_data = clone(reply.data);
                last_mod = reply.timestamp;
                cont(reply.data);
    	    }); 
	} 
	  
	page2.save = function (data, cont){
        ox.JSON.put(AjaxRoot + "/user?action=update&session=" + session +
            "&id=" + configGetKey("identifier") + "&timestamp=" + last_mod,
            data, plugin_userData_print_message);
		function plugin_userData_print_message(reply) {
			ox.Configuration.info(_("Your contact information have been saved."));
			last_mod = reply.timestamp || last_mod;
			var cacheData = clone(data);
			cacheData.timestamp = last_mod;
			corewindow.OXContactMapping.editObjects([old_user_data],cacheData);
			cont();
		}
	    
	}
}

if (configGetKey("modules.contacts.module")) plugin_userData_init();