function loadjscssfile (filename, filetype) {
	createCookie("style", filename, 365);
	if (filetype == "js") { //if filename is a external JavaScript file
		var fileref = document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", filename);
	}
	else if (filetype == "css") { //if filename is an external CSS file
		var fileref = document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
		/// Works if path won't be changed !!!
		//alert(filename+" == /MVHPortal/mvh_theme/css/high_contrast.css");
		if (filename == "/MVHPortal/mvh_theme/css/high_contrast.css") {
			unsetMenuEvents ();
		}
	}
	if (typeof fileref != "undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref);
}

function unsetMenuEvents () {
	var activeStyleFileName = readCookie('style');
	if (activeStyleFileName == "/MVHPortal/mvh_theme/css/high_contrast.css") {
//		alert(allMenuDIVs[0].onmouseover);
		allMenuDIVs = getElementsByClassName(document, "div", "menuElement");
		for (var i = 0; i < allMenuDIVs.length; i++) {
			allMenuDIVs[i].onmouseover = allMenuDIVs[i].onmouseout = null;
			allMenuDIVs[i].style.backgroundImage = "";
		}
	//	alert(allMenuDIVs[0].onmouseover);
		allMenuSubDIVs1 = getElementsByClassName(document, "div", "menuElementSub1");
		for (var i = 0; i < allMenuSubDIVs1.length; i++) {
			allMenuSubDIVs1[i].onmouseover = allMenuSubDIVs1[i].onmouseout = null;
			allMenuSubDIVs1[i].style.backgroundImage = "";
		}		
		allMenuSubDIVs2 = getElementsByClassName(document, "div", "menuElementSub2");
		for (var i = 0; i < allMenuSubDIVs2.length; i++) {
			allMenuSubDIVs2[i].onmouseover = allMenuSubDIVs2[i].onmouseout = null;
			allMenuSubDIVs2[i].style.backgroundImage = "";
		}		
		allMenuSubDIVs3 = getElementsByClassName(document, "div", "menuElementSub3");
		for (var i = 0; i < allMenuSubDIVs3.length; i++) {
			allMenuSubDIVs3[i].onmouseover = allMenuSubDIVs3[i].onmouseout = null;
			allMenuSubDIVs3[i].style.backgroundImage = "";
		}		
	}
}

/* HOW TO USE
loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file
*/
/*function setActiveStyleSheet(title) {
  createCookie("style", title, 365);
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      //enable the ones that are needed
      if (a.getAttribute("title") == title) a.disabled = false;
    }
  }
}*/
/*
function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}*/
/*
function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}*/

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return false;
}


// ha van már, beállítjuk
if (readCookie('style')) {
    var activeStyleFileName = readCookie('style');
    //setActiveStyleSheet(activeStyle);
	loadjscssfile (activeStyleFileName, "css");
	/*if (filename == "/MVHPortal/mvh_theme/css/high_contrast.css") {
		 setTimeout('addToOnload(document.body, unsetMenuEvents2())', 300);
	}*/
}
