var is_opera = (navigator.userAgent.indexOf('Opera')!=-1);
var is_ie = (!is_opera && navigator.userAgent.indexOf('MSIE')!=-1);

function include() {
	var head = document.getElementsByTagName('head').item(0);
	var d = new Date();
	for(var i=0;i<arguments.length;i++) {
	    var js = document.createElement('script');
	    js.setAttribute('language', 'javascript');
	    js.setAttribute('type', 'text/javascript');
	    js.setAttribute('src', ''+arguments[i]+"?"+parseInt(d.getTime()) );
	    head.appendChild(js);
	}
    return false;
}

function addEvent(obj, evType, fn, useCapture) {
	if (obj.addEventListener) {
		obj.addEventListener(evType,fn,useCapture);
		return true;
	} else if(obj.attachEvent){
		return obj.attachEvent("on"+evType,fn);
	} else {
		return false;
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if (node == null) node = document;
	if (tag == null) tag = '*';
	var elems = node.getElementsByTagName(tag);
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	var i = 0;
	for (var j = 0; j < elems.length; j++) {
		if ( pattern.test(elems[j].className) ) {
			classElements[i++] = elems[j];
		}
	}
	return classElements;
}

function getFirstElementByClass(searchClass,node,tag) {
	if (node == null) node = document;
	if (tag == null) tag = '*';
	var elems = getElementsByClass(searchClass,node,tag);
	if (elems.length > 0) return elems[0];
	return null;
}

include('/hungary/homepage/js/stats.js');
include('/hungary/homepage/js/cbollogin.js');





/*
	Ajax

*/





function sack(file) {
	this.xmlhttp = null;


	this.resetData = function() {
		this.method = "GET";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}




/*
	Citibank header- és footer include

*/
var includer = new Object();
includer.footerTypes = new Array(	'footer_normal_hu',
									'footer_normal_en',
									'footer_xhtml_hu',
									'footer_xhtml_hu2',
									'footer_xhtml_en',
									'footer_shell_hu',
									'footer_popup_hu',
									'footer_popup_en',
									'footer_plat_hu',
									'footer_gold_hu'
								);

includer.headerTypes = new Array(	'header_x_n0_hu',		//nyitólap: xhtml, nincs BW, nincs kijelölt menü, van citigold, magyar
									'header_x_n1_hu',		//landingek: xhtml, nincs BW, nincs SEMMI menü, nincs citigold, magyar
									'header_h_n1_hu',		//landingek: html, nincs BW, nincs SEMMI menü, nincs citigold, magyar
									'header_h_n0_en',		//nyitólap, angol, semmi sem aktív
									'header_h_n1_en',		//nyitólap, angol, semmi sem aktív, nincsenek is menük
									'header_h_n2_en',		//nyitólap, angol, semmi sem aktív, nincsenek is menük, van IBAN kalk.
									'header_h_n2_hu',		//online banking aktív, nincs citigold, magyar
									'header_h_n3_hu',		//egyikmenü sem aktív, nincs citigold, magyar
									'header_h_n4_hu',		//termekek es szolgaltatasok aktív, nincs citigold, magyar
									'header_h_n5_hu',		//termekek es szolgaltatasok aktív, van citigold, magyar
									'header_h_n6_hu',		//információ aktív, nincs citigold, magyar
									'header_h_b0_hu',		//citibusiness, főoldal aktív, magyar
									'header_h_b1_hu',		//citibusiness, semmi sem aktív, magyar
									'header_h_b2_hu',		//citibusiness, számlavezetés aktív, magyar
									'header_h_b3_hu',		//citibusiness, hitelezés aktív, magyar
									'header_h_b4_hu',		//citibusiness, egyéb aktív, magyar
									'header_h_b5_hu',		//citibusiness, kapcsolat aktív, magyar
									'header_h_b0_en',		//citibusiness, csak home van, angol
									'header_x_b0_hu',		//citibusiness, főoldal aktív, magyar, xhtml
									'header_h_p0'			//popup, csak logo van
								);

includer.dir = "/hungary/homepage/includes/";



includer.initIncludes = function(){


	//header include:
	headerInclude = new sack();
	headerInclude.method="GET";
	var headerIncludeType=includer.getIncludeType("header");
	if(headerIncludeType!=''){
		headerInclude.requestFile = includer.dir+headerIncludeType+'.xml';
		headerInclude.onCompletion = function(){
			incObj=document.getElementById(headerIncludeType);
			if(incObj!=null){incObj.innerHTML=headerInclude.response;}
		};
		headerInclude.runAJAX();
	}
	//footer include:
	footerInclude = new sack();
	footerInclude.method="GET";
	var footerIncludeType=includer.getIncludeType("footer");
	if(footerIncludeType!=''){
		footerInclude.requestFile = '/hungary/homepage/includes/'+footerIncludeType+'.xml';
		footerInclude.onCompletion = function(){
			incObj=document.getElementById(footerIncludeType);
			if(incObj!=null){incObj.innerHTML=footerInclude.response;}
		};
		footerInclude.runAJAX();
	}
};

includer.getIncludeType = function (type){

	//type = header || footer
	for(fit=0;fit<eval("includer."+type+"Types.length");fit++){
		if(document.getElementById(eval("includer."+type+"Types[fit]"))!=null){return eval("includer."+type+"Types[fit]");}
	}
	return '';
};

addEvent(window, 'load', includer.initIncludes);