/*
//
// CatGlobe_ImpSys
// Copyright (C) 2004 - CATINÉT CatGlobe ApS
//
*/

// ---------------------------------------------------------------- //

/*
//
// Enumeration CatGlobe_ImpSys_ObjectType
//
*/
var CatGlobe_ImpSys_ObjectType_PICTURE = 1;
var CatGlobe_ImpSys_ObjectType_WORD = 2;

// ---------------------------------------------------------------- //

/*
//
// Enumeration CatGlobe_ImpSys_RelationType
//
*/
var CatGlobe_ImpSys_RelationType_NONE = 1
var CatGlobe_ImpSys_RelationType_PICTURE2WORDS = 2;
var CatGlobe_ImpSys_RelationType_WORD2PICTURES = 3;
var CatGlobe_ImpSys_RelationType_PICTURE2PICTURES = 4;
var CatGlobe_ImpSys_RelationType_WORD2WORDS = 5;

// ---------------------------------------------------------------- //
// Model
// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_Object
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_Object(name, type, relationType)
{
   this.name = "CatGlobe_ImpSys_Object";
   this.type = CatGlobe_ImpSys_ObjectType_PICTURE;
   this.relationType = CatGlobe_ImpSys_RelationType_PICTURE2WORDS;

   if(name != null && name != "undefined") this.name = name;
   if(type != null && type != "undefined") this.type = type;
   if(relationType != null && relationType != "undefined") this.relationType = relationType;

   this.value = null;
   this.parentObject = null;
   this.relatedObjects = null;

   this.minRelationsCount = 2;
   this.maxRelationsCount = 2;

   this.maxParentObjectsCount = 1;
}

//
// Public properties
//
CatGlobe_ImpSys_Object.prototype.getName = function()
{
   return this.name;
}

CatGlobe_ImpSys_Object.prototype.setName = function(name)
{
   this.name = name;
}

CatGlobe_ImpSys_Object.prototype.getType = function()
{
   return this.type;
}

CatGlobe_ImpSys_Object.prototype.setType = function(type)
{
   this.type = type;
}

CatGlobe_ImpSys_Object.prototype.getValue = function()
{
   return this.value;
}

CatGlobe_ImpSys_Object.prototype.setValue = function(value)
{
   this.value = value;
}

CatGlobe_ImpSys_Object.prototype.getRelationType = function()
{
   return this.relationType;
}

CatGlobe_ImpSys_Object.prototype.setRelationType = function(relationType)
{
   this.relationType = relationType;
}

CatGlobe_ImpSys_Object.prototype.getRelatedObjects = function()
{
   if(this.relatedObjects == null)
   {
      this.relatedObjects = new Array();
   }

   return this.relatedObjects;
}

CatGlobe_ImpSys_Object.prototype.getMinRelationsCount = function()
{
   return this.minRelationsCount;
}

CatGlobe_ImpSys_Object.prototype.setMinRelationsCount = function(minRelationsCount)
{
   this.minRelationsCount = minRelationsCount;
}

CatGlobe_ImpSys_Object.prototype.getMaxRelationsCount = function()
{
   return this.maxRelationsCount;
}

CatGlobe_ImpSys_Object.prototype.setMaxRelationsCount = function(maxRelationsCount)
{
   this.maxRelationsCount = maxRelationsCount;
}

CatGlobe_ImpSys_Object.prototype.getCurrentRelationsCount = function()
{
   return this.relatedObjects.length;
}

CatGlobe_ImpSys_Object.prototype.getMaxParentObjectsCount = function()
{
   return this.maxParentObjectsCount;
}

CatGlobe_ImpSys_Object.prototype.setMaxParentObjectsCount = function(maxParentObjectsCount)
{
   this.maxParentObjectsCount = maxParentObjectsCount;
}

CatGlobe_ImpSys_Object.prototype.getParentObjects = function()
{
   if(this.parentObjects == null)
   {
      this.parentObjects = new Array();
   }

   return this.parentObjects;
}

//
// Public methods
//

CatGlobe_ImpSys_Object.prototype.HasParentObjects = function()
{
   if(this.parentObjects == null)
   {
      this.parentObjects = new Array();
   }

   return (this.parentObjects.length > 0);
}

CatGlobe_ImpSys_Object.prototype.MoreAssociationsAvailable = function()
{
   return (this.parentObjects.length < this.maxParentObjectsCount);
}

CatGlobe_ImpSys_Object.prototype.AssociationExists = function(object)
{
   for(i = 0; i < this.relatedObjects.length; i++)
   {
      if(this.relatedObjects[i].Equals(object))
      {
         return true;
      }
   }

   return false;   
}

CatGlobe_ImpSys_Object.prototype.AddParentObject = function(object)
{
   var i;
   var o1 = null;

   if(this.parentObjects == null)
   {
      this.parentObjects = new Array();
   }

   for(i = 0; i < this.parentObjects.length; i++)
   {
      if(this.parentObjects[i].Equals(object))
      {
         return;
      }
   }

   if(this.maxParentObjectsCount <= this.parentObjects.length && this.parentObjects.length > 0)
   {
      o1 = this.parentObjects[0];
      this.RemoveParentObject(o1);
      o1.RemoveObject(this);
   }

   this.parentObjects[this.parentObjects.length] = object;
}

CatGlobe_ImpSys_Object.prototype.RemoveParentObject = function(object)
{
   var i;

   if(this.parentObjects == null)
   {
      this.parentObjects = new Array();
   }

   for(i = 0; i < this.parentObjects.length; i++)
   {
      if(this.parentObjects[i].Equals(object))
      {
         i++;
         for(; i < this.parentObjects.length; i++)
         {
            this.parentObjects[i - 1] = this.parentObjects[i];
         }
         this.parentObjects.length--;
      }
   }
}

CatGlobe_ImpSys_Object.prototype.AddObject = function(object)
{
   var i, j;
   var o1 = null;
   var o2 = null;

   if(this.relatedObjects == null)
   {
      this.relatedObjects = new Array();
   }

   if(!this.AssociationExists(object))
   {
      // Have we reached the boundry of number of relations allowed
      if(this.relatedObjects.length >= this.maxRelationsCount)
      {
         o1 = this.relatedObjects[0];
         this.RemoveObject(o1);
      }
      this.relatedObjects[this.relatedObjects.length] = object;

      // Add this object to the list of parent objects for the added association object
      object.AddParentObject(this);
   }
}

CatGlobe_ImpSys_Object.prototype.RemoveObject = function(object)
{
   var i, j;

   if(this.relatedObjects == null)
   {
      this.relatedObjects = new Array();
   }

   for(i = 0; i < this.relatedObjects.length; i++)
   {
      if(this.relatedObjects[i].Equals(object))
      {
         i++;
         for(;i < this.relatedObjects.length; i++)
         {
            this.relatedObjects[i - 1] = this.relatedObjects[i];
         }
         this.relatedObjects.length--;

         object.RemoveParentObject(this);
      }
   }
}

