// Used by: cheque.html, credit_card.html, bill4upgrade.html
//          and add_module.html
//
// Convert a number into a 2 digit string for dollar amounts
//
function dollar(n)
{
	var str=n.toString();
  var i=str.indexOf(".");
  if (i==-1) str+=".00";
  if (i==str.length-2) str+="0";

  str="$" + str;
  return str;
}
//
// Is the field empty?
//
function is_empty(f)
{
 if ((f.value==null) || (f.value=="")) return true;
 for(var i=0;i<f.value.length;i++)
 {
  var c=f.value.charAt(i);
  if ((c!=' ') && (c!='\n') && (c!='\t')) return false;
 }

 return true;
}
//
// Is the field numeric?
//
function is_digits(f)
{
 if ((f.value==null) || (f.value=="")) return false;
 for(var i=0;i<f.value.length;i++)
 {
  var c=f.value.charAt(i);
  if ((c<'0') || (c>'9')) return false;
 }

 return true;
}

//
// Check if serial number was entered
// Only called by cheque.html
//
function serial_ok()
{
  if (document.buyer.delivery[0].checked)
  {
    if (is_empty(document.buyer.serial_mid))
    {
      alert("Please enter the middle part\n  of your serial number");
      document.buyer.serial_mid.focus();
      return;
    }
    if (is_empty(document.buyer.serial_end))
    {
      alert("Please enter the last part\n  of your serial number");
      document.buyer.serial_end.focus();
      return;
    }
  }
}

