//THIS FILE CONTAINS ALL THE COMPONENTS THAT WEB INTERFACE REQUIRE TO 
// - EDIT THE NEWS

//CLASS NEWS EDITOR

function LanguageEditor(menuTreeURL, flagsURL, renderTo){
	
	var saveButtonText = getLanguageForElement("saveButtonText", selectedLangId);
	var deleteButtonText = getLanguageForElement("deleteButtonText", selectedLangId);		
	var updateButtonText = getLanguageForElement("updateButtonText", selectedLangId);		
	var languageEditorDescriptionLabelText = getLanguageForElement("languageEditorDescriptionLabelText", selectedLangId);
	var languageEditorEditorTitleText = getLanguageForElement("languageEditorEditorTitleText", selectedLangId);
	
	this.selectedLanguage = null;

	/**
	 * TREE TO DISPLAY ALL THE LANGUAGES
	 */
	this.showLanguageTree = new createNavMenuXmlTree(
								'language_display_tree', 
								menuTreeURL,
								'All_languages',
								'languageCategory',
								'language',
								Ext.emptyFn,
								function(node, tree, e){
									var scope = tree.scope;									
									ajaxSendReadLanguage(node.attributes.nodeId, scope);		
								},
								this	//scope to update the gui fields
							);  

	//image and category fields selectors are mandatory
	//combo boxes for selecting the icons
	this.selectFlagIcons = new GmSoft.components.ImageSelector({
	    iconsURL: flagsURL,
		comboID: 'edit_language_flag_chooser',
		defaultImage: './img/empty.gif',
		comboWidth: 150
	});

	/**
	 * PANEL TO DISPLAY THE TEXT FIELD OF DESCRIPTION
	 */
	this.descriptionField = getTextFieldComponent('language_description', languageEditorDescriptionLabelText, 'description',150);

	//buttons of the panel
	//Save button
	this.edit_LanguageButtonSave = new Ext.Button(
		{
			text: saveButtonText,
			scope: this,
			minWidth: 100,			
			xtype: 'button',
			handler: function(This,e){
				//check if required field have value				
				var description = this.descriptionField.getRawValue();
				var image = this.selectFlagIcons.getCombo().getValue();
				ajaxSendNewLanguage(description,image, this);	
	        }				
		}
	);
	
	//update button
	this.edit_LanguageButtonUpdate = new Ext.Button({
			text: updateButtonText,
			xtype: 'button',
			minWidth: 100,		
			disabled: true,	
	        scope: this,			
	        handler: function(This,e){
					if(this.selectedLanguage != null){
						var description = this.descriptionField.getRawValue();
						var image = this.selectFlagIcons.getCombo().getValue();
						//inform about what is going to be done
				        ajaxSendUpdateLanguage(this.selectedLanguage, description, image, this);
					}else{
						showInfoDialog('info', 'no item selected');
					}
	        }			
	});	

	//delete button
	this.edit_LanguageButtonDelete = new Ext.Button({
			text: deleteButtonText,
			xtype: 'button',
			disabled: true,
			minWidth: 100,
	        scope: this,
	        handler: function(This, e){				
					if(this.selectedLanguage != null){
						//inform about what is going to be done
						Ext.MessageBox.confirm('delete language confirmation',
							'If you delete a language all language fields from any item will be deleted, do you want to proced?',
							function(btn, text){
							    if (btn == 'yes'){
							        ajaxSendDeleteLanguage(this.selectedLanguage,this);
							    }
							},this);						
					}else{
						showInfoDialog('info', 'no item selected');
					}
	        }	
	});

	//functions to enable and disable buttons
	this.enableUpdateDeleteLanguageButtons = function(){
		this.edit_LanguageButtonUpdate.enable();
		this.edit_LanguageButtonDelete.enable();	
	}
	this.disableUpdateDeleteLanguageButtons = function(){
		this.edit_LanguageButtonUpdate.disable();
		this.edit_LanguageButtonDelete.disable();
	}	

	this.descriptionPanel = new Ext.form.FormPanel({
			xtype:'form',	
			id: 'LanguageMenuDescriptionField',
	        width: 350,	
			autoHeight : true,
			bodyBorder: false,
			border: false,
			bodyStyle:'padding:0px 0px 0px 20px',		
			items: [this.descriptionField, this.selectFlagIcons],
			buttons: [this.edit_LanguageButtonSave,this.edit_LanguageButtonUpdate,this.edit_LanguageButtonDelete]		
	});

	/**
	 * MAIN PANEL THAT CONTAINS THE FORM TO CREATE AND DISPLAY THE NEWS AND THE TREE
	 */
	this.border = new Ext.Panel({
	    title: languageEditorEditorTitleText,
		id: 'Edit_LanguageMenu_ID',	
	    layout:'column',
		border:'false',
		bodyBorder: false,
		bodyStyle:'padding:20px 20px 20px 20px; text-align: left;',	
	    width: 540,
		height : 200,
	    items: [{
			xtype:'form',	
			id: 'LanguageMenuTreePanel',
	        width: 120,	
			autoHeight : true,
			bodyBorder: false,
			border: false,		
			items: [this.showLanguageTree.getTree()]	
	    }, this.descriptionPanel]
	});
		
	
	this.border.render(renderTo);
	//load the tree
	this.showLanguageTree.loadTree();

}