CatGlobe_ImpSys_Object.prototype.Validate = function()
{
   return (
      this.minRelationsCount <= this.getCurrentRelationsCount() &&
      this.getCurrentRelationsCount() <= this.maxRelationsCount
   );
}

// Is abstract
CatGlobe_ImpSys_Object.prototype.Equals = function(object)
{
   return false;
}

// Is abstract
CatGlobe_ImpSys_Object.prototype.ToString = function()
{
   return false;
}

// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_Picture
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_Picture(url)
{
   this.url = url;
}

// Inherit from CatGlobe_ImpSys_Object
CatGlobe_ImpSys_Picture.prototype = new CatGlobe_ImpSys_Object("CatGlobe_ImpSys_Picture", CatGlobe_ImpSys_ObjectType_PICTURE);

//
// Private methods
//

//
// Public properties
//
CatGlobe_ImpSys_Picture.prototype.getUrl = function()
{
   return this.url;
}

CatGlobe_ImpSys_Picture.prototype.setUrl = function(url)
{
   this.url = url;
}

//
// Public methods
//

// Is implementation
CatGlobe_ImpSys_Picture.prototype.Equals = function(picture)
{
   if(picture != null) return this.url == picture.url;

   return false;
}

// Is implementation
CatGlobe_ImpSys_Picture.prototype.ToString = function()
{
   return "picture(url="+this.url+")";
}

// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_Word
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_Word(text)
{
   this.text = null;

   if(text != null && text != "undefined") this.text = text;
}

// Inherit from CatGlobe_ImpSys_Object
CatGlobe_ImpSys_Word.prototype = new CatGlobe_ImpSys_Object("CatGlobe_ImpSys_Word", CatGlobe_ImpSys_ObjectType_WORD);

//
// Public properties
//
CatGlobe_ImpSys_Word.prototype.getText = function()
{
   return this.text;
}

CatGlobe_ImpSys_Word.prototype.setText = function(text)
{
   this.text = text;
}

//
// Public methods
//

// Is implementation
CatGlobe_ImpSys_Word.prototype.Equals = function(word)
{
   if(word != null) return this.text == word.text;

   return false;
}

// Is implementation
CatGlobe_ImpSys_Word.prototype.ToString = function()
{
   return "word(text="+this.text+")";
}

// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_Question
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_Question(relationType)
{
   this.relationType = CatGlobe_ImpSys_RelationType_NONE;
   this.dummyPicture = new CatGlobe_ImpSys_Picture("../../Images/Questionnaire/Viewer/ImpSys/dummy.png");
   this.dummyWord = new CatGlobe_ImpSys_Word("");

   if(relationType != null && relationType != "undefined") this.relationType = relationType;

   this.text = "";

   this.objects = new Array();
   this.associations = new Array();
}

//
// Public properties
//
CatGlobe_ImpSys_Question.prototype.getText = function()
{
   return this.text;
}

CatGlobe_ImpSys_Question.prototype.setText = function(text)
{
   this.text = text;
}

CatGlobe_ImpSys_Question.prototype.getRelationType = function()
{
   return this.relationType;
}

CatGlobe_ImpSys_Question.prototype.setRelationType = function(relationType)
{
   this.relationType = relationType;
}

CatGlobe_ImpSys_Question.prototype.getDummyWord = function()
{
   return this.dummyWord;
}

CatGlobe_ImpSys_Question.prototype.setDummyWord = function(word)
{
   this.dummyWord = word;
}

CatGlobe_ImpSys_Question.prototype.getDummyPicture = function()
{
   return this.dummyPicture;
}

CatGlobe_ImpSys_Question.prototype.setDummyPicture = function(picture)
{
   this.dummyPicture = picture;
}

CatGlobe_ImpSys_Question.prototype.getObjects = function()
{
   return this.objects;
}

CatGlobe_ImpSys_Question.prototype.getAssociations = function()
{
   return this.associations;
}

//
// Public methods
//
CatGlobe_ImpSys_Question.prototype.ApplyPermutation = function()
{
   var i, m, n, o, iterations;

   // Permutate objects
   iterations = Math.ceil((Math.random() * 100)) % 50;
   for(i = 0; i < iterations; i++)
   {
      m = Math.ceil((Math.random() * 100)) % this.objects.length;
      n = Math.ceil((Math.random() * 100)) % this.objects.length;

      o = this.objects[m];
      this.objects[m] = this.objects[n];
      this.objects[n] = o;

      this.objects[m].setValue(m);
      this.objects[n].setValue(n);
   }

   // Permutate associations
   iterations = Math.ceil((Math.random() * 100)) % 50;
   for(i = 0; i < iterations; i++)
   {
      m = Math.ceil((Math.random() * 100)) % this.associations.length;
      n = Math.ceil((Math.random() * 100)) % this.associations.length;

      o = this.associations[m];
      this.associations[m] = this.associations[n];
      this.associations[n] = o;
   }
}

CatGlobe_ImpSys_Question.prototype.AddObject = function(object)
{
   object.setRelationType(this.relationType);

   this.objects[this.objects.length] = object;
}

CatGlobe_ImpSys_Question.prototype.AddAssociation = function(association)
{
   this.associations[this.associations.length] = association;
}

CatGlobe_ImpSys_Question.prototype.ToString = function()
{
   var i;
   var result = "";

   result += this.type;   
   result += ",objects(";
   for(i = 0; i < this.objects.length; i++)
   {
      result += this.objects[i].ToString()+",";
   }
   result += "),associations(";
   for(i = 0; i < this.objects.length; i++)
   {
      result += this.asso[i].ToString()+",";
   }
   result += ")";

   return result;
}

// ---------------------------------------------------------------- //
// View
// ---------------------------------------------------------------- //

// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_ControlLayout
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_ControlLayout()
{
   this.Style = "";
   this.Width = "100%";
   this.Height = "";
   this.BackgroundColorEnabled = "#00aa00";
   this.BackgroundColorDisabled = "#aa0000";
   this.TextColorEnabled = "#000000";
   this.TextColorDisabled = "#000000";
}

// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_Control
//
*/
function CatGlobe_ImpSys_Control()
{
   this.IE4 = (document.all && !document.getElementById) ? true : false;
   this.IE5 = (document.all && document.getElementById) ? true : false;
   this.NS4 = (document.layers) ? true : false;
   this.N6 = (document.getElementById && !document.all) ? true : false;

   this.id = "CatGlobe_ImpSys_Control";
   this.name = "CatGlobe_ImpSys_Control";
   this.value = "";

   this.style = "";
   this.layout = new CatGlobe_ImpSys_ControlLayout();

   this.toolTipText = "V&aelig;lg her";

   this.visible = true;
   this.disabled = false;

   this.onClick = null;
}

//
// Private method
//
CatGlobe_ImpSys_Control.prototype._ParseFunctionName = function(aFunction)
{
   var functionName = aFunction+"";
   var pIndex = functionName.indexOf("(");

   functionName = functionName.substring(0, pIndex);
   functionName = functionName.replace(/function[\w]*/ig, "");

   return functionName;
}

