
function resetItem()
{

	for (var i=1; i<= Question.index; i++)
    {
		var questionRef = Question[i];
		if (questionRef.type == "DND")
        {
            /*
            for (var j=0; j<questionRef.numobjs; j++)
            {
                var obj = questionRef.objs[j];
                var base = questionRef.bases[0];
                if (obj.x == obj.startx && obj.y == obj.starty)
                {
                    result = "";
                }
                else
                {
                    result = questionRef.response_labels[j+1] + "=" + (obj.x - base.x) + "," + (obj.y - base.y);
                }
                f.elements[findex].value = result;
                findex++;
            }
            */
		}
		else if (questionRef.type == "PNC")
        {
            /*
			for (var j=0; j < questionRef.numresp; j++){
				//eval('obj = '+questionRef.id + 'Dot' + j); // assign obj to Dot object reference
				//eval('base = '+questionRef.id + 'Base'); // assign base to Base object reference
                var obj = questionRef.dots[j];
                var base = questionRef.bases[0];
				if (obj.getVisibility() != "visible"){ 
					result="";
				} // return the null string if no marker visible
				else {
					result = (obj.midx - base.x) + "," + (obj.midy - base.y);
				} 
				f.elements[findex].value = questionRef.response_labels[j+1] + "=" + result;
				findex++;
			}
            */
		}	
		else if (questionRef.type == "FIB")
        {
            // get form reference
            subform = document.getElementById(questionRef.id);
			subform.elements[0].value = "";
		}
		else if (questionRef.type == "TF" ||
                 questionRef.type == "MCSA")
        {
            // get form reference
            subform = document.getElementById(questionRef.id);

            if (questionRef.display == "dropdown")
            {
                var selectRef = subform[questionRef.response_id];
                selectRef.options[0].selected = true;
            }
            else
            {
                // Default Radio button display
    			for (var j=0; j < subform.elements.length; j++)
                {
    				subform.elements[j].checked = false;
    			} // end loop thru subform elements
            }
		} // end MCSA type
		else if (questionRef.type == "MCMA")
        {
            // get form reference
            subform = document.getElementById(questionRef.id);
			for (var j=0; j < subform.elements.length; j++)
            {
				subform.elements[j].checked = false;
			} // end loop thru subform elements
		
		} // end MCMA type
		else if (questionRef.type == "SVTXT")
        {
            // get form reference
            subform = document.getElementById(questionRef.id);
			subform.elements[0].value = "";
		}
        /*
		else if (questionRef.type == "SSI" || questionRef.type == "SST"){
			for (var j=1; j <= questionRef.numresp; j++){
                // if no labels defined (i.e. no user response) then reset values to null string
                if(questionRef.response_array[j] == "")
                {
        			f.elements[findex].value = ""; // set main form value
        			findex++;
                    numUnanswered++; // increment number of unanswered items/observables
                }
                else
                {                
        			result = questionRef.response_labels[j] + "=" + questionRef.response_array[j]; // grab text value
        			f.elements[findex].value = result; // set main form value
        			findex++;
                }
            }
		}
        */
	}		

} // end reset item


function charCount(id, maxchars) {
	var result=""; // initially set to null
    var subform = document.getElementById(id); // get form reference
	result = subform.elements[0].value; 

	if (result.length > maxchars) {
		alert('Please limit input to '+maxchars+' characters.');
		subform.elements[0].focus();
		return false; // needed for IE
	}
    else
    {
	    return true; // needed for IE
    }
}


function displayTimeLeft()
{
         var timeLeft = document.page.initial_time_left.value
         var hours = Math.floor(timeLeft / 3600000);
         var minutes = Math.floor((timeLeft - (hours * 3600000)) / 60000);
         var seconds = Math.floor((timeLeft - (hours * 3600000) - (minutes * 60000)) / 1000);

         document.time.time_left.value = (hours < 10 ? "0" + hours : hours) + ":" + 
                                         (minutes < 10 ? "0" + minutes : minutes) + ":" + 
                                         (seconds < 10 ? "0" + seconds : seconds);

         if(timeLeft <= 600000 && timeLeft > 599000)
         {
            var query = "theme="+escape(theme)+"&style="+escape(style)+"&exam="+escape(exam)+"&course="+escape(course)+"&version="+escape(version);
            window.open("exam_alert_10minutes.html?"+query, "","toolbar=no,location=no,directories=no,resizable=yes,menubar=no,scrollbars=yes,width=810,height=400");
         }
         
         if(document.page.initial_time_left.value == 0)
         {
			 clearInterval(display_id); // stop repeating of display
			 process('previous');
             response();
			 return false;
         }
         else
         {
			 if (timeLeft - 1000 < 0) 
			 { 
			     timeLeft = 0 
		     }
			 else 
			 { 
			     timeLeft = timeLeft - 1000 
			 }
							  
             document.page.initial_time_left.value = timeLeft;
			 return true;
         }
}

