/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */

componentPollOpenTextAnswer = function(name) 
{
	this.__componentText = __componentText;
	this.__componentText(name);

	this.interactions = true;
	this.interactionsType = "long-fill-in";  

	this.interactions = true;
};

componentPollOpenTextAnswer.prototype = new __componentText;
componentPollOpenTextAnswer.prototype.type = "componentPollOpenTextAnswer";

componentPollOpenTextAnswer.prototype.onCreate = function(obj)
{
	//this.captureMouseEvents();
	this.pollQuestion = this.initDynamicContent(this.pollQuestion);
};

componentPollOpenTextAnswer.prototype._getStyleDef = function()
{
	var styleDef = ""
	var borderStyle = (this.borderColor != null ? ("border: 1px solid " + this.borderColor + ";") : "");
	styleDef = "style='background-color: white; border: 1px solid #000000; width:100%;'" ;

	return styleDef;
};

componentPollOpenTextAnswer.prototype._setHTMLDef = function() 
{
	var t = "<form name='" + this.id + "Form' id='" + this.id + "Form'>";
	t += '<table style="width:' + this.w + 'px; height: '+this.h+'"><tr><td valign="top" style="color:' + this.fontColor + '; font-family:' + this.fontFamily + '; font-size:' + getFontSize(this.fontSize, 'px') + '; line-height:'+this.lineHeight+';" dir="'+this.dir+'">';
	t += this.pollQuestion + '</td></tr>';
	t += '<tr><td valign="bottom">';
	t += "<textarea name='txtarea' value='' " + "cols='" + this.cols + "' rows='" + this.rows + "' ";
	t += this._getStyleDef();
	
	t += ' onfocus="WBTObject.all[\''+this.id+'\'].invokeEvent(\'FocusIn\')"';
	t += ' onblur="WBTObject.all[\''+this.id+'\'].invokeEvent(\'FocusOut\')"';
	t += ' onkeydown="WBTObject.all[\''+this.id+'\'].invokeEvent(\'KeyDown\')"';
	t += ' onkeyup="WBTObject.all[\''+this.id+'\'].fnOnKeyUp()"';
	t += ' onkeypress="WBTObject.all[\''+this.id+'\'].fnOnKeyPress()"';
	t += ' onchange="WBTObject.all[\''+this.id+'\'].fnOnChange()"';
	t += ">";
	t += "</textarea>"
	t += '</td></tr></table>';
	t += "</form>";
	
	this.setHTML(t);
};

componentPollOpenTextAnswer.prototype.getAnswer = function() 
{ 
	var valStr  = "";
	var form = page.doc.forms[this.id + "Form"];
	if(form)
	if (this.caseSensitive == false) valStr = form[0].value.toLowerCase();
		else valStr = form[0].value;
	//pg2-skasowanie enterow
	var s = escape(valStr);
	while (s.indexOf("%0A") > -1) s = s.replace("%0A", " ");
	while (s.indexOf("%0D") > -1) s = s.replace("%0D", " ");
	s = unescape(s);
	return s; //valStr;
};

componentPollOpenTextAnswer.prototype.setAnswer = function(valStr) 
{
	var form = page.doc.forms[this.id + "Form"];
	if(form)
	form[0].value = valStr;
	return valStr;
};

componentPollOpenTextAnswer.prototype.baseUnselectable = componentPollOpenTextAnswer.prototype.unselectable;
componentPollOpenTextAnswer.prototype.unselectable = function() 
{
	this.baseUnselectable();
	
	var form = page.doc.forms[this.id + "Form"];
	if(form) form[0].unselectable = "";
};

