// FIX THIS FUNCTION TO REPLACE ALL AMPERSANDS!

function toggleHTML(elementName,on)
{
	var fstr = ELN(elementName).value;
	
	if (on)
	{
		while (fstr.indexOf("\n<br />") > -1)
		{ fstr = fstr.replace("\n<br />","<br />"); }
		while (fstr.indexOf("\n") > -1)
		{ fstr = fstr.replace("\n","<br />"); }
		
		while (fstr.indexOf("<br /><br />") > -1)
		{ fstr = fstr.replace("<br /><br />","<br />"); }		
		/*
		while (fstr.indexOf("\n<br />") > -1)
		{ fstr = fstr.replace("\n<br />","<br />"); }
		*/
		
		while (fstr.indexOf("  ") > -1)
		{ fstr = fstr.replace("  ","&nbsp;&nbsp;"); }
		while (fstr.indexOf("\t") > -1)
		{ fstr = fstr.replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;"); }		
	}
	else
	{
		if (fstr.indexOf("<") > -1 || fstr.indexOf(">") > -1)
		{
			if (!confirm("WARNING: If you disable HTML you will not be able to use any images/links you may have in place. Are you sure?"))
			{ document.getElementById('chkHTML').checked = true; return; toggleHTML(elementName,true) }
		} 
		
		while (fstr.indexOf("\n<br />") > -1)
		{ fstr = fstr.replace("\n<br />","\n"); }
		while (fstr.indexOf("<br />") > -1)
		{ fstr = fstr.replace("<br />","\n"); }
		while (fstr.indexOf("<br/>") > -1)
		{ fstr = fstr.replace("<br/>","\n"); }
		while (fstr.indexOf("<br>") > -1)
		{ fstr = fstr.replace("<br>","\n"); }
		while (fstr.indexOf("&nbsp;") > -1)
		{ fstr = fstr.replace("&nbsp;"," "); }
		
	}
	ELN(elementName).value = fstr;
}

function PlaceElement(obj,obj2) 
{
    var coors = findPos(obj2);
    obj.style.top = (coors[1]) + 'px';
    obj.style.left = (coors[0])-190 + 'px';
}

function findPos(obj) 
{
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
	        curleft += obj.offsetLeft;
	        curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft,curtop];
}

function AddHTMLLink(elm,mode)
{
	var fval; var text = prompt("Link Text:", "Home")
	if (mode)
	{
		if (text)
		{
			fval = '<br /><a href="index.php?pgid=' + document.getElementById('selPage').value + '">' + text + '</a>';
			document.getElementById(elm).value += fval;	
			document.getElementById('chkHTML').checked = true;
			
		}
	}
	else
	{
		if (text)
		{
			var url = prompt("Page: ", "http://")
			fval = '<a href="' + url + '">' + text + '</a>';
			document.getElementById(elm).value += fval;	
			document.getElementById('chkHTML').checked = true;
		}
	}
}

function ELVC(elementName)
{
	var fstr = ELN(elementName).value;
	while (fstr.indexOf('&') > -1)
	{ fstr = fstr.replace('&','%AMPERSAND%'); }
	return fstr;
}

function ELN(elementName)
{ 
	try
	{
		var element = document.getElementById(elementName);
	}
	catch (err)
	{
		alert("Element '"+elementName+"' does not exist!\n\n"+err.description); 
	}
	return element;
}

function FadeIn(x,objID,maxlen)
{
	if (document.getElementById(objID))
	{
		if (document.getElementById(objID).style.height == maxlen+'px')
		{ 
				document.getElementById(objID).style.visibility="visible"; 
				FadeOut(maxlen,objID,maxlen); 
		}
		else
		{
			if(x< maxlen) 
			{ 
				x++;
				document.getElementById(objID).style.opacity=x/maxlen;
				document.getElementById(objID).style.filter="alpha(opacity="+((x/maxlen)*100)+")";
				document.getElementById(objID).style.visibility="visible";
				if(x < maxlen) 
				{ setTimeout('FadeIn('+x+",'"+objID+"',"+maxlen+')',7); }
			}
		}
	}
}
function FadeOut(x,objID,maxlen)
{
	if(x > 1) 
	{ 
		x--; document.getElementById(objID).style.opacity=x/maxlen;
		document.getElementById(objID).style.filter="alpha(opacity="+((x/maxlen)*100)+")";
		setTimeout('FadeOut('+x+",'"+objID+"',"+maxlen+')',7); 
	} else { document.getElementById(objID).style.visibility="hidden"; }
}

function ReturnHeight(objID)
{
	return Number(document.getElementById(objID).style.height.slice(0,document.getElementById(objID).style.height.length-2));
}

function ExtendInfoBox(x,objID,maxlen,opt)
{
	if (!opt) { opt = 0; }
	if (document.getElementById(objID).style.height == maxlen+'px' || ReturnHeight(objID) >= maxlen)
	{ RetractInfoBox(maxlen,objID,maxlen,opt); }
	else
	{
		if(x< maxlen) 
		{ 
			x++; x = x + opt;
			document.getElementById(objID).style.height = x;
			document.getElementById(objID).style.opacity=x/maxlen;
			document.getElementById(objID).style.filter="alpha(opacity="+((x/maxlen)*100)+")";
			document.getElementById(objID).style.visibility="visible";
			if(x < maxlen) 
			{ setTimeout('ExtendInfoBox('+x+",'"+objID+"',"+maxlen+","+opt+')',7); }
		} else { document.getElementById(objID).style.overflow="scroll"; }
	}
}

