var permanent = 'Thu, 31 Dec 2020 23:59:59 UTC';
var expired = 'Sat, 1 Jan 1990 00:00:00 UTC';

function createCookie(name,value, expires) {
  var val= name + '=' + value + '; ';
  var exp = 'expires=' + expires  + '; ';
  document.cookie = val + exp + 'path=/';
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
      }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",expired);
}

function closeKeepAlive() {
  if (/AppleWebKit|MSIE/.test(navigator.userAgent)) {
    new Ajax.Request("/sessions/close", { asynchronous:false });
  }
}


var manufacturers = {
  showAssociationForm: function(){
    $('association_form').show();
    $('re_associate_link').hide();
    $('association_errors').hide();
  }
};

var filter_form = {
  setupAutoSubmit: function(){
    $$('.fitlerRightCol select, .fitlerRightCol input').each(function(filter_box) {
      Event.observe(filter_box, 'change', function() {
        $('filter_form').submit();
      });
  });
  }
};

var product_forms = {
  checkManufacturerVerified: function(checkbox_id){
    if (parseInt($F('manufacturer_verified_prompt_answered')) == 1 || $(checkbox_id).checked)
       return true;
    Modalbox.show('/prompt_manufacturer_verified?checkbox_id=' + checkbox_id, {title: 'Unverified Product', width: 400});
    return false;
  },

  submitProductForm: function(checkbox_id, change_mfr_verified, btn){
    btn = $(btn + '_btn');
    btn.disabled = true;
    $(checkbox_id).checked = change_mfr_verified;
    $('manufacturer_verified_prompt_answered').value = 1;
    $('edit_product_form').onsubmit();
  }
};

var change_log = {
  toggleDetails: function(change_dom_id) {
    if ($('show_' + change_dom_id + '_details').style.display != 'none') {
      $('show_' + change_dom_id + '_details').hide();
      $('hide_' + change_dom_id + '_details').show();
    }
    else {
      $('show_' + change_dom_id + '_details').show();
      $('hide_' + change_dom_id + '_details').hide();
      Element.remove(change_dom_id + '_details');
    }

  }
};


var propertiesTable = {
  toggle: function(id) {
    var arrow = $('arrow_' + id);
    table_id = 'properties_' + id;
    if ($(table_id).visible()) {
      arrow.removeClassName('expanded');
      arrow.addClassName('contracted');
      $(table_id).hide();
      createCookie('status_' + id, '0', permanent);
    }
    else {
      arrow.removeClassName('contracted');
      arrow.addClassName('expanded');
      $(table_id).show();
      createCookie('status_' + id, '1', permanent);
    }
  },

  setDisplay: function(id) {
    var cookie_val = readCookie('status_' + id);
    table = $('properties_' + id);
    arrow = $('arrow_' + id);
    if (cookie_val == '1') {
      arrow.removeClassName('contracted');
      arrow.addClassName('expanded');
      table.show();

    }
    else if (cookie_val == '0') {
      arrow.removeClassName('expanded');
      arrow.addClassName('contracted');
      table.hide();
    }
  }
};




Event.observe(window, 'load', function() {
  $$('form.protected').each(function(frm) {
       $('code').value = '16450';
    });

  filter_form.setupAutoSubmit();


      $$('span.email').each (function (mail) {
                           email = mail.innerHTML;
                           email = email.replace(/\s\[dot\]\s/ig, ".");
                           email = email.replace(/\s\[at\]\s/ig, "@");
                           mail.innerHTML = '<a href="mailto:' + email + '">' + email + '</a>';
                         }
                                      );
                $$('a').each(function(lnk) {
                if (lnk.href && lnk.rel == 'external')
                  lnk.target = "_blank";
                                 });

              });
