// --------------------------------------------------------------------------------
// ghpat_Application.js
// Guozhi Ma 23-Nov-2006
// Contains Javascript relevant to the Assessment Tool.
// --------------------------------------------------------------------------------

var fieldPrefix = "tmpl_appMain_assessmentapp_"

// cancel flag
var ghpat_cancelled = false;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
function() {

	var ghpat_btnNextAB = document.getElementById(fieldPrefix + "ghpat_btnNextAB");
	if (ghpat_btnNextAB != null)
		ow_f_AddEvent(ghpat_btnNextAB, "click", at_nextSectionAClicked, false);
		
	var ghpat_btnNextBC = document.getElementById(fieldPrefix + "ghpat_btnNextBC");
	if (ghpat_btnNextBC != null)
		ow_f_AddEvent(ghpat_btnNextBC, "click", at_nextSectionBClicked, false);
		
	var ghpat_btnNextCD = document.getElementById(fieldPrefix + "ghpat_btnNextCD");
	if (ghpat_btnNextCD != null)
		ow_f_AddEvent(ghpat_btnNextCD, "click", at_nextSectionCClicked, false);
						
	var ghpat_btnFinish = document.getElementById(fieldPrefix + "ghpat_btnFinish");
	if (ghpat_btnFinish != null)
		ow_f_AddEvent(ghpat_btnFinish, "click", at_formSubmitClicked, false);
    
	if (document.getElementById("ghpat_form") != null) {
		var inp = document.getElementById("ghpat_form").getElementsByTagName("input");
		for (var i = 0; i < inp.length; i++) {
			if (inp[i].type == "text") 
				ow_f_AddEvent(inp[i], "keypress", at_formTextSubmit, false);
		}
	}    	
}
);

// --------------------------------------------------------------------------------
// at_formTextSubmit()
// Fires when a textfield had focus and ENTER was pressed.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function at_formTextSubmit(e) {

	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code == 13) {
		document.getElementById(fieldPrefix + "ghpat_btnFinish").click();
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	}
}

// --------------------------------------------------------------------------------
// at_nextSectionAClicked()
// Fires when the 'Next Section' button on section A was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function at_nextSectionAClicked(e) {

	if (!at_checkSectionARequestData()) {
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return;
	}
}

// --------------------------------------------------------------------------------
// at_nextSectionBClicked()
// Fires when the 'Next Section' button on section B was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function at_nextSectionBClicked(e) {

	if (!at_checkSectionBRequestData()) {
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return;
	}
}
// --------------------------------------------------------------------------------
// at_nextSectionCClicked()
// Fires when the 'Next Section' button on section C was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function at_nextSectionCClicked(e) {

	if (!at_checkSectionCRequestData()) {
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return;
	}
}
// --------------------------------------------------------------------------------
// at_formSubmitClicked()
// Fires when the 'View Results' button was clicked.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- Nothing
// --------------------------------------------------------------------------------
function at_formSubmitClicked(e) {

	if (!at_checkSectionDRequestData()) {
		if (!e) var e = window.event;
		if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
	} else {
		return;
	}
}

function at_validateListBox(e,err)
{
	var e = document.getElementById(fieldPrefix + e);	
	if (e !=null) {	
		if (e.selectedIndex <= 0){
			alert(document.getElementById(fieldPrefix + err).value);
			e.focus();
			return false;
		}		
	}
	return true;	
}
// --------------------------------------------------------------------------------
// at_checkSectionARequestData()
// Validates the data entered on the section A.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------

function at_checkSectionARequestData()
{
	// validate sectioin A
	if (at_validateListBox("ghpat_lstQuestionCat1_1a","rfvQuestionA1") == false)
		return false;
		
	if (at_validateListBox("ghpat_lstQuestionCat1_1b","rfvQuestionA2") == false)
		return false;		
	
	if (at_validateListBox("ghpat_lstQuestionCat1_1c","rfvQuestionA3") == false)
		return false;	
			
	if (at_validateListBox("ghpat_lstQuestionCat1_1d","rfvQuestionA4") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat1_2a","rfvQuestionA5") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat1_2b","rfvQuestionA6") == false)
		return false;		

	if (at_validateListBox("ghpat_lstQuestionCat1_2c","rfvQuestionA7") == false)
		return false;	
						
	return true;	
}
	
// --------------------------------------------------------------------------------
// at_checkSectionBRequestData()
// Validates the data entered on the section B.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------

function at_checkSectionBRequestData()
{
	// validate sectioin B
	if (at_validateListBox("ghpat_lstQuestionCat2_1a","rfvQuestionB1") == false)
		return false;
		
	if (at_validateListBox("ghpat_lstQuestionCat2_1b","rfvQuestionB2") == false)
		return false;		
	
	if (at_validateListBox("ghpat_lstQuestionCat2_2a","rfvQuestionB3") == false)
		return false;	
			
	if (at_validateListBox("ghpat_lstQuestionCat2_2b","rfvQuestionB4") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat2_2c","rfvQuestionB5") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat2_3a","rfvQuestionB6") == false)
		return false;		

	if (at_validateListBox("ghpat_lstQuestionCat2_3b","rfvQuestionB7") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat2_3c","rfvQuestionB8") == false)
		return false;		
												
	return true;	
}

