var formElements  = new Array();
var checkReadOnly = new Array();
function setCheckbox(imageObject,formElement,inputElement) {
  var setReadonly   = false;
  for (i = 0; i < checkReadOnly.length; i++) {
    if (checkReadOnly[i] == formElement) {
      setReadonly = true;
      break;
    } else {
      setReadonly = false;
    }
  }
  if (setReadonly == false) {
    var boxImgsrc;
    if(formElements[formElement][2] != "true") {
      formElements[formElement][2] = "true";
      if (document.images[imageObject.id].src.indexOf('-h.') != -1) {
        boxImgsrc = document.images[imageObject.id].src;
      } else {
        boxImgsrc = document.images[imageObject.id].src.split('.gif');
        boxImgsrc = boxImgsrc[0] + '-h.gif'
      }
      document.images[imageObject.id].src = boxImgsrc;
      document.forms[0][inputElement].value = formElements[formElement][1];
    } else {
      formElements[formElement][2] = "false";
      if (document.images[imageObject.id].src.indexOf('-h.') != -1) {
        boxImgsrc = document.images[imageObject.id].src.split('-h.');
        boxImgsrc = boxImgsrc[0] + '.gif'
      } else {
        boxImgsrc = document.images[imageObject.id].src;
      }
      document.images[imageObject.id].src = boxImgsrc;
      document.forms[0][inputElement].value = formElements[formElement][0];
    }
    handleBehaviours(inputElement,imageObject.id);
  }
}
function selectBoxNotify(formField,formFieldValue,currentIndex) {
  if (typeof dependingSelects != 'undefined') {
    if(dependingSelects[formField] && formFieldValue != "" && typeof optionGroups[dependingSelects[formField]][formFieldValue] != 'undefined') {
      divContent = writeSelectBox(dependingSelects[formField], optionGroups[dependingSelects[formField]][formFieldValue],currentIndex-1,289,4);
      writeIntoLayer(document.getElementsByTagName("div")[dependingSelects[formField]],divContent);
      if (typeof dependingSelects != 'undefined' && dependingSelects[formField]) {
        for (i=0; i < dependingSelects[formField].length; i++) {
          setVisibility('element_'+dependingSelects[formField][i],null,'block');
        }
      }
    } else if (dependingSelects[formField]) {
      for (i=0; i < dependingSelects[formField].length; i++) {
        setVisibility('element_'+dependingSelects[formField][i],null,'none');
        document.forms[0][dependingSelects[formField][i]].value = '';
      }
    }
    notifyValue(formField,formFieldValue);
  }
}
function submitForm(actionName){
  document.forms[0].elements['action'].value = actionName;
  document.forms[0].submit();        
  return false;
}
function renderDropSelect(elem) {
  var checkNotify = false;
  if ((typeof dependingSelects != 'undefined' && dependingSelects[elem.name]) || (typeof behaveDependOn != 'undefined' && behaveDependOn[elem.name])) {
    checkNotify = true;
  }
  if (elem.defaultText != "") {
	elem.options.unshift("");
	elem.options.unshift(elem.defaultText);
  }
  var selectHeight=elem.getOptionCount();
  if (selectHeight > 10) selectHeight = 10;
  if (selectHeight < 1) selectHeight = 1;
  var selectWidth;
  var selectedValue = elem.elementValue;
  if (elem.elementValue == "") {
    selectedValue = elem.defaultText;
  }
  if (elem.getAttribute('class').indexOf('default')!=-1){
    selectWidth = 289;
  } else {
    selectWidth = 289;
  }
  document.write(writeSelectBox(elem.name,elem.options,elem.getAttribute('zIndex'),selectWidth, selectHeight, selectedValue, checkNotify,elem.hasError,elem.isReadonly));
}
var checkboxCounter = 0;
function renderCheckbox(elem) {
  formElements[elem.name]  = new Array("false","true",elem.elementValue);
  checkboxCounter ++;
  var checkNotify = "false";
  if (typeof behaviours != 'undefined' && behaveDependOn[elem.name]) {
    checkNotify = "true";
  }
  var boxWidth;
  if (elem.getAttribute('class').indexOf('default')!=-1){
    boxWidth = 289;
  } else {
    boxWidth = 289;
  }
  var isChecked = 'false';
  for (i = 0; i < elem.options.length; i++) {
    if(elem.options[1] == elem.elementValue) {
      isChecked = 'false';
    } else if (elem.options[3] == elem.elementValue){
      isChecked = 'true';
    }
  }
  document.write(writeCheckbox(elem.name, elem.elementLabel, isChecked, elem.name, checkboxCounter, elem.getAttribute('zIndex'), boxWidth, checkNotify, elem.hasError, elem.getAttribute('class'), elem.mandatory,elem.isReadonly));
}
function renderButton(elem){
  document.write(writeButton(elem.id,elem.name,elem.getAttribute('class'),elem.elementValue,elem.defaultText));
}
function handleSections (elemName) {
  var sectionDependant, sectionHandler;
  if (typeof behaviours != 'undefined' && sectionsBehaveDependOn[elemName]) {
    for (i = 0; i < sectionsBehaveDependOn[elemName].length; i++) {
      if (sectionBehaviours[sectionsBehaveDependOn[elemName][i]]) {
        for (e = 0; e < sectionBehaviours[sectionsBehaveDependOn[elemName][i]].length; e++) {
          if (sectionBehaviours[sectionsBehaveDependOn[elemName][i]][e][2] == 0) {
            setVisibility("section_" + sectionsBehaveDependOn[elemName][i],null,'none');
          } else {
            setVisibility("section_" + sectionsBehaveDependOn[elemName][i],null,'block');
          }
        } 
      }
    }
  }
}
var a = 0, u = 0;
var breakDisplay = "";
var breakVisibility = "";
var breakMandatory = "";
function handleBehaviours (elemName, token) {
  if (typeof behaviours != 'undefined') {
    handleSections(elemName);
    if (behaveDependOn[elemName]) {
      for (a = 0; a < behaveDependOn[elemName].length; a++) {
        parentElement = behaveDependOn[elemName][a];
        if (behaviours[parentElement][0].length > 0) {
          handleDisplay(parentElement,behaviours[parentElement][0],token);
        }
        if (behaviours[parentElement][1].length > 0) {
          handleVisibility (parentElement,behaviours[parentElement][1]);
        }
        if (behaviours[parentElement][2].length > 0) {
          handleMandatory (parentElement,behaviours[parentElement][2])
        }
      }
    }
  }
}
function handleDisplay (parentElement,displayArray,token) {
  for (i = displayArray.length -1; i >= 0 ; i--) {
    if (typeof document.forms[0].elements[displayArray[i][0]] != 'undefined' && typeof document.forms[0].elements[parentElement] == 'object') {
      if ((document.forms[0].elements[displayArray[i][0]].value == displayArray[i][1]) && (displayArray[i][1] != null) && (breakDisplay != parentElement)) {
        displayMode = displayArray[i][2];
        switchDisplayMode (displayMode,parentElement,displayArray[i][0],token);
        breakDisplay = parentElement;
      } else if (breakDisplay != parentElement && displayArray[i][1] == null) {
        displayMode = displayArray[i][2];
        switchDisplayMode (displayMode,parentElement,displayArray[i][0],token);
      }
    }
  }
  breakDisplay = "";
}
function switchDisplayMode (displayMode,parentElement,displayArrayElement,token) {
  switch (displayMode) {
    case 0:
      handleDisplayTextBehaviour(parentElement,displayArrayElement);
    break;
    case 1:
      handleDisplayReadonlyBehaviour (parentElement, displayArrayElement,token);
    break;
    case 2:
      handleDisplayEditBehaviour(parentElement, displayArrayElement,token);
    break;
  }
}
function handleDisplayTextBehaviour (parentElement, controlElement) {
  writeIntoLayer(parentElement,document.forms[0].elements[displayArray[i][0]].value);
}
function handleDisplayReadonlyBehaviour (parentElement, controlElement, imageId) {
  if (document.forms[0].elements[parentElement].type == "hidden" && (typeof document.getElementsByTagName("div")[parentElement] != 'undefined')) {
    var checkElem;
    for (checkElem in formElements) {
      if (parentElement == checkElem) {
        checkReadOnly.push(parentElement);
        if (formElements[parentElement][2] == "false") {
          document.images[imageId].src = checkboxDisabledGif;
        } else {
          document.images[imageId].src = checkboxDisabledGifHigh;
        }
      }
    }
  }
  if (typeof document.forms[0].elements[parentElement] == 'object') {
    if (document.forms[0].elements[parentElement].type == 'hidden') {
    } else {
      document.forms[0].elements[parentElement].readonly = true;
      setClassName(document.forms[0].elements[parentElement],'disabled input100');
    }
  }
}
function handleDisplayEditBehaviour (parentElement, controlElement, imageId) {
  var checkElem;
  for (checkElem in formElements) {
    if (parentElement == checkElem) {
      if (formElements[parentElement][2] == "false") {
        document.images[imageId].src = checkboxGif;
      } else{
        document.images[imageId].src = checkboxGifHigh;
      }
    }
  }
  document.forms[0].elements[parentElement].readonly = false;
  setClassName(document.forms[0].elements[parentElement],'input100');
}
function handleVisibility (parentElement,displayArray) {
  for (i = displayArray.length -1; i >= 0 ; i--) {
    if (typeof document.forms[0].elements[displayArray[i][0]] != 'undefined' && typeof document.forms[0].elements[parentElement] == 'object') {
      if ((document.forms[0].elements[displayArray[i][0]].value == displayArray[i][1]) && (displayArray[i][1] != null) && (breakVisibility != parentElement)) {
        visibilityMode = displayArray[i][2];
        switchVisibilityMode (visibilityMode,parentElement,displayArray[i][0]);
        breakVisibility = parentElement;
      } else if (breakVisibility != parentElement && displayArray[i][1] == null) {
        visibilityMode = displayArray[i][2];
        switchVisibilityMode (visibilityMode,parentElement,displayArray[i][0]);
      }
    }
  }
  breakVisibility = "";
}
function switchVisibilityMode (visibilityMode,parentElement,displayArrayElement) {
  switch (visibilityMode) {
    case 0:
      setVisibility("element_" + parentElement,null,'none');
    break;
    case 1:
      setVisibility("element_" + parentElement,null,'block');
    break;
  }
}
function handleMandatory (parentElement,displayArray) {
  for (i = displayArray.length -1; i >= 0 ; i--) {
    if (typeof document.forms[0].elements[displayArray[i][0]] != 'undefined' && typeof document.forms[0].elements[parentElement] == 'object') {
      if ((document.forms[0].elements[displayArray[i][0]].value == displayArray[i][1]) && (displayArray[i][1] != null) && (breakMandatory != parentElement)) {
        mandatoryMode = displayArray[i][2];
        switchMandatoryMode (mandatoryMode,parentElement,displayArray[i][0]);
        breakMandatory = parentElement;
      } else if (breakMandatory != parentElement && displayArray[i][1] == null) {
        mandatoryMode = displayArray[i][2];
        switchMandatoryMode (mandatoryMode,parentElement,displayArray[i][0]);
      }
    }
  }
  breakMandatory = "";
}
function switchMandatoryMode (mandatoryMode,parentElement,displayArrayElement) {
  switch (mandatoryMode) {
    case 0:
      setVisibility("mandatory_" + parentElement,null,"none");
    break;
    case 1:
      setVisibility("mandatory_" + parentElement,null,"inline");
    break;
  }
}
function notifyValue(elemName, curValue) {
  handleBehaviours(elemName);
}
function checkEmptySelects () {
  if (typeof dependingSelects != 'undefined') {
    if (typeof document.forms[0] != 'undefined'){
      var currentDependant;
      for (currentDependant in dependingSelects) {
        for(i=0; i < dependingSelects[currentDependant].length; i++){
        //if(document.forms[0][currentDependant] && document.forms[0][currentDependant].value == eval('fe_'+currentDependant).options[0]){
          if(document.forms[0][currentDependant] && document.forms[0][currentDependant].value == window["fe_" + currentDependant].options[0]){
            setVisibility('element_'+dependingSelects[currentDependant][i],null,'none');
          }/*
          alert("dependingSelects[\"" + currentDependant + "\"] : " + dependingSelects[currentDependant] + "\ndependingSelects[\"" + currentDependant + "\"][" + i + "] : " + dependingSelects[currentDependant][i]);
          alert('fe_'+dependingSelects[currentDependant][i]);
          alert(eval('fe_'+dependingSelects[currentDependant][i]));*/
        //if((typeof eval('fe_'+dependingSelects[currentDependant][i]) != 'undefined') && (typeof eval('fe_'+dependingSelects[currentDependant][i]).options[2] != 'string')){
          if((typeof window["fe_" + dependingSelects[currentDependant][i]] != "undefined") && (typeof window["fe_" + dependingSelects[currentDependant][i]].options[2] != "string")){
            setVisibility('element_'+dependingSelects[currentDependant][i],null,'none');
          }
        }
      }
    } else {
      window.setTimeout('dependingSelects()',500);
    }
  }
}

