﻿/**
	ClassList objektum
 */
function ClassList(labelList) {
	if (typeof labelList == 'string') {
		this.partOf = labelList.split(" ");
	} else {
		this.partOf = new Array();
	}
}

ClassList.prototype.addLabel = function(label) {
	var found = false;
	for (var i=0; i<this.partOf.length; i++) {
		if (this.partOf[i] == label) {
			found = true;
		}
	}
	if (!found) {
		this.partOf.push(label);
	}
}

ClassList.prototype.removeLabel = function(label) {
	var found = false;
	for (var i=0; i<this.partOf.length; i++) {
		if (found && i>0) {
			this.partOf[i-1] = this.partOf[i];
		}
		if (this.partOf[i] == label) {
			found = true;
		}
	}
	if (found) {
		this.partOf.pop();
	}
}

ClassList.prototype.hasLabel = function(label) {
	for (var i=0; i<this.partOf.length; i++) {
		if (this.partOf[i] == label) {
			return true;
		}
	}
	return false;
}

ClassList.prototype.toString = function() {
	return this.partOf.join(" ");
}

ClassList.prototype.getCheckType = function() {
	var checkt = new Array("onlyalpha","onlynumber","anything","ipaddr",
        "phonenum","domainname","email","url","taxnum","traderegnum");
	for (var i=0; i<checkt.length; i++) {
		if (this.hasLabel(checkt[i])) {
			return checkt[i];
		}
	}
	return "";
}

ClassList.prototype.setCheckType = function(checktype) {
	var checkt = new Array("onlyalpha","onlynumber","anything","ipaddr",
        "phonenum","domainname","email","url","taxnum","traderegnum");
	for (var i=0; i<checkt.length; i++) {
		this.removeLabel(checkt[i]);
	}
	this.addLabel(checktype);
	return true;
}

/**
	Parser objektum
 */

function EParser(str) {
}

EParser.prototype.skipSpace = function() {
	if (this.ch == ' ') {
		while (this.ch == ' ' && this.pos<=this.str.length) {
			this.ch = this.str.charAt(this.pos++);
		}
	}
}

EParser.prototype.getToken = function() {
	var token = new Object();
	var tok = '';
	var state = 0;
	this.skipSpace();
	if (this.ch >= 'a' && this.ch <= 'z') {
		token.type = 'var';
		while (this.ch >= 'a' && this.ch <= 'z' && this.pos<=this.str.length) {
			tok += this.ch;
			this.ch = this.str.charAt(this.pos++);
		}
	} else if (this.ch >= '0' && this.ch <= '9') {
		token.type = 'num';
		while (this.ch >= '0' && this.ch <= '9' && this.pos<=this.str.length) {
			tok += this.ch;
			this.ch = this.str.charAt(this.pos++);
		}
	} else if (this.ch == '+' || this.ch == '-' || this.ch == '*' || this.ch == '/') {
		token.type = 'op';
		tok = this.ch;
		this.ch = this.str.charAt(this.pos++);
	} else if (this.ch == '(') {
		token.type = 'expr';
		while (this.pos<=this.str.length) {
			tok += this.ch;
			if (this.ch == ')') {
				this.ch = this.str.charAt(this.pos++);
				break;
			}
			this.ch = this.str[this.pos++];
		}
	} else {
		if (this.pos <= this.str.length) {
			tok = 'undefined:' + this.ch;
			token.type = undefined;
			this.ch = this.str.charAt(this.pos++);
		}
	}
	token.value = tok;
	return token;
}

EParser.prototype.parse = function(str) {
	this.str = str;
	this.pos = 0;
	this.posch;
	this.ch = this.str.charAt(this.pos++);
	var tok = new Array();
	while (this.pos<=this.str.length) {
		tok.push(this.getToken());
	}
	return tok;
}

EParser.prototype.getVarNames = function(str) {
	var tokens = this.parse(str);
	var varNames = new Array();
	for (var i=0; i<tokens.length; i++) {
		if (tokens[i].type == 'var') {
			varNames.push(tokens[i].value);
		}
	}
	return varNames;
}

/**
	Other useful functions
*/

function searchForParentDiv(element) {
//	alert(element.tagName);
	if (!element) {
		return null;
	} else {
		if (element.tagName == 'DIV') {
			return element;
		} else {
			return searchForParentDiv(element.parentNode);
		}
	}
}

function checkInForm(tag) {
	var ret = false;
	if (!tag) return false;
	if (tag.tagName == "FORM") {
		return true;
	}
//	alert(tag.tagName);
	var t = tag.parentNode;
	if (t) {
		ret = checkInForm(t);
	} 
	return ret;
}

function hasFormParent() {
	var d = oEditor.FCK.EditorWindow;
	if (d.getSelection) {
		var sel = oEditor.FCK.EditorWindow.getSelection();		
		var range = sel.getRangeAt(0);
		return checkInForm(range.startContainer);
	} else { //ie
		var sel = oEditor.FCK.EditorDocument.selection;
		var range = sel.createRange();
		if (range.parentElement) {
			return checkInForm(range.parentElement());
		} else {
			return true;
		}
	}
} 

function formAlert() {
	if (!hasFormParent()) {
		alert('Az objektum nincs form-ban');
	}
}
	
function hasClassLabel(node, attr) {
	if (!node) return false;
	var attrList = node.getAttribute('class');
	if (!attrList) {
		attrList = node.className;
	}
	if (attrList) {
		var cl = new ClassList(attrList);
		if (cl.hasLabel(attr)) 
			return true;
	}
	return false;
}

function addClassLabel(node, attr) {
	var cl = new ClassList(node.className);
	cl.addLabel(attr);
	node.className = cl.toString();
	return true;
}

function removeClassLabel(node, attr) {
	var cl = new ClassList(node.className);
	cl.removeLabel(attr);
	node.className = cl.toString();
	return true;
}

function getSelectedDiv(tag) {
	var tagOk = false;
	try {
		if (tag.tagName == 'DIV')  { 
			tagOk = true;
		}
	} catch (e) {}
	
	if (!tagOk) {
		tag = FCKSelection.getParentDiv(tag);
		try {
			if (tag.tagName == 'DIV') {
				tagOk = true;
			}
		} catch (e) {}
	}
	if (!tagOk) {
		return null;
	} else {
		return tag;
	}
}

function DlgGetAttribute(el, attr) {
	
	if (el.getAttribute(attr)!=null) {
		return el.getAttribute(attr);
	} else {
		try {
			return el.attributes.getNamedItem(attr).value;
		} catch (e) { 
			return null;
		}
	}
}

function searchForLabel(node, label) {
	var found = false;
	var n = node;
	while (n && n.nodeName!='BODY') {
		if (hasClassLabel(n, label)) {
			found = true;
		}
		n = n.parentNode;
	}
	if (found) {
		return n;
	} else {
		return null;
	}
}

function isUniqueName(name, divEl) {
	var cObj = null;
	if (divEl && divEl.fDiv) {
		cObj = divEl.fDiv;
	}
	var d = oEditor.FCK.EditorDocument;
	var objects = d.getElementsByTagName('DIV');
	var namesList = "";
	for (var i=0; i<objects.length; i++) {
//		if (hasClassLabel(objects[i], 'form')) {
			if (objects[i].id == name && objects[i]!=cObj) {
				return false;
			}
//		}
	}
	return true;
}
