
function requestLicenceRequests(selectFunction, renderTo){
	
	/**JAVASCRIPT TO REQUEST LICENCES**/			
	var configSelectLicenceType = {
  		comboId: null,
  		label: 'licence type',
  		queryField: 'item',
  		mappingElement: 'name',
  		queryURL: 'XmlGMServlet?action_type=shop_actions&action=request_licence_types',
  		functionToExecOnSelection: Ext.emptyFn,
  		scope: this,
		notRemote: true
	}			
	this.selectLicenceType = new GmSoft.components.ComboSelector(configSelectLicenceType);
			
	var configSelectProduct = {
  		comboId: null,
  		label: 'product',
  		queryField: 'item',
  		mappingElement: 'name',
  		queryURL: 'XmlGMServlet?action_type=product_actions&action=get_all_software_products',
  		functionToExecOnSelection: function(combo, record, index){ //request all licences and add them to the this.selectLicenceType
			this.scope.store.load({
				params: {
					product_id : record.get('id')
				}
			});		
		},
  		scope: this.selectLicenceType
	}	
	
	this.amount = getTextFieldComponent(null,'amount',null,null);
	this.selectProduct = new GmSoft.components.ComboSelector(configSelectProduct);		
	
	this.buyButton =  new Ext.Button({
		text: 'request'
	});	
	
	this.buyButton.addListener('click',function( This, e){
		var amount = this.amount.getValue();
		var selectedProduct = this.selectedProduct.getValue().get('id');
		var selectedLicence = this.selectedLicence.getValue().get('id');
		sendRequestLicencesForSelectedProduct(selectedProduct, selectedLicence, amount);
	},{amount: this.amount, selectedProduct: this.selectProduct, selectedLicence: this.selectLicenceType});
	
	this.buyLicencePanel = new Ext.form.FormPanel({
	        width: 505,	
			autoHeight : true,
			bodyBorder: false,
			border: false,		
			bodyStyle: 'padding:20px 10px 10px 10px',
			items: [this.selectProduct, this.selectLicenceType, this.amount],
			buttons: [this.buyButton]
	});
		
	this.buyLicencePanel.render(renderTo);
}

function sendRequestLicencesForSelectedProduct(product, licence, amount){

	ajaxGenericCall('XmlGMServlet?action_type=shop_actions&action=request_licence',					
					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);							
						}
					},
					null,
					{
						lang: selectedLangId,
						product_id: product,
						licence_type_id: licence,
						licence_amount: amount
					}
	);

}

function sendRequestLicencesTypesByProductId(product,scope){

	ajaxGenericCall('XmlGMServlet?action_type=shop_actions&action=request_licence_types',					
					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,
					{
						product_id: product
					}
	);		
}

function activateInvalidateLicence(selectFunction, renderTo){
	
	var queryURL = 'XmlGMServlet?action_type=shop_actions&action=request_all_licences';
	var queryField = 'licence';
	
	//If this store is set will be updated at every button click
	this.updateStore = null;	
	this.setUpdateStore = function(store){
		this.updateStore = store;
	}
	
	this.fileRecord = new Ext.data.Record.create([
			{name: "activated", mapping: "activated", type:'string'},
			{name: "product", mapping: "product", type:'string'},	
			{name: "licence_type", mapping: "licence_type", type:'string'},			
			{name: "start_day", mapping: "start_day", type:'string'},			
			{name: "id", mapping: "id", type:'string'}
	]);
	
	this.proxy = new Ext.data.HttpProxy({url: queryURL});
    // create the Data Store
    this.store = new Ext.data.Store({
        // load using HTTP
        proxy: this.proxy,
        // the return will be XML, so lets set up a reader
        reader: new Ext.data.XmlReader({
               // records will have a "category" tag
               record: queryField
           }, this.fileRecord)
    });			

	this.store.load();	

	this.grid = new Ext.grid.GridPanel({
		store: this.store,
	    columns: [
			{header: 'activated',  sortable: true},
			{header: 'product', sortable: true},
			{header: 'licence_type', sortable: true},
			{header: 'start_day', width: 140, sortable: true}
	    ],
	    height:180,
	    width:480,
		title: "all user licences"
	});		
		
	
	this.fieldSet = null;
	
	this.fieldSet = new Ext.form.FieldSet({
		xtype: 'fieldset',
		title: "user licences",
		collapsible: false,
		collapsed: false,
		autoHeight: true,
		bodyStyle: 'text-align:left;',
		items: [this.grid]
	});
	
	this.buttonActivate = new Ext.Button(
		{
			text: 'activate',
			scope: this,
			minWidth: 100,			
			xtype: 'button',
			handler: function(This,e){
				
				//get the licence id				
				var selectionModel = this.grid.getSelectionModel();
				var licenceId = selectionModel.getSelected().get('id');
				
				requestLicenceActivationParameters(licenceId, this);			
	        }				
		}
	); 
	
	this.buttonInvalidate = new Ext.Button(
		{
			text: 'invalidate',
			scope: this,
			minWidth: 100,			
			xtype: 'button',
			handler: function(This,e){
				//get the licence id				
				var selectionModel = this.grid.getSelectionModel();
				var licenceId = selectionModel.getSelected().get('id');

				sendRequestInvalidateLicence(licenceId,this.store);
	        }				
		}
	);	
	
	this.showNews = new Ext.form.FormPanel({
	        width: 505,	
			autoHeight : true,
			bodyBorder: false,
			border: false,		
			labelWidth: 200,
			items: [this.fieldSet],
			buttons: [this.buttonActivate, this.buttonInvalidate]
	});
		
	this.showNews.render(renderTo);
	
}

