/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */
 
componentChangeText = function(name)
{
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.correctAnswersList = new Array();
	this.interactions = true;
	this.interactionsType = "numeric";
	this.rememberItemsArr = new Array();
	this.complete = false;
	this.basicText = '';
	this.counter = 0;
	this.iTimerID = null;
	this.allowGF = true;
	
	this.interactions = true;
	this.interactionsType = "choice";	
};

componentChangeText.prototype = new __componentText();
componentChangeText.prototype.type = "componentChangeText";

componentChangeText.prototype.componentChangeTextBaseOnCreate = componentChangeText.prototype.onCreate;
componentChangeText.prototype.onCreate = function(obj)
{	
	this.captureMouseEvents();
	this.correctAnswersList = this.correctAnswer;
	
	var font = {};
	font.family = this.fontFamily;
	font.size = this.fontSize;
	font.color = this.fontColor;
	
	if (this.allowGF && project.globalFormating && this.isDefaultFont(font) && !this.isDefaultFont(project.font))
	{
		this.fontFamily = project.font.family;
		this.fontSize = project.font.size;
		this.fontColor = project.font.color;
	};
	//this.componentChangeTextBaseOnCreate();
};

componentChangeText.prototype.isDefaultFont = function(font)
{
	var result = true;
	if (font.family != 'Arial') {
		result = false;
	} else if (font.size != 2) {
		result = false;
	} else if (font.color.replace(/#/g, "") != '000000') {
		result = false;
	}
	return result;
};

componentChangeText.prototype.getFontSize = function(value, format)
{
	var fontSizesPX = new Array(0,10,13,16,18,24,32,48);
	var fontSizesPT = new Array(0, 8,10,12,14,18,24,36);
	var pxSize = fontSizesPT[parseInt(value)];
	if (format == "px") {
		pxSize = fontSizesPX[parseInt(value)];
	}
	return pxSize;
};

componentChangeText.prototype.onPaint = function(obj)
{
	if (this.locked) return;

	var s = "";
	var dispAndCorrArray = this.text.toString().split("||");
	//selectedColor = this.selectedColor;
	selectedColor = this.markColor;
	var onlyText = '';
	
	bgColor = this.bgColor;
	if (PageInterface.GetValue("state") == psDesigning) {
		var ansArray = this.correctAnswer.split(",");
		var wasHit = false;
		
		s += '<table border="0" cellSpacing="0" cellPadding="0"><tr><td valign="top">&nbsp;&nbsp;<b>';
		if (this.anotherText == '-' && this.showCountNumber == 1) {
				s += '<span id='+this.id+'_span_counter style="background-color:'+this.selectedColor+';';
				s += ' font-size:'+this.getFontSize(this.fontSize, 'px')+'px; color:'+this.fontColor+'; ';
			
			if (this.answerCount == '') var answerCount = this.getAnswerCount(dispAndCorrArray);
			else answerCount = this.answerCount;
			
			s += ' font-family:'+this.fontFamily+';">'+answerCount+'</span>&nbsp;&nbsp;</b>';
			s += '</td>';
		};
		s += '<td>';
		
		for (var i=0; i < dispAndCorrArray.length; i++) {
			var textArray = dispAndCorrArray[i].split('@#@');
			for (var j=0; j < ansArray.length; j++) {
				if (textArray[1] != '') {
					s += "<span id='" + this.id + "_span_" + i + "' "+
						"style='background-color:" + selectedColor + "; font-size:" + this.getFontSize(this.fontSize, "px") + "px; color:" + this.fontColor + "; font-family:" + this.fontFamily + ";'>" ;
					onlyText = this.repalceStrbyStr(textArray[0], "$", "'");
					s += onlyText;						
					s += "</span> ";
					wasHit = true;
					break;
				}
			}
			if (!wasHit){
				s += "<span id='" + this.id + "_span_" + i + "' "+
					"style='background-color:" + this.bgColor + "; font-size:" + this.getFontSize(this.fontSize, "px") + "px; color:" + this.fontColor + "; font-family:" + this.fontFamily + ";'>";
				onlyText = this.repalceStrbyStr(textArray[0], "$", "'");
				s += onlyText;
				s += "</span> ";
			}
			wasHit = false;
		};
		s += '</td></tr></table>'
	} else {
		if (this.answerCount == '') var answerCount = this.getAnswerCount(dispAndCorrArray);
			else answerCount = this.answerCount;
		
		this.counter = answerCount;
		s += '<table border="0" cellSpacing="0" cellPadding="0"><tr><td valign="top">&nbsp;&nbsp;<b>';
		
		if (this.showCountNumber == 1){
			if (this.anotherText == '-') {
				s += '<span id='+this.id+'_span_counter style="background-color:'+this.selectedColor+';';
				s += ' font-size:'+this.getFontSize(this.fontSize, 'px')+'px; color:'+this.fontColor+'; ';
				
				
				s += ' font-family:'+this.fontFamily+';">'+answerCount+'</span>&nbsp;&nbsp;</b>';
				s += '</td>';
			}else if (page.all[this.anotherText]){
				this.basicText = page.all[this.anotherText].text;
				text = this.basicText.replace('{=counter}', this.counter);
				var contentObj = page.all[this.anotherText].findContentObj();
				if (contentObj)
					contentObj.innerHTML = text;				
			};
		}
		s += '<td>';
		
		for (var i=0; i < dispAndCorrArray.length; i++) {
			var textArray = dispAndCorrArray[i].split('@#@');			
			if (textArray[1] == '') textArray[1] = textArray[0];
		 	s += "<span id='" + this.id + "_span_" + i + "' "+
				"style='background-color:" + this.bgColor + "; font-size:" + this.getFontSize(this.fontSize, "px") + "px; color:" + this.fontColor + "; font-family:" + this.fontFamily + ";' "+
				"onclick = \'pc."+this.id+".clickSPAN(this, \""+bgColor+"\", \""+selectedColor+"\", \""+textArray[1]+"\", this.innerHTML );\'>";
			onlyText = this.repalceStrbyStr(textArray[0], "$", "'");
			s += onlyText;
			s += "</span> ";		
		};
		s += '</td></tr></table>'
	};

	s += '<div id='+this.id+'divInput style="visibility: hidden; left:0; top:0; position: absolute">';
	s += '<input id='+this.id+'InputInsideText style="background: '+this.backgroundInputColor+';';
	s += ' border-color: '+this.borderInputColor+'; border-width : '+this.inputBorder+'px; width: 100px" onKeyup="pc.'+this.id+'.keyUp(this);" onKeydown="pc.'+this.id+'.confirmChange(event);">';	
	s += '<img id='+this.id+'ButtonHideDiv src="' + resourcesURL + this.buttonHideDiv + '"';
	s += ' onMousedown="pc.'+this.id+'.mouseDownButtonHideDiv(this, true);" style="cursor: pointer;">';
	s += '</div>';
	
	this.setHTML(s);	
};

componentChangeText.prototype.componentChangeTextBaseOnPaint = componentChangeText.prototype.onPaint;
componentChangeText.prototype.onPaint = function()
{
	if (this.locked) return;
	this.componentChangeTextBaseOnPaint();
}

componentChangeText.prototype.confirmChange = function(event)
{
	if (event.keyCode == 13){
		this.mouseDownButtonHideDiv(true, true);
		if (is.moz) window.blur();
	}	
}

componentChangeText.prototype.getAnswerCount = function(arr)
{
	var result = 0;
	for(var i=0; i < arr.length; i++){
		var oneWord = arr[i].split("@#@");
		if (oneWord[1] != '') result += 1;
	}
	return result;
};

componentChangeText.prototype.mouseDownButtonHideDiv = function(sender, allowReplace)
{
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var div = document.getElementById(this.id+'divInput');
	var input = document.getElementById(this.id+'InputInsideText');
	var value = input.value;
	div.style.visibility = 'hidden';
	
	var sender = this.rememberItemsArr[0];
	var bgCol = this.rememberItemsArr[1];
	var selCol = this.rememberItemsArr[2];
	var text = this.rememberItemsArr[3];
	var correct = this.rememberItemsArr[4];
	var replacedWord = this.rememberItemsArr[5];
	
	if (sender){
		if (ns) var spanColor = this.RGBtoHex(sender.style.backgroundColor);
			else var spanColor = sender.style.backgroundColor;
		(spanColor.toLowerCase() == selCol)?sender.style.backgroundColor = bgCol:sender.style.backgroundColor = selCol;
		if (allowReplace) {
			if (value == '') value = '_';
			sender.innerHTML = value;
		};
	};
	this.rememberItemsArr[0] = null;
	var isCorrect = this.checkCounterDown(value, correct);
	if (sender && this.showCountNumber == 1){
		if (replacedWord != value){
			if (isCorrect){
				if (this.anotherText == '-'){
					var counterSpan = document.getElementById(this.id+'_span_counter');
					if (counterSpan && this.counter != 0) counterSpan.innerHTML = eval(--this.counter);
				}else if (page.all[this.anotherText] && this.counter != 0){
					text = this.basicText.replace('{=counter}', --this.counter);
					var contentObj = page.all[this.anotherText].findContentObj();
					if (contentObj)
						contentObj.innerHTML = text;
				};
			}else{
				if (replacedWord == correct || (correct == '' && value != replacedWord)){
					if (this.anotherText == '-'){
						var counterSpan = document.getElementById(this.id+'_span_counter');
						if (counterSpan) counterSpan.innerHTML = eval(++this.counter);
					}else if (page.all[this.anotherText]){
						text = this.basicText.replace('{=counter}', ++this.counter);
						var contentObj = page.all[this.anotherText].findContentObj();
						if (contentObj)
							contentObj.innerHTML = text;
					};			
				};
			};
		};
	};
};

componentChangeText.prototype.checkCounterDown = function(value, correctValues)
{
	if (typeof(correctValues) == 'undefined') return;
	var corrWordArr = correctValues.split('#');
	
	for (var i=0; i < corrWordArr.length; i++){
		if (corrWordArr[i] == value) return true;
	};
	return false;
};

componentChangeText.prototype.showCorrectWords = function(sender, bgCol, selCol, corrWord )
{
	var ns = (navigator.appName.indexOf("Netscape") != -1);

	var div = document.getElementById(this.id+'divInput');
	if (corrWord != ''){
		div.style.visibility = 'inherit';
		div.style.left = (this.w/2)-75;
		div.style.top = this.h-30;
		
		var button = document.getElementById(this.id+'ButtonWHYInput');
		
		button = document.getElementById(this.id+'ButtonHideDiv');
		if (button) {
			button.style.visibility = 'hidden';
		};
		
		var input = document.getElementById(this.id+'InputInsideText');
		input.value = corrWord;
		
		this.rememberItemsArr[0] = sender;
		this.rememberItemsArr[1] = bgCol;
		this.rememberItemsArr[2] = selCol;
		this.rememberItemsArr[3] = sender.innerHTML;
		this.rememberItemsArr[4] = corrWord;
	}else{
		div.style.visibility = 'hidden';
	}
	return;
};

componentChangeText.prototype.checkIsCorrectValue = function(value, corrValues)
{
	if (corrValues == '') return true;
	
	var splitedValue = value.split('');
	var correctWords = corrValues.split('#');
	
	var result = false;
	for (var i=0; i < correctWords.length; i++){
		var oneWord = correctWords[i].split('');
		
		if (eval(oneWord.length+1) > splitedValue.length){			
			for (var j=0; j < splitedValue.length; j++){
				
				if (typeof(oneWord[j]) != 'undefined' && oneWord[j] != ''){
					if (splitedValue[j] == oneWord[j]){
						result = true;
					}else {
						result = false;
						break;
					};
				};
			};
		};
		if (result) return result;
	}
	return result;
};

componentChangeText.prototype.keyUp = function(keyUpObj)
{
	if (this.complete) return;
	
	if (keyUpObj){
		var correctValue = this.checkIsCorrectValue(keyUpObj.value, this.rememberItemsArr[4]);	
		var button = document.getElementById(this.id+'ButtonAddInput');
		if (correctValue != '' || keyUpObj.value == '') {
			this.setColor(keyUpObj, this.correctColor);
		}else {
			this.setColor(keyUpObj, this.incorrectColor);
		};
	};
	return;
};

componentChangeText.prototype.setColor = function(obj, color)
{
	if (this.automaticCheck){
		obj.style.background = color;
	};
};

componentChangeText.prototype.clickSPAN = function(sender, bgCol, selCol, corrWord, sendWord)
{
	if(this.locked) {
		return;
		var oneCorr = corrWord.split('#');
		this.showCorrectWords(sender, bgCol, selCol, oneCorr[0] );
	};
	
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var div = document.getElementById(this.id+'divInput');

	div.style.visibility = 'inherit';
	div.style.left = (this.w/2)-75;
	div.style.top = this.h-30;
	
	var input = document.getElementById(this.id+'InputInsideText');
	input.value = sender.innerHTML;
	
	button = document.getElementById(this.id+'ButtonAddInput');
	var correctValue = this.checkIsCorrectValue(sender.innerHTML, corrWord);	
	if (correctValue){
		this.setColor(input, this.correctColor);
	}else{
		this.setColor(input, this.incorrectColor);
	};	

	if (this.rememberItemsArr[0]){
		if (ns) var spanColor = this.RGBtoHex(this.rememberItemsArr[0].style.backgroundColor);
			else var spanColor = this.rememberItemsArr[0].style.backgroundColor;
			
		(spanColor.toLowerCase() == selCol)?this.rememberItemsArr[0].style.backgroundColor = bgCol:this.rememberItemsArr[0].style.backgroundColor = selCol;
	};
	
	if (ns) var spanColor = this.RGBtoHex(sender.style.backgroundColor);
		else var spanColor = sender.style.backgroundColor;
	(spanColor.toLowerCase() == selCol)?sender.style.backgroundColor = bgCol:sender.style.backgroundColor = selCol;
		
	this.rememberItemsArr[0] = sender;
	this.rememberItemsArr[1] = bgCol;
	this.rememberItemsArr[2] = selCol;
	this.rememberItemsArr[3] = sender.innerHTML;
	this.rememberItemsArr[4] = corrWord;
	this.rememberItemsArr[5] = sendWord;
};

componentChangeText.prototype.inTable = function(table, value)
{
	for (var i=0; i < table.length; i++ ) {
		if (value == table[i]) return true;
	}
	return false;
};

componentChangeText.prototype.onRepaint = function(obj)
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator) 
	{
		this.complete = true;
		
		var dispAndCorrArray = this.text.toString().split("||");
		var colored = false;
		
		for (var i=0; i < dispAndCorrArray.length; i++ ) {
			var span = document.getElementById(this.id+"_span_"+i);
			var textArray = dispAndCorrArray[i].split('@#@');
			var value = span.innerHTML;
			value = this.repalceStrbyStr(value, "'", "$");
			if (textArray[1] == ''){
				if (value == textArray[0]) span.style.backgroundColor = this.correctColor;
					else span.style.backgroundColor = this.incorrectColor;
			}else{
				var corrWordsArr = textArray[1].split('#');
				for (var j=0; j < corrWordsArr.length; j++){
					if (this.caseSensitive == 1){
						if (value != corrWordsArr[j]) {
							span.style.backgroundColor = this.incorrectColor;
							colored = false;
						}else {
							span.style.backgroundColor = this.correctColor;
							colored = true;
						};
					}else{
						if (value.toLowerCase() != corrWordsArr[j].toLowerCase()){
							span.style.backgroundColor = this.incorrectColor;
							colored = false;
						}else{
							span.style.backgroundColor = this.correctColor;
							colored = true;
						};
					};
					if (colored) break;
				};		
			};
		};
	};
};