//
// Private methods
//
CatGlobe_ImpSys_Control.prototype._GenerateStyle = function()
{
   return this.style;
}

//
// Public propterties
//
CatGlobe_ImpSys_Control.prototype.getId = function()
{
   return this.id;
}

CatGlobe_ImpSys_Control.prototype.setId = function(id)
{
   this.id = id;
}

CatGlobe_ImpSys_Control.prototype.getName = function()
{
   return this.name;
}

CatGlobe_ImpSys_Control.prototype.setName = function(name)
{
   this.name = name;
}

CatGlobe_ImpSys_Control.prototype.getToolTipText = function()
{
   return this.toolTipText;
}

CatGlobe_ImpSys_Control.prototype.setToolTipText = function(toolTipText)
{
   this.toolTipText = toolTipText;
}

CatGlobe_ImpSys_Control.prototype.getValue = function()
{
   return this.value;
}

CatGlobe_ImpSys_Control.prototype.setValue = function(value)
{
   this.value = value;
}

CatGlobe_ImpSys_Control.prototype.getVisible = function()
{
   return this.visible;
}

CatGlobe_ImpSys_Control.prototype.setVisible = function(visible)
{
   this.visible = visible
}

CatGlobe_ImpSys_Control.prototype.getDisabled = function()
{
   return this.disabled;
}

CatGlobe_ImpSys_Control.prototype.setDisabled = function(disabled)
{
   this.disabled = disabled;
}

CatGlobe_ImpSys_Control.prototype.getLayout = function()
{
   return this.layout;
}

CatGlobe_ImpSys_Control.prototype.setLayout = function(layout)
{
   this.layout = layout;
}

// Events
CatGlobe_ImpSys_Control.prototype.getOnClick = function()
{
   return this.onClick;
}

CatGlobe_ImpSys_Control.prototype.setOnClick = function(onClick)
{
   this.onClick = onClick;
}

//
// Public methods
//

// Is abstract
CatGlobe_ImpSys_Control.prototype.Render = function()
{
   return "<hr>Please implement the Render() method<hr>";
}

// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_ButtonControl
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_ButtonControl(name)
{
   this.name = name;

   if(this.name == "undefined") this.name = "ImpSys_button";

   this.style = "";
   this.layout = new CatGlobe_ImpSys_ControlLayout();

   this.value = "";
   this.text = "Default button text";
   this.disabled = false;

   this.onClick = null;
}

// Inherit from CatGlobe_ImpSys_Control
CatGlobe_ImpSys_ButtonControl.prototype = new CatGlobe_ImpSys_Control();

//
// Private methods
//
CatGlobe_ImpSys_ButtonControl.prototype._GenerateStyle = function()
{
   var style = "";

   style +=
      this.layout.Style+
      ((this.layout.Width != "") ? "width : "+this.layout.Width+";" : "")+
      ((this.layout.Height != "") ? "height : "+this.layout.Height+";" : "")+
      "background-color : "+((this.disabled) ? this.layout.BackgroundColorDisabled : this.layout.BackgroundColorEnabled)+";"+
      "border-top: solid 2px #dfdfdf;"+
      "border-right: solid 2px #666666;"+
      "border-bottom: solid 2px #666666;"+
      "border-left: solid 2px #dddddd;"+
      "";

   return style;
}

//
// Public properties
//
CatGlobe_ImpSys_ButtonControl.prototype.getText = function()
{
   return this.text;
}

CatGlobe_ImpSys_ButtonControl.prototype.setText = function(text)
{
   this.text = text;
}

//
// Public methods
//
CatGlobe_ImpSys_ButtonControl.prototype.Render = function()
{
/*
   // NO GOOD FOR NEWEST VERSIONS OF IE
   var result =
      "<input "+
         "type=\"button\" "+
         "id=\""+this.name+"\" "+
         "name=\""+this.name+"\" "+
         "value=\""+this.text+"\" "+
         ((this.onClick != null) ? "onclick=\""+this._ParseFunctionName(this.onClick)+"("+this.value+");\" " : "")+
         "style=\""+this._GenerateStyle()+"\" "+
      "/>";
*/

   var result =
      "<table "+
         "border=\"0\" "+
         "style=\""+this._GenerateStyle()+"\" "+
      ">"+
         "<tr>"+
            "<td "+
               "align=\"center\" "+
               ((this.onClick != null) ? "onclick=\""+this._ParseFunctionName(this.onClick)+"("+this.value+");\" " : "")+
            ">"+
               ((this.text == "" || this.text == null) ? "&nbsp;" : this.text)+
            "</td>"+
         "</tr>"+
      "</table>";

   return result;
}

// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_WordControl
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_WordControl(word)
{
   this.word = word;

   this.style = "";
   this.layout = new CatGlobe_ImpSys_ControlLayout();
}

// Inherit from CatGlobe_ImpSys_Control
CatGlobe_ImpSys_WordControl.prototype = new CatGlobe_ImpSys_Control();

//
// Public properties
//
CatGlobe_ImpSys_WordControl.prototype.getWord = function()
{
   return this.word;
}

CatGlobe_ImpSys_WordControl.prototype.setWord = function(word)
{
   this.word = word;
}

//
// Public methods
//
CatGlobe_ImpSys_WordControl.prototype.Render = function()
{
   var impSysButtonControl = new CatGlobe_ImpSys_ButtonControl(this.word.getName());
   impSysButtonControl.setValue(this.value);
   impSysButtonControl.setText(this.word.getText());
   impSysButtonControl.setDisabled(this.disabled);
   impSysButtonControl.setLayout(this.layout);
   impSysButtonControl.setOnClick(this.onClick);

   return impSysButtonControl.Render();
}

