/**
 * 
 * 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 Suphi Basdemir <suphi.basdemir@open-xchange.com>
 * @author Stefan Preuss <stefan.preuss@open-xchange.com>
 * 
 */

var contactDetailId = 0;
var activefolderLocal = "";
var activemoduleLocal = corewindow.activemodule;
var session = null;
var lastTimestamp = 0;
var modulPopup = "new";
var contact_object = {};
var color_label = 0;
var myDistrListAdressBook = null;
var myDistrListExternEmails = null;
var distrListDataFromSearchResult = {};
var distruGrid = null;
var distruBigGrid = null;
var distibutionMenu = null;
var configGetKey = corewindow.configGetKey;
var configSetKey = corewindow.configSetKey;
var configContainsKey = corewindow.configContainsKey;
var clickedItem = "";
var clickedItemForSave = "";
var json = null;
var messageOnCancel = false;
var newContactId = 0;
var savePending = false;
var dataFromDb;
var triggerReady = jQuery.noop;

/********************************** before on unload *****************************************************************/
// modal dialog
function setModalBusy(state){
    if (state == true){
        ox.api.setModal(true);
        jQuery("#modal-dialog-decorator").addClass("busy");
    }else{
        ox.api.setModal(false);
        jQuery("#modal-dialog-decorator").removeClass("busy");
    }
}

function unloadMessage(){
	var messageCloseWindow = _("Do you really want to discard your changes and close the window?");
    return messageCloseWindow;
}

function setBeforeUnload(on){
    window.onbeforeunload = (on) ? unloadMessage : null;
    messageOnCancel = on;
}

//setBeforeUnload(true);
/********************************** Init *****************************************************************/

function closeWindow() {
	function cbyes() {
		window.onbeforeunload=null;
		window.onunload = null;
		ox.api.window.core.triggerEvent("OX_Window_Close",{handle: url.guid});
		ox.api.window.close(url.guid);
	}
	if(messageOnCancel) {
		var messageCloseWindow = _("Do you really want to discard your changes and close the window?");
		newConfirm(_("Close Window"),messageCloseWindow,AlertPopup.YESNO,null,null,cbyes,null);		
	} else
		cbyes();
}

var internalCache = null;
var config = null;
/**
 * init() onLoad
 */
register("Loaded", newDistributionListOnload);
function newDistributionListOnload() {
	internalCache = corewindow.internalCache;
	config = corewindow.config;
			
	distrFromAddressbookLive(); //init myDistrListAdressBook	
	myDistrListExternEmails = new DistrListExtEmail();	
}

function initDistributionListLoaded() {
	activemodule='contact_new';
	initContents('contact_new');
	initAll();
	// extend url object
	url = jQuery.extend((url || {}), ox.api.window.getData(url.guid));
	window.setTimeout(function() {
		var themeStyle = corewindow.oxThemeStyle || { name: "Default", path: "default" };
	    removeCSSFilesForNewWindow(themeStyle.path);
		loadCSSFileForNewWindow("themes/"+themeStyle.path+"/css/popup.css");
	},0);
}

register("Loaded",viewDistributionMenu);
function viewDistributionMenu() {		
	activefolderLocal = url.folder !== undefined ? url.folder : corewindow.activefolder;
	session = corewindow.session;
	activemodule="contact_new";	
	json = new JSON();	
	register("OX_SAVE_OBJECT", saveContact);
	register("OX_Cancel_Object", closeWindow);
    
    register("OX_Object_Distributionlist_Internal_Contact", 
    	function() {
    		participants.openAddParticipantsWindow();    		
    		triggerEvent('searchContacts');
			if($("distribution_extern_emails"))
				$("distribution_extern_emails").style.display = 'none';
    	}
    );
    
    register("OX_Object_Distributionlist_External_Mail", 
    	function() {
            myDistrListExternEmails.clearList(); 
    		myDistrListExternEmails.showDialog();
    		$('extern_displayname').focus();
    	}
    );
    
    register("OX_Object_Distributionlist_Delete", 
    	function() {
    		distruBigGrid.deleteIDs(distruBigGrid.selection.getSelected());
    	}
    );
    
	if(window.location.hash != "") {
		setModulPopup();	
	}	
	folderPath();
	loadingComplete();

	triggerEvent("OX_Switch_View","contact_new/distributionlist");
	triggerEvent("OX_menu_Change_Height",true);
};