componentChangeText.prototype.repalceStrbyStr =  function(baseStr, str, value)
{
	while (baseStr.indexOf(str) > -1){
		baseStr = baseStr.replace(str, value);
	}
	return baseStr;
}

componentChangeText.prototype.RGBtoHex =  function(color)
{
	if (color.indexOf('rgb') == -1) return color;
	color = color.replace('rgb(', '');
	color = color.replace(')', '');
	color = color.replace(/ /g, '');
	
	var arr = color.split(',');
	return '#' + this.toHex(parseInt(arr[0])) + this.toHex(parseInt(arr[1])) + this.toHex(parseInt(arr[2]));	
};

componentChangeText.prototype.toHex =  function(N)
{
	if (N==null) return "00";
	N=parseInt(N); if (N==0 || isNaN(N)) return "00";
	N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);

	return "0123456789ABCDEF".charAt((N-N%16)/16) + "0123456789ABCDEF".charAt(N%16);
};

componentChangeText.prototype.getAnswer = function(obj)
{
	var valInt = "";
	var sep = "";
	var textArray = this.text.toString().split("||");		
  	if (this.ignoreAnswer) return "";
	
	for (var i=0; i < textArray.length; i++ ) {
		span = document.getElementById(this.id + "_span_" + i);
		if (valInt != '') sep = ",";
		valInt += sep + span.innerHTML;
	};
	return(valInt);
};