/*
//
// Class CatGlobe_ImpSys_PictureControl
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_PictureControl(picture, disabledPicture)
{
   this.HtmlImage = new Image();

   this.picture = picture;
   this.disabledPicture = new CatGlobe_ImpSys_Picture("../../Images/Questionnaire/Viewer/ImpSys/checked.gif");

   if (picture != null && picture != "undefined") this.HtmlImage.src = this.picture.getUrl();
   if (disabledPicture != null && disabledPicture != "undefined") this.disabledPicture = disabledPicture;

   this.HtmlImage.src = this.disabledPicture.getUrl();

   this.style = "";
   this.layout = new CatGlobe_ImpSys_ControlLayout();
}

// Inherit from CatGlobe_ImpSys_Control
CatGlobe_ImpSys_PictureControl.prototype = new CatGlobe_ImpSys_Control();

//
// Private methods
//
CatGlobe_ImpSys_PictureControl.prototype._GenerateStyle = function()
{
   var style = "";

   style +=
      ((this.layout.Width != "") ? "width : "+this.layout.Width+";" : "")+
      ((this.layout.Height != "") ? "height : "+this.layout.Height+";" : "")+
      this.layout.Style;

   return style;
}

//
// Public properties
//
CatGlobe_ImpSys_PictureControl.prototype.getPicture = function()
{
   return this.picture;
}

CatGlobe_ImpSys_PictureControl.prototype.setPicture = function(picture)
{
   this.picture = picture;
}

CatGlobe_ImpSys_PictureControl.prototype.getDisabledPicture = function()
{
   return this.disabledPicture;
}

CatGlobe_ImpSys_PictureControl.prototype.setDisabledPicture = function(disabledPicture)
{
   this.disabledPicture = disabledPicture;
}

//
// Public methods
//

// Is implementation
CatGlobe_ImpSys_PictureControl.prototype.Render = function()
{
   var result = "";

   if(this.picture != null)
   {
      result += "<div style=\"position: relative; width: 100%; height: 100%; z-index: 1;\">";

      if(this.disabled)
      {
         result +=
            "<div style=\"position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; z-index: 2;\">"+
               "<img "+
                  "title=\""+this.toolTipText+"\" "+
                  "src=\""+this.disabledPicture.getUrl()+"\" "+
                  ((this.onClick != null) ? "onclick=\""+this._ParseFunctionName(this.onClick)+"("+this.value+");\" " : "")+
                  "style=\""+this._GenerateStyle()+"\" "+
               "/>"+
            "</div>";
      }

      result +=
            "<img "+
               "id=\""+this.picture.getName()+"\" "+
               "name=\""+this.picture.getName()+"\" "+
               "title=\""+this.toolTipText+"\" "+
               "src=\""+this.picture.getUrl()+"\" "+
               ((this.onClick != null) ? "onclick=\""+this._ParseFunctionName(this.onClick)+"("+this.value+");\" " : "")+
               "style=\""+this._GenerateStyle()+"\" "+
            "/>"+
         "</div>";
   }

   return result;
}

// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_ObjectControl
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_ObjectControl(impSysObject)
{
   this.impSysObject = impSysObject;

   this.control = null;

   this.layout = new CatGlobe_ImpSys_ControlLayout();
}

// Inherit from CatGlobe_ImpSys_Control
CatGlobe_ImpSys_ObjectControl.prototype = new CatGlobe_ImpSys_Control();

//
// Private methods
//

//
// Public properties
//
CatGlobe_ImpSys_ObjectControl.prototype.getControl = function()
{
   if(this.control == null)
   {
      switch(this.impSysObject.getType())
      {
         case CatGlobe_ImpSys_ObjectType_PICTURE:
            this.control = new CatGlobe_ImpSys_PictureControl(this.impSysObject);
            break;
         case CatGlobe_ImpSys_ObjectType_WORD:
            this.control = new CatGlobe_ImpSys_WordControl(this.impSysObject);
            break;
         default:
            return "";
            break;
      }
   }

   this.control.setId(this.id);
   this.control.setName(this.name);
   this.control.setValue(this.value);
   this.control.setDisabled(this.disabled);
   this.control.setToolTipText(this.toolTipText);
   this.control.setLayout(this.layout);
   this.control.setOnClick(this.onClick);

   return this.control;
}

CatGlobe_ImpSys_ObjectControl.prototype.setControl = function(control)
{
   this.control = control;
}

//
// Public methods
//
CatGlobe_ImpSys_ObjectControl.prototype.Render = function()
{
   return this.getControl().Render();
}

// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_QuestionControl
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_QuestionControl(impSysQuestion)
{
   this.impSysQuestion = impSysQuestion;

   this.impSysObjectSelectionControl = new CatGlobe_ImpSys_ObjectSelectionControl(this);

   this.selectedObject = null;
   this.selectedAssociation = null;

   this.style = "";
   this.layout = new CatGlobe_ImpSys_ControlLayout();
   this.layout.Width = "";
   this.layout.Height = "";

   this.objectLayout = new CatGlobe_ImpSys_ControlLayout();
   this.objectLayout.Width = "130px";
   this.objectLayout.Height = "";

   this.associatedObjectLayout = new CatGlobe_ImpSys_ControlLayout();
   this.associatedObjectLayout.Width = "130px";
   this.associatedObjectLayout.Height = "";

   this.objectColumnsCount = 4;
   this.objectDisplayWidth = "130px";
   this.objectDisplayHeight = "";

   this.associationColumnsCount = 1;
   this.associatedObjectDisplayWidth = "65px";
   this.associatedObjectDisplayHeight = "";

   this.onRefresh = null;
   this.onClick = null;
   this.onAssociationClick = null;
}

// Inherit from CatGlobe_ImpSys_Control
CatGlobe_ImpSys_QuestionControl.prototype = new CatGlobe_ImpSys_Control();

//
// Private methods
//
CatGlobe_ImpSys_QuestionControl.prototype._RenderObjects = function()
{
   var i, j;
   var result = "";
   var objects = this.impSysQuestion.getObjects();
   var relatedObjects = null;
   var impSysObjectControl = null;
   var borderColor = "#000000";

   result +=
      "<table "+
         "border=\"1\" "+
         "cellpadding=\"0\" "+
         "cellspacing=\"0\" "+
         "style=\""+
            ((this.layout.Width != "") ? "width: "+this.layout.Width+";" : "")+
            ((this.layout.Height != "") ? "height: "+this.leyout.Height+";" : "")+
         "\" "+
         ">";

   for(i = 0; i < objects.length; i++)
   {
      relatedObjects = objects[i].getRelatedObjects();

      if(i % this.objectColumnsCount == 0)
      {
         result += "<tr>";
      }

      result += "<td align=\"center\" valign=\"top\">";
      switch(objects[i].relationType)
      {
         case CatGlobe_ImpSys_RelationType_PICTURE2WORDS:
         case CatGlobe_ImpSys_RelationType_PICTURE2PICTURES:
            impSysObjectControl = new CatGlobe_ImpSys_ObjectControl(objects[i]);
            impSysObjectControl.getLayout().Width = this.objectDisplayWidth;
            impSysObjectControl.getLayout().Height = this.objectDisplayHeight;
            impSysObjectControl.getLayout().Style = "cursor: pointer;";
            impSysObjectControl.setValue(objects[i].getValue());
            impSysObjectControl.setOnClick(this.onClick);

            if(objects[i].getType() == CatGlobe_ImpSys_ObjectType_PICTURE)
            {
               impSysObjectControl.getControl().setDisabledPicture(new CatGlobe_ImpSys_Picture("../../Images/Questionnaire/Viewer/ImpSys/greyout.gif"));
            }

            if(objects[i].Validate())
            {
               impSysObjectControl.setDisabled(true);
               borderColor = this.layout.BackgroundColorDisabled;
            }
            else
            {
               impSysObjectControl.setDisabled(false);
               borderColor = this.layout.BackgroundColorEnabled;
            }

            result +=
               "<table "+
                  "border=\"0\" "+
                  "cellspacing=\"0\" "+
                  "cellpadding=\"0\" "+
                  "style=\""+
                     "border: solid "+borderColor+" 2px;"+
                     ((this.objectLayout.Width != "") ? "width: "+this.objectLayout.Width+";" : "")+
                     ((this.objectLayout.Height != "") ? "height: "+this.objectLayout.Height+";" : "")+
                  "\""+
                  ">"+
                  "<tr>"+
                     "<td align=\"center\" valign=\"top\">"+impSysObjectControl.Render()+"</td>"+
                  "</tr>"+
                  "<tr>"+
                     "<td "+
                        "align=\"center\" "+
                        "valign=\"top\" "+
                        ((this.onClick != null) ? "onclick=\""+this._ParseFunctionName(this.onClick)+"("+objects[i].getValue()+");\" " : "")+
                        "style=\"background-color: #ffffff; cursor: pointer;\">"+
                        this.impSysObjectSelectionControl.toolTipText+
                     "</td>"+
                  "</tr>";

            for(j = 0; j < objects[i].getMaxRelationsCount(); j++)
            {
               result +=
                  "<tr>"+
                     "<td align=\"center\" valign=\"top\">";

               if(j < relatedObjects.length)
               {
                  impSysObjectControl = new CatGlobe_ImpSys_ObjectControl(relatedObjects[j]);
                  impSysObjectControl.setValue(objects[i].getValue());

//                  impSysObjectControl.setOnClick(this.onClick);

                  if(objects[i].relationType == CatGlobe_ImpSys_RelationType_PICTURE2WORDS)
                  {
                     impSysObjectControl.setDisabled(true);
                  }
               }
               else
               {
                  if(objects[i].relationType == CatGlobe_ImpSys_RelationType_PICTURE2WORDS)
                  {
                     impSysObjectControl = new CatGlobe_ImpSys_WordControl(this.impSysQuestion.getDummyWord());
                     impSysObjectControl.setValue(objects[i].getValue());
                  }
                  else
                  {
                     impSysObjectControl = new CatGlobe_ImpSys_PictureControl(this.impSysQuestion.getDummyPicture());
                     impSysObjectControl.setValue(objects[i].getValue());
                  }
               }
               impSysObjectControl.getLayout().Width = this.objectDisplayWidth;
               impSysObjectControl.setToolTipText("");

               result +=
                        impSysObjectControl.Render();
                     "</td>"+
                  "</tr>";
            }

            result += "</table>";
            break;
         case CatGlobe_ImpSys_RelationType_WORD2WORDS:
         case CatGlobe_ImpSys_RelationType_WORD2PICTURES:
            break;
         default:
         case CatGlobe_ImpSys_RelationType_NONE:
            impSysObjectControl = new CatGlobe_ImpSys_ObjectControl(objects[i]);

            result += impSysObjectControl.Render();
            break;
      }
      result += "</td>";

      if(i % this.objectColumnsCount == this.objectColumnsCount - 1)
      {
         result += "</tr>";
      }
   }
   result += "</table>";

   return result;
}

CatGlobe_ImpSys_QuestionControl.prototype._RenderAssociations = function()
{
   var i;
   var result = "";
   var relations = this.impSysQuestion.getAssociations();
   var impSysObjectControl = null;

   result +=
      "<table "+
         "border=\"0\" "+
         "cellspacing=\"0\" "+
         "cellpadding=\"0\" "+
         "style=\""+
            "border: solid #000000 2px;"+
// ADD
//            "width: 200px;"+
//            "height: 300px;"+
         "\""+
         ">";
   for(i = 0; i < relations.length; i++)
   {
      impSysObjectControl = new CatGlobe_ImpSys_ObjectControl(relations[i]);
      impSysObjectControl.getLayout().Width = this.associatedObjectDisplayWidth;
      impSysObjectControl.setValue(i);
      impSysObjectControl.setToolTipText("");
      impSysObjectControl.setOnClick(this.onAssociationClick);

      if(relations[i].HasParentObjects())
      {
         impSysObjectControl.setDisabled(true);
         impSysObjectControl.setToolTipText("V&aelig;lg her");
         impSysObjectControl.getLayout().Style = "cursor: pointer;";
      }

      if(i % this.associationColumnsCount == 0)
      {
         result += "<tr>";
      }

      result +=
         "<td>"+
            impSysObjectControl.Render();
         "</td>";

      if(i % this.associationColumnsCount == this.associationColumnsCount - 1)
      {
         result += "</tr>";
      }
   }
   result += "</table>";

   return result;
}

//
// Public properties
//
CatGlobe_ImpSys_QuestionControl.prototype.getImpSysQuestion = function()
{
   return this.impSysQuestion;
}

CatGlobe_ImpSys_QuestionControl.prototype.setImpSysQuestion = function(impSysQuestion)
{
   this.impSysQuestion = impSysQuestion;
}

CatGlobe_ImpSys_QuestionControl.prototype.getSelectedObject = function()
{
   return this.selectedObject;
}

CatGlobe_ImpSys_QuestionControl.prototype.setSelectedObject = function(selectedObject)
{
   this.selectedObject = selectedObject;
}

CatGlobe_ImpSys_QuestionControl.prototype.getSelectedAssociation = function()
{
   return this.selectedAssociation;
}

CatGlobe_ImpSys_QuestionControl.prototype.setSelectedAssociation = function(selectedAssociation)
{
   this.selectedAssociation = selectedAssociation;
}

// Layout for objects
CatGlobe_ImpSys_QuestionControl.prototype.getObjectDisplayWidth = function()
{
   return this.objectDisplayWidth;
}

CatGlobe_ImpSys_QuestionControl.prototype.setObjectDisplayWidth = function(objectDisplayWidth)
{
   this.objectDisplayWidth = objectDisplayWidth;
}

CatGlobe_ImpSys_QuestionControl.prototype.getObjectDisplayHeight = function()
{
   return this.objectDisplayHeight;
}

CatGlobe_ImpSys_QuestionControl.prototype.setObjectDisplayHeight = function(objectDisplayHeight)
{
   this.objectDisplayHeight = objectDisplayHeight;
}

CatGlobe_ImpSys_QuestionControl.prototype.getObjectColumnsCount = function()
{
   return this.objectColumnsCount;
}

CatGlobe_ImpSys_QuestionControl.prototype.setObjectColumnsCount = function(objectColumnsCount)
{
   this.objectColumnsCount = objectColumnsCount;
}

// Layout for associated objects
CatGlobe_ImpSys_QuestionControl.prototype.getAssociatedObjectDisplayWidth = function()
{
   return this.associatedObjectDisplayWidth;
}

CatGlobe_ImpSys_QuestionControl.prototype.setAssociatedObjectDisplayWidth = function(associatedObjectDisplayWidth)
{
   this.associatedObjectDisplayWidth = associatedObjectDisplayWidth;
}

CatGlobe_ImpSys_QuestionControl.prototype.getAssociatedObjectDisplayHeight = function()
{
   return this.associatedObjectDisplayHeight;
}

CatGlobe_ImpSys_QuestionControl.prototype.setAssociatedObjectDisplayHeight = function(associatedObjectDisplayHeight)
{
   this.associatedObjectDisplayHeight = associatedObjectDisplayHeight;
}

CatGlobe_ImpSys_QuestionControl.prototype.getAssociationColumnsCount = function()
{
   return this.associationColumnsCount;
}

CatGlobe_ImpSys_QuestionControl.prototype.setAssociationColumnsCount = function(associationColumnsCount)
{
   this.associationColumnsCount = associationColumnsCount;
}

// Object selection control
CatGlobe_ImpSys_QuestionControl.prototype.getShowObjectSelectionControl = function()
{
   return this.impSysObjectSelectionControl.getVisible();
}

CatGlobe_ImpSys_QuestionControl.prototype.setShowObjectSelectionControl = function(show)
{
   this.impSysObjectSelectionControl.setVisible(show);
}

CatGlobe_ImpSys_QuestionControl.prototype.getObjectSelectionControl = function()
{
   return this.impSysObjectSelectionControl;
}

// Events
CatGlobe_ImpSys_QuestionControl.prototype.getOnRefresh = function()
{
   return this.onRefresh;
}

CatGlobe_ImpSys_QuestionControl.prototype.setOnRefresh = function(onRefresh)
{
   this.onRefresh = onRefresh;
}

CatGlobe_ImpSys_QuestionControl.prototype.getOnAssociationClick = function()
{
   return this.onAssociationClick;
}

CatGlobe_ImpSys_QuestionControl.prototype.setOnAssociationClick = function(onAssociationClick)
{
   this.onAssociationClick = onAssociationClick;
}

//
// Public methods
//
CatGlobe_ImpSys_QuestionControl.prototype.Refresh = function()
{
   if(this.onRefresh != null) this.onRefresh();
}

CatGlobe_ImpSys_QuestionControl.prototype.Render = function()
{
   var result = "";

   result +=
      "<table border=\"0\">";

   if(this.impSysQuestion.text != "")
   {
      result +=
         "<tr>"+
            "<td>"+this.impSysQuestion.text+"</td>"+
         "</tr>";
   }

   result +=
         "<tr>"+
            "<td>"+
               this.impSysObjectSelectionControl.Render()+
               "<table "+
                  "border=\"0\" "+
                  "cellspacing=\"0\" "+
                  "cellpadding=\"0\">"+
                  "<tr>"+
                     "<td align=\"center\" valign=\"top\">"+
                        this._RenderObjects()+
                     "</td>"+
                     "<td style=\"width: 5px;\"></td>"+
                     "<td align=\"center\" valign=\"top\">"+
                        this._RenderAssociations()+
                     "</td>"+
                  "</tr>"+
               "</table>"+
            "</td>"+
         "</tr>"+
      "</table>";

   return result;
}

/*
//
// Class CatGlobe_ImpSys_ObjectSelectionControl
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_ObjectSelectionControl(impSysQuestionControl)
{
   this.id = "objectSelectionControl";

   this.impSysQuestionControl = impSysQuestionControl;
   this.impSysQuestion = this.impSysQuestionControl.getImpSysQuestion();

   this.backgroundColor = "#ffffff";

   this.selectionText = "No text specified";

   this.width = 728;

   this.x = "";
   this.y = "";

   this.style = "";
   this.layout = new CatGlobe_ImpSys_ControlLayout();

   this.layout.Width = "";
   this.layout.Height = "";

   this.objectDisplayWidth = "260px";
   this.associatedObjectDisplayWidth = "130px";

   this.visible = false;

   this.columns = 4;
}

// Inherit from CatGlobe_ImpSys_Control
CatGlobe_ImpSys_ObjectSelectionControl.prototype = new CatGlobe_ImpSys_Control

//
// Private methods
//
CatGlobe_ImpSys_ObjectSelectionControl.prototype._GenerateStyle = function()
{
   var style = 
      "position: absolute;"+
      "z-index: 10;"+
      ((this.backgroundColor != "") ? "background-color: "+this.backgroundColor+";" : "")+
      ((this.y != "") ? "top: "+this.y+";" : "")+
      ((this.x != "") ? "left: "+this.x+";" : "")+
      ((this.layout.Width != "") ? "width: "+this.layout.Width+";" : "")+
      ((this.layout.Height != "") ? "height: "+this.layout.Height+";" : "")+
      this.style;

   return style;
}

//
// Public properties
//
CatGlobe_ImpSys_ObjectSelectionControl.prototype.getStyle = function()
{
   return this.style;
}

CatGlobe_ImpSys_ObjectSelectionControl.prototype.setStyle = function(style)
{
   this.style = style;
}

CatGlobe_ImpSys_ObjectSelectionControl.prototype.getSelectionText = function()
{
   return this.selectionText;
}

CatGlobe_ImpSys_ObjectSelectionControl.prototype.setSelectionText = function(selectionText)
{
   this.selectionText = selectionText;
}

CatGlobe_ImpSys_ObjectSelectionControl.prototype.getObjectDisplayWidth = function()
{
   return this.objectDisplayWidth;
}

CatGlobe_ImpSys_ObjectSelectionControl.prototype.setObjectDisplayWidth = function(objectDisplayWidth)
{
   this.objectDisplayWidth = objectDisplayWidth;
}

CatGlobe_ImpSys_ObjectSelectionControl.prototype.getAssociatedObjectDisplayWidth = function()
{
   return this.associatedObjectDisplayWidth;
}

CatGlobe_ImpSys_ObjectSelectionControl.prototype.setAssociatedObjectDisplayWidth = function(associatedObjectDisplayWidth)
{
   this.associatedObjectDisplayWidth = associatedObjectDisplayWidth;
}

//
// Public methods
//
CatGlobe_ImpSys_ObjectSelectionControl.prototype.Render = function()
{
   var i;
   var pictureControl = null;
   var objectControl = null;
   var associations = this.impSysQuestion.getAssociations();
   var result = "";
   var rows = Math.ceil(associations.length / this.columns) + 1;

   if(this.visible)
   {
      pictureControl = new CatGlobe_ImpSys_PictureControl(this.impSysQuestionControl.getSelectedObject());
      pictureControl.getLayout().Width = this.objectDisplayWidth;
      pictureControl.setToolTipText("");

      result +=
         "<div "+
            "id=\""+this.id+"\" "+
            "style=\""+this._GenerateStyle()+"\""+
         ">"+
            "<table "+
               "border=\"0\" "+
               "cellspacing=\"0\" "+
               "cellpadding=\"0\" "+
               "style=\""+
                  "border: solid #000000 2px;"+
               "\""+
               ">"+
               "<tr>"+
                  "<td "+
                     "rowspan=\""+rows+"\" "+
                     "valign=\"center\" "+
                     "align=\"center\" "+
                     "style=\""+
                        "border-right: solid #000000 2px;"+
                     "\""+
                     ">"+
                     pictureControl.Render()+
                  "</td>"+
                  "<td "+
                     "colspan=\""+this.columns+"\" "+
                     "valign=\"top\" "+
                     "height=\"1*\" "+
                     "style=\""+
                        "border-bottom: solid #000000 2px;"+
                     "\""+
                     ">"+
                     this.selectionText+
                  "</td>"+
               "</tr>";

      for(i = 0; i < associations.length; i++)
      {
         objectControl = new CatGlobe_ImpSys_ObjectControl(associations[i]);
         objectControl.getLayout().Width = this.associatedObjectDisplayWidth;
         objectControl.getLayout().Style = "cursor: pointer;";
         objectControl.setValue(i);
         objectControl.setOnClick(this.onClick);

         if(associations[i].HasParentObjects() && !associations[i].MoreAssociationsAvailable())
         {
            objectControl.setDisabled(true);
         }

         if(i % this.columns == 0)
         {
            result += "<tr>";
         }

         result += "<td>"+objectControl.Render()+"</td>";

         if(i % this.columns == this.columns - 1)
         {
            result += "</tr>";
         }
      }

      result +=
            "</table>"+   
         "</div>";
   }

   return result;
}

// ---------------------------------------------------------------- //
// Control
// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_ActionHandler
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_ActionHandler(impSysQuestionViewer)
{
   this.onRefresh = function() { alert("OnRefresh not implemented"); }
   this.onClick = function(wordValue) { alert("OnClick not implemented"); }
}

//
// Public properties
//
CatGlobe_ImpSys_ActionHandler.setOnRefresh = function(onRefresh)
{
   this.onRefresh = onRefresh;
}

CatGlobe_ImpSys_ActionHandler.setOnClick = function(onClick)
{
   this.onClick = onClick;
}

//
// Public methods
//
CatGlobe_ImpSys_ActionHandler.OnClick = function(value)
{
   this.onClick(value);
}

CatGlobe_ImpSys_ActionHandler.Select = function(index, associatedIndex)
{
}

// ---------------------------------------------------------------- //
// Interfacing
// ---------------------------------------------------------------- //

/*
//
// Class CatGlobe_ImpSys_QuestionnaireInterface
//
*/

