function bcm() {
  var o = this;
  this.pageID = 0;
  this.pageIDs = "";
  this.msgRequiredFields = "";
  this.msgMailError = "";

  // Update module content from XML result
  this.updateModule = function (responseXML) {
    if ($("PAGE", responseXML).size() == 0 && responseXML != "")
      alert ("Error:\n" + responseXML);
    else {
      $("MODULE", responseXML).each (
        function() {
          var m = $(this);
          $("#" + m.attr("id")).empty().replaceWith (m.text());
        });
      var debug = $("DEBUG", responseXML).text();
      if (debug != '') alert (debug);
    }
  }

  // General ajax call
  this.exec = function (moduleID, actionID, postData) {
    $.post ("engine/ajax.php?actionID=" + actionID + "&pageID=" + this.pageID + "&moduleID=" + moduleID,
            postData,
            this.updateModule,
            "xml");
    return (false);
  }

  // Add new item to cart
  this.addToCart = function (moduleID, groupName, itemID, propertyName) {
    $("#" + moduleID).effect ("highlight");
    return this.exec (null, "addToCart", {groupName: groupName, itemID: itemID, propertyName: propertyName});
  }
  this.shopAddToCart = function (moduleID, itemID) { return (this.addToCart (moduleID, "shop", itemID)); }
  this.testerAddToCart = function (moduleID, itemID) { return (this.addToCart (moduleID, "tester", itemID)); }

  // Remove item from cart
  this.removeFromCart = function (groupName, itemID, propertyName) {
    return this.exec (null, "removeFromCart", {groupName: groupName, itemID: itemID, propertyName: propertyName});
  }

  // Save cart
  this.saveCart = function() {
    $("#bcmCartBuyForm").ajaxSubmit (
      {url: "engine/ajax.php?actionID=saveCart&pageID=" + this.pageID,
       type: "POST",
       success: this.updateModule
      });
  }

  // Empty cart
  this.emptyCart = function() {
    return this.exec (null, "emptyCart");
  }

  // Buy
  this.confirmCart = function (moduleID) {
    if (!$("#bcmCartBuyForm [name=customerName]").val() ||
        !$("#bcmCartBuyForm [name=customerStreet]").val() ||
        !$("#bcmCartBuyForm [name=customerPostCode]").val() ||
        !$("#bcmCartBuyForm [name=customerPostName]").val() ||
        !$("#bcmCartBuyForm [name=customerMail]").val())
     alert (this.msgRequiredFields);
    else if (!$("#bcmCartBuyForm [name=customerMail]").val().isMail())
     alert (this.msgMailError);
    else
      $("#bcmCartBuyForm").ajaxSubmit (
        {url: "engine/ajax.php?actionID=confirmCart&pageID=" + this.pageID + "&moduleID=" + moduleID,
         type: "POST",
         success: this.updateModule
        });
    return (false);
  }

  // Contact form
  this.confirmContact = function (moduleID) {
    var mail = $("#bcmContactForm [name=customerMail]").val();
    if (!$("#bcmContactForm [name=customerName]").val() ||
        !$("#bcmContactForm [name=customerMsg]").val())
     alert (this.msgRequiredFields);
    else if (mail && !mail.isMail())
     alert (this.msgMailError);
    else
      $("#bcmContactForm").ajaxSubmit (
        {url: "engine/ajax.php?actionID=sendContact&pageID=" + this.pageID + "&moduleID=" + moduleID,
         type: "POST",
         success: this.updateModule
        });
    return (false);
  }

  // Ad form
  this.confirmAd = function (moduleID) {
    var mail = $("#bcmAdForm [name=customerMail]").val();
    if (!$("#bcmAdForm [name=customerName]").val() ||
        (!mail && !$("#bcmAdForm [name=customerPhone]").val()) ||
        !$("#bcmAdForm [name=adGroup]").val() ||
        !$("#bcmAdForm [name=adTitle]").val() ||
        !$("#bcmAdForm [name=adDesc]").val())
     alert (this.msgRequiredFields);
    else if (mail && !mail.isMail())
     alert (this.msgMailError);
    else
      $("#bcmAdForm").ajaxSubmit (
        {url: "engine/ajax.php?actionID=saveAd&pageID=" + this.pageID + "&moduleID=" + moduleID,
         type: "POST",
         success: this.updateModule
        });
    return (false);
  }

  // Subscribe to news
  this.confirmSubscribeToNews = function (moduleID) {
    if (!$("#bcmNewsSubscribeForm [name=email]").val().isMail())
     alert (this.msgMailError);
    else
      $("#bcmNewsSubscribeForm").ajaxSubmit (
        {url: "engine/ajax.php?actionID=subscribeToNews&pageID=" + this.pageID + "&moduleID=" + moduleID,
         type: "POST",
         success: this.updateModule
        });
    return (false);
  }

  // Survey comment visiblity
  this.surveyComment = function (moduleID) { $("#surveyComment" + moduleID).toggle(); }

  // Save survey
  this.confirmSurvey = function (moduleID, gender) {
    $("#bcmSurveyForm" + moduleID).ajaxSubmit (
      {url: "engine/ajax.php?actionID=saveSurvey&pageID=" + this.pageID + "&moduleID=" + moduleID + "&surveyGender=" + gender,
       type: "POST",
       success: this.updateModule
      });
  }

  // Change static gallery tab
  this.galleryTab = function (moduleID, fileName) {
    $("#" + moduleID + " > img").attr ("src", fileName);
  }

}

// Create and init object
var bcm = new bcm();
$(document).ready (function() {
  $("textarea[name=surveyCommentText], input[name=surveyCommentName]").css ("width", "100%");
  $("div[id^=surveyComment]").hide();
});