componentChangeText.prototype.setAnswer = function(valStr, showCorrect)
{
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	if (valStr != '' && !showCorrect){
		var ansArray = valStr.split(',');
		for (var i=0; i < ansArray.length; i++ ) {
			span = document.getElementById(this.id+"_span_"+i);
			span.innerHTML = ansArray[i];
		};
	}else{
		var dispAndCorrArray = this.text.toString().split("||");
		for (var i=0; i < dispAndCorrArray.length; i++ ) {
			var textArray = dispAndCorrArray[i].split('@#@');
			if (textArray[1] != ''){
				var arr = textArray[1].split('#');
				span = document.getElementById(this.id+"_span_"+i);
				span.innerHTML = arr[0];
			};
		};
	};
	this.onRepaint();
};

componentChangeText.prototype.checkAnswer = function() 
{
	this.mouseDownButtonHideDiv();
	if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator) this.colorMySpans();
	return(this.parseCheckAnswer(this)); 
};

componentChangeText.prototype.colorMySpans = function(obj) 
{
	var dispAndCorrArray = this.text.toString().split("||");
	var result = true;
	
	for (var i=0; i < dispAndCorrArray.length; i++ ) {
		var textArray = dispAndCorrArray[i].split('@#@');
		var span = document.getElementById(this.id+"_span_"+i);
		var value = span.innerHTML;
		value = this.repalceStrbyStr(value, "'", "$")
		if (textArray[1] != ''){
			var corrWordsArr = textArray[1].split('#');
			for (var j=0; j < corrWordsArr.length; j++){
				if (this.caseSensitive == 1){
					if (value == corrWordsArr[j]) {
						span.style.background = this.correctColor;
						break;
					}else{
						span.style.background = this.incorrectColor;
					}
				}else{
					if (value.toLowerCase() == corrWordsArr[j].toLowerCase()) {
						span.style.background = this.correctColor;
						break;
					}else{
						span.style.background = this.incorrectColor;	
					}
				};
			};
		};
	};
};

