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

componentCorrectSequence = function(name) {
	this.__componentText = __componentText;
	this.__componentText(name);
	
	this.passed = false;
	this.basketsCount = 0;
	this.texts = new Array();
	this.componentX = 0;
	this.componentY = 0;	
	
	this.corrSeqArr = new Array();
	this.userSeqArr = new Array();
	this.elementsByText = new Array();
	this.elementsCorrect = new Array();
	this.elementsUsed = new Array();
	
	this.dropAudioComp = null;
	
	this.interactions = true;
	this.interactionsType = "matching";		
};

componentCorrectSequence.prototype = new __componentText;
componentCorrectSequence.prototype.type = "componentCorrectSequence";
componentCorrectSequence.prototype.onBaseCreate = function(obj) {
	this.captureMouseEvents();
};

componentCorrectSequence.prototype.componentCorrectSequenceBaseOnCreate = componentCorrectSequence.prototype.onCreate;
componentCorrectSequence.prototype.onCreate = function(obj) {
	this.componentCorrectSequenceBaseOnCreate();
	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.dropAudioComp = page.addChild(e);
		} else {
			var audioElement = new componentAudio;
			audioElement.myParent = this;
			audioElement.id = this.id+'dropAudioComp'; 
			audioElement.src = this.audioDrop;
			audioElement.pvWMP64 = false;
			audioElement.pvWMP7up = false;
			audioElement.step = -1;
			audioElement.editorPaint = this.editorPaint;
			this.dropAudioComp = page.addChild(audioElement);
			audioElement.allowNextStep = true;
		}
		
		var sizeDiv = '<span id="'+this.id+'sizeDiv" style="visibility: hidden; position: absolute; font-family:' + this.font.family
		+ '; font-size:' + getFontSize(this.font.size, "px") + 'px; color:' + this.font.color + ';"></span>';
		
		var ob = document.getElementById(this.id);
		ob.innerHTML += sizeDiv;

		var width = 0;
		var height = 0;
		var tempX = this.x+this.borderWidth+1;
		var tempY = this.y + this.h - 1;
		var tempWidth = 0;
		var onlyText;
		//var match = this.text.split(" ");
		var badElem = this.text.match(/\[[^\[]+\]/gi);		
		var clearText = this.text;
		if (badElem){
			clearText = this.clearTextFromWrongElements(badElem, this.text);
		}
		var match = this.getSeparateBlocks(clearText);
		var allBaskets = match;
		if (badElem){
			allBaskets = match.concat(badElem);
		};		
		this.basketsCount = match.length;
		this.dynamicWidth = false;
		this.maxWidth = 0;
		
		if (!this.dynamicWidth) 
		{
			for (var i=0; i<allBaskets.length; i++) {
				// Czysty tekst bez nawiasow
				onlyText = allBaskets[i];
				// tymczasowy element do ustalania szerokosci i wysokosci ddelementow
				var sizeDiv = document.getElementById(this.id+"sizeDiv");
				if (sizeDiv) {
					sizeDiv.innerHTML = onlyText;
					width = sizeDiv.offsetWidth;
				}
				if ( width > this.maxWidth ) this.maxWidth = width;
			}
			width = this.maxWidth + this.elementPadding;
		}	
		var shiftW = 0;
		var shiftH = 0;		
	
		var baskets = "";
		if (this.showBaskets){
			var divBorder = 1;
			//var additonalHeight = -1;//this.elementPadding - 1;
			var additonalHeight = -3;
			shiftH = -4;
			if (is.moz){
				shiftW = 2;
				shiftH = -2;
			};							
		}else{
			baskets += "<div style='position: relative; display:inline; float:left; border: 1px solid black'>";
			var divBorder = 0;
			//var additonalHeight = -3;//this.elementPadding - 3;
			var additonalHeight = -1;
			if (is.moz){
				shiftW = -2;
				shiftH = -2;
			};				
		};
		var basketInLine = parseInt(this.w / (width+2));
		var downLineCount = eval(parseInt(this.basketsCount / basketInLine)*basketInLine);
		var shift = 0;
		if (is.moz){
			shift = -2;
		};		
		var elemIndex = 0;		
		for (var i=0; i<this.basketsCount; i++) {
			onlyText = match[i];
			
			if (document.getElementById(this.id+"sizeDiv")) {
				document.getElementById(this.id+"sizeDiv").innerHTML = onlyText;
				height = document.getElementById(this.id+"sizeDiv").offsetHeight+2;// + this.elementPadding;;
				if (this.dynamicWidth) width = document.getElementById(this.id+"sizeDiv").offsetWidth;
			};
				
			if (this.showPunctuationMark){
				baskets += this.getPunctuationMark(match[i-1], height, additonalHeight, divBorder, onlyText);
			};
			
			var objName = onlyText.replace("'", "#");
			objName = objName.replace('"', "@");	
			
			//pg2
			baskets += "<textarea name='"+objName+"' id='_"+this.id+"_"+i+"_basket' style='background: "+this.basketBckColor+"; border: "+divBorder+"px solid "+ this.basketBorderColor + "; width: " + eval(width+this.elementPadding-shiftW) + "px; height: " + (height+additonalHeight+1-shiftH)+ "px; margin: 1px; overflow: hidden;' disabled></textarea>";
			
			//pg2-works, but margin too small
			//baskets += "<textarea name='"+objName+"' id='_"+this.id+"_"+i+"_basket' style='background: "+this.basketBckColor+"; border: "+divBorder+"px solid "+ this.basketBorderColor + "; width: " + eval(width+this.elementPadding-shiftW) + "px; height: " + (height+additonalHeight+1-shiftH)+ "px; margin: 0px; padding-right: "+eval(width)+"px; overflow: hidden;' disabled></textarea>";
			//pg2- removed, 'cause under IE fast moving with d'n'd element caused its disappearing and showing in left-top corner of window
			//baskets += "<div name='"+objName+"' id=_"+this.id+"_"+i+"_basket style='position: relative; background: "+this.basketBckColor+";display:hidden; float:left; border: "+divBorder+"px solid "+this.basketBorderColor+"; width: " + eval(width+this.elementPadding-shiftW) + "px; height: " + (height+additonalHeight+1-shiftH) + "px; margin: 0px;' ></div>";
			
			
			if (i < downLineCount){
				baskets += "<div style='position: relative; display:inline; float:left; width: 1px; height: " + (height+additonalHeight+1) + "px;'><div style='position: relative; display:inline; float:left; width: 1px; height: " + (height+additonalHeight+2) + "px;'></div></div>";
			}else {
				/* //pg2-removed, 'cause white rectangles were appeared
				if (i < this.basketsCount-1)
				{
					baskets += "<div style='position: relative; background: "+this.basketBckColor+"; display:inline; float:left; width: 1px; height: " + (height+additonalHeight+1) + "px;'></div>";
				};
				*/
			};
			
			pos = this.getElemXY(i != 0, tempX, tempY, tempWidth, width, height);		
			tempX = pos[0];
			tempY = pos[1];			
			
			this.createDDEElement(elemIndex, tempX, tempY, width, height, true, objName, objName, onlyText);	
			tempWidth = width;
			elemIndex++;			
			this.corrSeqArr.push(onlyText);
			this.setElementIntoArray(onlyText, elemIndex);
		};
		
		if (badElem){
		for (var i=0; i<badElem.length; i++) {
			var onlyText = badElem[i].substr(1,badElem[i].length-2);			
				var objName = onlyText.replace("'", "#");
				objName = objName.replace('"', "@");				
			baskets = baskets.replace(badElem[i], "");		
			pos = this.getElemXY(true, tempX, tempY, tempWidth, width, height);		
			tempX = pos[0];
			tempY = pos[1];
			
			this.createDDEElement(elemIndex, tempX, tempY, width, height, true, objName, objName, onlyText);
			tempWidth = width;
			elemIndex++;
		};
		};
		
		if (this.showPunctuationMark){
			this.getLastDot(match[i-1], height, additonalHeight, divBorder, onlyText);
		};		
		if (!this.showBaskets){
			baskets += '</div>';
		};		
		if (this.elementsUp){
			this.valign = 'bottom';
		}else{
			this.valign = 'top';
		};
		
		this.text = baskets;
		
		if (PageInterface.GetValue("state") != psDesigning) this.randomDDElements();
	};
	
	this.componentX = this.x;
	this.componentY = this.y;
};

