/*
 * Copyright 2005 - 2009  Zarafa B.V.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3, 
 * as published by the Free Software Foundation with the following additional 
 * term according to sec. 7:
 *  
 * According to sec. 7 of the GNU Affero General Public License, version
 * 3, the terms of the AGPL are supplemented with the following terms:
 * 
 * "Zarafa" is a registered trademark of Zarafa B.V. The licensing of
 * the Program under the AGPL does not imply a trademark license.
 * Therefore any rights, title and interest in our trademarks remain
 * entirely with us.
 * 
 * However, if you propagate an unmodified version of the Program you are
 * allowed to use the term "Zarafa" to indicate that you distribute the
 * Program. Furthermore you may use our trademarks where it is necessary
 * to indicate the intended purpose of a product or service provided you
 * use it in accordance with honest practices in industrial or commercial
 * matters.  If you want to propagate modified versions of the Program
 * under the name "Zarafa" or "Zarafa Server", you may only do so if you
 * have a written permission by Zarafa B.V. (to acquire a permission
 * please contact Zarafa at trademark@zarafa.com).
 * 
 * The interactive user interface of the software displays an attribution
 * notice containing the term "Zarafa" and/or the logo of Zarafa.
 * Interactive user interfaces of unmodified and modified versions must
 * display Appropriate Legal Notices according to sec. 5 of the GNU
 * Affero General Public License, version 3, when you propagate
 * unmodified or modified versions of the Program. In accordance with
 * sec. 7 b) of the GNU Affero General Public License, version 3, these
 * Appropriate Legal Notices must retain the logo of Zarafa or display
 * the words "Initial Development by Zarafa" if the display of the logo
 * is not reasonably feasible for technical reasons."
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

function initContact()
{
	// Fileas
	setFileAs();
	
	// Email Address 1
	dhtml.getElementById("email_address").value = dhtml.getElementById("email_address_1").value;
	dhtml.getElementById("email_address_display_name").value = dhtml.getElementById("email_address_display_name_1").value;

	// Address
	var mailing_address = dhtml.getElementById("mailing_address").value;
	if(parseInt(mailing_address) > 0) {
		dhtml.setValue(dhtml.getElementById("checkbox_mailing_address"), true);
	}
	
	// Default: Business Address
	var address = dhtml.getElementById("business_address").value;
	switch(mailing_address)
	{
		// Home
		case "1":
			address = dhtml.getElementById("home_address").value;
			break;
		// Other
		case "3":
			address = dhtml.getElementById("other_address").value;
			break;
	}
	
	var address_button_value = _("Business") + ":";
	switch(mailing_address)
	{
		case "1":
			address_button_value = _("Home") + ":";
			break;
		case "3":
			address_button_value = _("Other") + ":";
			break;
	}
	dhtml.getElementById("address_button").value = address_button_value;

	dhtml.getElementById("selected_address").value = mailing_address;
	dhtml.getElementById("address").value = address;

	// set values for telephone input boxes
	dhtml.getElementById("telephone_number_1").value = dhtml.getElementById("office_telephone_number").value;
	dhtml.getElementById("telephone_number_2").value = dhtml.getElementById("home_telephone_number").value;
	dhtml.getElementById("telephone_number_3").value = dhtml.getElementById("business_fax_number").value;
	dhtml.getElementById("telephone_number_4").value = dhtml.getElementById("cellular_telephone_number").value;

	// Birthday
	dhtml.setDate(dhtml.getElementById("birthday"));
	
	// Wedding Anniversary
	dhtml.setDate(dhtml.getElementById("wedding_anniversary"));
	
	// Private
	if(dhtml.getElementById("sensitivity").value == "2") {
		dhtml.setValue(dhtml.getElementById("checkbox_private"), true);
	}
}

function submitContact()
{
	// before saving contents check for opened detailed dialogs
	for(var dialogname in webclient.dialogs) {
		if(dialogname.match("contactitem_detailfullnamedialog") != null && !window.BROWSER_IE) {
			if(typeof webclient.dialogs[dialogname].window == "undefined" || webclient.dialogs[dialogname].window.closed == false) {
				webclient.dialogs[dialogname].window.focus();
				return false;
			}
		} else if(dialogname.match("contactitem_detailphonenumberdialog") != null && !window.BROWSER_IE) {
			if(typeof webclient.dialogs[dialogname].window == "undefined" || webclient.dialogs[dialogname].window.closed == false) {
				webclient.dialogs[dialogname].window.focus();
				return false;
			}
		} else if(dialogname.match("contactitem_detailaddressdialog") != null && !window.BROWSER_IE) {
			if(typeof webclient.dialogs[dialogname].window == "undefined" || webclient.dialogs[dialogname].window.closed == false) {
				webclient.dialogs[dialogname].window.focus();
				return false;
			}
		}
	}

	// File As
	dhtml.getElementById("fileas").value = dhtml.getValue(dhtml.getElementById("select_fileas"));

	// Current Email Address
	var selected_email_address = dhtml.getElementById("selected_email_address");
	var email_address = dhtml.getElementById("email_address");
	var email_address_display_name = dhtml.getElementById("email_address_display_name");

	dhtml.getElementById("email_address_" + selected_email_address.value).value = email_address.value;
	dhtml.getElementById("email_address_display_name_" + selected_email_address.value).value = email_address_display_name.value;
	
	// Current Address
	var selected_address = dhtml.getElementById("selected_address");
	var address = dhtml.getElementById("address");
	
	var address_element_id = "business";
	switch(selected_address.value)
	{
		case "1":
			address_element_id = "home";
			break;
		case "3":
			address_element_id = "other";
			break;
	}
	
	dhtml.getElementById(address_element_id + "_address").value = address.value;

	// update phone number input fields
	updatePhoneNumberFields();

	// Mailing Address
	var mailing_address = dhtml.getElementById("mailing_address");
	var checkbox_mailing_address = dhtml.getElementById("checkbox_mailing_address");
	if(checkbox_mailing_address.checked) {
		mailing_address.value = selected_address.value;
	}
	
	// Birthday
	if(dhtml.getElementById("text_birthday").value != ""){
		dhtml.getElementById("birthday").value = Date.parseDate(dhtml.getElementById("text_birthday").value, _("%d-%m-%Y")).getTime()/1000;
	}else{
		dhtml.getElementById("birthday").value = "";
	}
	
	// Special Date
	if(dhtml.getElementById("text_wedding_anniversary").value != ""){
		dhtml.getElementById("wedding_anniversary").value = Date.parseDate(dhtml.getElementById("text_wedding_anniversary").value, _("%d-%m-%Y")).getTime()/1000;
	}else{
		dhtml.getElementById("wedding_anniversary").value = "";
	}

	// Private
	var checkbox_private = dhtml.getElementById("checkbox_private");
	if(checkbox_private.checked) {
		dhtml.getElementById("sensitivity").value = "2";
		dhtml.getElementById("private").value = "1";
	} else {
		dhtml.getElementById("sensitivity").value = "0";
		dhtml.getElementById("private").value = "-1";
	}
	
	// Contacts
	dhtml.getElementById("contacts_string").value = dhtml.getElementById("contacts").value;
	
	submit_contact();
}

function setFileAs()
{
	var fileas = dhtml.getElementById("fileas").value;
	var select_fileas = dhtml.getElementById("select_fileas");
	var display_name = dhtml.getElementById("display_name");
	
	// Save selected index temperory, to set that perticular index after change in values of fileas.
	var tempIndex = select_fileas.options.selectedIndex;

	// Remove all options in select list
	if(select_fileas.options.length > 0) {
		for(var i = select_fileas.options.length - 1; i >= 0; i--)
		{
			select_fileas.remove(i);
		}
	}

	// Remove prefix and suffix of name.
	var name = display_name.value.replace(dhtml.getElementById("display_name_prefix").value, "").replace(dhtml.getElementById("generation").value, "").replace(",", "").replace(" ", NBSP);
	
	// Split display name ([0] => SURNAME, [1] => GIVEN NAME, [2] => MIDDLE NAME)
	var names = name.split(NBSP);

	// Remove white spaces in array
	var tmpnames = names;
	names = new Array();
	for(var i = 0; i < tmpnames.length; i++)
	{
		if(tmpnames[i].length > 0 && tmpnames[i].trim() != "") {
			names.push(tmpnames[i].trim());
		}
	}

	if(names.length > 0) {
		// Build up two different options (SURNAME, GIVEN NAME MIDDLE NAME and GIVENNAME MIDDLE NAME SURNAME)
		var	surname = names.join(NBSP);
		var givenname = "";
		if(names.length > 1) {
			givenname = names[names.length-1] + "," + NBSP;
			names.splice(names.length-1,1);
			givenname += names.join(NBSP);
		}
		
		// Add the two options to select list
		if(surname.trim() != "")
			select_fileas.options[select_fileas.options.length] = new Option(surname, surname);
		if(givenname.trim() != "")
			select_fileas.options[select_fileas.options.length] = new Option(givenname, givenname);
	} else {
		var	surname = false;
		var givenname = false;
	}

	// Check if company name is set
	var company_name = dhtml.getElementById("company_name");
	if(company_name.value != "") {
		// Add company name to select list
		select_fileas.options[select_fileas.options.length] = new Option(company_name.value, company_name.value);

		// Add two options which contains the company name and the full name
		if(surname)
			select_fileas.options[select_fileas.options.length] = new Option(company_name.value + NBSP + "(" + surname + ")", company_name.value + NBSP + "(" + surname + ")");
		if(givenname)
			select_fileas.options[select_fileas.options.length] = new Option(company_name.value + NBSP + "(" + givenname + ")", company_name.value + NBSP + "(" + givenname + ")");
		if(surname)
			select_fileas.options[select_fileas.options.length] = new Option(surname + NBSP + "(" + company_name.value + ")", surname + NBSP + "(" + company_name.value + ")");
		if(givenname)
			select_fileas.options[select_fileas.options.length] = new Option(givenname + NBSP + "(" + company_name.value + ")", givenname + NBSP + "(" + company_name.value + ")");
	}

	// Set index of the combobox.
	if(tempIndex >=0 && tempIndex <= select_fileas.options.length)
		select_fileas.options[tempIndex].selected = true;
}

function onChangeEmailAddress()
{
	var select_email_address = dhtml.getElementById("select_email_address");
	var selected_email_address = dhtml.getElementById("selected_email_address");
	var email_address = dhtml.getElementById("email_address");
	var email_address_display_name = dhtml.getElementById("email_address_display_name");
	
	dhtml.getElementById("email_address_" + selected_email_address.value).value = email_address.value;
	dhtml.getElementById("email_address_display_name_" + selected_email_address.value).value = email_address_display_name.value;
	
	selected_email_address.value = select_email_address.options[select_email_address.selectedIndex].value;

	email_address.value = dhtml.getElementById("email_address_" + selected_email_address.value).value;
	email_address_display_name.value = dhtml.getElementById("email_address_display_name_" + selected_email_address.value).value;
}

function onUpdateEmailAddress()
{
	var email_address = dhtml.getElementById("email_address");
	var email_address_display_name = dhtml.getElementById("email_address_display_name");
	var select_fileas = dhtml.getElementById("select_fileas");

	email_address_display_name.value = "("+email_address.value.trim()+")";
	if (select_fileas.value.trim()!=""){
		email_address_display_name.value = select_fileas.value.trim()+NBSP+email_address_display_name.value;
	}
}

function onChangeAddress()
{
	var address_button = dhtml.getElementById("address_button");
	var selected_address = dhtml.getElementById("selected_address");
	var mailing_address = dhtml.getElementById("mailing_address");
	var checkbox_mailing_address = dhtml.getElementById("checkbox_mailing_address");
	var address = dhtml.getElementById("address");

	var address_element_id = "business";
	switch(selected_address.value)
	{
		case "1":
			address_element_id = "home";
			break;
		case "3":
			address_element_id = "other";
			break;
	}
	
	dhtml.getElementById(address_element_id + "_address").value = address.value;

	if(typeof module.selectedMenuItem == "undefined") {
		var selectedMenuItem = "business";
	} else {
		var selectedMenuItem = module.selectedMenuItem;
	}

	selected_address.value = 2;
	switch(selectedMenuItem)
	{
		case "home":
			selected_address.value = 1;
			break;
		case "other":
			selected_address.value = 3;
			break;
	}

	address.value = dhtml.getElementById(selectedMenuItem + "_address").value
	if(selected_address.value == mailing_address.value) {
		dhtml.setValue(checkbox_mailing_address, true);
	} else {
		dhtml.setValue(checkbox_mailing_address, false);
	}
}

function onChangeMailingAddress()
{
	var mailing_address = dhtml.getElementById("mailing_address");
	var selected_address = dhtml.getElementById("selected_address");
	
	mailing_address.value = selected_address.value;
}

function eventContactItemSendMailTo()
{
	var toValue = dhtml.getElementById("display_name").value+" <"+dhtml.getElementById("email_address").value+">";
	webclient.openWindow(this, "createmail", DIALOG_URL+"task=createmail_standard&to="+escape(toValue));
}

function categoriesCallBack(categories)
{
	dhtml.getElementById("categories").value = categories;
}

function abCallBack(recipients)
{
	dhtml.getElementById("contacts").value = recipients["contacts"].value;
}

/* functions for detailed contacts */

