/*
* $Id: addressbook.js 634 2008-07-29 02:11:41Z lukejee $

*/
Ext.namespace('NGMP.addressbook');

NGMP.addressbook.ContactReadPanel = function (A) {
	Ext.apply(this, A);
	this.tbar = [
		this.editButton = new Ext.Button({
			iconCls:'icon-edit',
			text: LANG_edit,
			cls:'x-btn-text-icon',
			handler:function() {
			//	console.log('edit button clicked');			
			},
			scope: this,
			disabled: true
		})
	];
	NGMP.addressbook.ContactReadPanel.superclass.constructor.call(this, {
		title: LANG_contact_details,
		region:'east',
		layout: 'fit',
		collapsible: true,
		collapsed: false,
		animCollapse: true,
		autoScroll: true,
		split: true,
		width: 450,
		html:''
	});
};
Ext.extend(NGMP.addressbook.ContactReadPanel, Ext.Panel, {
	initComponent: function() {
		this.template = new Ext.XTemplate(
	   		'<div><table class="readPanel" cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2" class="readPanelHeading">'
			+LANG_contact_details_for+' <b>{full_name}</b></td></tr><tr><td valign="top"><table cellpadding="0" cellspacing="0" border="0">',
			'<tr><td><tpl if="title.length">{title}</tpl>{fullname}<tpl if="street_address.length"><br>{[this.GoogleMapsCityStreet(values)]}',
			'</tpl><tpl if="zip_code.length || city.length"><br>{zip_code}{city}</tpl><tpl if="state.length"><br>{state}</tpl><tpl if="country.length"><br>{country}</tpl>',
			'</td></tr></table></td><td valign="top" class="contactCompanyDetailsPanelKolom"><table cellpadding="0" cellspacing="0" border="0">',
			'<tpl if="initials.length"><tr><td>'+LANG_initials+':</td><td>{initials}</td></tr></tpl>',
			'<tpl if="birthday"><tr><td>'+LANG_birthday+':</td><td>{birthday}</td></tr></tpl>',
			'</table></td></tr><tpl if="this.isContactFieldset(values)"><tr><td colspan="2" class="readPanelHeading">'+LANG_contact_details
			+'</td></tr><tr><td valign="top" class="contactCompanyDetailsPanelKolom60"><table cellpadding="0" cellspacing="0" border="0">',
			'<tpl if="email.length"><tr><td class="contactCompanyLabelWidth">'+LANG_email
			+':</td><td><a href="#">{email}</a></td>',
			'</tr></tpl><tpl if="email2.length"><tr><td class="contactCompanyLabelWidth">'+LANG_email+
			' 2:</td><td><a href="#">{email2}</a></td>',
			'</tr></tpl><tpl if="email3.length"><tr><td class="contactCompanyLabelWidth">'+LANG_email+
			' 3:</td><td><a href="#">{email3}</a></td>',
			'</tr></tpl><tpl if="this.isPhoneFieldset(values)"><tr><td colspan="2">&nbsp;</td></tr><tpl if="phone.length">',
			'<tr><td class="contactCompanyLabelWidth">'+LANG_phone+':</td><td><a href="callto:{phone}+type=phone">{phone}</a></td>',
			'</tr></tpl><tpl if="mobile.length"><tr><td class="contactCompanyLabelWidth">'+LANG_mobile+
			':</td><td><a href="callto:{mobile}+type=phone">{mobile}</a></td></tr></tpl>',
			'<tpl if="fax.length"><tr><td class="contactCompanyLabelWidth">'+LANG_fax+':</td><td>{fax}</td></tr></tpl></tpl>',
			'</table></td><tpl if="this.isWorkPhoneFieldset(values)"><td valign="top" class="contactCompanyDetailsPanelKolom40">',
			'<table cellpadding="0" cellspacing="0" border="0"><tpl if="phone_work.length"><tr><td class="contactCompanyLabelWidth">'
			+LANG_phone_work+':</td><td><a href="callto:{phone_work}+type=phone">{phone_work}</a></td></tr></tpl><tpl if="fax_work.length">',
			'<tr><td class="contactCompanyLabelWidth">'+LANG_fax_work+':</td><td>{fax_work}</td></tr></tpl></table></td></tpl></tr>',
			'</tpl><tpl if="this.isWorkFieldset(values)"><tr><td colspan="2" class="readPanelHeading">'+LANG_work+
			'</td></tr><tr><td colspan="2" valign="top" class="contactCompanyDetailsPanelKolom60"><table cellpadding="0" cellspacing="0" border="0">',
			'<tpl if="company.length"><tr><td class="contactCompanyLabelWidth">'+LANG_company+':</td><td>{company}</td>',
			'</tr></tpl><tpl if="work_function.length"><tr><td class="contactCompanyLabelWidth">'+LANG_work_function+
			':</td><td>{work_function}</td></tr></tpl><tpl if="department.length"><tr><td class="contactCompanyLabelWidth">'+LANG_department
			+':</td><td>{department}</td></tr></tpl></table></td></tr></tpl><tpl if="comments.length"><tr>',
			'<td class="readPanelHeading" colspan="2">'+LANG_comments+'</td></tr><tr><td colspan="2">{comments}</td></tr></tpl>',
			'</table><tpl if="links.length"><table class="readPanel" cellpadding="0" cellspacing="0" border="0"><tr><td colspan="4" class="readPanelHeading">' +LANG_links+'</td></tr><tr><td width="16" class="table_header_links">&nbsp;</td><td class="table_header_links">'+
			LANG_real_name+'</td><td class="table_header_links">'+LANG_type+'</td><td class="table_header_links">'+LANG_mtime+
			'</td></tr><tpl for="links"><tr><td><div class="go-link-icon-{link_type}"></div></td><td><a href="#">{name}</a></td><td>{type}</td><td>{mtime}</td></tr></tpl></tpl></div>',{
					isContactFieldset:function(A) {
						if (A.email.length || A.email2.length || A.email3.length || A.phone.length || A.fax.length || A.mobile.length || A.phone_work.length || A.fax_work.length)
							return true;
						else
							return false;
			  		}
				}, {
					isPhoneFieldset: function (A) {
						if (A.phone.length || A.fax.length || a.mobile.length)
							return true;
						else
							return false;
					}
				}, {
					isWorkPhoneFieldset: function(A) {
						if (A.phone_work.length || A.fax_work.length)
							return true;
						else
							return false;
					 }
				},{
					isWorkFieldset: function(A) {
						if (A.company.length || A.work_function.length || A.department.length)
							return true;
						else
							return false;
					}
				}, {
					GoogleMapsCityStreet: function(A) {
						return false;
					}
				}
			);
		NGMP.addressbook.ContactReadPanel.superclass.initComponent.call(this);
	},
	setData: function(A) {
		this.data = A;
		//this.editButton.setDisabled(!A.write_permission);
		this.template.overwrite(this.body, A);
	}
});

