//Main JScript File
var FirstItem;
//*************** SET BODY WIDTH&HEIGHT ****************//
function RestrictBodySize(width, height)
{
   document.body.style.width = width;
   document.body.style.height = height;
}
//******************************************************//

//********** INITIALIZE BUTTONS AFTER LOADING **********//
function BodyOnLoad()
{
    HideButtonBorder();
}

function HideButtonBorder()
{
    var btnPre = document.getElementById("btnPrevious");
    if(btnPre)
    {
    	btnPre.style.padding = 0;
		btnPre.cellSpacing = 0;
    }
    
    var btnNxt = document.getElementById("btnNext");    
    if(btnNxt)
    {
    	btnNxt.style.padding = 0;
		btnNxt.cellSpacing = 0;		
    }
    
    var btnEnd = document.getElementById("btnEnd");
    if(btnEnd)
    {
    	btnEnd.style.padding = 0;
		btnEnd.cellSpacing = 0;
    }
}

function SetFocusOnFirstItem()
{
	FirstItem = null;
	var mainBody = document.getElementById("divBody");
	FindFirstItem(mainBody);
	if(!ObjectIsNull(FirstItem))
	{
	    if(document.body.clientHeight+document.body.scrollTop > GetPosition(FirstItem).y&&!FirstItem.disabled)
		    FirstItem.focus();
	}
}

function FindFirstItem(parent)
{
	if(!ObjectIsNull(parent.name) && parent.name.indexOf("answer") != -1)
	{
	    if(parent.type !="hidden")
		    FirstItem = parent;		
		return;		
	}
	var len = parent.childNodes.length;
	var i = 0;
	while (i < len)
	{
		FindFirstItem(parent.childNodes[i]);
		if(!ObjectIsNull(FirstItem)) break;
		i = i + 1;
	}
}

function InitialUI()
{
    InitialSPMainTable();
    InitForSplitter();
    setMainWidth();
}

function SetShortAnswerStatus(chkID)
{
    var chkControl = document.getElementById(chkID);
    var textControl =document.getElementById(chkID + '_TXT');
    if(chkControl.checked ==0)
    {
        textControl.blur();
    }    
}

function InitialSPMainTable()
{
    if (isMoz || isKHTML)
    {
        var headerHeight = 0;
        var footerHeight = 0;
        if(document.getElementById("trHeader"))
        {
	        headerHeight = trHeader.clientHeight;
        }
        if(document.getElementById("trFooter"))
        {
	        footerHeight = trFooter.clientHeight;
        }
        
        var bodyTopMargin = 0;
        var bodyBottomMargin = 0;
        
        if(parseInt(GetCssValue("body","marginTop"))>0)
            bodyTopMargin = parseInt(GetCssValue("body","marginTop"));
        if(parseInt(GetCssValue("body","marginBottom"))>0)
            bodyBottomMargin = parseInt(GetCssValue("body","marginBottom"));
        
        var cthgt = ((document.body.clientHeight - bodyTopMargin - bodyBottomMargin)/document.body.clientHeight*100);
        var hgt;
        if (cthgt >= 100)
        {
            hgt = "100%";
        }
        else
        {
            hgt = cthgt.toString().substring(0,2) + "%";
        }
        document.body.style.height=hgt;

        // sp_MailTable
        if(document.getElementById("spMain_table"))
        {
            document.getElementById("spMain_table").style.height = document.body.clientHeight - bodyTopMargin - bodyBottomMargin;
        }
        // tvPackage
        if (document.getElementById("tvPackage"))
        {
            document.getElementById("tvPackage").style.height = document.getElementById("spMain_table").style.height;       
        }
    }   
}
//******************************************************//

//******* TOGGLES IMAGE FOR IMAGEBUTTON CONTROLS *******//
function ToggleImgInButton(objElement,imgObj)
{    
    if (imgObj == null || objElement==null)
    {
        return false;
    }
    objElement.src = imgObj;
}
//******************************************************//

