function checkrgb(elem)
{
  if(value = elem.value)
  {
    if((value == 'transparent') || (value == 'transp')) // This is acceptable.
    {
      return true;
    }
    if((value.length != 3) && (value.length != 6))
    {
      alert("RGB color codes should be 3 or 6 characters.");
      elem.focus();
      return false;
    }
    if(!/^[0-9a-fA-F]+$/i.test(value))
    {
      alert("RGB color codes may only contain numbers or the letters A-F.");
      elem.focus();
      return false;
    }
  }
  return true;
}

function currency(value)
{
  if(value == 0) return "0.00";
  temp = new String(Math.round(value * 100));
  return temp.substring(0, temp.length - 2) + '.' +
    temp.substring(temp.length - 2);
}

function formatPhone(element, format)
{
  // Strip everything but numbers.
  stripped = "";
  for(i = 0; i < element.value.length; i++)
  {
    letter = element.value.substring(i, i+1);
    if((letter >= "0") && (letter <= "9")) stripped = stripped + letter;
  }
  if(!stripped.length) return;

  if(format[0] == "-")
  {
    element.value = stripped.substr(0, 3) + "-" + stripped.substr(3, 3) +
      "-" + stripped.substr(6, 4) + (stripped.substr(10) ? "-" +
      stripped.substr(10) : "");
  }
  else if(format[0] == ".")
  {
    element.value = stripped.substr(0, 3) + "." + stripped.substr(3, 3) +
      "." + stripped.substr(6, 4) + (stripped.substr(10) ? "." +
      stripped.substr(10) : "");
  }
  else if(format[0] == "s")
  {
    element.value = stripped.substr(0, 3) + " " + stripped.substr(3, 3) +
      " " + stripped.substr(6, 4) + (stripped.substr(10) ? " " +
      stripped.substr(10) : "");
  }
  else
  {
    element.value = "(" + stripped.substr(0, 3) + ") " + stripped.substr(3, 3) +
      "-" + stripped.substr(6, 4) + (stripped.substr(10) ? " x" +
      stripped.substr(10) : "");
  }
}

function validateState(element)
{
  // Strip everything but letters.
  stripped = "";
  for(i = 0; i < element.value.length; i++)
  {
    letter = element.value.substring(i, i+1);
    if(((letter >= "a") && (letter <= "z")) ||
      ((letter >= "A") && (letter <= "Z"))) stripped = stripped + letter;
  }

  if(stripped.length != 2)
  {
    alert("Please use the two-letter state abbreviation.");
    element.focus();
  }
  else element.value = stripped.toUpperCase();
}

function validateZIP(element)
{
  // Strip everything but numbers.
  stripped = "";
  for(i = 0; i < element.value.length; i++)
  {
    letter = element.value.substring(i, i+1);
    if((letter >= "0") && (letter <= "9")) stripped = stripped + letter;
  }

  if((stripped.length != 5) && (stripped.length != 9))
  {
    alert("Please use either a five- or nine-digit ZIP code.");
    element.focus();
  }
  else element.value = stripped.substring(0, 5) + (stripped.length > 5 ? "-" +
    stripped.substring(5, 9) : "");
}

// New Beta functionality ------------------------------------------------------

function do_edit_caption(id)
{
  elem = document._form["_photo_caption[" + id + "]"];
  elem.value = prompt("Please enter a brief caption for this photo:",
    elem.value);
}

function do_remove_photo(id)
{
  if(confirm("Are you sure you want to remove this photo?"))
  {
    document._form.what.value = "unattach-photo";
    document._form._photo_id.value = id;
    document._form.submit();
  }
}

function do_tab(tab)
{
  document._form.tab.value = tab;
  document._form.submit();
}

function edit_lookup(kind)
{
  window.open("../lookup.php?kind=" + kind, "lookupWindow",
    "width=600,height=500,resizable=yes,scrollbars=yes").focus();
}

function go(id, tab)
{
  document._form.id.value = id;
  do_tab(tab ? tab : "edit");
}

function multiselect_add(from)
{
  to = document._form[from.name.substr(4)];
  to[to.length] = new Option(from[from.selectedIndex].text,
    from[from.selectedIndex].value);

  for(i = from.selectedIndex; i < from.length-1; i++)
    from[i] = new Option(from[i+1].text, from[i+1].value);
  from[from.length-1] = null;
}