/**
 * Function will create an advprompt dialog for detailed fullname
 * @param object moduleObject
 */
function eventShowDetailFullNameDialog(moduleObject) {
	if(!moduleObject.contactDialogOpen)
		return false;
	/*
	 * IE doesn't return webclient.dialogs[dialogname].window object so it will be always undefined,
	 * but since this fix is only needed for FF we have to remove this checking in IE
	 * in FF
	 * if user first time opens detailed dialog then window object will be undefined, in this case allow to show detailed dialog
	 * if user has opened and closed detailed dialog then window object will be defined so we have to check property window.closed for actual state of the dialog
	 * if detailed dialog is already open then window.closed will be false so don't show new detailed dialog
	 */
 	// check dialog is already opened or not (only for FF)
	for(var dialogname in webclient.dialogs) {
		if(dialogname.match("contactitem_detailfullnamedialog") != null && !window.BROWSER_IE) {
			if(typeof webclient.dialogs[dialogname].window == "undefined" || webclient.dialogs[dialogname].window.closed == false) {
				webclient.dialogs[dialogname].window.focus();
				return false;
			}
		}
	}

	var result = moduleObject.parseDetailedInfo(dhtml.getElementById("display_name").value, "full_name", true);
	var titles = new Object();
	var suffix = new Object();

	for(var key in moduleObject.titleOptions) {
		// if title is given then select it in select box
		if(typeof result["title"] != "undefined" && result["title"].toLowerCase() == moduleObject.titleOptions[key].toLowerCase()) {
			titles[key] = new Object();
			titles[key]["selected"] = true;
			titles[key]["text"] = moduleObject.titleOptions[key];
		} else {
			titles[key] = moduleObject.titleOptions[key];
		}
	}

	for(var key in moduleObject.suffixOptions) {
		// if suffix is given then select it in select box
		if(typeof result["suffix"] != "undefined" && result["suffix"].toLowerCase() == moduleObject.suffixOptions[key].toLowerCase()) {
			suffix[key] = new Object();
			suffix[key]["selected"] = true;
			suffix[key]["text"] = moduleObject.suffixOptions[key];
		} else {
			suffix[key] = moduleObject.suffixOptions[key];
		}
	}

	webclient.openModalDialog(moduleObject.id, 'contactitem_detailfullnamedialog', DIALOG_URL+'task=advprompt_modal', 325, 300, contactitemDetailfullname_dialog_callback, {
			moduleObject: moduleObject
		}, {
			windowname: _("Check Full Name"),
			dialogtitle: _("Name details"),
			fields: [{
				name: "title",
				label: _("Title") + ":",
				type: "select",
				required: false,
				value: titles
			},
			{
				name: "first_name",
				label: _("First") + ":",
				type: "input",
				required: false,
				value: result["first_name"]
			},
			{
				name: "middle_name",
				label: _("Middle") + ":",
				type: "input",
				required: false,
				value: result["middle_name"]
			},
			{
				name: "last_name",
				label: _("Last") + ":",
				type: "input",
				required: false,
				value: result["last_name"]
			},
			{
				name: "suffix",
				label: _("Suffix") + ":",
				type: "select",
				required: false,
				value: suffix
			},
			{
				name: "setting",
				label: _("Show this again when name is incomplete or unclear"),
				type: "checkbox",
				required: false,
				value: result["setting"]
			}]
		}
	);
}