//********** GET THE STYLE OF THE OBJECT **********//
function GetCssValue(SelectorText,StyleItem)
{
    var cssValue = '';
    var objStyle = document.styleSheets[0];
    if (objStyle)
    {
        var ruleLen;
        if (isIE)
        {
            ruleLen = objStyle.rules.length;
        }
        if (isMoz || isKHTML)
        {
            ruleLen = objStyle.cssRules.length;
        }
        for (ridx = 0; ridx < ruleLen; ridx++)
        {
            var oRules;
            if (isIE)
            {
                oRules = objStyle.rules;
            }
            if (isMoz || isKHTML)
            {
                oRules = objStyle.cssRules;
            }

            if (oRules[ridx].selectorText.toLowerCase().replace('.','') == SelectorText.toLowerCase())
            {
                switch (StyleItem)
                {
                    case 'fontSize':
                        cssValue = oRules[ridx].style.fontSize;
                        return cssValue;
                        break;
                    case 'color':
                        cssValue = oRules[ridx].style.color;
                        return cssValue;
                        break;
                    case 'backgroundColor':
                        cssValue = oRules[ridx].style.backgroundColor;
                        return cssValue;
                        break;
                    case 'marginTop':
                        cssValue = oRules[ridx].style.marginTop;
                        return cssValue;
                        break;
                    case 'marginRight':
                        cssValue = oRules[ridx].style.marginRight;
                        return cssValue;
                        break;
                    case 'marginBottom':
                        cssValue = oRules[ridx].style.marginBottom;
                        return cssValue;
                        break;
                    case 'marginLeft':
                        cssValue = oRules[ridx].style.marginLeft;
                        return cssValue;
                        break;
                    case 'width':
                        cssValue = oRules[ridx].style.width;
                        return cssValue;
                        break;
                    case 'height':
                        cssValue = oRules[ridx].style.height;
                        return cssValue;
                        break;
                    case 'textAlign':
                        cssValue = oRules[ridx].style.textAlign;
                        return cssValue;
                        break;
                    case 'cssText':
                        cssValue = oRules[ridx].style.cssText;
                        return cssValue;
                        break;
                }
            }
        }
    }
    return cssValue;
}
//******************************************************//

