// JavaScript generic utility functions for ShtetlSeeker.
// Warren Blatt, May 2006.


function setOptionValue(SelectID, Value)
{
  // debug
  // alert ("SelectID: '" + SelectID + "',     Value: '" + Value + "'");
  
  if ( ! Value ) return;

  var select = document.getElementById(SelectID);
  for (i=0; i < select.length; i++)
    {
      if (select.options[i].value == Value)
        {
          select.options[i].selected = 1;
          // Note that the Select's onChange handler is NOT called
          return;
        }
    }  
}


function getOptionValue(SelectID)
{
  var select = document.getElementById(SelectID);

  var option = select.options[select.selectedIndex];
  
  return option.value;
}


// Set the radio button with the given value as being checked
function setCheckedValue(radioObj, Value) 
{
  // If there are no radio buttons or a blank value, do nothing.
  if ( ! Value ) return;
  if ( ! radioObj ) return;

  var radioLength = radioObj.length;
  if (radioLength == undefined) 
    {
      radioObj.checked = (radioObj.value == Value);
      return;
    }

  // If the given value does not exist, 
  //   all the radio buttons are reset to unchecked.
  for (var i = 0; i < radioLength; i++) 
    {
      radioObj[i].checked = false;
      if (radioObj[i].value == Value) 
        { radioObj[i].checked = true; }
    }
}


function getCheckedValue(radioObj)
{
  // If there are no radio buttons, do nothing.
  if ( ! radioObj ) return;

  var radioLength = radioObj.length;
  for (var i=0; i < radioLength; i++)
    {
      if (radioObj[i].checked == true)
        { return radioObj[i].id; }
    }
}


function isNotEmpty(elem)
{
  var str = elem.value;
  if ( str == null || str.length == 0 )
    {
      elem.focus();
      elem.style.backgroundColor="red";
      return false;
    }
  else
    {
      return true;
    }
}


function get_cookie(Name) 
{
  var search = Name + "="
  var returnvalue = "";

  // debug
  // alert ("Cookie is: '" + document.cookie + "'");

  if (document.cookie.length > 0) 
    {
      offset = document.cookie.indexOf(search)
      // if cookie exists
      if (offset != -1) 
        { 
          offset += search.length
          // Set index of beginning of value.
          end = document.cookie.indexOf(";", offset);
          // Set index of end of value
          if (end == -1) end = document.cookie.length;
      
          returnvalue = unescape(document.cookie.substring(offset, end));
        }
    }
  return returnvalue;
}

// To retrieve the cookie value, use:
// var cookie = get_cookie(window.location.pathname)


// To be used as a window.OnUnload callback.
function set_cookie(Value)
{
  document.cookie = window.location.pathname + "=" + Value;
}