componentCorrectSequence.prototype.clearTextFromWrongElements = function(elementsArr, text)
{
	var newText = "";
	var sep = "";
	for (var i=0; i < elementsArr.length; i++)
	{
		text = text.replace(elementsArr[i], "");
	}
	
	var newTextArr = text.split(' ');	
	for (var i=0; i < newTextArr.length; i++)
	{
		if (newText != "")
		{
			sep = " ";
		}
		if (newTextArr[i] != "")
		{
			newText += sep + newTextArr[i];
		};
	};
	return newText;
}

componentCorrectSequence.prototype.getLastDot = function(word, height, additonalHeight, divBorder, onlyText)
{
	var text = '';	
	if (typeof(word) != 'undefined'){
		prevWord = word;
		if (prevWord.indexOf('.') != -1){
			text += "<div id=_"+this.id+"_"+i+"_basket style='position: relative; display:inline; float:left; border: "+divBorder+"px solid black; width: 0px; height: "+(height+additonalHeight)+"px; margin: 0px; font-size: "+getFontSize(this.font.size, "px")+"; font-color: "+this.font.color+"; font-family: "+this.font.family+"' ><b>.</b></div>";
		};
	};
	return text;
}

componentCorrectSequence.prototype.getPunctuationMark = function(word, height, additonalHeight, divBorder, onlyText)
{
	var text = '';	
	if (typeof(word) != 'undefined'){
		prevWord = word;
		onlyText = onlyText.replace(',', '');
		onlyText = onlyText.replace('.', '');
		onlyText = onlyText.replace(';', '');
		
		if (prevWord.indexOf(',') != -1){
			text += "<div style='position: relative; background: "+this.basketBckColor+"; display:inline; float:left; border: "+divBorder+"px solid "+this.basketBorderColor+"; width: 0px; height: " + (height+additonalHeight+1) + "px; margin: 0px; font-size: "+getFontSize(this.font.size, "px")+"; font-color: "+this.font.color+"; font-family: "+this.font.family+"' ><b>,</b></div>";
		}else if (prevWord.indexOf('.') != -1){
			text += "<div style='position: relative; background: "+this.basketBckColor+";display:inline; float:left; border: "+divBorder+"px solid "+this.basketBorderColor+"; width: 0px; height: " + (height+additonalHeight+1) + "px; margin: 0px; font-size: "+getFontSize(this.font.size, "px")+"; font-color: "+this.font.color+"; font-family: "+this.font.family+"' ><b>.</b></div>";
		}else if (prevWord.indexOf(';') != -1) {
			text += "<div style='position: relative; background: "+this.basketBckColor+";display:inline; float:left; border: "+divBorder+"px solid "+this.basketBorderColor+"; width: 0px; height: " + (height+additonalHeight+1) + "px; margin: 0px; font-size: "+getFontSize(this.font.size, "px")+"; font-color: "+this.font.color+"; font-family: "+this.font.family+"' ><b>;</b></div>";
		};
		text += "<div style='position: relative; display:inline; float:left; width: 1px; height: " + (height+additonalHeight+1) + "px;'></div>";
	};
	
	return text;
}