NGMP.addressbook.CompanyReadPanel = function (A) {
	Ext.apply(this, A);
	this.tbar = [
		this.editButton = new Ext.Button({
			iconCls:'icon-edit',
			text: LANG_edit,
			cls:'x-btn-text-icon',
			handler:function() {
		//		console.log('edit button clicked');			
			},
			scope: this,
			disabled: true
		})
	];
	NGMP.addressbook.CompanyReadPanel.superclass.constructor.call(this, {
		title: LANG_company_details,
		region:'east',
		layout: 'fit',
		collapsible: true,
		collapsed: false,
		animCollapse: true,
		autoScroll: true,
		split: true,
		width: 450,
		html:''
	});
};
Ext.extend(NGMP.addressbook.CompanyReadPanel, Ext.Panel, {
	initComponent: function() {
		this.template = new Ext.XTemplate(
			'<div><table class="readPanel" cellpadding="0" cellspacing="0" border="0"><tr>',
			'<tpl if="this.isCompanySecondColumn(values)"><td colspan="2" valign="top" class="readPanelHeading"></tpl>',
			'<tpl if="this.isCompanySecondColumn(values) == false"><td valign="top" class="readPanelHeading"></tpl>' +LANG_company_details_for+'&nbsp;<b>{name}</b></td></tr><tr>', '<tpl if="this.isCompanySecondColumn(values)"><tpl if="this.isBankVat(values)"><td valign="top" class="contactCompanyDetailsPanelKolom"></tpl>', '<tpl if="this.isBankVat(values) == false"><td colspan="2" valign="top" class="contactCompanyDetailsPanelKolom100"></tpl></tpl>', '<tpl if="this.isCompanySecondColumn(values) == false"><td valign="top" class="contactCompanyDetailsPanelKolom100"></tpl>', '<table cellpadding="0" cellspacing="0" border="0">', '<tpl if="phone.length"><tr><td class="contactCompanyLabelWidth">'+LANG_phone+':</td><td><a href="callto:{phone}+type=phone">{phone}</a></td></tr></tpl>', '<tpl if="fax.length"><tr><td class="contactCompanyLabelWidth">'+LANG_fax+':</td><td>{fax}</td></tr></tpl>', '<tpl if="email.length"><tr><td class="contactCompanyLabelWidth">'+LANG_email+':</td><td><a href="#" uuups="">{email}</a></td></tr></tpl>', '<tr><td colspan="2">&nbsp;</td></tr>', '<tpl if="homepage.length"><tr><td class="contactCompanyLabelWidth">'+LANG_homepage+':</td><td>&nbsp;<a href="{homepage}" target="_blank">{homepage}</a></td><tr></tpl>', '</table></td>', '<tpl if="this.isBankVat(values)"><td valign="top" class="contactCompanyDetailsPanelKolom"><table cellpadding="0" cellspacing="0" border="0">', '<tpl if="bank_number.length"><tr><td>'+LANG_bank_number+':</td><td>&nbsp;{bank_number}</td></tr></tpl>', '<tpl if="vat_number.length"><tr><td>'+LANG_vat_number+':</td><td>{vat_number}</td></tr></tpl>', '</table></td></tpl></tr>', '<tpl if="this.isAddress(values)"><tr>', '<tpl if="this.isCompanySecondColumn(values)"><td colspan="2" valign="top" class="readPanelHeading"></tpl>', '<tpl if="this.isCompanySecondColumn(values) == false"><td valign="top" class="readPanelHeading"></tpl>'+ LANG_contact+'</td></tr><tr>', '<tpl if="this.isAddressVisit(values)"><tpl if="this.isCompanySecondColumn(values)">', '<tpl if="this.isAddressPost(values)"><td valign="top" class="contactCompanyDetailsPanelKolom"></tpl>', '<tpl if="this.isAddressPost(values) == false"><td colspan="2" valign="top" class="contactCompanyDetailsPanelKolom100"></tpl></tpl>', '<tpl if="this.isCompanySecondColumn(values) == false"><td valign="top" class="contactCompanyDetailsPanelKolom100"></tpl>', '<table cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2" class="readPanelSubHeading">'+LANG_visit_address+'</td></tr><tr><td>', '<tpl if="visit_street_address.length ">{[this.GoogleMapsCityStreet(values)]}</tpl>', '<tpl if="visit_zip_code.length || visit_city.length"><br>{visit_zip_code}{visit_city}</tpl>', '<tpl if="visit_state.length"><br>{visit_state}</tpl>', '<tpl if="visit_country.length"><br>{visit_country}</tpl></table></td></tpl>', '<tpl if="this.isAddressPost(values)"><tpl if="this.isAddressVisit(values)"><td valign="top" class="contactCompanyDetailsPanelKolom"></tpl>', '<tpl if="this.isAddressVisit(values) === false"><td colspan="2" valign="top" class="contactCompanyDetailsPanelKolom100"></tpl>', '<table cellpadding="0" cellspacing="0" border="0"><tr><td colspan="2" class="readPanelSubHeading">'+LANG_post_address+'</td></tr><tr><td>', '<tpl if="post_street_address.length">{post_street_address}</tpl><tpl if="post_zip_code.length || post_city.length"><br>{post_zip_code}{post_city}</tpl>', '<tpl if="post_state.length"><br>{post_state}</tpl><tpl if="post_country.length"><br>{post_country}</tpl></table></td></tpl></tr></tpl></table>', '<tpl if="comments.length"><table cellpadding="0" cellspacing="0" border="0" class="readPanel"><tr><td class="readPanelHeading">'+LANG_comment+'</td></tr><tr><td>{comments}</td></tr></table></tpl>', '<tpl if="links.length"><table cellpadding="0" cellspacing="0" border="0" class="readPanel"><tr><td colspan="4" class="readPanelHeading">'+LANG_links+ '</td></tr><tr><td width="16" class="table_header_links">&nbsp;</td><td class="table_header_links">'+LANG_name+'</td><td class="table_header_links">' +LANG_type+'</td><td class="table_header_links">'+LANG_mtime+'</td></tr><tpl for="links"><tr><td><div class="go-link-icon-{link_type}">', '</div></td><td><a href="#" uuups="GO.linkHandlers[{link_type}].call(this, {id})">{name}</a></td><td>{type}</td><td>', '{mtime}</td></tr></tpl></table></tpl></div>',{
					isCompanySecondColumn:function(A) {
						if (this.isBankVat(A) || this.isAddressPost(A))
							return true;
						else
							return false;
			  		}
				}, {
					isBankVat: function (A) {
						if (A.bank_number.length||A.vat_number.length)
							return true;
						else
							return false;
					}
				}, {
					isAddress: function(A) {
						if (A.visit_street_address.length || A.visit_zip_code.length || A.visit_city.length || A.visit_state.length || A.visit_country.length || A.post_street_address.length || A.post_zip_code.length || A.post_city.length || A.post_state.length || A.post_country.length)
							return true;
						else
							return false;
					}
				}, {
					isAddressVisit: function(A) {
						if (A.visit_street_address.length || A.visit_zip_code.length || A.visit_city.length || A.visit_state.length || A.visit_country.length)
							return true;
						else
							return false;
					}
				}, {
					isAddressPost: function(A) {
						if (A.post_street_address.length || A.post_zip_code.length || A.post_city.length || A.post_state.length || A.post_country.length)
							return true;
						else
							return false;
					}
				}, {
					GoogleMapsCityStreet: function(A) {
						return false;
					}
				}
			);
		NGMP.addressbook.CompanyReadPanel.superclass.initComponent.call(this);
	},
	setData: function(A) {
		this.data = A;
		//this.editButton.setDisabled(!A.write_permission);
		this.template.overwrite(this.body, A);
	}
});