//**************** VALIDATE WHEN SUBMIT ****************//
//**** NAME: ANSWER_ESSAY_*_T ****
function checkItemReplied(ifAlert)
{

    var allInputElement = document.getElementsByTagName('input');
    var allTextAreaElement = document.getElementsByTagName('textArea');
    var arrNames = removeReplicativeName(allInputElement);
    var arrNamesOfTextArea = removeReplicativeName(allTextAreaElement);
    for (j=0; j<arrNamesOfTextArea.length; j++)
	{
	    arrNames.push(arrNamesOfTextArea[j]);
	} 
    var range = arrNames.length;
    
	for (i=(range-1); i>0; i--)
	{
        
		// Check if be replied
		var arrayName = arrNames[i].split("_");
		if (arrayName[0] != "answer")
		{
			continue;
		}
		
		if(arrNames[i].indexOf("_TXT")>0)
        {
            continue;
        }
		
		// Essay
		if (arrayName[1] == "Essay")
		{
		    var essays = document.getElementsByName(arrNames[i]);
		    for(var j=0;j<essays.length; j++)
		    {
		        var essay = essays[j];
                if (essay.value == "" && essay.disabled == false)
                {
				    // Alert and go focus
				    var result = AlertOrConfirm(ifAlert);
				    essay.focus();
				    return result;
                }
		    }
		}
		
		// Radio and checkbox
		if (arrayName[1] == "TF" || arrayName[1] == "MCSS" || arrayName[1] == "MCMS"|| arrayName[1] == "MCMSA"|| arrayName[1] == "MCSSA")
		{
		    var isShortAnswerEmpty = false;
		    var isAnyChoiceSelected = false;
		    var choices = document.getElementsByName(arrNames[i]);
		    for(var j=0;j<choices.length;j++)
		    {
	            if(choices[j]&&!choices[j].disabled&&choices[j].checked)
	            {
	                isAnyChoiceSelected = true;
	                var shortAnswerTextBox = getShortAnserTextBox(choices[j].id);
	                if(shortAnswerTextBox)
	                {
	                    if(!shortAnswerTextBox.value||shortAnswerTextBox.value =="")
	                    {
	                        isShortAnswerEmpty = true;
	                    }
	                }
	            }
		    }
		    
		    if(!isAnyChoiceSelected)
		    {
		        var result = AlertOrConfirm(ifAlert);
		        if(choices[0])
		        {
		            choices[0].focus();
		        }
		        return result;
		    }
		    if(isShortAnswerEmpty)
		    {
		        alert(GlobalMsg.ShortAnswerInvalid);
		        return false;
		    }
		}
	}
	return true;
}
function checkItemRepliedOnly() {

    var allInputElement = document.getElementsByTagName('input');
    var allTextAreaElement = document.getElementsByTagName('textArea');
    var arrNames = removeReplicativeName(allInputElement);
    var arrNamesOfTextArea = removeReplicativeName(allTextAreaElement);
    for (j = 0; j < arrNamesOfTextArea.length; j++) {
        arrNames.push(arrNamesOfTextArea[j]);
    }
    var range = arrNames.length;

    for (i = (range - 1); i > 0; i--) {

        // Check if be replied
        var arrayName = arrNames[i].split("_");
        if (arrayName[0] != "answer") {
            continue;
        }

        if (arrNames[i].indexOf("_TXT") > 0) {
            continue;
        }

        // Essay
        if (arrayName[1] == "Essay") {
            var essays = document.getElementsByName(arrNames[i]);
            for (var j = 0; j < essays.length; j++) {
                var essay = essays[j];
                if (essay.value == "" && essay.disabled == false) {
                    return false;
                }
            }
        }

        // Radio and checkbox
        if (arrayName[1] == "TF" || arrayName[1] == "MCSS" || arrayName[1] == "MCMS" || arrayName[1] == "MCMSA" || arrayName[1] == "MCSSA") {
            var isShortAnswerEmpty = false;
            var isAnyChoiceSelected = false;
            var choices = document.getElementsByName(arrNames[i]);
            for (var j = 0; j < choices.length; j++) {
                if (choices[j] && !choices[j].disabled && choices[j].checked) {
                    isAnyChoiceSelected = true;
                    var shortAnswerTextBox = getShortAnserTextBox(choices[j].id);
                    if (shortAnswerTextBox) {
                        if (!shortAnswerTextBox.value || shortAnswerTextBox.value == "") {
                            isShortAnswerEmpty = true;
                        }
                    }
                }
            }

            if (!isAnyChoiceSelected || isShortAnswerEmpty) {
                return false;
            }
        }
    }
    return true;
}
function getShortAnserTextBox(choiceName)
{
    var shortAnswerTextBoxID = choiceName+"_TXT";
	var shortAnswerTextBox = document.getElementById(shortAnswerTextBoxID);
	return shortAnswerTextBox;
}

function removeReplicativeName(inputElements)
{
    var keyArray = [];
    var keyObject = {};

    var addKey = function (key) 
    {
        if (!keyObject[key]) 
        {
            keyObject[key] = 1;
            keyArray[keyArray.length] = key;
        }
    }

    for(var i=0;i<inputElements.length;i++)
    {
        addKey(inputElements[i].name);
    }
    return keyArray;
}

function AlertOrConfirm(ifAlert)
{
	// When timeout skipping
	var timeoutFlag = document.getElementById("timeOutFlag");
	if (timeoutFlag)
	{
	    if (timeoutFlag.value == "true")
		{
		    return true;
		}
	}

	if (ifAlert)
	{
		AlertForAtLeastOneItemNotAnswered();
		return false;
	}
    return true;	
}

function checkItemRepliedAlert()
{
   return checkItemReplied(true);
}

function checkItemRepliedNotAlert()
{
   return checkItemReplied(false);
}

function ClosePopupWindow(windowName)
{
    var pwWindow = pcCommon.GetWindowByName(windowName);
    pcCommon.SetWindowContentHTML(pwWindow,"");
    pcCommon.HideWindow(pwWindow);
    return false;
}

