function validate_form(f) {
  if (isNotEmpty(f.companyName)) {
     if (ProductChosen(f.ProductID,f.Product_Other)) {
       if (isNotEmpty(f.FullName)) {
        if (isNotEmpty(f.email)) {
           if (isNotEmpty(f.recaptcha_response_field)) {
             return true;
           }
        }
     }
    }
  }
  return false;
}
function ProductChosen(select,fillin) {
    if (fillin.value == '') {
       if (select.selectedIndex == 0) {
           alert("Please make a choice from the list of products.");
           return false;
       }
    }
    return true;
}

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}
   var searchCompanyReq = getXmlHttpRequestObject();

//Check to see if the company name exists
var CompanyExistsMsgID = 'CompanyExistsMsg';
var setCompanyExistsMsg = 'Exist';
var CompanySearchID = 'companyName';
var CompanyExistsMsg = "<p class='error'>A Company with that name already exists in our Database, please contact our "+
                       "<a href='mailto:info@epicorusers.org'>office</a> before you submit this registration to avoid duplicate entries.</p>";
var CompanyExistsButSuspededMsg = "<p class='error'>A Company with that name already exists in our Database, please contact our "+
                       "<a href='mailto:info@epicorusers.org'>office</a> before you submit this registration to avoid duplicate entries.</p>";
function CheckCompanyExists() {
	if (searchCompanyReq.readyState == 4 || searchCompanyReq.readyState == 0) {
	    var elem = document.getElementById(CompanySearchID);
	    if (elem) {
	       //alert('Testing "'+elem.value+'"');
  		   var str = escape(elem.value);
		   searchCompanyReq.open("GET", '/members/NewMember_CompanyCheck.php?str=' + str, true);
		   searchCompanyReq.onreadystatechange = handleCompanyExists; 
		   searchCompanyReq.send(null);
		}
	}		
}

//Called when the AJAX response is returned.
function handleCompanyExists() {
	if (searchCompanyReq.readyState == 4) {
		var ss = document.getElementById(CompanyExistsMsgID)
		ss.innerHTML = '';
		var str = searchCompanyReq.responseText.split(";");
		//ss.innerHTML = '&quot;' + str + '&quot;';
        //debugger;        
		if (str[0] == "0") {
		   if (setCompanyExistsMsg == 'Not Exist') ss.innerHTML = CompanyExistsMsg;
		} else {
		   if (str[3] == "1") {
		      ss.innerHTML = "<p class='error'>An inactive company with that name already exists in our database, please contact our "+
                             "<a href='mailto:info@epicorusers.org'>office</a> to have the company marked to allow for renewal." +
                             " If you feel this is an error please contact our <a href='mailto:info@epicorusers.org'>office</a> for assistance.</p>";
           } else if (str[2] == "1") {
		      ss.innerHTML = "<p class='warning'>A suspended company with that name already exists in our Database, please use our "+
                             "<a href='members/membership_renewal.php?companyID="+str[0]+"'>membership renewal</a> page to renew your membership."+
                             " If you feel this is an error please contact our <a href='mailto:info@epicorusers.org'>office</a> for assistance</p>";
           } else{
              var exp = mysqlTimeStampToDate(str[1]);
              var today = new Date();
              if (exp.getFullYear < today.getFullYear) {
  		          ss.innerHTML = "<p class='error'>An expired company (as of "+str[1]+") with that name already exists in our database, please use our "+
                                 "<a href='members/membership_renewal.php?companyID="+str[0]+"'>membership renewal</a> page to renew your membership</p>";
              } else {
  		          ss.innerHTML = "<p class='error'>A current active company with that name already exists in our Database, please use our "+
                                 "<a href='members/request_access.php?companyID="+str[0]+"'>Access Request</a> page to have us e-mail the primary contact for "+
                                 "this company to ask to be added as a contact.  If you feel this is an error please contact our "+
                                 "<a href='mailto:info@epicorusers.org'>office</a> for assistance</p>";
              }
           }
		}
		
	}
}
function mysqlTimeStampToDate(timestamp) {
  //function parses mysql datetime string and returns javascript Date object
  //input has to be in this format: 2007-06-05 15:26:02
  var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
  var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
  return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}
function clearmsg(id) {
  var elem = document.getElementById(id);
  if (elem) {
    elem.innerHTML = '';
  }
}
var searchContactReq = getXmlHttpRequestObject();

//Check to see if the company name exists
var ContactExistsMsgID = 'ContactExistsMsg';
var setContactExistsMsg = 'Exist';
var ContactSearchID = 'email';
var ContactExistsMsg = "<p class='error'>A contact with that e-mail address already exists in our Database, please contact our "+
                       "<a href='mailto:info@epicorusers.org'>office</a> before you submit this registration to avoid duplicate entries.</p>";
function CheckContactExists() {
	if (searchContactReq.readyState == 4 || searchContactReq.readyState == 0) {
	    var elem = document.getElementById(ContactSearchID);
	    if (elem) {
  		   var str = escape(elem.value);
  		   if (str != '') { 
  	          //alert('Testing "'+str+'"');
 		      searchContactReq.open("GET", '/members/ContactSearchExists.php?str=' + str, true);
		      searchContactReq.onreadystatechange = handleContactExists; 
		      searchContactReq.send(null);
		   }
		}
	}		
}
//Called when the AJAX response is returned.
function handleContactExists() {
	if (searchContactReq.readyState == 4) {
		var ss = document.getElementById(ContactExistsMsgID)
		ss.innerHTML = '';
		var str = searchContactReq.responseText;
		//ss.innerHTML = '&quot;' + str + '&quot;';
        //debugger;        
		if (str == "0") {
		   if (setContactExistsMsg == 'Not Exist') ss.innerHTML = ContactExistsMsg;
		} else {
		   if (setContactExistsMsg == 'Exist') ss.innerHTML = ContactExistsMsg;
		}
	}
}