registerView("contact_new/distributionlist",
    null,
	function () {
		setTimeout(function() {	$("display_name").focus();	},0);
	},
	null,
	null,
	function () {
		setTimeout(function() {	$("display_name").focus();	},0);
	});


/********************************** Distribution List *****************************************************************/

function folderPath() {
	if(activefolderLocal && activefolderLocal != "" && activemoduleLocal =="contacts"){
		clickedItemForSave = activefolderLocal;
	} else {
		clickedItemForSave = corewindow.configGetKey("folder.contacts");
	}
	
	ox.api.folder.get({
	    folder: clickedItemForSave,
	    success: function (data) {
		
    	    var nPermission = ox.api.folder.derive("permissions", data, 0).bit;
    		
    		if(nPermission < 2) {
    			triggerEvent("OX_New_Error", 4, _("You do not have write permission for this folder. The new contact will be saved in your personal contacts folder. "));
    			clickedItemForSave = configGetKey("folder.contacts");
    		}
    		
    		ox.api.folder.getPathString({
    		    folder: clickedItemForSave,
    		    success: cb_DLgetpath
    		});
    		
    		var folderViewSettingsNewContact = new Object();
    		var folderViewSettingsNewContactById = new Object();
    		//Disable Email... 
    		// folder.popup
    		folderViewSettingsNewContact["mail"] = 4;
    		folderViewSettingsNewContactById["default"] = 4;
    		folderViewSettingsNewContact["contacts"] = 8;
    		folderViewSettingsNewContact["calendar"] = 4;
    		folderViewSettingsNewContact["tasks"] = 4;
    		folderViewSettingsNewContact["infostore"] = 4;
    		if(!dlTreeLoaded){
    			ox.api.ui.selectFolder({
    		    	type: "contacts",
    		    	id: "select-folder-popup-contacts",
    		    	callback: folderDLAction,
    		    	node: $('folder_tree_info')
    		    });
    
    			dlTreeLoaded = true;
    		}
	    }
	});
}

function cb_DLgetpath(sPath) {
	document.getElementsByName('newDL_folder')[0].value = sPath;
}	

function folderDLAction(data) {
	toggleTreePath();
	clickedItemForSave = data.id;
	ox.api.folder.getPathString({
	    folder: clickedItemForSave,
	    success: cb_DLgetpath
	});
	
	/*
	 * folderType: z.B. shared...
	if(newContactFolderTree.getNodeByFolderId(clickedItem).data.sType != 1){
		$("newappointment_personal_tr").style.visibility = "hidden";
	} else {
		$("newappointment_personal_tr").style.visibility = "visible";
	}
	* */
}

var dlTreeLoaded = false;
var dlTreeVisible = false;

function showTree(){
	centerPopupWindow($("treeDialogNewDL"));
	$("treeDialogNewDL").style.display = "block";
	$("treeDialogNewDL").style.top = "130px";
	$("treeDialogNewDL").style.left = "170px";
	
}

function hideTree(){
	$("treeDialogNewDL").style.display = "none";	
}

function toggleTreePath(){
    // prevent a second popup in background during folder selection
    if ($("treeDialogNewParticitpantList").style.display === "none" ) {
    	if(!dlTreeVisible){
    		centerPopupWindow($("treeDialogNewDL"));
    		$("treeDialogNewDL").style.display = "block";
    		$("treeDialogNewDL").style.top = "130px";
    		$("treeDialogNewDL").style.left = "170px";
    		dlTreeVisible = true;
    		if(!dlTreeLoaded){
    			newDlFolderTree.load(corewindow.optFolderState.getState('folder_tree_info'));
    			dlTreeLoaded = true;
    			}
    	}
    	else{
    		$("treeDialogNewDL").style.display = "none";
    		dlTreeVisible = false;
    	}
    }
}

