var TARGET="MatrixStore Shopping List";
var WIN=null;
var SYSTEM_SUCCESS = 0;
var TB = 0;
var curr;
var gbp = "&#163;";
var usd = "$";
var euro = "&#128;";
var currToDisplay = "";
var showAllCurrencies = false;
var _text = "";

var freeElement = 0; //FREE AMOUNT OF TB

var xserve = new Array();
xserve[0] =  "Xserve with One 2.8GHz Quad-Core Intel Xeon - Part Number: Z0E7";
xserve[1] =  "Dual Channel gigabit Ethernet card with PCI Express riser 065-6864";
xserve[2] =  "Dual Channel 4Gb Fibre Channel PCI Express Card 065-6863";
xserve[3] =  "Single 750W Power Supply 065-6869";
xserve[4] =  "80GB Serial ATA ADM @ 7200 rpm 065-6859";
xserve[5] =  "2GB (2*1GB) 065-7150";

//2135.32 ex vat
xserve[6] =  2135.32; <!-- pounds
//no ex vat price given
xserve[7] =  3799.00; <!-- usd
//3025.92    ex vat
xserve[8] =  3025.92; <!-- euro

var raid = new Array();
raid[0] =  "Xserve RAID - Part Number: Z0D3";
raid[1] =  "10500GB ADM (14x750GB Ultra ATA) 065-7047";
raid[2] =  "Cache Battery Back-up Modules 065-3777";

//8602.55 ex vat
raid[3] =  8602.55; <!-- pounds
//Not ex vat price given
raid[4] = 14149.00; <!-- usd
//12941.48 ex vat
raid[5] = 12941.48; <!-- euro

var praid = new Array();
praid[0] =  "Promise VTrak E-Class";
praid[1] =  "16 * 750GB 7200-rpm SATA drives (12TB raw capacity)";
praid[2] =  "Part No: TQ810";

//8602.55 ex vat
praid[3] =  9299.00; <!-- pounds
//Not ex vat price given
praid[4] = 14999.95; <!-- usd
//12941.48 ex vat
praid[5] = 12999.00; <!-- euro

var switches = new Array();
switches[0] = "USRobotics 24port 10/100/1000 un-managed switch PN:USR997931";
switches[1] = 250.00; <!-- pounds
switches[2] = 500.00; <!-- usd
switches[3] = 400.00; <!-- euro
    
var matrixstore = new Array();
matrixstore[0] = "MatrixStore Software 1TB Usable License";
matrixstore[1] = "Includes Search, Replication, Regulation Compliance Support";
matrixstore[2] = 500.00; <!-- pounds
matrixstore[3] = 1000.00; <!-- usd
matrixstore[4] = 750.00; <!-- euro

var xserveDesc = "";
var xserveRaidDesc = "";
var mxsDesc = "";
var pRaidDesc = "";
 
var nodesRequired;
var nodesRequiredpRAID;

var licencesRequired;
var totalMxsLicenses;
var freeElementCost;
var freeElementCostFormatted;
var mxsItemCost;
var totalCostSoftware;

var xserveItemCost;
var xserveItemCostFormatted;
var totalCostXserve;
var totalCostXserveFormatted;

var xserveRAIDItemCost;
var xserveRAIDItemCostFormatted;
var totalCostXserveRAID;
var totalCostXserveRAIDFormatted;

var pRAIDItemCost;
var pRAIDItemCostFormatted;
var totalCostpRAID;
var totalCostpRAIDFormatted;

var switchesRequired = 2;
var switchItemCost;
var totalCostSwitches;
var totalCostSwitchesFormatted;

var totalAppleHardware;
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 arrays for no currency choice 
// --------------------------------------------
function setPricingNoChoice()
{
    usdPricing[0] = xserve[7];
    usdPricing[1] = raid[4];
    usdPricing[2] = switches[2];
    usdPricing[3] = matrixstore[3];
    usdPricing[4] = praid[4];

    gbpPricing[0] = xserve[6];
    gbpPricing[1] = raid[3];
    gbpPricing[2] = switches[1];
    gbpPricing[3] = matrixstore[2];
    gbpPricing[4] = praid[3];

    euroPricing[0] = xserve[8];
    euroPricing[1] = raid[5];
    euroPricing[2] = switches[3];
    euroPricing[3] = matrixstore[4];
    euroPricing[4] = praid[5];
}
 
