//copyright, BENE ÁDÁM © - all rights reserved
//JavaScript - base | alapok

//----- ALAPOK -----
var nn4 = ((navigator.appName.indexOf("Netscape") > -1) && (navigator.appVersion.indexOf("4") > -1)) ? true : false;

function isIE() {
	if(navigator.userAgent.toLowerCase().indexOf('msie')!=-1) return true;
	return false;
}

// ---------- PAGE|WINDOW

function getPageTop() {
	Y=0;
	if (typeof(window.pageYOffset)=='number') {
		Y=window.pageYOffset;
	} else
	if (document.body && document.body.scrollTop) {
		Y=document.body.scrollTop;
	} else
	if (document.documentElement && document.documentElement.scrollTop) {
	    Y=document.documentElement.scrollTop;
	}
	return Y;
}

function getPageLeft() {
	X=0;
	if (typeof(window.pageXOffset)=='number') {
		X=window.pageXOffset;
	} else
	if (document.body && document.body.scrollLeft) {
		X=document.body.scrollLeft;
	} else
	if (document.documentElement && document.documentElement.scrollLeft) {
	    X=document.documentElement.scrollLeft;
	}
	return X;
}

function getWindowHeight() {
if (self.innerHeight) {
frameHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) {
frameHeight = document.documentElement.clientHeight;
} else if (document.body) {
frameHeight = document.body.clientHeight;
}
return parseInt(frameHeight);
}

function getWindowWidth() {
if (self.innerWidth) {
frameWidth = self.innerWidth;
} else if (document.documentElement && document.documentElement.clientWidth) {
frameWidth = document.documentElement.clientWidth;
} else if (document.body) {
frameWidth = document.body.clientWidth;
}
return parseInt(frameWidth);
}

function getPageHeight() {
/*var y;
var test1 = document.body.scrollHeight;
var test2 = document.body.offsetHeight
if (test1 > test2) {
y = document.body.scrollHeight;
} else {
y = document.body.offsetHeight;
}*/
var y=document.body.scrollHeight||document.body.offsetHeight;
return parseInt(y);
}

function getPageWidth() {
/*var y;
var test1 = document.body.scrollWidth;
var test2 = document.body.offsetWidth
if (test1 > test2) {
y = document.body.scrollWidth;
} else {
y = document.body.offsetWidth;
}*/
var y = window.screen.availWidth||getWindowWidth();
return parseInt(y);
}

// ---------- PAGE|WINDOW END

// ---------- ELEMENT

function getElementLeft(e) {
	var r = e.offsetLeft;
	p = e.offsetParent;
	while (p) {
		r += p.offsetLeft;
		p = p.offsetParent;
	}
	return r;
}

function getElementTop(e) {
	var r = e.offsetTop;
	p = e.offsetParent;
	while (p) {
		r += p.offsetTop;
		p = p.offsetParent;
	}
	return r;
}

function getElementWidth(e) {
	return e.offsetWidth;
}

function getElementHeight(e) {
	return e.offsetHeight;
}

function getTop(obj) {
	var s = obj.style.top;
	return Number(s.slice(0,s.length-2));
}

function setTop(obj,Y) {
	obj.style.top=String(Math.round(Y))+'px';
}

function setLeft(obj,X) {
	obj.style.left=String(Math.round(X))+'px';
}

function setHeight(obj,H) {
	obj.style.height=H+'px';
}

function setWidth(obj,W) {
	obj.style.width=W+'px';
}

function setOpacity(obj,percent) {
	var s = percent/100;
	obj.style.filter="alpha(opacity="+percent+")";
	obj.style.mozOpacity=s;
	obj.style.opacity=s;
}

function getChildren(el,tagName) {
	var r=new Array();
	var t=el.getElementsByTagName(tagName);
	for (var i=0;i<t.length;i++) if (t[i].parentNode==el) r.push(t[i]);
	return r;
}

function getElementBackgroundColor(el) {
	if(el.currentStyle) return el.currentStyle.backgroundColor;
	if(document.defaultView) return document.defaultView.getComputedStyle(el, '').getPropertyValue("background-color");
	return "#000000";
}

// ---------- ELEMENT END

// ---------- FORM

