/***
 * Creates new filter box
 * For example for supplier or clinic filter
 * Needs functions.js and requests.js
 */
function box(boxId, obj, title, boxWidth, boxHeight, resizeable) {
	this.divbox	= GetLayer('box' + boxId);
	if(this.divbox == null) {
		var divBox = this._createDiv('box' + boxId, 'Box');

		var body = document.getElementsByTagName("body")[0];
		body.appendChild(divBox);
	}
		
	// Set box
	this.divbox	= GetLayer('box' + boxId);
	if(this.divbox == null) {
		alert('Box "'+boxId+'" not found!');
		return;
	}
	
	this.id				= boxId;
	this.title			= title;

	this.initPosition	= false;

	this.startTop		= 0;
	this.startLeft		= 0;

	if(typeof obj == 'object') {
		this.startTop	= parseInt(GetY(obj));
		this.startLeft	= parseInt(GetX(obj));
	} else if(typeof obj == 'string' && obj != '') {
		var body = document.getElementsByTagName("body")[0];
		var bodyWidth = GetWidth(body);
		var bodyHeight = GetHeight(body);
		
		var positions = obj.split(",");

		if(positions[0] && positions[0] != '') {
			var left = parseInt(positions[0]);
			if(left >= 0) {
				this.startLeft	= left;
			} else {
				this.startLeft	= bodyWidth+left;
			}
		}
		if(positions[1] && positions[1] != '') {
			var top = parseInt(positions[1]);
			if(top >= 0) {
				this.startTop	= top;
			} else {
				this.startTop	= bodyHeight+top;
			}
		}
	}

	if(isEmpty(boxWidth)) {
		boxWidth = 0;
	}
	if(isEmpty(boxHeight)) {
		boxHeight = 0;
	}

	this.initSize		= false;
	this.startWidth		= boxWidth;
	this.startHeight	= boxHeight;

	this.headerHeight	= 20;
	this.footerHeight	= 11;

	this.initBox		= false;

	if(isEmpty(resizeable)) {
		resizeable		= true;
	}
	
	this.resizeable			= resizeable;
	this.minimized 			= false;
	this.closed 			= true;
	this.eventsInitalized	= false;
	
	this.initalizeBox();
}

// Initalize box
box.prototype.getContentId= function () {
	return 'box' + this.id + 'Content';
};

// Initalize box
box.prototype.getContentDiv= function () {
	return GetLayer(this.getContentId());
};

// Create div
box.prototype._createDiv= function (id, className) {

	var div = document.createElement('DIV');

	if(id != '') {
    	div.id = id;
	    div.name = id;
	    div.setAttribute('id', id);
	    div.setAttribute('name', id);
	}

	if(className != '') {
	    div.className = className;
	    div.setAttribute('className', className);
	}

	return div;
}

// Create image
box.prototype._createImg= function (id, src, width, height, align, hspace, vspace) {

	var img = document.createElement('IMG');
	img.border = 0;

	if(id != '') {
    	img.id = id;
	    img.name = id;
	    img.setAttribute('id', id);
	    img.setAttribute('name', id);
	}

	if(src != '') {
	    img.src = GeoBIP.getRoot() + 'layout/images/' + src;
	}

    img.width = width;
    img.height = height;

	if(align != '') {
	    img.align = align;
	}
	if(hspace != '') {
	    img.hspace = hspace;
	}
	if(vspace != '') {
	    img.vspace = vspace;
	}

	return img;
}

