function submitSurvey() {
  if ( !validateSurvey())  {
    dumpSurveyError();
    return;
  }
  
  saveSurvey();
}

function saveSurvey() {
  var selectSexId = document.getElementById('sex_id');
  var selectAgeId = document.getElementById('age_id');
  var selectEdId = document.getElementById('education_id');
  var selectRadioId = document.getElementById('radio_id');
  
  var postParameters = 'sex_id=' + selectSexId.value + '&age_id=' + selectAgeId.value + '&ed_id=' + selectEdId.value + '&radio_id=' + selectRadioId.value;
  
  var ajaxURL = 'xmlhttp.php?cmd=save_survey';
  
  // JS Integration to 3rd party websites, we use image loading
  if (typeof(surveyWWW) != 'undefined') {
    ajaxURL = surveyWWW + '/' + ajaxURL + '&js_integration=1&' + postParameters;
    var myImg = new Image();
    myImg.onload = function () {
      saveSurveySuccess();
    }
    myImg.src = ajaxURL;
    return;
  }
  
  var ajax = getHTTPObject();
  if (ajax.readyState == 4 || ajax.readyState == 0) {
    ajax.open('POST', ajaxURL, true);
    ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange = function() {
      if (ajax.readyState == 4) {
        saveSurveySuccess();
      }
    };
    ajax.send(postParameters);
  }
}

function saveSurveySuccess() {
  var oFORMSurvey = document.getElementById('survey');
  oFORMSurvey.style.display = "none";
}

function validateSurvey() {
  var oSELECT = document.getElementById('sex_id');
  if (oSELECT.value == 0) {
    return false;
  }
  
  oSELECT = document.getElementById('age_id');
  if (oSELECT.value == 0) {
    return false;
  }
  
  oSELECT = document.getElementById('education_id');
  if (oSELECT.value == 0) {
    return false;
  }
  
  oSELECT = document.getElementById('radio_id');
  if (oSELECT.value == 0) {
    return false;
  }
  
  return true;
}

function dumpSurveyError()  {
  var surveyError = document.getElementById('surveyError');
  surveyError.style.display = '';
}