/**
 * Callback Function to store details of fullname entered in advprompt dialog
 * @param array result[element id] => value
 * @param object callbackdata
 */
function contactitemDetailfullname_dialog_callback(result, callbackData) {
	if(callbackData.moduleObject){
		callbackData.moduleObject.storeDetailedInfo(result, "full_name", true);
	}
}

/**
 * Function will create an advprompt dialog for detailed phone numbers
 * @param object moduleObject
 * @param htmlElement element on which event executed
 */
function eventShowDetailPhoneNumberDialog(moduleObject, element) {
	// store id of phone input box on which operation should be perfomed
	moduleObject.selectedPhoneInputID = element.id;
	moduleObject.selectedPhoneInputID = moduleObject.selectedPhoneInputID.replace("button", "number");

	var result = moduleObject.parseDetailedInfo(dhtml.getElementById(moduleObject.selectedPhoneInputID).value, "phone_number", true);

	webclient.openModalDialog(moduleObject.id, 'contactitem_detailphonenumberdialog', DIALOG_URL+'task=advprompt_modal', 325,250, contactitemDetailphonenumber_dialog_callback, {
			moduleObject: moduleObject
		}, {
			windowname: _("Check Phone Number"),
			dialogtitle: _("Phone details"),
			fields: [{
				// @TODO make select box
				name: "country_code",
				label: _("Country/Region Code") + ":",
				type: "input",
				required: false,
				value: result["country_code"]
			},
			{
				name: "city_code",
				label: _("City/Area code") + ":",
				type: "input",
				required: false,
				value: result["city_code"]
			},
			{
				name: "local_number",
				label: _("Local number") + ":",
				type: "input",
				required: false,
				value: result["local_number"]
			},
			{
				name: "extension",
				label: _("Extension") + ":",
				type: "input",
				required: false,
				value: result["extension"]
			}]
		}
	);
}

