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

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

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

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

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

//
// 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.prospect.first_name))
 {
  alert("Please give your first name");
  document.prospect.first_name.focus();
  return false;
 }

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

 // Always need an email address
 if (is_empty(document.prospect.email))
 {
  alert("Please give your email address.");
  document.prospect.email.focus();
  return false;
 }
 else
 {
  var a=document.prospect.email.value.indexOf("@");
  var p=-1;
  if (a>0) p=document.prospect.email.value.indexOf(".",a);

  if ((a<=0) || (p<=0)  || (p>document.prospect.email.value.length-3))
  {
   alert("Your email address seems incomplete, please check it.");
   document.prospect.email.focus();
   return false;
  }
 }

 if (document.prospect.system_type.selectedIndex<1)
 {
  alert("Please choose your operating system from the list");
  document.prospect.system_type.focus();
  return false;
 }

 if (document.prospect.formsize.value=="long") return verify_long();
 		else return verify_short();
}
//
// Check the fields for the short demo form
//
function verify_short()
{
 if (document.prospect.org_type.selectedIndex<1)
 {
  alert("Please choose the type of organization from the list");
  document.prospect.org_type.focus();
  return false;
 }

 return true;
}
//
// Check the fields for the long demo form
//
function verify_long()
{
 if (!document.prospect.addr_type[0].checked && !document.prospect.addr_type[1].checked)
 {
  alert("Please indicate the type of address: home or organization");
  document.prospect.addr_type[0].focus();
  return false;
 }
 if (document.prospect.addr_type[0].checked && is_empty(document.prospect.company))
 {
  alert("Please indicate the organization");
  document.prospect.company.focus();
  return false;
 }
 if (!is_empty(document.prospect.company) && is_empty(document.prospect.c_telephone))
 {
  alert("Please indicate the organization phone #");
  document.prospect.c_telephone.focus();
  return false;
 }

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

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

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

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

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

 pc_changed();
 if (is_empty(document.prospect.p_code))
 {
  alert("Please give your postal/zip code");
  document.prospect.p_code.focus();
  return false;
 }


 if (land=="")
 {
   if (document.prospect.state.selectedIndex>1)
   {
    document.prospect.country.value="USA";
   }
   else  if (document.prospect.province.selectedIndex>1)
   {
	  document.prospect.country.value="Canada";
   }
   else if (!is_empty(document.prospect.int_prov))
   {
    alert("Please give your country");
    document.prospect.country.focus();
    return false;
   }
 }

 if (document.prospect.position.selectedIndex<1)
 {
  alert("Please choose your position from the list");
  document.prospect.position.focus();
  return false;
 }

 if (document.prospect.magazine.selectedIndex<1 &&
      document.prospect.search_eng.selectedIndex<1 &&
      document.prospect.website.selectedIndex<1 &&
          is_empty(document.prospect.other))
 {
  alert("Please tell indicate how you found out about us");
  document.prospect.magazine.focus();
  return false;
 }

 return true;
}
