var zip_Format = /^(\d{5})(-\d{4})?$/;
var phone3_Format = /^[2-9]\d{2}$/;
var phone4_Format = /^\d{4}$/;
var ext_Format = /^\d*$/;
var card_Format = /^\d{16}$/;
var email_Format = /^(.+\@.+\..+)?$/;
var mc_Format = /^5[1-5]\d{14}$/
var visa_Format = /^4\d{12}(\d{3})?$/

var date = new Date();
var thisYear = date.getYear();
var thisMonth = date.getMonth()+1;
var thisYear2 = thisYear.toString().slice(2);
var thisDate = date.getDate();



function not_Blank(input)
{
  if (input.value == "")
  {
    switch (input.name)
    {
      case 'Childs_Name':
        alert("Please enter a value for the \"Child's Name\" field.");
        break;

      case 'Your_Name':
        alert("Please enter a value for the \"Parent's Name\" field.");
        break;

      case 'Address':
        alert("Please enter a value for the \"Address\" field.");
        break;

      case 'Old_Address':
        alert("Please enter a value for the \"Address\" field.");
        break;

      case 'City':
        alert("Please enter a value for the \"City\" field.");
        break;

      case 'Old_City':
        alert("Please enter a value for the \"City\" field.");
        break;

      case 'Card_Holder':
        alert("Please enter a value for the \"Card Holder's Name\" field.");
        break;

      case 'Attn':
        alert("Please enter a value for the \"Attention\" field.");
        break;

      case 'Second_Childs_Name':
        alert("Please enter a value for the \"Second Child's Name\" field.");
        break;

      case 'Insurance':
        alert("Please enter a value for the \"Insurance\" field.");
        break;

      case 'Insurance_ID':
        alert("Please enter a value for the \"Insurance ID\" field.");
        break;

      case 'Reason':
        alert("Please enter a value for the \"Reason\" field.");
        break;

      case 'Specialist_Name':
        alert("Please enter a value for the \"Specialist's Name\" field.");
        break;

      case 'Provider_ID':
        alert("Please enter a value for the \"Specialist's Provider ID\" field.");
        break;

      case 'Provider_NPI':
        alert("Please enter a value for the \"Specialist's NPI #\" field.");
        break;

      case 'Hospital':
        alert("Please enter a value for the \"Specialist's Hospital\" field.");
        break;
    }

    input.focus();
    return (false);
  }

  return (true);
}



function date_Validator(Month, Date, Year)
{
  if (Month.selectedIndex == 0)
  {
    if (Month.name == "DOB_Month" || Month.name == "Second_DOB_Month")
      alert("The first \"Date of Birth Month\" option is not a valid selection.  Please choose one of the other options.");

    if (Month.name == "App_Month")
      alert("The first \"Appointment Month\" option is not a valid selection.  Please choose one of the other options.");

    Month.focus();
    return (false);
  }

  if (Date.selectedIndex == 0)
  {
    if (Date.name == "DOB_Date" || Date.name == "Second_DOB_Date")
      alert("The first \"Date of Birth Date\" option is not a valid selection.  Please choose one of the other options.");

    if (Date.name == "App_Date")
      alert("The first \"Appointment Date\" option is not a valid selection.  Please choose one of the other options.");

    Date.focus();
    return (false);
  }

  if (Year.selectedIndex == 0)
  {
    if (Year.name == "DOB_Year" || Year.name == "Second_DOB_Year")
      alert("The first \"Date of Birth Year\" option is not a valid selection.  Please choose one of the other options.");

    if (Year.name == "App_Year")
      alert("The first \"Appointment Year\" option is not a valid selection.  Please choose one of the other options.");

    Year.focus();
    return (false);
  }

  if (Month.selectedIndex == 2 && Date.value == 29 && Year.value%4 != 0)
  {
    alert("Please Select a valid day in the \"Date\" field.");
    Date.focus();
    return (false);
  }

  if ((Month.name == "DOB_Month" || Month.name == "Second_DOB_Month") && Year.value == thisYear && (Month.selectedIndex > thisMonth || (Month.selectedIndex == thisMonth && Date.selectedIndex > thisDate)))
  {
    alert("Please Select a valid date in the \"Date of Birth\" field.");
    Month.focus();
    return (false);
  }

  return (true);
}



