function CreateXMLHttp()
{
	xmlhttp = false;

	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest)
	{
		try
		{
			xmlhttp = window.createRequest();
		}
		catch (e)
		{
			xmlhttp=false;
		}
	}
	return xmlhttp;
}

function EraseOptions(sFormField)
{
	if (sFormField.options)
	{
		for(i = sFormField.options.length-1; i >= 0; --i)
		{
			sFormField.options[i]=null;
		}
	}
}

function Show(sRow)
{
	var oRow = document.getElementById(sRow);

	try
	{
	  oRow.style.display="table";
	}
	catch(e)
	{
	  oRow.style.display = "block";
	}
}

function Hide(sRow)
{
	var oRow = document.getElementById(sRow);
	oRow.style.display="none";
}

Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
	var i;
	for (i=0; i < this.length; ++i) 
	{
		if (this[i] == value)
		{
			return true;
		}
	}
	return false;
};

function UpdateContent(sContent, sTargetDiv)
{
	var oDiv = document.getElementById(sTargetDiv);
	//oDiv.innerHTML = sContent;
	
	evalAjaxJavascript(sContent, oDiv)
}

function ClearMessageDiv()
{
	oMessage = document.getElementById('message_div');
	if (oMessage)
	{
		oMessage.className = "";
		oMessage.innerHTML = '<br style="clear: both; line-height: 3.6em;" />';
	}
}

function SetTextareas()
{
	document.getElementsByClassName( 'replace-with-zeditor', 'TEXTAREA' ).each
	( 
		function( oValue, iIdx ) 
		{
			aEditors.push
			( 
				new ZEditor
				(
					oValue,
					{
						iWidth: oValue.offsetWidth + 'px',
						iHeight: oValue.offsetHeight + 'px',
						sFileBrowserUrl: ( window.location.toString().match( /zincastle/i ) || window.location.toString().match( /zssweb/i ) ? ZEditorParams.Development.sFileBrowserUrl : ZEditorParams.Live.sFileBrowserUrl ),
						sFileDir: ( window.location.toString().match( /zincastle/i ) || window.location.toString().match( /zssweb/i ) ? ZEditorParams.Development.sFileDir : ZEditorParams.Live.sFileDir ),
						sImageBrowserUrl: ( window.location.toString().match( /zincastle/i ) || window.location.toString().match( /zssweb/i ) ? ZEditorParams.Development.sImageBrowserUrl : ZEditorParams.Live.sImageBrowserUrl ),
						sImageDir: ( window.location.toString().match( /zincastle/i ) || window.location.toString().match( /zssweb/i ) ? ZEditorParams.Development.sImageDir : ZEditorParams.Live.sImageDir ),
						sUploadUrl: ( window.location.toString().match( /zincastle/i ) || window.location.toString().match( /zssweb/i ) ? ZEditorParams.Development.sUploadUrl : ZEditorParams.Live.sUploadUrl )
					}
				)
			);
			Event.observe(
				oValue,
				'keydown',
				function( oEvent )
				{
					if( oEvent.keyCode == Event.KEY_TAB )
					{
						Event.stop( oEvent );
						var oTextArea = this;
						Try.these(
							function()
							{
								document.selection.createRange().text = '\t';
							},
							function()
							{
								new ZSelection( oTextArea ).replaceSelectedText( '\t' );
								oTextArea.selectionStart = oTextArea.selectionEnd;
							},
							function()
							{
								alert( 'Unsupported browser!' );
							}
						);
					}
				}.bindAsEventListener( oValue ),
				false
			);
		} 
	);
}

/**
 * function to extract JS code from a text string.  Useful to call when loading
 * content dynamically through AJAX which contains a mix of HTML and JavaScript.
 * retrieves all of the JS code between <script></script> tags and adds it as a <script> node
 * @param string text   the text that contains a mix of html and inline javascript
 * @param DOMElement parentElement	the element that the text and JavaScript will added to
 */

function evalAjaxJavascript(text, parentElement) {
	parentElement.innerHTML = "";
	/* -- uncomment for debugging
	debugelement = document.createElement("pre");
	debugelement.appendChild(document.createTextNode(text));
	parentElement.appendChild(debugelement);
	*/
	pos2 = -1;
	//-- find the first occurrence of <script>
	pos1 = text.indexOf("<script", pos2+1);
	while(pos1>-1) {
		//-- append the text up to the <script tag to the content of the parent element
		parentElement.innerHTML += text.substring(0, pos1);

		//-- find the close of the <script> tag
		pos2 = text.indexOf(">",pos1+5);
		if (pos2==-1) {
			parentElement.innerHTML += "Error: incomplete text";
			return;
		}
		//-- create a new <script> element to add to the parentElement
		jselement = document.createElement("script");
		jselement.type = "text/javascript";
		//-- look for any src attributes
		scripttag = text.substring(pos1, pos2);
		regex = new RegExp("\\ssrc=\".*\"", "gi");
		results = scripttag.match(regex);
		if (results) {
			for(i=0; i<results.length; i++) {
				src = results[i].substring(results[i].indexOf("\"")+1, results[i].indexOf("\"", 6));
				src = src.replace(/&amp;/gi, "&");
				jselement.src = src;
			}
		}
		opos1 = pos1;
		pos1 = pos2;
		//-- find the closing </script> tag
		pos2 = text.indexOf("</script",pos1+1);
		if (pos2==-1) {
			parentElement.innerHTML += "Error: incomplete text";
			return;
		}
		//-- get the JS code between the <script></script> tags
		if (!results || results.length==0) {
			jscode = text.substring(pos1+1, pos2);
			if (jscode.length>0) {
				ttext = document.createTextNode(jscode);
				//-- add the JS code to the <script> element as a text node
				jscode=jscode.replace(/<!--/g, ''); // remove html comment [ 1737256 ]
				jscode=jscode.replace(/function ([^( ]*)/g,'window.$1 = function');
				eval(jscode);
			}
		}
		//-- add the javascript element to the parent element
		parentElement.appendChild(jselement);
		//-- shrink the text for the next iteration
		text = text.substring(pos2+9, text.length);
		//-- look for the next <script> tag
		pos1 = text.indexOf("<script");
	}
	//-- make sure any HTML/text after the last </script> gets added
	parentElement.innerHTML += text;
}
