﻿// 
// miscelaneous utility functions
//


function addLoadEvent(handler)
{
  if (typeof(handler) == 'function')
  {
    if (document.addEventListener)
	  {
      window.addEventListener('load',handler,false);
	  }
	  else // IE
	  {
	    window.attachEvent('onload',handler);
	  }
  }
}

function setStartFocus(id)
{
  var handler = new Function('','$get('+id+').focus()');
  if (document.addEventListener)
  {
    window.addEventListener('load',handler,false);
  }
  else // IE
  {
    window.attachEvent('onload',handler);
  }
}

function showElement(id)
{
  $get(id).style.visibility = 'visible';
}
function hideElement(id)
{
  $get(id).style.visibility = 'hidden';
}
function showHideElement(id,show)
{
  $get(id).style.visibility = show ? 'visible' : 'hidden';
}
function resizeElement(id,width,height)
{
  if (!(width > 0)) width = 0;
  if (!(height > 0)) height = 0;
  with($get(id))
  {
    style.width = String(width)+'px';
    style.height = String(height)+'px';
  }
}
function moveElement(id,left,top)
{
  with($get(id))
  {
    style.left = String(left)+'px';
    style.top = String(top)+'px';
  }
}
function setElement(id,left,top,width,height)
{
  with($get(id))
  {
    style.left = String(left)+'px';
    style.top = String(top)+'px';
    style.width = String(width)+'px';
    style.height = String(height)+'px';
  }
}

function disablePage(state)
{
  $get('progressLayer').style.visibility = state ? 'visible' : 'hidden';
}

function stringEmpty(s)
{
  if (s != null && typeof s != 'undefined' && s != '')
  {
    str = new String(s);
    return str.length == 0 || str.match(/^\s+$/);
  }
  return true;
}
function looksLikeEmail(s)
{
  if (!stringEmpty(s))
  {
    str = new String(s);
    return str.match(/[\w\.\-]+@[\w\.\-]+\.[a-z]{2,}/i);
  }
  return false;
}

// plain text <==> html

function html2plain(s)
{
  s = s.replace(/&nbsp;/g, ' ');
  s = s.replace(/<p\s*[^>]*>/ig, "\n");
  s = s.replace(/<br\s*\/?>/ig, "\n");
  s = s.replace(/<[^>]+>/g, '');
  return s;
}

function plain2html(s)
{
  s = s.replace(/\n/g, '<br />');
  return s;
}

