function toggleVerifyField(options)
{
  if (options[options.selectedIndex].value == "secureId")
  {
    loginForm.secureId.style.display = "";
    loginForm.fingerprintImg.style.display = "none";
  } else if (options[options.selectedIndex].value == "fingerprint")
  {
    loginForm.secureId.style.display = "none";
    loginForm.fingerprintImg.style.display = "";
  }
}

function validateLoginForm()
{
  if (loginForm.loginId.value.length > 40 ||
      loginForm.loginId.value.length == 0)
  {
    alert("Please enter at most 40 characters in the \"Login ID\" field.");
    loginForm.loginId.focus();
    return false;
  }

  if (loginForm.password.value.length > 20 ||
      loginForm.password.value.length == 0)
  {
    alert("Please enter at most 20 characters in the \"Password\" field.");
    loginForm.password.focus();
    return false;
  }

  if (loginForm.verify.options[loginForm.verify.options.selectedIndex].value == "secureId")
  {
    if (loginForm.secureId.value.length > 20 ||
        loginForm.secureId.value.length == 0)
    {
      alert("Please enter at most 20 characters in the \"Secure ID\" field.");
      loginForm.secureId.focus();
      return false;
    }
  }

  return true;
}

function selectFunctionPage(options)
{
  if (options[options.selectedIndex].value != "")
    window.location.href = options[options.selectedIndex].value;
}

function toggleCasOptions(state)
{
	if (state)
	{
		tCasOptions.style.display = "";
		fCasOptions.style.display = "none";
	} else
	{
		tCasOptions.style.display = "none";
		fCasOptions.style.display = "";
	}
}

function fingerPrint(theForm)
{
  theForm.ptfname.value = "William";
  theForm.ptlname.value ="Shakespeare";
  theForm.ptdob.value ="4/23/1564";
  theForm.ptssn.value ="123-45-6789";
  theForm.ptzip.value ="CV37 6QW";
}

function validatePtForm(theForm)
{
  if (theForm.ptfname.value.length > 30 ||
      theForm.ptfname.value.length == 0)
  {
    alert("Please enter a value in the \"Patient First Name\" field.");
    theForm.ptfname.focus();
    return false;
  }

  if (theForm.ptlname.value.length > 30 ||
      theForm.ptlname.value.length == 0)
  {
    alert("Please enter a value in the \"Patient Last Name\" field.");
    theForm.ptlname.focus();
    return false;
  }

  if (theForm.ptdob.value.length > 30 ||
      theForm.ptdob.value.length == 0)
  {
    alert("Please enter a value in the \"Patient DOB\" field.");
    theForm.ptdob.focus();
    return false;
  }

  if (theForm.ptssn.value.length > 11 ||
      theForm.ptssn.value.length == 0)
  {
    alert("Please enter a value in the \"Patient SSN\" field.");
    theForm.ptssn.focus();
    return false;
  }

  if (theForm.ptzip.value.length > 10 ||
      theForm.ptzip.value.length == 0)
  {
    alert("Please enter a value in the \"Patient Zip/Postal Code\" field.");
    theForm.ptzip.focus();
    return false;
  }

  return true;
}

function clearPtForm(theForm)
{
  for (i = 0; i < theForm.elements.length; i++)
  {
    if (theForm.elements[i].type == "text")
    {
      theForm.elements[i].value = "";
    }
  }
}

function validateLMRPForm(lmrpForm)
{
  // check patient info first
  if (!validatePtForm(lmrpForm))
    return false;

  // now check specific fields

  if (lmrpForm.dos.value.length > 20 ||
      lmrpForm.dos.value.length == 0)
  {
    alert("Please enter at date in the \"Date of Service\" field.");
    lmrpForm.dos.focus();
    return false;
  }

  if (lmrpForm.provider.value.length > 30 ||
      lmrpForm.provider.value.length == 0)
  {
    alert("Please enter a name in the \"Provider\" field.");
    lmrpForm.provider.focus();
    return false;
  }

  if (lmrpForm.icd.options[lmrpForm.icd.options.selectedIndex].value == "")
  {
    alert("Please select a valid ICD value.");
    lmrpForm.icd.focus();
    return false;
  }

  if (lmrpForm.cpt.options[lmrpForm.cpt.options.selectedIndex].value == "")
  {
    alert("Please select a valid CPT value.");
    lmrpForm.cpt.focus();
    return false;
  }

  return true;
}

function clearLMRPForm(lmrpForm)
{
  // clear patient info first
  clearPtForm(lmrpForm);

  // now clear specific fields 
//  lmrpForm.provider.value = "";
  lmrpForm.icd.options.selectedIndex = 0;
  lmrpForm.cpt.options.selectedIndex = 0;
}

