// ------------------------------------------------------------------------------
// Title: "The Official Valley Fever Survivor Medical Glossary" Paperback Book - Button Check/Update JavaScript
// Version: 1.01
// (C) 2008, Golden Phoenix Books, All Rights Reserved.
// ------------------------------------------------------------------------------


// -----------------------------------------------------
//
// Product ID Specific Functions
//
// -----------------------------------------------------


function get_book_title() {
   var sz_title_name = "The Official Valley Fever Survivor Medical Glossary";
   return (sz_title_name);
}


function get_book_isbn_num() {
   var sz_isbn_num = "9780979869228";
   return (sz_isbn_num);
}


function get_book_cost(num_books_in_pkg) {
   var sz_out_cost = "";
   switch( num_books_in_pkg )
   {
      case 1 :
        sz_out_cost = "14.95";
        break;
      case 2 :
        sz_out_cost = "29.90";
        break;
      case 3 :
        sz_out_cost = "44.85";
        break;
      case 4 :
        sz_out_cost = "59.80";
        break;
      default :
        sz_out_cost = "ERROR";
   }
   return (sz_out_cost);
}


function get_shipping_cost(num_books_in_pkg, sz_ship_to, sz_ship_speed) {
   // num_books_in_pkg == integer:  1 .. 4
   // sz_ship_to       == string:   "usa", "intl"
   // sz_ship_speed    == string:   "standard", "priority"
   var sz_out_cost = "ERROR";
   if (sz_ship_to=="usa" && sz_ship_speed=="standard")
   {
        switch( num_books_in_pkg )
        {
           case 1 :
             sz_out_cost =  "5.99";
             break;
           case 2 :
             sz_out_cost =  "5.99";
             break;
           case 3 :
             sz_out_cost =  "7.99";
             break;
           case 4 :
             sz_out_cost =  "7.99";
             break;
           default :
             sz_out_cost = "ERROR";
        }
   }
   else if (sz_ship_to=="usa" && sz_ship_speed=="priority")
   {
        switch( num_books_in_pkg )
        {
           case 1 :
             sz_out_cost =  "8.99"; // 
             break;
           case 2 :
             sz_out_cost =  "8.99"; // "9.75";
             break;
           case 3 :
             sz_out_cost =  "8.99"; // "10.75";
             break;
           case 4 :
             sz_out_cost =  "8.99"; // "11.75";
             break;
           default :
             sz_out_cost = "ERROR";
        }
   }
   else if (sz_ship_to=="intl")
   {
        switch( num_books_in_pkg )
        {
           case 1 :
             sz_out_cost =  "14.95"; // 
             break;
           case 2 :
             sz_out_cost =  "14.95"; // "15.95";
             break;
           case 3 :
             sz_out_cost =  "14.95"; // "32.90";
             break;
           case 4 :
             sz_out_cost =  "14.95"; // "33.90";
             break;
           default :
             sz_out_cost = "ERROR";
        }
   }
   return (sz_out_cost);
}


// -----------------------------------------------------
//
// Generic Functions Below
//
// -----------------------------------------------------


function verify_update_reqfields() {
   var sz_title = "Update Required Fields";
   var sz_reqfields_msg = "Please fill out the following required fields: ";
   var bv_allreqfields_ok = 1;
   // ---------------------------------------------

   var nv_out_num_of_books = 0;
   var sz_out_ship_to_type = "unknown";
   var sz_out_ship_speed   = "unknown";

   var sz_cur_pkg_disptext = "(unknown)";
   var sz_cur_ship_to      = "(unknown)";
   var sz_cur_ship_speed   = "(unknown)";

   var pkg_qty_obj = document.getElementById("id_SelectPkgQtyShipping");

   // determine shipping to USA or International customer
   if (document.vfe_pb_btn_form.btn_shipping_to_usa.checked==1)
   {
        sz_out_ship_to_type = "usa";
        sz_cur_ship_to      = " USA";
        // determine shipping speed type
        if (document.vfe_pb_btn_form.usa_shipping_speed_type.value=="shipping_speed_standard")
        {
            sz_out_ship_speed = "standard";
            sz_cur_ship_speed = " Standard";
        }
        else if (document.vfe_pb_btn_form.usa_shipping_speed_type.value=="shipping_speed_priority")
        {
            sz_out_ship_speed = "priority";
            sz_cur_ship_speed = " Priority";
        }
   }
   else if (document.vfe_pb_btn_form.btn_shipping_to_intl.checked==1)
   {
        sz_out_ship_to_type = "intl";
        sz_cur_ship_to      = " International";
        // determine shipping speed type
        sz_out_ship_speed = "standard";
        sz_cur_ship_speed = "";
   }

   // determine quantity for package contents
   if (pkg_qty_obj.value=="book_pkg_of_1")
   {
      nv_out_num_of_books = 1;
      sz_cur_pkg_disptext = "1 book,";
   }
   else if (pkg_qty_obj.value=="book_pkg_of_2")
   {
      nv_out_num_of_books = 2;
      sz_cur_pkg_disptext = "Package of 2 books,";
   }
   else if (pkg_qty_obj.value=="book_pkg_of_3")
   {
      nv_out_num_of_books = 3;
      sz_cur_pkg_disptext = "Package of 3 books,";
   }
   else if (pkg_qty_obj.value=="book_pkg_of_4")
   {
      nv_out_num_of_books = 4;
      sz_cur_pkg_disptext = "Package of 4 books,";
   }

   // obtain item name string and assign appropriate output rates
   var sz_cur_book_cost     = get_book_cost(nv_out_num_of_books);
   var sz_cur_shipping_cost = get_shipping_cost(nv_out_num_of_books, sz_out_ship_to_type, sz_out_ship_speed);

   var sz_cur_book_title = get_book_title();

   document.vfe_pb_btn_form.item_name.value   = sz_cur_book_title + " Paperback (" + sz_cur_pkg_disptext + sz_cur_ship_to + sz_cur_ship_speed + " Shipping). ALL SALES ARE FINAL.";
   document.vfe_pb_btn_form.item_number.value = get_book_isbn_num();
   document.vfe_pb_btn_form.amount.value      = sz_cur_book_cost;
   document.vfe_pb_btn_form.shipping.value    = sz_cur_shipping_cost;
   document.vfe_pb_btn_form.shipping2.value   = sz_cur_shipping_cost;

   // ---------------------------------------------------------------------------
   // if there are any empty fields, display msg and abort the submit of the form
   // ---------------------------------------------------------------------------
   if (bv_allreqfields_ok == 1)
   {
      document.vfe_pb_btn_form.submit();
   }
   else
   {
      alert(sz_reqfields_msg);
      return false;
   }
}


