/* **************************************************** *
** **************************************************** **
** Filename:    ShopCalc.js                             **
** Author:      Kain McBride (Leprichaun)               **
** Purpose:     Cross Platform Shopping List Generator	**
**              for use by members of Vampires: The 	**
**              Dark Alleyway, created by Raven Black.	**
** Start Date:  06.10.2008                              **
** Completed:   06.11.2008                              **
** Total Lines: Javascript: 489; HTML: 46; TOTAL: 535   **
** **************************************************** **
 * **************************************************** */
var stITEM_NAMES = new Array(17);
var prices       = new Array(17);
var itemList     = new Array(17);
var shopCart     = new Array(17);
var stQUOTE = '"';
var bARRAY_SET = false;

function setARRAYS()
 {
  if (bARRAY_SET == false)
   {
    stITEM_NAMES[0] = "Diamond Ring";
    stITEM_NAMES[1] = "Black Orchid";
    stITEM_NAMES[2] = "Red Rose";
    stITEM_NAMES[3] = "Dandelion";
    stITEM_NAMES[4] = "Holy Water";
    stITEM_NAMES[5] = "Garlic Spray";
    stITEM_NAMES[6] = "Sprint Potion";
    stITEM_NAMES[7] = "Necro Blood";
    stITEM_NAMES[8] = "Scroll of Teleportation";
    stITEM_NAMES[9] = "Scroll of Displacement";
    stITEM_NAMES[10] = "Scroll of Succour";
    stITEM_NAMES[11] = "Scroll of Turning";
    stITEM_NAMES[12] = "Scroll of Accounting";
    stITEM_NAMES[13] = "Titanium - Platinum Ring";
    stITEM_NAMES[14] = "Golden Ring";
    stITEM_NAMES[15] = "Amulet of Insidious Illusions";
    stITEM_NAMES[16] = "Flask of Heinous Deceptions";
    stITEM_NAMES[17] = "Necklace With Locket";
    prices[0] = 100000; 
    prices[1] = 1000; 
    prices[2] = 500; 
    prices[3] = 50; 
    prices[4] = 2000; 
    prices[5] = 1000; 
    prices[6] = 150; 
    prices[7] = 25; 
    prices[8] = 5000; 
    prices[9] = 1000; 
    prices[10] = 750; 
    prices[11] = 500; 
    prices[12] = 5000; 
    prices[13] = 100000; 
    prices[14] = 90000; 
    prices[15] = 80000; 
    prices[16] = 70000; 
    prices[17] = 50000;
    shopCart[0] = 0;
    shopCart[1] = 0;
    shopCart[2] = 0;
    shopCart[3] = 0;
    shopCart[4] = 0;
    shopCart[5] = 0;
    shopCart[6] = 0;
    shopCart[7] = 0;
    shopCart[8] = 0;
    shopCart[9] = 0;
    shopCart[10] = 0;
    shopCart[11] = 0;
    shopCart[12] = 0;
    shopCart[13] = 0;
    shopCart[14] = 0;
    shopCart[15] = 0;
    shopCart[16] = 0;
    shopCart[17] = 0;
    bARRAY_SET = true;
   }
 }

function setSHOP()
 {
  setARRAYS();
  var iSHOP = document.ShoppingListGen_HEAD.lstSHOP.selectedIndex;
  switch (iSHOP)
   {
    case 0: 
    case 3: 
    case 4:
    case 6:
    case 7:
    case 8: /* Magic  Shops */
     setMAGIC();
     break;
    case 1:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13: /* Potion Shops */
     setPOTION();
     break;
    case 2: 
    case 14: 
    case 15: 
    case 16: 
    case 17: 
    case 18: 
    case 19: 
    case 20: /*    Scroll Shop    */
     setSCROLL();
     break;
    case 21:
    case 22:
    case 23:
    case 24: /*     Pawn Shop     */
     setPAWN();
     break;
    case 5:  /* Silver Apothecary */
     setSA();
     break;
    case 25: /*     Gift Shop     */
     setCoS();
     break;
   }
  updateLIST();
  document.getElementById('lstITEMS').selectedIndex=0;
  getDESCRIPTION(getSelectedItem());
 }

function setMAGIC()
 {
  itemList[0]  = 1; 
  itemList[1]  = 1; 
  itemList[2]  = 1; 
  itemList[3]  = 1; 
  itemList[4]  = 1; 
  itemList[5]  = 1; 
  itemList[6]  = 1; 
  itemList[7]  = 0; 
  itemList[8]  = 1; 
  itemList[9]  = 1; 
  itemList[10] = 1; 
  itemList[11] = 1; 
  itemList[12] = 1; 
  itemList[13] = 0; 
  itemList[14] = 0; 
  itemList[15] = 0; 
  itemList[16] = 0; 
  itemList[17] = 0; 
 }