function callback_new(arg) {
	if(arg.error)
	{
		newServerError(arg);
	}
	else
	{
		finalFunction();
	}	
}

function callback_update(arg)
{
	if(arg.error)
	{
		newServerError(arg);
	}
	else
	{
		finalFunction();
	}
}


/*********** DL - Extern Email *********/

/**
 * Distribution List for external E-Mail adresses constructor
 */
function DistrListExtEmail() {	
	// init livegrid and new storage for external e-mail addresses
	this.selection = new Selection();
	this.storage = new Storage(0, [], 0, 0, 0, 0, function(external) { return external.id; });
	this.grid = new LiveGrid([
        { 
            text: "Name",
            i18n: true,
            clear: LiveGrid.makeClear(""),
            set: function(div, external) {
                if (div.firstChild)
                    div.firstChild.data = external.display_name;
                else
                    div.appendChild(document.createTextNode(external.display_name));
            }
        },
        {
            text: "E-Mail",
            i18n: true,
            clear: LiveGrid.makeClear(""),
            set: function(div, external) {
                if (div.firstChild)
                    div.firstChild.data = external.email;
                else
                    div.appendChild(document.createTextNode(external.email));
            }
        }
    ], this.selection);
    this.grid.emptylivegridtext = "";
    
    // add livegrid header and content to dom node
    $("dist.ext.mail.grid.header").appendChild(this.grid.getHeader());
    this.grid.getTable($("dist.ext.mail.grid"));
}

DistrListExtEmail.prototype = {
	
	/**
	 * Displays the external E-Mail Dialog
	 */
	showDialog: function() {        
        this.grid.enable(this.storage);
	    $("floatingdivParticipants").style.display = 'none';
	    centerPopupWindow($("distribution_extern_emails"));
        $("distribution_extern_emails").style.display = 'block';
	},	
	
	/**
	 * Closes the external E-Mail Dialog
	 */
	closeDialog: function() {
		$("distribution_extern_emails").style.display = 'none';
		this.grid.disable();
	},
	
	/**
	 * Resets the list of external e-mail addresses
	 */
	clearList: function(){
		$('extern_displayname').value = "";
        $('extern_email').value = "";
		this.storage.remove(0, this.storage.ids.length);
	},
	
	/**
	 * Adds a new external e-mail address to the list
	 * @param displayName The display name
	 * @param email The e-mail address (required)
	 */
	addToList: function(display_name, email) {
		display_name = trimStr(display_name || "");
		email = trimStr(email);
		this.storage.append([{ 'id': this.storage.ids.length, 'display_name': display_name, 'email': email }]);		
	}
}

/**
 * Closes the external E-Mail Dialog and clears the temp list.
 */
function closeExternalEmailDialog() {
	myDistrListExternEmails.clearList(); 
	myDistrListExternEmails.closeDialog();
}

/**
 * Gets the display name and email address from the page, validates and 
 * adds it to the list. Afterwards the fields on the page gets cleared
 * or fucused. 
 */
function addExternalEmailToList() {
	var disp = trimStr($('extern_displayname').value || ""); 
	var email = trimStr($('extern_email').value || "");
	var goahead = function(disp, email) {
        $("extern_email").className = removeClass($("extern_email").className, "border-color-error");
        myDistrListExternEmails.addToList(disp, email);
        $('extern_displayname').value = "";
        $('extern_email').value = "";
	}
	if (email.length == 0) {
        if (!$("extern_email").className.match(/border-color-error/gi)) 
            $("extern_email").className += " border-color-error";
        newAlert(_("Error"), _("Please enter a valid E-Mail address!"), function() {
        	   $("extern_email").focus();
            });
    } else if (!corewindow.validateEmail(email)) {
    	if (!$("extern_email").className.match(/border-color-error/gi))  	
            $("extern_email").className += " border-color-error";
        newConfirm(_("Error"),_("The E-Mail address you have entered seems not to be valid. Would you like to add it anyway?"),AlertPopup.YESNO,null,null,
            function() { goahead(disp, email); }, function () { $("extern_email").focus(); });
    } else {        
        goahead(disp, email);
        setFocus($('extern_displayname'));
    }
}

