// 	
//	AJAX functions for general use
//

//XMLHTTP request object
var xmlhttp;
var last_postback_event;

//Add a loading div to an element (removed when the element refreshes)
function addLoadingDIV(element)
{
	if(element)
	{
		html = element.innerHTML;
		html = '<div class="loading_div">' + html + "</div>";
		element.innerHTML = html;
	}
}

//Handle xml response
function genericCallbackMethod()
{
	//alert("readystate " + xmlhttp.readyState);
	var response = "";
	var chunk ="";
	var autoHideLoader = true;
	
	var debuggerer ="";
	
	//debug_switch = true;

	if (xmlhttp && xmlhttp.readyState==4)
	{				
		//alert("DEBUG: ajax callback");	
		var loop =0;
		//alert(xmlhttp.getAllResponseHeaders());
		//alert("event " + last_postback_event);
		if(debug_switch)
		{
			alert("Response XML " + xmlhttp.responseXML);
			alert("Response XML.xml " + xmlhttp.responseXML.xml);	
			alert("Response XML.documentElement " + xmlhttp.responseXML.documentElement);	
			alert("Response Text " + xmlhttp.responseText);
		}
		
		//firefox etc fix
		//if(!xmlhttp.responseXML.xml || xmlhttp.responseXML.xml.length<1)		
		//{
		//	response = (new DOMParser()).parseFromString(xmlhttp.responseText, "text/xml");		
			//alert("DOMparsed Response " + response);
		//	response = response.documentElement;
		//}
		//else			
		response = xmlhttp.responseXML.documentElement;								
		
		

		//alert("before loop");
		//iterate actions
		if(response && response.getElementsByTagName("action") && response.getElementsByTagName("action")[loop])
		while (action = response.getElementsByTagName("action")[loop])
		{				
			//alert("looping action "+ loop);
			//get element
			if(debug_switch)
			{
				alert("getting element " + action.getElementsByTagName("elementID")[0].childNodes[0].nodeValue);
			}
			debuggerer += "getting element " + action.getElementsByTagName("elementID")[0].childNodes[0].nodeValue;
			if(action.getElementsByTagName("elementID")[0])
				pageElement = $(action.getElementsByTagName("elementID")[0].childNodes[0].nodeValue);
			//get chunk
			chunk ="";
			//alert("getting chunk");
			if(action.getElementsByTagName("javascriptChunk")[0])
			{
				//The chunk might be huge (map)
				if(action.getElementsByTagName("javascriptChunk")[0].childNodes.length >1)
				{
					var chunks = action.getElementsByTagName("javascriptChunk")[0].childNodes.length;
					if(debug_switch)
						alert("have to rebuild chunk, it has: "+chunks+" bits");
					for(var k=0;k<chunks;k++)
					{
						if(debug_switch)
						{
							alert("re-building split chunk bit "+k+" :   " + action.getElementsByTagName("javascriptChunk")[0].childNodes[k].nodeValue);
						}
						chunk += action.getElementsByTagName("javascriptChunk")[0].childNodes[k].nodeValue;
					}					
				}
				else
				{
					chunk = action.getElementsByTagName("javascriptChunk")[0].childNodes[0].nodeValue;						
				}
			}
			
			if(pageElement)
			{
				//alert("setting element " + pageElement);
				//Do element refresh action
				if(HTML = action.getElementsByTagName("innerHTML")[0])
				{					
					if(HTML.childNodes.length >1)
					{
						var completeNode = "";						
						for(var j=0;j<HTML.childNodes.length;j++)
						{
							if(debug_switch)
							{
								alert("re-building split node " + HTML.childNodes[j].nodeValue);
							}
							completeNode += HTML.childNodes[j].nodeValue;
						}
						if(debug_switch)
						{
							alert("setting element " + completeNode);
						}
						pageElement.innerHTML = completeNode; 
						debuggerer += "setting element " + completeNode;
					}
					else
					{
						if(debug_switch)
						{
							alert("setting element " + HTML.childNodes[0].nodeValue);
						}
						pageElement.innerHTML = HTML.childNodes[0].nodeValue;
					}
				}
				//Do property set action
				if(property = action.getElementsByTagName("property")[0])
				{				
					value = action.getElementByTagName("value")[0].childNodes[0].nodeValue;					
					if(pageElement) pageElement.getAttribute(property.childNodes[0].nodeValue) = value;
				}
			}						
			
			if(chunk)
			{				
			
				//eval a chunk of javascript
				//alert("eval script " + chunk);
				try
				{
					//alert("pos1 "+chunk);
					eval(chunk);
					//console.log(chunk);
				}
				catch(err)
				{
					//console.log(chunk.replace(/\\/g,''));
					//alert("evaling "+chunk.replace(/\\/g,''));
					//alert("eval failed");					
					eval(chunk.replace(/\\/g,''));
					
					//extra debug
					//document.write()
				}
				//alert("script done");
			}
			
			loop++;
		}
		else		
		{
			alert("Oops, something went wrong, re-loading page.");
			alert(debuggerer);
			redirect(window.location.href);
		}
		
		//done, now hide the loader
		if(autoHideLoader)				
			HideLoader();		
	}
}

