function encp(zsPackage) 
{
  var work = zsPackage;
  var adj = work.length % 3;
  if (adj != 0)
  {
    for (var indx = 0; indx < (3 - adj); indx++) { work = work + ' '; }
  }
  var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var out = "", c1, c2, c3, e1, e2, e3, e4;
  for (var i = 0; i < work.length; ) 
  {
    c1 = work.charCodeAt(i++);
    c2 = work.charCodeAt(i++);
    c3 = work.charCodeAt(i++);
    e1 = c1 >> 2;
    e2 = ((c1 & 3) << 4) + (c2 >> 4);
    e3 = ((c2 & 15) << 2) + (c3 >> 6);
    e4 = c3 & 63;
    if (isNaN(c2)) { e3 = e4 = 64; }
    else if (isNaN(c3)) { e4 = 64; }
    out += tab.charAt(e1) + tab.charAt(e2) + tab.charAt(e3) + tab.charAt(e4);
  }
  return out;
}

function aq2eTSendRequestInfo(zsaq2eTPath, zsrequestinfo)
{
  location.href = zsaq2eTPath + '?aq2eT=' + zsrequestinfo;
}

function aq2eTPackageRequest(zsaq2eTPath)
{
try
{

  var Work = location.href.toLowerCase();
  Work = Work.replace('http://', '');
  Work = Work.replace('https://', '');
  Work = Work.replace('www.', '');
  var indx = Work.indexOf('/');
  if (indx > 0) { Work = Work.substr(0, indx); }
  
  var oform = document.forms[0];
  var package = Work + '|' +
                oform.aq2eTCoverage.options[oform.aq2eTCoverage.selectedIndex].value + '|' +
                oform.aq2eTTerm.options[oform.aq2eTTerm.selectedIndex].value + '|' +
                oform.aq2eTDbMonth.options[oform.aq2eTDbMonth.selectedIndex].value + '|' +
                oform.aq2eTDbDom.options[oform.aq2eTDbDom.selectedIndex].value + '|' +
                oform.aq2eTDbYr.options[oform.aq2eTDbYr.selectedIndex].value + '|' + 
                oform.aq2eTState.options[oform.aq2eTState.selectedIndex].value + '|' +
                oform.aq2eTPremiumPeriod.options[oform.aq2eTPremiumPeriod.selectedIndex].value + '|';
  
  if (oform.aq2eTGender[0].checked) { package = package + 'M|'; }
  else { package = package + 'F|'; }

  if (oform.aq2eTSmoker[0].checked) { package = package + 'N|'; }
  else { package = package + 'Y|'; }
  
//** Lead capture
  var lmnt = document.getElementById('aq2eTHolc');
  if (lmnt != null)
  {
	if (zoform.aq2eTHolc.value == 'Y')
    {
	  var FirstName = zoform.aq2eTFirstName.value;
	  var LastName = zoform.aq2eTLastName.value;
	  var EMailAddress = zoform.aq2eTEmail.value;
	  package = package + FirstName.replace(/|/g, '') + '|' +
	            zoform.LastName.replace(/|/g, '') + '|' +
				zoform.EMailAddress.replace(/|/g, '') + '|' +
				zoform.aq2eTPhac.value + '|' +
				zoform.aq2eTPhpx.value + '|' +
				zoform.aq2eTPhsn.value + '|';
	}
  }
  
  aq2eTSendRequestInfo(zsaq2eTPath, encp(package));
}
catch (error)
{
alert('aq2eTPackageRequest: ' + error);
}
}

function aq2eTPrintcheck()
{
  var prompt = 'To print the full form:\n' +
               ' 1. Click anywhere inside the form\n' +
	           ' 2. Press both the CTRL + A keyboard keys together\n' +
	           ' 3. Open the web browser\'s Print-Preview screen\n' +
	           ' 4. Select the [As Selected On Screen] and [Shrink To Fit] options if available\n' +
	           ' 5. If all of the form is in the Preview then click the [Print] button';
  alert(prompt);			  
}

