//
// 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;
}

//
// Version or module selected => calc price
//
function calc()
{
	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++;

  var base=59.97;
  var module=13.50;
  var currency=" US";
	var land=document.buyer.country.value;
  if (document.buyer.province.selectedIndex>1 || (land !="" && land!="Canada" && land!="USA"))
  {
    base=64.97;
    module=15.00;
    currency=" CDN";
  }
  var price=Math.round((base+module*num_modules)*100)/100;
  if (document.buyer.item_name[1].checked) price=Math.round(price*75)/100;

  document.buyer.cost.value=price.toString()+currency;
}

//
// Go to upgrade page
//
function upgrade()
{
	location="../support/bill4upgrade.html";
}

//
// Go to add module page
//
function add_module()
{
	location="../support/add_module.html";
}

//
// Province was changed
//
function prov_changed()
{
 if (document.buyer.province.selectedIndex>1)
 {
	document.buyer.country.value="Canada";
	document.buyer.int_prov.value="";
	document.buyer.state.selectedIndex=1;
 }
 else
 {
	var land=document.buyer.country.value;
	if ((land=="Canada") || (land=="USA"))
	{
	 document.buyer.country.value="";
	 document.buyer.int_prov.value="";
	}
 }

 calc();
}

//
// State was changed
//
function state_changed()
{
 if (document.buyer.state.selectedIndex>1)
 {
	document.buyer.country.value="USA";
	document.buyer.int_prov.value="";
	document.buyer.province.selectedIndex=1;
 }
 else
 {
	var land=document.buyer.country.value;
	if ((land=="Canada") || (land=="USA"))
	{
	 document.buyer.country.value="";
	 document.buyer.int_prov.value="";
	}
 }

 calc();
}

//
// International province changed
//
function int_prov_changed()
{
  document.buyer.province.selectedIndex=1;
  document.buyer.state.selectedIndex=1;
  document.buyer.country.value="";
}

//
// Postal code changed
//
function pc_changed()
{
 document.buyer.p_code.value=document.buyer.p_code.value.toUpperCase();
 if ((document.buyer.country.value=="Canada") && (document.buyer.p_code.value.length==6))
 {
	document.buyer.p_code.value=document.buyer.p_code.value.replace(/(.{3})(.{3})/,"$1 $2");
 }
}

//
// Serial # changed
//
function serial_changed()
{
  document.buyer.serial.value=document.buyer.serial.value.toUpperCase();
}

var org_msg=false;

//
// This function checks the fields on the form
// Returns: true if can submit the form
//		false if there was an error
//
function verify()
{
 if (is_empty(document.buyer.first_name))
 {
  alert("Please give your first name");
  document.buyer.first_name.focus();
  return false;
 }

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

 // Always need an email address
 if (is_empty(document.buyer.email))
 {
  alert("Please give your email address\n" +
	  "    so we can send you a copy of your receipt and\n" +
	  "         in case we have any problems with your order.");
  document.buyer.email.focus();
  return false;
 }
 else
 {
  var a=document.buyer.email.value.indexOf("@");
  var p=-1;
  if (a>0) p=document.buyer.email.value.indexOf(".",a);

  if ((a<=0) || (p<=0)  || (p>document.buyer.email.value.length-3))
  {
   if (document.buyer.delivery[2].checked)
     alert("Your email address seems incomplete, please check it.");
   else
    alert("Your email address seems incomplete, please check it.\n\n" +
	  "         (We need it to send you an activation code.)");

   document.buyer.email.focus();
   return false;
  }
 }

 if (!document.buyer.addr_type[0].checked && !document.buyer.addr_type[1].checked)
 {
  alert("Please indicate the type of address");
  document.buyer.addr_type[0].focus();
  return false;
 }
 if (document.buyer.addr_type[0].checked && is_empty(document.buyer.company))
 {
  alert("Please indicate the organization");
  document.buyer.company.focus();
  return false;
 }

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

 var num=0;
 if (document.buyer.province.selectedIndex>1) num++;
 if (document.buyer.state.selectedIndex>1) num++;
 if (!is_empty(document.buyer.int_prov)) num++;

 if (num==0)
 {
  alert("Please give your province or state");
  document.buyer.province.focus();
  return false;
 }
 if (num>1)
 {
  alert("Please give your province or state but not both");
  document.buyer.province.focus();
  return false;
 }

 land=document.buyer.country.value;
 land=land.toUpperCase();
 if (land=="CANADA" && document.buyer.province.selectedIndex<=1)
 {
  alert("Please choose your province from the list");
  document.buyer.province.focus();
  return false;
 }

 if (land=="USA" && document.buyer.state.selectedIndex<=1)
 {
  alert("Please choose your state from the list");
  document.buyer.state.focus();
  return false;
 }

 pc_changed();
 if (is_empty(document.buyer.p_code))
 {
  alert("Please give your postal/zip code");
  document.buyer.p_code.focus();
  return false;
 }
 if (!is_empty(document.buyer.int_prov) && is_empty(document.buyer.country))
 {
  alert("Please give your country");
  document.buyer.country.focus();
  return false;
 }
 if (document.buyer.delivery[0].checked && document.buyer.serial.value.length<10)
 {
  alert("Please give the serial number of your demo\n\nYou can find it by clicking on \"Serial Number\" on the main window");
  document.buyer.serial.focus();
  return false;
 }
 if (document.buyer.delivery[1].checked || document.buyer.delivery[2].checked)
 {
  var name=document.buyer.first_name.value.toUpperCase() + " " +
  		document.buyer.last_name.value.toUpperCase();
  var company=document.buyer.company.value.toUpperCase();

  if (name==company)
  {
	 alert("Please type the name of the church or organization that will\n" +
		"     be using the software, instead of your name.");
	 document.buyer.company.focus();
   return false;
  }
 }
 if (document.buyer.delivery[1].checked)
 {
  if (is_empty(document.buyer.company))
  {
	alert("Please give the name of the church or organization that will be using the software.\n\n" +
		"After your payment has been processed,\n" +
		"           you will be given an activation code that will work with this name.");
	document.buyer.company.focus();
	org_msg=true;
	return false;
  }
  else if (!org_msg)
  {
	 alert("Please check the spelling of the church or organization name then click \"Next >>>\"\n\n" +
		"After your payment has been processed,\n" +
		"           you will be given an activation code that will work with this name.");
	 document.buyer.company.focus();
	 org_msg=true;
	 return false;
  }
 }
 if (document.buyer.delivery[2].checked)
 {
  if (is_empty(document.buyer.company))
  {
	alert("Please give the name of the church or organization that will be using the software.\n\n" +
		"Together with your CD, you will find an activation code that will work with this name.");
	document.buyer.company.focus();
	org_msg=true;
	return false;
  }
  else if (!org_msg)
  {
	alert("Please check the spelling of the church or organization name then click \"Next >>>\"\n\n" +
		"Together with your CD, you will find an activation code that will work with this name.");
	document.buyer.company.focus();
	org_msg=true;  
	return false;
  }
 }

 if (!document.buyer.used_paypal[0].checked && !document.buyer.used_paypal[1].checked)
 {
  alert("Please indicate whether you have used PayPal.");
  document.buyer.used_paypal[0].focus();
  return false;
 }
 return true;
}