function validateMDForm(mdForm)
{
  // check patient info first
  if (!validatePtForm(mdForm))
    return false;

  // now check specific fields

  if (mdForm.dos.value.length > 20 ||
      mdForm.dos.value.length == 0)
  {
    alert("Please enter at date in the \"Date of Service\" field.");
    mdForm.dos.focus();
    return false;
  }

  if (mdForm.provider.value.length > 30 ||
      mdForm.provider.value.length == 0)
  {
    alert("Please enter a name in the \"Provider\" field.");
    mdForm.provider.focus();
    return false;
  }

  if (mdForm.providernum.value.length > 30 ||
      mdForm.providernum.value.length == 0)
  {
    alert("Please enter a name in the \"Provider Number\" field.");
    mdForm.providernum.focus();
    return false;
  }

  if (mdForm.icd.options[mdForm.icd.options.selectedIndex].value == "")
  {
    alert("Please select a valid ICD value.");
    mdForm.icd.focus();
    return false;
  }

  if (mdForm.cpt.options[mdForm.cpt.options.selectedIndex].value == "")
  {
    alert("Please select a valid CPT value.");
    mdForm.cpt.focus();
    return false;
  }

  return true;
}

function clearMDForm(mdForm)
{
  // clear patient info first
  clearPtForm(mdForm);

  // now clear specific fields 
  mdForm.icd.options.selectedIndex = 0;
  mdForm.cpt.options.selectedIndex = 0;
}

function toggleMsgCfg()
{
  cg_msg.style.display = "none";
  sw_msg.style.display = "none";
  if (msgForm.msgDest.options[msgForm.msgDest.options.selectedIndex].value == "sw")
  {
    sw_msg.style.display = "";
  } else if (msgForm.msgDest.options[msgForm.msgDest.options.selectedIndex].value == "cg")
  {
    cg_msg.style.display = "";
  }
}

function toggleMsgConf()
{
  if (msgForm.msgDest.options[msgForm.msgDest.options.selectedIndex].value != "")
  {
    msgConf.style.display = "";
  }
}

function validate837Form(_837Form)
{
  // check patient info first
  if (!validatePtForm(_837Form))
    return false;

  // now check specific fields
  if (_837Form.provider.value.length > 30 ||
      _837Form.provider.value.length == 0)
  {
    alert("Please enter a name in the \"Provider\" field.");
    _837Form.provider.focus();
    return false;
  }

  if (_837Form.providernum.value.length > 30 ||
      _837Form.providernum.value.length == 0)
  {
    alert("Please enter a name in the \"Provider Number\" field.");
    _837Form.providernum.focus();
    return false;
  }

  return true;
}

function clear837Form(_837Form)
{
  // clear patient info first
  clearPtForm(_837Form);

  // now clear specific fields 
//  _837Form.provider.value = "";
//  _837Form.providernum.value = "";
}

function validateBalForm(balForm)
{
  // check patient info first
  if (!validatePtForm(balForm))
    return false;

  // now check specific fields

  return true;
}

function clearBalForm(balForm)
{
  // clear patient info first
  clearPtForm(balForm);

  // now clear specific fields 
}

var addUserWindow;
function openAddUserWindow()
{
  if(addUserWindow && !addUserWindow.closed)
  {
    addUserWindow.focus();
  } else
  {
    params='toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=yes,width=500,height=500';
    addUserWindow = window.open('adduser.asp', 'add_user', params);
  }
}

var termUserWindow;
function openTermUserWindow()
{
  if(termUserWindow && !termUserWindow.closed)
  {
    termUserWindow.focus();
  } else
  {
    // get selected user
    var user;
    if (userForm.userlist.options.selectedIndex != -1)
    {
      if (userForm.userlist.options[userForm.userlist.options.selectedIndex].value == "")
      {
        alert("Please select a user from the list.");
        return false;
      } else
        user = userForm.userlist.options[userForm.userlist.options.selectedIndex].value;
    } else
    {
        alert("Please select a user from the list");
        return false;
    }
    url="termuser.asp?user="+user;
    params='toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=yes,width=500,height=500';
    termUserWindow = window.open(url, 'term_user', params);
  }
}

window.onerror = myOnErrorFnc
function myOnErrorFnc(msg, url, lno)
{
  alert("SCRIPT ERROR\n\nurl: "+url+"\nline: "+lno+"\nmsg: "+msg);
}

function addUserOk()
{
  // validate fields
  if (newUserForm.newLoginId.value.length > 40 ||
      newUserForm.newLoginId.value.length == 0)
  {
    alert("Please enter at most 40 characters in the \"User Name\" field.");
    newUserForm.newLoginId.focus();
    return false;
  }

  if (newUserForm.newPassword.value.length > 40 ||
      newUserForm.newPassword.value.length == 0)
  {
    alert("Please enter at most 40 characters in the \"Password\" field.");
    newUserForm.newPassword.focus();
    return false;
  }

  if (newUserForm.newSecureId.value.length > 40 ||
      newUserForm.newSecureId.value.length == 0)
  {
    alert("Please enter at most 40 characters in the \"Secure ID Unit No.\" field.");
    newUserForm.newSecureId.focus();
    return false;
  }

  if (newUserForm.secureId.value.length > 40 ||
      newUserForm.secureId.value.length == 0)
  {
    alert("Please enter at most 40 characters in the \"Secure ID\" field.");
    newUserForm.secureId.focus();
    return false;
  }

  // add new user to select object
  var userForm = window.opener.document.userForm;

// "javascript way" - doesn't seem to work...
//  var newUser = new Option(newUserForm.newLoginId.value, newUserForm.newLoginId.value);
//  var i = userForm.userlist.options.length+1;
//  userForm.userlist.options[i] = newUser;


// "ms script way" - doesn't seem to work either...
//  var oOption = document.createElement("OPTION");
//  oOption.innerText = newUserForm.newLoginId.value;
//  oOption.value = newUserForm.newLoginId.value;
//  userForm.userlist.options.add(oOption);

  // final disclaimer
  alert("By executing this enablement order, you settle upon the party, "+
        "with the above mentioned three identifiers, the ability to transmit "+
        "messages on behalf of your organization.  In doing so, you and/or "+
        "may attach liability for the training and supervision of this "+
        "person\'s activity in the medical electronic commerce system.");
  window.close();
  return true;
}