NGMP.addressbook.CompanyWindow = function(A) {

	this.company_id = A.id;

	this.addEvents({
		saved: true
	});

	this.widthLeftColumn = 250;
	this.widthRightColumn = 253;
	this.widthInputSeparator = 3;
	this.separator = ':';

	this.formName = new Ext.form.TextField({
		fieldLabel: LANG_name,
		name:'name',
		allowBlank: false,
		labelSeparator: this.separator,
		width: this.widthLeftColumn
	});

	this.formPhone = new Ext.form.TextField({
		fieldLabel: LANG_phone,
		name:'phone',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthLeftColumn
	});

	this.formFax = new Ext.form.TextField({
		fieldLabel: LANG_fax,
		name:'fax',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthLeftColumn
	});

	this.formEmail = new Ext.form.TextField({
		fieldLabel: LANG_email,
		name:'email',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthLeftColumn
	});
	this.formHomepage = new Ext.form.TextField({
		fieldLabel: LANG_homepage,
		name:'homepage',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthLeftColumn
	});
	this.formBankNumber = new Ext.form.TextField({
		fieldLabel: LANG_bank_number,
		name:'bank_number',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthLeftColumn
	});
	this.formVatNumber = new Ext.form.TextField({
		fieldLabel: LANG_vat_number,
		name:'vat_number',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthLeftColumn
	});

	this.formVisitStreetAddress = new Ext.form.TextField({
		fieldLabel: LANG_street_address,
		name: 'visit_street_address',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});

	this.formVisitZipCode = new Ext.form.TextField({
		fieldLabel: LANG_zip_code,
		name:'visit_zip_code',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formVisitCity = new Ext.form.TextField({
		fieldLabel: LANG_city,
		name:'visit_city',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formVisitState = new Ext.form.TextField({
		fieldLabel: LANG_state,
		name:'visit_state',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formVisitCountry = new Ext.form.TextField({
		fieldLabel: LANG_country,
		name:'visit_country',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});

	this.formPostStreetAddress = new Ext.form.TextField({
		name:'post_street_address',
		fieldLabel: LANG_street_address,
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});

	this.formPostZipCode = new Ext.form.TextField({
		fieldLabel: LANG_zip_code,
		name:'post_zip_code',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formPostCity = new Ext.form.TextField({
		fieldLabel: LANG_city,
		name:'post_city',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formPostState = new Ext.form.TextField({
		fieldLabel: LANG_state,
		name:'post_state',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formPostCountry = new Ext.form.TextField({
		fieldLabel: LANG_country,
		name:'post_country',
		allowBlank: true,
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});

	this.formComments = new Ext.form.TextArea({
		name:'comments',
		hideLabel: true,
		allowBlank: true
	});

	this.companyDetailsFieldset = {
		xtype: 'fieldset',
		title:LANG_company_details,
		autoHeight: true,
		collapsed: false,
		border: true,
		defaults: {border: false, layout:'table'},
		items:[{
			defaults:{border:false},
			items:[{layout:'form',items:[this.formName]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formPhone]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formFax]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formEmail]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formHomepage]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formBankNumber]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formVatNumber]}]
		}]
	};

	this.visitAddressFieldset = {
		xtype: 'fieldset',
		title: LANG_visit_address,
		autoHeight: true,
		collapsed: false,
		border: true,
		defaults: {border: false, layout: 'table'},
		items:[{
			defaults:{border:false},
			items:[{layout:'form',items:[this.formVisitStreetAddress]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formVisitZipCode]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formVisitCity]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formVisitState]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formVisitCountry]}]
		}]
	};
	this.postAddressFieldset = {
		xtype: 'fieldset',
		title: LANG_post_address,
		autoHeight: true,
		collapsed: false,
		border: true,
		defaults: {border: false, layout: 'table'},
		items:[{
			defaults:{border:false},
			items:[{layout:'form',items:[this.formPostStreetAddress]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formPostZipCode]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formPostCity]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formPostState]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formPostCountry]}]
		}]
	};

	this.companyPanel = new Ext.Panel({
		bodyStyle:'padding: 5px 5px 5px 5px',
		layout:'column',
		defaults:{border: false},
		items:[{
			columnWidth:0.5,
			defaults:{border:false},
			items:[this.companyDetailsFieldset]
		}, {
			columnWidth:0.5,
			defaults:{border:false},
			style: 'margin-left: 5px;',
			items:[
				this.visitAddressFieldset,
				this.postAddressFieldset
			]
		}],
		title:LANG_company_details
	});
	this.commentsPanel = new Ext.Panel({
		title:LANG_comments,	
		iconCls:'icon-comment-16',
		layout: 'fit',
		items:[this.formComments]
	});
	this.commentsPanel.on('show', function() {
		this.formComments.focus();
	}, this);
    this.companyForm=new Ext.FormPanel({
		border: false,
		url:"ngmp.php",
		baseParams:{
			func:'abGetCompany'
        },
		items: [
			this.tabPanel = new Ext.TabPanel({
				border:false,
				activeTab: 0,
				hideLabel: false,
				deferredRender: false,
				anchor:'100% 100%',
				items:[this.companyPanel, this.commentsPanel]
		})]
    });
    NGMP.addressbook.CompanyWindow.superclass.constructor.call(this,{
        id:"panelCompany_"+this.company_id,
		title:this.title,
		width:820,
		height:545,
		autoHeight:false,
		minWidth:820,
		minHeight:545,
		layout:"fit",
		modal:true,
		iconCls:"icon-addressbook-edit-company",
		plain:true,
		bodyBorder:false,
		constrain:true,
		buttonAlign:"right",
		maximizable:false,
		resizable:false,
		plugins:[new NGMP.plugins.IconMenu({
            
        }
        )],buttons:[{
            id:"buttonSaveCompany",
			text:LANG_save,
			handler:function(){
                this.companyForm.form.submit({
					params:{
						func:'abSaveCompany',
						id:this.company_id
					},
                    waitMsg:LANG_saving_company + "...",
					success:function(B,C){
						this.fireEvent('saved',this);
                       	this.close();
                    },
                    failure:function(B,C){
                        Ext.Msg.alert(LANG_error_saving_company, LANG_unable_to_save_company);
                    },
                    scope:this
                });
            },
            scope:this
        },
        {
            id:"buttonCancelCompany",
			text:LANG_cancel,
			handler:function(){
                this.close();
            },
            scope:this
        } ],
		items:[this.companyForm]
    });
};
Ext.extend(NGMP.addressbook.CompanyWindow,Ext.Window,{
    show:function(A){

		this.title = this.company_id == 0 ? LANG_add_company : LANG_edit_company;

		if (this.company_id > 0) {
			this.companyForm.load({
				params:{id:this.company_id}
			});
		}
        NGMP.addressbook.CompanyWindow.superclass.show.call(this);
    }
});

