var timerID = 0;
var charset = "";

function switchCategoryList(cat,selected){
	var subcatdiv = document.getElementById("subcat_" + cat);
	var imagecat = document.getElementById("imgcat_" + cat);

	if (subcatdiv.style.display == ""){		
		subcatdiv.style.display = "none";
		imagecat.src = app_path + "/img/open.gif";
	}else{
		subcatdiv.style.display = "";
		if (selected) imagecat.src = app_path + "/img/close.gif";
			else imagecat.src = app_path + "/img/open.gif";
	}
	return false;
}

function resetMainMenu(){
	i = 0;
	var option = document.getElementById ("menu_" + i);
	while (option){
		option.className="";
		i++;
		option = document.getElementById ("menu_" + i);
	}
}

function loadArticles(section,menuOption){
	stopTimer();
	if (xmlhttp) {
		var target = document.getElementById ("submenu");
		var option = document.getElementById (menuOption);
		
		target.innerHTML = "<img src='"+app_path+"/img/ajax-loader2.gif'/>";
		url = app_path + "/articles/ajax/list.php?sec=" + section;

		var myxmlhttp = new myXMLHttpRequest ();
		if (myxmlhttp) {
			myxmlhttp.open ("GET", url, true);
			if (myxmlhttp.overrideMimeType) {
				myxmlhttp.overrideMimeType("text/html; charset=" + charset);
			}
			myxmlhttp.send (null);		
						
			myxmlhttp.onreadystatechange = function () {
				if (myxmlhttp.readyState == 4) {
					var responseString = myxmlhttp.responseText;
					var errormatch = new RegExp ("^ERROR:");
					if (responseString.match (errormatch)) {
						target.innerHTML = "ERROR";					
					} else {					
						target.innerHTML = responseString;	
						resetMainMenu();							
						option.className = "selected";
					}
				}
			}			
		}
	}
	loadSectionInfo(section);
}

function loadSectionInfo(section){
	if (xmlhttp) {
		var target = document.getElementById ("main");
		target.innerHTML = "<div id=\"messagebody\"><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src='"+app_path+"/img/ajax-loader3.gif'/></div>";
		url = app_path + "/messages/ajax/sectioninfo.php?sec=" + section;

		var myxmlhttp = new myXMLHttpRequest ();
		if (myxmlhttp) {
			myxmlhttp.open ("GET", url, true);
			if (myxmlhttp.overrideMimeType) {
				myxmlhttp.overrideMimeType("text/html; charset=" + charset);
			}
			myxmlhttp.send (null);		
						
			myxmlhttp.onreadystatechange = function () {
				if (myxmlhttp.readyState == 4) {
					var responseString = myxmlhttp.responseText;
					var errormatch = new RegExp ("^ERROR:");
					if (responseString=="ERROR") {
						restoreMessage();				
					} else {
						target.innerHTML = responseString;		;			
					}
				}
			}			
		}
	}
}

function restoreMessage(){
	if (xmlhttp) {					
		url = app_path + "/messages/ajax/detail.php";
	    var target = document.getElementById ("main");
		var myxmlhttp = new myXMLHttpRequest ();
		if (myxmlhttp) {			
			myxmlhttp.open ("GET", url, true);
			if (myxmlhttp.overrideMimeType) {
				myxmlhttp.overrideMimeType("text/html; charset=" + charset);
			}
			myxmlhttp.send (null);		
						
			myxmlhttp.onreadystatechange = function () {
				if (myxmlhttp.readyState == 4) {								
					var responseString = myxmlhttp.responseText;
					var errormatch = new RegExp ("^ERROR:");
					if (responseString.match (errormatch)) {
						target.innerHTML = "ERROR";					
					} else {																												
						target.innerHTML = responseString;													
						new Effect.Appear(target);							
						messagearea	= document.getElementById ("messagebody");	
						timerID = setTimeout("refreshMessage()", 15000);			
					}
				}
			}						
		}
	}	
}

function refreshMessage(){
	//Element.hide(messagearea);	
	//new Effect.Fade(messagearea, {duration:.2});	
	if (xmlhttp) {					
		url = app_path + "/messages/ajax/detail.php";

		var myxmlhttp = new myXMLHttpRequest ();
		if (myxmlhttp) {			
			myxmlhttp.open ("GET", url, true);
			if (myxmlhttp.overrideMimeType) {
				myxmlhttp.overrideMimeType("text/html; charset=" + charset);
			}
			myxmlhttp.send (null);		
						
			myxmlhttp.onreadystatechange = function () {
				if (myxmlhttp.readyState == 4) {								
					var responseString = myxmlhttp.responseText;
					var errormatch = new RegExp ("^ERROR:");
					if (responseString.match (errormatch)) {
						messagearea.innerHTML = "ERROR";					
					} else {					
						Element.hide(messagearea);																							
						messagearea.innerHTML = responseString;													
						new Effect.Appear(messagearea);							
					}
				}
			}						
		}
	}
	timerID = setTimeout("refreshMessage()", 15000);
}

function stopTimer(){
   if(timerID) {
      clearTimeout(timerID);
      timerID  = 0;
   }
}