//Post to PHP page
function doCallbackPost(url,event,postData,preCallbackScript)
{		
	//alert("DEBUG: raising ajax event");

	if(xmlhttp && url == 'ajaxphp/cards_ajax.php')
	{
		xmlhttp.abort();
	}
	
	last_postback_event = event;
	if(debug_switch)		
		alert("Raising event " + last_postback_event);

	xmlhttp=GetXmlHttpObject();
	if(xmlhttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	//turn assoc array (postData) into post string.	
	var post = "postid=" + Math.random();
	for(var key in postData)	
	{
		//alert ("putting key " + key + ";" + postData[key] + " into the POST");
		post += "&" + "callback" + "=" + "true";
		if(!postData[key])
			post += "&" + key + "=" + ' ';
		else
			post += "&" + key + "=" + postData[key];
	}
	
	
	url = url+"?event="+event;
	//alert(url);
	
	xmlhttp.onreadystatechange=genericCallbackMethod;	
	xmlhttp.open("POST",url,true);
	
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", post.length);
	xmlhttp.setRequestHeader("Connection", "close");
		
	if(preCallbackScript)
	{
		eval(preCallbackScript);
		//alert(preCallbackScript);
	}

	//if(console)
		//console.log(url);

	xmlhttp.send(post);
}

//Get a new XMLHTTP request object
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function silentForumCreate(u,p,e,g,s)
{
	var postData = new Array();
	
	postData["u"] = u;
	postData["p"] = p;
	postData["e"] = e;

	//hideHelptext();
	doEtherPost("http://login.strongholdkingdoms.com/forumtest.php","profileSwitchSection",postData,"");
}

function doEtherPost(url,event,postData,preCallbackScript)
{		
	//alert("DEBUG: raising ajax event");

	if(xmlhttp && url == 'ajaxphp/cards_ajax.php')
	{
		xmlhttp.abort();
	}
	
	last_postback_event = event;
	if(debug_switch)		
		alert("Raising event " + last_postback_event);

	xmlhttp=GetXmlHttpObject();
	if(xmlhttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	//turn assoc array (postData) into post string.	
	var post = "postid=" + Math.random();
	for(var key in postData)	
	{
		//alert ("putting key " + key + ";" + postData[key] + " into the POST");
		post += "&" + "callback" + "=" + "true";
		if(!postData[key])
			post += "&" + key + "=" + ' ';
		else
			post += "&" + key + "=" + postData[key];
	}
	
	
	url = url+"?event="+event;
	//alert(url);
		
	xmlhttp.open("POST",url,true);
	
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", post.length);
	xmlhttp.setRequestHeader("Connection", "close");
			
	xmlhttp.send(post);
}

