var xmlHttpReq = false;

function ajaxIsAvailable() {
	available = 0;
	if (window.XMLHttpRequest || window.ActiveXObject) available = 1;
	return available;
}

function GetXmlHttpObject(handler) { 
	var objXMLHttp = null;
	
	if (window.XMLHttpRequest) {
		objXMLHttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

function extractAllFormVariablesForPost (formName) {
	var i, s; // index, string
	var f, e; // form, element
	var d = false; // done
	
	var x = 0;
	var dd;

	f = ( typeof( formName ) == 'object' ? formName : document.getElementById(formName) );
	//if (!f || !f.elements) f = document.forms[formName];
	
	s = "";
	
	if (f) {
		//alert(f);
		if (f.elements) {
			for (i = 0; i < f.elements.length; i++) {
				e = f.elements[i];
				
				if (d) s += "&";
				if (e.type == 'checkbox' || e.type == 'radio') {
					if (e.checked) s += e.name + "=" + e.value;
				}
				else if (e.type == 'select-multiple') {
					dd = false;
					for (var j = 0; j < e.options.length; j++) {
						if (dd) s += "&";
						if (e.options[j].selected) s += e.name + "=" + e.options[j].value;
						dd = true;
					}
				}
				else {
					s += e.name + "=" + escape( e.value );
				}
				d = true;
			}
		}
		//else alert("no elements!");
	}
	//else alert("no form!");

	return s;
}

function xaja (variables, doPost, formName, postToUrl, postDiv, customFunction ) {
	xmlHttpReq = false;
	var queryString = "";
	
	//alert ("variables: " + variables + ", doPost: " + doPost + ", formName: " + formName + ", postToUrl: " + postToUrl + ", postDiv: " + postDiv);

	xmlHttpReq = GetXmlHttpObject('');

	var returnFunction;
	if (typeof( customFunction ) == 'function') {
		returnFunction = customFunction;
	}
	else if (typeof( postDiv ) == 'object') {
		returnFunction = function() {
			if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
				if (xmlHttpReq.responseText) {
					window.ajaxPostDivTarget.innerHTML = xmlHttpReq.responseText;
				}
			}
		}
	}
	else {
		returnFunction = function() {
			if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200) {
				if (xmlHttpReq.responseText) {
					document.getElementById(postDiv).innerHTML = xmlHttpReq.responseText;
				}
				else {
					//document.getElementById(postDiv).innerHTML = 'An error occurred processing your request.';	
				}
			}		
		}
	}

	if (xmlHttpReq) {
		xmlHttpReq.open("POST", postToUrl, true);
		xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if( typeof( postDiv ) == 'object' ){
			window.ajaxPostDivTarget = postDiv;
		}
		xmlHttpReq.onreadystatechange = returnFunction;
		if (variables) queryString += variables;
		if (variables && doPost) queryString += "&";
		if (doPost) {
			//alert("do post! (formName: " + formName + ")");
			x = extractAllFormVariablesForPost(formName);
			if (x) queryString += x;
		}
		xmlHttpReq.send(queryString);
	}
	
	return xmlHttpReq;
}

function xajar (xmlhttpreqobj, postDiv) { // "ajax *r*esult" populate a div with ajax result
	if (xmlhttpreqobj.readyState == 4 && xmlhttpreqobj.status == 200) {
		if (xmlhttpreqobj.responseText) {
			document.getElementById(postDiv).innerHTML = xmlHttpReq.responseText;
		}
	}		
}

function xajajs (postDiv) { // execute javascript within an ajax result post
	var scriptindex = ((arguments[1] && arguments[1] != null) ? arguments[1] : null);
	var runall = ((arguments[2] && arguments[2] != null) ? arguments[2] : null);
	var debug = ((arguments[3] && arguments[3] != null) ? arguments[3] : null);

	if (scriptindex == null) scriptindex = 0;

	var o = document.getElementById(postDiv);
	if (o) {
		var s = o.getElementsByTagName('script');
		if (debug) alert(s);
		if (scriptindex < 0) {
			var allscripts = s.length;
			scriptindex = allscripts + scriptindex;
		}
		if (!runall) {
			if (debug) alert('scriptindex? ' + scriptindex + ' # of scripts? ' + s.length);
			if (scriptindex >= 0 && s[scriptindex] != null) {
				if (debug) alert("run this: " + s[scriptindex].text);
				try {
					if (s[scriptindex].text) eval(s[scriptindex].text);
				}
				catch (err) {
				
				}
			}
		}
		if (runall) {
			for (var i = 0; i < s.length; i++) {
				if (i >= 0 && s[i] != null) {
					if (debug) alert("run this: " + s[i].text);
					try {
						if (s[i].text) eval(s[i].text);
					}
					catch (err) {
					
					}
				}
			}
		}
	}
}

