//<![CDATA[  hide from older browsers
//
// This script handles the Patient Reorder page functions
//
//-------------------------
// MODIFICATION LOG
//-------------------------
// trr 05/17/2009 - created
// trr 06/08/2009 - finalized changes for remote ECP deployment
//

//----------------------------------------------------------------------------

function trim(data)
{
 // This function removes leading and trailing whitespace

 while(data.charAt(0) == " ")
 {
  data = data.substring(1,data.length);
 }
 while(data.charAt(data.length - 1) == " ")
 {
  data = data.substring(0,data.length - 1);
 }

 return(data);

}

//----------------------------------------------------------------------------

function reset_ship_addr()
{
 // This function removes the error flag from the special request field if the
 // proper drop down option is selected

 document.ReorderForm.ship_address.className = 'style6';
 document.ReorderForm.ship_city.className = 'style6';
 document.ReorderForm.ship_state.className = 'style6';
 document.ReorderForm.ship_zip.className = 'style6';

 return;

}

//----------------------------------------------------------------------------

function reset_special_req()
{
 // This function removes the error flag from the shipping address if the
 // proper radio button is selected

 if (document.ReorderForm.shipping.selectedIndex != 3)
 {
  // no special request info required
  document.ReorderForm.special_request.className = 'style6';
 }

}

//----------------------------------------------------------------------------

function reset_cc_bill_addr()
{
 // This function removes the error flag from the credit card billing address
 // if the proper radio button is selected

 document.ReorderForm.cc_address.className = 'style6';
 document.ReorderForm.cc_city.className = 'style6';
 document.ReorderForm.cc_state.className = 'style6';
 document.ReorderForm.cc_zip.className = 'style6';

 return;

}

//----------------------------------------------------------------------------