function calc2(ver)
{
	var num_modules=0;
	if (document.buyer.pledge_module.checked) num_modules++;
	if (document.buyer.gik_module.checked) num_modules++;
	if (document.buyer.pad_module.checked) num_modules++;
	if (document.buyer.custom_module.checked) num_modules++;
	if (document.buyer.letter_email_module.checked) num_modules++;
	if (document.buyer.relationship_module.checked) num_modules++;

  if (num_modules>0 &&
    typeof document.buyer.add_module!="undefined" &&
    !document.buyer.add_module.checked)
    document.buyer.add_module.checked=true;

  var curr_modules=num_modules;

  if (typeof document.buyer.curr_modules!="undefined")
  {
    if (is_digits(document.buyer.curr_modules))
      curr_modules=document.buyer.curr_modules.value;
    else
      curr_modules=0;
  }
  var extra_modules=0;

  if (num_modules>curr_modules) extra_modules=num_modules-curr_modules;
      else curr_modules=num_modules;

  var where;
  var upgrade=true;
  var add_module=false;
  var base=cdn_basE;
  var module=cdn_modulE;
 // 0=US 1=Ont, NL, NB 2=NS 3=BC 4=Can 5=Int 6=Developing
  where=document.buyer.where.selectedIndex;
  if (where==0)
  {
    base=us_basE;
    module=us_modulE;
  }
  else if (where==6)
  {
    base=Math.round(cdn_basE*40)/100;
    module=Math.round(cdn_modulE*40)/100;
  }

  if (typeof document.buyer.stamps!="undefined")
  {
    if (where==0) document.buyer.stamps.value="USPS requires extra postage when mailing to Canada";
      else document.buyer.stamps.value="";
  }

  var increase=0;
  if (typeof document.buyer.product!="undefined" &&
    document.buyer.product.length==2 &&
    typeof document.buyer.inc_donors!="undefined" &&
      document.buyer.inc_donors.checked)
  {
    if (document.buyer.product[1].checked)
      increase=Math.round(25*base)/100;
    else document.buyer.inc_donors.checked=false;
  }

  if (typeof document.buyer.delivery!="undefined")
  {
   upgrade=false;
  }
  else if (typeof document.buyer.upg_calc=="undefined")
  {
    add_module=true;
    base=0;
  }

  var price=Math.round((base+increase+module*num_modules)*100)/100;
  var discount=0;

  if ((typeof document.buyer.product!="undefined" &&
    document.buyer.product.length==2 &&
    document.buyer.product[1].checked &&
    typeof document.buyer.inc_donors!="undefined" &&
    !document.buyer.inc_donors.checked) ||
    (typeof document.buyer.small_org!="undefined" &&
      document.buyer.small_org.checked) )
  {
    discount=-Math.round(price*25)/100;
  }

  document.buyer.discount.value=dollar(discount);

  if (!upgrade)
  {
    var shipping=0;
    if (document.buyer.delivery[1].checked)
    {
 // 0=US 1=Ont, NL, NB 2=NS 3=BC 4=Can 5=Int 6=Developing
      if (where==0) shipping=us_shippinG;
      if (where==5) shipping=int_shippinG;
      else if (where==6) shipping=dev_shippinG;
      else shipping=cdn_shippinG;
    }

    document.buyer.shipping.value=dollar(shipping);

    price=Math.round((price+discount+shipping)*100)/100;
    document.buyer.subtot.value=dollar(price);

//    var pst=0;
//    if (where==1) pst=Math.round(8*price)/100;
    var gst=0;
    if (where==1) gst=Math.round(13*price)/100;
    if (where==2) gst=Math.round(15*price)/100;
    if (where==3) gst=Math.round(12*price)/100;
    if (where==4) gst=Math.round(5*price)/100;

//    document.buyer.pst.value=dollar(pst);
    document.buyer.gst.value=dollar(gst);

    price=Math.round((price+gst)*100)/100;

    var final=dollar(price);
    if (where==0) final+=" US";
      else final+=" CDN";

    document.buyer.total.value=final;
    document.buyer.total2.value=final;
	}
  else  // Upgrade
  {
    var full_price=price;
    document.buyer.subtot.value=dollar(price);

    price=Math.round((price+discount)*100)/100;
    var price_str=dollar(price);
    document.buyer.total.value=price_str;

    if (!add_module)
    {
      var diff=document.buyer.diff.value;
      if (diff>0.5)
      {
        diff=0.5;
        document.buyer.diff.value=diff;
      }
      if (ver && diff<=0.5 && diff>0)
      {
        var curr_ver=document.buyer.curr_ver.value;
        document.buyer.version.value=Math.round((curr_ver-diff)*100)/100;
      }

      price=Math.round((base+increase+module*curr_modules)*100)/100;
      var upg_cost=dollar(Math.round((price*diff+extra_modules*module)*100)/100);
      price_str=dollar(price);
      var upg_calc=price_str+" x "+ diff;
      if (extra_modules>0)
      {
        price_str=dollar(module);
        upg_calc=upg_calc+ " + " + extra_modules + " x " + price_str;
      }
      upg_calc=upg_calc + " = " + upg_cost;
      document.buyer.upg_calc.value=upg_calc;
    }
  }
}

//
// Version or module selected => calc price
//
function calc()
{
  calc2(1);
}

//
// Small org checked
//
function calc_small()
{
  if (document.buyer.small_org.checked) document.buyer.large_org.checked=false;

  calc();
}

//
// Large org checked
//
function calc_large()
{
  if (document.buyer.large_org.checked) document.buyer.small_org.checked=false;

  calc();
}

//
// Check for any missing items or problems
//
function verify()
{
  if (!document.buyer.small_org.checked && !document.buyer.large_org.checked)
  {
    alert("Please choose the size of your church/organization");
    document.buyer.relationship_module.focus();
    return false;
  }

  if (is_empty(document.buyer.name))
  {
    alert("Please give your name");
    document.buyer.name.focus();
    return false;
  }

  if (is_empty(document.buyer.company))
  {
    alert("Please give your church/organization");
    document.buyer.company.focus();
    return false;
  }

  if (is_empty(document.buyer.user_email) || is_empty(document.buyer.isp_email))
  {
    alert("Please give your email address");
    document.buyer.user_email.focus();
    return false;
  }

  if (!modules_ok()) return false;

  // Don't check if upgrade code or serial# given bc it is not needed
  // for a CD or requesting an upgrade code
  // and we don't know which submit button was clicked
  return true;
}
//
// Convert char to uppercase then check for misreading serial #/activation code
// Convert I-->1, B-->8, U-->V, Z-->2 and G-->6
//		(O,B,G,I,U,Z are never used in a serial #/activation code)
//
function fix_char(ch)
{
	ch=ch.toUpperCase(ch);

	if (ch=="I") return "1";
	else if (ch=="O") return "0";
	else if (ch=="B") return "8";
	else if (ch=="G") return "6";
	else if (ch=="U") return "V";
	else if (ch=="Z") return "2";
	else return ch;
}

