DefaultCopy = false;
AllowCopy   = DefaultCopy;

function disablePaste(p_event)
{
  document.body.onselectstart = function () { return AllowCopy; };
  document.body.ondrag        = function () { return AllowCopy; };
  document.body.onmousedown   = function () { return AllowCopy; };
  document.body.onfocus       = function () { return AllowCopy; };
}

function CharCount(p_field_name,p_paste)
{
  var f = document.getElementById(p_field_name);
  var c = document.getElementById(p_field_name+'count');

  if (c != null)
  {
    var v = f.value;
    var w = 0;
 
    if (p_paste)
    {
      v += window.clipboardData.getData('Text');
    }
    a = v.replace(/\s/g,' ');
    a = a.split(' ');
    for (i=0; i<a.length; i++)
    {
      if (a[i].length > 0) w++ ;
    }
    c.value = v.length + ' chars ' + w + ' words';
  }
  return(true) ;
}

function setupCharCount()
{
  for (i=0; i<document.forms.length; i++)
  {
    for (j=0; j<document.forms[i].elements.length; j++)
    {
      with (document.forms[i].elements[j])
      {
        if (type == 'textarea')
        {
          oninput   = function() { return CharCount(name,0); } ;
          onkeyup   = function() { return CharCount(name,0); } ;
          onkeydown = function() { return CharCount(name,0); } ;
          onpaste   = function() { return CharCount(name,1); } ;
        }
      }
    }
  }
}

function getHTTPObject()
{
  if (typeof XMLHttpRequest != 'undefined')
  {
    return( new XMLHttpRequest() );
  }

  try
  {
    return( new ActiveXObject("Msxml2.XMLHTTP") );
  } catch (e)
  {
    try
    {
      return( new ActiveXObject("Microsoft.XMLHTTP") );
    } catch (e) {}
  }

  return(false) ;
}

function checkField(p_path,p_field,p_err_field,p_err_color,p_ok_message,p_ok_color)
{
  if (p_field.value == '')
  {
    p_err_field.value = 'Please enter ' + p_field.name.toLowerCase().substr(4).replace('_',' ');
    p_err_field.style.color = p_err_color;
  }
  else
  {
    http = getHTTPObject();
    http.open("GET",p_path+"?"+p_field.name+"="+encodeURIComponent(p_field.value), true);
    http.onreadystatechange = function()
    {
      if (http.readyState == 4)
      {
        if (http.responseText == "OK")
        {
            p_err_field.value = p_ok_message;
            p_err_field.style.color = p_ok_color;
        }
        else
        {
            p_err_field.value = http.responseText;
            p_err_field.style.color = p_err_color;
        }
      }
    }
    http.send(null);
  }
}

function setElement(p_element_name,p_display)
{
  var e = document.getElementById(p_element_name) ;

  if (e != null)
  {
    e.style.display = p_display;
  }
}

image_popup = null;
 
function ShowFullImage(image,name,params,x,y)
{
  ClosePopup();
  image_popup = window.open(image,name,params);
  image_popup.moveTo(x,y);
  image_popup.focus();
}
 
function ClosePopup()
{
  if (image_popup != null)
  {
    image_popup.close();
  }
}

function setLinks(p_host)
{
  now = new Date();
  tz  = 'tzoffset=' + now.getTimezoneOffset();
  w   = '&width=' + screen.width;

  for (i=0; i<document.links.length; i++)
  {
    with (document.links[i])
    {
      if ((href.indexOf('mailto') == -1) && (href.indexOf('#') == -1) &&
          (href.indexOf(p_host) >= 0))
      {
        if (href.indexOf('?') == -1)
        {
          href += '?' + tz + w;
        } else {
          href += '&' + tz + w;
        }
      }
    }
  }

  for (i=0; i<document.forms.length; i++)
  {
    with (document.forms[i])
    {
      if (action.indexOf('?') == -1)
      {
        action += '?' + tz + w;
      } else {
        action += '&' + tz + w;
      }
    }
  }
}

BoxDisplayed = false ;

function getProgress(p_path,p_id)
{
  http = getHTTPObject();
  http.open("GET",p_path+"?progress_key="+p_id,true);
  http.onreadystatechange = function()
  {
    if (http.readyState == 4)
    {
      percent = http.responseText;
      if (!BoxDisplayed && (percent<100))
      {
        ProgressBox.style.display="block";
        BoxDisplayed = true;
      }
      ProgressBar.style.width = percent+"%";
      if (percent<100)
      {
        setTimeout("getProgress('"+p_path+"','"+p_id+"')",1000);  // 1000 = 1 second
      }
    }
  }
  http.send(null);
}

function startProgress(p_path,p_id,p_field)
{
  if (p_field.value != '')
  {
    // Wait a second in case the upload is quick and we don't need to display progress

    setTimeout("getProgress('"+p_path+"','"+p_id+"')",1000);
  }
}
