/*
Modifications:
15 May 2008		Added function doEditFromContent(id). It's mostly a copy of doEdit() from listPage.js with
Mark			a slight modification, and gets called from content.asp to edit the page the logged in user
				is looking at without having to go to listpage.asp.

*/
function preProcess()
	{
	var img = document.getElementsByTagName("img");
	var i = img.length;
	while ( i-- )
		if ( img[i].getAttribute("sja_isObject") == "true" )
		{
			//alert("found SO in preProcess in content.js");
			cmsObject(img[i], img[i].getAttribute('sja_name'), img[i].getAttribute('sja_url'));
			
		}
		else
		{
			if ( img[i].className.indexOf("mceItemEmbed") != -1 )
				img[i].outerHTML = "<IFRAME SRC='" + img[i].alt + "#toolbar=" + img[i].getAttribute("pdftoolbar") + "' width='" + img[i].width + "' height='" + img[i].height + "'></IFRAME>"
			else
				if ( img[i].className.indexOf("mceItemFlash") != -1 )
						img[i].outerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
						+ ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"'
						+ ' width="' + img[i].width + '" height="' + img[i].height + '">'
						+ '<param name="movie" value="' + img[i].title + '" />'
						+ '<embed src="' + img[i].title + ' pluginspage="http:/www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + img[i].width + '" height="' + img[i].height + '"></embed></object>';
		}
	// Use FORM POST to pass relevant details to CMS pages
	
	var a = document.getElementsByTagName("a");
	i = a.length;
	var l = document.location.href.indexOf("?");
	var str = document.location.href.substr(0,l);
	while ( i-- )
		if ( a[i].href.substr(0,1) != "#" && deHash(a[i].href) != deHash(document.location.href) && a[i].href.substr(0,l) == str )
			{
//			a[i].setAttribute("sja_url", a[i].href);
//			a[i].href = "#";
			a[i].onclick = gotoPage;
			}
	}

//referenceToElement.onclick = function () { alert('here'); };
//document.body.onLoad = function () {alert('here'); };
//document.body.onload = function () {preProcess(); };
//document.body.onload = preProcess;
//document.body.attachevent("onload", preProcess );

function addEvent(obj, evType, fn)
	{ 
	if (obj.addEventListener)
		{ 
		obj.addEventListener(evType, fn, false); 
		return true; 
		}
	else if (obj.attachEvent)
		{ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
		}
	else
		{ 
		return false; 
		} 
	}
addEvent(window, 'load', preProcess);

function deHash(url)
	{
	var x = url.indexOf("#");
	if ( x >= 0 )
		return url.substr(0,x)
	else
		return url;
	}

function gotoPage(e)
	{
	if ( typeof e == "undefined" )
		e = event;
	var obj = e.srcElement;
	
	//alert(obj.tagName);
	// Sometimes the tag that gets triggered is the tag within the <a> eg. a font tag .. so this works it's way through the parent tags till
	// It gets to the <A>
	while (obj.tagName != "A")
		obj = obj.parentNode;
	//alert(obj.tagName);

	var frm = document.forms[0];

	if ( frm.target.length > 0 )
		frm.target = obj.target
	else
		frm.target = "_self";

//alert(obj.getAttribute("sja_url"));
//	frm.action = obj.getAttribute("sja_url");
	frm.action = obj.href;
	
	frm.submit();
	return false;
	}