componentCorrectSequence.prototype.getElemXY = function(firstElem, tempX, tempY, tempWidth, width, height) 
{
	if (firstElem){
		if ( (tempX + tempWidth + width + 2) >  (this.x + this.w) ) {
			if (this.elementsUp){
				tempX = this.x + this.borderWidth + 1;
				tempY += height + 8;//+ (this.elementPadding-3);
			}else{
				tempX = this.x + this.borderWidth + 1;
				tempY -= height + 8;//+ (this.elementPadding-3); 
			};
		}else { 
			tempX += tempWidth + 8 + (this.elementPadding-3);
		}
	}else{ 
		if (this.elementsUp){
			tempY = this.y + 2 + this.borderWidth;//+ (this.elementPadding-3);
			tempX += tempWidth + this.borderWidth;
		}else {
			tempY -= height + 2 + this.borderWidth;//+ (this.elementPadding-3);
			tempX += tempWidth + this.borderWidth;
		};
	};
			
	return [tempX, tempY];
}

componentCorrectSequence.prototype.createDDEElement = function(elemIndex, tempX, tempY, width, height, solved, basketName, objName, onlyText) 
{
	// powoladnie ddelementow
	var textTemp = new __componentText;
	textTemp.parentText = this; 
	textTemp.id = this.id+'_DDE_'+elemIndex; 
	textTemp.destinationBasketId = "_"+this.id+"_"+elemIndex+"_basket";
	textTemp.text = onlyText;
	textTemp.name = objName;
	textTemp.x = tempX;
	textTemp.y = tempY;
	textTemp.x_backup = tempX;
	textTemp.y_backup = tempY;			
	textTemp.w = width;
	textTemp.h = height;
	textTemp.startX = tempX;
	textTemp.startY = tempY;			
	
	if (!this.dynamicWidth) {
		textTemp.w = width+this.elementPadding;
		textTemp.h = height+2;//+this.elementPadding;
	};
	textTemp.canDragging = true;
	textTemp.borderWidth = 1;
	textTemp.borderColor = this.elementBorderColor;
	textTemp.padding = 0;
	textTemp.bgColor = this.elementBckColor;
	textTemp.step = 0;
	textTemp.align = "center";
	textTemp.valign = 'middle';
	textTemp.font = this.font;			
	
	textTemp.onCreate = this.onCreateText;
	textTemp.beginDrag = this.beginDragText;
	textTemp.endDrag = this.endDragText;
	textTemp.onMove = this.mouseMoveText;		
	textTemp.baseOnPaint = textTemp.onPaint;
	textTemp.onPaint = this._onPaint;	
	
	textTemp.basketsCount = this.basketsCount; 
		
	textTemp.solved = solved;				
	textTemp.orgZ = textTemp.z = this.z+elemIndex;
	textTemp.inArea = null;
	textTemp.basketName = basketName;
	textTemp.insideBasketId = '';
	textTemp.cursor = this.ddelementCursor;
	
	tempWidth = width;
	page.addChild(textTemp);
	this.texts.push( textTemp );
		
	this.setAllElementIntoArray(textTemp.text, elemIndex)
}
		