NGMP.addressbook.ContactWindow=function(A){

	this.contact_id = A.id ? A.id : 0;

	if (this.contact_id == 0) {
		this.title = LANG_add_contact;
	} else {
		this.title = LANG_edit_contact;
	}

	this.addEvents({
		saved: true
	});

	this.widthLeftColumn = 256;
	this.widthRightColumn = 253;
	this.widthNameField = 100;
	this.widthExtraField = 50;
	this.widthInputSeparator=3;
	this.separator = ':';

	this.formFirstName = new Ext.form.TextField({
		fieldLabel: LANG_name,
		name:'first_name',
		id:'first_name',
		allowBlank: false,
		width: 100
	});
	this.formMiddleName = new Ext.form.TextField({
		fieldLabel:'',
		name:'middle_name',
		labelSeparator:null,
		hideLabel:true,
		width: this.widthExtraField,
		style:'margin-left:'+this.widthInputSeparator+'px;'
	});
	this.formLastName = new Ext.form.TextField({
		fieldLabel:'',
		name:'last_name',
		labelSeparator:null,
		hideLabel:true,
		width:this.widthLeftColumn-(this.widthNameField+this.widthExtraField+(2*this.widthInputSeparator)),
		allowBlank: false,
		style:'margin-left:'+this.widthInputSeparator+'px;'
	});
	this.formTitle = new Ext.form.TextField({
		fieldLabel:LANG_title,
		name:'title',
		labelSeparator:this.separator,
		width:126
	});
	this.formInitials = new Ext.form.TextField({
		fieldLabel:'',
		name:'initials',
		labelSeparator:null,
		hideLabel: true,
		width: 127,
		style: 'margin-left: '+this.widthInputSeparator+'px;'
	});
	this.formSexMale = new Ext.form.Radio({
		name:'sex',
		fieldLabel:LANG_sex,
		labelSeparator:this.separator,
		boxLabel:LANG_male,
		checked: true,
		inputValue:'M',
		dataIndex:'sexM',
		width:75
	});
	this.formSexFemale = new Ext.form.Radio({
		name:'sex',
		boxLabel: LANG_female,
		labelSeparator: null,
		hideLabel: true,
		inputValue:'F',
		dataIndex:'sexF',
		width: 75,
		height: 20
	});
	this.formSalutation = new Ext.form.TextField({
		fieldLabel:LANG_salutation,
		name:'salutation',
		id:'salutation',
		labelSeparator:this.separator,
		width: this.widthLeftColumn
	});

	this.formBirthday = new Ext.form.DateField({
		fieldLabel: LANG_birthday,
		name:'birthday',
		labelSeparator:this.separator,
		width:this.widthRightColumn
	});

	this.formEmail = new Ext.form.TextField({
		fieldLabel: LANG_email,
		name:'email',
		labelSeparator:this.separator,
		width: this.widthRightColumn
	});
	this.formEmail2 = new Ext.form.TextField({
		fieldLabel: LANG_email+'2',
		name:'email2',
		labelSeparator:this.separator,
		width: this.widthRightColumn
	});
	this.formEmail3 = new Ext.form.TextField({
		fieldLabel: LANG_email+'3',
		name:'email3',
		labelSeparator:this.separator,
		width: this.widthRightColumn
	});
	this.formIM = new Ext.form.TextField({
		fieldLabel: LANG_im,
		name:'im',
		labelSeparator:this.separator,
		width: this.widthRightColumn
	});
	this.formIM2 = new Ext.form.TextField({
		fieldLabel: LANG_im+'2',
		name:'im2',
		labelSeparator:this.separator,
		width: this.widthRightColumn
	});
	this.formIM3 = new Ext.form.TextField({
		fieldLabel: LANG_im+'3',
		name:'im3',
		labelSeparator:this.separator,
		width: this.widthRightColumn
	});

	this.formPhone = new Ext.form.TextField({
		fieldLabel: LANG_phone,
		name:'phone',
		labelSeparator:this.separator,
		width: this.widthRightColumn
	});
	this.formPhoneWork = new Ext.form.TextField({
		fieldLabel: LANG_phone_work,
		name:'phone_work',
		labelSeparator:this.separator,
		width: this.widthRightColumn
	});
	this.formFax = new Ext.form.TextField({
		fieldLabel: LANG_fax,
		name:'fax',
		labelSeparator: this.separator,
		width:this.widthRightColumn
	});

	this.formMobile = new Ext.form.TextField({
		fieldLabel: LANG_mobile,
		name:'mobile',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});

	this.formFaxWork = new Ext.form.TextField({
		fieldLabel: LANG_fax_work,
		name:'fax_work',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});

	this.formMobileWork = new Ext.form.TextField({
		fieldLabel: LANG_mobile_work,
		name:'mobile_work',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});

	this.formStreetAddress = new Ext.form.TextField({
		fieldLabel: LANG_street_address,
		name:'street_address',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});

	this.formZipCode = new Ext.form.TextField({
		fieldLabel: LANG_zip_code,
		name:'zip_code',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formCity = new Ext.form.TextField({
		fieldLabel: LANG_city,
		name:'city',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formState = new Ext.form.TextField({
		fieldLabel: LANG_state,
		name:'state',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formCountry = new Ext.form.TextField({
		fieldLabel: LANG_country,
		name:'country',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});

	this.formCompany = new Ext.form.TextField({
		fieldLabel: LANG_company,
		name:'company',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formDepartment = new Ext.form.TextField({
		fieldLabel: LANG_department,
		name:'department',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formFunction = new Ext.form.TextField({
		fieldLabel: LANG_work_function,
		name:'work_function',
		labelSeparator: this.separator,
		width: this.widthRightColumn
	});
	this.formComments = new Ext.form.TextArea({
		name:'comments',
		fieldLabel:'',
		hideLabel:true
	});

	this.personalFieldset = {
		xtype:'fieldset',
		title:LANG_personal_details,
		autoHeight: true,
		collapsed: false,
		defaults:{border:false, layout:'table'},
		items:[{
			defaults: {border:false, layout:'form'},
			items:[{
				items:[this.formFirstName]
			},{
				items:[this.formMiddleName]
			},{
				items:[this.formLastName]
			}]
		},{
			defaults:{border: false},
			items:[{
				layout:'form',
				items:[this.formTitle]
			},{
				layout:'form',
				items:[this.formInitials]
			}]
		}, {
			defaults:{border: false},
			items:[{
				layout:'form',
	   			items:[this.formSexMale]
			}, {
				layout:'form',
				items:[this.formSexFemale]
			}]
		},{
			defaults:{border:false},
			items:[{
				layout:'form',
	   			items:[this.formSalutation]
			}]
		}, {
			defaults:{border:false},
			items:[{
				layout:'form',
	   			items:[this.formBirthday]
			}]
		}]
	};

	this.addressFieldset = {
		xtype:'fieldset',
		title: LANG_address,
		autoHeight: true,
		collapsed: false,
		defaults: {border: false, layout:'table'},
		items:[{
			defaults:{border:false},
			items:[{
				layout:'form',
	   			items:[this.formStreetAddress]
			}]
		}, {
			defaults:{border:false},
			items:[{
				layout:'form',
	   			items:[this.formZipCode]
			}]
		}, {
			defaults:{border:false},
			items:[{
				layout:'form',
	  			items:[this.formCity]
			}]
		}, {
			defaults:{border:false},
			items:[{
				layout: 'form',
				items:[this.formState]
			}]
		}, {
			defaults:{border:false},
			items:[{
				layout: 'form',
				items:[this.formCountry]
			}]
		}]
	};

	this.contactFieldset = {
		xtype:'fieldset',
		title:LANG_contact,
		autoHeight: true,
		collapsed: false,
		defaults:{border:false, layout:'table'},
		items:[{
			defaults:{border:false},
			items:[{layout:'form',items:[this.formEmail]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formEmail2]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formEmail3]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formIM]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formIM2]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formIM3]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formPhone]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formFax]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formMobile]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formPhoneWork]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formFaxWork]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formMobileWork]}]
		}, {
		}]
	};

	this.workFieldset = {
		xtype:'fieldset',
		title: LANG_work,
	  	autoHeight: true,
	  	collapsed: false,
	  	defaults:{border:false, layout:'table'},
	  	items:[{
			defaults:{border:false},
			items:[{layout:'form',items:[this.formCompany]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formDepartment]}]
		}, {
			defaults:{border:false},
			items:[{layout:'form',items:[this.formFunction]}]
		}]
	};


	this.personalPanel = new Ext.Panel({
		title: LANG_personal_details,
		bodyStyle: 'padding: 5px 5px 5px 5px',
		layout: 'column',
		defaults: {border: false},
		items:[{
			columnWidth: 0.5,
			autoScroll: true,
			items:[this.personalFieldset, this.addressFieldset]
		},{
			columnWidth: 0.5,
			style: 'margin-left: 5px;',
			items:[
				this.contactFieldset,
				this.workFieldset
			]
		}]
	});
	this.commentPanel = new Ext.Panel({
		title: LANG_comments,
		iconCls:'icon-comment-16',
		layout:'fit',
		border:false,
		items:[this.formComments]
	});
	this.commentPanel.on('show', function() {this.formComments.focus();},this);

    this.contactForm=new Ext.FormPanel({
		border: false,
		url:'ngmp.php',
		baseParams:{
			func:'abGetContact'
        },
		items: [
			this.tabPanel = new Ext.TabPanel({
				border:false,
				activeTab: 0,
				hideLabel: false,
				deferredRender: false,
				anchor:'100% 100%',
				items:[this.personalPanel, this.commentPanel]
		})]
    });
    NGMP.addressbook.ContactWindow.superclass.constructor.call(this,{
        id:"panelContact_"+this.contact_id,
		title:this.title,
		width:820,
		height:580,
		autoHeight:false,
		minWidth:820,
		minHeight:545,
		layout:"fit",
		modal:true,
		iconCls:"icon-contacts",
		plain:true,
		bodyBorder:false,
		constrain:true,
		buttonAlign:"right",
		maximizable:false,
		resizable:false,
		plugins:[new NGMP.plugins.IconMenu({
            
        }
        )],buttons:[{
            id:"buttonSaveContact",
			text:LANG_save,
			handler:function(){
                this.contactForm.getForm().submit({
					params: {
						func: 'abSaveContact',
						id: this.contact_id
					},
                    waitMsg:LANG_saving_contact + "...",
					success:function(B,C){
						this.fireEvent('saved');
                  		this.close();
					},
                    failure:function(B,C){
                        Ext.Msg.alert(LANG_error_saving_contact, LANG_unable_to_save_contact);
                    },
                    scope:this
                });
            },
            scope:this
        },
        {
            id:"buttonCancelContact",
			text:LANG_cancel,
			handler:function(){
                this.close();
            },
            scope:this
        }
        ],items:[this.contactForm]
    });
};
Ext.extend(NGMP.addressbook.ContactWindow,Ext.Window,{
    show:function(){
		this.title = this.contact_id === 0 ? LANG_add_contact : LANG_edit_contact;
		if (this.contact_id > 0)
			this.contactForm.load({
				params:{
					id: this.contact_id
				}
			});
        NGMP.addressbook.ContactWindow.superclass.show.call(this);
/*
		if (this.config.data) {
		this.formFirstName.setValue(this.config.data.first_name);
		this.formMiddleName.setValue(this.config.data.middle_name);
		this.formLastName.setValue(this.config.data.last_name);
		this.formTitle.setValue(this.config.data.title);
		this.formInitials.setValue(this.config.data.initials);
		this.formSexMale.checked = this.config.data.sex == 'M' ? true : false;
		this.formSexFemale.checked = this.config.data.sex == 'F' ? true : false;
		this.formSalutation.setValue(this.config.data.salutation);
		this.formBirthday.setValue(this.config.data.birthday);
		this.formStreetAddress.setValue(this.config.data.street_address);
		this.formZipCode.setValue(this.config.data.zip_code);
		this.formState.setValue(this.config.data.state);
		this.formCity.setValue(this.config.data.city);
		this.formCountry.setValue(this.config.data.country);
		this.formPhone.setValue(this.config.data.phone);
		this.formFax.setValue(this.config.data.fax);
		this.formEmail.setValue(this.config.data.email);
		this.formEmail.setValue(this.config.data.email);
		this.formEmail2.setValue(this.config.data.email2);
		this.formEmail3.setValue(this.config.data.email3);
		this.formIM.setValue(this.config.data.im);
		this.formIM2.setValue(this.config.data.im2);
		this.formIM3.setValue(this.config.data.im3);
		this.formPhone.setValue(this.config.data.phone);
		this.formFax.setValue(this.config.data.fax);
		this.formMobile.setValue(this.config.data.mobile);
		this.formPhoneWork.setValue(this.config.data.phone_work);
		this.formFaxWork.setValue(this.config.data.fax_work);
		this.formMobileWork.setValue(this.config.data.mobile_work);
		this.formCompany.setValue(this.config.data.company);
		this.formDepartment.setValue(this.config.data.department);
		this.formFunction.setValue(this.config.data.work_function);
		
		this.formComments.setValue(this.config.data.comments);
		}
        if(this.config.type=="Edit" && this.config.data.writable!==true){
			this.companyForm.getForm().items.each(function(f){
				f.el.dom.readOnly = true;
			});
			Ext.getCmp('buttonSaveCompany').disable();
        }
*/
    }
});