/**
 * Append the external added email addresses to the big grid.
 */
function appendExternalToBigGrid() {
	
	var data = myDistrListExternEmails.storage;
	// loop items
	data.newIterate(data.ids, null, function(key, item) {
		var id = item.id || -distruBigGrid.storage.ids.length || -1;
		// add to storage (live grid)
		var cObj = { 'id': id, 'display_name': item.display_name || "",
				'email': item.email || ""
		};
		// if not set, use default 1 - required to set recurrence
		if (item.mail_field == undefined) {
			item.mail_field = 1;
		}
		cObj.recurrence = item.mail_field - 1;
		distruBigGrid.storage.append([cObj]);
	});
}

// to move
var participants = null;

/*********** DL - From Adressbook *********/
function distrFromAddressbookLive() {
	// selection
	var selection = new Selection();
	// storage
	function serialize(id) { return id.id + "." + id.email; }
    var storage = new Storage(0, [], 0, 0, 0, serialize,
        function(data) { return { id: data.id, email: data.email }; });
	// live grid
	distruBigGrid = new LiveGrid([
        { 
            text: "Name",
            i18n: true,
            clear: LiveGrid.makeClear(""),
            set: function(div, external) {
                if (div.firstChild)
                    div.firstChild.data = external.display_name;
                else
                    div.appendChild(document.createTextNode(external.display_name));
            }
        },
        {
            text: "E-Mail",
            i18n: true,
            clear: LiveGrid.makeClear(""),
            set: function(div, external) {
                if (div.firstChild)
                    div.firstChild.data = external.email;
                else
                    div.appendChild(document.createTextNode(external.email));
            }
        }
    ], selection);
			
	distruBigGrid.emptylivegridtext = "";	
    // add livegrid header and content to dom node
    $("newDistrListLiveHeader").appendChild(distruBigGrid.getHeader());
    distruBigGrid.getTable($("newDistrListLive"));    
    distruBigGrid.enable(storage);
    
    // what to do when select a appointment
	distruBigGrid.events.register("Selected", function(ids) {		
		var count = distruBigGrid.selection.getSelected().length;		
		triggerEvent("SubSelected", count);
	});
	
	distruBigGrid.events.register("Delete", function(ids) {
		triggerEvent("OX_Object_Distributionlist_Delete");
	});
    
    // ------------------------------------------------------------------------
	folderOwner = configGetKey("identifier");
	
	// use ParticipantsSmall (without its own LiveGrid)
	participants = new ParticipantsSmall(
		null,
		// callback function to process contacts provided by the popup window
		function(items) {
			// loop items
			for(i = 0; i < items.length; i++) {
				// shortcut
				var item = items[i];
				// type
				switch (item.type) {
					// user
					case 1:
					// contact
					case 5:
						var myObject = { 
								'id': item.contactID,
								'display_name': item.display_name || "",
								'email': item.mail,
								'recurrence': item.recurrence
							};
						// duplicate?
                        if (!(serialize(myObject) in storage.indices)) {
							// add to list
							storage.append([myObject]);
						}
						break;
				}
			}
		},
		// showUser, showGroup, showResource, showFolder, title, id, singleFolderSearch
		true, false, false, true, "Select Contacts", folderOwner); /*i18n*/
	participants.allEmailAddresses=true;
	
	// bind close button to "Window Close" events for intercom
    window.onunload = function() {
        ox.api.window.core.triggerEvent("OX_Window_Close", {
            handle: url.guid
        });
    };
    
    triggerReady = function () {
        
        // init new toolbar
        ox.gui.initToolBarNewWindowDistributionlist();
        
        // special object for intercom lib
        var intercomWindowData = {
            handle: url.guid,
            type: "distributionlist",
            id: (window.url.id !== -1) ? window.url.id : null,
            folder_id: (window.url.folder !== -1) ? window.url.folder : null,
            title: window.document.title
        };
        
        // triggerEvent "Window Ready" for intercom with special intercom Object and whole window object
        ox.api.window.core.triggerEvent("OX_Window_Ready", intercomWindowData, window);
    };
	
	initParticipantContent();
}

