function showAjaxLoader() {
  document.getElementById('ajaxLoader').style.display = 'block';
}
function hideAjaxLoader() {
  document.getElementById('ajaxLoader').style.display = 'none';
}

function contactObj(){
    this.xmlReq = null;
    this.onAjaxSuccess = function(){
      var tmpResult = this.xmlReq.responseText.split('||');
      if (tmpResult[0] == 'EMPTY.FIELD') {
        //markEmptyField(tmpResult[1]);
        showEmptyFieldNotice();

      } else if (tmpResult[0] == 'SUCCESS') {
        insertList('5', 'kontakt', document.getElementById('contactFirstname').value, document.getElementById('contactName').value, document.getElementById('contactEmail').value, document.getElementById('contactPhone').value, document.getElementById('contactStreet').value, document.getElementById('contactPostcode').value, document.getElementById('contactVillage').value, document.getElementById('contactCountry').value, document.getElementById('contactCompany').value, document.getElementById('contactHow').value, document.getElementById('contactTitle').value);
        if (document.getElementById('contactPackage').checked) {
          insertList('2', 'kontakt', document.getElementById('contactFirstname').value, document.getElementById('contactName').value, document.getElementById('contactEmail').value, document.getElementById('contactPhone').value, document.getElementById('contactStreet').value, document.getElementById('contactPostcode').value, document.getElementById('contactVillage').value, document.getElementById('contactCountry').value, document.getElementById('contactCompany').value, document.getElementById('contactHow').value, document.getElementById('contactTitle').value);
        }

        document.getElementById('contactTitle').selectedIndex = '0';
        document.getElementById('contactName').value = '';
        document.getElementById('contactFirstname').value = '';
        document.getElementById('contactCompany').value = '';
        document.getElementById('contactStreet').value = '';
        document.getElementById('contactPostcode').value = '';
        document.getElementById('contactVillage').value = '';
        document.getElementById('contactCountry').value = '';
        document.getElementById('contactPhone').value = '';
        document.getElementById('contactEmail').value = '';
        document.getElementById('contactPackage').checked = false;
        document.getElementById('contactHow').selectedIndex = '0';
        document.getElementById('contactHowText').value = '';
        document.getElementById('contactMessage').value = '';

        document.getElementById('contactResultContent').innerHTML = 'Ihre Anfrage wurde gesendet. Wir werden uns so schnell wie möglich mit Ihnen in Verbindung setzen.';
        document.getElementById('contactResultBox').style.display = 'block';
      }
      hideAjaxLoader();
    };
    this.onAjaxError = function(){
      hideAjaxLoader();
    };
}

var myContactObj = new contactObj();

function sendContact() {
  showAjaxLoader();
  document.getElementById('contactResultBox').style.display = 'none';

  var params = '';
  params += 'contactTitle=' + document.getElementById('contactTitle').value;
  params += '&contactName=' + document.getElementById('contactName').value;
  params += '&contactFirstname=' + document.getElementById('contactFirstname').value;
  params += '&contactCompany=' + document.getElementById('contactCompany').value;
  params += '&contactStreet=' + document.getElementById('contactStreet').value;
  params += '&contactPostcode=' + document.getElementById('contactPostcode').value;
  params += '&contactVillage=' + document.getElementById('contactVillage').value;
  params += '&contactCountry=' + document.getElementById('contactCountry').value;
  params += '&contactPhone=' + document.getElementById('contactPhone').value;
  params += '&contactEmail=' + document.getElementById('contactEmail').value;
  params += '&contactPackage=' + document.getElementById('contactPackage').checked;
  params += '&contactHow=' + document.getElementById('contactHow').value;
  params += '&contactHowText=' + document.getElementById('contactHowText').value;
  params += '&contactMessage=' + document.getElementById('contactMessage').value;

  var myReq = new AjaxRequest('/ajax/sendContact.php',
    {
      method:'POST',
      params:params
    },
    myContactObj
  );
}

function markEmptyField(fieldID) {
  document.getElementById(fieldID).style.border = '1px solid #CC0000';
}

function showEmptyFieldNotice() {
  document.getElementById('contactResultContent').innerHTML = 'Fehler: Füllen Sie bitte <u>alle</u> Pflichtfeler aus!';
  document.getElementById('contactResultBox').style.display = 'block';
}

function listObj(){
    this.xmlReq = null;
    this.onAjaxSuccess = function(){};
    this.onAjaxError = function(){};
}

var myListObj = new listObj();