/**
 * Callback Function to store details of phone numbers entered in advprompt dialog
 * @param array result[element id] => value
 * @param object callbackdata
 */
function contactitemDetailphonenumber_dialog_callback(result, callbackData) {
	if(callbackData.moduleObject){
		callbackData.moduleObject.storeDetailedInfo(result, "phone_number", true);
	}
}

/**
 * Function will create an advprompt dialog for detailed address
 * @param object moduleObject
 */
function eventShowDetailAddressDialog(moduleObject) {
	if(!moduleObject.contactDialogOpen)
		return false;
	// check dialog is already opened or not (only for FF)
	/*
	 * IE doesn't return webclient.dialogs[dialogname].window object so it will be always undefined,
	 * but since this fix is only needed for FF we have to remove this checking in IE
	 * in FF
	 * if user first time opens detailed dialog then window object will be undefined, in this case allow to show detailed dialog
	 * if user has opened and closed detailed dialog then window object will be defined so we have to check property window.closed for actual state of the dialog
	 * if detailed dialog is already open then window.closed will be false so don't show new detailed dialog
	 */
	for(var dialogname in webclient.dialogs) {
		if(dialogname.match("contactitem_detailaddressdialog") != null && !window.BROWSER_IE) {
			if(typeof webclient.dialogs[dialogname].window == "undefined" || webclient.dialogs[dialogname].window.closed == false) {
				webclient.dialogs[dialogname].window.focus();
				return false;
			}
		}
	}

	var result = moduleObject.parseDetailedInfo(dhtml.getElementById("address").value, "address", true);

	webclient.openModalDialog(moduleObject.id, 'contactitem_detailaddressdialog', DIALOG_URL+'task=advprompt_modal', 325,300, contactitemDetailaddress_dialog_callback, {
			moduleObject: moduleObject
		}, {
			windowname: _("Check Address"),
			dialogtitle: _("Address details"),
			fields: [{
				name: "street",
				label: _("Street") + ":",
				type: "textarea",
				required: false,
				value: result["street"]
			},
			{
				name: "city",
				label: _("City") + ":",
				type: "input",
				required: false,
				value: result["city"]
			},
			{
				name: "state",
				label: _("State/Province") + ":",
				type: "input",
				required: false,
				value: result["state"]
			},
			{
				name: "zip",
				label: _("ZIP/Postal code") + ":",
				type: "input",
				required: false,
				value: result["zip"]
			},
			{
				// @TODO make select box
				name: "country",
				label: _("Country/Region") + ":",
				type: "input",
				required: false,
				value: result["country"]
			},
			{
				name: "setting",
				label: _("Show this again when address is incomplete or unclear"),
				type: "checkbox",
				required: false,
				value: result["setting"]
			}]
		}
	);
}

