function MFranchiseSelector(MOptions) {
	MOptions = MOptions ? MOptions : {};
	this.parent = MOptions.container ? MOptions.container : null;
	this.self = this;

	this.dir = '';
	this.controller = MOptions.controller ? MOptions.controller : null;
	this.franchises = Array();

	this.highColor = '#BBC1FF';

	this.initialize();
};



MFranchiseSelector.prototype.initialize = function() {
	this.container = document.createElement('DIV');
	this.parent.appendChild(this.container);
	this.container.style.border = '1px solid gray';
	this.container.style.background = '#ffffff';
	this.container.style.font = 'normal 10px verdana';
};



MFranchiseSelector.prototype.clearAll = function() {
	while (this.container.childNodes.length) {
		var removed = this.container.removeChild(this.container.childNodes[0]);
		removed = null;
	}
}


MFranchiseSelector.prototype.loadFranchises = function() {
	var self = this.self;
	this.clearAll();
	var postData = '';
	var url = app.serverScript + '?r=' + Math.random();	
	url += '&task=fList';
	ajaxLoad(url,function(http_request){
		self.processResponse(http_request);
	});
}


MFranchiseSelector.prototype.processResponse = function(req) {
	var self = this.self;

	var txt = req.responseText;
//	GLog.write(txt);
	eval(txt);
	
	for (var n = 0 ; n < response.length ; n++) {
		var func = eval( '(' + 'self.' + response[n].action + ')' ); 
		if (typeof func == 'function') {
			func(response[n],self);
		}
		else {
//			GLog.write(func + ' Not a function')
		}
	}
}



///////////////////////////////////////////////////////////////////////////
// Actions called by server responses

MFranchiseSelector.prototype.showList = function(obj,self) {
	var txt = obj.dataStr;
	var rows = txt.split('\|*\|');
	var header = rows.shift();
	for (var n = 0 ; n < rows.length ; n++ ) {
		if (rows[n]) {
			var fDiv = self.createFranchiseDiv(header,rows[n]);
			self.container.appendChild(fDiv);
		}
	}
};

MFranchiseSelector.prototype.createFranchiseDiv = function(header,fInfo) {
	var self = this.self;

	var franchiseDiv = document.createElement('DIV');
	var table = this.createTable(1,2);
	franchiseDiv.appendChild(table);
	franchiseDiv.style.cursor = 'pointer';
	
	franchiseDiv.button = this.addIcon(table.rowArray[0].cellArray[0],this.dir + '/images/plus2.png');
	table.rowArray[0].cellArray[0].style.lineHeight = '6px';
	table.rowArray[0].cellArray[0].style.verticalAlign = 'top';


	franchiseDiv.button.style.cursor = 'pointer';
	franchiseDiv.button.onclick =  function(ev) {
		try{ev.stopPropagation()}catch(e){event.cancelBubble=true};
		self.toggleChildren(franchiseDiv);
	};

	var fInfoArray = fInfo.split('|');
	var fHeaderArray = header.split('|');
	franchiseDiv.fid =  parseInt(fInfoArray[fHeaderArray.indexOf('Franchise ID')]);
	franchiseDiv.userPermissions =  parseInt(fInfoArray[fHeaderArray.indexOf('_permissions')]);

//---------------
	franchiseDiv.titleDiv = document.createElement('DIV');
	franchiseDiv.titleDiv.style.cursor = 'pointer';
	table.rowArray[0].cellArray[1].appendChild(franchiseDiv.titleDiv);
	franchiseDiv.titleDiv.innerHTML = fInfoArray[fHeaderArray.indexOf('Name')];

//---------------
	franchiseDiv.infoDiv = document.createElement('DIV');
	table.rowArray[0].cellArray[1].appendChild(franchiseDiv.infoDiv);
	franchiseDiv.infoDiv.style.display = 'none'
	franchiseDiv.infoDiv.innerHTML = '';
	franchiseDiv.infoDiv.style.margin = '2px';
	franchiseDiv.infoDiv.style.padding = '2px';





	var fTable = self.createTable(fHeaderArray.length,2);
	franchiseDiv.infoDiv.appendChild(fTable);

	for (var n = 0 ; n < fHeaderArray.length ; n++ ) {
		if (!fHeaderArray[n].match(/^_/)) {
			fTable.rowArray[n].cellArray[0].innerHTML = '<b>' + fHeaderArray[n] +':</b> ';
			fTable.rowArray[n].cellArray[0].setAttribute("noWrap",1);
			fTable.rowArray[n].cellArray[0].setAttribute("vAlign","top");
			fTable.rowArray[n].cellArray[1].innerHTML = fInfoArray[n];
		}
	}

	franchiseDiv.onclick = function(){self.loadFranchise(this.fid,this.userPermissions)};
	franchiseDiv.onmouseover = function(){this.style.background = self.highColor};
	franchiseDiv.onmouseout = function(){this.style.background = ''};

	return franchiseDiv;
};


MFranchiseSelector.prototype.loadFranchise = function(fid,permissions) {
	if (this.controller) {
		this.controller.loadFranchise(fid,permissions);
	}
}



MFranchiseSelector.prototype.toggleChildren = function(fDiv) {
	if (fDiv.infoDiv.style.display == 'none') {
		fDiv.infoDiv.style.display = '';
		fDiv.infoDiv.scrollIntoView(false);
		fDiv.button.src = this.dir + '/images/minus2.png';
	}
	else {
		fDiv.infoDiv.style.display = 'none';
		fDiv.button.src = this.dir + '/images/plus2.png';
	}
}






MFranchiseSelector.prototype.createTable = function(rowCount,colCount) {
	var table = document.createElement('TABLE');
	table.tBody = document.createElement('TBODY');
	table.appendChild(table.tBody);
	table.rowArray = Array();

	for (var n = 0 ; n < rowCount ; n++ ) {
		this.addRow(table,colCount);
	}

	return table;
};


MFranchiseSelector.prototype.addRow = function(table,colCount) {
	var tRow = document.createElement('TR');
	tRow.cellArray = Array();
	table.tBody.appendChild(tRow);
	table.rowArray.push(tRow);
	for (var n = 0 ; n < colCount ; n++ ) {
		this.addCell(tRow);
	}
};

MFranchiseSelector.prototype.addCell = function(tRow) {
	var tCell = document.createElement('TD');
	tRow.appendChild(tCell);
	tRow.cellArray.push(tCell);
};

MFranchiseSelector.prototype.addIcon = function(tCell,src) {
	if (tCell) {
		var oImg = document.createElement('IMG');
		tCell.appendChild(oImg);
		oImg.src = src;
		return oImg;
	}
};