function sendRequestInvalidateLicence(licenceID, store){
	ajaxGenericCall('XmlGMServlet?action_type=shop_actions&action=invalidate_licence',					
					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.load();
							
							var result = conn.responseXML;
							var licenceText = getResponseText(conn.responseXML);
							showInfoDialog('Licence', licenceText);
						}
					},
					store,
					{
						licence_id: licenceID
					}
	);		
}

function requestLicenceActivationParameters(licenceID, scope){

	ajaxGenericCall('XmlGMServlet?action_type=shop_actions&action=request_activation_parameters',					
					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 xmlResult = conn.responseXML;	
							
							var params = xmlResult.getElementsByTagName('parameter');
							
							showActivationRequestFormPanel(response.params.licence_id, params, scope.store);													
						}
					},
					scope,
					{
						licence_id: licenceID
					}
	);		
}


function showActivationRequestFormPanel(licenceID, params, store){
	
    var win = new Ext.Window({
		title: "Activation Parameters",
        layout:'form',
        width:450,
		autoHeight:true,
        closeAction:'hide',
        plain: true,
        //modal: true,
		bodyStyle:'padding:10px 10px 10px 10px',
		licence_id : licenceID,
		storeToReload : store
    });	
	
	
	var buttonRequest = new Ext.Button(
		{
			text: 'Request',
			scope: win,
			minWidth: 100,			
			xtype: 'button',
			handler: function(This,e){
				var params = {};

				var i = 0;
				var item = This.scope.getComponent(i);
				while(item != null){
					params[item.fieldLabel] = item.getValue();
					i++;
					item = This.scope.getComponent(i);
				}
				
				sendActivationRequest(This.scope.licence_id, params, This.scope.storeToReload);

				This.scope.close();
				
				/*
				//get the licence id				
				var selectionModel = this.grid.getSelectionModel();
				var licenceRequestId = selectionModel.getSelected().get('id');
				var description = this.paymentOrRefuseDescription.getValue();
				//send the ajax call
				ajaxRefuseRequest(licenceRequestId, description, this);
				*/
	        }				
		}
	);			
	win.addButton(buttonRequest);
	
	
	//write a component for each parameter	
	for (i = 0; i < params.length; i++) {
		var node = params[i];
		var component = getParameterFieldFromXML(node);
		
		win.add(component);		
	}	
	
	win.show();	
}

function sendActivationRequest(licenceID, params, store){
	var allParameters = Ext.apply(params, {licence_id: licenceID});

	ajaxGenericCall('XmlGMServlet?action_type=shop_actions&action=activate_licence',					
					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.load();
							
							var result = conn.responseXML;
							var licenceText = getResponseText(conn.responseXML);
							if (licenceText == null) {
								showInfoDialog('Licence', "No licence text could be generated");
							}else{
								showInfoDialog('Licence', licenceText);
							}														
						}
					},
					store,
					allParameters
	);		
}

/*
 * 
 * Node Containing the following structure:
	<parameter>
		<name>hard_disk_id</name>
		<type>string</type>
		<description>HD identifier</description>
	</parameter> 
 * 
 */
function getParameterFieldFromXML(node){
	var name = findDirectTextNodeInsideTagValue(node,'name');
	var type = findDirectTextNodeInsideTagValue(node,'type');
	var description = findDirectTextNodeInsideTagValue(node,'description');
	
	return new Ext.form.TextField({
	        fieldLabel: name,
			width: 300
	    });
}