//
// Constructor
//
function CatGlobe_ImpSys_QuestionnaireInterface(catGlobeQuestion, relationType, gender, maxReuseCount)
{
   this.catGlobeQuestion = catGlobeQuestion;
   this.relationType = relationType;
   this.gender = gender;                           // 1 - Male, 2 - Female

   this.impSysQuestion = null;
   this.impSysQuestionControl = null;

   this.maxReuseCount = maxReuseCount;
   if(this.maxReuseCount == null || this.maxReuseCount == "") this.maxReuseCount = 1;

   this.imagePath = "../../Images/Questionnaire/Viewer/ImpSys/";
   this.manImages = new Array();
   this.womanImages = new Array();
   this.animalImages = new Array();

   this._Init();

   // Apply the permutation
   this.impSysQuestion.ApplyPermutation();
}

//
// Private methods
//
CatGlobe_ImpSys_QuestionnaireInterface.prototype._InitWordAssociation = function()
{
   var i;
   var object, association;
   var questions = this.catGlobeQuestion.questions;
   var options = questions[0].options;

   this.impSysQuestion = new CatGlobe_ImpSys_Question(CatGlobe_ImpSys_RelationType_PICTURE2WORDS);
   for(i = 0; i < questions.length; i++)
   {
      object = new CatGlobe_ImpSys_Picture(questions[i].text)
      object.setName(questions[i].label);
      object.setValue(i);
      object.setMinRelationsCount(2);
      object.setMaxRelationsCount(2);

      this.impSysQuestion.AddObject(object);
   }
   for(i = 0; i < options.length; i++)
   {
      association = new CatGlobe_ImpSys_Word(options[i].text);
      association.setMaxParentObjectsCount(this.maxReuseCount);
      association.setValue(i);

      this.impSysQuestion.AddAssociation(association);
   }

   this.impSysQuestionControl = new CatGlobe_ImpSys_QuestionControl(this.impSysQuestion);
   this.impSysQuestionControl.setOnRefresh(Refresh);
   this.impSysQuestionControl.setAssociatedObjectDisplayWidth("130px");
   this.impSysQuestionControl.setOnClick(ShowObjectSelectionControl);
   this.impSysQuestionControl.setOnAssociationClick(DeSelect);

   var objectSelectionControl = this.impSysQuestionControl.getObjectSelectionControl();
   objectSelectionControl.setObjectDisplayWidth("200px");
   objectSelectionControl.setAssociatedObjectDisplayWidth("130px");
   objectSelectionControl.setSelectionText("<b>V&aelig;lg et af nedenst&aring;ende ord</b>");
   objectSelectionControl.setOnClick(Select);
}

