/**
*  questioncontent.js: the 3 basic question containers
*
*  Last Checked In By $Author: sasman $
*  $Date: 2005/03/09 18:35:30 $
*  $Revision: 1.16 $
*
*  (c) 2004 Copyright Basis Applied Technology
*  CONFIDENTIAL INFORMATION
*  All rights are reserved. Copying or other reproduction of
*  this program except for archival purposes is prohibited
*  without the prior written consent of Basis Applied Technology
*
*  Basis Applied Technology
*  Gibsons, BC
*  604.886.0721
*
*  REVISION HISTORY
*
*  $Log: questioncontent.js,v $
*  Revision 1.16  2005/03/09 18:35:30  sasman
*  obfuscation
*
*  Revision 1.15  2005/03/09 09:50:48  sasman
*  obfuscation
*
*  Revision 1.14  2005/03/04 15:35:07  sasman
*  *** empty log message ***
*
*  Revision 1.13  2005/01/17 13:09:22  sasman
*  *** empty log message ***
*
*  Revision 1.12  2004/12/15 21:32:49  sasman
*  added beginnings of proper dialog support. added sharing to contetn objects. lots of ui bug fixes.
*
*  Revision 1.11  2004/11/29 19:14:25  sasman
*  *** empty log message ***
*
*  Revision 1.9  2004/11/25 20:47:53  sasman
*  more reporting support integration. plus added some other goodies to domapi codebase (splitpane and more flexible object creation)
*
*  Revision 1.8  2004/11/22 17:17:23  sasman
*  implemented security tokens and cascading rights on content objects.
*
*  Revision 1.7  2004/11/16 13:47:52  sasman
*  *** empty log message ***
*
*  Revision 1.5  2004/11/03 17:18:59  sasman
*  more preparation for obfuscation compatibility
*
*  Revision 1.4  2004/11/03 09:11:26  sasman
*  prepared scripts for obfuscation and checking in initial _c files
*
*  Revision 1.2  2004/10/27 14:49:58  sasman
*  *** empty log message ***
*
*  Revision 1.1  2004/10/15 17:21:47  sasman
*  *** empty log message ***
*
*
*/

/*
* put in a 'show buttons' arg so that they can be easily grouped.
*
*/

core.loadUnit('base/contentobject');
core.loadUnit('base/tooltip');

if (CONTENT_CENTRE)
{ core.loadUnit('base/questioncontentprops'); }


core.registerComponent('questioncontent');

// --------------------------------------------------------
core.comps['questioncontent'].getAnonymous = function()
{
    return this.m_anonymous;
};
// --------------------------------------------------------
core.comps['questioncontent'].getAllowResubmit = function()
{
    return this.m_resubmit;
};
// --------------------------------------------------------
core.comps['questioncontent'].getAllowMultiples = function()
{
    return this.m_multiples;
};
// --------------------------------------------------------
core.comps['questioncontent'].getUpdateScores = function()
{
    return this.m_scores;
};
// --------------------------------------------------------
core.comps['questioncontent'].hasAnswered = function()
{
    return this.m_answered;
};
// --------------------------------------------------------
core.comps['questioncontent'].submit = function()
{   
    // sending up the context here is fine. in most cases, the context is used for security.
    // since we already have read rights in order for this to work, it stands to reason that
    // sending up the context doesn'ty constitute any breach in security. also, it's required
    // in oder to make sure the answer will be accepted. 
    var response = core.xmlrpc.Call(this.server, base.key, 'Question.UpdateResult', [this.getObjectID(),this.getAnswer(),this.getContext()]);
    if (response instanceof XMLRPCFault)
    { base._handleFault(response); }
    else
    {
        alert("Thank you. Your response has been submitted.");
        
        this.m_answered = true;
        
        this._fixSubmit();
        // if i allow multiples, i still don't want someone answering the same question ten times on the same page, so grey it out.
        if (!base.loggedIn() || this.getAnonymous() || !this.getAllowResubmit() || this.getAllowMultiples())
        {
           //this.m_submit.disabled = true;
           this.m_submit.style.visibility = 'hidden';
        }        
    }
};