function aq2eTOnViewChange()
{
  switch (document.forms[0].hmode.value)
  {
    case '0':
	  document.forms[0].hmode.value = 2;
	  break;

    case '3':
	  document.forms[0].hmode.value = 4;
	  break;

    case '5':
	  document.forms[0].hmode.value = 6;
	  break;
  }
  document.forms[0].submit();	
}

function aq2eTViewPolicyDetails(zpolicyid)
{
  document.forms[0].hpolicyid.value = zpolicyid;
  document.forms[0].hmode.value = 3;
  document.forms[0].submit();
  return false;
}

function aq2eTViewUnderwritingGuidelines(zpolicyid)
{
  document.forms[0].hpolicyid.value = zpolicyid;
  document.forms[0].hmode.value = 5;
  document.forms[0].submit();
  return false;
}

function aq2eTRecalculate()
{
  document.forms[0].hmode.value = 10;
  document.forms[0].submit();
  return false;
}

function aq2eTAppRequestClick(zscompanyName, zspolicyName, zsType, zpolicyid)
{
  document.forms[0].hpolicyid.value = zpolicyid;
  document.forms[0].hcompany.value = zscompanyName;
  document.forms[0].hpolicy.value = zspolicyName;
  switch (zsType)
  {
    case "0":  // Long form
      document.forms[0].hmode.value = 12;
      break;
    case '1':  // Short form only
	  document.forms[0].hmode.value = 11;
	  break;
    case '2':  // Short form with long form option
	  document.forms[0].hmode.value = 11;
	  break;
  }
  document.forms[0].submit();
  return false;
}

function aq2eTValidateLeadEntries(zoform)
{
  try
  {
    if (zoform.aq2eTFirstName.value.length < 1)
    {
	  alert('Please enter your First Name');
	  zoform.aq2eTFirstName.focus();
	  return false;
    }
	
    if (zoform.aq2eTLastName.value.length < 1)
    {
	  alert('Please enter your Last Name');
	  zoform.aq2eTLastName.focus();
	  return false;
    }

    if (zoform.aq2eTEmail.value.length > 0)
    {
	  var atsym = zoform.aq2eTEmail.value.indexOf('@');
	  var cmsym = zoform.aq2eTEmail.value.lastIndexOf('.');
	  if ((atsym < 1) || (cmsym <= atsym) || ((cmsym + 2) >= zoform.aq2eTEmail.value.length) || (zoform.aq2eTEmail.value.indexOf(' ') > -1))
	  {
	    alert('Please check your Email Address for accuracy');
	    zoform.aq2eTEmail.focus();
	    return false;
	  } 
    }

    if ((zoform.aq2eTPhac.value.length > 0) || (zoform.aq2eTPhpx.value.length > 0) || (zoform.aq2eTPhsn.value.length > 0))
    {
	  if ((zoform.aq2eTPhac.value.length != 3) || (zoform.aq2eTPhpx.value.length != 3) || (zoform.aq2eTPhsn.value.length != 4) || (isNaN(zoform.aq2eTPhac.value)) || (isNaN(zoform.aq2eTPhpx.value)) || (isNaN(zoform.aq2eTPhsn.value)))
	  {
	    alert('Please check your Phone Number for accuracy');
	    zoform.aq2eTPhac.focus();
	    return false;
	  }
    }
	
    if ((zoform.aq2eTPhac.value.length == 0) && (zoform.aq2eTEmail.value.length == 0))
    { 
	  alert('Please enter an email address or phone number that we can use to contact you');
	  zoform.aq2eTEmail.focus();
	  return false;
    }
  	
    return true;
  }
  catch (error)
  {
    alert('aq2eTValidateLeadEntries: ' + error);
    return false;
  }
}

