function QuestionSemantics(){
    this.sQuestionType = 'SEMANTICS';
    this.HORIZONTAL = 0;
    this.VERTICAL = 1;
    
    this.NONE = 0;
    this.LETTERS = 1;
    this.NUMBERS = 2;
    
    //System settings
    this.iInputCellWidth = 400;
    this.iRowHeight = 30;
    this.iTextCellPadding = 10;
    this.iSegmentPadding = 10;
    this.iHeaderMarginBottom = 5;
    this.sSemanticsLabelTag = 'strong';
    this.sLabelSuffix = ')&nbsp;&nbsp;';
    
    //Quiz settings
    this.oMaster = null;
    this.sServerSaveURL = '';
    this.bSaveParticipation = false;
    
    //User settings
    this.bDisplayBorder = true;
    this.sBorderColor = 'gray';
    this.iNbValue = 0;
    this.iOrientation = this.HORIZONTAL;
    this.iLabelType = this.NONE;
    this.bShowValue = false;
    
    //Variables
    this.sQuestionText = '';
    this.sTitle = null;
    this.sImageFileName = null;
    this.oVideoInfo = null;
    this.sAudioFileName = null;
    this.sSemanticsLabels = new Array();
    this.oInputElements = new Array();
    this.iCurrChoiceID = new Array();
    this.sImgsToPreload = new Array();
    this.iQuestionID = 0;
    this.condition = new Condition();
    this.sCommentTitle = null;
    this.sCommentText = '';
    
    //Mandatory functions
    this.display = function(oParentElement,iWidthAvail){
        var oTable = null;
        var oCurrRow = null;
        var oCurrCell = null;
        var oChoiceTable = null;
        var oChoiceRow = null;
        var oChoiceCell = null;
        var iInputCellWidth = 0;
        
        //Create p
        var oP = ns_buildHTMLElement('p',{style:'display:inline;'});
        oP.className = 'QuestionText';
        oP.innerHTML = this.sQuestionText;
        oParentElement.appendChild(oP);
        
        if(this.sQuestionText || this.sQuestionText.length > 0){
            oParentElement.appendChild(ns_buildHTMLElement('br',{}));
            oParentElement.appendChild(ns_buildHTMLElement('img',{src:'images/spacer.gif', width:'5', height:'19'}));
            oParentElement.appendChild(ns_buildHTMLElement('br',{}));
            oParentElement.appendChild(ns_buildHTMLElement('img',{src:'images/question_line.gif', width:iWidthAvail, height:'1'}));
            oParentElement.appendChild(ns_buildHTMLElement('br',{}));
            oParentElement.appendChild(ns_buildHTMLElement('img',{src:'images/spacer.gif', width:'5', height:'19'}));
            oParentElement.appendChild(ns_buildHTMLElement('br',{}));
        }
        
        //Create content
        oTable = ns_buildHTMLElement('table',{cellpadding:'0',cellspacing:'0',border:'0'});
        oParentElement.appendChild(oTable);
        
        if(this.bDisplayBorder){ 
            oTable.style.borderTop = 'solid 1px ' + this.sBorderColor;
            oTable.style.borderRight = 'solid 1px ' + this.sBorderColor;
        }
        
        for(var i = 0;i < this.sSemanticsLabels.length;i++){
            oCurrRow = oTable.insertRow(oTable.rows.length);
            //Input
            oCurrCell = oCurrRow.insertCell(0);
            if(this.bDisplayBorder){
                oCurrCell.style.borderLeft = 'solid 1px ' + this.sBorderColor;
                if(this.iOrientation == this.HORIZONTAL){
                    oCurrCell.style.borderBottom = 'solid 1px ' + this.sBorderColor;
                }
            }
            
            oCurrCell.height = this.iRowHeight;
            oCurrCell.align = 'left';
            oCurrCell.style.paddingLeft = this.iTextCellPadding + 'px';
            oCurrCell.style.paddingRight = this.iTextCellPadding + 'px';
            oCurrCell.style.paddingTop = this.iSegmentPadding + 'px';
            oCurrCell.style.paddingBottom = this.iSegmentPadding + 'px';
            
            oChoiceTable = ns_buildHTMLElement('table',{cellpadding:'0',cellspacing:'0',border:'0'});
            oChoiceRow = oChoiceTable.insertRow(0);
            oChoiceCell = oChoiceRow.insertCell(0);
            iInputCellWidth = iWidthAvail;
            
            if(this.iLabelType != this.NONE){
                if(this.iLabelType == this.LETTERS){
                    sLabel = ns_getLetter(i + 1) + this.sLabelSuffix;
                }else if(this.iLabelType == this.NUMBERS){
                    sLabel = (i + 1) + this.sLabelSuffix;
                }
                
                oChoiceCell.vAlign = 'middle';
                oChoiceCell.width = '25';
                oChoiceCell.rowSpan = '2';
                oChoiceCell.innerHTML = sLabel;
                oChoiceCell = oChoiceRow.insertCell(1);
                iInputCellWidth -= 25;
            }
            oChoiceCell.appendChild(this.getSemanticsHeader(i,iInputCellWidth));
            oChoiceRow = oChoiceTable.insertRow(1);
            oChoiceCell = oChoiceRow.insertCell(0);
            oChoiceCell.appendChild(this.getSemanticsInputs(i,iInputCellWidth));
            
            oCurrCell.appendChild(oChoiceTable);
            
            if(this.bDisplayBorder){
                oCurrCell.style.borderLeft = 'solid 1px ' + this.sBorderColor;
                oCurrCell.style.borderBottom = 'solid 1px ' + this.sBorderColor;
            }
        }
        
        oParentElement.appendChild(this.oMaster.buildCommentLayout(this));
    }
    
    this.save = function(){
        for(var i = 0;i < this.oInputElements.length;i++){
            for(var j = 0;j < this.oInputElements[i].length;j++){
                if(this.oInputElements[i][j].checked) {
                    this.iCurrChoiceID[i] = j;
                }
            }
        }
        
        //Comment
        if(this.sCommentTitle){
            this.sCommentText = $(this.oMaster.sTxtCommentPrefix + this.iQuestionID).value;
        }
    }
    
    this.submit = function(){
        if(this.bSaveParticipation){
            for(var i = 0;i < this.iCurrChoiceID.length;i++){
                this.oMaster.saveParticipation(this.iQuestionID,i,this.iCurrChoiceID[i]);
            }
            
            //Comment
            if(this.sCommentTitle){
                this.oMaster.saveComment(this.iQuestionID,this.sCommentText);
            }
        }
    }
    
    this.isAnswered = function() {
        for(var i = 0;i < this.iCurrChoiceID.length;i++){
            if(this.iCurrChoiceID[i] == null){
                return false;
            }
        }
        
        return true;
    }
    
    this.hide = function(){}
    
    this.setQuestionText = function(s){
        this.sQuestionText = s;
    }
    
    this.setTitle = function(s){
        this.sTitle = s;
    }
    this.setImage = function(s){
        this.sImageFileName = s;
        this.sImgsToPreload[this.sImgsToPreload.length] = s;
    }
    this.setLabelType = function(i){
        this.iLabelType = i;
    }
    this.setVideo = function(s,iWidth,iHeight){
        this.oVideoInfo = {sFileName:'',iWidth:0,iHeight:0};
        this.oVideoInfo.sFileName = s;
        this.oVideoInfo.iWidth = iWidth;
        this.oVideoInfo.iHeight = iHeight;   
    }
    this.setAudio = function(s){
        this.sAudioFileName = s;
    }
    //Question specific functions
    this.addChoice = function(sLabel1, sLabel2){
        var index = this.sSemanticsLabels.length;
        
        this.sSemanticsLabels[index] = new Array();
        this.sSemanticsLabels[index][0] = sLabel1;
        this.sSemanticsLabels[index][1] = sLabel2;
        this.iCurrChoiceID[index] = null;
    }
    this.setNbValue = function(iNb){
        this.iNbValue = iNb;
    }
    this.setOrientation = function(i){
        this.iOrientation = i;
    }
    this.getSemanticsHeader = function(index,iWidthAvail){
        
        var oTable = ns_buildHTMLElement('table',{cellpadding:'0',cellspacing:'0',border:'0'});
        oTable.style.marginBottom = this.iHeaderMarginBottom + 'px';
        var oCurrRow = oTable.insertRow(0);
        var oCurrCell = oCurrRow.insertCell(0);
        oCurrCell.align = 'left';
        oCurrCell.width = Math.round(iWidthAvail / 2);
        oCurrCell.innerHTML = '<' + this.sSemanticsLabelTag + '>&nbsp;' + this.sSemanticsLabels[index][0].replace(/ /g,'&nbsp;') + '&nbsp;</' + this.sSemanticsLabelTag + '>';
        
        oCurrCell = oCurrRow.insertCell(1);
        oCurrCell.align = 'right';
        oCurrCell.width = Math.round(iWidthAvail / 2);
        
        
        oCurrCell.innerHTML = '<' + this.sSemanticsLabelTag + '>&nbsp;' + this.sSemanticsLabels[index][1].replace(/ /g,'&nbsp;') + '&nbsp;</' + this.sSemanticsLabelTag + '>';
        
        return oTable;
    }
    this.getSemanticsInputs = function(iChoiceID,iWidthAvail){
        var oTable = ns_buildHTMLElement('table',{cellpadding:'0',cellspacing:'0',border:'0'});
        var oCurrRow = oTable.insertRow(0);
        var oCurrCell = null;
        var oSpanNb = null;
        var sChecked = '';
        this.oInputElements[iChoiceID] = new Array();
        
        for(var i = 0;i < this.iNbValue;i++){
            oCurrCell = oCurrRow.insertCell(i);
            oCurrCell.align = 'center';
            oCurrCell.width = Math.round(iWidthAvail / this.iNbValue);
            sChecked = (i == this.iCurrChoiceID[iChoiceID] ? 'checked' : null);
            this.oInputElements[iChoiceID][i] = ns_buildHTMLElement('input',{type:'radio',name:'rdo-' + this.iQuestionID + '-' + iChoiceID,id:'rdo-' + this.iQuestionID + '-' + iChoiceID + '-' + i,value:i,checked:sChecked}); 
            oCurrCell.appendChild(this.oInputElements[iChoiceID][i]);
            if(this.bShowValue){
                oCurrCell.appendChild(ns_buildHTMLElement('br',{}));
                oSpanNb = ns_buildHTMLElement('span',{});
                oSpanNb.innerHTML = (i + 1);
                oCurrCell.appendChild(oSpanNb);
            }
        }
        
        return oTable;
    }
}