function checkForm(form) {
	if (!form) return false;
	var is = form.getElementsByTagName("input");
	var ts = form.getElementsByTagName("textarea");
	for (var i in is) if (is[i].value=="") return "incomplete";
	for (var i in ts) if (ts[i].value=="") return "incomplete";
	return "filled";
}

function formToArray(form) {
	if (!form) return false;
	var a = new Array();
	var is = form.getElementsByTagName("input");
	var ts = form.getElementsByTagName("textarea");
	for (var i=0;i<is.length;i++) {
		if (is[i].type=='checkbox') {
			if (is[i].checked) a[is[i].name]=is[i].value;
		} else a[is[i].name]=is[i].value;
	}
	for (var i=0;i<ts.length;i++) a[ts[i].name]=ts[i].value;
	//for (var i in ts) a[ts[i].name]=ts[i].value;
	return a;
}

// ---------- FORM END

function getRuleTextByName(name) {
	var theRules = new Array();
	theRules = (document.styleSheets[0].cssRules)?(document.styleSheets[0].cssRules):((document.styleSheets[0].rules)?(document.styleSheets[0].rules):(false));
	for (i=0;i<theRules.length;i++) {
		if (theRules[i].selectorText.toUpperCase()==name.toUpperCase()) {
			return theRules[i].style.cssText.toUpperCase();
		}
	}
	return false;
}

function getStyleProperty(name,prop) {
	var text=getRuleTextByName(name);
	text=text.replace(/ /g,'');
	var br = text.split(";");
	var br2;
	for (i=0;i<br.length;i++) {
		if (br[i]!="") {
			br2=br[i].split(":");
			if (br2[0]==prop.toUpperCase()) {
				return br2[1];
			}
		}
	}
	return false;
}

function getPropertyById(id,prop) {
	var ob=document.getElementById(id);
	return getStyleProperty("."+document.getElementById(id).className,prop);
}

function getTopp(obj) {
	var s = obj.style.marginTop;
	return Number(s.slice(0,s.length-2));
}

function getMargin() {
	var s=getStyleProperty('body','margin');
	if (!s) {
		s=getStyleProperty('body','margin-top');
	}
	s=s.replace('PX','');
	return Number(s);
}

// ---------- EVENT

function addEvent(el,evt,func) {
	function newFunc() {
		if (typeof(prevFunc)=="function") prevFunc();
		func();
	}
	eval("var prevFunc = el.on"+evt);
	eval("el.on"+evt+" = newFunc");
}

function XXXaddEventByObject(obj,event_in,code) {
	var event=String(event_in).toLowerCase();
	if (obj.addEventListener) {
		obj.addEventListener(event, new Function('event', code), false);
	} else {
		if (event=='scroll') {
			var newFunc = new Function(code);
			var oldHandler = document.body.onscroll;
			if(typeof document.body.onscroll != 'function')
				document.body.onscroll = newFunc;
			else {
				document.body.onscroll = function() {
					oldHandler();
					newFunc();
				}
			}
		} else {
			obj.attachEvent("on"+event, new Function('event', code));
		}
	}
	/*var oc=obj.getAttribute(event);
	if (oc===null) {oc="";}
	var noc="";
	if (isIE()) {
		var s=oc.toString();
		s=s.substr(s.indexOf('{')+1);
		noc=s.substr(0,s.length-1);
		noc+=String(code);
		obj.setAttribute(event,new Function('e',noc));
	} else {
		noc=String(oc)+String(code);
		obj.setAttribute(event,new Function('e',noc));
	}*/
	return true;//noc;
}

function addEventByObject(obj,event_in,code) {
	var event=String(event_in).toLowerCase();
	if (obj.addEventListener) {
		obj.addEventListener(event, code, false);
	} else {
		if (event=='scroll') {
			//alert(typeof code);
			var oldHandler = document.body.onscroll;
			if(typeof document.body.onscroll != 'function')
				document.body.onscroll = code;
			else {
				document.body.onscroll = function() {
					oldHandler();
					code();
				}
			}
		} else {
			obj.attachEvent("on"+event, code);
		}
	}
	return true;
}

function addEventByTagName(tagname,event,code) {
	return addEventByObject(document.getElementsByTagName(String(tagname).toLowerCase())[0],event,code);
}

function addEventById(id,event,code) {
	return addEventByObject(document.getElementById(id),event,code);
}

