/**
 * @author albert.sole
 * THIS FILE CONTAINS ALL THE COMPONENTS THAT WEB INTERFACE REQUIRE TO 
 * - RENDER THE QUICK LINK EDITOR
 */ 

function ProductEditor(iconsURL, categoriesURL, technical_documentURL, productsMenu, renderTo, languages){

		var productsEditorEditorTitleText = getLanguageForElement("productsEditorEditorTitleText", selectedLangId);
		var productsEditorProductIDLabelText = getLanguageForElement("productsEditorProductIDLabelText", selectedLangId);
		var productsEditorTechDocLabelText = getLanguageForElement("productsEditorTechDocLabelText", selectedLangId);
		var productsEditorDisplayOrderLableText = getLanguageForElement("productsEditorDisplayOrderLableText", selectedLangId);

		var config = {
			globalParameters: {
				renderTo: renderTo,
				formTitle: productsEditorEditorTitleText,
				categoriesURL: categoriesURL,
				iconsURL: iconsURL,
				defaultIconImage: './img/icons/blank-48x48.gif',
				languages: languages
			},
			navigationTreeConfig: {				
				treeID: 'tree',
				treeRootNode: 'All_products',
				treeRequestDataURL: productsMenu,
				treeDoubleClickCategoryFunction: function(node, tree, e){ //function that will be executed on category double click
					var scope = tree.scope;
					ajaxRequestProductCategory(node.attributes.nodeId, scope);
				},
				treeDoubleClickItemFunction: function(node, tree, e){ //function that will be executed on item double click
					var scope = tree.scope;
					ajaxRequestProductItem(node.attributes.nodeId, scope);
				}
			},
			itemEditor: {
				displayCategory : true,
				specificComponents:{
						product_id: getTextFieldComponent('product_id', productsEditorProductIDLabelText, 'product_id',250),
						//tecnical_doc: getTextFieldComponent('product_tecnical_doc', productsEditorTechDocLabelText, 'product_tecnical_doc',200),
						tecnical_doc: new GmSoft.components.ComboSelector(
									{
								  		comboId: 'edit_product_itemsProduct_tecnical_doc',
								  		label: productsEditorTechDocLabelText,
								  		queryField: 'file',
								  		mappingElement: 'url',
								  		queryURL: technical_documentURL,
								  		functionToExecOnSelection: Ext.emptyFn,
										comboWidth: 250,
								  		scope: this 
									}),
						itemOrder: getNumberFieldComponent('product_order',productsEditorDisplayOrderLableText,'product_order',250),
						softwareInfo: new GmSoft.components.ProductSoftwareDescriptor({})
				},			
				updateComponentsFromXML: function(xmlDoc, components){		
					//select the destination value from the combo
					//read and load the order field
					var order = findDirectTextNodeValue(xmlDoc.getElementsByTagName('order')[0]);
					components.itemOrder.setValue(order);
					var product_id = findDirectTextNodeValue(xmlDoc.getElementsByTagName('product_id')[0]);
					components.product_id.setValue(product_id);
					var technical_doc = findDirectTextNodeValue(xmlDoc.getElementsByTagName('technical_document')[0]);
					//components.tecnical_doc.setValue(technical_doc);
					components.tecnical_doc.setComboValue(technical_doc);				
					
					var is_software = findDirectTextNodeValue(xmlDoc.getElementsByTagName('is_software')[0]);
					if (is_software) {
						components.softwareInfo.setIsSoftwareValue('true');
					}else{
						components.softwareInfo.setIsSoftwareValue('false');
					}
				},
				send: function(image, category, content, specificComponents, scope){
					//get specific component values
					var product_id = specificComponents.product_id.getValue();
					//var technical_doc = specificComponents.tecnical_doc.getValue();				
					var technical_doc = specificComponents.tecnical_doc.getCombo().getValue();
					var order = specificComponents.itemOrder.getValue();
					var isSoftware = specificComponents.softwareInfo.getIsSoftwareValue();
					//send the request				
					ajaxSendNewProduct(image, category, product_id, technical_doc, order, isSoftware, content, scope);
				},
				update: function(id, image, category, content, specificComponents, scope){
					//get specific component values
					var product_id = specificComponents.product_id.getValue();
					//var technical_doc = specificComponents.tecnical_doc.getValue();				
					var technical_doc = specificComponents.tecnical_doc.getCombo().getValue();
					var order = specificComponents.itemOrder.getValue();
					var isSoftware = specificComponents.softwareInfo.getIsSoftwareValue();
					//send the request				
					ajaxSendUpdateProduct(id,image, category, product_id, technical_doc, order, isSoftware, content, scope);				
				},
				remove: function(selectedItem, specificComponents, scope){
					ajaxSendDeleteProduct(selectedItem, scope);
				}				
			},
			categoryEditor: {
				specificComponents:{
					nestedCategory : new GmSoft.components.CategoryEditor({
						comboID: 'product_category_category_editor',
						categoryURL: categoriesURL,
						allowedCategories: 1
					}),
					categoryOrder: getNumberFieldComponent('product_category_order',productsEditorDisplayOrderLableText,'product_category_order',250)			
				},
				updateComponentsFromXML: function(xmlDoc, components){
					//select the destination value from the combo
					//read and load the order field
					var order = findDirectTextNodeValue(xmlDoc.getElementsByTagName('order')[0]);
					components.categoryOrder.setValue(order);	
					
					//update the categories
					var categoryNodes = xmlDoc.getElementsByTagName("categories")[0];
					if (categoryNodes != null) {
						var nodeChilds = categoryNodes.childNodes;
						var i = 0;
						var idArray = new Array(nodeChilds.length);
						var nameArray = new Array(nodeChilds.length);
						for (i = 0; i < nodeChilds.length; i++) {
							idArray[i] = findDirectTextNodeInsideTagValue(nodeChilds[i], 'id');
							nameArray[i] = findDirectTextNodeInsideTagValue(nodeChilds[i], 'name');
						}
						components.nestedCategory.setCategoryRecords(idArray, nameArray);
					}
				},
				send: function(image, content, specificComponents, scope){
					var order = specificComponents.categoryOrder.getValue();
					var categories = specificComponents.nestedCategory.getXMLSelectedCategoriesID();
					//send ajax request	
					ajaxSendNewProductCategory(image, content, categories, order, scope);				
				},
				update: function(id, image, content, specificComponents, scope){
					var order = specificComponents.categoryOrder.getValue();
					var categories = specificComponents.nestedCategory.getXMLSelectedCategoriesID();
					//send ajax request	
					ajaxSendUpdateProductCategory(id, image, content, categories, order, scope);				
				}, 				
				remove: function(selectedItem, specificComponents, scope){
						ajaxSendDeleteProductCategory(selectedItem, scope);
				}
			}
		};
		var the_component = new genericCateogryItemEditor(config);
		/*
		//hide all not required elements
		//hide categories
		the_component.edit_menu_itemsForm.remove(the_component.selectMenuItemsIcon,false);
		the_component.edit_menu_itemsForm.doLayout(false);
		the_component.edit_menu_itemsForm.render(true);
		//hide edit categories tab
		the_component.tabPanel.remove(the_component.edit_menu_categoriesForm);
		the_component.border.doLayout(false);
		*/
		return navEditor = the_component;
}

