
// ********************************
// Survey Question Template Scripts
// ********************************

function cs() {
	try {	
		disable(elementById("Select_MaintainSurvey"));
		disable(elementById("Button_MaintainSurvey"));
	}
	catch(e) {
		logErr("cs", e)
	}
}

function ci() {
	try {
		hide(elementById("pageBody"));
		show(elementById("pageBodyHidden"));
	}
	catch(e) {
		logErr("cs", e)
	}
}

// Saves the check property in the defaultChecked property to overcome a "bug" in IE6 where
// the checked property is set to the defaultChecked property when the row is swapped
function cc(objItem) {
	try {
		objItem.defaultChecked = objItem.checked;
	}
	catch(e) {
		logErr("cc", e)
	}
}

function cst(objItem) {
	try {
		cs();
		// Show the Questions Columns
		show(elementById("IncSurveyTypeQst"));
		// show(elementById("SurveyTypeQuestion"));
		// if the question is not selected
		var rowItem = getRowNumber(findRowParent(objItem, "st")) + 1;
		var intRows = getTableRowCount(tblQ);
		if (rowItem > 1) {
			while (rowItem < intRows && 
					rowType(tblQ, rowItem) == "q") {
				var nodRow = getRow(tblQ, rowItem)
				var nodCheckbox = findChildIdStartingWith(nodRow, "IQ_") 
				if (isSelected(objItem)) {
					// Show the question rows for this survey type
					// Select the questions for this survey type
					showRow(tblQ, rowItem);
					// selectCheckbox(nodCheckbox);
				}
				else {
					// Unselect the questions for this survey type
					// Hide the questions row for this survey type
					hideRow(tblQ, rowItem);
					unselectCheckbox(nodCheckbox);
				}
				rowItem++;
			}
				
		}
	}
	catch(e) {
		logErr("cst", e)
	}
}

// move the survey type row up one item
function msu(objTable, objList, objItem) {
	try {
		// Find this rows details
		var objRowItem = findRowParent(objItem, "st");
		var intRowItem = getRowNumber(objRowItem);
		
		// Find the previous survey type row
		var intRowTarget = getPreviousRowNumber(objTable, intRowItem-1, "st");
		
		// If we aren't the first row in the table
		if (intRowTarget > 0) {
		
			// Find the last question row of this survey type
			var intEndRow = getLastRowNumberOfType(objTable, intRowItem+1, "q");
			
			// Update the order of the survey types
			var intFirst = getTypeSequence(objTable,  "st", 0, intRowItem)-1;
			var intSecond = getTypeSequence(objTable,  "st", 0, intRowTarget)-1;
			swapSortOrder(objList, intFirst, intSecond)

			// Move this row and all the child rows before the previous row
			var i;
			for (i=intRowItem; i <= intEndRow; i++)
				moveRowBefore(objTable, intRowTarget++, i);
		}
	}
	catch(e) {
		logErr("msu", e)
	}
}

// move the survey type down one row
function msd(objTable, objList, objItem) {
	try {
		// Find this rows details
		var objRowItem = findRowParent(objItem, "st");
		var intRowItem = getRowNumber(objRowItem);
		
		// Find the next survey type row
		var intRowTarget = getNextRowNumber(objTable, intRowItem+1, "st");
		
		// If we aren't the first row in the table
		if (intRowTarget > 0) {
		
			// Find the last question row of the target survey type
			var intEndRow = getLastRowNumberOfType(objTable, intRowTarget+1, "q");
			
			// Update the order of the survey types
			var intFirst = getTypeSequence(objTable,  "st", 0, intRowItem)-1;
			var intSecond = getTypeSequence(objTable,  "st", 0, intRowTarget)-1;
			swapSortOrder(objList, intFirst, intSecond)

			// Move the target row and all the child rows before this row
			var i;
			for (i=intRowTarget; i <= intEndRow; i++)
				moveRowBefore(objTable, intRowItem++, i);
		}
	}
	catch(e) {
		logErr("msd", e)
	}
}

// move the question row up one item
function mqu(objTable, objList, objItem) {
	try {
		// Find this rows details
		var objRowItem = findRowParent(objItem, "q");
		var intRowItem = getRowNumber(objRowItem);
		
		// If we aren't the first row in this survey type
		if (rowType(objTable, intRowItem - 1) == "q") {
		
			// Find the first row in this survey type
			var intFirstRow = getFirstRowNumberOfType(objTable, intRowItem, "q");
		
			// Update the order of the survey types
			var intFirst = intRowItem - intFirstRow - 1;
			var intSecond = intFirst - 1;
			swapSortOrder(objList, intFirst, intSecond)

			// Move this row before the previous row
			moveRowBefore(objTable, intRowItem, intRowItem - 1);
		}
	}
	catch(e) {
		logErr("mqu", e)
	}
}

// move the question row down one item
function mqd(objTable, objList, objItem) {
	try {
		// Find this rows details
		var objRowItem = findRowParent(objItem, "q");
		var intRowItem = getRowNumber(objRowItem);
		
		// Find the last row in this survey type
		var intLastRow = getLastRowNumberOfType(objTable, intRowItem, "q");

		// If we aren't the first row in this survey type
		if (intRowItem != intLastRow) {
		
			// Find the first row in this survey type
			var intFirstRow = getFirstRowNumberOfType(objTable, intRowItem, "q");

			// Update the order of the survey types
			var intFirst = intRowItem - intFirstRow - 1;
			var intSecond = intFirst + 1;
			swapSortOrder(objList, intFirst, intSecond)

			// Move this row after the previous row
			moveRowBefore(objTable, intRowItem, intRowItem + 1);
		}
	}
	catch(e) {
		logErr("mqd", e)
	}
}
