var TARGET="The MatrixStore Shopping List";
var WIN=null;
var SYSTEM_SUCCESS = 0;
var TB = 0;
maxTB = 0;
var CAP ="";
var nodeCap=0;
var curr;
var gbp = "&#163;";
var usd = "$";
var euro = "&#128;";
var currToDisplay = "";
var showAllCurrencies = false;
var _text = "";
var _singleNodeUsable;
var raidLoss=0;

var freeElement = 0; //FREE AMOUNT OF TB

var matrixstorenode = new Array();
matrixstorenode[0] =  "MatrixStore Node";
matrixstorenode[1] =  "One 2.8GHz Quad-Core Intel Xeon";
matrixstorenode[2] =  "Quad Channel gigabit Ethernet";
matrixstorenode[3] =  "RAID Controller";
matrixstorenode[4] =  "4GB RAM";

var capacity = new Array();
capacity[0] =  "9TB raw";
capacity[1] =  "12TB raw";
capacity[2] =  "18TB raw";
capacity[3] =  "24TB raw";

var prices = new Array();
prices[0] =  7995.00;
prices[1] =  8995.00;
prices[2] =  11995.00;
prices[3] =  13995.00;

var switches = new Array();
switches[0] = "24port 10/100/1000 switch";
switches[1] = 250.00; <!-- pounds
switches[2] = 500.00; <!-- usd
switches[3] = 400.00; <!-- euro
    
var mxsDesc = "";
var capDesc = "";
 
var nodesRequired;

var mxsItemCost;
var mxsItemCostFormatted;
var totalCostmxs;
var totalCostmxsFormatted;

var switchesRequired = 2;
var switchItemCost;
var totalCostSwitches;
var totalCostSwitchesFormatted;

var costpertb;
var costpertbformatted;

var totalmxsHardware;
var totalHardware;
var total;
var totalFormatted;

var pricing = new Array(); 
var gbpPricing = new Array(); 
var usdPricing = new Array(); 
var euroPricing = new Array(); 
  
 
// --------------------------------------------
//                  setPricing
// Sets pricing depending on Currency chosen
// --------------------------------------------
function setPricing(CAP)
{

  if(CAP =="9TB")
  {
    pricing[0] = prices[0]; 
  }

  if(CAP =="12TB")
  {
    pricing[0] = prices[1]; 
  }

  if(CAP =="18TB")
  {
    pricing[0] = prices[2]; 
  }
  if(CAP =="24TB")
  {
    pricing[0] = prices[3]; 
  }

    pricing[1] = switches[1]; 
}

// --------------------------------------------
//                  setNodeCap
// Sets capacity depending on node chosen
// --------------------------------------------
function setNodeCap(CAP)
{

  if(CAP =="9TB")
  {
	nodeCap=9;
	raidLoss=1;
  }

  if(CAP =="12TB")
  {
	nodeCap=12;
	raidLoss=1;
  }

  if(CAP =="18TB")
  {
	nodeCap=18;
	raidLoss=2;
  }
  if(CAP =="24TB")
  {
	nodeCap=24;
	raidLoss=2;
  }
}

// --------------------------------------------
//                  setNodeCap
// Sets capacity depending on node chosen
// --------------------------------------------
function getUsableCap(nodeCap)
{

  //determine number of nodes/switches required.
  var tbAfterRaid = (nodeCap-raidLoss)/nodeCap;
  _singleNodeUsable = (nodeCap*tbAfterRaid)/2;
}


// ---------------------------------------------------
//                  describe
// Populates the hardware info strings from the arrays
// ---------------------------------------------------
function describe()
{
  mxsDesc = "";

    for(var x=0; x<matrixstorenode.length; x++)
    {
        if(x==0)
        {
            mxsDesc+="<b>"+matrixstorenode[x]+"</b><br>";
        }
        else
        {
            mxsDesc+=matrixstorenode[x]+"<br>";
        }
    }
}

// ---------------------------------------------
//                  pop
// Launch a new browser window with the results
// ---------------------------------------------
function pop(_page)
{
  WIN=window.open();
  WIN.document.open();
  WIN.document.write(_page);
  WIN.document.close();
}

//WIDGET SECTION

// ---------------------------------------------
//                  widgetPop
// Launch browser with the results from widget
// ---------------------------------------------
function widgetPop(_page)
{
      var sharedDirectory = "/Users/Shared/";
      var filename = "MatrixStoreShoppingList" + TB + "TB.html";
      saveTextToFile(sharedDirectory+filename, _page);

      widget.openURL("file://"+sharedDirectory+filename);
}

// ---------------------------------------------
//            executeSysCommand
// launches system command from widget only
// ---------------------------------------------
function executeSysCommand  ( command )
{
        var output = widget.system ( command , null);
        return output;
}

// ---------------
//   nothing
// Does nothing
// ---------------
function nothing()
{}

