var CustomerEditTable = function(sender, id, form, parentNode, params) {
	this.opens = 0;
	this.form = form;
	this.transform = sender;
	this.lnkObj = Ext.get(this.transform);
	this.target=Ext.get(id);
	this.target.dom.style.display='';
	if (this.lnkObj) {
		this.lnkObj.dom.onclick=this.openCloseEditorPanel;
		this.parentNode = this.lnkObj.dom.parentNode;
		if (parentNode) this.parentNode = Ext.get(parentNode).dom; 
		this.lnkObj.dom.parentObj = this;
		this.parentNode.style.position='relative';
		this.setupEditorPanel();
	}
	
	this.confirmText='If you change customer information, your table will reset. Really change?';
	if (params)
	{
		if (params.confirmText) this.confirmText = params.confirmText;
	}
	
}
CustomerEditTable.prototype.setupEditorPanel = function()
{
	var self = this;
	this.lnkPos = Utilities.findRelativePos(this.lnkObj.dom,this.parentNode);
	
	this.refreshButton = new Ext.Button(
	{
		id:'closeButton'+Ext.id(),
		text:'Change',
		parentObj:self,
		ctCls:'editCustomerButton',
		handler: self.refreshPage
	}
	);
	this.closeButton = new Ext.Button(
	{
		id:'closeButton'+Ext.id(),
		text:'Cancel',
		parentObj:self,
		ctCls:'editCustomerButton',
		handler: self.closeEditorPanel
	}
	);
	this.instructionPanel = new Ext.Panel(
		{id:'instructionPanel'+Ext.id(),
		title:'',
		cls:'editinstructions',
		//headerAsText:false,
		width:'500'//,
		//items:[self.refreshButton,self.closeButton]
		}
	);
	this.selectorPanel = new Ext.Panel(
		{id:'selectorPanel'+Ext.id(),
		hidden:true,
		title:'Edit Customer Info',
		width:500,
		hideMode:'offsets',
		floating:true,
		shadow:true,
		shadowOffset:8,
		style:'position:absolute;z-index:500',
		items:[self.instructionPanel]
		}
	);
	this.selectorPanel.render(Ext.get(this.parentNode));

	this.selectorPanel.setPosition(this.lnkPos[0]-450,this.lnkPos[1]+30);
	
	var pnlDom = this.target.dom;
	pnlDom.parentNode.removeChild(pnlDom);
	
	this.selectorPanel.getEl().dom.appendChild(pnlDom);

	this.refreshButton.render(this.selectorPanel.getEl());
	this.closeButton.render(this.selectorPanel.getEl());
	
}

CustomerEditTable.prototype.openCloseEditorPanel = function()
{
    if (!this.parentObj) this.parentObj = this;
	this.parentObj.selectorPanel.setVisible(!this.parentObj.selectorPanel.isVisible());
	if (this.parentObj.selectorPanel.isVisible()===true) {
		var div = this.parentObj.selectorPanel.getEl().dom;
		if (!this.parentObj.visibleLeft && !this.parentObj.visibleTop)
		{
			this.parentObj.visibleLeft=div.style.left;
			this.parentObj.visibleTop=div.style.top;
		}
		else if (Utilities.trimPx(div.style.left)<0){
			div.style.left=this.parentObj.visibleLeft;
			div.style.top=this.parentObj.visibleTop
		}
	}

    if (Ext.isIE6 && this.parentObj.opens<2) {
        this.parentObj.opens++;
        this.parentObj.openCloseEditorPanel();
    }

	return false;
}
CustomerEditTable.prototype.openEditorPanel = function()
{
	this.parentObj.selectorPanel.setVisible(true);
	
	return false;
}
CustomerEditTable.prototype.closeEditorPanel = function()
{
	this.parentObj.selectorPanel.setVisible(false);
	return false;
}
CustomerEditTable.prototype.refreshPage = function(q)
{
	var res = confirm(q.parentObj.confirmText);
	var ok = q.parentObj.validateInputs(q.parentObj);
	if (res===true && ok)
	{
		var el = q.getEl().dom;
		var f = Ext.get(q.parentObj.form);
		f.dom.method = "POST";
		f.dom.submit();
	}
	
	if (ok===false) {
		alert("ERRORS: "+q.parentObj.errors);
	}
}
CustomerEditTable.prototype.validateInputs = function(self)
{
	var passed = false;
	var tbl = Ext.query("table",self.target.dom);
	tbl = tbl[0];
	self.errors = "";
	var invalidDate = self.formatDate('notadate');
	var cnt =0;
	for (var i=0;i<tbl.rows.length;i++) {

		var inptb = Ext.get("birthdates["+i+"]");
		var inptm = Ext.get("male["+i+"]");
		var inptf = Ext.get("female["+i+"]");
		if (inptb)
		{
			var p = inptb.dom.parentNode.parentNode.cells[0].childNodes[0].innerHTML;

			inptb=inptb.dom;
			inptm=inptm.dom;
			inptf=inptf.dom;
			
			var oneIsChecked = inptm.checked || inptf.checked;
			var isOK = true;

			if (inptb.value&&inptb.value!="") {
				var formattedDate = self.formatDate(inptb.value);
				var isADate = formattedDate!=invalidDate;
				if (isADate===false) {
					self.errors+="\nInvalid date format for "+p+", row "+(i+1);
					isOK = false;
				}
				if (oneIsChecked===false) {
					self.errors+="\nGender is required for "+p+", row "+(i+1)+"";
					isOK = false;
				}
			}
			else if (oneIsChecked===true)
			{
				self.errors+="\nBirthdate is required for "+p+", row "+(i+1)+"";
					isOK = false;
			}
			
			if (isOK===true)
			{
				cnt++;
			}

		}
		
		
	}
	if (cnt==0) self.errors+="\nAt least one valid customer is required.";
	return self.errors.length==0;
}
CustomerEditTable.prototype.formatDate = function (value) {
        return value ? 
        	(value.dateFormat ? value.dateFormat('m/d/Y') : Ext.util.Format.date(value,'m/d/Y')) : '';
    };

