// Script begin
var INVALID = -1;

var PRICE_SALE = "PRICE_SALE";
var SHIP_SALE = "SHIP_SALE";
var NO_SALE = "NO_SALE";
var sale_type = SHIP_SALE;

var LOCATION_MENU = "LOCATION_MENU";
var locationSelected = 0;
var locations = new Array( 4 );
init2DArray( locations, 2 );
locations[ 0 ][ 0 ] = "CDN_ON";
locations[ 0 ][ 1 ] = "Canada: Ontario";
locations[ 1 ][ 0 ] = "CDN_NON";
locations[ 1 ][ 1 ] = "Canada: Others";
locations[ 2 ][ 0 ] = "USA";
locations[ 2 ][ 1 ] = "USA";
locations[ 3 ][ 0 ] = "OVERSEAS";
locations[ 3 ][ 1 ] = "Overseas";


var QUANTITY_MENU = "QUANTITY_MENU";
var quantitySelected = 0;
var quantities = new Array( 2 );
init2DArray( quantities, 2 );
quantities[ 0 ][ 0 ] = 1;
quantities[ 0 ][ 1 ] = "One";
quantities[ 1 ][ 0 ] = 2;
quantities[ 1 ][ 1 ] = "Two";

var taxes = new Array( locations.length );
taxes[ 0 ] = 13;
taxes[ 1 ] = 5;
taxes[ 2 ] = 0;
taxes[ 3 ] = 0;

var ship = new Array( locations.length );
// promotion - set the shipping to $0.00 in compute()
init2DArray( ship, quantities.length );
ship[ 0 ][ 0 ] = 8.32;
ship[ 0 ][ 1 ] = 10.15;
ship[ 1 ][ 0 ] = 11.48;
ship[ 1 ][ 1 ] = 13.54;
ship[ 2 ][ 0 ] = 14.25;
ship[ 2 ][ 1 ] = 17.54;
ship[ 3 ][ 0 ] = 20.00;
ship[ 3 ][ 1 ] = 25.00;

var price = 39.95;
var discount = 0.00;

var cost;
var tax_percent;
var tax_amount;
var shipping;
var no_ship_total;
var total;

var valueStyle = "width: 60px; background-color: #fff; font-weight: bold; text-align: right; color: #000;";
var totalValueStyle = "width: 60px; font-weight: bold; text-align: right; color: #5f311d; background-color: #fff; font-weight: bold;"
var selectStyle = "width: 150px; background-color: #fff; color: #000;";
var submitStyle = "margin: 8px auto; padding: 8px 0px; width: 80%; background-color: #f90;";

function price_sale_compute( )
{
  discount = 10.00  * quantities[ quantitySelected ][ 0 ];
  cost = price * quantities[ quantitySelected ][ 0 ];
  cost = cost - discount;
  tax_percent = taxes[ locationSelected ];
  tax_amount = Math.round( cost * tax_percent ) / 100;
  shipping = ship[ locationSelected ][ quantitySelected ];
  no_ship_total = Math.round( ( cost + tax_amount ) * 100 ) / 100;
  total = Math.round( ( no_ship_total + shipping ) * 100 ) / 100;
//  alert( 'PRICE_SALE cost: ' + cost + 'tax_percent: ' + tax_percent + 'tax_amount: ' + tax_amount + 'shipping: ' + shipping + 'discount: ' + discount + 'no_ship_total: ' + no_ship_total + 'total: ' + total );
}

function ship_sale_compute( )
{
  cost = price * quantities[ quantitySelected ][ 0 ];
  tax_percent = taxes[ locationSelected ];
  tax_amount = Math.round( cost * tax_percent ) / 100;
  shipping = ship[ locationSelected ][ quantitySelected ];
  discount = shipping;
  no_ship_total = Math.round( ( cost + tax_amount ) * 100 ) / 100;
  total = Math.round( ( no_ship_total + shipping - discount ) * 100 ) / 100;
//  alert( 'SHIP_SALE cost: ' + cost + 'tax_percent: ' + tax_percent + 'tax_amount: ' + tax_amount + 'shipping: ' + shipping + 'discount: ' + discount + 'no_ship_total: ' + no_ship_total + 'total: ' + total );
}

function no_sale_compute( )
{
  cost = price * quantities[ quantitySelected ][ 0 ];
  tax_percent = taxes[ locationSelected ];
  tax_amount = Math.round( cost * tax_percent ) / 100;
  shipping = ship[ locationSelected ][ quantitySelected ];
  no_ship_total = Math.round( ( cost + tax_amount ) * 100 ) / 100;
  total = Math.round( ( no_ship_total + shipping ) * 100 ) / 100;
//  alert( 'NO SALE cost: ' + cost + 'tax_percent: ' + tax_percent + 'tax_amount: ' + tax_amount + 'shipping: ' + shipping + 'discount: ' + discount + 'no_ship_total: ' + no_ship_total + 'total: ' + total );
}

