<!--

   //when set to true, the whole content is hidden while the apropriate display is set for
   //dynamic content. the whole page is shown only when all the data is loaded. this feature seems
   //to be a little buggy, hence it is set to false for the time being.
   var MDFormShowAllAtOnce = false;
   
   //change css rule to hide body
   if ( MDFormShowAllAtOnce ) document.styleSheets [ 0 ].cssRules [ 0 ].style.display = 'none';
   
   function MDFormShowOnlyRelevantQuestions ( question ) {
    for ( var i = 0; i < question.sub.length; i ++ ) {
     document.getElementById ( 'MDFormQuestion_' + question.sub [ i ].id ).style.display = question.originalLevel >= question.sub [ i ].min ? 'block' : 'none';
     MDFormShowOnlyRelevantQuestions ( question.sub [ i ] );
    }
   }
   
   function MDFormShowOnlyRelevantFields () {
    for ( var i = 0; i < MDFormConditionalFields.length; i ++ )
     if ( MDFormConditionalFields [ i ].condition ) document.getElementById ( MDFormConditionalFields [ i ].name ).style.display = MDFormConditionalFields [ i ].condition.evaluate () ? 'block' : 'none';
   }

   function MDFormOnLoad () {
    //for each MDQuestion show only if necessary
    if ( window.MDFormQuestions ) for ( var i = 0; i < MDFormQuestions.length; i ++ )
     MDFormShowOnlyRelevantQuestions ( MDFormQuestions [ i ] );
    //for each MDConditionalField show only if necessary
//    if ( window.MDFormConditionalFields ) for ( var i = 0; i < MDFormConditionalFields.length; i ++ )
//     MDFormShowOnlyRelevantFields ( MDFormConditionalFields [ i ] );
    //change css rule to show body
    if ( MDFormShowAllAtOnce ) document.styleSheets [ 0 ].cssRules [ 0 ].style.display = 'block';
   }

   function MDFormQuestion ( id, idParent, min, originalLevel )
   {
    this.id = id;
    this.idParent = idParent;
    this.min = min;
    this.originalLevel = originalLevel;
    this.sub = new Array ();
   }
   
   function MDFormRadioAnswerChange ( index, value ) {
    for ( i = 0; i < MDFormQuestionList [ index ].sub.length; i ++ )
     document.getElementById ( 'MDFormQuestion_' + MDFormQuestionList [ index ].sub [ i ].id ).style.display = MDFormQuestionList [ index ].sub [ i ].min <= value ? 'block' : 'none';
   }
   
   function MDFormGoBackToModify () {
    if ( ( document ) && ( document.getElementById ) && ( document.getElementById ( 'submit_confirm' ) ) ) {
     document.getElementById ( 'submit_confirm' ).value = '0';
     if ( document.getElementById ( 'MDForm' ) ) document.getElementById ( 'MDForm' ).submit ();
    }
   }

   function MDFormFieldChange ( name, value ) {
    var i, field = null;
    for ( i = 0; i < MDFormConditionalFields.length; i ++ ) if ( MDFormConditionalFields [ i ].name == name ) {
     field = MDFormConditionalFields [ i ];
     break;
    }
//    alert(name);
//    if ( field ) alert ( 'Changing field ' + name + ' conditions other fields.' );
    if ( field ) for ( i = 0; i < field.sub.length; i ++ )
     document.getElementById ( 'MDFormField_' + MDFormConditionalFields [ index ].sub [ i ].name ).style.display = 'none';
   }
   
   function MDCondition ( operator, value ) {
    this.operator = operator;
    if ( value == true ) this.value = 'true';
    else if ( value == false ) this.value = 'false';
    else if ( parseFloat ( value ) != Number.NaN ) this.value = new String ( parseFloat ( value ) );
    else this.value = '\'' + ( value == null ? '' : value ) + '\'';
   }
   
   function MDFieldCondition ( conditionalOperand, conditions ) {
    this.conditionalOperand = conditionalOperand;
    this.conditions = conditions;
    
    this.evaluate = MDFieldConditionEvaluate;
   }
   
   function MDFieldConditionEvaluate ( value ) {
    if ( ( this.conditionalOperand != "||" ) && ( this.conditionalOperand != "&&" ) ) return alert ('error on operator.');
    var i, l = this.conditions.length, expression = "";
    if ( l == 0 ) return true;
    for ( i = 0; i < l; i ++ )
     expression += ( i == 0 ? '' : this.conditionalOperand ) + '(value' + this.conditions [ i ].operator + this.conditions [ i ].value + ')';
    return eval ( '(' + expression + ')' );
   }
   
   function MDFormConditionalField ( name, originalValue, condition ) {
    this.name = name;
    this.originalValue = originalValue || "";
    this.condition = condition || null;
    this.sub = new Array ();
   }
   
//   var MDCFieldList = new Array ();
//   MDCFieldList [ 0 ] = new MDFormConditionalField ( "field_name", "originalValue", new MDFieldCondition ( "||", [ new MDCondition ("==", 4), new MDCondition ("==",8) ] ) );
//   MDCFieldList [ 1 ] = new MDFormConditionalField ( "sub_field_name1", "originalValue1", new MDFieldCondition ( "||", [ new MDCondition ("==", 14), new MDCondition ("==",12) ] ) );
//   MDCFieldList [ 0 ].sub [ MDCFieldList [ 0 ].sub.length ] = MDCFieldList [ 1 ];
//   MDCFieldList [ 2 ] = new MDFormConditionalField ( "sub_field_name2", "originalValue2", new MDFieldCondition ( "||", [ new MDCondition ("==", 14), new MDCondition ("==",12) ] ) );
//   MDCFieldList [ 0 ].sub [ MDCFieldList [ 0 ].sub.length ] = MDCFieldList [ 2 ];
   
if ( window.onload ) window.oldOnload = window.onload;
window.onload = function() {
 MDFormOnLoad ();
 if ( window.oldOnload ) window.oldOnload ();
}

//-->