var soid = 0;
function cmsObject(obj, nam, url)
	{
	/*	try{
    bustMe()
  } 
  catch(e){ 
    expose(e,document.getElementById("errObj"))
  }
  return "Returned Successfully?"
  */
  
		// UPDATE  24 SEP 2007   MARK: Added the [ + "&";] at the end of this line
		url = url.replace(/&amp;/,"&") + "&";
		
		
		//This code adds the parameters found from the previous form submitted to content.asp (for special objects)
		//Note: special objects will have to retrieve parameters by (request("[obj name]")) instead of (request.form("[obj name]"))
		try
		{
			url = url + prevFormParameters.submit_params.value;
			
		}
		catch(e)
		{
			//alert("Error in content.js: [prevFormParameters.submit_params.value] has no value!");
		}
		//alert(url);

// PASS THROUGH PARAMETERS TO THE NEXT PAGE ?
// Create a new hidden input in document.forms[0] ...
	var frm = document.forms[0];
	var parms = url.replace(/\?/,"").split("&");
	var pair, el;
	
	var l = parms.length;
	
	var recombine = false;
//alert(url);

	while ( l-- )
		{
		pair = parms[l].split("=");
		
// if form variable exists and this value is empty the use the form value
//alert("params[" + l + "] = " + parms[l]);
//alert("pair.length = " + pair.length);
//alert("eval(typeof frm.cms_" + pair[0] + ") = " + eval("typeof frm.cms_" + pair[0]));
		if ( ( pair.length == 1 || ( pair.length == 2 && pair[1].length == 0 ) ) && eval("typeof frm.cms_" + pair[0]) != "undefined" )
			{
			recombine = true;
			parms[l] = pair[0] + "=" + eval("frm.cms_" + pair[0]).value;
//alert("new params[" + l + "] = " + parms[l]);
			}

// if the value is not blank and doesn't already exists in the form, then add it
		if ( pair.length == 2 && pair[1].length > 0 && eval("typeof frm.cms_" + pair[0]) == "undefined" )
			{
			el = document.createElement('hidden');
			el.setAttribute('name','`cms_' + pair[0] + '`>');
			//el = document.createElement("<input type='hidden' name=`cms_" + pair[0] + "`>");
			el.value = pair[1];
			frm.appendChild(el);
			}

		}
// Add defaulted values into url
//alert("recombine = " + recombine);
	if ( recombine )
		{
		var str = "";
		l = parms.length;
		while ( l-- )
			str += "&" + parms[l];
		url = url.substr(0,1) + str.substr(1);
		}

	var xmlRequest;
	
	if (window.XMLHttpRequest)
	{
		xmlRequest = new XMLHttpRequest
	
	}
	else
	{	
		xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	
	/*xmlRequest.open("POST", nam + url, true);*/  /* POST doesn't work with IIS on Win2003 .. */
	xmlRequest.open("GET", nam + url, true);	/* GET is limited to a querystring of 1000 characters */
	/*xmlRequest.open("GET", "http://www.ambulance.net.au/specObj/pubList.asp?", true);*/
	
	/*xmlRequest.setRequestHeader('Length', '1000');*/
	
	xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlRequest.setRequestHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.5.0.7");

	xmlRequest.onreadystatechange = function (e)
				{
				if ( xmlRequest.readyState == 4 )
					{
					
					//alert("cmsObject js function in content.asp!  " + "POST", nam + url);

// Use insertAdjacentHTML for IE
					if ( typeof obj.insertAdjacentHTML != "undefined" )
						{
						obj.insertAdjacentHTML("beforeBegin", "<SPAN ID=_so" + (++soid) + ">" + xmlRequest.responseText + "</SPAN>");
// Even though the script is already on the page we must add it again !
						newObj = obj.previousSibling;
						s = newObj.getElementsByTagName("SCRIPT");
						l = s.length;
						while ( l-- )
							{
							x = document.createElement("SCRIPT");
							x.language = s[l].language;
							x.src = s[l].src;
							//newObj.insertBefore(x);
							newObj.replaceChild(x, s[l]);
							}
						obj.removeNode(true);
						}
// Use insertAdjacentHTML for IE
					else
						{
						var df;   // : DocumentFragment
						var r = document.createRange();
							   
						r.setStartBefore(obj);
						df = r.createContextualFragment(xmlRequest.responseText);
							
						obj.parentNode.replaceChild(df, obj);
						}
/*
					var newWin = window.open("about:blank","_blank","location=0,toolbar=0,scrollbars=1,resizable=1,menubar=0");
					var newDoc = newWin.document;
					newDoc.write(xmlRequest.responseText);
					newDoc.close();
*/
					window.status = "Done";
					}
				else
					window.status = "Page Loading . . .";
				return true;
				};

	//IE
	//alert("SEFR");
	//xmlRequest.onReadyStateChange = function () {tryIt(xmlRequest, nam, url, obj);};
	
	//IE with alert only
	//xmlRequest.onReadyStateChange = function () {alert('aaa');};
	
	//FIREFOX
	//xmlRequest.addEventListener("onReadyStateChange", tryIt(xmlRequest, nam, url, obj));
	
	//FIREFOX with alert only
	//xmlRequest.attachEvent("onReadyStateChange", alert);
	

	xmlRequest.send(null);
	
	//alert("cmsObject js function in content.asp!");
	}


function tryIt(xmlRequest, nam, url, obj)
{
alert("Try It" + xmlRequest.readyState);
	if ( xmlRequest.readyState == 4 )
		{
		
		if ( obj.outerHTML )
			{
			obj.outerHTML = xmlRequest.responseText;
			}
		else
			{
			var df;   // : DocumentFragment
			var r = document.createRange();
			   
			r.setStartBefore(obj);
			df = r.createContextualFragment(xmlRequest.responseText);
			
			obj.parentNode.replaceChild(df, obj);
			}

		window.status = "Done";
		}
	else
		window.status = "Page Loading . . .";
		
	return true;
}

var frm = document.forms[0];
function doEditFromContent(id)
{
	//frm.t_id.selectedIndex = 0;
	frm.id.value = id;
	frm.mode.value = "edit";
	frm.action = "content.asp?id=" + escape(id);
	frm.target = "page" + id;
	frm.submit();
	try { eval("page" + id + ".focus()"); } catch ( e ) { }
	frm.action = "listPage.asp";
	frm.target = "_self";
}