function ShowFeedback(previewMode,level,key,type,feedback,nextPage,evt)
{
    var lKey;
    var lFeedback;
    if (level=="Item")
    {
        lKey = key;
        lFeedback = feedback;
    }
    else
    {
        var currentItemName = "";
        var currentItemValue;

        var arrNode = document.getElementsByTagName('input');
        var range = arrNode.length;

        for (i=(range-1); i>0; i--)
        {
            var choice = arrNode[i];
            if (choice.name == null)
            {
                continue;
            }

            if (choice.name == "btnCancel")
            {
	            break;
            }

            var arrayName = choice.id.split("_");
            //alert(choice.name + ',' + choice.id);
            if (arrayName[0] != "answer")
            {
	            continue;
            }

            // Essay
            if (arrayName[1] == "Essay")
            {
                continue;
            }
            // Radio and checkbox
            if (arrayName[1] == "TF" || arrayName[1] == "MCSS" || arrayName[1] == "MCMS" || arrayName[1] == "MCMSA"|| arrayName[1] == "MCSSA")
            {
	            if (currentItemName == "")
	            {
	                if(choice.type !="text")
	                {
		                currentItemName = choice.name;
		            }
	            }
            	
	            if (choice.name==currentItemName)
	            {
		            if (choice.checked==true)
		            {
		                if (arrayName.length>6)
		                {
			                currentItemValue = arrayName[5];
			                lFeedback = arrayName[6];
			                break;
			            }
		            }
	            }
            }		
        }
        if (currentItemValue !== null)
        {
            lKey = currentItemValue;
        }
        if (ObjectIsNull(lFeedback))
        {
            lFeedback = "NF";
        }
    }

    if (!ObjectIsNull(lKey) && !ObjectIsNull(lFeedback) && lFeedback == "F")
    {
        var url = 'Messages.aspx?isPreviewMode='+previewMode+'&Level='+level+'&MessageType='+type+'&ItemKey='+lKey +'';        
        var pwFeedback = pcCommon.GetWindowByName('Feedback');
        pcCommon.SetWindowContentHTML(pwFeedback,""); 
        pwFeedback.SetHeaderText('Feedback');
        pcCommon.SetWindowContentUrl(pwFeedback,url);
        pcCommon.ShowWindow(pwFeedback);       

        if (!ObjectIsNull(nextPage))
        {
            var btn = document.getElementById('btnFeedbackClose');

            btn.onclick = null;

            btn.onclick=function(s,e){
                ClosePopupWindow('Feedback');
                return JumpToPage(nextPage,s);
            }
        }
    }
    if (!ObjectIsNull(lFeedback) && lFeedback == "NF")
    {
        evt = evt ? evt : (window.event ? window.event : null);
        var event = evt;
        return JumpToPage(nextPage,event);
    }
    return false;
}
function ShowItemFeedbackOnEnd(previewMode,level,key,type,feedback,isShowModalPopup)
{
    var lKey;
    var lFeedback;
    if (level=="Item")
    {
        lKey = key;
        lFeedback = feedback;
    }
    else
    {
        var currentItemName = "";
        var currentItemValue;

        var arrNode = document.getElementsByTagName('input');
        var range = arrNode.length;

        for (i=(range-1); i>0; i--)
        {
            var choice = arrNode[i];
            if (choice.name == null)
            {
                continue;
            }

            if (choice.name == "btnCancel")
            {
	            break;
            }

            var arrayName = choice.id.split("_");
            if (arrayName[0] != "answer")
            {
	            continue;
            }

            // Essay
            if (arrayName[1] == "Essay")
            {
                continue;
            }
            // Radio and checkbox
            if (arrayName[1] == "TF" || arrayName[1] == "MCSS" || arrayName[1] == "MCMS" || arrayName[1] == "MCMSA"|| arrayName[1] == "MCSSA")
            {
	            if (currentItemName == "")
	            {
	                if(choice.type !="text")
	                {
		                currentItemName = choice.name;
		            }
	            }
            	
	            if (choice.name==currentItemName)
	            {
		            if (choice.checked==true)
		            {
		                if (arrayName.length>6)
		                {
			                currentItemValue = arrayName[5];
			                lFeedback = arrayName[6];
			                break;
			            }
		            }
	            }
            }		
        }
        if (currentItemValue != null)
        {
            lKey = currentItemValue;
        }
        if (ObjectIsNull(lFeedback))
        {
            lFeedback = "NF";
        }
    }
    
    if (!ObjectIsNull(lKey) && !ObjectIsNull(lFeedback) && lFeedback == "F")
    {        
        var url = 'Messages.aspx?isPreviewMode='+previewMode+'&Level='+level+'&MessageType='+type+'&ItemKey='+lKey +'';      
        var pwFeedback = pcCommon.GetWindowByName('Feedback');
        pwFeedback.SetHeaderText('Feedback');
        pcCommon.SetWindowContentUrl(pwFeedback,url);
        pcCommon.ShowWindow(pwFeedback);       

        var btn = document.getElementById('btnFeedbackClose');
        btn.onclick = null;
        
        if (isShowModalPopup == "true")
        {
            btn.onclick=function(s,e){
                ClosePopupWindow('Feedback');
                if(ShowModalPopup())
                {
                    btnEndPostBack();
                } 
            }
        }
        else {
            if (isShowModalPopup == "auto") {
                SetTakeRest();
            }
            btn.onclick=function(s,e){
                ClosePopupWindow('Feedback');
                btnEndPostBack();
            }
        }
    }
    if(level == "Choice" && lFeedback == "NF")
    {
        if (isShowModalPopup == "true") {
            ShowModalPopup();
        }
        else if (isShowModalPopup == "auto") {
            SetTakeRest();
        }
        else {
            return true;
        }
    }
    return false;
}

