/*

	Methods:
	runAJAX(page, elementID, append, method)
	toggle(elementID);

*/

/*
	
	Useage:
		page		-	location/page.ext	- Which page to process.
		elementID	-	idNameOfElement		- Update an elementID with the output from the page.
		append		-	true || false		- Add the content on to the end of the innerHTML or not.
		method		-	POST || GET			- How we request the data to be transmitted.
		
	Support:
		Internet Explorer 5					- successful
		Internet Explorer 6					- successful
		Internet Explorer 7					- successful
		Mozilla Firefox						- successful
		Google Chrome						- successful
		Opera								- failed
		
	Warning:
		When using any form of dynamic loader, ensure that if you are using an auto re-load
		method, check for mySQL vulnerabilites, don't process too many queries either, could
		lead to a server crash.
		
	Copyright:
		All source is copyright PHPOutburst.
		This source is free under the GNU/GPL as long as all of this comment code remains as is
		upon issue. We are here to help and get credit where due.
		
*/

function runAJAX(page, elementID, append, method){
	
	//Default method
	if (typeof(method) == 'undefined') var method = 'GET';
	if (typeof(append) == 'undefined') var append = false;
	
	var xmlhttp;
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject){
		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}

	//We should now have xmlhttp in a variable.
	
	//If we are expecting output.
	if (elementID != ''){
		xmlhttp.onreadystatechange=function(){
			//If the state has obtained output
			if(xmlhttp.readyState==4){
				//Obtain the element from the page
				obj = document.getElementById(elementID);
				
				//Insert the contents
				if (typeof(obj.value) != 'undefined'){
					//If we have an obj.value such as a form.
					if (append == true){
							obj.value = "" + obj.value + "" + xmlhttp.responseText;
						} else {
							obj.value = xmlhttp.responseText;
						}
				} else {
					//If we don't have an obj.value such as a DIV element.
					if (append == true){
						obj.innerHTML = "" + obj.innerHTML + "" + xmlhttp.responseText;
					} else {
						obj.innerHTML = xmlhttp.responseText;
					} //end if
				} //end if
				
			}
		}
	} //end if

	
	xmlhttp.open(method, page, true);
	xmlhttp.send(null);

	//Lets check if the result is cached for clearing.
	//Damn browser support.
	if(xmlhttp.getAllResponseHeaders()) {
		var cached = xmlhttp;

		if (window.XMLHttpRequest){
			// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		} else if (window.ActiveXObject){
			// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			alert("Your browser does not support XMLHTTP!");
		}


		var ifModifiedSince = cached.getResponseHeader("Last-Modified");
		ifModifiedSince = (ifModifiedSince) ? ifModifiedSince : new Date(0); // January 1, 1970
		xmlhttp.open(method, page, true);
		xmlhttp.setRequestHeader("If-Modified-Since", ifModifiedSince);
		xmlhttp.send();
		if(xmlhttp.status == 304) {
			xmlhttp = cached;
		}
	}
	
} //end function
	
function toggle(elementID, style, content, popupHolder){
	if (typeof(style) == 'undefined') style = '';
	if (typeof(content) == 'undefined') content = '';

	var obj = document.getElementById(elementID).style;
	obj.display = (obj.display == 'none') ? 'block' : 'none';
	if (obj.display == 'block' && content != ''){
		document.getElementById(popupHolder).innerHTML = popupTemplate(elementID, style, content);
	}
} //end function

function popupTemplate(id, style, content){
	mystyle= 'position:absolute;border:solid black 1px;background-color:#FFDDDD;color:#000000;padding:5px;display:block;text-align:left;';
	output = '<div style="'+mystyle+style+'" id="'+id+'"><div class="popupTitle">'+id+'</div><div class="popupContent">'+content+'</div><div class="popupClose"><a style="cursor:pointer" onClick="toggle(\''+id+'\')" class="popupLink">Cancel</a></div></div>';
	return output;
} //end function

function popup(popupHolder, title, content, style){
	if (typeof(style) == 'undefined') style = '';

	if (typeof(active) != "undefined" && active == title){
		toggle(title, style, content, popupHolder);
	} else {
		document.getElementById(popupHolder).innerHTML = popupTemplate(title, style, content);
	} //end if

	active = title;
	
} //end function