componentChangeText.prototype.parseCheckAnswer = function(obj) 
{
	var dispAndCorrArray = this.text.toString().split("||");
	var result = false;

	if (this.anotherText != '-' && this.showCountNumber == 1){
		text = this.basicText.replace('{=counter}', this.counter);
		var textObj = PageInterface.GetValue("getChild", this.anotherText);
		
		var contentObj = textObj.findContentObj();
		if (contentObj){
			contentObj.innerHTML = text;	
		}
	}
	
	for (var i=0; i < dispAndCorrArray.length; i++ ) {
		var textArray = dispAndCorrArray[i].split('@#@');
		var span = document.getElementById(this.id+"_span_"+i);
		var value = span.innerHTML;
		value = this.repalceStrbyStr(value, "'", "$");
		if (textArray[1] != ''){
			var corrWordsArr = textArray[1].split('#');
			result = false;
			for (var j=0; j < corrWordsArr.length; j++){
				if (this.caseSensitive == 1){
					if (value == corrWordsArr[j]){
						result = true;
						break;
					}
				}else{
					if (value.toLowerCase() == corrWordsArr[j].toLowerCase()){
						result = true;
						break;
					}
				}
			}
			if (!result) return false;
		}else if (value != textArray[0]) {
			return false;			
		}
	};
	return result;
};