NGMP.addressbook.ContactsPanel=function(A){
    this.config=A||{
        
    };
    this.addEvents({
        refresh:true
    });
    this.tplBusinessCard=new Ext.XTemplate(
	'<tpl for=".">','<div class="thumb-wrap">','<div class="x-box" style="width:250px;">','<div class="x-box-tl">','<div class="x-box-tr">',
	'<div class="x-box-tc">',"</div>","</div>","</div>",'<div class="x-box-ml">','<div class="x-box-mr">',
	'<div class="x-box-mc" style="height:110px">',"<table>","<tr>",'<td valign="top">','<img src="themes/ngmp/images/Contacts32.png" align="top"/>',
	"</td>",'<td style="padding-left:20px">',"<h3>{name}</h3>","<p>{email}</p>","<p>{street}</p>","<p>{city}</p>",
	"<p>{state}</p>","<p>{work}</p>","</td>","</tr>","</table>","</div>","</div>","</div>",'<div class="x-box-bl">','<div class="x-box-br">',
	'<div class="x-box-bc">',"</div>","</div>","</div>","</div>","</div>","</tpl>",'<div class="x-clear"></div>');
    this.tplBusinessCard.compile();
    this.tplAddressCard=new Ext.XTemplate(
	'<tpl for=".">','<div class="thumb-wrap">','<div class="x-box" style="width:200px">','<div class="x-box-tl">','<div class="x-box-tr">',
	'<div class="x-box-tc">',"</div>","</div>","</div>",'<div class="x-box-ml">','<div class="x-box-mr">','<div class="x-box-mc">',
	"<h3>{name}</h3>","<table>","<tr>",'<td width="50px">',"E-mail:","</td>",'<td class="email-right">',"{shortemail}","</td>",
	"</tr>","</table>","</div>","</div>","</div>",'<div class="x-box-bl">','<div class="x-box-br">','<div class="x-box-bc">',
	"</div>","</div>","</div>","</div>","</div>","</tpl>",'<div class="x-clear"></div>');
    this.tplAddressCard.compile();
    this.btb=new Ext.Toolbar({
        id:"toolbarBottomContactsPanel",
		height:24
    });
    this.tb=new Ext.Toolbar({
        id:"toolbarTopContactsPanel",stateful:false,items:[{
            id:"tbbuttonNewContact",
			iconCls:"icon-add-contact",
			cls:"x-btn-text-icon",
			text:LANG_new_contact,
			tooltip:LANG_new_contact_tooltip,
			enableToggle:false,
			pressed:false,
			handler:function(){
            //    var C="contact_"+this.config.contactsStore.getCount();
                var B=new NGMP.addressbook.ContactWindow({id:0});
				B.render(this.body);
                B.show();
            },
			scope:this
        },
        {
            id:"tbbuttonDeleteContact",
			iconCls:"icon-trash",
			cls:"x-btn-text-icon",
			text:LANG_delete,
			disabled:true,
			tooltip:LANG_delete_contact_tooltip,
			enableToggle:false,
			pressed:false,
			handler:function(){
                var C=this.contactsView.getSelectedIndexes();
                var B="";
                for(i=0;i<C.length;i++){
                    if(B.length>0){
                        B+=",";
                    }
                    B+=C[i];
                }
                Ext.Ajax.request({
                    url:"ngmp.php",
					params:{
						func:'contacts',
                        id:B,opt:"dele"
                    },
                    method:"GET",
					success:function(D,E){
                        var F=Ext.util.JSON.decode(D.responseText);
                        if(F.success){
                            Ext.Msg.alert(LANG_delete, LANG_contact_deleted_successfully,function(){
                                this.fireEvent("refresh",this);
                            },
                            this);
                        }
                        else{
                            Ext.Msg.alert(LANG_error, LANG_unable_to_delete_contact);
                        }
                        
                    },
                    failure:function(D,E){
                        Ext.Msg.alert(LANG_error,LANG_unable_to_delete_contact);
                    }
                    ,scope:this
                });
            },
            scope:this
        }
        ]
    });
//    this.config.contactsStore.on("datachanged",function(){
 //       if(this.itemCount){
  //          this.itemCount.el.innerHTML=this.config.contactsStore.getCount()+' ' + LANG_items
   //     }
        
  //  },
  //  this);

    this.contactsView=new Ext.DataView({
        id:"dataviewContacts",
		loadingText:LANG_loading+ '...',
		singleSelect:true,
		cls:"ychooser-view",
	//	store:this.config.contactsStore,
		emptyText:'<div style="padding:10px;">'+LANG_no_contacts_to_display +'</div>',
		tpl:this.tplBusinessCard,
		itemSelector:"div.thumb-wrap",
		selectedClass:"x-box-blue",
		border:true,
		stateful:true
 /*   	plugins:[new NGMP.dataview.Search({
            mode:"local",
			minLength:2,
			tb:this.tb,
			checkIndexes:["name"],
			width:200,
			dataIndexes:[
			{name:"Name",dataIndex:"name"},
            {name:"Email",dataIndex:"email"},
			{name:"Address",dataIndex:"street"},
			{name:"Town/City",dataIndex:"city"},
			{name:"Country",dataIndex:"state"},
			{name:"Phone No.",dataIndex:"work"}
            ],align:"right"
        }
        )] */
    });
    this.contactsView.prepareData=function(B){
        B.shortemail=Ext.util.Format.ellipsis(B.email,15);
        return B;
    };
    this.contactsView.on("dblclick",function(F,B,C,D){
        var G="contact_"+B;
        var E=Ext.WindowMgr.get(G);
        if(E){
            Ext.WindowMgr.bringToFront(E);
        }
        else{
            E=new NGMP.addressbook.ContactWindow({id:D.selModel.selections.keys[0]});
            E.render(this.body);
            E.show();
        }
        
    },this);
    this.contactsView.on("selectionchange",function(D,B){
        var C=this.getTopToolbar().items;
        if(B.length> 0){
            C.get("tbbuttonDeleteContact").enable();
        } else{
            C.get("tbbuttonDeleteContact").disable();
        }
        
    },this); 
    NGMP.addressbook.ContactsPanel.superclass.constructor.call(this,{
        id:"panelContacts",
		autoShow:true,
		title:LANG_contacts,
		border:true,
		stateful:true,
		tbar:this.tb,
		bbar:this.btb,
	    items:this.contactsView,
		bodyStyle:"padding:5px;background-color: transparent"
    });
    this.on("show",function(){
        this.cascade(function(B){
            B.fireEvent("shown",this);
        });
    },this);
    this.btb.on("render",function(){
//        this.itemCount=this.btb.addText(this.config.contactsStore.getCount()+" Items");
        this.btb.addSeparator();
    },this);
};
Ext.extend(NGMP.addressbook.ContactsPanel,Ext.Panel,{
    showBusiness:function(){
        if(this.contactsView.rendered){
            this.contactsView.tpl=this.tplBusinessCard;
            this.contactsView.refresh();
        }
        
    },
    showAddress:function(){
        if(this.contactsView.rendered){
            this.contactsView.tpl=this.tplAddressCard;
            this.contactsView.refresh();
        }
        
    }
    
});
NGMP.addressbook.ContactsAccordionPanel=function(){
    this.addEvents({
        business:true,address:true
    });
    this.radioBusiness=new Ext.form.Radio({
        id:"radioBusiness",
		name:"businessView",
		boxLabel:LANG_business_cards,
		checked:true,
		hideLabel:true
    });
    this.radioBusiness.on("check",function(A,B){
        if(B){
            this.radioAddress.setValue(false);
        }
  //      this.fireEvent("business",this)
    },this);
    this.radioAddress=new Ext.form.Radio({
        id:"radioAddress",
		name:"addressView",
		boxLabel:LANG_address_cards,
		checked:false,
		hideLabel:true
    });
    this.radioAddress.on("check",function(A,B){
        if(B){
            this.radioBusiness.setValue(false);
        }
   //     this.fireEvent("address",this)
    },this);
    NGMP.addressbook.ContactsAccordionPanel.superclass.constructor.call(this,{
        id:"panelContactsAccordionPanel",
		title:LANG_contacts,
		iconCls:"icon-contacts",
		autoShow:true,
		hideMode:"visibility",
		bodyStyle:"background-color: white",
		items:{
            xtype:"panel",
			title:LANG_current_view,
			collapsible:true,
			autoHeight:true,
			border:true,
			layout:"form",
			frame:true,
			bodyStyle:"background-color:#ffffff;padding:5px",
			items:[this.radioBusiness,this.radioAddress]
        }
        
    });
};
Ext.extend(NGMP.addressbook.ContactsAccordionPanel,Ext.Panel);

