/**
 * @author Andrew Welch, Aurora Public Library
 */

// **********************************************
// loadSnip()
// Uses AJAX to load the contents of another page (snipSrc) into a div
// on the current page (divID). To load only a portion of the page,
// srcWrap must be specified and the portion must be bracketed by HTML
// comment tags in a specific form. E.g., if srcWrap = 'storytimes',
// the tags would be <!-- Start storytimes --> and <!-- End storytimes -->.
// To load the entire page (snipSrc), use "" for srcWrap.
function loadSnip(snipSrc, srcWrap, divID) {
var ajaxOK = createAjaxObj();
	if (!ajaxOK) {}
	else if (ajaxOK) {
		var obj = document.getElementById(divID);
		ajaxOK.open("GET", snipSrc);
		
		ajaxOK.onreadystatechange = function() {
			if (ajaxOK.readyState == 4 &&
			ajaxOK.status == 200) {
				var doc = ajaxOK.responseText;
				if (srcWrap != "") {
					var snipStart = doc.indexOf("<!-- Start " + srcWrap);
					var snipEnd = doc.indexOf("<!-- End " + srcWrap);
					var snipDiv = doc.slice(snipStart,snipEnd);
					obj.innerHTML = snipDiv;
				}
				else { obj.innerHTML = doc; }
			}
		}
		ajaxOK.send(null);	
	}
}

function createAjaxObj(){
var httprequest = false;
	if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		httprequest = new XMLHttpRequest();
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml');
	}
	else if (window.ActiveXObject){ // if IE
		try {
			httprequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try {
				httprequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
			}
		}
	}
return httprequest;
}
