// http://www.sitepoint.com/blogs/2009/09/03/javascript-session-variable-library/
// http://www.loadinfo.net/

var tableWidth = 0;
var tableHeight = 0;
  
/*  
var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
}
*/

function reqCreate(req) {
  if (window.ActiveXObject)  {
    // IE
    try {
      req = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch (e) { }
  }
  else {
    // Mozilla
    if (window.XMLHttpRequest) {
      try {
        req = new XMLHttpRequest();
      }
      catch (e) { }
    }
  }
  return req;
}

function getHTTP(element,URL, mode) {

  hideGaleryAll('show_gallery');

  var req = null;
  req = reqCreate(req);

  wpanel = document.getElementById(element);
  wpanel.style.visibility = 'visible';
  wpanel.innerHTML = '<div align="center"><img src="img/loaderb64.gif"></div>';
  
  if (req == null) {
    alert('Az ön böngészője nem támogatja ezt az oldalt. Javasolt böngésző: Internet Explorer.');
  } 
  else {
    
    req.onreadystatechange = function () {
      if (req.readyState == 4) {
        if (req.status == 200){
          wpanel.innerHTML = req.responseText;
          wpanel.style.visibility = 'visible';
        }
      }
    }  
    
    try {
      my_lang = Session.get("my_lang");
      if (my_lang != 'hun') {
        URL = my_lang + "/" + URL;
      }
      req.open("post","manage_messages.php",true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-2");
      req.send("file=" + URL+ "&mode="+ mode);
    }
    catch (e) {
      alert(e.description);
    };
  }
}

function callPHP(fileName, parameters, onreadystatechange) {

  var req = null;
  req = reqCreate(req);
  if (req == null) {
    alert('Az ön böngészője nem támogatja ezt az oldalt. Javasolt böngésző: Internet Explorer.');
  } 
  else {
    // req.onreadystatechange = onreadystatechange;
    
    try {
      req.open("post", fileName, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-2");
      req.send(parameters);
    }
    catch (e) {
      alert(e.description);
    }
  }
  return req;
}

var reqGetWorkAreaSize = null;

function onreadystatechangeGetWorkAreaSize() {
  if (reqGetWorkAreaSize.readyState == 4) {
    if (reqGetWorkAreaSize.status == 200){
    }
  }
}

function init() {
  tableWidth = document.getElementById('main_table').clientWidth;
  tableHeight = document.getElementById('main_table').clientHeight;
  Session.set("tableWidth", tableWidth);
  Session.set("tableHeight", tableHeight);
  // reqGetWorkAreaSize = callPHP(reqGetWorkAreaSize, 'getworkareasize.php', "&tableWidth="+ tableWidth+ "&tableHeight="+ tableHeight, onreadystatechangeGetWorkAreaSize);
  document.getElementById('top_menu').style.display = 'block';
  getHTTP('work_area', 'menu_home.html', 'html');
}

function setLang() {
  if (my_lang == 'hun') {
    my_lang = 'eng';
  }
  else {
    my_lang = 'hun';
  }
  Session.set("my_lang", my_lang);
  callPHP('set_lang.php', 'lang=' + my_lang, null);
  window.location.reload('index.php');
}
	
window.onload = init;