function RetractInfoBox(x,objID,maxlen,opt)
{
	if (!opt) { opt = 0; }
	if(x > 1) 
	{ 
		x--; if ((x-opt) > -1) { x = x - opt; }
		document.getElementById(objID).style.height = x;
		document.getElementById(objID).style.opacity=x/maxlen;
		document.getElementById(objID).style.filter="alpha(opacity="+((x/maxlen)*100)+")";
		setTimeout('RetractInfoBox('+x+",'"+objID+"',"+maxlen+","+opt+')',7); 
	} else { document.getElementById(objID).style.visibility="hidden"; FadeIn(0,'nameBox',60) }
}

function DispLoadBox(objid,xv)
{
	document.getElementById(objid).style.visibility="visible";
	document.getElementById('ldbx').style.opacity=xv;
	document.getElementById('ldbx').style.filter="alpha(opacity="+(xv*100)+")";
	var t=setTimeOut('DispLoadBox('+objid+','+xv+')',10);
}

function ReplaceQuotes(str)
{
	while(str.match('%dquot%'))
	{ str = str.replace('%dquot%','"'); }
	while(str.match('%squot%'))
	{ str = str.replace('%squot%',"'"); }
	return str;
}

function DispLoadBox(msg)
{
	if (document.getElementById('ldbx') != null)
	{
		if (!msg) { msg = 'Loading...'; }
		document.getElementById('ldbx').innerHTML = msg;
		document.getElementById('ldbx').style.visibility="visible";
	}
	else
	{
		var boxprop = '<div id="ldbx" style="visibility: visible; position: absolute; z-index: 100;'
		boxprop += ' top: 40%; left: 35%; width: 150; height: 60; border: solid; border-width: 1px;'
		boxprop += ' background-color: white; filter:alpha(opacity=85);-moz-opacity:.85; opacity: .85;'
		boxprop += ' padding-top: 8px; text-align: center;" >'
		if (msg) { boxprop += msg; } else { boxprop += 'Loading...'; }
		boxprop += '</div>';
		document.body.innerHTML += boxprop; 
	}
}

function ajaxFunction(data2pass,elementid, mode, lscript, usePOST, debug, AJAXURLU,LoadMsg)
	{
	
	if (LoadMsg)
	{
		if (LoadMsg.toLowerCase().match('%loadbox%') != null)
		{
		 	DispLoadBox(LoadMsg.replace('%loadbox%',''));
		}
		else if (LoadMsg.toLowerCase().match('%fade:%') != null)
		{
			FadeItem(LoadMsg.replace('%fade:%',''),1);
		}
		else
		{
			switch (mode.toLowerCase())
			{
				case "value":
				{ document.getElementById(elementid).value=LoadMsg; break; }
				case "text":
				{ document.getElementById(elementid).text=LoadMsg; break; }
				default:
				{ document.getElementById(elementid).innerHTML=LoadMsg; }
			}
		}
	}
	
	var xmlHttp;
	try
	{ xmlHttp=new XMLHttpRequest(); 
	  xmlHttp.overrideMimeType('text/html');
	}
	catch (e)
	{
		try
      	{ xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
   		catch (e)
      	{
      		try
        	{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
      		catch (e)
      		{
        		alert("Your browser does not support AJAX!");
        		return false;
      		}
      	}
    }

	xmlHttp.onreadystatechange=function()
    {
    	if (xmlHttp.readyState==4)
        {
			if (!mode) { mode = ""; }
			if (debug) { alert(xmlHttp.responseText); }
			switch (mode.toLowerCase())
			{
				case "value":
				{
					document.getElementById(elementid).value=xmlHttp.responseText;
					break;
				}
				case "src":
				{
					document.getElementById(elementid).src=xmlHttp.responseText;
					break;
				}
				case "href":
				{
					document.getElementById(elementid).href=xmlHttp.responseText;
					break;
				}
				case "text":
				{
					document.getElementById(elementid).text=xmlHttp.responseText;
					break;
				}
				default:
				{
					document.getElementById(elementid).innerHTML=xmlHttp.responseText; 
				}
				if (document.getElementById('ldbx') != null)
				{ document.getElementById('ldbx').style.visibility="hidden"; }
				
				if (lscript) 
				{ 
					lscript = ReplaceQuotes(lscript);
					setTimeout(lscript,1); 
				}
			}
		}
    }
	var AJAXURL = 'AJAXresponse.php?mode=';
	if (AJAXURLU) { AJAXURL = AJAXURLU; }
	
	if (usePOST)
	{ 
		if (debug) { alert("Calling: POST "+AJAXURL+usePOST+"\n\nData:\nContent-type: application/x-www-form-urlencoded\nContent-length: "+data2pass.length+"\n"+data2pass); }
		var constring = AJAXURL;
		if (usePOST != 1) { constring = constring + usePOST; }
		xmlHttp.open("POST",constring,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 		xmlHttp.setRequestHeader("Content-length", data2pass.length);
  		try 
		{ 
			xmlHttp.send(data2pass);
			xmlHttp.send(null); 
		}
      	catch (e) { var a = 1; }
		if (debug) { alert("Calling: "+AJAXURL+usePOST+"\n\nPOST Request:\n\nContent-type: application/x-www-form-urlencoded\nContent-length: "+data2pass.length+"\nConnection: close\n"+data2pass); }
	}
	else
	{
		if (debug) { alert("Calling: GET "+AJAXURL+data2pass); }
		xmlHttp.open("GET",AJAXURL+data2pass,true);
		try { xmlHttp.send(null); }
      	catch (e) { var a = 1; }
	}  
}