function addGlobalEvent(event_in,code) {
	var event=event_in.toLowerCase();
	if (isIE()) {
		if (event=="resize") {
			addEventByObject(window,event,code);
		} else {
			addEventByTagName("body",event,code);
		}
	} else {
		addEventByObject(window,event,code);
	}
}

// ---------- EVENT END

// ---------- CODING

function urlencode(str) {
//str = escape(str);
if ((typeof str).toLowerCase() != "string") {
	try {
		str=str.toString();
	} catch(e) {
		str += '';
	}
}
str = str.replace(/\$/g, '%24');
str = str.replace(/&/g, '%26');
str = str.replace(/\+/g, '%2B');
str = str.replace(/\//g, '%2F');
str = str.replace(/=/g, '%3D');
str = str.replace(/%20/g, '+');
str = str.replace(/\*/g, '%2A');
str = str.replace(/\?/g, '%3F');
str = str.replace(/\@/g, '%40');
//alert(str)
return str;
}

function xxurlencode(b) {
var c= '';
for(i=0; i<b.length; i++){
	if(b.charCodeAt(i)>127){ c += '&#' + b.charCodeAt(i) + ';'; }else{ c += b.charAt(i); }
}
alert(c);
return c;
}

function urldecode(str) {
str = str.replace(/\+/g, ' ');
str = unescape(str);
return str;
}

function countProperties(obj) {
	if (typeof obj=="object") {
		var c=0;
		for (i in obj) c++;
		return c;
	} else return false;
}

function base64Encode(input) {
   var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

// ---------- CODING END

// ---------- CLASS

function Class(source)
{
	var self=function () {
		if (typeof(this.construct)=="function")
			this.construct.apply(this,arguments);
	}

	for(var i in source) {
		self.prototype[i]=source[i];
	}

	self.Extends=function (parent) {
		if(!this.parent) {
			for(var i in parent.prototype) {
				if (this.prototype[i]===undefined)
					this.prototype[i]=parent.prototype[i];
			}
			this.prototype.parent=parent.prototype;
		}
		return this;
	}

	return self;
}

Array.create=function (o) {
	var a=[];
	for (var i=0; i<o.length; i++)
		a[i]=o[i];
	return a;
}

Function.prototype.delay=function (destination,msecs) {
	var method=this;
	return function () {
		var a=Array.create(arguments);
		setTimeout(function () {
			method.apply(destination,a);
		},msecs);
	}
}

Function.prototype.interval=function (destination,msecs) {
	var method=this;
	var a=Array.create(arguments);
	return setInterval(function() {method.apply(destination,a);},msecs);
}

Function.prototype.setTo=function (method) {
	method=this;
	return true;
}


/*var Osztaly = Class({
	construct: function () {
		this.arguments = arguments;
		this.alert_v.delay(this,5000)();
		this.text=this.arguments[0];
	},
	alert_v: function () {
		alert(this.text);
	}
});
var a=new Osztaly("aa");
var c=new Osztaly("naanaa");
*/



var TToolTip = Class({
	construct: function (tips_in,bgColor,color) {
		this.arguments = arguments;
		this.tips=tips_in;
		this.t_id = 0;
		this.i_id = 0;
		this.opacity = 100;
		this.maxOpacity = 80;
		this.timeoutTime = 700;
		this.visible = false;
		this.h = document.createElement("DIV");
		setOpacity(this.h,this.maxOpacity);
		this.h.innerHTML="";
		this.h.style.position="absolute";
		this.h.style.display="none";
		this.h.style.width="auto";
		this.h.style.height="auto";
		this.h.style.padding="5px";
		this.h.style.color=color?color:"#000000";
		this.h.style.backgroundColor=bgColor?bgColor:"#ffff99";
		this.h.style.border="1px solid black";
		document.getElementsByTagName("BODY")[0].appendChild(this.h);
	},
	_Show: function () {
		if (this.t_id!=0) {
			clearTimeout(this.t_id);
			this.t_id=0;
		}
		if (this.i_id!=0) {
			clearInterval(this.i_id);
			this.i_id=0;
		}
		this.opacity=this.maxOpacity;
		setOpacity(this.h,this.opacity);
		this.h.style.display="inline-block";
		this.visible=true;
	},
	Show: function (obj_in, tip_in, options) {
		/*if ((!tip_in||!obj_in)||(this.i_id!=0)) return false;
		if (!tip_in||!obj_in) return false;*/
		this._Show();
		var tip=(this.tips&&this.tips[tip_in])?this.tips[tip_in]:tip_in;
		this.h.innerHTML=tip;
		this.h.style.left=(getElementLeft(obj_in)+getElementWidth(obj_in))+"px";
		//this.h.style.top=(getElementHeight(obj_in)+getElementTop(obj_in))+"px";
		this.h.style.top=getElementTop(obj_in)+"px";
		if (options=="holdover") {
		var _t=this;
		this.h.onmouseover=function(){_t._Show();}
		this.h.onmouseout=function(){_t.Hide();}
		} else {
			this.h.onmouseover=this.h.onmouseout=function(){};
		}
	},
	Hide: function () {
		this.HideFade();
	},
	_Hide: function () {
		this.h.style.display="none";
		this.visible=false;
		clearTimeout(this.t_id);
		this.t_id=0;
		clearInterval(this.i_id);
		this.i_id=0;
	},
	HideTimeout: function () {
		if (this.t_id!=0) clearInterval(this.t_id);
		var _t=this;
		this.t_id = setTimeout(function(){_t._Hide();},this.timeoutTime);
	},
	HideFade: function () {
		//if (this.t_id!=0) clearInterval(this.t_id);
		if (this.i_id!=0) return false;
		var _t=this;
		this.i_id = setInterval(function(){
			_t.opacity-=15;
			_t.opacity=(_t.opacity<=0?0:_t.opacity);
			_t.opacity=(_t.opacity>=_t.maxOpacity?_t.maxOpacity:_t.opacity);
			setOpacity(_t.h,_t.opacity);
			if (_t.opacity<=0) {
				_t.opacity=0;
				_t._Hide();
			}
		},40);
		//this.t_id = setTimeout(function(){_t._Hide();},this.timeoutTime);
	}
});



// ---------- CLASS END

// ---------- EGYEBEK

function treshold(f,low,high) {
	return (f<low)?low:((f>high)?high:f);
}

function roundEx(f,d) {
	if (typeof d=="undefined") return Math.round(f);
	d=Math.round(d)*10;
	return Math.round(f*d)/d;
}

function checkEnter(e, func){
	var characterCode;
	if(e && e.which){
		e = e;
		characterCode = e.which;
	} else {
		e = event;
		characterCode = e.keyCode;
	}	 
	if (characterCode == 13) {
		eval(func);
		return false;
	}
	return true;
}

function myGetTime() {
	var d = new Date();
	return d.getTime();
}

function writeLoading() {
	s = "<image src=\"loading_line.gif\" width=\"50px\" height=\"5px\">";
	return s;
}

function writeByID(text, ID) {
	document.getElementById(ID).innerHTML = text;
}

function EvalResponse(response) {
	tag = "script";
	vissza = "";
	startpoz = -1;
	endpoz = -1;
	startpoz = response.indexOf("<"+tag+">", startpoz + 1);

	// js részek kinyerése
	while (startpoz > -1) {
		// értelmes válasz kigyűjtése
		vissza +=  response.substr(0, startpoz);
		response = response.substr(startpoz + tag.length + 2);

		startpoz  = -1;
		endpoz = response.indexOf("</"+tag+">", startpoz + 1);

		if (endpoz > -1) {
			futtat =  response.substr(0, endpoz);
			response = response.substr(endpoz + tag.length + 3);
			eval(futtat);
		}
	startpoz = response.indexOf("<"+tag+">", startpoz + 1);
	}

	// maradék rész a válasz része
	if (response != "") {
		vissza += response;
	}
	return vissza;
}

// ---------- EGYEBEK END

// ---------- FRAMEWORK - ADAM'S

function setupElementsForPrototype() {
//<!--[if IE]><style type="text/css">*{behavior:url('ie_fix.htc')}</style><![endif]-->
if (isIE())
if ((typeof Element).toLowerCase()=="undefined")
{
var styleNode = document.createElement('style');
styleNode.type = "text/css";
styleNode.styleSheet.cssText = "*{behavior:url('ie_fix.htc')}";
document.getElementsByTagName('head')[0].appendChild(styleNode);
//if(!window.Element){
Element=function(){};
//}
}
}