//
// Convert a string in another base (using the given encoding scheme)
//		 into a #
//	eg: 2AF in hex => 2*256 + 10*16 + 15 = 687
//
// returns: -1 if any invalid chars in str_
//
// Note: chars in str_ can be in upper/lower case
//			 chars in scheme_ are assumed to be uppercase
//
function convert_base(str_)
{
  var eP="ACEFHJKLMNPRSTVWXY123456789";	// Product # encoding
	var base=eP.length;
	var len=str_.length;
	var digit=0;

	var num=0;
	for (var i=0;i<len;i++)
	{
		digit=eP.indexOf(fix_char(str_.charAt(i)));
		if (digit<0) return -1;

		num=base*num+digit;
	}

	return num;
}

var module_msg=false;
//
// Check if the modules the customer has now have been selected
//
function modules_ok()
{
  if (module_msg) return true;
  // Find out which modules the customer has now
  var prod=convert_base(document.buyer.front_serial.value)-20;

  if (prod<=0) return true;

  var missing="";
  var num_modules=0;
	if (prod&1 && !document.buyer.pledge_module.checked)
  {
    missing+="      Pledge Tracking\n";
    num_modules++;
  }
	if (prod&2 && !document.buyer.gik_module.checked)
  {
    missing+="      Gifts in Kind\n";
    num_modules++;
  }
	if (prod&4 && !document.buyer.pad_module.checked)
  {
    missing+="      Pre-authorized Deposits\n";
    num_modules++;
  }
	if (prod&8 && !document.buyer.custom_module.checked)
  {
    missing+="      Custom Receipts\n";
    num_modules++;
  }
	if (prod&16 && !document.buyer.letter_email_module.checked)
  {
    missing+="      Letters/Emails\n";
    num_modules++;
  }
	if (prod&32 && !document.buyer.relationship_module.checked)
  {
    missing+="      Relationships/Families\n";
    num_modules++;
  }

  if (num_modules==0) return true;

  var heading="You have should select ";
  if (num_modules==1) heading=heading+"this module:\n";
      else heading+="these modules:\n";
  var msg=heading+missing+"because you are using ";
  if (num_modules==1) msg+="it";
    else msg+="them";
  msg+=" now.\n\nPlease select the modules you need.\n(If you don't select them they will be de-activated.)";
  alert(msg);
  module_msg=true;
  document.buyer.product.focus();
  return false;
}

//
// Updating current version #
//
function version_changed()
{
  var version=document.buyer.version.value;
  var curr_ver=document.buyer.curr_ver.value;
  var diff=Math.round(Math.min(0.50,curr_ver-version)*100)/100;
  if (diff<0) diff=0;
  document.buyer.diff.value=diff;
  calc2(0);
}

//
// Serial # changed
//
function serial_changed()
{
 if (typeof document.buyer.front_serial!="undefined")
   document.buyer.front_serial.value=document.buyer.front_serial.value.toUpperCase();

 document.buyer.middle_serial.value=document.buyer.middle_serial.value.toUpperCase();
 document.buyer.end_serial.value=document.buyer.end_serial.value.toUpperCase();
}

//
// Upgrade code changed
//
function code_changed()
{
 document.buyer.middle_code.value=document.buyer.middle_code.value.toUpperCase();
 document.buyer.end_code.value=document.buyer.end_code.value.toUpperCase();
}