function setPOTION()
 {
  setMAGIC();
  itemList[0]  = 0; 
  itemList[1]  = 0; 
  itemList[2]  = 0; 
  itemList[3]  = 0; 
  itemList[7]  = 1; 
  itemList[8]  = 0; 
  itemList[9]  = 0; 
  itemList[10] = 0; 
  itemList[11] = 0; 
  itemList[12] = 0; 
 }

function setSCROLL()
 {
  setMAGIC();
  itemList[0]  = 0; 
  itemList[1]  = 0; 
  itemList[2]  = 0; 
  itemList[3]  = 0; 
  itemList[4]  = 0; 
  itemList[5]  = 0; 
  itemList[6]  = 0; 
  itemList[7]  = 0; 
 }

function setSA()
 {
  setPOTION();
  itemList[0]  = 1; 
  itemList[1]  = 1; 
  itemList[2]  = 1; 
  itemList[3]  = 1; 
 }

function setCoS()
 {
  itemList[0]  = 1; 
  itemList[1]  = 1; 
  itemList[2]  = 1; 
  itemList[3]  = 1; 
  itemList[4]  = 0; 
  itemList[5]  = 0; 
  itemList[6]  = 0; 
  itemList[7]  = 0; 
  itemList[8]  = 0; 
  itemList[9]  = 0; 
  itemList[10] = 0; 
  itemList[11] = 0; 
  itemList[12] = 0; 
  itemList[13] = 1; 
  itemList[14] = 1; 
  itemList[15] = 1; 
  itemList[16] = 1; 
  itemList[17] = 1; 
 }

function setPAWN()
 {
  setMAGIC();
  itemList[7]  = 0; 
  itemList[13] = 1; 
  itemList[14] = 1; 
  itemList[15] = 1; 
  itemList[16] = 1; 
  itemList[17] = 1; 
 }

function getDESCRIPTION(iDATA)
 {
  var stRETURN = "<TEXTAREA ROWS=3 class=" + stQUOTE + "taLIAM2" + stQUOTE + " STYLE=" + stQUOTE + "WIDTH:100%" + stQUOTE + ">";
  switch (parseInt(iDATA))
   {
    case 0:
     stRETURN = stRETURN + "A flawless diamond is set amungst a number of smaller diamonds, light reflecting through the facets into sparkling rainbows. The arangement itself is set into a simple unassuming gold band. (gift item)";
     break;
    case 1:
     stRETURN = stRETURN + "A deep black orchid, flawless, with a beautiful soft purple stamen. (gift item)";
     break;
    case 2:
     stRETURN = stRETURN + "A beautiful long-stemmed red rose, fresh and undamaged. (gift item)";
     break;
    case 3:
     stRETURN = stRETURN + "A dandelion in good condition, bright clean yellows and whites. (gift item)";
     break;
    case 4:
     stRETURN = stRETURN + "The Weapon of choice against strong vampires, but following the saying 'the bigger they are, the harder they fall!' this item doesn't do a lot of damage to weaker vampires.";
     break;
    case 5:
     stRETURN = stRETURN + "The complete opposite of holywater, this weapon is strong against weaker vampires. Though it will do the same damage to stronger pires, holy water is more effective against them. Garlic spray deals 25 to 50 damage to a target, and a vampire with a high level of shadows can throw 2 at once dealing 50 to 100 damage.";
     break;
    case 6:
     stRETURN = stRETURN + "In a hurry? This sprint potion may be right for you! Use this potion while facing the direction of choice, it will cause you to sprint 5 times further than you would normally walk. Teleports are still less expencive if you intend to cross the entire city, but how often do you do that? Public transit is of course the most cost effective method of transportation, however, it can't be used with certain quests.";
     break;
    case 7:
     stRETURN = stRETURN + "Come in... the necromancer shall see you now... battled blood shall be restored, but at what cost? In this realm, you are fortunate indeed that the necromancer seeks only wealth. 25 coin per pint, but he will not see you until you've been attacked by another vampire.";
     break;
    case 8:
     stRETURN = stRETURN + "The scroll of teleportation makes the shortest distance between two places, 1 ap. An expencive method of transpotation, but sometimes neccessary. It is advisable to have at least a few scrolls of teleportation on hand when you start a celerity quest.";
     break;
    case 9:
     stRETURN = stRETURN + "Feeling lucky? With this item you can displace yourself to a random location on the map. Unlike the teleport though, you can use this on other vampires! This is good to get rid of enemies in a hurry, as well as move allies out of harms way. Quest wise, it can be useful on celerity quests if you have to move from one extreem end of the city to another.";
     break;
    case 10:
     stRETURN = stRETURN + "The scroll of succour is a nice item to have in any cost efficient vampire's inventory. This allows you to transport directly to your companion or sire, and costs significantly less than the similar scroll of teleportation.";
     break;
    case 11:
     stRETURN = stRETURN + "'Out of my way!' never had so much power! This item will clear the vampires from an adjacent square. It also deals 10 damage to those pires, unless they are neutral, or of a different colour than yourself. (ie: a smurf when you're not)";
     break;
    case 12:
     stRETURN = stRETURN + "This scroll allows the holder to check their bank account status at any time, without travelling to the bank.";
     break;
    case 13:
     stRETURN = stRETURN + "Platinum is the hardest and the most valuable of the precious metals. Its intense lustre remains intact over the years. This unique ring measures 7mm in width and has combined Platinum inlays with aircraft grade Titanium and Tungsten Carbide.";
     break;
    case 14:
     stRETURN = stRETURN + "A 6mm wide 24k gold ring with multiple millgrain grooves and frosted finish centre.";
     break;
    case 15:
     stRETURN = stRETURN + "A replica of a very ornate heirloom made out of mysterious metals and gems, forged and crafted by those who dwell within the Cloister of Secrets. And it <i>is</i> just a replica.";
     break;
    case 16:
     stRETURN = stRETURN + "A replica of an ancient artifact. This replica is made out of stainless steel with high-polish finish. Like the original, it has kanji-like sigils carved all over it. It is rumoured that the original contained an elixir of sorts. The replica is empty.";
     break;
    case 17:
     stRETURN = stRETURN + "A gold chain necklace with a heart-shaped locket.";
     break;
   }
  stRETURN = stRETURN + "</TEXTAREA>";
  document.getElementById('tdDESCRIPTION').innerHTML = stRETURN;
 }