componentCorrectSequence.prototype.setAllElementIntoArray = function(elemText, objID) 
{
	if (!this.elementsByText[elemText]){
		this.elementsByText[elemText] = new Array();
	}
	this.elementsByText[elemText].push(objID);		
		};
		
componentCorrectSequence.prototype.setElementIntoArray = function(elemText, objID) 
{
	if (!this.elementsCorrect[elemText]){
		this.elementsCorrect[elemText] = new Array();
	}
	this.elementsCorrect[elemText].push(objID);		
};

componentCorrectSequence.prototype.editorPaint = function(text) 
{
	return "";
}

componentCorrectSequence.prototype.onCreateText = function(text) 
{
	this.captureMouseEvents();
};

componentCorrectSequence.prototype.getSeparateBlocks = function(text) 
{
	var wordsArr = new Array();
	var splited = text.split('');
	var closeChar = ' ';
	var newBlock = '';
	
	for (var i=0; i < splited.length; i++){
		if (i == eval(splited.length-1) || splited[i] == closeChar && newBlock != '') {
			if (i == eval(splited.length-1) && splited[i] != closeChar) newBlock += splited[i];
			wordsArr[wordsArr.length] = newBlock;
			newBlock = '';
		};
		
		if (splited[i] == '{'){
			closeChar = '}';
 		}else if (splited[i] == '}'){
			closeChar = ' ';
		};
		
		if (splited[i] != '{' && splited[i] != '}'){
			if (splited[i] == ' ' && closeChar == '}') {
				newBlock += splited[i];
			}else if (splited[i] != ' '){
				newBlock += splited[i];
			}
		};
	};
	return wordsArr;
};

componentCorrectSequence.prototype.randomSeparateBlocks = function(arr) 
{
	for (var i=0; i < arr.length; i++){
		j = parseInt(Math.random()*(arr.length));
		do{
			k = parseInt(Math.random()*(arr.length));
		}while(k == j)
		
		var before = arr[j];
		var after  = arr[k];
		var remember = {
			id : before.id, 
			destinationBasketId : before.destinationBasketId,
			text : before.text
		};
		
		before.id = after.id;
		before.destinationBasketId = after.destinationBasketId;
		before.text = after.text;
		
		after.id = remember.id;
		after.destinationBasketId = remember.destinationBasketId;
		after.text = remember.text;
	};
	return arr;
};