// ------------------------------------------------------------------------------------------------------

core.registerComponent('textquestioncontent');

eval("TextQuestionContent = " + function( obj )
{
    var me = core._inherit2( obj, ['contentobject','questioncontent','textquestioncontent'] );
    me.init( obj );
    me.reDraw();      
    return me;
});

core.comps['textquestioncontent']._setProps = function( obj )
{    
    core.comps['contentobject']._setProps.call( this, obj );
    
    this.server = core.rVal(obj.server, 'services/svc_question.php');
    
    this._createContentArea();    

    if (this.getObjectID() == 0) {
        this.m_question.innerHTML = "Ask your Question Here.";
    }
    else
    {        
        this._initCommonProps(obj);               
        this._fixSubmit();                
        this.m_answer.value = obj['ContentData']['result'];                                                                
    }           
};

core.comps['questioncontent']._initCommonProps = function(obj)
{
    this.m_anonymous = obj['ContentData']['anonymous'];
    this.m_resubmit  = obj['ContentData']['resubmit'];
    this.m_multiples = obj['ContentData']['multiples'];
    this.m_scores    = obj['ContentData']['update_scores'];
    this.m_answered  = obj['ContentData']['answered'];
    this.m_note      = obj['ContentData']['note'];        
    this.m_question.innerHTML = obj['ContentData']['question'];
         
    core.disallowSelect(this.m_feedback);
    core.Elm2(this.m_feedback);
    this.m_feedback.innerHTML = (this.m_note.length > 0) ? ('Feedback') : "";    
    this.m_feedback.style.cursor = core.cursors.hand;
    this.m_feedback.style.textDecoration = 'underline';
    this.m_feedback.style.color = this.theme.selBgColor;
    this.m_feedback.style.marginRight = '5px';
    this.m_feedback.theme = this.theme;
    this.m_feedback.setTooltip(this.m_note);    
};
    
core.comps['questioncontent']._fixSubmit = function()
{
    this.m_submit.disabled = false;
    this.m_submit.innerHTML = 'submit';        
            
    // at this stage, if i'm the visitor, i can always answer again.    
    if (!base.loggedIn())
    {
        return;            
    }      
    
    // ...from this point on, we're logged in, and we have answered the question!
    if ( this.hasAnswered() )
    {
        if (this.getAnonymous())
        {
            if (!this.getAllowMultiples())
            { this.m_submit.disabled = true; }
        }
        else
        {
            if (!this.getAllowMultiples())
            {
                if (this.getAllowResubmit())
                { this.m_submit.innerHTML = 'change'; }
                else
                {this.m_submit.disabled = true;}
            }
            else
            {
                this.m_submit.innerHTML = 'submit another';
            }
        }                      
    }                             
};

core.comps['textquestioncontent'].getQuestion = function()
{
    return this.m_question.innerHTML;
};
core.comps['textquestioncontent'].getAnswer = function()
{
    return this.m_answer.value;
};
core.comps['textquestioncontent']._createContentArea = function()
{
    if (!this.m_inited)
    {
        this.m_inited = true;
        
        var HTML = "<table width='100%' height='100%' border=0 cellpadding='0px' cellspacing='5px'>" +
                    "<tbody>" +
                        "<tr height='1px' width=100%>" +
                            "<td valign='top'><span></span></td>" +
                        "</tr>" +
                        "<tr>" +
                            "<td><textarea></textarea></td>" +
                        "</tr>" +
                        "<tr height='1px'>" +
                            "<td valign ='bottom' align='right'>" +
                                "<span></span><button>submit</button>" +
                            "</td>" +
                        "</tr>" +
                    "</tbody>" +
                    "</table>";        
                    
        this.setText(HTML);
        
        var qtn = this.getElementsByTagName('span')[0];        
        this.m_question = qtn;
         
        var ans = this.getElementsByTagName('textarea')[0];
        ans.style.height = '100%';
        ans.style.width = '100%';
        this.m_answer = ans;
        
        var btn = this.getElementsByTagName('button')[0];        
        btn.parent = this;
        btn.onclick = function(){this.parent.submit();};        
        btn.style.cursor = core.cursors.hand;
        this.m_submit = btn;        
        
        this.m_feedback = this.getElementsByTagName('span')[1];          
                
        if (this.editicon) { this.appendChild(this.editicon); }   
    }
};