function getSHOP_ITEMS()
 {
  var stHTML = "";
  stHTML = stHTML + "<select id=" + stQUOTE + "lstITEMS" + stQUOTE + " class=" + stQUOTE + "selLIAM2" + stQUOTE + " size=" + stQUOTE + "10" + stQUOTE + " style=" + stQUOTE + "width:100%" + stQUOTE + " OnChange=" + stQUOTE + "itemSELECT();" + stQUOTE + ">";
  stHTML = stHTML + getSHOP_ITEMS_OPTIONS();
  stHTML = stHTML + "</select>";
  document.getElementById("tdITEMS").innerHTML = stHTML;
 }
function getSHOP_ITEMS_OPTIONS()
 {
  /* this function assumes the presence of the array itemList(x); this array is made up of values of either 0 or 1, 0 = not available, 1 = available...
     this functon also assumes the presence of the array stITEM_NAMES(x); this array is self explanitory */
  var stRETURN = "";
  for (var x = 0; x <=17; x++)
   {
    if (itemList[x] == 1)
     { 
      stRETURN = stRETURN + "<OPTION VALUE=" + x.toString() + ">" + stITEM_NAMES[x] + "</option>";
     }
   }
  return stRETURN;
 }
function getSHOP_CART()
 {
  var stHTML = "";
  stHTML = stHTML + "<select id=" + stQUOTE + "lstCART" + stQUOTE + " class=" + stQUOTE + "selLIAM2" + stQUOTE + " size=" + stQUOTE + "10" + stQUOTE + " style=" + stQUOTE + "width:100%" + stQUOTE + ">";
  stHTML = stHTML + getSHOP_CART_OPTIONS();
  stHTML = stHTML + "</select>";
  document.getElementById("tdCART").innerHTML = stHTML;
  getCART_TOTAL();
 }
function getSHOP_CART_OPTIONS()
 {
  /* this function assumes the same as getSHOP_ITEMS_OPTIONS()
     also assumes the existance of shopCart(x); */
  var stRETURN = "";
  for (var x = 0; x <=17; x++)
   {
    if ((itemList[x] == 1) && (shopCart[x] >= 1) )
     {
      stRETURN = stRETURN + "<OPTION VALUE=" + stQUOTE + x.toString() + stQUOTE + ">" + stITEM_NAMES[x] + " (" + shopCart[x].toString() + ")</option>";
     }
   }
  return stRETURN;
 }

function updateLIST()
 {
  getSHOP_ITEMS();
  getSHOP_CART();
  getCART_TOTAL();
 }

function itemSELECT()
 {
  var iITEM = document.getElementById('lstITEMS');
  getDESCRIPTION(iITEM.options[iITEM.selectedIndex].value);
 }

function getSelectedItem()
 {
  return document.getElementById('lstITEMS').options[document.getElementById('lstITEMS').selectedIndex].value;
 }

function getSelectedCart()
 {
  if (document.getElementById('lstCART').selectedIndex >=0)
   {
    return document.getElementById('lstCART').options[document.getElementById('lstCART').selectedIndex].value;
   }
 }

function cmdADD_ITEM()
 {
  shopCart[getSelectedItem()] = shopCart[getSelectedItem()] + 1;
  getSHOP_CART();
 }