/**
*   <!-- UNICOMMENT
*   setItemResponse will be called by Flash items with an XML string containing 
*   the response.  This response will be converted to the expected output for 
*   the servlet and stored until the user navigates to the next page.
*
*   Method:     setItemResponse
*
*   Parameters:
*       responseXML        String
*
*   Return: void
*   UNICOMMENT -->
*/
function setItemResponse(responseXML)
{
    // If IE6, grab responseXML from the Flash directly
    if (navigator.ie && navigator.browserVersion== 6)
    {
        // Expecting groupID to use to grab full response XML from movie
        responseXML = window.document[responseXML+"Flash"].GetVariable("xmlOut");
    }
    responseXML = unescape(responseXML);
	// Define array to hold all item IDs from the response XML
    var itemIDArray = new Array();
    
    // Split response string by <item to identify the number of items (in case of testlet)
    var itemSplit = responseXML.split('<item');
    // Loop thru split string ignoring the 0 element which contains string prior to 1st <item
    for (var i=1; i<itemSplit.length; i++)
    {
        // Grab item's id attribute value
        var itemID = getAttributeValueByName(itemSplit[i],"id");
    	// Define array to hold the observable names for this item
        var obsName = new Array();
        // Add item ID to array of all item IDs for the response?
	    itemIDArray[itemIDArray.length] = itemID;
       
        // Define regular expression to grab observable element of XML
        obsRE = /<observable[^>]*>([^<]*)<\/observable>/gi;
        obsMatch = itemSplit[i].match(obsRE);
        // Loop thru each observable match
        for (var ii=0; ii<obsMatch.length; ii++)
        {
            // Grab the observables name attribute value
            obsName[ii] = getAttributeValueByName(obsMatch[ii],"name");
            // Grab the substring that contains the observable node value
            obsMatch[ii] = obsMatch[ii].substring(obsMatch[ii].indexOf(">")+1,obsMatch[ii].indexOf("</"));
        } // end for loop thru observables
        
        // Loop thru all Questions on page to find match for the itemID
        for (var ii=1; ii<= Question.index; ii++) 
        {
           if (itemID == Question[ii].id)
           {
               // Store all observables in the Question response property arrays
               for (var iii=0; iii<obsMatch.length; iii++)
               {
                   Question[ii].response_array[iii+1] = obsMatch[iii]; // save parsed XML string for item
                   Question[ii].response_labels[iii+1] = obsName[iii];
               } // end for loop thru item observables
           } // end if item ID match to Question
        } // end for loop thru Questions

	 }// end for loop thru all items in XML
} // end function

/**
*   <!-- UNICOMMENT
*   Method takes an XML string and returns the value in double-quotes that 
*   follows a section containing another specified string.  This is a crude
*   way to obtain the attribute value by specifying the attribute name.
*
*   Method:     getAttributeValueByName
*
*   Parameters:
*       xmlString               String
*       name                    String
*
*   Return: String
*   UNICOMMENT -->
*/
function getAttributeValueByName(xmlString,name)
{
	var xmlDiv = xmlString.split('"');
	for (var i=0; i<xmlDiv.length; i++)
	{
		if (xmlDiv[i].indexOf(name) > -1)
		{
			return xmlDiv[i+1];
		}
	}
}

/**
*   <!-- UNICOMMENT
*   This method will set a flag in each Flash AI to prevent IE from submitting
*	to the page after page submission has begun.
*
*   Method:     endFlashSubmission
*
*   Parameters: none
*
*   Return: void
*   UNICOMMENT -->
*/
function endFlashSubmission()
{ 
	var flashHash = new Object();
    for (var i=1; i<= Question.index; i++) 
    {
		var questionRef = Question[i];
		if(questionRef.type == "SSI" || (questionRef.type == "SST" && !flashHash[questionRef.groupid]))
		{
			window.document[questionRef.groupid+"Flash"].SetVariable("hdrName","No submit");
			window.document[questionRef.groupid+"Flash"].SetVariable("response","none");
			flashHash[questionRef.groupid] = true;
		}
    } // end for loop thru Questions
	
}