CatGlobe_ImpSys_QuestionnaireInterface.prototype._InitPictureAssociation = function()
{
   var i;
   var object, association;
   var questions = this.catGlobeQuestion.questions;
   var options = questions[0].options;

   this.impSysQuestion = new CatGlobe_ImpSys_Question(CatGlobe_ImpSys_RelationType_PICTURE2PICTURES);
   for(i = 0; i < questions.length; i++)
   {
      object = new CatGlobe_ImpSys_Picture(questions[i].text)
      object.setName(questions[i].label);
      object.setValue(i);
      object.setMinRelationsCount(1);
      object.setMaxRelationsCount(1);

      this.impSysQuestion.AddObject(object);
   }
   for(i = 0; i < options.length; i++)
   {
      association = new CatGlobe_ImpSys_Picture(options[i].text);
      association.setMaxParentObjectsCount(this.maxReuseCount);
      association.setValue(i);

      this.impSysQuestion.AddAssociation(association);
   }

   this.impSysQuestionControl = new CatGlobe_ImpSys_QuestionControl(this.impSysQuestion);
   this.impSysQuestionControl.setOnRefresh(Refresh);
   this.impSysQuestionControl.setOnClick(ShowObjectSelectionControl);
   this.impSysQuestionControl.setOnAssociationClick(DeSelect);

   var objectSelectionControl = this.impSysQuestionControl.getObjectSelectionControl();
   objectSelectionControl.setOnClick(Select);
   objectSelectionControl.setObjectDisplayWidth("275.75px");
   objectSelectionControl.setAssociatedObjectDisplayWidth("130px");
   objectSelectionControl.setSelectionText("<b>V&aelig;lg et af nedenst&aring;ende dyrebilleder</b>");
}