/**
 * Callback Function to store details of address entered in advprompt dialog
 * @param array result[element id] => value
 * @param object callbackdata
 */
function contactitemDetailaddress_dialog_callback(result, callbackData) {
	if(callbackData.moduleObject){
		callbackData.moduleObject.storeDetailedInfo(result, "address", true);
	}
}

/**
 * Function will show phone selection menu
 * @param object moduleObject
 * @param htmlElement element which called this function
 * @param event event information
 */
function eventShowPhoneTypeMenu(moduleObject, element, event) {
	var items = new Array();
	var selected = "not_selected";

	for(var key in moduleObject.phone_type) {
		if(key != "isdn" && key.indexOf("_fax") == -1 && dhtml.getElementById(key + "_telephone_number").value != "") {
			// if phone number then suffix is "_telephone_number"
			selected = "selected";
		} else if((key == "isdn" || key.indexOf("_fax") != -1) && dhtml.getElementById(key + "_number").value != "") {
			// if fax number or isdn number then suffix is "_number"
			selected = "selected";
		} else {
			selected = "not_selected";
		}

		items.push(webclient.menu.createMenuItem(selected + " " + key, moduleObject.phone_type[key], false, eventOnChangePhoneType, false, false, key));
	}

	// show context menu
	webclient.menu.buildContextMenu(moduleObject.id, element.id, items, element.offsetLeft + element.offsetWidth, element.offsetTop);

	// store id of element which initiated menu
	element = element.previousSibling;
	moduleObject.selectedPhoneElementID = element.id.slice(element.id.lastIndexOf("_") + 1); // value can be 1, 2, 3, 4
}

