function facebook_click() {
  u=location.href;
  t=document.title;
  window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
  return false;
}

function twitter_click() {
  u=location.href;
  t=document.title;
  window.open('http://twitter.com/home?status='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=810,height=436');
  return false;
}

jQuery(document).ready(function($) {
  jQuery("#newsletter").unbind().bind("submit",function(e) {
      e.preventDefault();
      var email = jQuery(this).find("#newsletter_epost").val();
      var firstName = jQuery(this).find("#newsletter_fornavn").val();
      var lastName = jQuery(this).find("#newsletter_etternavn").val();
      var phone = jQuery(this).find("#newsletter_telefon").val();
      if(isValidEmail(email)) {
        subscribeEmail("5",email,firstName,lastName,phone,function() {
          alert("Du er p\u00E5meldt!");
          jQuery("#newsletter_epost").val("");
          jQuery("#newsletter_fornavn").val("");
          jQuery("#newsletter_etternavn").val("");
          jQuery("#newsletter_telefon").val("");
        });
      }
      else {
          alert("Feil epost");
      }
    });
});


var urlroot = window.location.host.indexOf('idium.n') !== -1 ? window.location.pathname.substring(0,window.location.pathname.indexOf('/',2)) + "/" : "/";

var subscribeEmail = function(groups,email,firstName,lastName,phone,callback) {
  jQuery.get(urlroot+"?module=login&amp;action=logout&amp;template=xml",function() {
    jQuery.post(urlroot+"?module=Webshop&amp;action=ShoppingBasketView.displayShoppingBasket;template=xml;",function() {
      var postdata = {
        "module"    : "Users",
        "action"    : "User.publicEditCurrentUser",
        "template"  : "xml",
        "input"     : "yes",
        "mode"      : "diff",
        "gr"      : "in",
        "groups"    : groups,
        "email" : email,
        "_ips_encoding" : "utf-8"
      };
      if (firstName != undefined) 
        postdata.firstName = firstName;
      if (lastName != undefined) 
        postdata.lastName = lastName;
      if (phone != undefined) 
        postdata.attribute46 = phone;
      jQuery.post(urlroot,postdata,function(data) {
        if(callback != undefined)
          callback();
      });
    });
  });
}
function isValidEmail(str) {
  return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
