
//
// REQUESTS WITHOUT RESULT (eg. for tracking events)
//

function ajaxTrack(operation) {
  var trackRequest = new AjaxRequest(false, false);
  trackRequest.startRequest(operation, false);
}


//
// CITYREFRESH
//


//
// main call
//
function ajaxCityRefresh(formId, operation) {
  var form = document.getElementById(formId);
  if (form && form.country && form.city) {
    // build query string
    if (operation.indexOf('?') > 0) {
      operation += "&";
    } else {
      operation += "?";
    }
    operation += "country=" + form.country.options[form.country.options.selectedIndex].value;

    if (form.showAll) {
      operation += "&showAll=" + form.showAll.value;
    }
    if (form.nullValue) {
      operation += "&nullValue=" + form.nullValue.value;
    }

    // collect data needed for later result/error-handling
    var dataBag = new Array();
    dataBag["formId"] = formId;

    // do the ajax request
    var cityRequest = new AjaxRequest("cityrefeshResultHandler", "cityrefeshErrorHandler");
    cityRequest.startRequest(operation, dataBag);
  }
}

//
// handle ajax result
//
function cityrefeshResultHandler(dataBag, resultData) {
  // updates city selectbox
  // format of resultdata: value=data&value=data...
  if (resultData && dataBag) {
    var form = document.getElementById(dataBag["formId"]);

    if (form && form.city) {
      var arrOptions = resultData.split("&");
      form.city.options.length = 0;
      for(var i = 0; i < arrOptions.length; i++ ) {
        arrOption = arrOptions[i].split("=");
        form.city.options[i] = new Option(arrOption[1], arrOption[0], false, false);
      }
    }
  }
}

//
// handle ajax error
//
function cityrefeshErrorHandler(dataBag) {
  // try it with an page reload
  if (dataBag) {
    var form = document.getElementById(dataBag["formId"]);
    if (form && form.ajaxreload) {
      form.ajaxreload.value = "1";
      form.submit();
    }
  }
}