NGMP.addressbook.AddressBookWindow = function(A) {
	this.config = A || {};

	this.addEvents({
		saved: true,
		show: true
	});

	this.tabAddressbookProperties = new Ext.FormPanel({
		name: 'properites',	
		title:LANG_properties,	
		labelWidth: 85,
		defaultType: 'textfield',
		border: false,
		defaults:{anchor:'100%'},
		cls:'ngmp-form-panel',
		items:[{
			fieldLabel: LANG_name,
			name: 'name',
			value: this.config.abook_name, 
			allowBlank: false
		}]
	});

	this.buttonHandler = function(A) {
		switch (A.id) {
			case 'ok':
				switch (this.mainTabPanel.activeTab.name) {
					case 'properites':
						this.tabAddressbookProperties.getForm().submit({
							url:'ngmp.php',
							params:{
								func: 'abSaveAddressBook',
								id: this.config.abook_id
							},
							waitMsg:LANG_saving_addressbook+'...',
							success:function(C,D) {
								this.fireEvent('saved',this);
							//	Ext.Msg.alert(LANG_save_addressbook, LANG_addressbook_successfully_saved);
								this.close();
							},
							failure:function(CD) {
								Ext.Msg.alert(LANG_error_saving_addressbook, LANG_unable_to_save_addressbook);
								this.close();
							},
							scope:this
						});
						break;
					default:
						break;
				}
				break;
			case 'close':
				this.close();
				break;
			default:
				break;
		}
	};

	this.buttons = [{
		id: 'upload',
		text: LANG_upload,
		handler: this.buttonHandler,
		hidden: true,
		scope: this
	}, {
		id: 'export',
		text: LANG_export,
		handler: this.buttonHandler,
		hidden: true,
		scope: this
	}, {
		id:'ok',
	   text: LANG_ok,
	   handler: this.buttonHandler,
	   scope: this
	}, {
		id: 'close',
		text: LANG_close,
		handler: this.buttonHandler,
		scope: this
	}];

	this.on('show', function() {
		if (this.config.abook_id > 0) {
		}
	},this);

	this.mainTabPanel = new Ext.TabPanel({
		activeTab: 0,
		items:[
			this.tabAddressbookProperties
		]
	});

    NGMP.addressbook.AddressBookWindow.superclass.constructor.call(this,{
		id:'addressbook-window-edit-addressbook',
		layout:'fit',
		modal: true,
		shadow: false,
		height:450,
		width:550,
		plain: true,
		title: LANG_edit_addressbook,
		enableTabScroll: true,
		items:[this.mainTabPanel]
	});
};
Ext.extend(NGMP.addressbook.AddressBookWindow, Ext.Window);

NGMP.addressbook.MailTemplateWindow = function(A) {
	this.config = A || {};

	this.tabMailTemplateProperties = new Ext.Panel({
		title: LANG_properties
	});

	NGMP.addressbook.MailTemplateWindow.superclass.constructor.call(this, {
		id:'addressbook-window-edit-mailtemplate',
		layout:'fit',
		modal: true,
		shadow: false,
		height:450,
		width:550,
		plain: true,
		title: LANG_edit_mail_template,
		enableTabScroll: true,
		items:[
			new Ext.TabPanel({
				activeTab: 0,
				items:[
					this.tabMailTemplateProperties,
					new NGMP.grid.PermissionsPanel({
						type: 'READ'
					}),
					new NGMP.grid.PermissionsPanel({
						type: 'WRITE'
					})
				]
			})
		]
	});
};
Ext.extend(NGMP.addressbook.MailTemplateWindow, Ext.Window);

NGMP.addressbook.OOTemplateWindow = function(A) {
	this.config = A || {};

	this.tabOOTemplateProperties = new Ext.Panel({
		title: LANG_properties
	});

	NGMP.addressbook.OOTemplateWindow.superclass.constructor.call(this, {
		id:'addressbook-window-edit-ootemplate',
		layout:'fit',
		modal: true,
		shadow: false,
		height: 450,
		width:550,
		plain: true,
		title: LANG_edit_mail_template,
		enableTabScroll: true,
		items:[
			new Ext.TabPanel({
				activeTab: 0,
				items:[
					this.tabOOTemplateProperties,
					new NGMP.grid.PermissionsPanel({
						type: 'READ'
					}),
					new NGMP.grid.PermissionsPanel({
						type: 'WRITE'
					})
				]
			})
		]
	});
};
Ext.extend(NGMP.addressbook.OOTemplateWindow, Ext.Window);

NGMP.addressbook.MailingWindow = function(A) {
	this.config = A || {};
	
	this.tabMailingProperties = new Ext.Panel({
		title: LANG_properties
	});

	this.tabUsers = new Ext.Panel({
		title: LANG_users
	});

	this.tabContacts = new Ext.Panel({
		title: LANG_contacts
	});

	this.tabCompanies = new Ext.Panel({
		title: LANG_companies
	});

	NGMP.addressbook.MailingWindow.superclass.constructor.call(this, {
		id:'addressbook-window-edit-mailing',
		layout:'fit',
		modal: true,
		shadow: false,
		height: 450,
		width: 550,
		plain: true,
		title: LANG_edit_mailing,
		enableTabScroll: true,
		items: [
			new Ext.TabPanel({
				activeTab: 0,
				items:[
					this.tabMailingProperties,
					this.tabUsers,
					this.tabContacts,
					this.tabCompanies,
					new NGMP.grid.PermissionsPanel({
						type: 'READ'
					}),
					new NGMP.grid.PermissionsPanel({
						type: 'WRITE'
					})
				]
			})
		]
	});
};
Ext.extend(NGMP.addressbook.MailingWindow, Ext.Window);

