function ajax_call(url, reqName, selection) {
  // first of all change cursor to busy
  $("body").css("cursor", "progress");

  // if no selection then use reqName to find element
  var selection = typeof(selection) != 'undefined' ? selection : reqName;

  var getArgs = {};
  getArgs['action']    = 'ajax';
  getArgs['req_name']  = reqName;
  getArgs['req_value'] = $(selection).val();

  $.ajaxSetup({"error": ajax_callback_error});
  $.getJSON(url, getArgs, ajax_callback);
}

function ajax_callback(response) { 
  // loop through whole array and implement instruction on DOM from each part
  $.each(response, function() {
    switch (this.type) {
      case 'html':
        $(this.selection).html(this.html);
        break;
      case 'val':
        $(this.selection).val(this.value);
        break;
      case 'addClass':
        $(this.selection).addClass(this.className);
        break;
      case 'removeClass':
        $(this.selection).removeClass(this.className);
        break;
      case 'attr':
        $(this.selection).attr(this.attrName, this.attrValue);
        break;
      case 'removeAttr':
        $(this.selection).removeAttr(this.attrName);
        break;
      case 'eval':
        eval(this.script);
        break;
      case 'replaceWith':
        $(this.selection).replaceWith(this.html);
        break;
    }
  })

  // restore cursor
  $("body").css("cursor", "auto");
}

function ajax_callback_error(XMLHttpRequest, textStatus, errorThrown) {   
  alert(errorThrown);

  // restore cursor
  $("body").css("cursor", "auto");
}

function select_change(srcid, srcname) {
  // first of all change cursor to busy
  $("body").css("cursor", "progress");
  
  // move selected item from "available list" to "selected list"
  // and make sure that they are no longer selected
  $('#' + srcid + ' option:selected').each(function() {
      curObj1 = $('#' + srcid + '-selected option[value=' + this.value + ']');
      if (curObj1.length == 0) {
        $('<option>').attr({value: this.value}).prependTo('#' + srcid + '-selected').html(this.text);
      }
      this.selected = '';
  });

  // remove all selected item from "selected list"
  $('#' + srcid + '-selected option:selected').remove();
  // remove all hidden boxes for selected list
  $('input#' + srcid + '-save').remove();
  // reset color of all existing selected items from "available list"
  $('#' + srcid + ' option').removeAttr('disabled').css('color', 'black');

  // loop through all "selected list" and find related entries
  // in "available list" if found make them grey. and also create a 
  // related hidden box entry for each selected item
  $('#' + srcid + '-selected option').each(function() {
      curObj2 = $('#' + srcid + ' option[value=' + this.value + ']');
      if (curObj2.length != 0) {
        curObj2.attr('disabled', 'disabled').css('color', 'grey');
      }
      $('<input>').attr({
          type: 'hidden',
          name: srcname + '[' + this.value + ']',
          id: srcid + '-save',
          value: this.value
      }).insertAfter('#' + srcid + '-selected');
  });
  $('#' + srcid).animate({width: "auto"}, "slow");
  
  // restore cursor
  $("body").css("cursor", "auto");
}

function select_selectAll(src) {
  $('#' + src + ' option').attr('selected', 'selected');
}

/**
    * Allows to create/modify a field value within a form before submitting it.
    * Launches the above function depending on the status of a trigger checkbox

    * @param   string   formName Obviously the form name you want to submit
    * @param   string   fieldToUpdate The element name you want to modify
    * @param   string   fieldValue
    * @param   bool      doCreate If you want to create a hidden input element instead of modifying an existing one
    *
    * @return  void Submit the form
    */
function formSubmit(formId, fieldName, fieldValue, doCreate)
{
    var selectedForm = document.getElementById(formId);
    if (typeof doCreate != "undefined" && doCreate == 1) {
        newInput = document.createElement("input");
        newInput.setAttribute('name', fieldName);
        newInput.setAttribute('value', fieldValue);
        newInput.setAttribute('type', 'hidden');
        selectedForm.appendChild(newInput);
    } else {
        if (fieldName) {
            var elm = selectedForm.elements[fieldName];
            elm.value = fieldValue;
        }
    }
    selectedForm.submit();
}

//  Allows to reset a form
function formReset(formId)
{
    var selectedForm = document.getElementById(formId);
    if (!selectedForm) {
        return;
    }
    selectedForm.reset();
}

function confirmSubmit(action, formName, msgText)
{
    var alertMsg = (typeof(msgText) != 'undefined') ? msgText : "Are you sure you want to delete this item?";
    if (action != 'delete_all') {
      var flag = false;
      $('#' + formName + ' input[type=checkbox]:checked').each(function() {
        flag = true;
      });
      if (flag == false) {
          alert('You must select an element to proceed');
          return false;
      }
    }
    var agree = confirm(alertMsg);
    if (agree)
        return true;
    else
        return false;
}

function openwindow(file, w, h)
{
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  window.open(file,"mywindow","location=1,status=1,scrollbars=1,settings='height='+h+',width='+w+',top='+wint+',left='+winl+'");
}