componentCorrectSequence.prototype.onLoad = function() {
	return;
}

componentCorrectSequence.prototype._onPaint = function() 
{
	if (this.locked) {
		if (this.solved) {
			this.setBgColor(this.parentText.elementCorrectBckColor);
		}else { 
			this.setBgColor(this.parentText.elementInCorrectBckColor);
		}
		return;
	}
	this.baseOnPaint();
}

componentCorrectSequence.prototype.componentCorrectSequenceBaseOnPaint = componentCorrectSequence.prototype.onPaint;
componentCorrectSequence.prototype.onPaint = function() 
{
	if (this.locked) return;

	this.componentCorrectSequenceBaseOnPaint();
	var newX = this.componentX - this.x;
	var newY = this.componentY - this.y; 
	
	this.componentX = this.x;
	this.componentY = this.y;
	
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].x = this.texts[i].x_backup;
		this.texts[i].y = this.texts[i].y_backup;
		this.texts[i].startX = this.texts[i].x_backup;
		this.texts[i].startY = this.texts[i].y_backup;
		
		this.texts[i].insideBasketId = '';
		this.texts[i].solved = false;
		
		this.texts[i].startX -= newX;
		this.texts[i].startY -= newY;
		
		this.texts[i].x -= newX;
		this.texts[i].y -= newY;
		
		this.texts[i].setX(this.texts[i].x);
		this.texts[i].setY(this.texts[i].y);
		
		this.texts[i].setBorder(this.elementBorderColor, 1);
		this.texts[i].setBgColor(this.elementBckColor);		
	};
	this.setButtonActivity(this.texts[this.texts.length-1], true) ;
};

componentCorrectSequence.prototype.onShow = function() 
{
	if (PageInterface.GetValue("state") != psDesigning) {
		for (var i=0; i < this.texts.length; i++) {
			this.texts[i].show();
		};
	};
};

componentCorrectSequence.prototype.findPosX = function (obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}else if (obj.x){
		curleft += obj.x;
	};
	return curleft;
};

componentCorrectSequence.prototype.findPosY = function(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		};
	} else if (obj.y) {
		curtop += obj.y;
	};
	return curtop;
}

componentCorrectSequence.prototype.mouseMoveText = function(e) {
};

componentCorrectSequence.prototype.get_random = function(from, to) {
    var ranNum = Math.floor( Math.random() * (to-from+1) ) + from;
    return ranNum;
};

componentCorrectSequence.prototype.randomDDElements = function() 
{
	var indexes = new Array();
	var sizes = new Array();
	for (var i=0; i<this.texts.length; i++) {
		indexes.push(i);
		sizes.push( 
			{
				x: this.texts[i].x, 
				y: this.texts[i].y,
				x_backup: this.texts[i].x_backup,
				y_backup: this.texts[i].y_backup
			}
		);
	};
	var randomIndexes = new Array();
	
	while( indexes.length != 0 ) {
		randomIndexes.push( indexes.splice( this.get_random(0, indexes.length-1),1 ) );
	};
	
	for (var i=0; i<this.texts.length; i++) {
		this.texts[i].x = sizes[ randomIndexes[i] ].x;
		this.texts[i].y = sizes[ randomIndexes[i] ].y;
		this.texts[i].x_backup = sizes[ randomIndexes[i] ].x_backup;
		this.texts[i].y_backup = sizes[ randomIndexes[i] ].y_backup;

		this.texts[i].startX = this.texts[i].x;
		this.texts[i].startY = this.texts[i].y;
		this.texts[i].startY = this.texts[i].x_backup;
		this.texts[i].startY = this.texts[i].y_backup;
	}
};

componentCorrectSequence.prototype.playDropAudio = function(audioFile) 
{	
	if (this.enableFlashPlayer) {
		this.dropAudioComp.flashVars = "playIdx=0&autoStart=true&playOnce=true";
		this.dropAudioComp.show();
	} else {
		this.dropAudioComp.actionMediaPlay();
	}
};	

