/**
 *	Zoki Test js engine functions
 *
 *	This file provides js functions that makes test work
 *
 *	Copyright (c) 2007, Zoki Soft <info@zokisoft.com>.
 * 
 *	This file is part of ZokiTest.
 *	
 *	Zoki Test is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Zoki Test is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public License
 *	along with Zoki Test.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// =========================================================================
// Display==================================================================
// =========================================================================
	    	function testVariantList(variant, bReset)
	    	{

				// if 'variant is set - it means that this function called first time
				if ( variant )
				{
					// generate array form given xml
					aTezt = new Array();
					aTezt['id'] = variant.getElementsByTagName('id')[0].firstChild.data;

					// get weclomepage
					var aWelcomePage = variant.getElementsByTagName('welcomepage');
					aTezt['welcomePage'] = new Array();
					aTezt['welcomePage']['text'] = aWelcomePage[0].getElementsByTagName('text')[0].firstChild.data;
					if ( null !== aWelcomePage[0].getElementsByTagName('image')[0].firstChild )
						aTezt['welcomePage']['image'] = aWelcomePage[0].getElementsByTagName('image')[0].firstChild.data;
					else
						aTezt['welcomePage']['image'] = '';

					// get pages
					var aPageList = variant.getElementsByTagName('page');
					aTezt['pageList'] = new Array();
					for ( var i = 0; i < aPageList.length; i++ )
		    		{

						aTezt['pageList'][i] = new Array();
						aTezt['pageList'][i]['question'] = aPageList[i].getElementsByTagName('question')[0].firstChild.data;
						if ( null !== aPageList[i].getElementsByTagName('image')[0].firstChild )
							aTezt['pageList'][i]['image'] = aPageList[i].getElementsByTagName('image')[0].firstChild.data;
						else
							aTezt['pageList'][i]['image'] = '';

						var aVariantList = aPageList[i].getElementsByTagName('variant');
						aTezt['pageList'][i]['variantList'] = new Array();
						for ( var j = 0; j < aVariantList.length; j++ )
		    			{
			    			aTezt['pageList'][i]['variantList'][j] = new Array();
					    	aTezt['pageList'][i]['variantList'][j]['text'] = aVariantList[j].getElementsByTagName('text')[0].firstChild.data;
					    	aTezt['pageList'][i]['variantList'][j]['points'] = aVariantList[j].getElementsByTagName('points')[0].firstChild.data;

					    }
					}

					// get resultpage
					var aResultPage = variant.getElementsByTagName('resultpage');

					aTezt['resultPage'] = new Array();
					if ( null !== aResultPage[0].getElementsByTagName('text')[0].firstChild )
						aTezt['resultPage']['text'] = aResultPage[0].getElementsByTagName('text')[0].firstChild.data;
					else
						aTezt['resultPage']['text'] = '';
					var aResultList = aResultPage[0].getElementsByTagName('variant');
					aTezt['resultPage']['resultList'] = new Array();
					for ( var i = 0; i < aResultList.length; i++ )
		    		{
		    			aTezt['resultPage']['resultList'][i] = new Array();
						if ( null !== aResultList[i].getElementsByTagName('image')[0].firstChild )
							aTezt['resultPage']['resultList'][i]['image'] = aResultList[i].getElementsByTagName('image')[0].firstChild.data;
						else
							aTezt['resultPage']['resultList'][i]['image'] = '';
				    	aTezt['resultPage']['resultList'][i]['text'] = aResultList[i].getElementsByTagName('text')[0].firstChild.data;
				    	aTezt['resultPage']['resultList'][i]['points'] = new Array();
						aTezt['resultPage']['resultList'][i]['points']['min'] = aResultList[i].getElementsByTagName('min')[0].firstChild.data;
						aTezt['resultPage']['resultList'][i]['points']['max'] = aResultList[i].getElementsByTagName('max')[0].firstChild.data;
					}
				}
// -----------------------------

				var ID = aTezt['id'];

				// disable 'next' button
				var oNextButton = document.getElementById('dtest_actions_next_' + ID );
				var oAnswerButton = document.getElementById('dtest_actions_answer_' + ID );
				//oNextButton.className = 'testButtonInactive';
				oNextButton.style.display='none';
				oAnswerButton.style.display='inline';

				// if page number is not set - starts from -1 and show welcome page;
				// otherwise use this number as current page
				// if this is last page - show result list
				if ( undefined === window.iCurrentPage || bReset )
				{
					iCurrentPage = -1;
					
					if ( !bReset )
					{
						// make hidden field that will collect all points
						// and field that will keep last vote points
			    		container = document.getElementById('dtest_' + ID );
						var newInput = document.createElement("input");
						newInput.type = "hidden";
						newInput.id = "dtest_points_" + ID;
						newInput.value = '0';
						container.appendChild(newInput);
						var newInput = document.createElement("input");
						newInput.type = "hidden";
						newInput.id = "dtest_lastpoint_" + ID;
						newInput.value = '0';
						container.appendChild(newInput);
}
					else
					{
						// reset 'dtest_points_' and 'dtest_lastpoint_' values
						document.getElementById('dtest_points_' + ID ).value='0';
						document.getElementById('dtest_lastpoint_' + ID ).value='0';
										
					}

					// enable 'next' button
					oNextButton.style.display = 'inline';
					oAnswerButton.style.display = 'none';					
					document.getElementById('dtest_actions_reset_' + ID ).style.display = 'none';
					document.getElementById('dtest_actions_next_' + ID ).style.display = 'none';
document.getElementById('dtest_actions_begin').style.display = 'inline';
//oNextButton.className = 'testButtonActive';

					return testWelcomeList();
				}
				else
				{
					if ( iCurrentPage < (aTezt['pageList'].length-1) ) {
						iCurrentPage++;
						document.getElementById('dtest_actions_reset_' + ID ).style.display = 'inline';
						document.getElementById('dtest_actions_begin').style.display = 'none';

					}
					else
					{
						oNextButton.style.display = 'none';
						return testResultList();
					}
				}

// make question -------------------
	    		var container = document.getElementById('dtest_caption_' + ID );
	    		container.innerHTML = '';

				var newDiv = document.createElement("div");
				newDiv.id = "dtest_caption_text_" + ID;
				newDiv.className = "testTextBox";

				newDiv.innerHTML = aTezt['pageList'][iCurrentPage]['question'];
				container.appendChild( newDiv );
// -----------------------------------------
// make variant lists ----------------------

				// reset 'lastpoint' value
				document.getElementById("dtest_lastpoint_" + ID).value = 0;

	    		container = document.getElementById('dtest_content_' + ID );
	    		container.innerHTML = '';

				// append image
				if ( '' != aTezt['pageList'][iCurrentPage]['image'] )
				{
					newImg = document.createElement("img");
					newImg.src = aTezt['pageList'][iCurrentPage]['image'];
					newImg.onload = function(){ scrollDisplay(container, ID); };

				    newDiv = document.createElement("div");
				    newDiv.className = "testPageImageBox";
				    newDiv.appendChild(newImg);

				    container.appendChild(newDiv);
				}

	    		for ( var i = 0; i < aTezt['pageList'][iCurrentPage]['variantList'].length; i++ )
	    		{

				    var sText = aTezt['pageList'][iCurrentPage]['variantList'][i]['text'];
					var iPoints = aTezt['pageList'][iCurrentPage]['variantList'][i]['points'];

				    newDiv = document.createElement("div");
				    newDiv.id = 'var_' + ID + '_' + i;
				    newDiv.className = "testTextBox";

					newDiv.innerHTML = sText;

				    var newDiv2 = document.createElement("div");
					newDiv2.className = "testTextBoxWrapper";
				    newDiv2.appendChild( newDiv );

				    var newDiv3 = document.createElement("div");
				    newDiv3.className = "testTextContainer";

				    newInput = createNamedElement( "input", "vote_" + ID );
				    newInput.type = "radio";
				    newInput.value = iPoints;
				    newInput.className = "testRadioButton";
				    newInput.onclick = function(){ setPoints(this.value,ID); };

				    newDiv3.appendChild( newInput );
				    newDiv3.appendChild( newDiv2 );

				    container.appendChild( newDiv3 );

				}
// ---------------------------------
				scrollDisplay(container, ID);

				return true;

	    	}


			function testWelcomeList()
			{

				var ID = aTezt['id'];

				// resel caption
	    		var container = document.getElementById('dtest_caption_' + ID );
	    		container.innerHTML = '';

				// reset body
				container = document.getElementById('dtest_content_' + ID );
				container.innerHTML = '';

				// append image
				if ( '' != aTezt['welcomePage']['image'] )
				{
					newImg = document.createElement("img");
					newImg.src = aTezt['welcomePage']['image'];
					newImg.onload = function(){ scrollDisplay(container, ID); };

				    newDiv = document.createElement("div");
				    newDiv.className = "testPageImageBox";
				    newDiv.appendChild(newImg);

				    container.appendChild(newDiv);
				}

			    var sText = aTezt['welcomePage']['text'];

			    newDiv = document.createElement("div");
			    newDiv.id = 'wel_' + ID;
			    newDiv.className = "testTextBox";

				newDiv.innerHTML = sText;

			    var newDiv2 = document.createElement("div");
				newDiv2.className = "testTextBoxWrapper";
			    newDiv2.appendChild( newDiv );

			    var newDiv3 = document.createElement("div");
			    newDiv3.className = "testTextContainer";

			    newDiv3.appendChild( newDiv2 );

			    container.appendChild( newDiv3 );

				scrollDisplay(container, ID);

				return true;

			}



	    	function testResultList()
	    	{

				var ID = aTezt['id'];

// make question -------------------
	    		var container = document.getElementById('dtest_caption_' + ID );
	    		container.innerHTML = '';

				var newDiv = document.createElement("div");
				newDiv.id = "dtest_caption_text_" + ID;
				newDiv.className = "testTextBox";

				newDiv.innerHTML = aTezt['resultPage']['text'];
				container.appendChild( newDiv );
// -------------------------------------
// make result

				var iPoints = document.getElementById("dtest_points_" + ID).value;

				container = document.getElementById('dtest_content_' + ID );
				container.innerHTML = '';

				//hide Select Answer from test response
				document.getElementById('dtest_actions_answer_' + ID ).style.display='none';

				for ( var i = 0; i < aTezt['resultPage']['resultList'].length; i++ )
	    		{
					var iMax = aTezt['resultPage']['resultList'][i]['points']['max'];
					var iMin = aTezt['resultPage']['resultList'][i]['points']['min'];

					if ( (+iPoints) >= (+iMin) && (+iPoints) <= (+iMax) )
					{

						// append image
						if ( '' != aTezt['resultPage']['resultList'][i]['image'] )
						{
							newImg = document.createElement("img");
							newImg.src = aTezt['resultPage']['resultList'][i]['image'];
							newImg.onload = function(){ scrollDisplay(container, ID); };

						    newDiv = document.createElement("div");
						    newDiv.className = "testPageImageBox";
						    newDiv.appendChild(newImg);

						    container.appendChild(newDiv);
						}

					    var sText = aTezt['resultPage']['resultList'][i]['text'];

					    newDiv = document.createElement("div");
					    newDiv.id = 'var_' + ID + '_' + i;
					    newDiv.className = "testTextBox";

					    newDiv.innerHTML = sText;

					    var newDiv2 = document.createElement("div");
						newDiv2.className = "testTextBoxWrapper";
					    newDiv2.appendChild( newDiv );

					    var newDiv3 = document.createElement("div");
					    newDiv3.className = "testTextContainer";

					    newDiv3.appendChild( newDiv2 );

					    container.appendChild( newDiv3 );

						break;
					}
	    		}

				scrollDisplay(container, ID);

	    		return true;
	    	}

// =========================================================================
// Utils====================================================================
// =========================================================================

		// create element with name ( this is problem with IE )
			function createNamedElement( type, name )
			{
				var element;
				try
				{
					element = document.createElement('<'+type+' name="'+name+'">');
				}catch(e){}
				if (!element || !element.name) // Cool, this is not IE !!
				{
					element = document.createElement(type)
					element.name = name;
				}
				return element;
			}


		    function scrollDisplay(container, ID)
		    {

				// jump to top position
				container.style.top = '0px';

				if ( ( container.offsetTop + container.offsetHeight ) <= container.parentNode.offsetHeight )
				{
				    document.getElementById( 'dtest_arr_up_' + ID ).style.display='none';
				    document.getElementById( 'dtest_arr_down_' + ID ).style.display='none';
				}
				else
				{
				    document.getElementById( 'dtest_arr_up_' + ID ).style.display='block';
				    document.getElementById( 'dtest_arr_down_' + ID ).style.display='block';
				}

				return true;
	    	}


    		function scrollStart( item, direction )
    		{

				currentItem = item;

				if ( 'up' == direction )
				{
				    scrollStop();
				    iter = window.setInterval( 'moveUp()', 10 );
				}
				else if ( 'down' == direction )
				{
				    scrollStop();
				    iter = window.setInterval( 'moveDown()', 10 );
				}

				return true;

    		}


    		function scrollStop()
    		{
				if ( undefined != window.iter )
				{
	    			window.clearInterval(iter);
				}
    		}


		    function moveUp()
    		{
				if ( (currentItem.offsetTop + currentItem.offsetHeight) > currentItem.parentNode.offsetHeight )
				{
	    			currentItem.style.top = (currentItem.offsetTop-2) + 'px';
				}
		    }


    		function moveDown()
    		{
				if ( currentItem.offsetTop < 0 )
				{
	    			currentItem.style.top = (currentItem.offsetTop+2) + 'px';
				}
			}


			function setPoints(iPoints,ID)
			{
				// enable 'next' button
				document.getElementById('dtest_actions_next_' + ID ).style.display='inline';
				document.getElementById('dtest_actions_answer_' + ID ).style.display='none';
				var oPointSumm = document.getElementById("dtest_points_" + ID);
				var oLastPoint = document.getElementById("dtest_lastpoint_" + ID);

				oPointSumm.value=(+oPointSumm.value)-oLastPoint.value+(+iPoints);
				oLastPoint.value = iPoints;
			}

