//**********************************************************************************//
//* DOM Helper Methods											
//* By Darryn van der Walt (www.ventureflow.co.za)																        
//* Last Updated : 25 April 2006        
//**********************************************************************************//

//**********************************************************************************//
// Retrieve a list of checkboxes based on the group name obtained from the controlid
//**********************************************************************************//
function retrieveCheckedValuesList(thisControlID) {
	thisControlsGroupName = determineCheckBoxGroupName(thisControlID);
	returnString = "";
	checkCount = document.getElementsByTagName("input");
		
    for (i=0; i<checkCount.length; i++) {
          if(checkCount[i].type == "checkbox") {
				if(thisControlsGroupName == determineCheckBoxGroupName(checkCount[i].id) && checkCount[i].checked) {
					returnString = returnString + checkCount[i].value + ";";
				}
          }
    }
    return returnString;
}

//**********************************************************************************//
// Remove all occurrences of a token in a string. s  string to be processed. t  token to be removed
//**********************************************************************************//
function remove(s, t) {
  i = s.indexOf(t);
  r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + remove(s.substring(i + t.length), t);
  return r;
}

//**********************************************************************************//
// Determing Checkbox Group Name from passed parameter
//**********************************************************************************//
function determineCheckBoxGroupName(thisControlID) {
	groupIdent   = "XxX";
	pos          = thisControlID.indexOf(groupIdent);
	groupName    = thisControlID.substring(pos+groupIdent.length,thisControlID.length);
	return groupName;
}

//**********************************************************************************//
// Check radio button when textbox receives focus
//**********************************************************************************//
function AJAX_CheckRadioButtonOnTextboxFocus(radioButtonID) {
	if(DOMGetElement(radioButtonID) != null) {
		radioButtonToCheck = DOMGetElement(radioButtonID);
		radioButtonToCheck.checked = true;
	}
}

//**********************************************************************************//
// Function to hide/show password and password confirm textboxes
//**********************************************************************************//
function AJAX_toggleRegistrationPasswordFields(thisControl) {
	if(thisControl.checked) { 
		if(document.getElementById("divPassword")) { 
			document.getElementById("divPassword").style.display = ""; 
		}
		if(document.getElementById("divPasswordConfirm")) { 
			document.getElementById("divPasswordConfirm").style.display = ""; 
		}
	} else {
		if(document.getElementById("divPassword")) { 
			document.getElementById("divPassword").style.display = "none"; 
		}
		if(document.getElementById("divPasswordConfirm")) { 
			document.getElementById("divPasswordConfirm").style.display = "none"; 
		}
	}
}

//**********************************************************************************//
// Function to clear a specified control's value
//**********************************************************************************//
function AJAX_ClearTextBox(controlIDToClear) {
	if(DOMGetElement(controlIDToClear) != null) {
		control = DOMGetElement(controlIDToClear);
		control.value = "";
	}
}

//**********************************************************************************//
// Function to clear a specified control's value
//**********************************************************************************//
function AJAX_EnableTextBoxOnChange(controlIDToEnable) {
	if(DOMGetElement(controlIDToEnable) != null) {
		control = DOMGetElement(controlIDToEnable);
		control.disabled = false;
		control.focus();
	}
}

//**********************************************************************************//
// Function to disable a specified control
//**********************************************************************************//
function AJAX_DisableTextBoxOnChange(controlIDToDisable) {
	if(DOMGetElement(controlIDToDisable) != null) {
		control = DOMGetElement(controlIDToDisable);
		control.disabled = true;
	}
}

//**********************************************************************************//
// Function to enable/disable a specified control when a certain value is encountered
//**********************************************************************************//
function AJAX_ToggleEnableDisableTextBox(controlInCharge, controlIDToToggle, valueForDisable) {
	if(document.getElementById(controlIDToToggle)) {
		valueOfControl = GetValueOfControl(controlInCharge, 'true') + '';
		if(valueOfControl == valueForDisable) {
			document.getElementById(controlIDToToggle).disabled = true;
		} else {
			document.getElementById(controlIDToToggle).disabled = false;		
		}
	}
}

//**********************************************************************************//
// Function to attach events in a browser safe manner
//**********************************************************************************//
function addEvent(obj, evType, fn) {  
	if (obj.addEventListener) { 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} 
	else if (obj.attachEvent) { 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} 
	else { 
		return false; 
	} 
}

//**********************************************************************************//
// helper method that retrieves a specified object
//**********************************************************************************//
function DOMGetElement(o) {
  if (document.getElementById)
	return document.getElementById(o);
  else if (document.all)
	return document.all[o];
  else if (document.layers)
	return document.layers[o];
  return null;
}