core.comps['textquestioncontent'].reDraw = function()
{
    if (this.editicon)
    { this.editicon.posType = 'bottom-left'; }
    
    core.comps['contentobject'].reDraw.call( this );            
    
    var o;
    o = this.m_question;
    o.style.font = this.theme.font;    
    
    o = this.m_answer;
    o.style.font = this.theme.ctrlFont;
    
    o = this.m_submit;
    o.style.font = this.theme.ctrlFont;    
};

core.comps['textquestioncontent'].setW = function(w)
{
    core.elmProto.setW.call(this,w);
    this.layout();
};

core.comps['textquestioncontent'].setH = function(h)
{
    core.elmProto.setH.call(this,h);    
    this.layout();
};

// ----------------------------------------------------------------------------------------------------------------

core.loadUnit('slider2');
core.registerComponent('sliderquestioncontent');

eval("SliderQuestionContent = " + function( obj )
{
    var me = core._inherit2( obj, ['contentobject','questioncontent','sliderquestioncontent'] );
    me.init( obj );
    me.reDraw();
    return me;
});

core.comps['sliderquestioncontent'].SLIDERH  = 21;
core.comps['sliderquestioncontent'].SLIDERIMG  = core.imagePath+'sliders/horz_line_1_thumb.gif';
core.comps['sliderquestioncontent'].SLIDERIMGW = 11;
core.comps['sliderquestioncontent'].SLIDERIMGH = 21;
core.comps['sliderquestioncontent'].SLIDERBG   = core.imagePath+'sliders/generic_bg.gif';
core.comps['sliderquestioncontent'].SLIDERBGH  = 7;

core.comps['sliderquestioncontent']._setProps = function( obj )
{
    core.comps['contentobject']._setProps.call( this, obj );    
    this.server = core.rVal(obj.server, 'services/svc_question.php');
    
    this._createContentArea();    

    if (this.getObjectID() == 0) {
        this.m_question.innerHTML = "Ask your Question Here.";
    }
    else {                
        this._initCommonProps(obj);               
        this._fixSubmit();                
        this.m_answer.setPos((obj['ContentData']['result']*100.0), 0);        
    }
    
    this.setW(this.getW());
};
core.comps['sliderquestioncontent'].getQuestion = function() { return this.m_question.innerHTML; };

// for now, i'm forcing scalar values... range is always from 0.0F to 1.0F.
core.comps['sliderquestioncontent'].getAnswer = function() { return this.m_answer.xPercent/100.0; };
core.comps['sliderquestioncontent'].getMin = function() { return 0; };
core.comps['sliderquestioncontent'].getMax = function() { return 1; };
core.comps['sliderquestioncontent'].getMinLabel = function() { return ""; };
core.comps['sliderquestioncontent'].getMaxLabel = function() { return ""; };

