/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */
 
componentGroupBlanksInText = function(name)
{
	this.componentBlanksInText = componentBlanksInText;
	this.componentBlanksInText(name);
	
	this.correctValues = new Array(new Array(), new Array());
	this.inputsArr = new Array();
	this.complete = false;
	this.addInactive = false;
	this.incorrectEmpty = true;	
	this.iTimerID == null;
	this.compAudio = null;
		
	this.interactions = true;
	this.interactionsType = "fill-in";	
};

componentGroupBlanksInText.prototype = new componentBlanksInText;
componentGroupBlanksInText.prototype.type = "componentGroupBlanksInText";

componentGroupBlanksInText.prototype.componentGroupBlanksInTextBaseOnCreate = componentGroupBlanksInText.prototype.onCreate;
componentGroupBlanksInText.prototype.onCreate = function(obj)
{
	if ( PageInterface.GetValue("state") != psDesigning && lms && this.disableOnlineOfflineMix != false ) 
	{
		this.resourcesStatus 	= lms.resourcesStatus();
		this.resourcesURL 		= lms.resourcesURL();
	} else this.resourcesURL = resourcesURL;
	
	
	if (PageInterface.GetValue("state") != psDesigning) {
		if (this.enableFlashPlayer) {
			var e = new componentFlashAudioPlayer;
			e.myParent = this;
			e.id = this.id + 'Audio';
			e.step = -1;
			e.src = this.flashAudioPlayerSrc;
			e.x = 0;
			e.y = 0;
			e.w = 1;
			e.h = 1;
			e.loop = false;
			e.flashVars = "";
			this.compAudio = page.addChild(e);
		} else {		
			var audioElement = new componentAudio;
			audioElement.myParent = this;
			audioElement.id = this.id+'Audio'; 
			audioElement.src = this.src;
			audioElement.pvWMP64 = false;
			audioElement.pvWMP7up = false;
			audioElement.step = -1;
			audioElement.editorPaint = this.editorPaint;
			this.compAudio = page.addChild(audioElement);		
		}
		this.getInputsGroup(this.inputsCorrectValues);
	};
	
	this.componentGroupBlanksInTextBaseOnCreate();
};

componentGroupBlanksInText.prototype.editorPaint = function()
{
	return '';
};

componentGroupBlanksInText.prototype.getCorrectValues = function(text)
{
	var value = text;
	var str ='';
	var inputIdAndValues = value.split('||');
	for (var i=0; i < inputIdAndValues.length; i++){
		var values = inputIdAndValues[i].split('#!#');
		var idArr = values[0].split('_');
		this.correctValues[idArr[1]] = values[1];
		str += idArr[1] + ' | ';
	};
	return ;
};


componentGroupBlanksInText.prototype.getInputsGroup = function(text)
{
	var value = text;
	var inputIdAndValues = value.split('||');
	
	for (var i=0; i < inputIdAndValues.length; i++){
		var values = inputIdAndValues[i].split('#!#');
		var name = values[0].replace('specialID', this.id);
		if (!this.inputsArr[values[1]]){
			this.inputsArr[values[1]] = new Array();
		};
		this.inputsArr[values[1]].push(name);
	};
};

componentGroupBlanksInText.prototype.keyUp = function(keyUpObj)
{
	if (this.complete) 
	var name = '';
	
	name = keyUpObj.id.toString();
	var id = name.split('_');
	
	if (keyUpObj){
		var correctValue = this.checkIsCorrectValue(keyUpObj.value, id[1]);	
		if (correctValue != ''){
			if (keyUpObj.value == correctValue){
				this.fillOtherInputs(correctValue, correctValue);
			};
			this.setColor(keyUpObj, this.correctColor);
		}else{
			if (this.incorrectEmpty){
				var corr = this.findCorrectValue(this.inputsCorrectValues, keyUpObj.id);
				var rem = keyUpObj.value;
				this.fillOtherInputs(corr, '');
				keyUpObj.value = rem;
			};
			this.setColor(keyUpObj, this.incorrectColor);
		};
	};
};

componentGroupBlanksInText.prototype.fillOtherInputs = function(corrValue, fillValue)
{
	for (var i=0; i < this.inputsArr[corrValue].length; i++){
		var objName = this.inputsArr[corrValue][i];
		var inputObj = document.getElementById(objName);
		if (inputObj){
			inputObj.value = fillValue;
			if (fillValue != ''){
				this.setColor(inputObj, this.correctColor);
			}else{
				this.setColor(inputObj, this.backgroundInputColor);
			};
		};
	};
};

componentGroupBlanksInText.prototype.checkAnswer = function(index)
{
	if (this.showAnswerIndicatorAnyTime) this.colorMyInputs();
	if (this.parseCheckAnswer(this)){
		this.audioPlay();
		return true;
	}else{
		return false;
	};
};

componentGroupBlanksInText.prototype.findCorrectValue = function(text, objID)
{
	var value = text;
	var inputIdAndValues = value.split('||');
	
	for (var i=0; i < inputIdAndValues.length; i++){
		var values = inputIdAndValues[i].split('#!#');
		var name = values[0].replace('specialID', this.id);
		if (name == objID) {
			return values[1];
		};
	};
	return ;
};

componentGroupBlanksInText.prototype.checkIsCorrectValue = function(value, index)
{
	var inputValue = value;
	var splitedValue = inputValue.split('');
	var result = false;
	
	var correct = this.correctValues[index].split('');
	if (value == '') return this.correctValues[index];
	for (var j=0; j < splitedValue.length; j++){
		if (splitedValue[j] == correct[j]){
			result = true;
		}else {
			result = false;
			break;
		};
		if (j == eval(splitedValue.length-1) && result){
			return this.correctValues[index];
		};
	};
	return '';
};

componentGroupBlanksInText.prototype.audioPlay = function()
{
	if (this.audioSrc == '' || this.audioSrc == 'undefined' || typeof(this.audioSrc) == 'undefined')
	return;
	
	if (this.enableFlashPlayer) {
		this.compAudio.flashVars = "playIdx=0&autoStart=true&playOnce=true";
		this.compAudio.show();
	} else {
		this.compAudio.setUrl(this.resourcesURL + this.audioSrc);
		this.compAudio.actionMediaPlay();
	};			
};