//add new product category
function ajaxSendNewProductCategory(image, content, categories, order, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=product_actions&action=add_category',
					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;
							scope.edit_menu_itemsCategoryChooser.reload();
							scope.config.categoryEditor.specificComponents.nestedCategory.reload();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);	
							//update the navTree
							scope.showNewsTree.loadTree();
						}						
					},
					thisScope,
					{
							image: image,
							content: content,
							order: order,
							category: categories
					}
	);				
}

//add update product category
function ajaxSendUpdateProductCategory(id, image, content, categories, order, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=product_actions&action=update_category', 
					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;
							scope.edit_menu_itemsCategoryChooser.reload();
							scope.config.categoryEditor.specificComponents.nestedCategory.reload(true);
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);	
							//update the navTree
							scope.showNewsTree.loadTree();
						}						
					},
					thisScope,
					{
						image: image,
						content: content,
						id: id,
						order: order,
						category: categories
					}
	);				
}

//save the product
function ajaxSendNewProduct(image, category, product_id, technical_doc, order, isSoftware, content, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=product_actions&action=add_product',
					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;
			   				//scope.showNewsTree.render();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showNewsTree.loadTree();
							scope.disableUpdateDeleteItemButtons();
						}						
					},
					thisScope,
					{
						image: image,
						category: category, 
						product_id: product_id,
						technical_document: technical_doc,
						order: order,
						content: content,
						isSoftware: isSoftware
					}
	);
}

//update the product
function ajaxSendUpdateProduct(id, image, category, product_id, technical_doc, order, isSoftware, content, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=product_actions&action=update_product',
					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;
			   				//scope.showNewsTree.render();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showNewsTree.loadTree();
							scope.disableUpdateDeleteItemButtons();
						}						
					},
					thisScope,
					{
						image: image, 
						category: category, 
						product_id: product_id,
						technical_document: technical_doc,
						order: order,
						id: id,
						content: content,
						isSoftware: isSoftware
					}
	);
}

function ajaxSendDeleteProduct(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=product_actions&action=delete_product',
					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;
							//clear all fields
							
							//update the tree	
							scope.showNewsTree.loadTree();
							scope.edit_menu_itemsCategoryChooser.reload();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							scope.disableUpdateDeleteItemButtons();			
						}					
					},
					thisScope,
					{
						id: id
					}
	);
}

//Delete a category
function ajaxSendDeleteProductCategory(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=product_actions&action=delete_category',
					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;
							//clear all fields
							
							//update the tree	
							scope.showNewsTree.loadTree();
							scope.edit_menu_itemsCategoryChooser.reload();
							scope.config.categoryEditor.specificComponents.nestedCategory.reload();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);;
							scope.disableUpdateDeleteCategoryButtons();							
						}					
					},
					thisScope,
					{
						id: id
					}
	);
}

//Request quick link information
function ajaxRequestProductItem(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=product_actions&action=get_product',
					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;
							mapItemToMenuItemForm(conn.responseXML,scope);
							scope.tabPanel.activate("edit_menu_items");
							//select the element
							scope.selectedItem = response.params.id;
							//enable update and delete buttons
							scope.enableUpdateDeleteItemButtons();
						}						
					},
					thisScope,
					{
						id:id
					}
	);
}

//Request category information
function ajaxRequestProductCategory(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=product_actions&action=get_category',
					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;
							mapCatgoryToCategoryForm(conn.responseXML,scope);
							scope.tabPanel.activate("edit_menu_categories");
							//select the element
							scope.selectedCategory = response.params.id;
							//enable update and delete buttons
							scope.enableUpdateDeleteCategoryButtons();
						}						
					},
					thisScope,
					{
						id:id
					}
	);				
}


