function hexToDec(f1) {
  return parseInt(f1.toUpperCase(), 16);
}
//
//
//
function decToHex(i) {
  a2 = ''
  ihex = hexQuot(i);
  idiff = eval(i + '-(' + ihex + '*16)')
  a2 = itohex(idiff) + a2;
  while( ihex >= 16) {
    itmp = hexQuot(ihex);
    idiff = eval(ihex + '-(' + itmp + '*16)');
    a2 = itohex(idiff) + a2;
    ihex = itmp;
  } 
  a1 = itohex(ihex);
  return a1 + a2 ;
}
//
//
//
function hexQuot(i) {
  return Math.floor(eval(i +'/16'));
}
//
//
//
function itohex(i) {
  if( i == 0) {
     aa = '0' }
  else { if( i== 1) {
           aa = '1'}
    else {if( i== 2) {
            aa = '2'}
      else {if( i == 3) {
              aa = '3' }
        else {if( i== 4) {
                aa = '4'}
          else {if( i == 5) {
                  aa = '5' }
            else {if( i== 6) {
                    aa = '6'}
              else {if( i == 7) {
                      aa = '7' }
                else {if( i== 8) {
                        aa = '8'}
                  else {if( i == 9) {
                           aa = '9'}
                    else {if( i==10) {
                            aa = 'A'}
                      else {if( i==11) {
                              aa = 'B'}
                        else {if( i==12) {
                                aa = 'C'}
                          else {if( i==13) {
                                  aa = 'D'}
                            else {if( i==14) {
                                    aa = 'E'}
                              else {if( i==15) {
                                       aa = 'F'}

                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  return aa
}

var a = new Array();
var order_total;
var order_in_total; // to be passed as form's field
var f;
var total = 0;

//
// Init for fast execute
//
function Init() {

  var e = new Array('4 fl.oz.', 'Pint', 'Quart', 'Gallon', '5 Gallon')
  var g = new Array('4 fluid ounces', 'Pint - 16 fl. oz.', 'Quart - 32 fl. oz.', 'Gallon - 128 fl. oz.', '5 Gallon Pail')

  order_total = document.getElementById("OrderTotal");
  order_in_total = document.getElementById("OrderInTotal");

  f = document.forms["ONLINE_ORDER"]

  for(i = 0; i - price.length < 0; i ++) {

    b = price[i]
    c = b.split(",")

    if (c.length < 5) continue

    var d = new Array(6);

    d[0] = c[0]; // 107

    d[6] = document.getElementById("Size_" + c[0]);
    d[7] = document.getElementById("Price" + c[0]);
    d[8] = document.getElementById("Total" + c[0]);
    d[9] = document.getElementById("Quantity_" + c[0]);
    d[10] = document.getElementById("Name_" + c[0]);
    d[11] = c[1];

    if (d[6]) {

      var k = 1;

      for (j = 0; j < 5; j++) {

        if (c[j + 4]) {

          d[k] = c[j + 4];

          k++;
        }
        else {
          //
          // if no price - let's delete appropriate <option> in select list
          //
          for (m = 1; m < (d[6]).options.length; m++) {

            if ((d[6]).options[m].value == e[j]) (d[6]).options[m] = null
          }
        }
        //d[1] = c[4];
        //d[2] = c[5];
        //d[3] = c[6];
        //d[4] = c[7];
        //d[5] = c[8];
      }

    }

    a[a.length] = d; // array of arrays
  }

}

//
// heart of script
//
function Recalcualate() {

  total = 0

  for(i = 0; i < a.length; i++) {

    e = a[i][6];

    q = a[i][7]
    p = a[i][8]
    n = a[i][10]

    if (e.selectedIndex == -1 || e.selectedIndex == 0) {
      if (q) q.innerHTML = "&nbsp;"
      if (p) p.innerHTML = "&nbsp;"
      if (n) n.value = ""
    }
    else {
      //
      // Let's show price
      //
      if (n) n.value = a[i][11]

      v = a[i][9].value;
      w = a[i][e.selectedIndex]

      if (v && isNumeric(v)) {

        pr = v * w

        //alert(v + "\n" + pr + "\n" + a[i][3 + e.selectedIndex] + "\n" + e.selectedIndex)

        if (p) {

          if (pr)
            p.innerHTML = "&nbsp;$" + cent(pr)
          else
            p.innerHTML = "&nbsp;"
        }

        total += pr;
      }
      //else {
      //  alert(w + "\n" + e.selectedIndex)
      //}

      if (q) q.innerHTML = "&nbsp;" + w
    }

  }

  if (order_total) order_total.innerHTML = "&nbsp;<b>$" + cent(total) + "</b>"

  if (order_in_total) order_in_total.value = "$" + cent(total)

}

function cent(amount) {
  // returns the amount in the .99 format 
  amount -= 0;
  amount = (Math.round(amount*100))/100;
  return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}


var numStr = "0123456789";

//
// check - is parameter 'data' numeric or not
//
function isNumeric(data) {

  var thisChar;
  var counter = 0;

  for (j = 0; j < data.length; j++) {

    thisChar = data.substring(j, j + 1);

    if (numStr.indexOf(thisChar) != -1) counter++
    else break;
  }

  if (counter == data.length) return true
  else return false
}

//
// Validate & Submit
//
function ValidateAndSubmit() {

  er = ""

  if (!f.billingcontactname.value) er += "Please, input Contact Name" +"\n"
  if (!f.billingphonenumber.value) er += "Please, input Phone" +"\n"
  if (!f.billingemail.value) er += "Please, input Email" +"\n"

  if (!f.billingaddressline1.value) er += "Please, input Address Line 1" +"\n"
  if (!f.billingcity.value) er += "Please, input City" +"\n"
  if (!f.billingstate.value) er += "Please, input State" +"\n"
  if (!f.billingzip.value) er += "Please, input Zip" +"\n"

  if (total - 20 < 0) er += "Please, $20.00 minimum" +"\n"

  if (er) {
    alert(er)
  }
  else {
    f.submit()
  }
}
