/**
 * Common JS utilities. Used across our code, but also used by public API code 
 * (e.g. Widget), so keep it as small as possible. 
 */

/*
 * Show the element identified by showid.
 */
function LLShow(_showid) {
  if (_showid != null) {
    var showElem = document.getElementById(_showid);
    if (showElem != null) {
      showElem.style.display = 'block';
    }
  }
}

/*
 * Hide the element identified by hideid.
 */
function LLHide(_hideid) {
  if (_hideid != null) {
    var hideElem = document.getElementById(_hideid);
    if (hideElem != null) {
      hideElem.style.display = 'none';
    }
  }
}
 
function LLSearchSetForm(_x, _d, _fontSz) {
    if (_x.value == _d) {
      _x.value = '';
      _x.style.color="black";
      if (_fontSz) {
        _x.style.fontSize = _fontSz;
      }
   }
}

function LLSearchResetForm(_x, _d, _fontSz) {
    if (_x.value == '') {
      _x.value = _d;
      _x.style.color="silver";
      if (_fontSz) {
        _x.style.fontSize = _fontSz;
      }
    }
}

function LLTextLimit(field, maxlen, event, alertCode)
{
  event = window.event || event;
  
  var key = event.which || event.keyCode;
  
  if(!((key >= 48 && key <= 90) || (key > 95 && key < 112) || (key > 185) || key == 12 || key == 13)) {
    return true;
  }

  if ((field.value.length) >= maxlen) {
    if (1 == alertCode) {
      alert('Please press the "Next Step" button below, then enter more text in "Details" field.');
    }
    return false;
  } else {
    return true;
  }
}

// extraArgs: append these args. Use leading comma (e.g. ",scrollbars,toolbar").
function LLShowPopup(url, extraArgs, h, w, t, l) 
{
  if (!h) {
    h = 620;
  }
  if (!w) {
    w = 900;
  }
  if (!t) {
    t = 60;
  }
  if (!l) {
    l = 100;
  }
  newwindow=window.open(url,'name','height='+h+',width='+w+',top='+t+',left='+l+',resizable' + extraArgs); 
  if (window.focus) {
    newwindow.focus()
  } 
} 

function GetComponentURL() 
{
  var url = null; 
  var idx = location.href.lastIndexOf(location.pathname);
  if (idx >= 0) {
    url = location.href.substr(0,idx) + "/getcomponent.php";    
  } else {
    url = "/getcomponent.php";  // Relative path?   
  }
  return (url);
}

function LLTrim(str)
{
  return(str.replace(/^\s+|\s+$/g, "")); 
}

function LLValidateEmail(str)
{
  return(str.match(/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i));
}
