Tools = new Object()

Tools.GetInnerText = function(obj)
{
	///http://geekswithblogs.net/timh/archive/2006/01/19/66383.aspx
	if (document.all) // IE;
		return obj.innerText;
	else if (obj.textContent)
		return obj.textContent;
	else
		return "Error: Unknown or unsupported browser"
}

Tools.SetInnerText = function(obj, newValue)
{
	if (document.all) // IE;
		obj.innerText = newValue;
	else if (obj.textContent)
		obj.textContent = newValue;
}

Tools.OutputMailTo = function(username, servername)
{
	address = username + "@" + servername
	document.write("<a href=\"mailto:" + address + "\">" + address + "</a>")
}

Tools.TextToHtml = function(text)
{
	text = text.replace(/&/g, "&amp;")
	text = text.replace(/</g, "&lt;")
	text = text.replace(/\n/g, "&nbsp;<br/>")
	return text
}

Tools.ShowMask = function(message)
{
    window.onresize = Tools.SetLayerPosition;
    window.onscroll = Tools.SetLayerPosition;
    Tools.SetLayerPosition();

    document.getElementById("masker").style.display = "block"; 
    document.getElementById("progress").style.display = "block"; 
}

Tools.SetProgress = function(message)
{
    Tools.SetInnerText(document.getElementById("ProgressMessage"), message)
}

Tools.HideMask = function()
{
    window.onresize = function(){ }
    window.onscroll = function(){ }

    var masker = document.getElementById("masker");
    var progress = document.getElementById("progress");

    masker.style.display = "none"; 
    progress.style.display = "none";
}

Tools.SetLayerPosition = function() 
{
    ///Masker should mask all the screen and scrolled areas:
    
    var shadow = document.getElementById("masker");

    intH = document.body.scrollHeight;
    intW = document.body.scrollWidth;
        
    shadow.style.width = intW + "px";
    shadow.style.height = intH + "px";

    ///Then center the loading div on the page:
    if(typeof window.innerWidth  == 'number' ) 
    {
       intH = window.innerHeight;
       intW = window.innerWidth;
    } 
    else
    {
        intH = document.body.clientHeight;
        intW = document.body.clientWidth;
    }
    
    var progress = document.getElementById("progress");
    progress.style.left = parseInt((intW - 200) / 2) + document.body.scrollLeft + "px";
    progress.style.top = parseInt((intH - 100) / 2) + document.body.scrollTop;    
}

Tools.GetUrlParam = function(name)
{
  ///http://www.netlobo.com/url_query_string_javascript.html
  
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


strNullFieldMessage = "Please enter a value for #FIELD_NAME#."

function checkAlertNull2(element, strFieldName)
{
	if(element.value == "")
	{
	    message = strNullFieldMessage.replace("#FIELD_NAME#", strFieldName)
		alert(message)
		if(element.type != "hidden") //could happen in the case of node picker, color picker, etc.
			element.focus()
	    
	    AjaxForms.DisplayNotification("ErrorMessage", message)
	    
		return false
	}
	else
		return true
}

function showInputError(message)
{
    alert(message)
    AjaxForms.DisplayNotification("ErrorMessage", message)
}

function strGetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
    {
		if(!aCrumb[1])
		{
			//alert("returning nothing");
			return ""
		}
		else
		{
			//alert("returning: " + unescape(aCrumb[1]))
			return URLDecode(aCrumb[1]);
		}
    }
  }

  // a cookie with the requested name does not exist
  return "";
}

Tools.Logout = function()
{	
	Tools.SetProgress('Logging out...'); 
	AjaxForms.DoAjax('login.php', 'Logout=true')
}


function strGetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
    {
		if(!aCrumb[1])
		{
			//alert("returning nothing");
			return ""
		}
		else
		{
			//alert("returning: " + unescape(aCrumb[1]))
			return URLDecode(aCrumb[1]);
		}
    }
  }

  // a cookie with the requested name does not exist
  return "";
}


function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output.replace(/\+/g, " ");
}


function setCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue) + ";"
}

Tools.DeleteCookie = function( name ) 
    {
        d = new Date()
        document.cookie = name + "=blah; expires=" + d.toGMTString() + "; ; path=/";
    }


	    function closeWindowAndReturn()
    {
		try
		{
    		if(typeof(window.opener) != "undefined")
    			if(!window.opener.closed)
    				window.opener.focus()
		}
		catch(something){ }
		    
    	window.close()
    }
    function closeWindowRefreshAndReturn(strLocation)
    {
    	// if the window.open reference appends # to the URL, strip it off
    	try
    	{
    		if(typeof(strLocation) == "undefined")
	    		strLocation = new String(window.opener.location.href)
	    	
    		if (strLocation.search('#') > -1)
    			strLocation = strLocation.substring(0,strLocation.search('#'))
    		
    		window.opener.location.href = strLocation
    	}
    	catch(something)
    	{
    		///Possible to get "permission denied" exception if user browsed somewhere
    		// else or parent window was closed.
    	}
    	
    	closeWindowAndReturn()
    }