function distributionlistContact() {
		var searchString = $('distr_search').value;
		myDistrListAdressBook.searchstring = searchString;
					
		myDistrListAdressBook.getResult();
		distruGrid.events.register("Activated", function(ids){		
			});
		
		distruGrid.events.register("Selected", function(ids){
				myDistrListAdressBook.numberOfSelection = ids;	
			});
}

function addDistrToList() {
	if (myDistrListAdressBook) {
		myDistrListAdressBook.addToList('bySelection');
	}	
}

function addAllDistrToList() {
	if (myDistrListAdressBook) {
		myDistrListAdressBook.addToList('all');
	}	
}


/********************************** Save Contact *****************************************************************/
/**
 * triggerEvent -> save Button
 * switch: new, edit, dublicate, newmember
 */
function saveContact()
{
	if(savePending) return;
	else savePending = true;
	
	if(modulPopup == "new" || modulPopup == "duplicate")
	{
		writeNewContact();
	} else if(modulPopup == "edit")
	{
        
        /********************************** update Contact ********************************************************/	
		
		contact_object.last_name = $('display_name').value;
		contact_object.display_name = $('display_name').value;
		contact_object.folder_id = clickedItemForSave;
		
		var cb_put = function() {
			json.put(AjaxRoot + "/contacts?action=update&session=" + session + 
				"&id=" + contactDetailId + "&folder=" + activefolderLocal + 
				"&timestamp="+lastTimestamp,
				contact_object, 
				null, 
				function(updatedFields)
				{	
					if(updatedFields) {
						if(updatedFields.error) {
							//error
						} else {
							finalFunction();
						}
					}
				}
			);
		}
		
		getDistrForSave(cb_put);

		
	}
}

/*
 * build contact_object.distribution_list for save 
 * */

function getDistrForSave(callback) {
	
	var data = distruBigGrid.storage;
	var tmpDistObj = new Array();
	// loop items
	data.newIterate(data.ids, null, function(key, obj) {
		// collect items
		tmpDistObj.push({
			// positive id=user/contact; negative id=temp-id for external users
			"id": obj.id < 0 ? "" : obj.id,
			"display_name": obj.display_name,
			"mail": obj.email, 
			"mail_field" : obj.id < 0 ? 0 : Number(obj.recurrence) + 1
		});		
	});
	// done...
	contact_object.distribution_list = tmpDistObj;
	contact_object.color_label = color_label || 0;
	
	// callback
	if(callback) callback();
}


/********************************** set var Module *****************************************************************/
/**
 * Init Popup for setting the Modul
 * @param {String} parameters from the url 
 */
function setModulPopup(params) {
	
	/////////
	// Modul Edit or Duplicate
	if (url['modul'] && (url['modul'] == "edit" || url['modul'] == "duplicate")) {
			modulPopup = url['modul'];
			contactDetailId = url['id'];
			activefolder = url['folder'];
			session = url['session'];
			currentView = url['currentView'];
			fillFieldById();	
	} else {
		fillFields(url);		
		// disable loading screen
        $("loading_data_bg").style.display="none";
        triggerReady();
	}
	
}

/********************************** get ContactById *****************************************************************/

/**
 * fill the Fields which comes from DB 
 * id is a Param in the url, see {@link setModulPopup}
 */