function btnEndPostBack() {
    window.onunload = "";
    __doPostBack('btnEnd', '');
}

//******************************************************//

//************* GET TEXTAREA HEIGHT&WIDTH - USED WHEN CREATING ESSAY ITEM **************//
var g_width = 430;
var g_height = 5 + 16 * 10;

function getWidth(element,textAreaWidth, marginLeft, marginRight)
{
	if(textAreaWidth == null)
		textAreaWidth = g_width;	
//	var str_width;
//	var bodyoffsetwidh = document.body.offsetWidth;
//	var bodyoffsetleft = document.body.offsetLeft;	
//	var scrollWidth=document.body.scrollWidth;
//	var treeWidth = 0;	
//	var tvPackage = document.getElementById("tvPackage");
//	if(!ObjectIsNull(tvPackage))
	//		treeWidth = tvPackage.offsetWidth;

	var parent = element.offsetParent;
	var width = 0;
	if (parent.offsetWidth && parent.offsetHeight) {
	    width = parent.offsetWidth;
	}
	else if (parent.style && parent.style.pixelWidth && parent.style.pixelHeight) {
	    width = parent.style.pixelWidth;
	}
	if (textAreaWidth > width) {
	    return "100%";
	}
	else
	{
	    return textAreaWidth + "px";
	}
}

function getHeight(textAreaHeight)
{
	if(textAreaHeight == null)
		textAreaHeight = g_height;
	var str_height;
	var bodyoffsetheight = document.body.offsetHeight;
	var bodyoffsettop= document.body.offsetTop;	
	if (bodyoffsetheight < (bodyoffsettop + textAreaHeight + 120))
	{
		str_height = bodyoffsetheight - bodyoffsettop - 120 + "px";
	}
	else
	{
		str_height = textAreaHeight + "px";
	}
	return str_height;
}

function setMainWidth()
{
    if(!isIE)
        return;
    var tvPackageWidth = 0;
    if(document.getElementById("tvPackage"))
    {
        tvPackageWidth = document.getElementById("tvPackage").offsetWidth;
    }
    var bodyObj = document.getElementById("htmlBody");
    var bodyoffsetwidh = bodyObj.offsetWidth;
    var divBodyObj = document.getElementById("divBody");
    var splitterWidth = 0;
    if (document.getElementById("splitter"))
    {
        if("" != document.getElementById("splitter").style.width)
        {
            splitterWidth = parseInt(document.getElementById("splitter").style.width);
        }
    }
    var bodyRightMargin = 0;
    if (parseInt(GetCssValue("body", "marginRight")) > 0)
    {
        bodyRightMargin = parseInt(GetCssValue("body", "marginRight"));
    }    
    var mainWidth = bodyoffsetwidh - tvPackageWidth - splitterWidth - bodyRightMargin;
    divBodyObj.style.width = mainWidth+"px";
    if(bodyObj.scrollWidth >= bodyoffsetwidh+60)
    {
        mainWidth = mainWidth - 60;
    }
    divBodyObj.style.width = mainWidth+"px";
    if(bodyoffsetwidh > bodyObj.clientWidth)
    {
        mainWidth = mainWidth - (bodyoffsetwidh - bodyObj.clientWidth);
        divBodyObj.style.width = mainWidth+"px";
    }
}