function validate_form()
{
 // This function checks everything on the form before submitting it

 var error_msg = "One or more required fields is not filled out properly.   Fields that contain errors are marked red.\n\n";
 var error_msg_ftr = "\nPlease correct all errors before submitting this form.";
 var temp = '';
 var email = '';
 var confirm_email = '';
 var right_brand = '';
 var left_brand = '';
 var OK = "Y";		// default to passing edits

 temp = trim(document.ReorderForm.first_name.value);
 if (temp == "")
 {
  OK = "N";
  document.ReorderForm.first_name.className = "error";
  error_msg += "\t- missing patient first name\n";
 }
 temp = trim(document.ReorderForm.last_name.value);
 if (temp == "")
 {
  OK = "N";
  document.ReorderForm.last_name.className = "error";
  error_msg += "\t- missing patient last name\n";
 }
 temp = trim(document.ReorderForm.address.value);
 if (temp == "")
 {
  OK = "N";
  document.ReorderForm.address.className = "error";
  error_msg += "\t- missing patient street address\n";
 }
 temp = trim(document.ReorderForm.city.value);
 if (temp == "")
 {
  OK = "N";
  document.ReorderForm.city.className = "error";
  error_msg += "\t- missing patient city\n";
 }
 temp = trim(document.ReorderForm.state.value);
 if (temp == "")
 {
  OK = "N";
  document.ReorderForm.state.className = "error";
  error_msg += "\t- missing patient state\n";
 }
 temp = trim(document.ReorderForm.zip.value);
 if (temp == "")
 {
  OK = "N";
  document.ReorderForm.zip.className = "error";
  error_msg += "\t- missing patient zip code\n";
 }
 email = trim(document.ReorderForm.email.value);
 confirm_email = trim(document.ReorderForm.confirm_email.value);
 if ((email == "") || (confirm_email == "") || (confirm_email != email))
 {
  OK = "N";
  document.ReorderForm.email.className = "error";
  document.ReorderForm.confirm_email.className = "error";
  error_msg += "\t- email addresses must match and be non-blank\n";
 }
 if (document.ReorderForm.shipping.selectedIndex == 3)
 {
  // make sure special request section is filled out
  temp = trim(document.ReorderForm.special_request.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.special_request.className = "error";
   error_msg += "\t- missing shipping special request\n";
  }
 }
 if (document.ReorderForm.ship_addr_same[1].checked)
 {
  // user specified that the shipping address is different
  // make sure all address information is present
  temp = trim(document.ReorderForm.ship_address.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.ship_address.className = "error";
   error_msg += "\t- missing shipping street address\n";
  }
  temp = trim(document.ReorderForm.ship_city.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.ship_city.className = "error";
   error_msg += "\t- missing shipping city\n";
  }
  temp = trim(document.ReorderForm.ship_state.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.ship_state.className = "error";
   error_msg += "\t- missing shipping state\n";
  }
  temp = trim(document.ReorderForm.ship_zip.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.ship_zip.className = "error";
   error_msg += "\t- missing shipping zip code\n";
  }
 }
 right_brand = trim(document.ReorderForm.right_brand.value);
 if (right_brand != "")
 {
  // brand name specified, must have valid quantity as well
  temp = trim(document.ReorderForm.right_quantity.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.right_quantity.className = "error";
   error_msg += "\t- missing right eye quantity\n";
  }
 }
 left_brand = trim(document.ReorderForm.left_brand.value);
 if (left_brand != "")
 {
  // brand name specified, must have valid quantity as well
  temp = trim(document.ReorderForm.left_quantity.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.left_quantity.className = "error";
   error_msg += "\t- missing left eye quantity\n";
  }
 }
 if ((right_brand == "") && (left_brand == ""))
 {
  // no products specified
  OK = "N";
  document.ReorderForm.right_brand.className = "error";
  document.ReorderForm.left_brand.className = "error";
  error_msg += "\t- no lenses have been ordered\n";
 }
 if (document.ReorderForm.card_type.selectedIndex == 0)
 {
  OK = "N";
  document.ReorderForm.card_type.className = "error";
  error_msg += "\t- credit card type not selected\n";
 }
 if (document.ReorderForm.expiration_month.selectedIndex == 0)
 {
  OK = "N";
  document.ReorderForm.expiration_month.className = "error";
  error_msg += "\t- credit card expiration month not selected\n";
 }
 if (document.ReorderForm.expiration_year.selectedIndex == 0)
 {
  OK = "N";
  document.ReorderForm.expiration_year.className = "error";
  error_msg += "\t- credit card expiration year not selected\n";
 }
 temp = trim(document.ReorderForm.name_on_card.value);
 if (temp == "")
 {
  OK = "N";
  document.ReorderForm.name_on_card.className = "error";
  error_msg += "\t- missing credit card holder's name\n";
 }
 temp = trim(document.ReorderForm.card_number.value);
 if (temp == "")
 {
  OK = "N";
  document.ReorderForm.card_number.className = "error";
  error_msg += "\t- missing credit card number\n";
 }
 if (document.ReorderForm.cc_addr_same[1].checked)
 {
  // user specified that the credit card billing address is different
  // make sure all address information is present
  temp = trim(document.ReorderForm.cc_address.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.cc_address.className = "error";
   error_msg += "\t- missing credit card billing street address\n";
  }
  temp = trim(document.ReorderForm.cc_city.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.cc_city.className = "error";
   error_msg += "\t- missing credit card billing city\n";
  }
  temp = trim(document.ReorderForm.cc_state.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.cc_state.className = "error";
   error_msg += "\t- missing credit card billing state\n";
  }
  temp = trim(document.ReorderForm.cc_zip.value);
  if (temp == "")
  {
   OK = "N";
   document.ReorderForm.cc_zip.className = "error";
   error_msg += "\t- missing credit card billing zip code\n";
  }
 }

 // submit form
 if (OK == "Y")
 {
  // form has passed edits
  temp = trim(document.ReorderForm.security_code.value);
  if (temp == "")
  {
   var agree = confirm("You have not entered a security code for your credit card.  If your card has a security code, click 'Cancel' to go back and enter it before submitting your order.  Otherwise click 'OK' to proceed.");
   if (agree)
   {
    document.ReorderForm.submit();
   }
  }
  else
  {
   document.ReorderForm.submit();
  }
 }
 else
 {
  // one or more fields failed edits, assemble error message text
  error_msg += error_msg_ftr;
  alert(error_msg);
 }

 return;

}

//]]>