function compute( )
{
  if( sale_type == PRICE_SALE ){
    price_sale_compute( );
  }
  else if( sale_type == SHIP_SALE ){
    ship_sale_compute( );
  }
  else{
    no_sale_compute( );
  }
//  alert( 'compute cost: ' + cost + 'tax_percent: ' + tax_percent + 'tax_amount: ' + tax_amount + 'shipping: ' + shipping + 'discount: ' + discount + 'no_ship_total: ' + no_ship_total + 'total: ' + total );
}

function update( )
{
  // variables
    var flocationMenu = document.orderForm.LOCATION_MENU;
    var fquantityMenu = document.orderForm.QUANTITY_MENU;
    var fprice = document.orderForm.price;
    var ftax_percent = document.orderForm.tax_percent;
    var ftax_amount = document.orderForm.tax_amount;
    var fshipping = document.orderForm.shipping;
    var ftotal = document.orderForm.total;
    var famount = document.orderForm.amount;
//    var fdiscount = document.orderForm.discount;
    var index;

  // location retrieval
    index = getSelectedIndex( flocationMenu, locations );
    if( index == INVALID ){
      return;
    }
    locationSelected = index;

  // quantity retrieval
    index = getSelectedIndex( fquantityMenu, quantities );
    if( index == INVALID ){
      return;
    }
    quantitySelected = index;

  // compute refresh data
    compute( );

  // refresh data
    ftax_percent.value = tax_percent + "%";
    ftax_amount.value = tax_amount;
    fshipping.value = 0; //shipping;
    famount.value = no_ship_total;
    ftotal.value = total;
//    fdiscount.value = discount;
//  alert( 'update ftax_percent.value: ' + ftax_percent.value + 'ftax_amount.value: ' + ftax_amount.value + 'fshipping.value: ' + fshipping.value + 'famount.value: ' + famount.value + 'fdiscount.value: ' );
}

function getSelectedIndex( menu, array )
{
  var index = INVALID;
  value = menu.options[ menu.selectedIndex ].value;
  for( i = 0; i < array.length; i++ ){
    if( value == array[ i ][ 0 ] ){
      index = i;
    }
  }
  return index;
}

function init2DArray( array, inner )
{
  for( i = 0; i < array.length; i++ ){
    array[ i ] = new Array( inner );
  }
}

function valueCode( name )
{
  var value;
  var classname;
  var style;
  if( name == "price" ){
    value = price;
    style = valueStyle;
  }
  else if( name == "discount" ){
    value = discount;
    style = valueStyle;
  }
  else if( name == "tax_amount" ){
    value = tax_amount;
    style = valueStyle;
  }
  else if( name == "shipping" ){
    value = shipping;
    style = valueStyle;
  }
  else if( name == "total" ){
    value = total;
    style = totalValueStyle;
  }
  else{
    alert( "Invalid name: " + name );
  }
  var str = '$<input readonly type="text" name="' + name + '"';
  str = str + 'style="' + style + '" value="' + value + '" /> Cdn';
  writeLine( str );
}

function taxPercentCode( )
{
  var str = '<input readonly type="text" name="tax_percent" style="' + valueStyle + '" ';
  str = str + 'value="' + taxes[ locationSelected ] + '%" />';
  writeLine( str );
}

function menuCode( menu_name )
{
  var array;
  if( menu_name == LOCATION_MENU ){
    name = "locationMenu";
    array = locations;
  }
  else if( menu_name == QUANTITY_MENU ){
    name = "quantityMenu";
    array = quantities;
  }
  else{
    return;
  }
  var indent = "                        ";
  var str = indent + '<select style="' + selectStyle + '" name="' + menu_name + '" ';
  str = str + 'onChange="update( )">';
  writeLine( str );
  for( i = 0; i < array.length; i++ ){
    str = indent + '  <option value="' + array[ i ][ 0 ] + '"';
    if( i == 0 ){
      str = str + 'selected';
    }
    str = str + '>' + array[ i ][ 1 ] + '</option>';
    writeLine( str );
  }
  str = indent + '</select>';
  writeLine( str );
}

function writeLine( string )
{
  this.document.writeln( string );
}

function orderFormCode( )
{
  var name = "email";
  var site = "stoneplay.com";
  var business = name + "@" + site;
  writeLine( '  <input type="hidden" name="cmd" value="_xclick">' );
  writeLine( '  <input type="hidden" name="business" value="' + business + '">' );
  writeLine( '  <input type="hidden" name="item_name" value="Terakh Boardgame">' );
  writeLine( '  <input type="hidden" name="item_number" value="SPBG0001ON">' );
  writeLine( '  <input type="hidden" name="amount" value="">' );
  writeLine( '  <input type="hidden" name="no_note" value="1">' );
  writeLine( '  <input type="hidden" name="currency_code" value="CAD">' );
  writeLine( '  <input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but23.gif" border="0" name="submit" alt="Buy Now">' );
}