CatGlobe_ImpSys_QuestionnaireInterface.prototype._ImageCaching = function()
{
   var i;
   var manImages = new Array();
   var womanImages = new Array();
   var animalImages = new Array();

   // Cache man images
   for(i = 0; i < this.manImages.length; i++)
   {
      manImages[i] = new Image();
      manImages[i].src = manImages[i];
   }

   // Cache woman images
   for(i = 0; i < this.womanImages.length; i++)
   {
      womanImages[i] = new Image();
      womanImages[i].src = womanImages[i];
   }

   // Cache animal images
   for(i = 0; i < this.animalImages.length; i++)
   {
      animalImages[i] = new Image();
      animalImages[i].src = manImages[i];
   }
}

CatGlobe_ImpSys_QuestionnaireInterface.prototype._Init = function()
{
   var i;
   var subQuestions = this.catGlobeQuestion.questions;
   var options = subQuestions[0].options;
   var images = null;

   for(i = 0; i < 8; i++)
   {
      this.manImages[i] = "man_no_"+(i + 1)+".png";
      this.womanImages[i] = "woman_no_"+(i + 1)+".png";
   }

   this.animalImages[0] = "koala.png"
   this.animalImages[1] = "elephant.png"
   this.animalImages[2] = "snake.png"
   this.animalImages[3] = "fox.png"
   this.animalImages[4] = "bird.png"
   this.animalImages[5] = "dog.png"
   this.animalImages[6] = "rabbit.png"
   this.animalImages[7] = "tiger.png"

   if(this.gender == 1)
   {
      images = this.manImages;
   }
   else
   {
      images = this.womanImages;
   }

   for(i = 0; i < subQuestions.length; i++)
   {
//      if(!isNaN(subQuestions[i].text))
//      {
//         subQuestions[i].text = this.imagePath+images[i];
//      }
      /*
         Because the implementation of ImpSys question is not complete. So user can not input the image or text
         new project will be fired to handle this problem.
         So, to prevent the bug may happen, whenever user input in subquestion text, images in system will always
         be shown.
      */
      subQuestions[i].text = this.imagePath+images[i];
   }

   if(this.relationType == CatGlobe_ImpSys_RelationType_PICTURE2WORDS)
   {
      this._InitWordAssociation();
   }
   else
   {
      for(i = 0; i < options.length; i++)
      {
         if(!isNaN(options[i].text))
         {
            options[i].text = this.imagePath+this.animalImages[(options[i].value - 1)];
         }
      }

      this._InitPictureAssociation();
   }

   this.impSysQuestion.setText(this.catGlobeQuestion.text);

   this._ParseAnswer();
}