core.comps['sliderquestioncontent']._createContentArea = function()
{
    if (!this.m_inited)
    {
        this.m_inited = true;
        
        var HTML = "<table width='100%' height='100%' border=0 cellpadding='5px' cellspacing='0px'>" +
                    "<tbody>" +
                        "<tr height='1px' width=100%>" +
                            "<td valign='top'><span></span></td>" +
                        "</tr>" +
                        "<tr height='" + this.SLIDERH + "px'>" +
                            "<td><div></div></td>" +
                        "</tr>" +
                        "<tr>" +
                            "<td valign ='bottom' align='right'>" +
                                "<span></span><button>submit</button>" +
                            "</td>" +
                        "</tr>" +
                    "</tbody>" +
                    "</table>";        
                    
        this.setText(HTML);
        
        this.m_tbl = this.getElementsByTagName('table')[0];
        
        var qtn = this.getElementsByTagName('span')[0];        
        this.m_question = qtn;
                 
        //var td  = this.getElementsByTagName('td')[1];        
        var div  = this.getElementsByTagName('div')[0];        
        var ans = Slider2({elm:div, theme:this.theme, tw: this.SLIDERIMGW, th: this.SLIDERIMGH, bgImg:this.SLIDERBG, thumbImg: this.SLIDERIMG});                 
        
        ans.thumb.style.cursor=core.cursors.hand;        
        ans.setH(this.SLIDERH);
        
        this.m_answer = ans;
        
        var btn = this.getElementsByTagName('button')[0];        
        btn.parent = this;
        btn.onclick = function(){this.parent.submit();};        
        btn.style.cursor = core.cursors.hand;
        this.m_submit = btn;                        
        
        this.m_feedback = this.getElementsByTagName('span')[1];          
        
        if (this.editicon) { this.appendChild(this.editicon); }        
    }   
};

core.comps['sliderquestioncontent'].reDraw = function()
{
    if (this.editicon)
    { this.editicon.posType = 'bottom-left'; }
    
    core.comps['contentobject'].reDraw.call( this );            
    
    var o;
    o = this.m_question;
    o.style.font = this.theme.font;    
    
    o = this.m_answer;
    o.setBgColor('');
    
    o = this.m_submit;
    o.style.font = this.theme.ctrlFont;    
};

core.comps['sliderquestioncontent'].setW = function(w)
{
    core.elmProto.setW.call(this,w);
    
    var ans = this.m_answer;
    if (ans)
    {
        var off = (this.m_tbl.cellSpacing*2) + (this.m_tbl.cellPadding*2);        
        ans.setW(w - off - 2);
        
        var val = ans.xPercent;
        ans.setRange(0,0,ans.getW()-this.SLIDERIMGW,0);   
        ans.bgImage.setSize(ans.getW(),this.SLIDERBGH);        
        ans.bgImage.setY(6);
        ans.setPos(val,0);
    }
        
    this.layout();
};

core.comps['sliderquestioncontent'].setH = function(h)
{
    core.elmProto.setH.call(this,h);    
    this.layout();
};

// ----------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------

core.registerComponent('mcquestioncontent');
 
eval("MCQuestionContent = " + function( obj )
{
    var me = core._inherit2( obj, ['contentobject','questioncontent','mcquestioncontent'] );
    me.init( obj );
    me.reDraw();
    return me;
});

core.comps['mcquestioncontent']._setProps = function( obj )
{
    core.comps['contentobject']._setProps.call( this, obj );        
    this.server = core.rVal(obj.server, 'services/svc_question.php');    
    
    this._createContentArea();    
    
    // here, we need to refresh the choice list with current data _before_ we set the answer.        
    var arr;
    if (this.getObjectID() == 0)
    { arr = new Array(); }
    else
    { arr = obj['ContentData']['choices']; }
    this.setChoices(arr);
    	                        
    if (this.getObjectID() == 0) {
        this.m_question.innerHTML = "Ask your Question Here.";
    }
    else {
        
        this._initCommonProps(obj);               
        this._fixSubmit();                        
    
        var r = this.m_tbl.firstChild.firstChild;
	    var input;
	    var result = obj['ContentData']['result'];                                                        
        
        // this is the first row that contains an option.
	    var r = this.m_tbl.firstChild.firstChild;
	
	    var input;
	    while (r)
	    {
	        input = r.firstChild.firstChild;
	        if (input.value == result)
	        {
	            input.checked = true;
	            break;
	        } 	   
	        r = r.nextSibling;
	    }	                  
    }
        
    this.setW(this.getW());
};
core.comps['mcquestioncontent'].getQuestion = function() { return this.m_question.innerHTML; };