// ---------------------------------------------
//                  saveTextToFile
// Writes a file to disk from widget only
// ---------------------------------------------
function saveTextToFile ( filename ,text )
{
       var command = "/bin/cat >" + filename;
       command = widget.system ( command , nothing);
       command.write ( text );
       command.close();
}

//Common section

// ----------------------------------------
//                  addCommas
// Add commas to the number as appropriate.
// ----------------------------------------
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// ---------------------------------------------------
//                  doNow
// the main function called from the browser
// ---------------------------------------------------
function doNow()
{

  //FOR WIDGET
  //document.getElementById("doButton").src = "Images/Button_down.png";

  var _tb=0;
  var _node="";

  _text = "";

  _tb = document.getElementById("no_tb").value;
  TB = _tb;

  _node = document.getElementById("node").value;
  CAP = _node;
  matrixstorenode[0] = CAP + " 'The MatrixStore' Node";

  setPricing(CAP);
  setNodeCap(CAP);

  //Get the useable capacity of the node
  getUsableCap(nodeCap);

  //Get the nodes required to cover the cap
  nodesRequired = Math.ceil(_tb/_singleNodeUsable);

  if(nodesRequired < 3) nodesRequired=3;

  if(nodesRequired>10)
  {  
    var remaind = nodesRequired%10;
    if(remaind==0)
    {
         switchesRequired = (nodesRequired/10)*2;
    }
    else
    {
         switchesRequired = Math.ceil(nodesRequired/10)*2;
    }
  }

  //Set the maxCapacity the cluster can support.§
  maxTB = nodesRequired * _singleNodeUsable;

  describe();

  //create the html header
  createHtmlHead();

  //set the pricing
  {
      currToDisplay = gbp;
      setPricingValues();
      createHtmlBody(0);
  }
  
  //create the html footer
  createHtmlFoot();

  //uncomment relevant piece until can find code to differentiate
  //FOR WIDGET
  //widgetPop(_text);
  //FOR BROWSER
  pop(_text);
}

// --------------------------------------------
//            setPricingValues
// Sets pricing value for currency choice
// --------------------------------------------
function setPricingValues()
{
   var total = 0;

   mxsItemCost = addCommas(pricing[0]);
   mxsItemCostFormatted = addCommas(pricing[0].toFixed(2));

   switchItemCost = addCommas(pricing[1].toFixed(2));
   totalCostSwitches = switchesRequired*pricing[1];
   totalCostSwitchesFormatted = addCommas((switchesRequired*pricing[1]).toFixed(2));

   totalmxsHardware = nodesRequired*pricing[0];
   totalmxsHardwareFormatted = addCommas((nodesRequired*pricing[0]).toFixed(2));

   total = totalmxsHardware+totalCostSwitches; 
   totalHardware = addCommas(total.toFixed(2));

   costpertb = total/TB;
   costpertbformatted = addCommas(costpertb.toFixed(2)); 
}

function createHtmlHead()
{
  _text+="<html>";
  _text+="<head>";

  //FOR WIDGET
  //_text+="<style type='text/css'>";
  //        _text+="td{ font-size: 10pt; font-family: Trebuchet MS, Geneva, Arial;}";
  //        _text+=".difftd  { font-size: 9pt; font-family: Trebuchet MS, Geneva, Arial; color:black;}";
  //        _text+=".redtd   { font-size: 8pt; font-family: Trebuchet MS, Geneva, Arial; color:red;}";
  //        _text+=".headingtd   { font-size: 11pt; font-family: Trebuchet MS, Geneva, Arial; color:black; font-weight: bold}";
  //        _text+=".wheadingtd   { font-size: 18pt; font-family: Trebuchet MS, Geneva, Arial; color:white; font-weight: bold}";
  //        _text+=".whitetd   { font-size: 11pt; font-family: Trebuchet MS, Geneva, Arial; color:white;}";
  //        _text+=".largetexttd   { font-size: 12pt; font-family: Trebuchet MS, Geneva, Arial; color:black;}";
  //        _text+=".largetextwhitetd   { font-size: 12pt; font-family: Trebuchet MS, Geneva, Arial; color:white;}";
  //        _text+="@media print {input.noPrint { display: none; }}";
  //_text+="</style>";

  //FOR BROWSER 
  _text+="<link rel='icon' href='favicon.ico' type='image/x-icon' /><link rel='shortcut icon' href='favicon.ico' type='image/x-icon' /><link href='http://www.object-matrix.com/style.css' type='text/css' rel='stylesheet' name='style' /></head>";

  _text+="<body>"; 
  _text+="<center>";

  _text+="<table border='0'>";
  _text+="<tr>";

  //FOR WIDGET
  //_text+="<td colspan='4' class='largetextwhitetd' align='right'>The MatrixStore</img>&nbsp;</td></tr>";
  //FOR BROWSER
  _text+="<td colspan='4' class='largetextwhitetd' align='right'><img src='http://www.object-matrix.com/thematrixstore/js/Banner.png' border='0'></img>&nbsp;</td></tr>";


  _text+="<tr bgcolor='444444'>";
  _text+="<td colspan='4' class='largetextwhitetd' align='left'>&nbsp; Shopping List to Protect "+TB+"TB of Assets &nbsp;</td></tr>";
  _text+="<tr bgcolor='999999'>";
  _text+="<td colspan='4' align='left'>&nbsp; Using Node Configuration: "+CAP+" &nbsp;</td></tr>";
  _text+="<tr bgcolor='999999'>";
  _text+="<td colspan='4' align='left'>&nbsp; Number of Nodes Required: "+nodesRequired+" &nbsp;</td></tr>";
  _text+="<tr bgcolor='cccccc'>";
  _text+="<td colspan='4' align='left'>&nbsp; Maximum Usable Capacity: "+maxTB+"TB &nbsp;</td></tr>";
 
   //FOR WIDGET
   //_text+="<tr>";
   //_text+="<td colspan='4' align='right'>&nbsp; &nbsp;</td></tr>";

}