function address_Validator(Address, City, State, Zip)
{
  if (!not_Blank(Address))
    return (false);

  if (!not_Blank(City))
    return (false);

  if (State.selectedIndex == 0)
  {
    alert("The first \"State\" option is not a valid selection.  Please choose one of the other options.");
    State.focus();
    return (false);
  }

  if (!zip_Format.test(Zip.value))
  {
    alert("Please enter a valid value for the \"Zip Code\" field.");
    Zip.focus();
    return (false);
  }

  return (true);
}



function phone_Validator(Phone1, Phone2, Phone3)
{
  if (!phone3_Format.test(Phone1.value))
  {
    alert("Please enter the three digit area code.");
    Phone1.focus();
    return (false);
  }

  if (!phone3_Format.test(Phone2.value))
  {
    switch(Phone2.name)
    {
      case 'Phone2':
        alert("Please enter a valid value for the \"Home Telephone\" field.");
        break;

      case 'Work_Phone2':
        alert("Please enter a valid value for the \"Work/Cell Phone\" field.");
        break;

      case 'Spec_Phone2':
        alert("Please enter a valid value for the \"Specialist's Telephone Number\" field.");
        break;

      case 'Fax2':
        alert("Please enter a valid value for the \"Fax Number\" field.");
        break;
    }

    Phone2.focus();
    return (false);
  }

  if (!phone4_Format.test(Phone3.value))
  {
    switch(Phone3.name)
    {
      case 'Phone3':
        alert("Please enter a valid value for the \"Home Telephone\" field.");
        break;

      case 'Work_Phone3':
        alert("Please enter a valid value for the \"Work/Cell Phone\" field.");
        break;

      case 'Spec_Phone3':
        alert("Please enter a valid value for the \"Specialist's Telephone Number\" field.");
        break;

      case 'Fax3':
        alert("Please enter a valid value for the \"Fax Number\" field.");
        break;
    }

    Phone3.focus();
    return (false);
  }

  return (true);
}



/* Email Form Validator */

function Email_Validator(theForm)
{
  if (!not_Blank(theForm.Childs_Name))
    return (false);

  if (!date_Validator(theForm.DOB_Month, theForm.DOB_Date, theForm.DOB_Year))
    return (false);

  if (!not_Blank(theForm.Your_Name))
    return (false);

  if (!address_Validator(theForm.Address, theForm.City, theForm.State, theForm.Zip_Code))
    return (false);

  if (!phone_Validator(theForm.Phone1, theForm.Phone2, theForm.Phone3))
    return (false);

  if (theForm.Work_Phone1.value != "" || theForm.Work_Phone2.value != "" || theForm.Work_Phone3.value!= "" || theForm.Work_Ext.value!= "")
    if (!phone_Validator(theForm.Work_Phone1, theForm.Work_Phone2, theForm.Work_Phone3))
      return (false);

  if (!ext_Format.test(theForm.Work_Ext.value))
  {
    alert("Please enter a valid value for the \"Extension\" field.");
    theForm.Work_Ext.focus();
    return (false);
  }

  if (!email_Format.test(theForm.Email.value))
  {
    alert("Please enter a valid value for the \"Email\" field. (Leave blank if you do not have an email account)");
    theForm.Email.focus();
    return (false);
  }

  return (true);
}



/* Address Form Validator */

function Address_Validator(theForm)
{
  if (!not_Blank(theForm.Childs_Name))
    return (false);

  if (!date_Validator(theForm.DOB_Month, theForm.DOB_Date, theForm.DOB_Year))
    return (false);

  if (!not_Blank(theForm.Your_Name))
    return (false);

  if (!address_Validator(theForm.Old_Address, theForm.Old_City, theForm.Old_State, theForm.Old_Zip_Code))
    return (false);

  if (!address_Validator(theForm.Address, theForm.City, theForm.State, theForm.Zip_Code))
    return (false);

  if (!phone_Validator(theForm.Phone1, theForm.Phone2, theForm.Phone3))
    return (false);

  if (theForm.Work_Phone1.value != "" || theForm.Work_Phone2.value != "" || theForm.Work_Phone3.value!= "" || theForm.Work_Ext.value!= "")
    if (!phone_Validator(theForm.Work_Phone1, theForm.Work_Phone2, theForm.Work_Phone3))
      return (false);

  if (!ext_Format.test(theForm.Work_Ext.value))
  {
    alert("Please enter a valid value for the \"Extension\" field.");
    theForm.Work_Ext.focus();
    return (false);
  }

  if (!email_Format.test(theForm.Email.value))
  {
    alert("Please enter a valid value for the \"Email\" field. (Leave blank if you do not have an email account)");
    theForm.Email.focus();
    return (false);
  }

  return (true);
}