/**
 * Function will be executed when you select a phone type from menu
 * @param object moduleObject
 * @param htmlElement element which called this function
 * @param event event information
 */
function eventOnChangePhoneType(moduleObject, element, event) {
	var phoneElementID = moduleObject.selectedPhoneElementID;

	// retrieve values of phone number from hidden input fields
	if(element["data"] != "isdn" && dhtml.getElementById(element["data"] + "_telephone_number") != null) {
		dhtml.getElementById("telephone_number_" + phoneElementID).value = dhtml.getElementById(element["data"] + "_telephone_number").value;
	} else {
		dhtml.getElementById("telephone_number_" + phoneElementID).value = dhtml.getElementById(element["data"] + "_number").value;
	}

	// name of the selected phone type is stored in data variable of element
	dhtml.getElementById("selected_phone_" + phoneElementID).value = element["data"];

	dhtml.getElementById("telephone_button_" + phoneElementID).value = element.innerHTML + ":";

	// set focus to the phone input box
	dhtml.getElementById("telephone_number_" + phoneElementID).focus();

	// remove context menu
	if(dhtml.getElementById("contextmenu")) {
		dhtml.deleteElement(dhtml.getElementById("contextmenu"));
	}
}

/**
 * Function will show address selection menu
 * @param object moduleObject
 * @param htmlElement element which called this function
 * @param event event information
 */
