/*
 *	Main Function :		get the name of user's browser
 *  Parameters    :		none
 *  Return Value  :		String	:	name of the browser
 */
function getBrowserName() {		
	var sBrowser
	var sAgentInfo = navigator.userAgent.toLowerCase();
		
	if (sAgentInfo.indexOf("firefox") >= 0) {
		sBrowser = "firefox";
	} 
	else if (sAgentInfo.indexOf("opera") >= 0) {
		sBrowser = "opera";
	}
	else if (sAgentInfo.indexOf("msie") >= 0) {
		sBrowser = "ie/netscape";
	} 
	else if (sAgentInfo.indexOf("gecko") >= 0) {
		// For Netscape8.0 and later [Display Like Firefox]
		sBrowser = "firefox";
	}
	
	return sBrowser;
}

function submitClicked(id) {
    var strPrefix = id.replace("submit", "");
    var intQuestionTotal = comParseInt(document.getElementById(strPrefix+"questionstotal").value);
    var arrControls = null;
    var intValSum = 0;
    var intSelectionCnt = 0;
    var iLoop = 0;
    var jLoop = 0;
    
    for (jLoop=1; jLoop<=intQuestionTotal; jLoop++) {
		arrControls = document.getElementsByName(strPrefix+"range"+jLoop);		
		for (iLoop=0; iLoop<arrControls.length; iLoop++) {
			if (arrControls[iLoop].type=="radio" && arrControls[iLoop].checked) {
				intValSum += comParseInt(arrControls[iLoop].value);
				intSelectionCnt++;
			}
		}
    }
    if (intSelectionCnt < intQuestionTotal) {
        alert(document.getElementById(strPrefix + "error01").value);
    }
    else{
        var arrRangefr = document.getElementById(strPrefix+"hidrangefr").value.split("||");
        var arrRangeto = document.getElementById(strPrefix+"hidrangeto").value.split("||");
        var arrResults = document.getElementById(strPrefix+"hidresults").value.split("||");
        var arrHeadings = document.getElementById(strPrefix+"hidheadings").value.split("||");
        var arrLinksheadGroup = document.getElementById(strPrefix+"hidlinkshead").value.split("||");
        var arrLinksurlGroup = document.getElementById(strPrefix+"hidlinksurl").value.split("||");
        var arrLinksaltGroup = document.getElementById(strPrefix+"hidlinksalt").value.split("||");
        var arrLinksabstractGroup = document.getElementById(strPrefix+"hidlinksabstract").value.split("||");
        
        for (iLoop=0; iLoop<arrResults.length; iLoop++) {
            if (intValSum>=arrRangefr[iLoop] && intValSum<=arrRangeto[iLoop]) {
                document.getElementById(strPrefix+"resultcontent").innerHTML = arrResults[iLoop];
                
                // Build links
                var arrLinkhead = arrLinksheadGroup[iLoop].split("::");
                var arrLinkurl = arrLinksurlGroup[iLoop].split("::");
                var arrLinkalt = arrLinksaltGroup[iLoop].split("::");
                var arrLinkabstract = arrLinksabstractGroup[iLoop].split("::");
                var strLinks = "";
                
                if (arrHeadings[iLoop]!="") {
					strLinks += "<div class='quiz_heading' id='" + strPrefix + "heading'>";
					strLinks += arrHeadings[iLoop];
					strLinks += "</div>";
                }
                
                for (jLoop=0; jLoop<arrLinkhead.length; jLoop++) {
					strLinks += "<div class='quiz_linkhead' id='" + strPrefix + "linkhead'>";
					if (arrLinkalt[jLoop]!="") {
						strLinks += "<a href='" + arrLinkurl[jLoop] + "' target='_blank' title='" + arrLinkalt[jLoop] + "'>" + arrLinkhead[jLoop] + "</a>";
					}
					else {
						strLinks += "<a href='" + arrLinkurl[jLoop] + "' target='_blank'>" + arrLinkhead[jLoop] + "</a>";
					}		
					strLinks += "</div>";
					strLinks += "<div class='quiz_linkabstract' id='" + strPrefix + "linkabstract'>";
					strLinks += arrLinkabstract[jLoop];
					strLinks += "</div>";
				}
                document.getElementById(strPrefix+"links").innerHTML = strLinks;
                
                document.getElementById(strPrefix+"hide").style.display = "block";  
                document.getElementById(strPrefix+"clear").className = document.getElementById(strPrefix+"clear").className.replace("quiz_clear", "");
				document.getElementById(strPrefix + "instruction").style.display = "block";
                break;
            }
        }
    }
}