componentPollOpenTextAnswer.prototype.onPaint = function(obj) 
{
	if(this.bgColor == null)
	this.bgColor = is.ns4 ? null : 'transparent';

	if((this.borderColor == null) || (this.borderColor == "null") || (this.borderColor == "" ))
	this.borderColor = null;

	var w = this.w;
	var h = this.h;
	this.w = ( ( w == null ) ? this.w : ( w < 0 ? 0 : w ) );
	this.h = ( ( h == null ) ? this.h : ( h < 0 ? 0 : ( !is.ns4 ? h : 40 ) ) );
	
	var textarea = document.getElementById(this.id+"textarea");
	
	if (is.moz)
	{
		textarea.rows = this.rows-1;
	}else{
		textarea.rows = this.rows;	
	}

	if((this.w == null) && (this.h == null)) { }  
	else
	if(this.style !=null) 
	{
		if(is.ns4) 
		{
			this.style.clip.width = this.w;
			this.style.clip.height = this.h;
		}
		else 
		{
			this.style.width = this.w;
			this.style.height = this.h;
			this.style.clip = 'rect(0px ' + (this.w||0) + 'px ' + (this.h||0) + 'px 0px)';
		}
	}
	if (this.execution) {
		this.contentObj = this.findContentObj();
		if (this.contentObj) {
			this.contentObj.innerHTML = this.initText(this.contentObj.innerHTML);
			this.reinit();
		}
	} else {
		this._setHTMLDef();
	}
};

componentPollOpenTextAnswer.prototype.splitAnswer = function(s) 
{
	var ans = new Array();
	if(s.charAt(0) == '"') s = s.substr(1, s.length);
	if(s.charAt(s.length-1) == '"') s = s.substr(0, s.length-1);
	ans = s.split('||');
	//
	for(var i=0; i < ans.length; i++) {
	ans[i] = ans[i].replace(new RegExp('#&034', "g"), '"');
	}
	return(ans);
};

componentPollOpenTextAnswer.prototype.checkAnswer = function() 
{
	return(this.parseCheckAnswer(this));
};

componentPollOpenTextAnswer.prototype.parseCheckAnswer = function(obj)
{
	if(!obj) return(false);
	return true;
};

componentPollOpenTextAnswer.prototype.onLock = function() 
{
	var form = page.doc.forms[this.id + "Form"];
	if(form) valStr = form[0].disabled = true;
	return(true);
};

componentPollOpenTextAnswer.prototype.onUnLock = function() 
{
	var form = page.doc.forms[this.id + "Form"];
	if(form) valStr = form[0].disabled = false;
};

componentPollOpenTextAnswer.prototype.fnOnKeyPress = function()
{
	this.invokeEvent("KeyPress", window.event);
	//if (window.event.keyCode == 13) window.event.returnValue = false;
};

componentPollOpenTextAnswer.prototype.fnOnKeyUp = function()
{
	this.invokeEvent("KeyUp", window.event);
};

componentPollOpenTextAnswer.prototype.fnOnMouseout = function()
{
	this.invokeEvent("MouseOut");
}

componentPollOpenTextAnswer.prototype.fnOnMouseover = function()
{
	this.invokeEvent("MouseOver");
}

componentPollOpenTextAnswer.prototype.fnOnChange = function()
{
	this.isChanged = true;
	this.invokeEvent("Change");
}

componentPollOpenTextAnswer.prototype.checkIsChanged = function()
{
	return(this.isChanged);
}

componentPollOpenTextAnswer.prototype.onClear = function() 
{
	if (!this.checkAnswer()) {
		var form = getForm(this.id + "Form");
		if (form) form[0].value = "";
	}
};

componentPollOpenTextAnswer.prototype.focus = function() 
{
	var form = getForm(this.id + "Form");
	if (form && this.visible) {
		form[0].focus();		
		form[0].value = form[0].value;		
	}
};

componentPollOpenTextAnswer.prototype.checkIsFocused = function() 
{
	var form = getForm(this.id + "Form");
	return (form[0] == document.activeElement);
};

componentPollOpenTextAnswer.prototype.interactionsCorrectResponses = function()
{
	return "";
};

componentPollOpenTextAnswer.prototype.interactionsStudentResponse = function()
{
	return this.getAnswer();
};

componentPollOpenTextAnswer.prototype.interactionsCorrectResponsesAsText = function()
{
	return "";
};

componentPollOpenTextAnswer.prototype.interactionsStudentResponseAsText = function()
{
	return this.getAnswer();
};

componentPollOpenTextAnswer.prototype.interactionsCorrectResponsesCount = function()
{
	return 1;
};