componentCorrectSequence.prototype.beginDragText = function() {
	/**
		var obj = document.getElementById(this.destinationBasketId);
		obj.style.borderColor = "red";
	*/
	this.setZIndex(1000);
};

componentCorrectSequence.prototype.endDragText = function(e) 
{
	this.setZIndex(this.orgZ);
	
	var ddbasket = document.getElementById(this.destinationBasketId);
	var ddelement = document.getElementById(this.id);
	/** ddbasket.style.borderColor = "black"; */
	
	this.parentText.playDropAudio();

	var middlePoint = { 
		x: (ddelement.offsetLeft + ddelement.offsetWidth/2), 
		y: (ddelement.offsetTop + ddelement.offsetHeight/2)
	};

	for (var i=0; i < this.basketsCount; i++) {
		var ddbasket = document.getElementById("_"+this.parentText.id+"_"+i+"_basket");
		var left = this.parentText.findPosX(ddbasket);
		var right = this.parentText.findPosX(ddbasket) + ddbasket.offsetWidth;
		var top = this.parentText.findPosY(ddbasket);
		var bottom = this.parentText.findPosY(ddbasket) + ddbasket.offsetHeight;
		this.inArea = null;
		
		if ( middlePoint.x > left && middlePoint.x < right && middlePoint.y > top && middlePoint.y < bottom ){
			this.inArea = ddbasket;
			this.parentText.userSeqArr[i] = this.text;
			break;
		};
	};
	
	if (this.inArea && !this.parentText.checkIsBasketFilled(this, this.inArea.id)) {
		this.parentText.setIntoBasket(this, ddelement, this.inArea)
	}else{
		this.parentText.moveToStart(this, ddelement);
	};
	
	// if (this.parentText.texts){
		// var active = this.parentText.checkIsCorrect();
		// var lastIndex = this.parentText.texts.length;
		// var obj = this.parentText.texts[lastIndex-1];
		// this.parentText.setButtonActivity(obj, active);
	// };
};

componentCorrectSequence.prototype.setButtonActivity = function(obj, status) 
{
	if (status){
		obj.srcOut = this.addButtonSrcOut;
		obj.srcIn = this.addButtonSrcIn;		
		obj.active = true;
		obj.paint();
	}else{
		obj.srcOut = this.addButtonInactiveSrcOut;
		obj.srcIn = this.addButtonInactiveSrcIn;	
		obj.active = false;
		obj.paint();
	};
};

componentCorrectSequence.prototype.setIntoBasket = function(sender, dde, obj) 
{
	var x = sender.parentText.findPosX(obj)+sender.parentText.borderWidth;
	var y = sender.parentText.findPosY(obj)+sender.parentText.borderWidth;

	dde.style.left = x;
	dde.style.top = y;
	
	this.userSeqArr[i] = this.text;	
	
	sender.x = x;
	sender.y = y;
	sender.insideBasketId = obj.id;
	var i=0;
	var elemName = "";
	if (typeof(obj.name) == 'undefined') {
		while (obj.attributes[i].nodeName != 'name')i++;
		elemName = obj.attributes[i].value;
	}else{
		elemName = obj.name;
	}
	
	sender.insideBasketName = elemName;
	sender.solved = (elemName == sender.name);
	//sender.solved = (obj.id == sender.destinationBasketId);
};

componentCorrectSequence.prototype.moveToStart = function(obj, elem) 
{
	if (obj.parentText.showAnswerIndicatorAnyTime){
		obj.setBorder(obj.parentText.elementBorderColor, 1);
		obj.setBgColor(obj.parentText.elementBckColor);		
	};
	obj.insideBasketId = '';
	obj.inArea = null;
	//obj.solved = false;
	
	obj.solved = (obj.basketName == "")?true:false;
	obj.x = obj.startX;
	obj.y = obj.startY;
	
	elem.style.left = obj.startX;
	elem.style.top = obj.startY;
};

componentCorrectSequence.prototype.checkIsCorrect = function() 
{
	for (var i=0; i < this.basketsCount; i++) {
		if (!this.texts[i].solved && this.texts[i].insideBasketId != '') return false;
	};
	
	if (this.checkIsCorrect()) return false;
	return true;
};

