function contact_check_form() {

var error_msg = "The following error(s) occurred:\n\n";
var error_exists = 0;
var warn_msg = "The following warning(s) occurred:\n\n";
var warn_exists = 0;

if (trim(document.frm_ctc.txt_name.value) == "") {
  error_exists = 1;
  error_msg = error_msg + "Name is required.\n";
}

if (trim(document.frm_ctc.txt_email.value) == "") {
  error_exists = 1;
  error_msg = error_msg + "Email address is required.\n";
}

if (trim(document.frm_ctc.txt_phone.value) == "") {
  error_exists = 1;
  error_msg = error_msg + "Phone is required.\n";
}

if (warn_exists && error_exists) {
  alert(warn_msg);
  alert(error_msg);
  return false;
}
else if (warn_exists && !error_exists) {
  alert(warn_msg);
  return true;
}
else if (!warn_exists && error_exists) {
  alert(error_msg);
  return false;
}

return true;

}

function contact_sendmail() {

if (contact_check_form()) {
  document.frm_ctc.action = "http://www.ctcins.com/contact.php";
  document.frm_ctc.submit();
  return true;
}
else {
  return false;
}

}

function enews_check_email() {

var error_msg = "The following error(s) occurred:\n\n";
var error_exists = 0;
var warn_msg = "The following warning(s) occurred:\n\n";
var warn_exists = 0;

if (trim(document.frm_ctc.txt_email.value) == "") {
  error_exists = 1;
  error_msg = error_msg + "You must enter an email address.\n";
}
else {
  email = document.frm_ctc.txt_email.value
  AtPos = email.indexOf("@")
  StopPos = email.lastIndexOf(".")

  if (AtPos == -1 || StopPos == -1) {
    error_exists = 1;
    error_msg = error_msg + "Not a valid email address.\n";
  }  

  if (StopPos < AtPos) {
    error_exists = 1;
    error_msg = error_msg + "Not a valid email address.\n";
  }

  if (StopPos - AtPos == 1) {
    error_exists = 1;
    error_msg = error_msg + "Not a valid email address.\n";
  } 
}

if (warn_exists && error_exists) {
  alert(warn_msg);
  alert(error_msg);
  return false;
}
else if (warn_exists && !error_exists) {
  alert(warn_msg);
  return true;
}
else if (!warn_exists && error_exists) {
  alert(error_msg);
  return false;
}

return true;

}

function enews_join() {

if (enews_check_email()) {
  document.frm_ctc.action = "http://www.ctcins.com/enewsjoin.php";
  document.frm_ctc.submit();
  return true;
}
else {
  return false;
}

}

function trim(s)
{
    return rtrim(ltrim(s));
}

function ltrim(s)
{
    var l=0;
    while(l < s.length && s[l] == ' ')
    {    l++; }
    return s.substring(l, s.length);
}

function rtrim(s)
{
    var r=s.length -1;
    while(r > 0 && s[r] == ' ')
    {    r-=1;    }
    return s.substring(0, r+1);
}