componentChangeText.prototype.onLock = function() 
{
	text = this.basicText.replace('{=counter}', this.counter);
	var textObj = PageInterface.GetValue("getChild", this.anotherText);
	if (typeof(textObj) != 'undefined'){
		var contentObj = textObj.findContentObj();
		if (contentObj)
			contentObj.innerHTML = text;	
	};
	
	this.locked = true;
	this.mouseDownButtonHideDiv(this.rememberItemsArr[0], false);
  	return true;
};

componentChangeText.prototype.onUnLock = function() 
{
	this.locked = false;
  	return true; 
};

componentChangeText.prototype.componentChangeTexttBaseUnselectable = componentChangeText.prototype.unselectable;
componentChangeText.prototype.unselectable = function()
{
	this.componentChangeTexttBaseUnselectable();
	
	var obj = document.getElementById(this.id+'InputInsideText');
	if (obj){
		obj.unselectable = "";
	};
};

componentChangeText.prototype.interactionsCorrectResponsesCount = function() 
{
	var length = 1;
	return length;
}

componentChangeText.prototype.interactionsCorrectResponses = function() {
	return this.correctAnswersList;
}

componentChangeText.prototype.interactionsCorrectResponsesAsText = function() {
	return this.correctAnswersList;
}

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

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

componentChangeText.prototype.onFree = function() {
	if (this.iTimerID != null) 
		window.clearTimeout(this.iTimerID);
}
/********************************************************************/
componentChangeText.prototype.interactionsCorrectResponsesCount = function()
{
	return 1;
};

componentChangeText.prototype.interactionsCorrectResponses = function()
{
	var result = "";
	var sep = "";
	var dispAndCorrArray = this.text.toString().split("||");
	for (var i=0; i < dispAndCorrArray.length; i++ ) {
		var textArray = dispAndCorrArray[i].split('@#@');
		if (result != '') sep = ",";
		if (textArray[1] != ''){
			var arr = textArray[1].split('#');
			result += sep + arr[0];
		}else{
			result += sep + textArray[0];
		}
	};
	return result;
};

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

componentChangeText.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var result = "";
	var sep = "";
	var dispAndCorrArray = this.text.toString().split("||");
	for (var i=0; i < dispAndCorrArray.length; i++ ) {
		var textArray = dispAndCorrArray[i].split('@#@');
		if (result != '') sep = ",";
		if (textArray[1] != ''){
			var arr = textArray[1].split('#');
			result += sep + arr[0];
		}else{
			result += sep + textArray[0];
		}
	};
	return result;
};

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