//request language information
function ajaxSendReadLanguage(id,thisScope){
    Ext.Ajax.request({
        url: 'XmlGMServlet?action_type=language_actions&action=get_language',
        success: function(conn, response, options){
			var scope = response.scope;
			mapLanguageToForm(conn.responseXML,scope); //map fields and enable buttons update and delete
        },
        failure: function(){
            showErrorDialog('Error', 'could not connect to server');
        },
        scope: thisScope,
		params: 
			{
				id: id
			}	
    });
}

//Create new language
function ajaxSendNewLanguage(description, flag, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=language_actions&action=add_language',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if (failure != null) { //in case there was an error show it
							showErrorDialog('ERROR!!', failure);
						}else {
							var scope = response.scope;
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showLanguageTree.loadTree();
							scope.disableUpdateDeleteLanguageButtons();
						}
					},
					thisScope,
					{
						description: description,
						flag: flag
					}
	);
}

//Update a language
function ajaxSendUpdateLanguage(id, description, flag, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=language_actions&action=update_language',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if (failure != null) { //in case there was an error show it
							showErrorDialog('ERROR!!', failure);
						}else {
							var scope = response.scope;
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showLanguageTree.loadTree();
							scope.disableUpdateDeleteLanguageButtons();
						}
					},
					thisScope,
					{
						id: id,
						description: description,
						flag: flag						
					}
	);
}

//delete language
function ajaxSendDeleteLanguage(id, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=language_actions&action=delete_language',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if (failure != null) { //in case there was an error show it
							showErrorDialog('ERROR!!', failure);
						}else {
							var scope = response.scope;
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showLanguageTree.loadTree();
							scope.disableUpdateDeleteLanguageButtons();
						}
					},						
					thisScope,
					{
						id: id
					}
	);
}

function mapLanguageToForm(doc, scope){
	var description = findDirectTextNodeValue(doc.getElementsByTagName('name')[0]);
	if (description != '') {
		scope.descriptionField.setValue(description);
	}
	
	//read and load image icon
	var image = findAttributValueInNode(doc.getElementsByTagName("language")[0],"flag");
	if (image != ''){ 
		scope.selectFlagIcons.selectImage(image);
	}	
	
	var id = findDirectTextNodeValue(doc.getElementsByTagName('id')[0]);
	if (id != '') {
		scope.selectedLanguage = id;
		scope.enableUpdateDeleteLanguageButtons();
	}else{
		showErrorDialog('ERROR!','server response not valid');
	}
	
}