componentCorrectSequence.prototype.checkIsCorrect = function() 
{
	var result = true;
	for (var i=0; i < this.basketsCount; i++) {
		if (!this.texts[i].solved) {
			result = false;
		};
	}
	return result;
};

componentCorrectSequence.prototype.checkIsBasketFilled = function(obj, basketId) 
{
	for (var i=0; i < this.basketsCount; i++) {
		if (this.texts[i].id != obj.id && this.texts[i].insideBasketId == basketId){
			if (this.replaceElements){
				this.moveToStart(this.texts[i], this.texts[i]);	
				return false;
			}else{
				this.moveToStart(obj, obj);	
				return true;
			}
		}
	}	
	return false;
};

componentCorrectSequence.prototype.onRepaint = function() 
{
	if (PageInterface.GetValue("allowCAI") && this.showCorrectAnswerIndicator) 
	{
		for (var i=0; i < this.texts.length; i++) {
			if (!this.texts[i].solved) {
				if (this.showCorrectAnswerIndicator == true) {
					this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);
					this.texts[i].setBgColor(this.elementInCorrectBckColor);						
				}
			}
			else if (this.showCorrectAnswerIndicator == true) { 
				this.texts[i].setBorder(this.elementCorrectBorderColor, 1);
				this.texts[i].setBgColor(this.elementCorrectBckColor);				
			}
		}
	}
}

componentCorrectSequence.prototype.getAnswer = function(obj)
{
	var result = '';
	var sep = '';
	for (var i=0; i < this.texts.length; i++) {
		if (result != '') sep = '[,]';
		result += sep + this.texts[i].insideBasketId +'[.]'+this.texts[i].id;
	}
	return(result);
};

componentCorrectSequence.prototype.setAnswer = function(valStr, showCorrect)
{
	if ((typeof(valStr) == 'undefined' || valStr == '') && !showCorrect) return;
	if (valStr == '' && (typeof(showCorrect) == 'undefined' || showCorrect.toString() == '')) showCorrect = true;
	
	if (!showCorrect){
		var splited = valStr.split('[,]');
		for (var i=0; i < splited.length; i++){
			var elem = splited[i].split('[.]');
			var ddbasket = document.getElementById(elem[0]);
			var ddelement = document.getElementById(elem[1]);
			var j=-1;
			do{
				j++;
			}while (this.texts[j].id != elem[1] && j < this.texts.length)
			
			if (ddbasket) {
				this.setIntoBasket(this.texts[j], ddelement, ddbasket);		
				this.userSeqArr[i] = this.texts[i].text;
			}
		};
		
		var correct = this.checkSequence();		
		if (correct){
			this.setAllCorrect();
		};		
	}else{
		for (var i=0; i < this.texts.length; i++) {
			var ddelement = document.getElementById(this.texts[i].id);
			var ddbasket = document.getElementById(this.texts[i].destinationBasketId);
			
			if (ddbasket && ddelement){
				this.setIntoBasket(this.texts[i], ddelement, ddbasket);
				this.userSeqArr[i] = this.texts[i].text;
			}else{
				this.moveToStart(this.texts[i], this.texts[i]);
			}
		};
		this.setAllCorrect();
	};
	this.onRepaint();	
};

componentCorrectSequence.prototype.getUsedElements = function()
{
	for (var i=0; i < this.userSeqArr.length; i++)
	{
		if (!this.elementsUsed[this.userSeqArr[i]]){
			this.elementsUsed[this.userSeqArr[i]] = new Array();
		};
		this.elementsUsed[this.userSeqArr[i]].push('used');
	};
}

componentCorrectSequence.prototype.componentCorrectSequenceBaseResetElement = componentCorrectSequence.prototype.resetElement;
componentCorrectSequence.prototype.resetElement = function() 
{
	this.componentCorrectSequenceBaseResetElement();
	
	this.userSeqArr = [];
	this.elementsUsed = [];	
}

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

