/*
This file contains javascript functions for use with communitas's Website
*/


function communitas_page_setup () {

// This function should be called from the body element of an html page as <body onload="communitas_page_setuo();">

// This function performs two tasks. First it preloads a number of important images

	preload_rollover_images();

// Second, this function searches the html document for <div> elements whose id begins "tmpl_...". When it finds such an element, it will search the templates subdirectory for a template whose name matches the remainder of the div's id, and fills the div with the code in that template.

/* So, for example, if you have a template called banner.html in the templates subdirectory, all you need to do to include that as a template is to put a div element in your file with an id of "tmpl_banner" i.e. in your html document you would put

...<div id="tmpl_banner"><div>...

*/

// First get all the divs in the document
var divs = document.getElementsByTagName('div');

// Now loop through the divs to find the ones whose name begins "tmpl_"

for (var i=0; i<divs.length; i++) {
	
	
	
	if (divs[i].id.substring(0,5) == 'tmpl_') {
	
	// OK, so we've found a div whose name begins "tmpl_..."
	
	// First we're going to extract template name i.e. all the stuff that comes after "tmpl_" in the id
	
	var tmpl_identifier = divs[i].id.substring(5,divs[i].id.length);
	
	// Then we send the extracted name to the communitas_include function
	communitas_include(tmpl_identifier);
	}
}

	// finally we show the navigation instructions, if any
	communitas_show_instructions();

}


function communitas_include (identifier) {

	
  	var template_prefix = templates_dir_prefix();

	var template_suffix = ".html";
	var url_prefix = "";

    if (document.getElementById('tmpl_' + identifier)) {
	
	client_side_include('tmpl_' + identifier, template_prefix + identifier + template_suffix);	
	}

}



function preload_rollover_images() {
	
	preload_image_object = new Image();
	
	var mypics = new Array();
	mypics[0] = "b1_f2.gif";
mypics[1] = "b1.gif";
mypics[2] = "b2_f2.gif";
mypics[3] = "b2.gif";
mypics[4] = "b3_f2.gif";
mypics[5] = "b3.gif";
mypics[6] = "b4_f2.gif";
mypics[7] = "b4.gif";
mypics[8] = "b5_f2.gif";
mypics[9] = "b5.gif";
mypics[10] = "b6_f2.gif";
mypics[11] = "b6.gif";
mypics[12] = "b7_f2.gif";
mypics[13] = "b7.gif";
mypics[14] = "b8_f2.gif";
mypics[15] = "b8.gif";
mypics[16] = "b9_f2.gif";
mypics[17] = "b9.gif";
mypics[18] = "banner.gif";
mypics[19] = "communitasdiamond.gif";
mypics[20] = "communitasdiamond.jpg";
mypics[21] = "email_f2.gif";
mypics[22] = "email.gif";
mypics[23] = "frontpage.gif";
mypics[24] = "topmenu1.gif";
mypics[25] = "topmenu2.gif";
mypics[26] = "topmenu3.gif";
mypics[27] = "topmenu4.gif";
mypics[28] = "topmenu5.gif";
mypics[29] = "topmenu6.gif";
mypics[30] = "topmenu7.gif";
mypics[31] = "topmenu8.gif";
mypics[32] = "topmenu9.gif";
	
	for(i=0; i<33;i++){
		preload_image_object.src = "http://www.integralfacilitation.org.uk/images/" + mypics[i];
	}
}



function client_side_include(id, url) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id +
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}



function templates_dir_prefix () {
	
	var req = false;
	if (window.XMLHttpRequest) {
	    try {
	      req = new XMLHttpRequest();
	    } catch (e) {
	      req = false;
	    }
	  } else if (window.ActiveXObject) {
	    // For Internet Explorer on Windows
	    try {
	      req = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	      try {
	        req = new ActiveXObject("Microsoft.XMLHTTP");
	      } catch (e) {
	        req = false;
	      }
	    }
	  }

	var base_templates_url = "templates/";
		
	if (url_exists(req, base_templates_url)) {
		return base_templates_url;
	} 
	else {return "../templates/";}
}


function url_exists (req, url) {
	
	try {
			req.open("HEAD", url, false);
			req.send(null);		
			return req.status== 200 ? true : false;
		}
		catch (er) {
			return false;
		}
}

