/*
* Simple Ajax implementation
* @author: Joseph "Pcjoe" Florencio
* @date: 07-14-2005
*/

http_req = null;
function makeRequestBasic()
{	var http_req_temp = false;
	// Open new xmlhttpreqww
	if(window.XMLHttpRequest)
	{	http_req_temp = new XMLHttpRequest();
		if(http_req_temp.overrideMimeType)
		{//	http_req.overrideMimeType('text/xml');
			http_req_temp.overrideMimeType('text/html');
		}
	}
	else if(window.ActiveXObject)
	{	try
		{	http_req_temp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{	try
			{	http_req_temp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{}
		}
	}
	
	return http_req_temp;
}

function makeRequest( url, dontMakeHistory, stateChangeFunc, additional_info )
{	// Cancel older request
	if(http_req != null && http_req.readyState < 4)	
	{	http_req.abort();
		http_req = null;
	}
	
	http_req = makeRequestBasic();
	
	
	// Use default updateContents function if we don't define a stateChangeFunc
	if(stateChangeFunc == null)
		http_req.onreadystatechange = function() { updateContents(http_req); };
	else
	{	if(additional_info == null)
			http_req.onreadystatechange = function() { stateChangeFunc(http_req); };
		else
			http_req.onreadystatechange = function() { stateChangeFunc(http_req, additional_info); };
	}
	http_req.open('GET',url,true);
	http_req.send(null);
	
	// Push on history stack
	if(dontMakeHistory == null)
		makeHistory(url);
	// Update nav bar
	changeNavToCurrent(url);
}

function makeRequestLocal( url, dontMakeHistory, stateChangeFunc, additional_info )
{	var http_req_temp = makeRequestBasic();
	
	// Moved above to prevent reuse in IE of xmlhttprequest
	http_req_temp.open('GET',url,true);
	
	// Use default updateContents function if we don't define a stateChangeFunc
	if(stateChangeFunc == null)
		http_req_temp.onreadystatechange = function() { updateContents(http_req_temp); };
	else
	{	if(additional_info == null)
			http_req_temp.onreadystatechange = function() { stateChangeFunc(http_req_temp); };
		else
			http_req_temp.onreadystatechange = function() { stateChangeFunc(http_req_temp, additional_info); };
	}
	http_req_temp.open('GET',url,true);
	http_req_temp.send(null);
	
	// Push on history stack
	if(dontMakeHistory == null)
		makeHistory(url);
	// Update nav bar
	changeNavToCurrent(url);
}

function makeRequestPost( url, dontMakeHistory, stateChangeFunc, additional_info, post_param )
{	var http_req_temp = makeRequestBasic();
	
	// Moved above to prevent reuse in IE of xmlhttprequest
	http_req_temp.open('POST',url,true);
	
	// Use default updateContents function if we don't define a stateChangeFunc
	if(stateChangeFunc == null)
		http_req_temp.onreadystatechange = function() { updateContents(http_req_temp); };
	else
	{	if(additional_info == null)
			http_req_temp.onreadystatechange = function() { stateChangeFunc(http_req_temp); };
		else
			http_req_temp.onreadystatechange = function() { stateChangeFunc(http_req_temp, additional_info); };
	}
	
	http_req_temp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_req_temp.setRequestHeader("Content-length", post_param.length);
	http_req_temp.setRequestHeader("Connection", "close");
	http_req_temp.send(post_param);
	
	// Push on history stack
	if(dontMakeHistory == null)
		makeHistory(url);
	// Update nav bar
	changeNavToCurrent(url);
}

/*
* Loading Text
*/
loadingText = "<p class=\"loading\">Loading...</p>";
failedText = "<p class=\"failed\">Error loading data!</p>";

function updateContents(http_req)
{	content_win = document.getElementById("content_window");
	// Loading
	if(http_req.readyState < 4)
	{	content_win.innerHTML = loadingText;
	}
	// Done!
	else
	{	// Perfect, update content window
		if(http_req.status == 200)
		{	content_win.innerHTML = http_req.responseText;
			// HACK HACK: Just try to fill this variable, needed when anime page pops up
			oldListElem = document.getElementById("defaulttab");
			oldBoxElem = document.getElementById("atab_watched");
			updateBtnMatchCookieArray(new Array('desc', 'dl', 'anime_gal'));
		}
		else
		{	content_win.innerHTML = failedText;
		}
	}
}

function updateContentsComments(http_req, elementid)
{	comment_box = document.getElementById(elementid);
	// Complete
	if(http_req.readyState == 4)
	{	// Perfect, update content window
		if(http_req.status == 200)
		{	comment_box.innerHTML = http_req.responseText;
			// elementid is in the form of "comment_box_X", X being the id. Extract it, and reuse
			id = elementid.substring("comment_box_".length,elementid.length);
			// Since we updated the comment box, update the comment amount as well
		//	makeRequestLocal('comment.php?id='+id+'&show_total=1', true, updateContentsCommentsAmt, elementid+"_amt");
		}
		else
			comment_box.innerHTML = failedText;
	}	
	// Still loading
	else
	{	// Only display loading if it's 100% empty
		if(comment_box.innerHTML == "")
			comment_box.innerHTML = loadingText;
	}
}

function updateContentsCommentsAmt(http_req, elementid)
{	comment_box = document.getElementById(elementid);
	// Complete
	if(http_req.readyState == 4)
	{	// Perfect, update content window
		if(http_req.status == 200)
		{	// Insert only if it's a new number
			if(comment_box.innerHTML != http_req.responseText)
				comment_box.innerHTML = http_req.responseText;
		}
		else
			comment_box.innerHTML = failedText;
	}
}

function commentSubmit(news_id)
{
	var strPost = "name="+encodeURI( document.getElementById("cmt_name_"+news_id).value )+
					"&website="+encodeURI( document.getElementById("cmt_web_"+news_id).value )+
					"&post="+encodeURI( document.getElementById("cmt_post_"+news_id).value );
	
	makeRequestPost('comment.php?id='+news_id, true, updateContentsComments, "comment_box_"+news_id, strPost);
}

// Comment ajax functions
function showComments(news_id)
{
	comment_box = document.getElementById("comment_box_"+news_id);
	// Nothing, show comments
	if(comment_box && comment_box.innerHTML.length == 0)
	{	makeRequest('comment.php?id='+news_id, true, updateContentsComments, "comment_box_"+news_id);
	}
	// Filled, empty
	else if(comment_box)
	{	comment_box.innerHTML = "";
	}
}

// Make History
function makeHistory(newHash)
{	window.location.hash = newHash;
}