function fillFieldById() {
	
	// load distribution list (ajax call)
	new JSON().get(
		AjaxRoot + "/contacts?action=get&session=" + session +
		"&id=" + contactDetailId + "&folder=" + activefolderLocal, null,
		// callback to process the JSON response
		function(response) {
			
			// remember response (used by dialog closer)
			dataFromDb = response;
			
			// shortcut			
			var d = response.data;	
			fillFields(d);
			
			// remember timestamp
			lastTimestamp = response.timestamp;

			// disable loading screen
		    $("loading_data_bg").style.display="none";
		    
		    triggerReady();
		}
	);
}

function fillFields(data) {
    
	// map title to required fields
	if (data.title && !data.display_name) {
		data.display_name = data.title;
	}
	
	// set "display_name" (text field) -> name of the distribution list
	var title = data["display_name"] || "";
	$('display_name').value = title;
	
	// set document title too
	document.title = _("Distribution List") + (title ? ": " + title : "");
	
	// set(hidden field)
	$('last_name').value = data['display_name'] || "";
	data.file_as = data.display_name;
	
	// set color label
	if (data['color_label']) color_label = data['color_label'];

	if (data['distribution_list']) {
		// shortcut
		var dl = data['distribution_list'];				
		// loop entries
		for(i = 0; i < dl.length; i++ ) {
			var id = dl[i].id || -distruBigGrid.storage.ids.length || -1;
			// add to storage (live grid)
			var cObj = { 'id': id, 'display_name': dl[i].display_name || "",
					'email': dl[i].mail || ""
			};
			// if not set, use default 1 - required to set recurrence
			if (dl[i].mail_field == undefined) {
				dl[i].mail_field = 1;
			}
			cObj.recurrence = dl[i].mail_field - 1;
			distruBigGrid.storage.append([cObj]);
		}				
	}
}


/********************************** Write new Contact *****************************************************************/
/**
 * Write all filled fields in DB
 * json.put -> contact_object
 */
function writeNewContact() {
	if($("display_name").value == "") {
		triggerEvent("OX_New_Error", 4, _("Mandatory Field(s) missing!"));
		$("display_name").className += " borderDefault border-color-error";
		$("display_name").focus();
		savePending = false;	
	} else {		
		contact_object.last_name = $('display_name').value;
		contact_object.display_name = $('display_name').value;
		contact_object.folder_id = clickedItemForSave;
				
		var tmpString = new Array;
		contact_object.distribution_list = tmpString; //mark as Distribution List
		getDistrForSave();
		setModalBusy(true);
		json.put(AjaxRoot + "/contacts?action=new" + 
				"&session=" + corewindow.session + 
				"&folder=" + clickedItemForSave , 
				contact_object, null, function(idObj){
				if(idObj) {	
					newContactId = idObj.data.id;
					finalFunction();
				}
			},setModalBusy);
	}
}


function finalFunction() {
	
	if(modulPopup == "new" || modulPopup == "duplicate") {
		// new/duplicate
		setBeforeUnload(false);
		corewindow.OXContactMapping.createObject(contact_object);
		corewindow.closeContactPopUp(window, clickedItemForSave);
	} else if(modulPopup = "edit") {
		// 	edit
		setBeforeUnload(false);
        corewindow.OXCache.newRequest(null, "contacts", { columns: null,
            objects: [corewindow.OXContactMapping.createKeyFromObject({
                module: "contacts", folder_id: clickedItemForSave,
                id: contactDetailId
            })] }, null, emptyFunction, true);
        if (activefolderLocal != clickedItemForSave) {
            corewindow.OXContactMapping.deleteObjectsInternal(
                [corewindow.OXContactMapping.createKeyFromObject({
                    module: "contacts", folder_id: activefolderLocal,
                    id: contactDetailId
                })]);
		}
		corewindow.closeContactPopUp(window, clickedItemForSave);
    }
}