function init_reload() {
      update_on_click_shipping_to_rad(1);
      update_pkgqty_costs_popup_menu();
}


function update_on_click_shipping_to_rad(num_option) {
   // ---------------------------------------------
   if (num_option==1)
   {
      document.vfe_pb_btn_form.btn_shipping_to_usa.checked  = 1;
      document.vfe_pb_btn_form.btn_shipping_to_intl.checked = 0;
   }
   else if (num_option==2)
   {
      document.vfe_pb_btn_form.btn_shipping_to_usa.checked  = 0;
      document.vfe_pb_btn_form.btn_shipping_to_intl.checked = 1;
   }

   // also update lists in popup menu
   update_pkgqty_costs_popup_menu();
   // ---------------------------------------------
}


function add_option_to_select(sz_sel_obj_id, sz_opt_value, sz_opt_text) {
   // add option to end of select list object
   // ---------------------------------------------
   var   sel_obj = document.getElementById(sz_sel_obj_id);
   var   opt_obj = document.createElement('option');

   opt_obj.value = sz_opt_value;
   opt_obj.text  = sz_opt_text;

   try
   {
      sel_obj.add(opt_obj,null); // standard browsers
   }
   catch(ex)
   {
      sel_obj.add(opt_obj);      // IE browser
   }
   // ---------------------------------------------
}


function update_pkgqty_costs_popup_menu() {
   // ---------------------------------------------
   // get the Select list objects
   var x1 = document.getElementById("id_SelectPkgQtyShipping");
   var cur_x1_index = x1.selectedIndex;

   var sst1 = document.getElementById("id_ship_speed_type");

   var ship_to_type = "usa";
   if (document.vfe_pb_btn_form.btn_shipping_to_intl.checked==1)
   {
      ship_to_type = "intl";
   }
   var ship_method_type = "standard";
   if (sst1.value=="shipping_speed_standard")
   {
      ship_method_type = "standard";
   }
   else if (sst1.value=="shipping_speed_priority")
   {
      ship_method_type = "priority";
   }

   // alert(sst1.value);

   // remove all elements from Select list (note that index=0 for first element in select list object)
   var i;
   var list_len = x1.length;
   for (i=1;i<=list_len;i++)
   {
      x1.remove(0);
   }

   // now rebuild the Select list based upon current option settings
   if (ship_to_type=="usa")
   {
        sst1.disabled = 0; // enable shipping speed (method) popup menu

        if (ship_method_type=="standard")
        {
            // USA standard ground shipping
            add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_1", "Single (One) book  " + get_book_cost(1) + " + "+get_shipping_cost(1,"usa","standard")+" S&H (US)");
            add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_2", "Package of 2 books " + get_book_cost(2) + " + "+get_shipping_cost(2,"usa","standard")+" S&H (US)");
            add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_3", "Package of 3 books " + get_book_cost(3) + " + "+get_shipping_cost(3,"usa","standard")+" S&H (US)");
            add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_4", "Package of 4 books " + get_book_cost(4) + " + "+get_shipping_cost(4,"usa","standard")+" S&H (US)");
        }
        else if (ship_method_type=="priority")
        {
            // USA priority shipping
            add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_1", "Single (One) book  "+get_book_cost(1)+" + "+get_shipping_cost(1,"usa","priority")+" S&H (US)");
            add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_2", "Package of 2 books "+get_book_cost(2)+" + "+get_shipping_cost(2,"usa","priority")+" S&H (US)");
            add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_3", "Package of 3 books "+get_book_cost(3)+" + "+get_shipping_cost(3,"usa","priority")+" S&H (US)");
            add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_4", "Package of 4 books "+get_book_cost(4)+" + "+get_shipping_cost(4,"usa","priority")+" S&H (US)");
        }

        // put the current index back for the selected popup menu list item after the list has been re-populated
        x1.selectedIndex = cur_x1_index;
   }
   else if (ship_to_type=="intl")
   {
        // set shipping speed (method) to standard
        sst1.selectedIndex = 0;
        sst1.disabled = 1; //  disable popup menu

        // International standard shipping
        add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_1", "Single (One) book  " + get_book_cost(1) + " + "+get_shipping_cost(1,"intl","standard")+" S&H (INT'L)");
        add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_2", "Package of 2 books " + get_book_cost(2) + " + "+get_shipping_cost(2,"intl","standard")+" S&H (INT'L)");
     // add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_3", "Package of 3 books " + get_book_cost(3) + " + "+get_shipping_cost(3,"intl","standard")+" S&H (INT'L)");
     // add_option_to_select("id_SelectPkgQtyShipping", "book_pkg_of_4", "Package of 4 books " + get_book_cost(4) + " + "+get_shipping_cost(4,"intl","standard")+" S&H (INT'L)");

        x1.selectedIndex = 0;
   }

   // ---------------------------------------------
}