function multiselect_remove(from)
{
  to = document._form["_ms_" + from.name];
  to[to.length] = new Option(from[from.selectedIndex].text,
    from[from.selectedIndex].value);

  for(i = from.selectedIndex; i < from.length-1; i++)
    from[i] = new Option(from[i+1].text, from[i+1].value);
  from[from.length-1] = null;
}

function multiselect_save(elem)
{
  tags = document.getElementsByTagName("select");
  for(i = 0; i < tags.length; i++)
  {
    target = tags[i];
    if(target.name.substr(0, 4) == "_ms_")
    {
      elem = document._form[target.name.substr(4)];
      for(j = 0; j < elem.length; j++) elem[j].selected = true;
    }
  }
}

function suggest_multiselect_remove(from)
{
  for(i = from.selectedIndex; i < from.length-1; i++)
    from[i] = new Option(from[i+1].text, from[i+1].value);
  from[from.length-1] = null;
  
  if(!from.length)
  {
    $(from).hide();
  }
}

function suggest_multiselect_save(elem)
{
  
 $$('select[id^="suggest_"] option').each(function(e) {
   e.selected = true;
  });          
}

function suggest_text_save(elem)
{  
  $$('input[id^="suggest_"][class="suggest_text"]').each(function(e) {
    if($F(e) == '')
    {   
      id = e.name.substr(8);
      $(id).setValue(0);
    }
  });          
}

function note_remove(nid)
{
  elem = document.getElementById("_note_" + nid);
  elem.innerHTML = "<i>This note will be removed when you click 'Save'.</i>";
  document._form._note_ids.value += " " + nid;
}

function required(field, desc)
{
  alert("'" + desc + "' is a required field.");
  required_ok = false;
  document._form[field].focus();
}

function toggle(name, image_closed, image_open)
{
  target = document.getElementById(name);
  if(target.style.display == "none")
  {
    target.style.display = "";
    document.images["toggle_" + name].src = image_open;
    document._form["toggle_" + name].value = 1;
  }
  else
  {
    target.style.display = "none";
    document.images["toggle_" + name].src = image_closed;
    document._form["toggle_" + name].value = 0;
  }
}

function unattach_file(file)
{
  if(confirm("Are you sure you want to remove this file?"))
  {
    document._form.what.value = "unattach";
    document._form.attached_file.value = file;
    document._form.submit();
  }
}

function unattach_image(file)
{
  if(confirm("Are you sure you want to remove this image?"))
  {
    document._form.what.value = "unattach";
    document._form.attached_file.value = file;
    document._form.submit();
  }
}

function validate_domain(elem)
{
  value = elem.value;
  if(!value.length) return true;

  return /^\w[\w-]*\.[a-zA-Z][a-zA-Z]+$/.test(value);
}

function validate_email(elem)
{
  value = elem.value;
  if(!value.length) return true;

  return /^\w[\w\.-]*@[\w\.-]+\.\w+$/.test(value);
}

function validate_image(elem)
{
  value = elem.value;
  if(!value.length) return true;

  result =
    /gif$/i.test(value) ||
    /jpg$/i.test(value) ||
    /jpeg$/i.test(value) ||
    /png$/i.test(value);
  return result;
}

function validate_image_swf(elem)
{
  value = elem.value;
  if(!value.length) return true;

  result =
    /gif$/i.test(value) ||
    /jpg$/i.test(value) ||
    /jpeg$/i.test(value) ||
    /png$/i.test(value) ||
    /swf$/i.test(value);
  return result;
}

function validate_rgb(elem)
{
  value = elem.value;
  if(!value.length || (value == 'transparent') || (value == 'transp'))
  {
    return true;
  }
  return ((value.length == 3) || (value.length == 6)) &&
    /^[0-9a-f]+$/i.test(value);
}

function validate_url(elem)
{
  value = elem.value;
  if(!value.length) return true;
  if(value.indexOf("://") > 0) value = value.substr(value.indexOf("://") + 3);

  return /^\w[\w\.-]*\.\w/.test(value);
}

function warning(message, field)
{
  alert(message);
  if(field) document._form[field].focus();
  return false;
}
