var childNode='';
var parentNode='';
var budget;

function budgets() {
	// id of the node we are working with
	var id;
	var type;
}

budgets.prototype.enableList = function(someId) {
	childNode = someId;
	var bd = Ext.getCmp('tab-panel').enable();
	tab = bd.getActiveTab();
	tab.getUpdater().update({url:tab.url, callback: cnfjs, params:tab.params+'&active=1&pid='+childNode});
}

budgets.prototype.additem = function(url,title) {
	var tabs = new Ext.Panel({
        region: 'center',
        id:'configure-rate-pane',
        frame:false,
        border:false,
        hideBorders:true,
        margins:'3 3 3 0', 
        activeTab: 0,
        defaults:{autoScroll:true},
        items:[{
            listeners: {afterlayout: openItem},
            url: 'service.php',
            params: '&request='+url+'&parentNode='+parentNode.replace('root_', '')+'&childNode='+childNode
        }]
    });

    // Panel for the west

    var win = new Ext.Window({
        title: 'Configure Rate Schedule',
        closable:true,
        modal:true,
        width:400,
        height:325,
        plain:true,
        layout: 'border',
        items: [tabs]
    });

    win.show(this);
}

budgets.prototype.openItem = function(tab) {
	tab.getUpdater().update({url:tab.url, callback: cnfjs, callback: cnfjs, params:tab.params+'&active=1&pid='+childNode});
}

budgets.prototype.resetCreate = function() {
	var url = 'system.php';
	var params = '&request=budgets.chargecode.create&childNode='+childNode+'&parentNode='+parentNode;
	Ext.getCmp('configure-rate-pane').getUpdater().update({url:url, params: params});
}

budgets.prototype.addsub = function() {
	var string='';
	var error=0;
	$("#add-subs select").each(function() {
		if ($(this).val() != 0) {
			string += '&' + $(this).attr("id") + '=' + $(this).val();
		}
		else {
			error = 1;
		}
	});
	$("#add-subs input").each(function() {
		if ($(this).val() != '') {
			string += '&' + $(this).attr("id") + '=' + $(this).val();
		}
		else {
			error = 1;
		}
	});
	if (error) {
		alert('You must complete all fields to add a sucontractor');
	}
	else {
		sys.connect(null, 'common.js&runmethod=addsub&pclass='+pClass+'&pid='+pId+string, 'nowrite');
	}
}

budgets.prototype.addequip = function() {
	var string='';
	var error=0;
	$("#add-equip select").each(function() {
		if ($(this).val() != 0) {
			string += '&' + $(this).attr("id") + '=' + $(this).val();
		}
		else {
			error = 1;
		}
	});
	$("#add-equip input").each(function() {
		if ($(this).val() != '') {
			string += '&' + $(this).attr("id") + '=' + $(this).val();
		}
		else {
			error = 1;
		}
	});
	if (error) {
		alert('You must complete all fields to add materials/equipment.');
	}
	else {
		sys.connect(null, 'common.js&runmethod=addequip&pclass='+pClass+'&pid='+pId+string, 'demo');
	}
}

function editbudget(pclass,pid) {
	
	var tabs = new Ext.Panel({
        region: 'center',
        id:'configure-budget-pane',
        frame:false,
        border:false,
        hideBorders:true,
        margins:'3 3 3 0', 
        activeTab: 0,
        defaults:{autoScroll:true},
        items:[{
            listeners: {afterlayout: openItem},
            url: 'service.php',
            params: '&request=common.budgets.create&pclass='+pclass+'&pid='+pid
        }]
    });

    // Panel for the west

    var win = new Ext.Window({
    	id:'configure-budget-window',
        title: 'Configure Item Budget',
        closable:true,
        pageX:125,
        pageY:75,
        modal:true,
        width:450,
        height:200,
        plain:true,
        items: [tabs]
    });

    win.show(this);
}

function itemConvert() {
	var grid = new Ext.ux.grid.TableGrid('charge_list', {border:false});
	grid.render();
}

var budget = new budgets();