function aq2eTValidateRequest(zoform)
{
  try
  {
    if (zoform.aq2eTCoverage.selectedIndex == 0)
    {
      alert('Please select a Benefit Amount');
  	  zoform.aq2eTCoverage.focus();
	  return false;
    }

    if (zoform.aq2eTTerm.selectedIndex == 0)
    {
      alert('Please select a Term Length');
	  zoform.aq2eTTerm.focus();
	  return false;
    }

    if (zoform.aq2eTDbMonth.selectedIndex == 0)
    {
      alert('Please select a Birth Month');
	  zoform.aq2eTDbMonth.focus();
	  return false;
    }

    if (zoform.aq2eTDbDom.selectedIndex == 0)
    {
      alert('Please select a Birth Day-of-Month');
	  zoform.aq2eTDbDom.focus();
	  return false;
    }

    if (zoform.aq2eTDbYr.selectedIndex == 0)
    {
      alert('Please select a Birth Year');
	  zoform.aq2eTDbYr.focus();
	  return false;
    }

    if (zoform.aq2eTState.options[zoform.aq2eTState.selectedIndex].value.length < 1)
    {
      alert('Please select a State');
	  zoform.aq2eTState.focus();
	  return false;
    }

    if (zoform.aq2eTPremiumPeriod.options[zoform.aq2eTPremiumPeriod.selectedIndex].value.length < 1)
    {
      alert('Please select a Premium Payment period');
	  zoform.aq2eTPremiumPeriod.focus();
	  return false;
    }

    if ((!zoform.aq2eTGender[0].checked) && (!zoform.aq2eTGender[1].checked))
    {
      alert('Please select a Gender');
	  return false;
    }

    if ((!zoform.aq2eTSmoker[0].checked) && (!zoform.aq2eTSmoker[1].checked))
    {
      alert('Please select if Tobacco is used');
	  return false;
    }
  
    var lmnt = document.getElementById('aq2eTHolc');
    if (lmnt != null)
	{
	  if (zoform.aq2eTHolc.value == 'Y')
      {
        if (aq2eTValidateLeadEntries(zoform)) { return true; }
		else { return false; }
	  }
    }
	else { return true; }
  }
  catch (error)
  {
    alert('aq2eTValidateRequest: ' + error);
    return false;
  }
}

function aq2eTOnPhoneKeyUp(zoentry, zonext, zilimit)
{
  if ((zoentry.value.length >= zilimit) && (zonext.value.length < 1)) { zonext.focus(); }
}

function aq2eTValidateShortApplication(zoform)
{
  if (!aq2eTValidateLeadEntries(zoform)) { return false; }
  
  if (zoform.aq2eTHomeStreet.value.length < 1)
  {
	alert('Please enter your street Address');
	zoform.aq2eTHomeStreet.focus();
	return false;
  }

  if (zoform.aq2eTHomeCity.value.length < 1)
  {
	alert('Please enter your City name');
	zoform.aq2eTHomeCity.focus();
	return false;
  }

  if (zoform.aq2eTState.value.length != 2)
  {
	alert('Please enter your State Abbreviation');
	zoform.aq2eTState.focus();
	return false;
  }

  if ((zoform.aq2eTHomeZip.value.length != 5) &&  (zoform.aq2eTHomeZip.value.length != 10))
  {
    alert("Please enter a Zipcode");
    zoform.aq2eTHomeZip.focus();
    return (false);
  }
  
  if ((zoform.aq2eTHomeZip.value.length == 5) && (isNaN(zoform.aq2eTHomeZip.value)))
  {
    alert("Please enter a zip code number");
    zoform.aq2eTHomeZip.focus();
    return (false);
  }

  return true;  
}

function aq2eTOnSelectFocus(zoselect, zindx)
{

  if ((zoselect.length == 1) || (zoselect.selectedIndex != 0)) { return; }
/*
  if (zindx == 0)
  {
    if (zoselect.length <= 13) { zoselect.size = zoselect.length; }
	else { zoselect.size = 13 }
  }
  else
  {
    if (zoselect.length <= zindx) { zoselect.size=zoselect.length; }
	else { zoselect.size = zindx }
  }

  zoselect.click();
*/
}

function aq2eTOnSelectBlur(zoselect, zonext)
{
  if (zoselect.length == 1) { return; }
  
//  zoselect.size = 0;
  if (zonext != null) { zonext.focus(); }
}

function aq2eTOnSelectChange(zoselect, zonext)
{
//  zoselect.size=0;
  if (zonext != null) { zonext.focus(); }
}


