var ie5 = false;
var ns6 = false;
var moz = false;
var mac = false;

agent = navigator.userAgent.toLowerCase();
ie5   = (document.all && document.getElementById)  ? true : false;
ns6   = (document.getElementById && !document.all) ? true : false;
moz   = (document.getElementById && !document.all) ? true : false;
mac   = (agent.indexOf("mac")!=-1);

function IsArray(obj) { return obj instanceof Array; }
function SafeMail(name, domain, display, css, subject) {
  var css       = (css)     ? ' class="' + css + '"' : '';
  var subject   = (subject) ? '?subject=' + subject : '';
  var displayed = (display) ? display : name + '@' + domain;
  var mailto    = name + '@' + domain + subject;
  document.write('<a href="mailto:' + mailto + '"' + css + '>' + displayed + '</a>');
}
function Popup(title,filename,w,h,scroll,resizable,menubar) {
  title     = (title == '')         ? 'popup' : title;
  scroll    = (scroll == 'scroll')  ? 'yes'   : 'no';
  resizable = (resizable == 'lock') ? 'no'    : 'yes';
  menubar   = (menubar == 'menu')   ? 'yes'   : 'no';
  
  winOptions = eval("'width="+w+",height="+h+",toolbar=no,location=no,directories=no,status=no,menubar="+menubar+",scrollbars="+scroll+",resizable="+resizable+",copyhistory=no'");
  window.open(filename,title,winOptions);
  return;
}
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 SetFocus(formname, fieldname){if (formname == ''){ formname = 'form'; }if (eval("document."+formname+".elements[fieldname].value == ''")){ eval("document."+formname+".elements[fieldname].focus();"); }}
function SpellCheck() {
	var speller = new spellChecker();
	speller.spellCheckAll();
}
function DisableButtonImg(obj) {
  obj.src          = 'images/loading.gif';
  obj.disabled     = true;
  obj.className    = 'no-border';
}
function DisableButtonTxt(obj) {
  obj.value        = 'Processing...';
  obj.disabled     = true;
  obj.className    = 'no-border';
}
function SubmitForm(obj, form_action) {
  // disable button
  DisableButtonTxt(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 ToggleElement(id) {
  if ($(id)) { Element.toggle(id); }
}
function TestPassword(passwd) {
  var e = $('passwd');
  if (passwd.length > 5) {
    e.removeClassName('password-bad');
    e.addClassName('password-good');
  } else {
    e.removeClassName('password-good');
    e.addClassName('password-bad');
  }
}
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) {
  /////////
  // :NOTE:
  // 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'] = '#e2f9e3';
  colors['error']   = '#ffcfce';
  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 confirmDelete() {
  var agree=confirm("Are you sure you wish to delete this record?");
  if (agree)
  	return true;
  else
  	return false;
}
function confirmDeleteCategory() {
  var agree=confirm("Are you sure you wish to delete this category? All dependent records will also be deleted.");
  if (agree)
  	return true;
  else
  	return false;
}

// insert link icons
Event.observe(window, 'load', function() {
  $$('a[href$=.doc]').each(function(element) { element.addClassName('link_msword'); })
  $$('a[href$=.word]').each(function(element) { element.addClassName('link_msword'); })
  $$('a[href$=.txt]').each(function(element) { element.addClassName('link_txt'); })
  $$('a[href$=.text]').each(function(element) { element.addClassName('link_txt'); })
  $$('a[href$=.rtf]').each(function(element) { element.addClassName('link_txt'); })
  $$('a[href$=.xml]').each(function(element) { element.addClassName('link_code'); })
  $$('a[href$=.xls]').each(function(element) { element.addClassName('link_xls'); })
  $$('a[href$=.pdf]').each(function(element) { element.addClassName('link_pdf'); })
});