function clearClicked(id) {
    var strPrefix = id.replace("clear", "");
    var intQuestionTotal = comParseInt(document.getElementById(strPrefix+"questionstotal").value);
    var arrControls = null;
    var iLoop = 0;
    var jLoop = 0;
    
    if (document.getElementById(id).className.indexOf("quiz_clear")<0) {
		for (jLoop=1; jLoop<=intQuestionTotal; jLoop++) {
			arrControls = document.getElementsByName(strPrefix+"range"+jLoop);		
			for (iLoop=0; iLoop<arrControls.length; iLoop++) {
				if (arrControls[iLoop].type=="radio" && arrControls[iLoop].checked) {
					arrControls[iLoop].checked = false;
				}
			}
		}
        document.getElementById(strPrefix+"hide").style.display = "none"; 
        if (document.getElementById(id).className=="") {
			document.getElementById(id).className = "quiz_clear";
			document.getElementById(strPrefix + "instruction").style.display = "none";
			
        }
        else {
			document.getElementById(id).className += " quiz_clear";
			document.getElementById(strPrefix + "instruction").style.display = "none";
        }
    }
}

function quiz_initial(prefix) {
	var iLoop = 0;
	var jLoop = 0;
    var arrControls = null;
    var intQuestionTotal = comParseInt(document.getElementById(prefix+"questionstotal").value); 
    var objQuestion = null;   
    var objAnswer = null;   
    var objNo = null;      
    var arrDivControls = new Array();
    var intValidHeight = 0;
    var intOldPaddingTop = 0;
  
    for (iLoop=1; iLoop<=intQuestionTotal; iLoop++) {
		objQuestion = document.getElementById(prefix + "question" + iLoop);
		objAnswer = document.getElementById(prefix + "answer" + iLoop);
		objNo = document.getElementById(prefix + "no" + iLoop);
		arrDivControls[jLoop] = document.getElementById(prefix + "answerbt" + iLoop);
	
		if (objQuestion.clientHeight > 30) {
			objQuestion.style.height = comParseInt(objQuestion.clientHeight);
		}
	
		if (objAnswer.clientHeight > 30) {
			objAnswer.style.height = comParseInt(objAnswer.clientHeight);
		}
	
		if (objQuestion.clientHeight<objAnswer.clientHeight) {
			intValidHeight = comParseInt(objAnswer.clientHeight);
			//objQuestion.style.height = intValidHeight + "px";	
			
			intOldPaddingTop = comParseInt(objQuestion.style.paddingTop);
			objQuestion.style.paddingTop = Math.floor((intValidHeight - comParseInt(objQuestion.clientHeight))/2) + "px";
			objQuestion.style.height = intValidHeight - comParseInt(objQuestion.style.paddingTop) + intOldPaddingTop + "px";
		}
		else {	
			intValidHeight = comParseInt(objQuestion.clientHeight);
			objAnswer.style.height = intValidHeight + "px";
		}
		
		intOldPaddingTop = comParseInt(objNo.style.paddingTop);
		objNo.style.paddingTop = Math.floor((intValidHeight - comParseInt(objNo.clientHeight))/2) + "px";
		objNo.style.height = intValidHeight - comParseInt(objNo.style.paddingTop) + intOldPaddingTop + "px";
		
		// Set padding for radio buttons
		arrDivControls = objAnswer.getElementsByTagName("Div");
		for (jLoop=0; jLoop<arrDivControls.length; jLoop++) {	
			arrDivControls[jLoop].style.paddingTop = Math.ceil((intValidHeight - comParseInt(arrDivControls[jLoop].clientHeight))/2) + "px";
		}
    }
	
	if (getBrowserName() == "opera") {
		arrControls = document.getElementsByTagName("input");		
		for (iLoop=0; iLoop<arrControls.length; iLoop++) {
			if (arrControls[iLoop].type=="radio" && arrControls[iLoop].name.indexOf(prefix)==0) {
				arrControls[iLoop].onclick = function(e) {
					var jLoop = 0;
					var arrCtrls = document.getElementsByName(this.name);					
					for (jLoop = 0; jLoop<arrCtrls.length; jLoop++) {
						if (arrCtrls[jLoop].value != this.value) {
							arrCtrls[jLoop].checked = false;
						}
					}
				}
			}
		}
	}
	
	
}

function comParseInt(str) {
	if (parseInt(str).toString()=="NaN") {
		return 0;
	}
	else {
		return parseInt(str);
	}
}