function createHtmlBody(idCounter)
{
  var _idCounter = idCounter;
 
  _text+="<tr>";
  _text+="<td align='right' colspan='4' class='difftd'>";
  _text+="Currency: "+currToDisplay+" &nbsp;</td></tr>";
  _text+="<tr bgcolor='333333'>";
   
  _text+="<tr bgcolor='d4d4d4'><td>&nbsp;Quantity&nbsp;</td><td>&nbsp;Component&nbsp;</td><td>&nbsp;Item Price&nbsp;</td><td>&nbsp;Totals&nbsp;</td></tr>";
  
  //mxs
  _text+="<tr><td valign='top'>"+nodesRequired+"</td><td>"+mxsDesc+"</td><td valign='top'>"+currToDisplay+mxsItemCostFormatted+"</td><td align='right' valign='top'>"+currToDisplay+totalmxsHardwareFormatted+"</td></tr>";
 
  //Switches
  _text+="<tr><td valign='top'>"+switchesRequired+"</td><td><b>"+switches[0]+"</b></td><td valign='top'>"+currToDisplay+switchItemCost+"</td><td align='right' valign='top'>"+currToDisplay+totalCostSwitchesFormatted+"</td></tr>";

  _text+="<tr bgcolor='cfcfcf' height='4'><td colspan='4'> </td></tr>";
  _text+="<tr><td colspan='4' align='right'>Total : "+currToDisplay+totalHardware+"</td></tr>";
  _text+="<tr bgcolor='cfcfcf' height='4'><td colspan='4'> </td></tr>";

  getPrintButton();
  _text+="<tr height='2'><td colspan='4'> </td></tr>";
  _text+="<tr><td colspan='4' align='right'>All prices quoted are list price and exclusive of Tax/VAT/TVA </td></tr>";

  createPricingInfo();
}

function createPricingInfo()
{
  _text+="<tr height='20'><td colspan='4'> </td></tr>";
  _text+="<tr><td width='400' colspan='4' align='justify'>If you do not have two copies of your data you do not have it. If you cannot find it you do not have it. ";
  _text+="The prices quoted above covers the protection of your data by maintaining two copies of your data. Always. ";
  _text+="It also includes the ability to search for and bring back your content so you can work on it whenever you need it. ";
  _text+="Included in the cost is the hardware and all the software functionality. There are no hidden extras. ";
  _text+="No making and verifying multiple copies of tape. No messing around with LUNs, directories or Volumes. ";
  _text+="Your data, protected at the highest levels and available when and where you need it.</td></tr>";


  _text+="<tr>";

  _text+="<td colspan='2'>";
  _text+="<ul>";
  _text+="<li>Search via metadata support</li>";
  _text+="<li>Guaranteed data authenticity</li>";
  _text+="<li>Self-Healing, Self-Managing</li>";
  _text+="<li>Non-Proprietary</li>";
  _text+="<li>Automated Capacity upgrades</li>";
  _text+="</ul>";
  _text+="</td>";

  _text+="<td colspan='2'>";
  _text+="<ul>";
  _text+="<li>Offsite Replication</li>";
  _text+="<li>Multiple Tencancy</li>";
  _text+="<li>Fully Audited</li>";
  _text+="<li>Regulation Compliant</li>";
  _text+="</ul>";
  _text+="</td>";
  _text+="</tr>";

  _text+="<tr height='20'><td colspan='4'> </td></tr>";
  _text+="<tr><td width='400' colspan='4' align='right'>1TB = 1,000,000,000,000 bytes</td></tr>";
  _text+="<tr height='20'><td colspan='4'> </td></tr>";
}

function createHtmlFoot()
{
  _text+="</table>";
  _text+="</center>";
  _text+="</body></html>";
}

function getPrintButton()
{
  _text+="<tr><td colspan='4' align='right'><form><input class='noPrint' type=button value='Print Shopping List' onClick='window.print()'></form></td></tr>";
}

function buttonUpOut()
{
        document.getElementById("doButton").src = "Images/Button.png";
}

function buttonOver()
{
        document.getElementById("doButton").src = "Images/Button_down.png";
}
