

//
function is_def (obj)
{
  return (typeof (obj) != "undefined");
}

//
function get_obj (id)
{ 
  return document.getElementById (id);
}

//
function set_class (obj, class_name)
{
  obj.className = class_name;
}

//
function str_list (str, add, separator)
{
  return str + ((str != "") ? (separator) : ("")) + add;
}

//
function trace_x (id, msg)
{
  get_obj (id).innerHTML += msg + "<br/>";
}

//
function find_pos (obj)
{
  var curleft = 0;
  var curtop = 0;

  if (obj.offsetParent)
    do
    {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;

      obj = obj.offsetParent
    } 
    while (obj);

  return [curleft, curtop];
}

//
function embed_flash (id, file_name, width, height, background, wmode, vars)
{
  document.write ('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="' + id + '" align="middle" />');
  document.write ('<param name="allowScriptAccess" value="sameDomain" />');
  document.write ('<param name="movie" value="' + file_name + '" />');
  document.write ('<param name="FlashVars" value="' + vars + '" />');
  document.write ('<param name="quality" value="high" />');
  document.write ('<param name="bgcolor" value="' + background + '" />');
  document.write ('<param name="menu" value="false" />');
  document.write ('<param name="wmode" value="' + wmode + '" />');
  document.write ('<embed src="' + file_name + '" FlashVars="' + vars + '" width="' + width + '" height="' + height + '" align="middle" quality="high" bgcolor="' + background + '" menu="false" wmode="' + wmode + '" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
  document.write ('</embed>');
  document.write ('</object>');
}

//
function bookmark_this_site (title, url)
{
  if (window.sidebar) // firefox
    window.sidebar.addPanel (title, url, "");
  else 
    if(window.opera && window.print) // opera
    {
      var elem = document.createElement ("a");
      elem.setAttribute ("href", url);
      elem.setAttribute ("title", title);
      elem.setAttribute ("rel", "sidebar");
      elem.click();
    }
    else
      if(document.all)// ie
        window.external.AddFavorite (url, title);
}

//
function set_as_home_page_IE (obj, url)
{
  obj.style.behavior = "url(#default#homepage)";
  obj.setHomePage (url);
}


var MESSAGE_ERROR = 1;
var MESSAGE_FEEDBACK = 2;
var MESSAGE_ALL = 100;

//
function message_box_init (mbox_id, on_class, off_class)
{
  var box = get_obj (mbox_id);

  box.classes = new Object ();
  box.classes.on = on_class;
  box.classes.off = off_class;

  box.messages = new Array ();
}
function message_line (type, msg, alert_group, ids, text_class, over_class, ids_class, tag)
{
  var n, i;

  this.type = type;
  this.msg = msg;
  this.alert_group = alert_group;
  this.ids = ids;
  this.classes = new Object ();
  this.classes.text = text_class;
  this.classes.over = over_class;
  this.classes.ids = ids_class;
  this.tag = tag;
}
function remove_messages (mbox_id, type)
{
  if (mbox_id == "") return ;
  var box = get_obj (mbox_id);

//  for (i in box.messages)
  for (i = 0; i < box.messages.length; i++)
  {
    var line = box.messages[i];
    if (type != MESSAGE_ALL && line.type != type) continue;

//    for (id in line.ids)
    for (id = 0; id < line.ids.length; id++)
    {
      var elem = get_obj (line.ids[id]);
      if (is_def (elem.old_className))
        elem.className = elem.old_className;
    }
  }

  box.messages.length = 0;
}
function add_to_messages (mbox_id, type, msg, alert_group, ids, text_class, over_class, ids_class, tag)
{
  if (mbox_id == "") return ;

  var box = get_obj (mbox_id);

  box.messages.push (new message_line (type, msg, alert_group, ids, text_class, over_class, ids_class, tag));
}
function clear_messages (mbox_id, type)
{
  if (mbox_id == "") return ;
  var box = get_obj (mbox_id);

  remove_messages (mbox_id, type);

  box.innerHTML = "";
  box.className = box.classes.off;
}
function display_messages (mbox_id, type)
{
  if (mbox_id == "") return ;

  var box = get_obj (mbox_id);
  var alerts = new Array ();
  var txt = "";
  var i;

//  for (i in box.messages)
  for (i = 0; i < box.messages.length; i++)
  {
    var line = box.messages[i];

    if (type != MESSAGE_ALL && line.type != type) continue;

    if (line.alert_group != 0)
    {
      if (!is_def (alerts[line.alert_group])) alerts[line.alert_group] = "";
      alerts[line.alert_group] += line.msg + "\n";

      continue;
    }

//    for (id in line.ids)
    for (id = 0; id < line.ids.length; id++)
    {
      var elem = get_obj (line.ids[id]);
      if (!is_def (elem.old_className))
        elem.old_className = elem.className;
      elem.className = line.classes.ids;
    }

    if (line.msg != "")
    {
      txt += 
        '<div class="' + line.classes.text + '"' +
             'onmouseover="this.className = \'' + line.classes.over + '\'"' +
             'onmouseout="this.className = \'' + line.classes.text + '\'"';
      if (line.ids.length > 0)
        txt += 
             'onclick="get_obj (\'' + line.ids[0] + '\').focus ();';
      txt += 
          '">' + line.msg + 
        '</div>';
    }
  }

  box.innerHTML = txt;
  if (box.messages.length > 0 && txt != "")
    box.className = box.classes.on;
//  for (i in alerts)
  for (i = 0; i < alerts.length; i++)
    alert (alerts[i]);
}

//
function input_defaults_on_enter (event, target_id)
{
  var obj;
  var keynum;

  if (window.event) // IE
    keynum = event.keyCode;
  else
    if (event.which) // Netscape/Firefox/Opera
      keynum = event.which;

  if (keynum == 13)
    switch ((obj = get_obj (target_id)).type)
    {
      case "text": case "password":
        obj.focus ();
        break;
      case "button":
        obj.onclick ();
        break;
      default:
        obj.focus ();
        break;
    }
}

//
function form_submit (form, inc, action)
{
  form.inc.value = inc;
  form.action.value = action;

  for (var i = 3; i < arguments.length; i++)
//    get_obj (form.name + "_action_tag" + (i - 2)).value = arguments[i];
    form["action_tag" + (i - 2)].value = arguments[i];

  form.submit ();
}
//
function form_validate_empty (id)
{
  var obj = get_obj (id);

  if (obj == null) return true;
  if (obj.value == null) return false;

  return obj.value.length > 0;
}

//
function form_validate_empty_more (elem_ids, invalids)
{
//  for (i in elem_ids)
  for (i = 0; i < elem_ids.length; i++)
    if (!form_validate_empty (elem_ids[i])) invalids.push (elem_ids[i]);

  return invalids.length == 0;
}

//
function form_validate_float_only (id)
{
  var obj = get_obj (id);

  if (obj == null) return true;

  return obj.value != "" && isFinite (obj.value);
}

//
function form_validate_float_only_more (elem_ids, invalids)
{
//  for (i in elem_ids)
  for (i = 0; i < elem_ids.length; i++)
    if (!form_validate_float_only (elem_ids[i])) invalids.push (elem_ids[i]);

  return invalids.length == 0;
}

//
function form_validate_int_only (id)
{
  var obj = get_obj (id);

  if (obj == null) return true;
  
  return obj.value != "" && (parseInt (obj.value) == parseFloat (obj.value));
}

//
function form_validate_int_only_more (elem_ids, invalids)
{
//  for (i in elem_ids)
  for (i = 0; i < elem_ids.length; i++)
    if (!form_validate_int_only (elem_ids[i])) invalids.push (elem_ids[i]);

  return invalids.length == 0;
}

//
function form_validate_interval (id, min, max)
{
  var obj = get_obj (id);

  if (obj == null) return true;

  var num = parseFloat (obj.value);

  return (num >= min && num <= max);
}

//
function form_validate_interval_more (elem_ids, min, max, invalids)
{
//  for (i in elem_ids)
  for (i = 0; i < elem_ids.length; i++)
    if (!form_validate_interval (elem_ids[i], min, max)) invalids.push (elem_ids[i]);

  return invalids.length == 0;
}




//
function form_validate_illegal_values (id, values)
{
  var obj = get_obj (id);
  var f, i;

  if (obj == null) return true;
  if (obj.value == null) return false;

  for (f = false, i = values.length - 1; i >= 0 && !f; i--)
    f = (obj.value == values[i]);

  return !f;
}

//
function form_validate_illegal_values_more (elem_ids, values, invalids)
{
//  for (i in elem_ids)
  for (i = 0; i < elem_ids.length; i++)
    if (!form_validate_illegal_values (elem_ids[i], values)) invalids.push (elem_ids[i]);

  return invalids.length == 0;
}