function termUserOk()
{
  // validate fields
  if (termUserForm.termPassword.value.length > 40 ||
      termUserForm.termPassword.value.length == 0)
  {
    alert("Please enter at most 40 characters in the \"Password\" field.");
    termUserForm.termPassword.focus();
    return false;
  }

  if (termUserForm.termSecureId.value.length > 40 ||
      termUserForm.termSecureId.value.length == 0)
  {
    alert("Please enter at most 40 characters in the \"Secure ID Unit No.\" field.");
    termUserForm.termSecureId.focus();
    return false;
  }

  if (termUserForm.secureId.value.length > 40 ||
      termUserForm.secureId.value.length == 0)
  {
    alert("Please enter at most 40 characters in the \"Secure ID\" field.");
    termUserForm.secureId.focus();
    return false;
  }

  // final disclaimer
  if(confirm("By executing this Termination order, you will permanently "+
             "disable the access this person currently has to transact "+
             "business for your organization on the healthcare finance "+
             "electronic commerce system.  Such authority will require "+
             "the re-registration of this person should you terminate the "+
             "wrong person or otherwise reverse this decision.  Please "+
             "verify that you are terminating the right person."))
  {
    window.close();
    return true;
  } else 
    return false;
}

function toggleTermOptions(which)
{
  if (which == "now")
    termUserForm.termdate.disabled = true;
  else if (which == "later")
    termUserForm.termdate.disabled = false;
}

function showICDCPTLabel(options, label)
{
  label.value = options[options.selectedIndex].value;
}

function showPayerNum(options, label)
{
  label.value = options[options.selectedIndex].value;
}

function showCPTRate(options, label)
{
  if (options[options.selectedIndex].value == "")
    label.value = "";
  else
    label.value = "$"+options[options.selectedIndex].value;
}

function verifyDrSel(theForm)
{
  var checked = false;
  for (i = 0; i < theForm.elements.length; i++)
  {
    if (theForm.elements[i].type == "checkbox")
    {
      if (theForm.elements[i].checked == true)
      {
        checked = true;
        break;
      }
    }
  }

  if (!checked)
  {
    alert("Please select at least one Doctor from the list.");
    return false;
  }
  return true;
}

var editDrWindow;
function openEditDrWindow(theForm)
{
  if(editDrWindow && !editDrWindow.closed)
  {
    editDrWindow.focus();
  } else
  {
    var url="editdr.asp?";

    // get selected drs and build url
    var n = 1;
    for (i = 0; i < theForm.elements.length; i++)
    {
      if (theForm.elements[i].type == "checkbox")
      {
        if (theForm.elements[i].checked == true)
        {
          if (n>1) url+="&";
          url += "dr="+theForm.elements[i].value;
          n++;
        }
      }
    }
    params='toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=yes,width=700,height=300';
    editDrWindow = window.open(url, 'edit_dr', params);
  }
}

function verifyHospSel(theForm)
{
  var checked = false;
  for (i = 0; i < theForm.elements.length; i++)
  {
    if (theForm.elements[i].type == "checkbox")
    {
      if (theForm.elements[i].checked == true)
      {
        checked = true;
        break;
      }
    }
  }

  if (!checked)
  {
    alert("Please select at least one Hospital Department from the list.");
    return false;
  }
  return true;
}

var editHospWindow;
function openEditHospWindow(theForm)
{
  if(editHospWindow && !editHospWindow.closed)
  {
    editHospWindow.focus();
  } else
  {
    var url="edithosp.asp?";

    // get selected hospitals and build url
    var n = 1;
    for (i = 0; i < theForm.elements.length; i++)
    {
      if (theForm.elements[i].type == "checkbox")
      {
        if (theForm.elements[i].checked == true)
        {
          if (n>1) url+="&";
          url += "hosp="+theForm.elements[i].value;
          n++;
        }
      }
    }
    params='toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=yes,width=700,height=300';
    editHospWindow = window.open(url, 'edit_hosp', params);
  }
}

function lookupPtHistory()
{
  if (ptLookupForm.ptNameText.value != "")
    ptNameField.innerText = ptLookupForm.ptNameText.value;
  ptHistory.style.display = "";
}