function resizeWindow(width, height)
{
	var screenWidth = window.screen.width;
	var screenHeight = window.screen.height;
	if(width >= screenWidth)
	{
		width = screenWidth;
	}
	if(height >= screenHeight)
	{
		height = screenHeight;
	}
	window.resizeTo(width,height);
	window.moveTo((screenWidth - width)/2,(screenHeight - height)/2);
}
//******************************************************//

//******************* TIME FUNCTION ********************//
// show time
function renderTime()
{
    var timeoutFlag = document.getElementById("timeOutFlag");
    if (timeoutFlag)
    {
	    if (timeoutFlag.value == "true")
	    {
		    return;
		}
	}
	
	var lastTime = document.getElementById("timeBox").value;
	
	var timeParts = lastTime.split(':');
	if (timeParts.length != 3)
	{
		return;
	}
	var h = timeParts[0];
	var m = timeParts[1];
	var s = timeParts[2];
	if (isNaN(h) || isNaN(m) || isNaN(s))
	{
		return;
	}
	
	s--;	
    if (s < 0)
    {
	    s = 59;
	    m --;
	    if (m < 0)
	    {
		    m = 59;
		    h --;
		    if (h < 0)
		    {
			    h = 0;
			    m = 0;
			    s = 0;
		    }
	    }
    }
    thisTime = timeFormat(h) + ":" + timeFormat(m) + ":" + timeFormat(s);
    document.getElementById("timeBox").value = thisTime;
	    
	// When last 3 seconds, disable btnPrevious to prevent user break the rule
	if (h<=0 && m<=0 && s<=3)
	{
	    if(document.getElementById("btnPrevious"))
	    {
		    document.getElementById("btnPrevious").disabled = true;
		}
	}
		
	// Submit form
	if (h<=0 && m<=0 && s<=0)
	{
	    if (document.getElementById("timeOutFlag"))
	    {
		    document.getElementById("timeOutFlag").value = "true";
		    alert("The allocated time for this form has expired. The form will be closed.");
		}
		btnGoToPostBack();
	}
}
function btnGoToPostBack() {
    window.onunload = "";
    __doPostBack('btnGoTo', '');
}
function timeFormat(num)
{
	var str = num.toString(10);
	if (str.length == 1)
		return "0" + str;
	else
		return str;
}
//******************************************************//

//****************** MESSAGE FUNCTION ******************//
// Show feedback
function showPopUpInfor(title, message)
{
	if (title=="")
	{
		title="RealAbility";
	}
	var cssStyleCode = "<style type=text/css>\n.btnCss{width:60px;}\n.MsgText{color:000000;font-size:12px;font-family:Arial;}\n</style>\n";
	var okEvent = " onclick='window.close();'";
	var okButtonCode = "<input type=button value='OK'"+okEvent+" class=btnCss>";
	var jspCode = "";
	var dialogHtmlCode = "javascript:document.write(\"<html>\n<head>\n<title>";
	dialogHtmlCode += title + "</title>\n" + jspCode + "\n" + cssStyleCode + "</head>\n<body>\n";
	dialogHtmlCode += "<table border=0 width=100% height=100% cellpadding=0 cellspacing=0>"
	dialogHtmlCode += "<tr><td align=center class=MsgText>" + message + "</td></tr>";
	dialogHtmlCode += "<tr><td align=center>" + okButtonCode + "</td></tr>";
	dialogHtmlCode += "</table>\n</body>\n</html>\")";
	return window.showModalDialog(dialogHtmlCode,'RealAbility','scroll:0;status:0;help:0;resizable:0;dialogWidth:300px;dialogHeight:150px');
}
//******************************************************//

//******* MODAL POPUP DIALOG OF TAKER INTERFACE ********//