// for now, i'm forcing scalar values... range is always from 0.0F to 1.0F.
core.comps['mcquestioncontent'].getAnswer = function()
{    
    // this is the first row that contains an option.
	var r = this.m_tbl.firstChild.firstChild;
	
	var input;
	var ans = 0;
	
	while (r)
	{
	    input = r.firstChild.firstChild;
	    if (input.checked)
	    {
	        ans = input.value;
	        break;
	    } 	   
	    r = r.nextSibling;
	}
	return ans;
};

core.comps['mcquestioncontent'].getChoices = function()
{
    return this.m_choices;
};

core.comps['mcquestioncontent'].setChoices = function(c)
{
    this.m_choices = c;
    var t = this.m_tbl;
    var r = this.m_cloneRow;
           
    while (t.rows.length > 0) {t.deleteRow(0);}
        
    var rn; 
    var input;
    var choice;   
    for (var i=0 ; i<c.length ; i++)    
    {
        rn = r.cloneNode(true);                                
        input = rn.getElementsByTagName('input')[0];        
        input.value = c[i]['id'];
        input.owner = this;                
        input.onclick = function(){this.owner.onAnswerSelected(this);};
                    
        choice = rn.getElementsByTagName('span')[0];
        choice.innerHTML = c[i]['value'];
        choice.style.font = this.theme.font;
        t.firstChild.appendChild(rn);
    }
};

core.comps['mcquestioncontent'].onAnswerSelected = function(input)
{
    this.m_answer = input.value;
};
    
core.comps['mcquestioncontent']._createContentArea = function()
{    
    if (!this.m_inited)
    {
        this.m_inited = true;
        
        var HTML = "<table height='100%' width='100%' border=0 cellpadding='5px' cellspacing='0px'>" +
                   "<tbody>" +
                        "<tr height='1px' width=100%>" +
                            "<td valign='top'><span></span></td>" +
                        "</tr>" +
                        "<tr height='1px'>" +
                            "<td><table border=0 cellpadding='1px' cellspacing='0px'><tbody><tr width=100%><td width='20px'><input type='radio' name='"+core.guid()+"' value=0' /></td><td><span></span></td></tr></tbody></table></td>" +
                        "</tr>" +
                        "<tr>" +
                            "<td valign ='bottom' align='right'>" +
                                "<span></span><button>submit</button>" +
                            "</td>" +
                        "</tr>" +
                    "</tbody>" +
                    "</table>";        
                    
        this.setText(HTML);
                        
        var qtn = this.getElementsByTagName('span')[0];        
        this.m_question = qtn;
                         
        this.m_tbl = this.getElementsByTagName('table')[1];
        this.m_cloneRow = this.m_tbl.firstChild.removeChild(this.m_tbl.firstChild.firstChild);
        
        var btn = this.getElementsByTagName('button')[0];        
        btn.parent = this;
        btn.onclick = function(){this.parent.submit();};        
        btn.style.cursor = core.cursors.hand;
        this.m_submit = btn;                        
        
        this.m_feedback = this.getElementsByTagName('span')[1];               
    }   
};

core.comps['mcquestioncontent'].reDraw = function()
{
    if (this.editicon)
    { this.editicon.posType = 'bottom-left'; }
    
    core.comps['contentobject'].reDraw.call( this );            
    
    var o;
    o = this.m_question;
    o.style.font = this.theme.font;    
    
    o = this.m_submit;
    o.style.font = this.theme.ctrlFont;           
};

core.comps['mcquestioncontent'].setW = function(w)
{
    core.elmProto.setW.call(this,w); 
    this.layout();
};

core.comps['mcquestioncontent'].setH = function(h)
{
    core.elmProto.setH.call(this,h);    
    this.layout();
};

core.comps['mcquestioncontent'].submit = function()
{   
    if (this.getAnswer() == 0)
    { alert("You must make a choice in order to submit your answer."); }
    else
    { core.comps['questioncontent'].submit.call(this); }
};
