function getXMLHttp() {
  var xmlHttp;
  try {
    xmlHttp = new XMLHttpRequest();
  } catch(e) {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return xmlHttp;
}

function isMember() {
  var xmlHttp = getXMLHttp();
  xmlHttp.open("GET", "/ajax/ismember.php?q=" + document.getElementById('email_address').value, false);
  xmlHttp.send(null);
  if(xmlHttp.responseText=="YES") {
    return true;
  } else if(xmlHttp.responseText=="RENEW") {
    return true;
  } else {
    return false;
  } 
}

function isMemberNew() {
    var xmlHttp = getXMLHttp();
    xmlHttp.open("GET", "/ajax/ismember_new.php?q=" + document.getElementById('email_address').value, false);
    xmlHttp.send(null);
    return xmlHttp.responseText;
}

function getMID() {
    var xmlHttp = getXMLHttp();
    xmlHttp.open("GET", "/ajax/getmemmid.php?q=" + document.getElementById('email_address').value, false);
    xmlHttp.send(null);
    return xmlHttp.responseText;
}

function getPatron() {
  var xmlHttp = getXMLHttp();
  xmlHttp.open("GET", "/ajax/getpatron.php?q=" + document.getElementById('email_address').value, false);
  xmlHttp.send(null);
  if(xmlHttp.responseText!="") {
    return xmlHttp.responseText;
  } else {
    return false;
  }
}

function isOnMailingList() {
  var xmlHttp = getXMLHttp();
  xmlHttp.open("GET", "/ajax/isonmailinglist.php?q=" + document.getElementById('email_address').value, false);
  xmlHttp.send(null);
  if(xmlHttp.responseText=="YES") {
    return true;
  } else {
    return false;
  } 
}

/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
**/
 
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function checkMembership() {
  if(trim(document.getElementById('email_address').value) == "") {
    alert('Please enter an e-mail address');
  } else {
    document.getElementById('input_box').style.display='none';
    if(isMember()) {
      document.getElementById('found_member').style.display='block';
    } else {
      if(getPatron()!=false) {
        document.getElementById('patron_company').innerHTML = getPatron();
        document.getElementById('not_found_member_free').style.display='block';
      } else {
        if(isOnMailingList()) {
          document.getElementById('not_found_member_but_on_list').style.display='block';
        } else {
          document.getElementById('not_found_member').style.display='block';
        }
      }
    }
  }
}

function checkMembershipNew() {
    if(trim(document.getElementById('email_address').value) == "") {
        alert('Please enter an e-mail address');
    } else {
        document.getElementById('input_box').style.display='none';
        var member_result = isMemberNew();
        if(member_result == "YES") {
            document.getElementById('found_member').style.display='block';
        } else if(member_result == "RENEW") {
            document.getElementById('ra_renew_link').href = '/renewmembership.php?mid=' + getMID();
            document.getElementById('needs_renewal').style.display='block';
        } else {
            if(getPatron()!=false) {
                document.getElementById('patron_company').innerHTML = getPatron();
                document.getElementById('not_found_member_free').style.display='block';
            } else {
                if(isOnMailingList()) {
                    //document.getElementById('ra_fullmember_link').href = '/upgrademembership.php?mid=' + getMID();
                    document.getElementById('not_found_member_but_on_list').style.display='block';
                } else {
                    document.getElementById('not_found_member').style.display='block';
                }
            }
        }
    }
}
