function setFormFields(ajaxForm, form_data) {

   var formElement = null;
   var fieldValue  = null;
   var ajaxData    = form_data.split('|');

   for (var indx = 0; indx < ajaxData.length - 1; indx += 2) {

      formElement = ajaxForm.elements[ajaxData[indx]];
      fieldValue  = ajaxData[indx+1];

      if (formElement) {

         if (formElement.tagName == 'INPUT') {
            setInput(null, fieldValue, formElement);
         }
         else if (formElement.tagName == 'SELECT') {
            setSelectOption(formElement, fieldValue);
         }
         else if (formElement.tagName == 'TEXTAREA') {
            formElement.innerHTML = fieldValue;
         }
      }
   }
}


function setInput(inputId, inputValue, inputObject) {

   var status = false;

   if (inputObject) {
      var ajaxInput = inputObject;
   }
   else if (inputId) {
      var ajaxInput = document.getElementById(inputId);
   }

   if (ajaxInput) {

       status = ajaxInput.disabled;
       ajaxInput.disabled = false;

       if (ajaxInput.type == 'text'  ||  ajaxInput.type == 'hidden') {
          ajaxInput.value = inputValue;
       }
       else if (ajaxInput.type == 'checkbox') {
          if (inputValue  &&  inputValue != 0) {
             ajaxInput.checked = true;
          }
          else {
             ajaxInput.checked = false;
          }
       }

       if (status) {
          ajaxInput.disabled = true;
       }
   }
}


function setSelectOption(html_select, option_value) {

   for (var indx = 0; indx < html_select.options.length; indx++) {
      if (html_select.options[indx].value == option_value) {
         html_select.options[indx].selected = true;
      }
   }
}


function getSelectValue(select_id) {

   var value      = null;
   var htmlSelect = document.getElementById(select_id);
   var optionNr   = htmlSelect.selectedIndex;

   if (optionNr != -1) {
      value = htmlSelect.options[optionNr].value;
   }

   return value;
}


function removeOptions(select, set_default) {

   for (var i = select.options.length; i > 0; i--) {
      select.options[i-1] = null;
   }

   if (set_default) {
      select.options[0] = new Option('Loading...', 0);
   }
}


function getCheckedValue(radioObj) {

   if(!radioObj) {
      return "";
   }

   var radioLength = radioObj.length;

   if(radioLength == undefined) {
      if(radioObj.checked) {
         return radioObj.value;
      }
      else {
         return "";
      }
   }

   for(var i = 0; i < radioLength; i++) {
      if(radioObj[i].checked) {
         return radioObj[i].value;
      }
   }

   return "";
}


//Checkbox functions.

function setCheckBoxes(name, values) {

   var checkBoxes = document.getElementsByName(name);
   var newInput   = null;
   var valueArray = values.split('|');

   function in_array(value) {
      for (var indy = 0; indy < valueArray.length; indy++) {
         if (valueArray[indy] == value) {
            return true;
         }
      }
      return false;
   }

   for (var indx = 0; indx < checkBoxes.length; indx++) {
      if (in_array(checkBoxes[indx].value)) {
         checkBoxes[indx].checked = true;
      }
      else {
         checkBoxes[indx].checked = false;
      }
   }
}


function enableCheckBoxes(name, values) {

   var checkBoxes = document.getElementsByName(name);
   var newInput   = null;
   var valueArray = values.split('|');

   function in_array(value) {
      for (var indy = 0; indy < valueArray.length; indy++) {
         if (valueArray[indy] == value) {
            return true;
         }
      }
      return false;
   }

   for (var indx = 0; indx < checkBoxes.length; indx++) {
      if (in_array(checkBoxes[indx].value)) {
         checkBoxes[indx].disabled = false;
      }
      else {
         checkBoxes[indx].disabled = true;
      }
   }
}
