/*
	All the files here are used for ajax control and initialisation
*/

// used to hold xml arrays
var XMLarray;
var SEARCHFOCUS = 0;

// instantiate the ajax class
function newAjax() {
	var Ajax;	// used to hold the instance for this class
	// test to see if this is FF family and instantiate the xml http request class
	try {
		// Firefox, Opera 8.0+, Safari
		Ajax = new XMLHttpRequest();
	}
	// catch an error and react
	catch (e) {
		// test to see if this is IE family and instantiate the xml http request class
		try {
			Ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			// test to see if this is old IE family and instantiate the xml http request class
			try {
				Ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}
			// alert to the user this is not a ajax viable option
			catch (e) {
				alert("AJAX Sams4 application could not be instantiated, ERROR #001");
				return false;
			}
		}
	}
	return Ajax;	// return this instance
}

// instantiate a generic ajax class
var Ajax = newAjax();

function url_encode(text) {
		text = text.replace(/\&/g, "%26");
		text = text.replace(/\+/g, "%2B");
		text = text.replace(/#/g, "%23");
		
		return text;
}

// send the message to irc
function populate_element(id, url) {
	var element = document.getElementById(id);
	
	if (element) {	// make sure the elements exist
		// send this to irc pm
		Ajax.open("GET", url_encode(url), false);
		// send null as a touch
		Ajax.send(null);

		// apply to chat window
		element.innerHTML = Ajax.responseText;
	}
}

function gig_suggest(keyword, extraDIR) {
	var element = document.getElementById("locationsuggestions");
	
	if (element && keyword>'') {	// make sure the elements exist
		// send this to irc pm
		Ajax.open("GET", url_encode(extraDIR+"modules/gig-suggest.php?keyword="+keyword), false);
		// send null as a touch
		Ajax.send(null);

		// apply to chat window
		element.innerHTML = Ajax.responseText;
		if (Ajax.responseText>'') {
			element.style.display = 'block';
		} else {
			element.style.display = 'none';
		}
	}
}

function relocate_suggestions() {
	domainsX = domainsY = 0;
	var element = document.getElementById("locationsuggestions");
	var searchfield = document.getElementById("searchlocation");
	if (element && searchfield) {
		// get the x and y abs position of the domains button
		// clone the reference to the domain button
		var current_element = searchfield;
		// iterate through all valid parents of this button that have a positional affect
		while(current_element) {
			// keep tally of the x and y values cumulatively
			domainsX += current_element.offsetLeft;
			domainsY += current_element.offsetTop;

			// move the internal pointer to the direct parent of the current element
			current_element = current_element.offsetParent;
		}

		// move the invisible menu to this location using style and abs poitioning
		if (domainsX>0) {
			element.style.left = (domainsX)+"px";
			element.style.top = (domainsY + 18)+"px";
		}
	}
}

function refresh_calendar(month, direction) {
	var calendarcontent = document.getElementById("calendarcontent");
	
	if (calendarcontent) {	// is it on the calendar page
		// send this to irc pm
		if (month>'' && direction>'') {
			Ajax.open("GET", "/modules/generate_calendar.php?month="+month+"&direction="+direction, false);
		} else {
			Ajax.open("GET", "/modules/generate_calendar.php", false);
		}
		// send null as a touch
		Ajax.send(null);

		// apply to chat window
		calendarcontent.innerHTML = Ajax.responseText;
	}
}