function insertList(liste, formId, firstname, name, email, phone, street, postcode, village, country, company, how, title) {
  var params = '';
  params += 'liste=' + liste;
  params += '&contactName=' + name;
  params += '&contactFirstname=' + firstname;
  params += '&contactEmail=' + email;
  if (phone != '' && phone != undefined) {
    params += '&contactPhone=' + phone;
  }
  if (street != '' && street != undefined) {
    params += '&contactStreet=' + street;
  }
  if (postcode != '' && postcode != undefined) {
    params += '&contactPostcode=' + postcode;
  }
  if (village != '' && village != undefined) {
    params += '&contactVillage=' + village;
  }
  if (country != '' && country != undefined) {
    params += '&contactCountry=' + country;
  }
  if (company != '' && company != undefined) {
    params += '&contactCompany=' + company;
  }
  if (how != '' && how != undefined) {
    params += '&contactHow=' + how;
  }
  if (title != '' && title != undefined) {
    params += '&contactTitle=' + title;
  }
  params += '&formId=' + formId;

  var myReq = new AjaxRequest('/ajax/insertList.php',
    {
      method:'POST',
      params:params
    },
    myListObj
  );
}

function callbackObj(){
    this.xmlReq = null;
    this.onAjaxSuccess = function(){
      var tmpResult = this.xmlReq.responseText.split('||');
      if (tmpResult[0] == 'EMPTY.FIELD') {
        //markEmptyField(tmpResult[1]);
        showEmptyFieldNotice();

      } else if (tmpResult[0] == 'SUCCESS') {
        insertList('5', 'rueckruf', document.getElementById('contactFirstname').value, document.getElementById('contactName').value, document.getElementById('contactEmail').value, document.getElementById('contactPhone').value);

        document.getElementById('contactName').value = '';
        document.getElementById('contactFirstname').value = '';
        document.getElementById('contactPhone').value = '';
        document.getElementById('contactEmail').value = '';
        document.getElementById('contactDate').calue = '';
        document.getElementById('contactFromTime').value = '';
        document.getElementById('contactToTime').value = '';
        document.getElementById('contactMessage').value = '';

        document.getElementById('contactResultContent').innerHTML = 'Ihre Anfrage wurde gesendet. Wir werden uns so schnell wie möglich mit Ihnen in Verbindung setzen.';
        document.getElementById('contactResultBox').style.display = 'block';
      }
      hideAjaxLoader();
    };
    this.onAjaxError = function(){
      hideAjaxLoader();
    };
}

var myCallbackObj = new callbackObj();

function sendCallback() {
  showAjaxLoader();
  document.getElementById('contactResultBox').style.display = 'none';

  var params = '';
  params += 'contactName=' + document.getElementById('contactName').value;
  params += '&contactFirstname=' + document.getElementById('contactFirstname').value;
  params += '&contactPhone=' + document.getElementById('contactPhone').value;
  params += '&contactEmail=' + document.getElementById('contactEmail').value;
  params += '&contactDate=' + document.getElementById('contactDate').value;
  params += '&contactFromTime=' + document.getElementById('contactFromTime').value;
  params += '&contactToTime=' + document.getElementById('contactToTime').value;
  params += '&contactMessage=' + document.getElementById('contactMessage').value;

  var myReq = new AjaxRequest('/ajax/sendCallback.php',
    {
      method:'POST',
      params:params
    },
    myCallbackObj
  );
}

function nlObj(){
    this.xmlReq = null;
    this.onAjaxSuccess = function(){
      if (this.xmlReq.responseText == 'SUCCESS') {
        document.getElementById('contactName').value = '';
        document.getElementById('contactFirstname').value = '';
        document.getElementById('contactEmail').value = '';

        document.getElementById('contactResultContent').innerHTML = 'Vielen Dank für Ihre Anfrage! Bitte bestätigen Sie Ihre Anmeldung, indem Sie den Link in der E-Mail klicken, die wir Ihnen soeben zugesendet haben!';
        document.getElementById('contactResultBox').style.display = 'block';
      }
      hideAjaxLoader();
    };
    this.onAjaxError = function(){
      hideAjaxLoader();
    };
}

var myNlObj = new nlObj();

function insertNL(formId, additionalList) {
  showAjaxLoader();
  if (additionalList != '' && additionalList != undefined) {
    insertList(additionalList, formId, document.getElementById('contactFirstname').value, document.getElementById('contactName').value, document.getElementById('contactEmail').value);
  }
  document.getElementById('contactResultBox').style.display = 'none';

  var params = '';
  params += 'liste=5';
  params += '&contactName=' + document.getElementById('contactName').value;
  params += '&contactFirstname=' + document.getElementById('contactFirstname').value;
  params += '&contactEmail=' + document.getElementById('contactEmail').value;
  params += '&formId=' + formId;

  var myReq = new AjaxRequest('/ajax/insertList.php',
    {
      method:'POST',
      params:params
    },
    myNlObj
  );
}
