/* * * * * * * * * * * * * * * * * * * * * * * * *
 *			 Seir Anphin AJAX Library            *
 *			  Copyright Darrel Grant             *
 *			  http://www.anphin.com              *
 * * * * * * * * * * * * * * * * * * * * * * * * *
 *	 Implements Ajax functions that require      *
 *	 specific support in the server-side app,    *
 *	 therefore not easily portable to other      *
 *	 projects.                                   *
 * * * * * * * * * * * * * * * * * * * * * * * * */

ajax = {
	xhr : false, // XMLHttpRequest object
	targetId : '', // DIV element ID used to insert Ajax content

	getPage : function(url, targetLayerId, linkId, callBackObj) {
		var url = url + '&ajax';
		// Make URL unique to prevent IE caching it
		url += "&smallpage&t=" + new Date().getTime();
		
		this.init();
		this.targetId = targetLayerId;

		if (sa2.oldLinkId != '') {
			sa2.setClassName(sa2.oldLinkId, 'menu_item_off');
		}

		if (linkId != '') {
			sa2.setClassName(linkId, 'menu_item_on');
			sa2.oldLinkId = linkId;
		}

		this.makeRequest('get', url, callBackObj);
	},

	postPage : function(targetLayerId, parameters, callBackObj) {
		this.init();
		this.targetId = targetLayerId;
		parameters +=  "&ajax=1";
		this.makeRequest('post', 'index.php', callBackObj, parameters);
	},
	

	init : function() {
		this.xhr = this.getAjaxObject();
	},

	getAjaxObject : function() {
		try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
		try { return new XMLHttpRequest( ); } catch(e) {}
		alert("XMLHttpRequest not supported - some functions on this site will not work");
		return null;
	},

	makeRequest : function(method, url, callBack, parameters) {
		if (!this.xhr) this.init();

		if (typeof callBack == 'undefined') {
			callBack = this.showLoaderGraphic;
		}

		this.xhr.onreadystatechange = callBack;
		this.xhr.open(method, url, true);
		if (method == 'post') {
			this.xhr.overrideMimeType('text/html');
			this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.xhr.setRequestHeader("Content-length", parameters.length);
			this.xhr.setRequestHeader("Connection", "close");
			this.xhr.send(parameters);
		} else {
			this.xhr.send(url);
		}
	},

	// Callbacks
	printStatus : function() {
		var div = $(ajax.targetId);
		switch(ajax.xhr.readyState) {
			case 0:
				div.innerHTML = 'Sending request';
				break;
			case 1:
				div.innerHTML = 'Loading...';
				break;
			case 2:
				div.innerHTML = 'Response loaded';
				break;
			case 3:
				div.innerHTML = 'Response ready';
				break;
			case 4:
				if (Ajax.xhr.status == 200) {
					div.innerHTML = ajax.xhr.responseText;
				} else {
					div.innerHTML = "Error: could not load AJAX data " + ajax.xhr.status;
				}
				return ajax.xhr.status;
		}
	},
	
	silentLoader : function() {
		var div = $(ajax.targetId);
		if (ajax.xhr.readyState == 4) {
			if (ajax.xhr.status == 200) {
				div.innerHTML = ajax.xhr.responseText;
			} else {
				div.innerHTML = "Error: could not load AJAX data " + ajax.xhr.status;
			}
			return ajax.xhr.status;
		}
	},
	
	// There is no loading graphic made yet...
	showLoaderGraphic : function() {
		var div = $(ajax.targetId);

		if (ajax.xhr.readyState < 4) {
			$('loading').style.visibility = 'visible';
			y = 150 + ajax.getYOffset();
			$('loading').style.top = y + 'px';
		}
		if (ajax.xhr.readyState == 4) {
			if (ajax.xhr.status == 200) {
				$('loading').style.visibility = 'hidden';

				div.innerHTML = ajax.xhr.responseText;
			} else {
				div.innerHTML = "Error: could not load AJAX data " + ajax.xhr.status;
			}
			return ajax.xhr.status;
		}
	},


	buildParams : function(form_id) {
		form = $(form_id);
		var elements = form.elements;
		var pairs = new Array();

		for (var i = 0; i < elements.length; i++) {
			if ((name = elements[i].name) && (value = elements[i].value))
				pairs.push(name + "=" + encodeURIComponent(value));
		}
		return pairs.join("&");
	},

	postNote : function(formId, textId, targetLayerId) {
		if (typeof formId == 'undefined') {
			formId = 'noteboxform';
		}
		if (typeof textId == 'undefined') {
			textId = 'notetext';
		}
		if (typeof targetLayerId == 'undefined') {
			targetLayerId = 'notes';
		}

		parameters = 'm=' + 'blocks_notebox' + '&';
		parameters += 'op=' + 'postnote' + '&';
		parameters += ajax.buildParams(formId);
		ajax.postPage(targetLayerId, parameters);
		$(textId).value = '';
	},

	// This function builds the form tag used when scripting
	// is supported. A "scriptless" form tag will be used between
	// <noscript> tags in a given template.
	writeFormTag : function(formAction, formId) {
		document.write('<form method="post" id="' + formId + '" action="' + formAction + '">');
	},

	// Return a Y offset of windows scroll position for the loading div
	getYOffset : function() {
		// Firefox
		if (window.scrollY) {
			return window.scrollY;

		// Opera
		} else if (window.pageYOffset) {
			return window.pageYOffset;

		// IE
		} else if (document.documentElement.scrollTop) {
			return document.documentElement.scrollTop;
		}
	}
};