function xajawjs (xmlhttpreqobj, postDiv) { //executes javascript in the returned ajax post
	var scriptindex = (arguments[2] ? arguments[2] : null);
	
	xajar(xmlhttpreqobj, postDiv);
	xajajs(postDiv, (scriptindex != null ? scriptindex : 0));
}

function xajad (xmlhttpreqobj) { // "ajax *d*one" checks to verify that the ajax request is complete
	if (xmlhttpreqobj.readyState == 4 && xmlhttpreqobj.status == 200) return true;
	return false;
}


function spinner (divId) {
	if (!divId) return;
	var spinnerType = arguments[1];
	var spin = '', spinnerUrl = '';
	
	var spinnerUrl = '/img/pix/spinner.gif';
	if (spinnerType) switch (spinnerType) {
		case 'small-white': spinnerUrl = '/pix/processing_small-white.gif'; break;
		case 'small-gray': spinnerUrl = '/pix/processing_small-gray.gif'; break;
	}
	
	spin = '<div style="display: inline; width: 100%; height: 100%; vertical-align: middle; margin-top: auto; margin-bottom: auto; text-align: center;"><img src="' + spinnerUrl + '" alt="" border="0" /></div>';
	if (document.getElementById) document.getElementById(divId).innerHTML = spin;
	else if (document.all) document.all[divId].innerHTML = spin;
	else if (document.layers) document.layers[divId].innerHTML = spin;
}

/* not really ajax... */
function showDiv (divId) {
    var Display = arguments[1];
	if (!Display) Display = "";
	if (document.getElementById && document.getElementById(divId) != null) document.getElementById(divId).style.display = Display;
	else if (document.all) document.all[divId].style.visibility = 'visible';
	else if (document.layers) document.layers[divId].visibility = 'show';
}

function hideDiv (divId) {
	if (document.getElementById) {
		if (document.getElementById(divId) != null) document.getElementById(divId).style.display = 'none';
	}
	else if (document.all) document.all[divId].style.visibility = 'hidden';
	else if (document.layers) document.layers[divId].visibility = 'hide';
}

function showDivs (divarray) {
	if (divarray instanceof Array) for (div in divarray) {
		showDiv (divarray[div]);
	}
}

function hideDivs (divarray) {
	if (divarray instanceof Array) for (div in divarray) {
		hideDiv (divarray[div]);
	}
}

function toggleDivs (divarray) {
	if (divarray instanceof Array) for (div in divarray) {
		toggleDiv (divarray[div]);
	}
}

function fillDiv (div, content) {
	d = getElement(div);
	if (d) {
		d.innerHTML = content;
	}
}

var delayHideTimer = setTimeout("var delayHideTimerVar = 1", 1);

function delayHide (divId) {
	var theDelay = arguments[1];
	if (!theDelay) theDelay = 500; //delay 1/2 second by default
	delayHideTimer = setTimeout("hideDiv('" + divId + "')", theDelay);
}

function clearAndHideDiv (divId) {
	hideDiv(divId);
	clearDiv(divId);
}

function clearDiv (divId) {
	if (document.getElementById) document.getElementById(divId).innerHTML = '';
	else if (document.all) document.all[divId].innerHTML = '';
	else if (document.layers) document.layers[divId].innerHTML = '';
}

function toggleDiv (divId) {
    var Display = arguments[1];
	var status;

	if (document.getElementById) status = (document.getElementById(divId).style.display == 'none') ? "h" : "v";
	else if (document.all)  status = (document.all[divId].style.visibility == 'hidden') ? 'h' : 'v';
	else if (document.layers) status = (document.layers[divId].visibility == 'hide') ? 'h' : 'v';

	if (Display) {
		if (status == 'v') hideDiv(divId);
		else if (status == 'h') showDiv(divId, Display);
	}
	else {
		if (status == 'v') hideDiv(divId);
		else if (status == 'h') showDiv(divId);
	}
}

function divIsVisible (divId) {
	var v = 0;
	
	if (document.getElementById) v = (document.getElementById(divId).style.display == 'none') ? 0 : 1;
	else if (document.all)  v = (document.all[divId].style.visibility == 'hidden') ? 0 : 1;
	else if (document.layers) v = (document.layers[divId].visibility == 'hide') ? 0 : 1;

	return v;
}

function executeInnerJavascript ( aDiv ) {
	var scripts = aDiv.getElementsByTagName( "script" );
	for ( var i = 0; i < scripts.length; i++ ) {
		try {
			eval( scripts[i].text );
		}
		catch( e ) {
		}
	}
}