NGMP.addressbook.ManageDialog = function(A) {
    this.config=A||{
    };
	this.myAddressbooks = new Ext.data.JsonStore({
		url:'ngmp.php',
		baseParams:{
			func:'abGetMyAddressBooks'
		},
		root:'results',
		id:'id',
		fields:[
			{name:'id', type:'int'},
			{name:'name', type:'string'},
			{name:'default', type:'boolean'}
		]
	});
	this.addressbooksGrid = new Ext.grid.GridPanel({
		layout: 'fit',
		paging: true,
		border:false,
		id:'ab-addressbook-grid',
		store: this.myAddressbooks,
		view: new Ext.grid.GridView({
			autoFill: true,
			forceFit: true
		}),
		cm: new Ext.grid.ColumnModel([
			{id:'name', header: LANG_name, dataIndex:'name', width:200, sortable: true},
			new NGMP.grid.CheckColumn({
				id:'default',
				header: LANG_lang_default,
				dataIndex:'default',
				width:30
			})
		]),
		sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
		autoScroll: false,
		trackMouseOver: true,
		enableDragDrop: false,
		collapsible: false
	});
	this.addressbooksGrid.on('rowdblclick', function() {
		var abook_id = this.addressbooksGrid.getSelectionModel().getSelected().get('id');
		var abook_name = this.addressbooksGrid.getSelectionModel().getSelected().get('name');
		var B = new NGMP.addressbook.AddressBookWindow({abook_id: abook_id, abook_name: abook_name});
		B.show();
	},this);
	this.addressbookPanelTbar = [{
		id:'add-addressbook',
	 	iconCls:'icon-add',
		text:LANG_add_addressbook,
		cls:'x-btn-text-icon',
		handler: function(){
			var B = new NGMP.addressbook.AddressBookWindow();
			B.on('saved', function() {
				this.myAddressbooks.reload();
			},this);
			B.show();
		},
		scope: this
	}, {
		id:'delete-addressbook',
		iconCls:'icon-delete',
		text:LANG_delete_addressbook,
		cls:'x-btn-text-icon',
		handler: function() {
			if (this.addressbooksGrid.selModel.selections.keys.length === 1 ){
				this.myAddressbooks.baseParams.delete_id = this.addressbooksGrid.selModel.selections.keys[0];
				this.myAddressbooks.reload();
				delete this.myAddressbooks.delete_id;
			}
		},
		scope: this
	},{
		iconCls:'icon-default',
		text:LANG_set_as_default,
		cls:'x-btn-text-icon',
		handler:function() {
			if (this.addressbooksGrid.selModel.selections.keys.length === 1 ){
				this.myAddressbooks.baseParams.default_id = this.addressbooksGrid.selModel.selections.keys[0];
				this.myAddressbooks.reload();
				delete this.myAddressbooks.default_id;
			}
		},
		scope:this
	}];
	this.addressbookPanel = new Ext.Panel({
		name:'ab_addressbook_panel',
		title: LANG_addressbook,
		layout: 'fit',
		border: false,
		tbar: this.addressbookPanelTbar,
		items: [this.addressbooksGrid]
	});

	this.tabPanel = new Ext.TabPanel({
		activeTab: 0,
		border:false,
		items:[
			this.addressbookPanel
		]
	});

	this.tabPanel.on('tabchange', function(A,B) {
		switch (B.name) {
			case 'ab_addressbook_panel':
				this.myAddressbooks.reload();
				break;
			default:
				break;
		}
	},this);

    NGMP.addressbook.ManageDialog.superclass.constructor.call(this,{
		id:'addressbook-window-manage',
		layout: 'fit',
		modal: true,
		shadow: false,
		border: false,
		height: 450,
		width:800,
		plain: true,
		iconCls:'icon-addressbook-manage',
		title:LANG_manage,
		items:[this.tabPanel],
		buttons: [{
			id:'close',
			text:LANG_close,
			handler: function() {
				this.close();
			},
			scope:this
		}]
	});
};

Ext.extend(NGMP.addressbook.ManageDialog, Ext.Window, {
	show:function() {
		this.myAddressbooks.reload();
		NGMP.addressbook.ManageDialog.superclass.show.call(this);
	}
});

