var shcp_ele = document.getElementById('sharedcopy-email-button');
shcp_ele.onclick = function() {
  // inside onclick so that these mods take effect 
  // only when user clicks on the email function
  window.SharedCopyExtender = {
    validate_emails: function(element_id) {
      var element = document.getElementById(element_id);
      if (element && element.value) {
        var array = element.value.split(/[,;\s]+/);
        for (var x = 0; array[x]; x++) {
          if (! array[x].match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i)) {
            alert("Invalid email address: " + array[x]);
            return false;
          }
        }
        return true;
      } else {
        alert('Please check your email addresses and try again');
      }
      return false;
    },
    validate_email: function(element_id) {
      var element = document.getElementById(element_id);
      if (element && element.value) {
        if (
          element.value.match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) ||
          element.value.match(/^\"[^\"]*\" <([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})>\s*$/i) ||
          false
        ) {
          return true;
        } else {
          alert("Invalid email address: " + element.value);
        }
      } else {
        alert('Please check your email addresses and try again');
      }
      return false;
    },

    on_document_loaded: function() {
      var main_form = jQuery('#karea form')[0];
      var original_submit = main_form.onsubmit;
      main_form.onsubmit = function() {
        if (SharedCopyExtender.validate_email('shcp_bookmarklet_email_from') && SharedCopyExtender.validate_emails('shcp_bookmarklet_email_to')) {
          return original_submit();
        } else {
          return false;
        }
      }

      var email_element = jQuery("" + 
      "<div id='custom_toolbar' onclick='return true;'>" + 
        "<dl>" + 
          "<dt>From<span>your email</span></dt>" + 
          "<dd><input type='text' name='bookmarklet_email[from]' id='shcp_bookmarklet_email_from' /></dd>" + 
          "<dt>To<span>your friends; comma separated emails</span></dt>" + 
          "<dd><input type='text' name='bookmarklet_email[to]' id='shcp_bookmarklet_email_to' /></dd>" + 
          "<dt>Personal message<span>to accompany your annotations</span></dt>" + 
          "<dd><textarea name='bookmarklet_email[message]' id='shcp_bookmarklet_email_message'>Hey, check this out!</textarea></dd>" + 
          "<br />" + 
          "<input type='button' value='Send' style='text-align: center; margin-bottom: 1em;' />" + 
        "</dl>" + 
      "</div>" + 
      "");
      jQuery('#karea form').append(email_element);
      if (SharedCopy.config.user_email) jQuery('#shcp_bookmarklet_email_from', email_element).val(SharedCopy.config.user_email);
      jQuery('#karea #k_button_save').hide();
      jQuery("input[value='Send']", email_element).click(function() { shcp.toolbar.common_buttons.save_button.events.self.click(); });

      // fix FF2 blinking-cursor issue
      shcp.toolbar.position_toolbar();
      setTimeout(function() {
        shcp.toolbar.position_toolbar();
        try {
          jQuery('#shcp_bookmarklet_email_' + (SharedCopy.config.user_email ? 'to' : 'from'), email_element).focus();
        } catch (e) { }
      }, 700);    
    }
  }
  var u="http://sharedcopy.com";
  var c=document;
  var b=c.body;
  var d=b.appendChild(c.createElement('div'));
  d.id="k_deleteme";
  d.innerHTML="<h1 style='position:absolute;top:"+((b.scrollTop||c.documentElement.scrollTop)+10)+"px;background-color:#eee'><img src='"+u+"/images/loading.gif?V0' />LOADING..</h1>";
  var s=c.createElement('script');
  s.id='k_script';
  s.src=u+'/javascripts/k/shcp_system_email.js?v=1'+ ('%s'==('%'+'s')?'':'&t=%s')+'&r='+Math.random();
  b.appendChild(s);
  return false;
}
if (navigator.userAgent && navigator.userAgent.match(/MSIE/)) {
  shcp_ele = document.getElementById('sharedcopy-email-button-img');
  shcp_ele.src = shcp_ele.src.replace(".png", ".gif");
}