// --------------------------------------------
//                  setPricing
// Sets pricing depending on Currency chosen
// --------------------------------------------
function setPricing(curr)
{

  if(curr=="$")
  {
    pricing[0] = xserve[7]; 
    pricing[1] = raid[4]; 
    pricing[2] = switches[2]; 
    pricing[3] = matrixstore[3]; 
    pricing[4] = praid[4]; 
  }

  if(curr=="pound")
  {
    pricing[0] = xserve[6]; 
    pricing[1] = raid[3]; 
    pricing[2] = switches[1]; 
    pricing[3] = matrixstore[2]; 
    pricing[4] = praid[3]; 
  }

 if(curr=="euro")
  {
    pricing[0] = xserve[8];
    pricing[1] = raid[5];
    pricing[2] = switches[3];
    pricing[3] = matrixstore[4];
    pricing[4] = praid[5]; 
  }
}

// ---------------------------------------------------
//                  describe
// Populates the hardware info strings from the arrays
// ---------------------------------------------------
function describe()
{
  xserveDesc = "";
  xserveRaidDesc = "";
  mxsDesc = "";
  pRaidDesc = "";

  var minusPricingInfo = 3;

    for(var x=0; x<xserve.length-minusPricingInfo; x++)
    {
        if(x==0)
        {
            xserveDesc+="<b>"+xserve[x]+"</b><br>";
        }
        else
        {
            xserveDesc+=xserve[x]+"<br>";
        }
    }


    for(var y=0; y<raid.length-minusPricingInfo; y++)
    {
        if(y==0)
        {
            xserveRaidDesc+="<b>"+raid[y]+"</b><br>";
        }
        else
        {
            xserveRaidDesc+=raid[y]+"<br>";
        }
    }
	
    for(var z=0; z<matrixstore.length-minusPricingInfo; z++)
    {
        if(z==0)
        {
            mxsDesc+="<b>"+matrixstore[z]+"</b><br>";
        }
        else
        {
            mxsDesc+=matrixstore[z]+"<br>";
        }
    }

   for(var y=0; y<praid.length-minusPricingInfo; y++)
    {
        if(y==0)
        {
            pRaidDesc+="<b>"+praid[y]+"</b><br>";
        }
        else
        {
            pRaidDesc+=praid[y]+"<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()
{
  var _tb=0;
  freeElement = -5;

  _text = "";

  showAllCurrencies = false;

  _tb = document.getElementById("no_tb").value;
  TB = _tb;

  //We do not want to subsidise the harware!
  if(_tb < freeElement)
  {
        freeElement = -_tb;
  }

  //determine number of nodes/switches required.
  var _singleNodeRaw = 10.5;
  var _singlePRAIDNodeRaw = 12;

  var _singleNodeUsable = (_singleNodeRaw*(6/7))/2;
  var _singleNodeUsablePRAID = (_singlePRAIDNodeRaw*(6/7))/2;

  nodesRequired = Math.ceil(_tb/_singleNodeUsable);
  nodesRequiredPRAID = Math.ceil(_tb/_singleNodeUsablePRAID);

  //change here later
  nodesRequired = nodesRequiredPRAID;

  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;
    }
  }

  //detemine if all currencies should be shown.
  var currfield=document.getElementById("currency");
  if (currfield == null)
  { 
   showAllCurrencies = true;
  }
  else
  {
    curr=document.getElementById("currency").value;
  }

  describe();

  //create the html header
  createHtmlHead();

  //set the pricing
  if(showAllCurrencies)
  {
      setPricingNoChoice();	
  
      for(var x=0; x<3 ; x++)
      {
           if(x==0)
           {
              pricing = usdPricing;
              currToDisplay = usd;
           }

           if(x==1)
           {
              pricing = gbpPricing;
              currToDisplay = gbp;
           }

           if(x==2)
           {
              pricing = euroPricing;
              currToDisplay = euro;
           }

           setPricingValues();
           createHtmlBody(x);
      }
  }
  else
  {
      if (curr == "pound") currToDisplay = gbp;
      if (curr == "$") currToDisplay = usd;
      if (curr == "euro") currToDisplay = euro;
      setPricing(curr);
      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 totalSoftware = 0;
   var totalHardwareU = 0;

   licencesRequired = TB*pricing[3];
   totalMxsLicenses = addCommas((TB*pricing[3]).toFixed(2));

   freeElementCost = freeElement*pricing[3];
   freeElementCostFormatted = addCommas((freeElement*pricing[3]).toFixed(2));

   mxsItemCost = addCommas(pricing[3].toFixed(2));
   totalSoftware = licencesRequired + freeElementCost;
   totalCostSoftware = addCommas(totalSoftware.toFixed(2));

   switchItemCost = addCommas(pricing[2].toFixed(2));

   xserveItemCost = addCommas(pricing[0]);
   xserveItemCostFormatted = addCommas(pricing[0].toFixed(2));
   totalCostXserve = nodesRequired*pricing[0];
   totalCostXserveFormatted = addCommas((nodesRequired*pricing[0]).toFixed(2));

   xserveRAIDItemCost = addCommas(pricing[1]);
   xserveRAIDItemCostFormatted = addCommas(pricing[1].toFixed(2));
   totalCostXserveRAID = nodesRequired*pricing[1];
   totalCostXserveRAIDFormatted= addCommas((nodesRequired*pricing[1]).toFixed(2));

   pRAIDItemCost = addCommas(pricing[4]);
   pRAIDItemCostFormatted = addCommas(pricing[4].toFixed(2));
   totalCostpRAID = nodesRequired*pricing[4];
   totalCostpRAIDFormatted= addCommas((nodesRequired*pricing[4]).toFixed(2));

   totalCostSwitches = switchesRequired*pricing[2];
   totalCostSwitchesFormatted = addCommas((switchesRequired*pricing[2]).toFixed(2));

   // RAID totalAppleHardware = (totalCostXserve+totalCostXserveRAID);
   totalAppleHardware = totalCostXserve;
   totalHardwareU = totalAppleHardware+totalCostSwitches+totalCostpRAID; 
   totalHardware = addCommas(totalHardwareU.toFixed(2));

   total = totalHardwareU + totalSoftware;
   totalFormatted = addCommas(total.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 bgcolor='222222'>";

  //FOR WIDGET
  //_text+="<td colspan='4' class='largetextwhitetd' align='right'>Object Matrix</img>&nbsp;</td></tr>";
  //FOR BROWSER
  _text+="<td colspan='4' class='largetextwhitetd' align='right'><img src='http://www.object-matrix.com/images/logo.png' border='0'></img>&nbsp;</td></tr>";

  _text+="<tr bgcolor='444444'>";
  _text+="<td colspan='4' class='largetextwhitetd' align='right'>&nbsp; Shopping List to Protect "+TB+"TB of Assets &nbsp;</td></tr>";
  _text+="<tr bgcolor='999999'>";
  _text+="<td colspan='4' align='right'>&nbsp; Number of Nodes Required: "+nodesRequired+" &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+="<td colspan='4' class='largetextwhitetd'>";
  _text+="&nbsp;Total: "+currToDisplay+totalFormatted+"</td></tr>";
   
  _text+="<tr bgcolor='A0A0A0'><td colspan='4'>&nbsp;Hardware (all pricing current list prices from apple.com)</td></tr>";
  _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>";
  
  //Xserve
  _text+="<tr><td valign='top'>"+nodesRequired+"</td><td>"+xserveDesc+"</td><td valign='top'>"+currToDisplay+xserveItemCostFormatted+"</td><td align='right' valign='top'>"+currToDisplay+totalCostXserveFormatted+"</td></tr>";
 
  //Xserve RAID
  //  _text+="<tr><td valign='top'>"+nodesRequired+"</td><td>"+xserveRaidDesc+"</td><td valign='top'>"+currToDisplay+xserveRAIDItemCostFormatted+"</td><td align='right' valign='top'>"+currToDisplay+totalCostXserveRAIDFormatted+"</td></tr>";
 
  //pRAID
  _text+="<tr><td valign='top'>"+nodesRequired+"</td><td>"+pRaidDesc+"</td><td valign='top'>"+currToDisplay+pRAIDItemCostFormatted+"</td><td align='right' valign='top'>"+currToDisplay+totalCostpRAIDFormatted+"</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 hardware: "+currToDisplay+totalHardware+"</td></tr>";
  _text+="<tr bgcolor='cfcfcf' height='4'><td colspan='4'> </td></tr>";

  _text+="<tr bgcolor='A0A0A0'><td colspan='4'>&nbsp;Software&nbsp;</td></tr>";
  _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>";
  _text+="<tr><td valign='top'>"+TB+"</td><td>"+mxsDesc+"</td><td valign='top'>"+currToDisplay+mxsItemCost+"</td><td align='right' valign='top'>"+currToDisplay+totalMxsLicenses+"</td></tr>";
  _text+="<tr><td valign='top'>1</td><td>5TB Free MatrixStore Software</td><td valign='top'>"+currToDisplay+"nada</td><td align='right' valign='top'><font color='red'>"+currToDisplay+freeElementCostFormatted+"</font></td></tr>";
  _text+="<tr bgcolor='cfcfcf' height='4'><td colspan='4'> </td></tr>";
  _text+="<tr><td colspan='4' align='right'>Total Software: "+currToDisplay+totalCostSoftware+"</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'>All prices quoted are exclusive of Tax/VAT/TVA </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";
}
