function popupFax(form_url, external)
{
	if (external === undefined) {
		external = false;
	}
	
	if (!external) {
		url = root + 'Fax?form=' + form_url;
	} else {
		url = form_url;
	}
	
	window.open(url, 'fax_window', 'status=0,toolbar=0' +
	',location=0,menubar=0,directories=0,resizable=1,scrollbars=1,' +
	'width=788,height=400');
}

// for physician letter
function popupLetterFax()
{
	window.open(root + 'Fax/PhysicianLetter', 'fax_window', 'status=0,toolbar=0' +
	',location=0,menubar=0,directories=0,resizable=1,scrollbars=1,' +
	'width=788,height=400');
}

function popupRedeterminationFax(drug_name)
{
   drug_name = encodeURIComponent(drug_name);
	window.open(root + 'pharmacy/RedeterminationForm?drug=' + drug_name, 'fax_window', 'status=0,toolbar=0' +
	',location=0,menubar=0,directories=0,resizable=1,scrollbars=1,' +
	'width=795,height=425');
}

function checkFaxNumber(field_id, form_id)
{
	field = document.getElementById(field_id);
	re = /^\d{3}([-])?\d{3}([-])?\d{4}$/;
  	  	
  	if (re.test(field.value)) {
  		submit_form(form_id);
  	} else {
  		alert('Please enter a ten digit telephone number, with or without dashes (-).');
  		return false;
  	}
}

function checkFaxNumberWithVerification(field_id, form_id)
{
	field = document.getElementById(field_id);
	verified = document.getElementById('verified');
	re = /^\d{3}([-])?\d{3}([-])?\d{4}$/;
  	msg = 'The following errors were encountered:\n'; 
  	errors = false;
  	  	
  	if (!re.test(field.value)) {
  		msg += 'Please enter a ten digit telephone number, with or without dashes (-).\n';
  		errors = true;
  	}
  	
  	if (!verified.checked) {
  	    msg += 'Please verify that the fax number you entered is correct, and check the box.\n';
  	    errors = true;
    }
  	
  	if (errors) {
  	    alert(msg);
  	} else {
  	    submit_form(form_id);
  	}
  	
  	return !errors;
}

function checkFaxNumberAndUpdate(field_id, form_id, provider_id)
{
	field = document.getElementById(field_id);
	re = /^\d{3}([-])?\d{3}([-])?\d{4}$/;

  	if (re.test(field.value)) {
  		load(root + 'Ajax/UpdatePhysicianFax?fax=' + field.value + '&provider=' + provider_id);
  		submit_form(form_id);
  	} else {
  		alert('Please enter a ten digit telephone number, with or without dashes (-).');
  		return false;
  	}
}

function validate_redetermination_form()
{
   patient_name = document.getElementById('patient_name');
   ccrx_id = document.getElementById('ccrx_id');
   dob = document.getElementById('dob');
   speed_standard = document.getElementById('speed_standard');
   speed_expedited = document.getElementById('speed_expedited');
   requested_by = document.getElementById('requested_by');
   date_received = document.getElementById('date_received');
   csr_name = document.getElementById('csr_name');
   information = document.getElementById('information');
   
   errors = false;
   errorMsg = "Please fix the following errors:\n";
   
   if (patient_name.value == '')
   {
      errors = true;
      errorMsg += "* Please fill in the patient's name.\n";
   }

   if (ccrx_id.value == '')
   {
      errors = true;
      errorMsg += "* Please fill in the patient's CCRx ID number.\n";
   }
   
   if (dob.value == '')
   {
      errors = true;
      errorMsg += "* Please fill in the patient's date of birth.\n";
   }
   
   if (!speed_expedited.checked && !speed_standard.checked)
   {
      errors = true;
      errorMsg += "* Please select either Expedited or Standard speed.\n";
   }
   
   if (requested_by.value == '')
   {
      errors = true;
      errorMsg += "* Please indicate who requested the re-determination.\n";
   }
   
   if (date_received.value == '')
   {
      errors = true;
      errorMsg += "* Please fill in the date and time received.\n";
   }
   
   if (csr_name.value == '')
   {
      errors = true;
      errorMsg += "* Please enter your name in the CCRx CSR name field.\n";
   }
   
   if (information.value == '')
   {
      errors = true;
      errorMsg += "* Please document the enrolee's request (in their words).\n";
   }
   
   if (errors) {
      alert(errorMsg);
   }
   
   return !errors;
}

function submitRedeterminationPDF()
{
   if (!validate_redetermination_form())
   {
      return false;
   }
   
   form = document.getElementById('redetermination_form');
   form.action = 'RedeterminationFormSubmit';
   submit_form('redetermination_form');
}

function submitRedeterminationFax()
{
   if (!validate_redetermination_form())
   {
      return false;
   }
   
   form = document.getElementById('redetermination_form');
   form.action = 'RedeterminationFormFax';
   submit_form('redetermination_form');
}
