// -------------------------------------------------------------------------
//
// Prepares list of plans and prices
//
// -------------------------------------------------------------------------
function price_list()
{
  var prices = new Array();

  var index = 0;

  //                              plan     Assets  Partners  Purchase Spread   FC Office FC Web    Pr1   Pr2   Pr3
  //                               0         1         2         3       4         5        6       7     8     9
  //                              ----     ------  --------  -------- ------   --------- ------    ---   ---   ---
  prices[index++]   = new Array("2510",      25      , 10     , 3500   , 275     , 135    ,  80   , 30 ,  2  ,  1 );
  prices[index++]   = new Array("2515",      25      , 15     , 3500   , 275     , 160    , 160   , 30 ,  3  ,  1 );
  prices[index++]   = new Array("2525",      25      , 25     , 3500   , 275     , 175    , 175   , 30 ,  4  ,  1 );
  prices[index++]   = new Array("2550",      25      , 50     , 3500   , 275     , 205    , 205   , 30 ,  5  ,  1 );
  prices[index++]   = new Array("25100",     25      , 100    , 3500   , 275     , 280    , 280   , 30 ,  6  ,  1 );
  prices[index++]   = new Array("25200",     25      , 200    , 3500   , 275     , 510    , 510   , 30 ,  7  ,  1 );

  prices[index++]   = new Array("10025",     100     , 25     , 4500   , 330     , 160    , 160   , 31 ,  8  ,  1 );
  prices[index++]   = new Array("10050",     100     , 50     , 4500   , 330     , 175    , 175   , 31 ,  9  ,  1 );
  prices[index++]   = new Array("100100",    100     , 100    , 4500   , 330     , 280    , 280   , 31 ,  10 ,  1 );
  prices[index++]   = new Array("100200",    100     , 200    , 4500   , 330     , 510    , 510   , 31 ,  11 ,  1 );
  prices[index++]   = new Array("100300",    100     , 300    , 4500   , 330     , 665    , 665   , 31 ,  12 ,  1 );
  prices[index++]   = new Array("100400",    100     , 400    , 4500   , 330     , 1000   , 1000  , 31 ,  13 ,  1 );

  prices[index++]   = new Array("20050",     200     , 50     , 6500   , 480     , 250    , 250   , 32 ,  14 ,  1 );
  prices[index++]   = new Array("200100",    200     , 100    , 6500   , 480     , 375    , 375   , 32 ,  15 ,  1 );
  prices[index++]   = new Array("200200",    200     , 200    , 6500   , 480     , 725    , 725   , 32 ,  16 ,  1 );
  prices[index++]   = new Array("200300",    200     , 300    , 6500   , 480     , 1000   , 1000  , 32 ,  17 ,  1 );
  prices[index++]   = new Array("200500",    200     , 500    , 6500   , 480     , 1375   , 1375  , 32 ,  18 ,  1 );
  
  prices[index++]   = new Array("50050",     500     , 50     , 10500  , 810     , 300    , 300   , 1  ,  1  ,  1 );
  prices[index++]   = new Array("500100",    500     , 100    , 10500  , 810     , 500    , 500   , 1  ,  1  ,  1 );
  prices[index++]   = new Array("500200",    500     , 200    , 10500  , 810     , 1000   , 1000  , 1  ,  1  ,  1 );
  prices[index++]   = new Array("500300",    500     , 300    , 10500  , 810     , 1375   , 1375  , 1  ,  1  ,  1 );
  prices[index++]   = new Array("500301",    500     , 100000 , 10500  , 810     , 2000   , 2000  , 1  ,  1  ,  1 );

  return prices;
}




// -------------------------------------------------------------------------
//
// Invokes when user wants to know which plan suits his needs
//
// -------------------------------------------------------------------------
function calculate_price()
{
  if ((document.quick_calc.memnum.value=="") || 
      (document.quick_calc.memnum.value == ""))
  {
    return;
  }

  var memnum = document.quick_calc.memnum.value;
  var totval = document.quick_calc.totval.value;

  var prices = price_list();
  var index = prices.length;

  var lastFound = index - 1;
  for(i = index; i > 0; i--)
  {
    if ((prices[i - 1][1] >= memnum) &&
        (prices[i - 1][2] >= totval))
        {
          lastFound = i - 1;
        }
  }

  document.calc_values.plan_no.value=prices[lastFound][0];
  document.calc_values.one_time.value=prices[lastFound][3];
//  document.calc_values.spread.value=prices[lastFound][4];
  document.calc_values.quarter_fee_office.value=prices[lastFound][5];
  document.calc_values.quarter_fee_web.value=prices[lastFound][6];

  // populate plan # field in the purchase form
  document.purchase_new.Plan.value=prices[lastFound][0];
}




// -------------------------------------------------------------------------
//
// Invokes when new customer enters plan # and click "Check out"
//
// -------------------------------------------------------------------------
function purchase_new_submit()
{
  var trace = false;

  var plan = document.purchase_new.Plan.value;
  var fco = document.purchase_new.FCO.checked;
  var fcw = document.purchase_new.FCW.checked;
  
  if (!fco && !fcw)
  {
    alert("Please selected at least one product.");
    return;
  }

  if (plan == "")
  {
    alert("Please enter plan #");
    return;
  }

  // load the price list
  var prices = price_list();

  // selected plan and it's characteristics
  var plan_info = null;

  // looking for existing plan
  for (i = 0; i < prices.length; i++)
  {
    if (prices[i][0]==plan)
    {
      plan_info = prices[i];
      break;
    }
  }

  // plan not found
  if (plan_info == null)
  {
    alert("There is no such plan [" + plan + "]. Please enter correct one.");
    return;
  }

  // OK, plan found, continue....
  if (trace)
  {
    alert("Plan #" + plan_info[0] + " selected. FCOffice=" + fco + ",  FCWeb=" + fcw);
  }

  // Producing URL string for www.2co.com
  var DEMO = "N";
  var SID = "340031";

  var process_url = "https://www.2checkout.com/2co/buyer/purchase?sid=" + SID;

  if (DEMO == "Y")
  {
    process_url += "&demo=Y";
  }

  // adding products to URL

  // Adding FC Office if necessary
  if (fco)
  {
    process_url += "&product_id=" + plan_info[7] + "&quantity=1&product_id1=" + plan_info[8] + "&quantity1=1";
  }

  // Adding FC Web if necessary
  if (fcw)
  {
    var product_index = fco ? "2" : "";
    process_url += "&product_id" + product_index + "=" + plan_info[9] + "&quantity" + product_index + "=1";
  }

  if (trace){
    alert("Open URL:\n\n" + process_url);
  }

  window.open(process_url, "_self");
}

// -------------------------------------------------------------------------
//
// Invokes when existing customer enters plan # and click "Check out"
//
// -------------------------------------------------------------------------
function purchase_ex_submit()
{
  var trace = false;

  var plan = document.purchase_ex.Plan.value;
  
  if (plan == "")
  {
    alert("Please enter plan #");
    return;
  }

  // OK, plan found, continue....
  if (trace)
  {
    alert("Plan #" + plan);
  }

  // Producing URL string for www.2co.com
  var DEMO = "N";
  var SID = "340031";

  var process_url = "https://www.2checkout.com/2co/buyer/purchase?sid=" + SID;

  if (DEMO == "Y")
  {
    process_url += "&demo=Y";
  }

  // adding products to URL

  process_url += "&product_id=" + plan + "&quantity=1";

  if (trace){
    alert("Open URL:\n\n" + process_url);
  }

  window.open(process_url, "_self");
}