/***
 * Creates new filter box
 * For example for supplier or clinic filter
 * Needs functions.js and requests.js
 */
function filterbox(boxId, boxWidth, boxHeight, fieldTextLength) {
	this.divbox	= GetLayer('box' + boxId);
	if(this.divbox == null) {
		var divBox = document.createElement('DIV');
	    divBox.id = 'box' + boxId;
	    divBox.name = 'box' + boxId;
	    divBox.setAttribute('id', 'box' + boxId);
	    divBox.setAttribute('name', 'box' + boxId);
	
		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;
	}
	
	// Sets parameter
	if(isEmpty(fieldTextLength) || parseInt(fieldTextLength) < 1) {
		this.fieldtextlength = 6;
	} else {
		this.fieldtextlength = parseInt(fieldTextLength);
	}

	if(isEmpty(boxWidth)) {
		boxWidth = 0;
	}
	if(isEmpty(boxHeight)) {
		boxHeight = 0;
	}

	this.root			= '';
	this.session		= '';

	this.id					= boxId;
	this.width				= boxWidth;
	this.height				= boxHeight;
	this.fieldId			= '';
	this.field				= null;
	this.boxMode			= '';
	this.checkTextLength	= true;

	this.query			= '';
	this.jsQuerySet		= '';

	// Sets default values
	this.currentPosition = 0;
	
	delete this.currentMarkedObject;
	this.currentMarkedObject = null;
	
	this.lastFilterText = null;
	
	this.closed		= true;
	this.overBox	= false;

	this.ajax = new ajax();
	this.ajax.parent = this;

	var current	= this;

	this.divbox.onmouseout	= function(e) { current.overBox = false; };
	this.divbox.onmouseover	= function(e) { current.overBox = true; };
	this.divbox.onfocus		= function(e) { current.checkFilterbox(); };
	this.divbox.onkeyup		= function(e) { current.checkFilterbox(); };
};

// Set root
filterbox.prototype.setRoot = function (strRoot) {
	if(typeof strRoot == 'string') {
		this.root = strRoot;
	}
};

// Returns root
filterbox.prototype.getRoot = function () {
	return this.root;
};

// Set session
filterbox.prototype.setSession = function (strSession) {
	if(typeof strSession == 'string') {
		this.session= strSession;
	}
};

// Returns session
filterbox.prototype.getSession = function () {
	return this.session;
};

// Set style
filterbox.prototype.setFilterboxStyles= function (styles) {
	SetStyles(this.divbox, styles);
};

// Returns the current target field
filterbox.prototype.setFieldId = function (fieldId) {
	this.fieldId = fieldId;
};

// Returns the current text field
filterbox.prototype.getField = function () {
	return GetObject('txt' + this.fieldId);
};

// Returns the current target field
filterbox.prototype.getTargetField = function () {
	return GetObject('fld' + this.fieldId);
};

// Set fields
filterbox.prototype.setFields = function (fieldValue, textValue) {
	var field = this.getTargetField();
	if(field) { field.value = fieldValue; }

	var txt = this.getField();
	if(txt) {
		var width = GetWidth(txt);
		txt.value = textValue.replace(/<br>/ig, ' ');
		SetStyle(txt, 'width', parseInt(width) + 'px');
	}
};

// Clear fields
filterbox.prototype.clearFields = function (fieldId) {
	this.fieldId	= fieldId;
		
	var field = this.getTargetField();
	if(field) { field.value = ''; }

	var txt = this.getField();
	if(txt) { txt.value = ''; }
};

// Close box
filterbox.prototype.isClosed = function () {
	return this.closed;
};

// Close filterbox
filterbox.prototype.closeFilterbox= function () {
	if(this.overBox == false) {
		SwitchLayer(this.divbox, 0);
		
		this.closed = true;
		this.lastFilterText = null;
		this.fieldId	= '';
		
//		showSelect();

		if(BoxManager) BoxManager.setBox(this);
	}
};

filterbox.prototype.openFilterbox = function () {
	var field = this.getField();
		
	if(this.isClosed() && field) {
		SwitchLayer(this.divbox, 1);
		this.closed = false;
		this.checkTextLength = false;
		
		field.focus();
			
		//hideSelect();

		if(BoxManager) BoxManager.setBox(this);
	}
};

filterbox.prototype.showFilterbox = function (boxMode, fieldId, query, jsQuerySet) {
	var current = this;

	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.showFilterbox(boxMode, fieldId, query, jsQuerySet); }, 100);
	} else {

		// Set todo for set and clear
		if(isEmpty(query)) {
			this.query		= '';
		} else {
			this.query		= query;
		}
		if(isEmpty(jsQuerySet)) {
			this.jsQuerySet		= '';
		} else {
			this.jsQuerySet		= jsQuerySet;
		}
		
		this.boxMode	= boxMode;
		this.fieldId	= fieldId;

		var field		= this.getField();
		field.onblur	= function (e) { current.closeFilterbox(e); };
		field.onkeyup	= function (e) { current.setFieldId(fieldId); current.checkFilterbox(e); };
		field.onclick	= function (e) { current.setFieldId(fieldId); };

		SetStyle(this.divbox, 'position', 'absolute');
		SetStyle(this.divbox, 'left', GetX(field));
		SetStyle(this.divbox, 'top', GetY(field)+18);

		if(this.width == 0) {
			SetStyle(this.divbox, 'width', (GetWidth(field)) + 'px');
		} else {
			SetStyle(this.divbox, 'width', parseInt(this.width) + 'px');
		}
		if(this.height == 0) {
			SetStyle(this.divbox, 'height', '100px');
		} else {
			SetStyle(this.divbox, 'height', parseInt(this.height) + 'px');
		}

		if(this.checkTextLength && field.value.length < 1) {
			return;
		} else {
			this.checkTextLength = true;
		}
	}
};