componentCorrectSequence.prototype.checkSequence = function()
{
	var result = true;
	for (var i=0; i < this.corrSeqArr.length; i++) {
		if (this.corrSeqArr[i] != this.userSeqArr[i])
		{
			result = false;
			break;
		};
	};
	return result;
}
	
componentCorrectSequence.prototype.setAllCorrect = function()
{
		for (var i=0; i < this.texts.length; i++) 
		{
			this.texts[i].solved = true;
		};
}

componentCorrectSequence.prototype.parseCheckAnswer = function(index)
{
	var correct = this.checkSequence();
	
	if (correct){
		this.setAllCorrect();
	}else{
		if (this.notCorrectOutBasketWrong){
			this.setAllOutBasketsWrong();			
	}else{
		this.setSolveToElements();
		}
	};
	
	for (var i=0; i < this.texts.length; i++) {
		var obj = this.texts[i];
		if (!this.texts[i].solved) {
			if (this.returnWhenIncorrect && page.countAnswers != 0){
				this.moveToStart(this.texts[i], this.texts[i]);	
			};
		};
		if (this.showAnswerIndicatorAnyTime && this.showCorrectAnswerIndicator){
			if (this.texts[i].solved){
				this.texts[i].setBorder(this.elementCorrectBorderColor, 1);
				this.texts[i].setBgColor(this.elementCorrectBckColor);
			}else{
				this.texts[i].setBorder(this.elementInCorrectBorderColor, 1);
				this.texts[i].setBgColor(this.elementInCorrectBckColor);
			}
		};		
	}
	return correct;
};

componentCorrectSequence.prototype.setAllOutBasketsWrong = function() 
{
	for (var i=0; i < this.texts.length; i++) 
	{
		if (this.texts[i].insideBasketId == ""){
			this.texts[i].solved = false;
		}
};
}

componentCorrectSequence.prototype.setSolveToElements = function() 
{
	for (var k in this.elementsCorrect)
	{		
		if (this.elementsUsed[k]){
			if (this.elementsCorrect[k].length == this.elementsUsed[k].length)
			{
				if (this.wereUsedInTheSamePlace(k))
				{
					for (var i=0; i < this.elementsByText[k].length; i++)
					{	
						this.texts[this.elementsByText[k][i]].solved = true;
					};
				}else{
					for (var i=0; i < this.elementsByText[k].length; i++)
					{	
						if (this.texts[this.elementsByText[k][i]].basketName == ""){
							this.texts[this.elementsByText[k][i]].solved = true;
						};
					};
				};
			};
		};
		};		
	}

componentCorrectSequence.prototype.wereUsedInTheSamePlace = function(elemText) 
{
	var result = true;
	
	for (var i=0; i < this.corrSeqArr.length; i++) 
	{
		if (this.corrSeqArr[i] == elemText){
			if (this.corrSeqArr[i] != this.userSeqArr[i])
			{
				result = false;
				break;
			};		
		};
	};
	return result;
};

componentCorrectSequence.prototype.onLock = function() 
{
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].canDragging = false;
		this.texts[i].locked = true;
	}
};

componentCorrectSequence.prototype.onUnLock = function() 
{
	for (var i=0; i < this.texts.length; i++) {
		this.texts[i].canDragging = true;
		this.texts[i].locked = false;
	}
};

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

componentCorrectSequence.prototype.interactionsCorrectResponses = function(index)
{
	var result = '';
	var sep = '';
	for (var i=0; i < this.texts.length; i++) {
		if (result != '') sep = '[,]';
		result += sep + this.texts[i].destinationBasketId +'[.]'+this.texts[i].id; 
	}
	return(result);
};

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

componentCorrectSequence.prototype.interactionsCorrectResponsesAsText = function(index)
{
	var result = '';
	var sep = '';
	for (var i=0; i < this.basketsCount; i++) {
		if (result != '') sep = '[,]';
		result += sep + this.texts[i].name+'[.]'+this.texts[i].text;
	}
	return(result);
};

componentCorrectSequence.prototype.interactionsStudentResponseAsText = function()
{
	var result = '';
	var sep = '';
	for (var i=0; i < this.texts.length; i++) {	
		if (result != '') sep = '[,]';
		result += sep + this.texts[i].insideBasketName+'[.]'+this.texts[i].text;
	}
	return(result);
};