CatGlobe_ImpSys_QuestionnaireInterface.prototype._ParseAnswer = function()
{
   var i, j;
   var objectIndex, associationIndex;
   var answer = this.catGlobeQuestion.answer;
   var objects = this.impSysQuestion.getObjects();
   var associations = this.impSysQuestion.getAssociations();

   var answerArray = answer.split("_|_");
   var optionsArray = null;

   for(i = 0; i < answerArray.length; i++)
   {
      optionsArray = answerArray[i].split("_:_");
      for(j = 1; j < optionsArray.length; j++)
      {
         if(optionsArray[j] != "")
         {
            objectIndex = parseInt(optionsArray[0]);
            associationIndex = parseInt(optionsArray[j]) - 1;

            objects[objectIndex].AddObject(associations[associationIndex]);
         }
      }
   }
}

//
// Public properties
//
CatGlobe_ImpSys_QuestionnaireInterface.prototype.getAnswer = function()
{
}

CatGlobe_ImpSys_QuestionnaireInterface.prototype.getImpSysQuestionControl = function()
{
   return this.impSysQuestionControl;
}

CatGlobe_ImpSys_QuestionnaireInterface.prototype.getMaxReuseCount = function()
{
   return this.maxReuseCount;
}

CatGlobe_ImpSys_QuestionnaireInterface.prototype.setMaxReuseCount = function(maxReuseCount)
{
   this.maxReuseCount = maxReuseCount;

   var i;
   var associations = this.impSysQuestion.getAssociations();

   for(i = 0; i < associations.length; i++)
   {
      associations[i].setMaxParentObjectsCount(this.maxReuseCount);
   }
}

//
// Public methods
//
CatGlobe_ImpSys_QuestionnaireInterface.prototype.Validate = function()
{
   var i;
   var objects = this.impSysQuestion.getObjects();
   var valid = true;

   for(i = 0; i < objects.length; i++)
   {
      valid = valid && objects[i].Validate();
   }

   return valid;
}

CatGlobe_ImpSys_QuestionnaireInterface.prototype.Render = function()
{
   var i, j, k;
   var objects = this.impSysQuestion.getObjects();
   var associations = null;
   var subQuestion = this.catGlobeQuestion.questions;
   var result = "";
   var value = "";

   // Render answers
   for(i = 0; i < subQuestion.length; i++)
   {
      value = "";

      for(j = 0; j < objects.length; j++)
      {
         if(objects[j].name == subQuestion[i].label)
         {
            associations = objects[j].getRelatedObjects();

            for(k = 0; k < associations.length; k++)
            {
               value += (parseInt(associations[k].value) + 1)+","
            }
         }
      }

      result += "<input type=\"hidden\" name=\""+subQuestion[i].label+"\" value=\""+value+"\" />";
   }

   result += this.impSysQuestionControl.Render();

   return result;
}

// ----------------------------------------------------------------------------------- //

function Refresh()
{
   var impSysQuestionControl = impSysQuestionnaireInterface.getImpSysQuestionControl();
   var impSysQuestionPH = document.getElementById("impSysQuestionPlaceHolder");

   impSysQuestionPH.innerHTML = impSysQuestionnaireInterface.Render();
}

function HideObjectSelectionControl()
{
   var impSysQuestionControl = impSysQuestionnaireInterface.getImpSysQuestionControl();

   impSysQuestionControl.setShowObjectSelectionControl(false);
   impSysQuestionControl.Refresh();
}

function ShowObjectSelectionControl(index, associatedIndex)
{
   var impSysQuestionControl = impSysQuestionnaireInterface.getImpSysQuestionControl();
   var object = impSysQuestionControl.getImpSysQuestion().getObjects()[index];

   if(associatedIndex != "undefined")
   {
   }

   impSysQuestionControl.setSelectedObject(object);
   impSysQuestionControl.setShowObjectSelectionControl(true);

   Refresh();
}

function Select(index)
{
   var impSysQuestionControl = impSysQuestionnaireInterface.getImpSysQuestionControl();
   var association = impSysQuestionControl.getImpSysQuestion().getAssociations()[index];

   impSysQuestionControl.getSelectedObject().AddObject(association);
   impSysQuestionControl.setSelectedObject(null);
   impSysQuestionControl.setShowObjectSelectionControl(false);

   impSysQuestionControl.Refresh();
}

function DeSelect(index)
{
   var impSysQuestionControl = impSysQuestionnaireInterface.getImpSysQuestionControl();
   var association = impSysQuestionControl.getImpSysQuestion().getAssociations()[index];
   var objects = association.getParentObjects();

   if(objects.length > 0)
   {            
      objects[0].RemoveObject(association);

      impSysQuestionControl.Refresh();
   }
}