function SetMediaVisibility(isVisible)
{
    var v="hidden";
    if(isVisible) v="visible";
    
    var mediaobj = document.getElementsByTagName("embed");
    if (mediaobj.length > 0)
    {
        for (var i = 0; i < mediaobj.length; i++)
        {
            mediaobj[i].style.visibility = v;
        }
    }
    mediaobj = document.getElementsByTagName("object");
    if (mediaobj.length > 0)
    {
        for (var i = 0; i < mediaobj.length; i++)
        {
            mediaobj[i].style.visibility = v;
        }
    }
}
//Show ModalPopupDialog
function SetTakeRest() {
    if (document.getElementById('rbnTakeARest')) {
        document.getElementById('rbnTakeARest').checked = !checkItemRepliedOnly();
    }
    return true;
}
function ShowModalPopup()
{
    SetMediaVisibility(false);
    
    if(isIE)
    {
        var con = document.getElementById("showConfirm");
        con.style.marginLeft = "0px";
        con.style.left = document.body.clientWidth/2 - 130 + document.body.scrollLeft;
    }
        
    document.getElementById('cover').style.display = 'block';//cover layer
    document.getElementById("showConfirm").style.display = 'block';//above layer
    document.getElementById('btnCancel').focus();
    
    document.getElementById('cover').style.width = document.body.scrollWidth;
    document.getElementById('cover').style.height = document.body.scrollHeight;
    if(document.getElementById('rbnTakeARest'))
    {
        document.getElementById('rbnT').style.display = 'block';
        document.getElementById('rbnF').style.display = 'block';   
    }
    else
    {
        document.getElementById('rbnT').style.display = 'none';
        document.getElementById('rbnF').style.display = 'none'; 
    }
    return false;
}

// Hide ModalPopupDialog
function HideModalPopup()
{
    SetMediaVisibility(true);
    
    document.getElementById("showConfirm").style.display = 'none';
    document.getElementById('cover').style.display = 'none';
    return false;
}

// CheckItemAllowSkip
function IsAllowSkipMP()
{
    if((document.getElementById('rbnFinished') && document.getElementById('rbnFinished').checked) || document.getElementById('rbnFinished') == null)
    {
        if(!checkItemReplied(true))
        {
            if(document.getElementById('btnCancel'))
            {
                document.getElementById('btnCancel').focus();
            }
            return false;
        }
    }
}

function ShowLoading()
{	
    document.getElementById("loading").style.display = 'block';
    document.getElementById('LoadingCover').style.width = document.body.scrollWidth;
    document.getElementById('LoadingCover').style.height = document.body.scrollHeight;
    document.getElementById("LoadingCover").style.display = 'block';          
}

function HideLoading()
{
    document.getElementById("loading").style.display = 'none';
    document.getElementById('LoadingCover').style.width = document.body.scrollWidth;
    document.getElementById('LoadingCover').style.height = document.body.scrollHeight;
    document.getElementById("LoadingCover").style.display = 'none';
}
//******************************************************//

// Save items name
var packageItemName = new Array(1);

function FindItem(name)
{
    for (var i = 0; i < packageItemName.length; i++)
    {
        if (packageItemName[i] == name)
            return false;
        else
            continue;
    }
    return true;
}

function AddItemName(name)
{
    if ( FindItem(name) )
        packageItemName.push(name);
}

function CheckNodesIsAnswer(coll)
{
    for(var i = 0; i < coll.length; i++)
    {
        try {
            if (coll[i].name.indexOf("answer") >= 0)
            {
                AddItemName(coll[i].name);
            }
        }
        catch(ex)
        {
            continue;
        }
    }
}

function ListTheTag()
{
    var inputnodes = document.getElementsByTagName("input");
    var essaynodes = document.getElementsByTagName("textarea");
    
    CheckNodesIsAnswer(inputnodes);
    CheckNodesIsAnswer(essaynodes);
    packageItemName.sort(SortAnswer);
}

function GetAnswerNo(an)
{
    var strArray = an.split("_");
    //if(an.indexOf("Essay") >= 0)
        return parseInt(strArray[2]);
    //else
        //return parseInt(strArray[3]);
}

function SortAnswer(a,b)
{
    return GetAnswerNo(a) - GetAnswerNo(b);
}

// Scroll to the view item
function ScrollToView(index)
{
    var divBody = document.getElementById('divBody');

    if (packageItemName.length > 0)
    {
        var scnode = document.getElementsByName(packageItemName[index-2])[0];
        if (index == 1)
        {
            window.scrollTo(0,0);
        }
        else if(scnode)
        {
            if (index == packageItemName.length)
            {
                scnode.scrollIntoView(true);
            }
            else
            {
                scnode.scrollIntoView(true);
                divBody.scrollTop -= scnode.scrollHeight * 2;
            }
        }
    }
}