// Initalize box
box.prototype.initalizeBox= function () {

	if(this.initBox == true) { return; }

	var parent = this;

	var Table			= document.createElement('TABLE');
    Table.width			= '100%';
	Table.border		= '0';
	Table.cellSpacing	= '0';
	Table.cellPadding	= '0';
	this.divbox.appendChild(Table);

	var TBody = document.createElement('TBODY');
	Table.appendChild(TBody);

	var global = (GeoBIP.hasProjectSetting()) ? 'global_' : '';

	// Table top
	var row	= TBody.insertRow(-1);
	row.vAlign	= 'bottom';
		var cell		= row.insertCell(-1);
		var imgCornerTopLeft = this._createImg('', 'box/' + global + 'shadow_top_left.png', '10', '20', '', '', '');
		cell.appendChild(imgCornerTopLeft);
	
		var cell				= row.insertCell(-1);
		cell.width				= '100%';
		cell.style.background	= 'url(' + GeoBIP.getRoot() + 'layout/images/box/' + global + 'shadow_top.png) bottom no-repeat';

		var imgBoxClose = this._createImg('imgActionClose' + this.id, 'box/box_close.gif', '12', '12', 'right', '2', '2');
		imgBoxClose.onmouseover	= function() { switchCursor(imgBoxClose, '1'); };
		imgBoxClose.onmouseout	= function() { switchCursor(imgBoxClose, '0'); };
		imgBoxClose.onclick		= function() { parent.closeBox(); };

		var imgBoxSwitch = this._createImg('imgActionSwitch' + this.id, 'box/box_content_close.gif', '12', '12', 'right', '4', '2');
		imgBoxSwitch.onmouseover	= function() { switchCursor(imgBoxSwitch, '1'); };
		imgBoxSwitch.onmouseout		= function() { switchCursor(imgBoxSwitch, '0'); };
		imgBoxSwitch.onclick		= function() { parent.switchBoxContent(imgBoxSwitch); };

		var divHeader = this._createDiv('box' + this.id + 'Header', 'BoxHeader');
		divHeader.ondblclick = function() { parent.switchBoxContent(imgBoxSwitch); };

		divHeader.appendChild(imgBoxClose);

		divHeader.appendChild(imgBoxSwitch);

		cell.appendChild(divHeader);

		var titleText = document.createTextNode(this.title);
		divHeader.appendChild(titleText);
	
		var cell		= row.insertCell(-1);
		var imgCornerTopRight = this._createImg('', 'box/' + global + 'shadow_top_right.png', '10', '20', '', '', '');
		cell.appendChild(imgCornerTopRight);

	// Table middle
	var row		= TBody.insertRow(-1);
	row.vAlign	= 'top';
		var cell		= row.insertCell(-1);
		cell.className	= 'BoxShadowLeft';
		cell.style.background	= 'url(' + GeoBIP.getRoot() + 'layout/images/box/' + global + 'shadow_middle_left.png) left no-repeat';

		var cell		= row.insertCell(-1);
		cell.width		= '100%';
		var divContent = this._createDiv(this.getContentId(), 'BoxContent');
		cell.appendChild(divContent);
	
		var cell		= row.insertCell(-1);
		cell.className	= 'BoxShadowRight';
		cell.style.background	= 'url(' + GeoBIP.getRoot() + 'layout/images/box/' + global + 'shadow_middle_right.png) right no-repeat';

	// Table bottom
	var row		= TBody.insertRow(-1);
	row.vAlign	= 'top';
		var cell		= row.insertCell(-1);
		var imgCornerBottomLeft = this._createImg('imgActionResizeSW', 'box/' + global + 'shadow_bottom_left.png', '10', '11', '', '', '');
		cell.appendChild(imgCornerBottomLeft);

		var cell		= row.insertCell(-1);
		cell.width		= '100%';
		cell.style.background	= 'url(' + GeoBIP.getRoot() + 'layout/images/box/' + global + 'shadow_bottom.png) top no-repeat';
	
		var divFooter = this._createDiv('box' + this.id + 'Footer', 'BoxFooter');
		cell.appendChild(divFooter);
	
		var cell		= row.insertCell(-1);
		var imgCornerBottomRight = this._createImg('imgActionResizeSE', 'box/' + global + 'shadow_bottom_right.png', '10', '11', '', '', '');
		cell.appendChild(imgCornerBottomRight);

	this.initBox = true;
};

// Set style
box.prototype.setBoxStyles= function (styles) {
	SetStyles(this.divbox, styles);

	if(BoxManager) { BoxManager.setBox(this); }
};

// Returns true if box is closed
box.prototype.isClosed= function () {
	return this.closed;
};

// Returns true if box is minimized
box.prototype.isMinimized= function () {
	return this.minimized;
};

// Returns true if box has drag n drop
box.prototype.hasEventsInitalized = function () {
	return this.eventsInitalized;
};

// Sets the value if the box is resizeable or not
box.prototype.setResizeable= function (value) {
	this.resizeable = value;
};

// Close box
box.prototype.closeBox= function () {
	SwitchLayer(this.divbox, 0);

	this.closed = true;

//	showSelect();

	if(BoxManager) { BoxManager.setBox(this); }
};

box.prototype.switchBox = function (e) {
	if(this.isClosed()) {
		this.showBox();
	} else {
		this.closeBox();
	}
};

box.prototype.switchBoxContent = function (img) {

	var divContent = this.getContentDiv();

	if(GetStyle(divContent, 'display') == 'none') {
		SwitchLayer(divContent, 1);

		if(img) {
			img.src = img.src.replace(/open/g, "close");
		}

		this.minimized = false;
	} else {
		SwitchLayer(divContent, 0);
		
		if(img) {
			img.src = img.src.replace(/close/g, "open");
		}

		this.minimized = true;
	}
};

box.prototype.showBox = function (e) {
	// First open before initalization
	SwitchLayer(this.divbox, 1);
	this.closed = false;

	if(this.initSize == false) {
		if(this.startWidth > 0 && this.startHeight > 0) {
			SetStyle(this.divbox, 'width',	this.startWidth + 'px');
			SetStyle(this.divbox, 'height',	'auto');
			
			SetStyle(this.getContentDiv(), 'height', this.startHeight + 'px');
			SetStyle(this.getContentDiv(), 'maxHeight', this.startHeight + 'px');

			this.initSize = true;
		}
	}

	if(this.initPosition == false) {
		if(this.startTop > 0 || this.startLeft > 0) {
			this.startTop	+= GetY(this.divbox)/2;
			this.startLeft	+= GetX(this.divbox)/2;
		} else {
			var body = document.getElementsByTagName("body")[0];
			var bodyWidth = GetWidth(body);
			var bodyHeight = GetHeight(body);
			
			this.startTop	= (bodyHeight/2 - this.startHeight/2);
			this.startLeft	= (bodyWidth/2 - this.startWidth/2);
		}

		SetStyle(this.divbox, 'left',	this.startLeft + 'px');
		SetStyle(this.divbox, 'top',	this.startTop + 'px');
		this.initPosition = true;
	}

	if(this.minimized) {
		this.switchBoxContent(GetImage('imgActionSwitch' + this.id));
	}

//	hideSelect();
//	showSelect(this.divbox);
	
	if(BoxManager) { BoxManager.setBox(this); }
};

box.prototype.getBoxContent = function (e) {
	return GetContent(this.divbox);
};