function eventShowAddressTypeMenu(moduleObject, element, event) {
	var items = new Array();
	var selected = "not_selected";
	for(var key in moduleObject.address_type) {
		if(dhtml.getElementById(key + "_address").value != "") {
			selected = "selected";
		} else {
			selected = "not_selected";
		}
		items.push(webclient.menu.createMenuItem(selected + " " + key, moduleObject.address_type[key], false, eventOnChangeAddressType, false, false, key));
	}

	// show context menu
	webclient.menu.buildContextMenu(moduleObject.id, element.id, items, element.offsetLeft + element.offsetWidth, element.offsetTop);
}

/**
 * Function will be executed when you select an address type from menu
 * @param object moduleObject
 * @param htmlElement element which called this function
 * @param event event information
 */
function eventOnChangeAddressType(moduleObject, element, event) {
	dhtml.getElementById("address_button").value = element.innerHTML + ":";

	// name of the selected address type is stored in data variable of element
	moduleObject.selectedMenuItem = element["data"];

	onChangeAddress();

	// set focus to the address textarea
	dhtml.getElementById("address").focus();

	// remove context menu
	if(dhtml.getElementById("contextmenu")) {
		dhtml.deleteElement(dhtml.getElementById("contextmenu"));
	}
}

/**
 * Function will update prefix & suffix according to change in display name
 * and vice versa
 * called on "onchange" event of display name, prefix and suffix
 */
function updateDisplayName() {
	// save result of advprompt dialog to hidden variables
	var display_name;
	var display_name_prefix = dhtml.getElementById("display_name_prefix").value;
	var given_name = dhtml.getElementById("given_name").value;
	var middle_name = dhtml.getElementById("middle_name").value;
	var surname = dhtml.getElementById("surname").value;
	var generation = dhtml.getElementById("generation").value;

	display_name = (display_name_prefix != "") ? display_name_prefix + NBSP : "";
	display_name += (given_name != "") ? given_name + NBSP : "";
	display_name += (middle_name != "") ? middle_name + NBSP : "";
	display_name += (surname != "") ? surname + NBSP : "";
	display_name += (generation != "") ? generation : "";

	// set value for display name input box
	dhtml.getElementById("display_name").value = display_name;

	// set value for fileas field
	setFileAs();
}

/**
 * Function will update hidden input boxes with values entered in phone input boxes
 */
function updatePhoneNumberFields() {
	var selected_phone, telephone_number;

	// store phone numbers to hidden input fields
	for(var i = 1; i <= 4; i++) {
		selected_phone = dhtml.getElementById("selected_phone_" + i).value;
		telephone_number = dhtml.getElementById("telephone_number_" + i).value;

		if(dhtml.getElementById(selected_phone + "_telephone_number") != null) {
			dhtml.getElementById(selected_phone + "_telephone_number").value = telephone_number;
		} else {
			dhtml.getElementById(selected_phone + "_number").value = telephone_number;
		}
	}

	/**
	 * display changes done in phone numbers fields to user,
	 * so user will be aware if he overwrites any value
	 */
	for(var i = 1; i <= 4; i++) {
		selected_phone = dhtml.getElementById("selected_phone_" + i).value;

		if(dhtml.getElementById(selected_phone + "_telephone_number") != null) {
			dhtml.getElementById("telephone_number_" + i).value = dhtml.getElementById(selected_phone + "_telephone_number").value;
		} else if(dhtml.getElementById(selected_phone + "_number") != null) {
			dhtml.getElementById("telephone_number_" + i).value = dhtml.getElementById(selected_phone + "_number").value;
		}
	}
}
