// $Id: xspHelper.js,v 1.6 2011/12/20 10:39:02 obo Exp $

// Create new pop-up window
function newWindow(mypage, myname, w, h, scroll) {
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 3;
  winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
  if(dojo.isIE) {
    setTimeout(
      function() {
        var w = window.open(mypage, myname, winprops);
        w.focus();
      }
    ,1);
  } else {
    var w = window.open(mypage, myname, winprops);
    w.focus();
  }
}

// Form manipulation for the profile page
function profileDisableNotificationElements() {
  profileDisableArray(document.getElementsByName("Method"));
  profileDisableArray(document.getElementsByName("NotificationLimit"));
}
function profileDisableArray(array) {
  for (i = 0; i < array.length; i++) {
    array[i].disabled = true;
  }
}

// Reload the whole window
function windowReload() {
  var path = window.location.pathname;
  var qs = window.location.search;
  window.location.replace(path + qs);
  return false;
}

// Send a password reminder to the address defined
function sendPassword(email, lang) {
  var targetURL = "/services/mymarketpulse/send_password_" + lang + ".json?email=" + email;

  dojo.style("sendPasswordInfoOKButtonRow", "display", "none");
  dojo.style("sendPasswordInfoBeforeRequest", "display", "");
  dojo.style("sendPasswordInfoAfterRequest", "display", "none");
  dijit.byId("sendPasswordDialog").show();
  dojo.style("sendPasswordInfo", "opacity", 1);
  dojo.style("sendPasswordInfo", "display", "");

  // Make a request to the XSP that handles the send password function
  dojo.xhrGet( {
    url: targetURL,
    handleAs: "json-comment-filtered",
    timeout: 20000,

    // If HTTP 200 (successful or "Problem")
    load: function(response, ioArgs) {
      dojo.byId("sendPasswordInfoAfterRequest").innerHTML = response.Message;
      dojo.style("sendPasswordInfoBeforeRequest", "display", "none");
      dojo.style("sendPasswordInfoAfterRequest", "display", "");

      // Show button and wait for user
      dojo.style("sendPasswordInfoOKButtonRow", "display", "");
      dijit.byId("sendPasswordInfoOKButton").setDisabled(false);
      return response;
    },
    // HTTP not OK - report error message and wait for user
    error: function(response, ioArgs) {
      dojo.style("sendPasswordInfo", "display", "");
      dojo.byId("sendPasswordInfoAfterRequest").innerHTML = message;
      dojo.style("sendPasswordInfoBeforeRequest", "display", "none");
      dojo.style("sendPasswordInfoAfterRequest", "display", "");
      dojo.style("sendPasswordInfoOKButtonRow", "display", "");
      dijit.byId("sendPasswordInfoOKButton").setDisabled(false);
      dijit.byId("sendPasswordDialog").show();
      return response;
    }
  });
}

// Convert 1234567890 into 1'234.568 (used in Live Turnover)
function formatTurnover(value, divisor, decimals) {

  // Divisor for scaling the numbers, 1e6 by default.
  if (!divisor)
    divisor = 1e6;
  if (divisor == 0)
    return 0;

  // No. of decimals after the point, 3 by default.
  if (!decimals)
    decimals = 3;

  // scale the number, set the decimals and apply the separator
  var formatted  = value/divisor;
  formatted = formatted.toFixed(decimals);
  return applySeparator(formatted);
}

// Convert 1234567 into 1'234'567
function applySeparator(value, separator, grouping) {

  // Character used in separation, "'" by default
  if (!separator)
    separator = "'";

  // No. of integers in group, 3 by default.
  if (!grouping)
    grouping = 3;

  // Save the decimal part, reverse the integer part, insert the separators,
  // reverse back again and tag on the decimals (if they exist).
  var tokens = value.split(".");
  var integerPart = tokens[0];
  var decimalPart = tokens[1];
  var integerPartReversed = integerPart.split("").reverse().join("");
  var integerPartArray = integerPartReversed.split("");
  var buffer = new String("");
  for (var i = 0; i < integerPartArray.length; i++) {
    if (i > 0 && i%grouping == 0)
      buffer = buffer.concat(separator);
    buffer = buffer.concat(integerPartArray[i]);
  }
  var result = buffer.split("").reverse().join("");
  if (decimalPart)
    result = result.concat(".", decimalPart);
  return result;
}

