function IsArray(obj) { return obj instanceof Array; }
function SafeMail(name, domain, addinfo) {
  style     = (addinfo.style)   ? ' class="' + addinfo.style + '"' : '';
  subject   = (addinfo.subject) ? '?subject=' + addinfo.subject : '';
  displayed = (addinfo.display) ? addinfo.display : name + '@' + domain;
  mailto    = name + '@' + domain + subject;
  document.write('<a href="mailto:' + mailto + '"' + style + '>' + displayed + '</a>');
}
function PopWindow(title, url, width, height, modal) {
  var id = 'popwindow';
  if (!$(id)) {
    BuildWindow({id:id, title:title, url:url, width:width, height:height, modal:modal});
  }
}
function SpellCheck() {
	var speller = new spellChecker();
	speller.spellCheckAll();
}
function SetFocus(fieldname,formname) {
  if (formname == '') {
    formname = 'form';
  }
  if (eval("document."+formname+".elements[fieldname].value == ''")) {
    eval("document."+formname+".elements[fieldname].focus();");
  }
}
function DisableButtonImg(obj) {
  obj.src          = '/images/loading.gif';
  obj.disabled     = true;
  obj.style.border = 'none';
}
function DisableButton(obj) {
  obj.blur();
  obj.value        = 'Processing...';
  obj.disabled     = true;
  obj.style.border = 'none';
}
function SubmitForm(obj, form_action) {

	// disable button
  DisableButtonImg(obj);
  
  // get elements
  var frm = obj.form;
  var act = frm.elements['form_action'];
  
  // set form action
  act.value = form_action;
  
  // submit form
  frm.submit();
}
function IncludeLibraries(libraries) {
  libraries.each(function(library) {
    document.write('<script type="text/javascript" src="' + library + '"></script>');
  });
}
function StripeTable(id) {
  $A($(id).getElementsByTagName('tbody')).each(function(tbody) {
    $A(tbody.immediateDescendants()).each(function(row, i) {
      row.removeClassName('odd');
      row.removeClassName('even');
      row.addClassName((i % 2) ? 'odd' : 'even');
    });
  });
}
function StripeRows(id) {
  $A($(id).immediateDescendants()).each(function(row, i) {
    row.removeClassName('odd');
    row.removeClassName('even');
    row.addClassName((i % 2) ? 'odd' : 'even');
  });
}
function SetNotice(content, status) {
  // :KLUDGE: having to force background colors
  //          highlight effect isn't reading new color
  //          from dynamically set classname
  // status related colors
  var colors = new Array();
  colors['success'] = '#eee';
  colors['error']   = '#eee';
  var bgcolor = (status) ? colors[status] : colors['success'];
  
  // update notice
  var e = $('notice');
  e.update(unescape(content));
  if (status) { e.className = 'notice-' + status; }
  e.show();
  new Effect.Highlight(e, {duration:1.5, keepBackgroundImage:true, startcolor:'#ffff88', endcolor:bgcolor, restorecolor:bgcolor});
}
function ClearNotice() {
  var e = $('notice');
  e.update('');
  e.hide();
}
function SelectAction(form, action, id) {
  // require confirmation?
  if (IsArray(immediate_actions) && immediate_actions.indexOf(action) >= 0) {
    PostForm(form, action, id);
  } else if (IsArray(confirmed_actions) && confirmed_actions.indexOf(action) >= 0) {
    ConfirmAction(form, action, id);
  }
}
function ConfirmAction(form, action, id) {
  // "now" indicates action from list
  if (action.match(/_now$/)) {
    // highlight selected row
    $('row' + id).addClassName('highlight');
    
    // ask for confirmation
    response = confirm("Please confirm you wish to " + action.sub('_now', '') + " the highlighted item.");
  } else {
    // ask for confirmation
    response = confirm("Please confirm you wish to " + action + " this item.");
  }
  
  // check response
  if (response === true) {
    // post form
    PostForm(form, action.sub('_now', ''), id);
  } else if (action.match(/_now$/)) {
    // remove highlight
    $('row' + id).removeClassName('highlight');
    
    // clear select
    if ($('select' + id)) { $('select' + id).selectedIndex = ''; }
  }
}
function PostForm(form, action, id) {

  // get form
  var frm = $(form);
  
  // set values and submit
  frm.elements['form_action'].value = action;
  frm.elements['id'].value          = id;
  frm.submit();
}
function ToggleElement(id) {
  if ($(id)) { Element.toggle(id); }
}
function AddBookmark(url, title) {
  if (window.sidebar) {
    // Mozilla Firefox Bookmark
    window.sidebar.addPanel(title, url,"");
	} else if ( window.external ) {
    // IE Favorite
    window.external.AddFavorite( url, title); 
  }	else if (window.opera && window.print) {
    // Opera Hotlist
    /*
  	var elem = document.createElement('a');
  	elem.setAttribute('href', url);
  	elem.setAttribute('title', title);
  	elem.setAttribute('rel', 'sidebar');
  	elem.click();
    */
    alert('Press Ctrl+D to bookmark this page.');
  }
}
function SortRecipes(category_id, section_id, type) {
  if ((category_id || section_id) && type) {
    // update packages
    var url    = '/ajax_sort_recipes.php';
    var pars   = 'category_id=' + category_id + '&section_id=' + section_id + '&type=' + type;
    var myAjax = new Ajax.Request(
      url,
      {
        method:'get',
        parameters:pars,
        onComplete:function(req) {
          if (req.responseText) {
            // update
            Element.update('recipe-list', req.responseText);
          }
        }
      }
    );
  }
}