function cmdADD_X_ITEMS()
 {
  shopCart[getSelectedItem()] = shopCart[getSelectedItem()] + parseInt(prompt('Add how many ' + stITEM_NAMES[getSelectedItem()] + 's?', 10));
  getSHOP_CART();
 }

function cmdREMOVE_ITEM()
 {
  shopCart[getSelectedCart()] = shopCart[getSelectedCart()] - 1;
  if (shopCart[getSelectedCart()] < 0)
   { shopCart[getSelectedCart()] = 0; }
  getSHOP_CART();
 }

function cmdREMOVE_X_ITEMS()
 {
  if (document.getElementById('lstCART').selectedIndex >=0)
   {
    shopCart[getSelectedCart()] = shopCart[getSelectedCart()] - prompt('Remove how many ' + stITEM_NAMES[getSelectedCart()] + 's?', 10);
    if (shopCart[getSelectedCart()] < 0)
     { shopCart[getSelectedCart()] = 0; }
   }
  getSHOP_CART();
 }

function cmdREMOVE_ALL()
 {
  shopCart[getSelectedCart()] = 0;
  getSHOP_CART();
 }

function cmdCLEAR_LIST()
 {
  shopCart[0] = 0;
  shopCart[1] = 0;
  shopCart[2] = 0;
  shopCart[3] = 0;
  shopCart[4] = 0;
  shopCart[5] = 0;
  shopCart[6] = 0;
  shopCart[7] = 0;
  shopCart[8] = 0;
  shopCart[9] = 0;
  shopCart[10] = 0;
  shopCart[11] = 0;
  shopCart[12] = 0;
  shopCart[13] = 0;
  shopCart[14] = 0;
  shopCart[15] = 0;
  shopCart[16] = 0;
  shopCart[17] = 0;
  getSHOP_CART();
 }

function getCartName()
 {
  return stITEM_NAMES[getSelectedCart()];
 }

function getCART_TOTAL()
 {
  var myTOTAL = 0;
  for (var x = 0; x<=17; x++)
   {
    if ((itemList[x] == 1) && (shopCart[x] >= 1) )
     {
      myTOTAL = myTOTAL + (getPRICE(x) * shopCart[x]);
     }
   }
  /* set the total */
  document.getElementById('lblTOTAL').innerHTML = FORMAT_COIN(myTOTAL);
 }

function getPRICE(testItem)
 {
  var priceReturn = 0;
  priceReturn = getCHARISMA(Math.round((prices[testItem] * (getSHOP_MATH() / 100))))
  if (prices[testItem]==25)
   { priceReturn = 25; }
  return priceReturn;
 }

function getCHARISMA(price)
 {
  var chPrice=0;
  var charisma = document.ShoppingListGen_HEAD.lvlCHARISMA.selectedIndex;
  if (charisma==0)
   { chPrice = price; }
  if (charisma==1)
   { chPrice = Math.round(price*0.97); }
  if (charisma==2)
   { chPrice = Math.round(price*0.93); }
  if (charisma==3)
   { chPrice = (Math.round(price*0.9)) - 1; }
  if (parseInt(getSHOP_MATH) == 50)
   { chPrice = price; }
  if (parseInt(getSHOP_MATH) == 110)
   { chPrice = price; }
  if (price==25)
   { chPrice = 25; }
  return chPrice;
 }

function getSHOP_MATH()
 {
  // gets shop information from the select box
  var iSHOP = document.ShoppingListGen_HEAD.lstSHOP.selectedIndex;

  var shopMath = 0;
  if (iSHOP==25)
   { shopMath = 110; }
  if (iSHOP <= 24)
   { shopMath = 50; }
  if (iSHOP <= 20)
   { shopMath = 100; }
  if (iSHOP <=2)
   { shopMath=70; }
  return shopMath;
 }

function FORMAT_COIN(iVALUE)
 {
  /* ******************************** **
  ** Going to need a fair bit of code **
  ** to make this work, will probably **
  ** take a lot of attempts before it **
  ** pans out the way i want it to... **
  ** keep at it, get 'er done...      **
  ** ******************************** */
  var stREVERSE = "";
  var stNUMBERS = iVALUE.toString();
  if (stNUMBERS.length > 3)
   {
    // reverse the number... just for kicks..?
    for (var x = 0; x<=stNUMBERS.length -1; x++)
     {
      stREVERSE = stNUMBERS.charAt(x) + stREVERSE;
     }
    // turn em around again :p - why not?
    stNUMBERS = "";
    for (var x = 0; x<=stREVERSE.length -1; x++)
     {
      if (x/3 == Math.round(x/3) && x > 0)
       {
        stNUMBERS = "," + stNUMBERS;
       }
      stNUMBERS = stREVERSE.charAt(x) + stNUMBERS;
     }
   }
  return stNUMBERS + " Coin";
 }