/* Health Form Validator */

function claim(howSend)
{
  var how = howSend;
  var process = window.document.Health_Form.Process;
  var send = window.document.Health_Form.Send;
  for (i=0;i<how.length;i++)
    if (how[i].checked)
    {
      if (how[i].value == "Regular")
        for (j=0;j<process.length;j++)
          if (process[j].checked)
            process[j].checked = false;

      if (how[i].value == "Expediate")
        process[0].checked = true;

      if (how[i].value == "Fax")
        for (j=0;j<send.length;j++)
          if (send[j].value == "Expediate")
            send[j].checked = true;

      if (how[i].value == "Pick_Up")
        for (j=0;j<send.length;j++)
          if (send[j].value == "Expediate")
            send[j].checked = true;

      if (how[i].value == "Mail")
        for (j=0;j<send.length;j++)
          if (send[j].value == "Expediate")
            send[j].checked = true;
    }
}

function goodLUHN(cardNumber)
{
  var sum = 0;
  var alt = false;
  var digits = new Array();
  for (var j = 0; j < cardNumber.length; j++ )
  {
    digits[j] = parseInt(cardNumber.substring(j, j + 1), 10);
  }
  for (var i = digits.length - 1; i >= 0; i--)
  {
    if (alt)
    {
      digits[i] *= 2;
      if (digits[i] > 9)
      {
        digits[i] -= 9; // equivalent to adding the value of digits
      }
    }
    sum += digits[i];
    alt = !alt;
  }
  return (sum % 10) == 0;
}

function Health_Form_Validator(theForm)
{
  if (!not_Blank(theForm.Card_Holder))
    return (false);

  if (!goodLUHN(theForm.Card_Number.value) || !((theForm.Card[0].checked && visa_Format.test(theForm.Card_Number.value)) || (theForm.Card[1].checked && mc_Format.test(theForm.Card_Number.value))))
  {
    alert("Please enter a valid value for the \"Card Number\" field.");
    theForm.Card_Number.focus();
    return (false);
  }

  if (theForm.Exp_Month.selectedIndex == 0)
  {
    alert("The first \"Expiration Date Month\" option is not a valid selection.  Please choose one of the other options.");
    theForm.Exp_Month.focus();
    return (false);
  }

  if (theForm.Exp_Year.selectedIndex == 0)
  {
    alert("The first \"Expiration Date Year\" option is not a valid selection.  Please choose one of the other options.");
    theForm.Exp_Year.focus();
    return (false);
  }

  if (theForm.Exp_Year.value == thisYear2 && theForm.Exp_Month.value < thisMonth)
  {
    alert("Please enter a valid experation date.");
    theForm.Exp_Month.focus();
    return (false);
  }

  if (theForm.Process[0].checked == true || theForm.Fax1.value != "" || theForm.Fax2.value != "" || theForm.Fax3.value!= "" || theForm.Attn.value!= "")
  {
    if (!phone_Validator(theForm.Fax1, theForm.Fax2, theForm.Fax3))
      return (false);

    if (!not_Blank(theForm.Attn))
      return (false);
  }

  if (!not_Blank(theForm.Childs_Name))
    return (false);

  if (!date_Validator(theForm.DOB_Month, theForm.DOB_Date, theForm.DOB_Year))
    return (false);

  if (theForm.Second_Childs_Name.value != "" || theForm.Second_DOB_Month.selectedIndex != 0 || theForm.Second_DOB_Date.selectedIndex != 0 || theForm.Second_DOB_Year.selectedIndex != 0)
  {
    if (!not_Blank(theForm.Second_Childs_Name))
      return (false);

    if (!date_Validator(theForm.Second_DOB_Month, theForm.Second_DOB_Date, theForm.Second_DOB_Year))
      return (false);
  }

  if (!not_Blank(theForm.Your_Name))
    return (false);

  if (!address_Validator(theForm.Address, theForm.City, theForm.State, theForm.Zip_Code))
    return (false);

  if (!phone_Validator(theForm.Phone1, theForm.Phone2, theForm.Phone3))
    return (false);

  if (theForm.Work_Phone1.value != "" || theForm.Work_Phone2.value != "" || theForm.Work_Phone3.value!= "" || theForm.Work_Ext.value!= "")
    if (!phone_Validator(theForm.Work_Phone1, theForm.Work_Phone2, theForm.Work_Phone3))
      return (false);

  if (!ext_Format.test(theForm.Work_Ext.value))
  {
    alert("Please enter a valid value for the \"Extension\" field.");
    theForm.Work_Ext.focus();
    return (false);
  }

  if (!email_Format.test(theForm.Email.value))
  {
    alert("Please enter a valid value for the \"Email\" field. (Leave blank if you do not have an email account)");
    theForm.Email.focus();
    return (false);
  }

  return (true);
}