// --------------------------------------------------------------------------------
// at_checkSectionCRequestData()
// Validates the data entered on the section C.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------

function at_checkSectionCRequestData()
{
	// validate sectioin C
	if (at_validateListBox("ghpat_lstQuestionCat3_1a","rfvQuestionC1") == false)
		return false;
		
	if (at_validateListBox("ghpat_lstQuestionCat3_1b","rfvQuestionC2") == false)
		return false;		
	
	if (at_validateListBox("ghpat_lstQuestionCat3_1c","rfvQuestionC3") == false)
		return false;	
			
	if (at_validateListBox("ghpat_lstQuestionCat3_2a","rfvQuestionC4") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat3_2b","rfvQuestionC5") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat3_2c","rfvQuestionC6") == false)
		return false;		

	if (at_validateListBox("ghpat_lstQuestionCat3_3a","rfvQuestionC7") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat3_3b","rfvQuestionC8") == false)
		return false;		
		
	if (at_validateListBox("ghpat_lstQuestionCat3_3c","rfvQuestionC9") == false)
		return false;	
														
	return true;	
}

// --------------------------------------------------------------------------------
// at_checkSectionDRequestData()
// Validates the data entered on the section D.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------

function at_checkSectionDRequestData()
{
	// validate sectioin D
	if (at_validateListBox("ghpat_lstQuestionCat4_1a","rfvQuestionD1") == false)
		return false;
		
	if (at_validateListBox("ghpat_lstQuestionCat4_1b","rfvQuestionD2") == false)
		return false;		
	
	if (at_validateListBox("ghpat_lstQuestionCat4_1c","rfvQuestionD3") == false)
		return false;	
			
	if (at_validateListBox("ghpat_lstQuestionCat4_2a","rfvQuestionD4") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat4_2b","rfvQuestionD5") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat4_2c","rfvQuestionD6") == false)
		return false;		

	if (at_validateListBox("ghpat_lstQuestionCat4_3a","rfvQuestionD7") == false)
		return false;	
		
	if (at_validateListBox("ghpat_lstQuestionCat4_3b","rfvQuestionD8") == false)
		return false;		
		
	if (at_validateListBox("ghpat_lstQuestionCat4_3c","rfvQuestionD9") == false)
		return false;	
														
	return true;	
}
// --------------------------------------------------------------------------------
// Togglerow
// changes the plus and minus for the clicked row and displays/hides the questions
// --------------------------------------------------------------------------------
// Arguments:
//	- Section header (3 characters) for e.g. Human Resources Planning will be HRP
// Returns:
//	- Controls the results table display
// --------------------------------------------------------------------------------

	function togglerow(CurObj,arrQuest)
	{
		//CurObj will be 'this' object and arrQuest is an array with total questions for each sub-section
		var ObjCurrentTR = CurObj.parentNode.parentNode;
		var ArrRows = arrQuest.split(",");

		//image names
		var strPlusimg='Plusbox.gif';
		var strMinusimg='Minubox.gif';
		
		//image source
		var strPlussrc='/site-ghp2/applications/AssessmentTool/en/' + strPlusimg;
		var strMinussrc='/site-ghp2/applications/AssessmentTool/en/' + strMinusimg; 
		
		//Parse the Filename
		var intCount = CurObj.src.split("/").length-1;
		var strFileName= CurObj.src.split("/")[intCount];
		
		//Check for the Filename
		if(strFileName.toUpperCase() == strPlusimg.toUpperCase())
		{
			//Change the img source to minus
			CurObj.src=strMinussrc;
		
			//Start the loop to display the questions for each sub-section
			for(var i=0;i<=ArrRows.length - 1;i++)
			{
				//Skip the sub-category row i.e. Succession Corporate Planning, Training etc
				ObjCurrentTR = ObjCurrentTR.nextSibling;
				
				//Display the questions
				for(var j=0;j<=ArrRows[i]-1;j++)
				{
					ObjCurrentTR = ObjCurrentTR.nextSibling;
					ObjCurrentTR.style.display='';
				}
			}
		}
		else if (strFileName.toUpperCase() == strMinusimg.toUpperCase())
		{
			//Change the img source to plus
			CurObj.src=strPlussrc;

			//Start the loop to hide the questions for each sub-section
			for(var i=0;i<=ArrRows.length - 1;i++)
			{
				//Skip the sub-category row i.e. Succession Corporate Planning, Training etc
				ObjCurrentTR = ObjCurrentTR.nextSibling;
				
				//Hide the questions
				for(var j=0;j<=ArrRows[i]-1;j++)
				{
					ObjCurrentTR = ObjCurrentTR.nextSibling;
					ObjCurrentTR.style.display='none';
				}
			}			
		}
	}