filterbox.prototype.checkFilterbox = function (e) {
	var current = this;

	if(this.ajax.isBusy()) {
		window.setTimeout(function() { current.checkFilterbox(e); }, 500);
	} else {
		if(!e) e = window.event;
		
		var keyCode = '';
		if(e && e.keyCode) {
			keyCode = e.keyCode;
		} else if(e && e.charCode) {
			keyCode = e.charCode;
		}

		// Last loading
		if(this.ajax.isBusy()) {
			this.ajax.reset();
		}
	
		var field = this.getField();
		
		if(keyCode == '38') { // Cursor up
			delete this.currentMarkedObject;
			this.currentMarkedObject == null;
			
			this.currentPosition--;
			if(this.currentPosition < 1) {
				this.currentPosition = 1;
			}
			this.setFilterboxPosition();
		} else if(keyCode == '40') { // Cursor down
			delete this.currentMarkedObject;
			this.currentMarkedObject == null;

			this.currentPosition++;
			this.setFilterboxPosition();
		} else if(keyCode == '13') { // Enter
			this.setFilterbox();
			return;
		} else if(keyCode == '27') { // Esc
			field.blur();
			this.closeFilterbox();
			return;
		} else if(1==0 && keyCode == '8') { // Delete
			return;
		} else if(1==0 && keyCode == '46') { // Delete
			return;
		} else if(keyCode == '37') { // Cursor left
			return;
		} else if(keyCode == '39') { // Cursor right
			return;
		} else if(keyCode == '16') { // Shift
			return;
		} else if(keyCode == '17') { // Strg
			return;
		} else if(keyCode == '35') { // Ende
			return;
		} else if(keyCode == '36') { // Pos1
			return;
		} else {
		}

		var text = field.value.substr(0, this.fieldtextlength);
		if(text == '') {
			this.closeFilterbox();
			return;
		}
	
		if(this.lastFilterText != text) {
			this.lastFilterText = text;
			this.checkTextLength = false;
			this.currentPosition = 0;
			
			delete this.currentMarkedObject;
			this.currentMarkedObject = null;
		
			var targetField = this.getTargetField();
			if(targetField) { targetField.value = ''; }
		
			SetContent(this.divbox, '');
			
			this.ajax.load(this.divbox.id, this.getRoot() + 'load.php?' + this.getSession() + '&type=box&action=select.' + this.boxMode + '&mode=' + this.boxMode + '&text=' + encodeURIComponent(text) + getQueryGlue(this.query));
		}

		var tmpText = field.value;
		field.value = "";
		field.focus();
		field.value = tmpText;
	}
};

filterbox.prototype.setFilterbox = function () {
	if(this.currentMarkedObject != null) {
		var objectPk = this.currentMarkedObject.getAttribute("objectPk");
		var objectText = GetContent(this.currentMarkedObject);
		var action = this.currentMarkedObject.getAttribute("action");
	} else {
		var objectPk = '';
		var objectText = '';
		var action = '';
	}

	if(objectPk == 'all') {
		this.setFields('', '');

		this.overBox = true;
		this.currentPosition = 0;

		delete this.currentMarkedObject;
		this.currentMarkedObject = null;
		
		this.checkFilterbox();
	} else {
		if(objectText != '') {
			var text = objectText.charEntities();
			var textAddon = GetObject('txt' + this.currentPosition + 'Addon');
			if(textAddon) {
				text += ' (' + GetContent(textAddon) + ')';
			}
		
			this.setFields(objectPk, text);
		}
	
		if(!isEmpty(action)) {
			eval(action);	
		} else if(this.jsQuerySet != '') {
			var query = this.jsQuerySet;
			query = query.replace(/objectPk/gi, objectPk);
			eval(query);
		}

		var field = this.getField();
		if(field && field.blur) { field.blur(); }
		
		this.overBox = false;
		this.closeFilterbox();
	}
};

filterbox.prototype.overFilterboxPosition = function (object) {
	this.currentMarkedObject = null;
	this.currentPosition = parseInt(object.getAttribute("objectPosition"));
		
	switchCursor(object, '1');
	this.setFilterboxPosition();
};

filterbox.prototype.outFilterboxPosition = function (object) {
	delete this.currentMarkedObject;
	
	this.currentMarkedObject = null;

	switchCursor(object, '0');
};

filterbox.prototype.setFilterboxPosition = function () {
	var objects = this.divbox.getElementsByTagName("div");
	var lastPosition = 0;
	var lastObject = '';

	for(var i=0;i<objects.length;i++) {
		if(stristr(objects[i].className, 'Aktiv')) {
			objects[i].className = objects[i].className.replace('Aktiv', '');
		}
		
		if(objects[i].getAttribute("objectPk") != null && objects[i].getAttribute("objectPk") != '') {
			lastObject = objects[i];
			lastPosition = parseInt(lastObject.getAttribute("objectPosition"));

			if(lastPosition == this.currentPosition) {
				this.currentMarkedObject = lastObject;
				this.currentMarkedObject.className += "Aktiv";
			}
		}
	}

	if(lastPosition < this.currentPosition) {
		this.currentMarkedObject = lastObject;
		this.currentMarkedObject.className += "Aktiv";
		this.currentPosition = lastPosition;
	}
};