/* Refferal Form Validator */

function Referral_Validator(theForm)
{
  if (!not_Blank(theForm.Childs_Name))
    return (false);

  if (!date_Validator(theForm.DOB_Month, theForm.DOB_Date, theForm.DOB_Year))
    return (false);

  if (!not_Blank(theForm.Your_Name))
    return (false);

  if (!phone_Validator(theForm.Phone1, theForm.Phone2, theForm.Phone3))
    return (false);

  if (theForm.Work_Phone1.value != "" || theForm.Work_Phone2.value != "" || theForm.Work_Phone3.value!= "" || theForm.Work_Ext.value!= "")
    if (!phone_Validator(theForm.Work_Phone1, theForm.Work_Phone2, theForm.Work_Phone3))
      return (false);

  if (!ext_Format.test(theForm.Work_Ext.value))
  {
    alert("Please enter a valid value for the \"Extension\" field.");
    theForm.Work_Ext.focus();
    return (false);
  }

  if (!email_Format.test(theForm.Email.value))
  {
    alert("Please enter a valid value for the \"Email\" field. (Leave blank if you do not have an email account)");
    theForm.Email.focus();
    return (false);
  }

  if (!not_Blank(theForm.Insurance))
    return (false);

  if (!not_Blank(theForm.Insurance_ID))
    return (false);

  if (!date_Validator(theForm.App_Month, theForm.App_Date, theForm.App_Year))
    return (false);

  if (!not_Blank(theForm.Reason))
    return (false);

  if (!not_Blank(theForm.Specialist_Name))
    return (false);

  if (!not_Blank(theForm.Provider_ID))
    return (false);

  if (!not_Blank(theForm.Provider_NPI))
    return (false);

  if (!not_Blank(theForm.Hospital))
    return (false);

  if (!address_Validator(theForm.Address, theForm.City, theForm.State, theForm.Zip_Code))
    return (false);

  if (!phone_Validator(theForm.Spec_Phone1, theForm.Spec_Phone2, theForm.Spec_Phone3))
    return (false);

  if (!ext_Format.test(theForm.Spec_Ext.value))
  {
    alert("Please enter a valid value for the \"Extension\" field.");
    theForm.Spec_Ext.focus();
    return (false);
  }

  if (!phone_Validator(theForm.Fax1, theForm.Fax2, theForm.Fax3))
    return (false);

  if (theForm.PCP.selectedIndex == 0)
  {
    alert("The first \"Name of PCP\" option is not a valid selection.  Please choose one of the other options.");
    theForm.PCP.focus();
    return (false);
  }

  return (true);
}



/* Display Form Names */

function blah(form){
  win2=window.open(""); //open blank window and write to it
  win2.document.open(); //open document stream
  for(i=0; i<form.length; i++)
    win2.document.write(form.elements[i].name + "<br>");
  win2.document.close();
}