NGMP.addressbook.AddressBookModule = function(A) {
	if (!A) {
		A = {};
	}
	this.contact = null;

	this.contactsJsonReader = new Ext.data.JsonReader({
		root:'results',
		totalProperty: 'total',
		id:'id',
		fields:[
			{name:'ab_id', type:'int'},
			{name:'id', type:'int'},
			{name:'fullname', type:'string'},
			{name:'email', type:'string'},
			{name:'company', type:'string'},
			{name:'mobile', type:'string'},
			{name:'phone_work', type:'string'}
		]
	});

	this.contactsStore = new Ext.data.GroupingStore({
		baseParams: {
			func: 'abGetContactSummaryList'
		},
		proxy: new Ext.data.HttpProxy({
			url: 'ngmp.php'
		}),
		reader: this.contactsJsonReader
	});

	this.company = null;

	this.companiesJsonReader = new Ext.data.JsonReader({
		root:'results',
		totalProperty: 'total',
		id:'id',
		fields: [
			{name:'id', type:'int'},
			{name: 'ab_id', type:'int'},
			{name:'name', type:'string'},
			{name:'email', type:'string'},
			{name:'phone', type:'string'},
			{name:'fax', type:'string'}
		]
	});

	this.companiesStore = new Ext.data.GroupingStore({
		baseParams: {
			func: 'abGetCompanySummaryList'
		},
		proxy: new Ext.data.HttpProxy({
			url:'ngmp.php'
	   	}),
		reader: this.companiesJsonReader
	});
	this.alphabetArray=LANG_alphabet.split(',');
	var F = new Array();
	F.push({value:"[0-9]"});
	for (var D=0;D<this.alphabetArray.length;D++) {
		F.push({value:this.alphabetArray[D]});
	}

	var B = new Ext.XTemplate(
			"<tpl for='.'><span class='letter' uuups=''>{value}</span></tpl>");

			  
	this.alphabetView = new Ext.DataView({
		height: 30,
		text:'asdfasdf',
		store: new Ext.data.JsonStore({
			fields:['value'],
			data:F
		}),
		tpl:B,
		autoHeight: true,
		singleSelect: true
	});
	this.alphabetView.on('selectionchange', function(I,H) {
			if (H[0]) {
//				console.log(H[0].textContent);
			}
	},this);
	this.north = new Ext.Panel({
		region:'north',
		border: false,
		height: 73,
		defaults: {
			border: false
		},
		items:[this.alphabetView]
	});

	this.companiesCM = new Ext.grid.ColumnModel(
			[{
				id:'name', 
				dataIndex:'name', 
				width:300, 
				sortable: true,
				header:LANG_company_name
			},{
				id:'email',
				dataIndex:'email',
				width:200,
				sortable: true,
				header:LANG_email
			},{
				id:'phone',
				dataIndex:'phone',
				width:100,
				sortable: true,
				header: LANG_phone
			},{
				id:'fax',
				dataIndex:'fax',
				width:100,
				sortable: true,
				header: LANG_fax
			}]);

	this.contactsCM = new Ext.grid.ColumnModel([
		{header:LANG_real_name, dataIndex: 'fullname', width:130, sortable:true},
		{header:LANG_company, dataIndex:'company', width:300, sortable:true},
		{header:LANG_email, dataIndex:'email', width:160, sortable: true},
		{header:LANG_mobile, dataIndex:'mobile', width:130, sortable: true},
		{header:LANG_phone_work, dataIndex:'phone_work', width:130, sortable: true}
	]);
	this.contactsGrid = new Ext.grid.GridPanel({
		layout: 'fit',
		region:'center',
		paging: true,
		id: 'addressbook-contacts-gridpanel',
		cm: this.contactsCM,
		store: this.contactsStore,
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		autoScroll:false,
		collapsible: false,
		loadMask: {
			msg: LANG_loading_contacts
		},
		bbar: new Ext.PagingToolbar({
			pageSize: 1,
			store: this.contactsStore,
			displayInfo: true,
			displayMsg: LANG_displaying_contacts + ' {0} - {1} @ {2}',
			emptyMsg: LANG_there_are_no_contacts_to_display
	 	})
	});
	this.contactsEastPanel = new NGMP.addressbook.ContactReadPanel();

	this.companiesGrid = new Ext.grid.GridPanel({
		layout: 'fit',
		region: 'center',
		paging: true,
		id: 'addressbook-companies-gridpanel',
		store: this.companiesStore,
		cm: this.companiesCM,
		sm: new Ext.grid.RowSelectionModel({singleSelect:false}),
		autoScroll: false,
		autoExpandColumn: 'name',
		autoExpandMax: 2500,
		collapsible: false,
		loadMask: {
			msg: LANG_loading_companies
		},
		bbar: new Ext.PagingToolbar({
			id: 'pagingAddressBookCompanies',
			pageSize: 1,
			store: this.companiesStore,
			displayInfo: true,
			displayMsg: LANG_displaying_companies + ' {0} - {1} @ {2}',
			emptyMsg: LANG_there_are_no_companies_to_display
	 	})
	});
	

	this.companiesEastPanel = new NGMP.addressbook.CompanyReadPanel();

	this.contactsPanel = new Ext.Panel({
		id:'tabpanelAddressbookContacts',
		iconCls:'icon-contact',
		title: LANG_contacts,
	    layout:'border',
	    items: [this.contactsGrid,this.contactsEastPanel]
	});
	this.contactsPanel.on('show', this.onPanelShow, this);
	this.companiesPanel = new Ext.Panel({
		id:'tabpanelAddressbookCompanies',
		iconCls:'icon-company',
		title: LANG_companies,
	    layout:'border',
	   	items: [this.companiesGrid,this.companiesEastPanel]
	});
	this.companiesPanel.on('show', this.onPanelShow,this);	
	this.tabPanel = new Ext.TabPanel({
		region:'center',
		autoShow: true,
		activeTab: 0,
		border: true,
		plain:true,
		enableTabScroll: false,
		hideMode: 'offsets',
        items:[this.contactsPanel, this.companiesPanel]
	});
	this.buttonHandler = function(Z) {
			var B;
			switch(Z.id) {
				case 'btnAddContact':
					B = new NGMP.addressbook.ContactWindow({id:0});
					B.on('saved', function(F) {
						this.contactsStore.reload();	
					},this);
					B.render(this.ownerCt.body);
					B.show();
					this.tabPanel.setActiveTab(this.contactsPanel);
					break;
				case 'btnAddCompany':
					B = new NGMP.addressbook.CompanyWindow({id:0});
					B.on('saved', function(F) {
						this.companiesStore.reload();
					}, this);
					B.render(this.ownerCt.body);
					B.show();
					this.tabPanel.setActiveTab(this.companiesPanel);	
					break;
				case 'btnAddressbookManage':
					B = new NGMP.addressbook.ManageDialog();
					B.render(this.ownerCt.body);
					B.show();
					break;
				case 'btnDelete':
					switch (this.tabPanel.activeTab) {
						case this.companiesPanel:
							this.companiesStore.baseParams.delete_id = this.companiesGrid.selModel.selections.keys[0];
							this.companiesStore.reload();
							delete this.companiesStore.baseParams.delete_id;
							break;
						case this.contactsPanel:
							this.contactsStore.baseParams.delete_id = this.contactsGrid.selModel.selections.keys[0];
							this.contactsStore.reload();
							delete this.contactsStore.baseParams.delete_id;
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}
	};

	this.tbar = new Ext.Toolbar({
		id: 'toolbarAddressbookTop',
		border: false,
		items:[
			{
				id: 'btnAddContact',
				iconCls:'icon-add-contact',
				text: LANG_add_contact,
				cls:'x-btn-text-icon',
				handler: this.buttonHandler,
				scope: this
			},
			{
				id: 'btnAddCompany',
				iconCls:'icon-addressbook-company',
				text: LANG_add_company,
				cls:'x-btn-text-icon',
				handler: this.buttonHandler,
				scope: this
			},
			{
				id: 'btnAddressbookManage',
				iconCls:'icon-addressbook-manage',
				text: LANG_manage,
				cls:'x-btn-text-icon',
				handler: this.buttonHandler,
				scope: this
			},
			{
				id: 'btnDelete',
				iconCls:'icon-delete',
				text: LANG_delete,
				cls:'x-btn-text-icon',
				handler: this.buttonHandler,
				scope: this
			}
		]
	});
	this.companiesGrid.getSelectionModel().on('selectionchange', this.selectionChange,this);
	this.contactsGrid.selModel.on('selectionchange', this.selectionChange, this);
	this.companiesGrid.on('rowdblclick', this.rowDoubleClick, this);
	this.contactsGrid.on('rowdblclick', this.rowDoubleClick,this);	
	A.items = [this.tabPanel];
		
	NGMP.addressbook.AddressBookModule.superclass.constructor.call(this,A);
};

Ext.extend(NGMP.addressbook.AddressBookModule,Ext.Panel, {
	afterRender: function (){
		NGMP.addressbook.AddressBookModule.superclass.afterRender.call(this);
	},
	onPanelShow: function(B) {
			switch (B.id) {
				case 'tabpanelAddressbookCompanies':
					this.companiesStore.reload({
						params: {
							start: 0,
							limit: 1
						}
					});
					break;
				case 'tabpanelAddressbookContacts':
					this.contactsStore.reload({
						params:{
							start:0,
							limit:1
						}
						});
					break;
				default:
					break;
			}
	},
	rowDoubleClick: function(C) {
		var B;
		switch (C.id) {
			case 'addressbook-companies-gridpanel':
				B = new NGMP.addressbook.CompanyWindow({id:this.companiesGrid.selModel.selections.keys[0]});
				B.on('saved', function(F) {
					this.companiesStore.reload();
				}, this);
				B.render(this.ownerCt.body);
				B.show();
				break;
			case 'addressbook-contacts-gridpanel':
				B = new NGMP.addressbook.ContactWindow({id:this.contactsGrid.selModel.selections.keys[0]});
				B.on('saved', function(F) {
					this.contactsStore.reload();
				},this);
				B.render(this.ownerCt.body);
				B.show();
				break;
			default:
				break;
		}
	},
	selectionChange: function(D) {
		switch (this.tabPanel.activeTab.id) {
			case 'tabpanelAddressbookCompanies':
				if (!this.companiesEastPanel.collapsed && D.getCount() == 1) {
					Ext.Ajax.request({
						url:'ngmp.php',
						params:{
							func:'abGetCompany',
							id: this.companiesGrid.selModel.selections.keys[0]
						},
						method: 'POST',
						success: function(F, G) {
							this.company = Ext.util.JSON.decode(F.responseText);
							this.companiesEastPanel.setData(this.company.data);
						},
						failure: function(F,G) {
							//console.log('failure');
						},
						scope: this
					});
				}
				break;
			case 'tabpanelAddressbookContacts':
				if (!this.contactsEastPanel.collapsed && D.getCount() == 1) {
					Ext.Ajax.request({
						url:'ngmp.php',
						params:{
							func:'abGetContact',
							id: D.getSelected().get('id')
						},
						method: 'POST',
						success: function(F, G) {
							this.contact = Ext.util.JSON.decode(F.responseText);
							this.contactsEastPanel.setData(this.contact.data);
						},
						failure: function(F,G) {
						//	console.log('failure');
						},
						scope: this
					});
				}
				break;
			default:
				break;
		}
	}
	
});

NGMP.moduleManager.addModule('addressbook', NGMP.addressbook.AddressBookModule, {
	frame: false,
   	title:LANG_addressbook,
	iconCls:'icon-addressbook',
	id: 'addressbookModule',
	closable: false,
	layout:'border',
	border: false
});
