// JavaScript Document


function findObj(theObj, theDoc){
  var p, i, foundObj;
  if(!theDoc) 
  	  theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
	  theDoc = parent.frames[theObj.substring(p+1)].document;
	  theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) 
	  foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++)
	  foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
	  foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) 
	  foundObj = document.getElementById(theObj);
  return foundObj;
}

function toggleFields(obj, fields){

	//Is the checkboxed checked or not?
	var retVal = (!obj.checked)? true : false;
	var i,j,curObj;
	var fieldList = fields
    var sepFieldList = fieldList.split(",");
	var fieldCount = sepFieldList.length;

	//Create a list of common fields minus their cst and cstShp prefixes respectively
	var arrCommonFields = sepFieldList;
	var arrCommonFields_length = arrCommonFields.length;

	//Create a list of special fields that have need other work done before they can have a value set or be enabled/disabled
	var arrSpecialFields = sepFieldList;

	//An array of all fields, both common and special for the purpose of enabling/disabling the fields
	var arrAllFields = arrCommonFields.concat(arrSpecialFields);
	var arrAllFields_length = arrAllFields.length;

	//Loop through all fields and disable them if the box is checked, otherwise enable the fields.
	for(j=0; j<arrAllFields_length;j++){
		curObj = findObj(arrAllFields[j]);
		if(curObj){			
			if(retVal)
			{
			  curObj.setAttribute("disabled","disabled");
			} else {
			  curObj.removeAttribute("disabled");
			}
		}
	}
    return retVal;
  
}


// Elizabeth Hicks 01-23-09 

// conditionField = 'multiday', conditionValue = 'on' 			- toggle related fields 'endmm,enddd,endyy'
// conditionField = 'recurrences', conditionValue = 'on'		- toggle related fields 'recurr,instances,endOnmm,endOndd,endOnyy'

// condition 1 ('multiday','on','endmm,enddd,endyy')
// condition 2 ('recurrences','on','recurr,instances,endOnmm,endOndd,endOnyy')


// This function checks if the conditionalValue for conditionField is me
// if so it then toggles the relatedFields
function conditionalToggleFields(obj, conditionField, conditionValue, relatedFields) {
	
	var i,j,curObj;
    
	// alert("If " + conditionField + " is turned " + conditionValue + " toggle these fields: " + relatedFields);

	var conditionMet;
	if(conditionValue == "off")
	{
	  var cndValue = false;
	} else if(conditionValue == "on") {
	  var cndValue = true;
	}
	
	// check if condition met
	var cndMet = checkConditionMet(conditionField, conditionValue);
	// alert(cndMet);
	
	// if condition met then toggle the related fields
	// if(cndMet)
	// {
		// alert("Toggle fields: " + relatedFields);
	// }
	// toggleFields(conditionField, relatedFields);
	
	//Is the toggling checkbox checked or not?
	var objChckdValue = (obj.checked) ? true : false;
	// alert(obj + " " + objChckdValue);

	//Is the conditional checkbox checked or not?
	cndObj = findObj(conditionField);
	var cndChckdValue = (cndObj.checked) ? true : false;
	// alert(conditionField + " " + cndChckdValue);

	var sepFieldList = relatedFields.split(",");
	var fieldCount = sepFieldList.length;
	
	//Loop through all fields and disable them if the box is checked, otherwise enable the fields.
	for(j=0; j<fieldCount;j++)
	{
		// alert(sepFieldList[j]);
		curObj = findObj(sepFieldList[j]);
		if(curObj)
		{			
		    // alert(curObj.name + ": " + cndMet + " obj checked: " + objChckdValue + " cnd field checked: " + cndChckdValue);
			if(!objChckdValue)
			{
			    // alert("Obj Field not checked, leave related fields disabled");
			    // if obj field is not checked leave the related fields disabled
				curObj.setAttribute("disabled","disabled");
			} else {
				if(cndMet) 
				{
				  // alert("Obj field checked AND condition met, enable related fields");
			  	  curObj.removeAttribute("disabled");
				} else {
				  // alert("Obj field checked AND condition NOT met, leave related fields disabled");
				  curObj.setAttribute("disabled","disabled");
				}
			}
		}
	}

}


// This function gets the check value of a field - conditionField 
// and compares that to the passed conditionValue of 'on' (true) or 'off' (false)
// to determine if the condition to toggle the conditional fields was met
function checkConditionMet(conditionField, conditionValue) {

	var conditionMet;
	if(conditionValue == "off")
	{
	  var cndValue = false;
	} else if(conditionValue == "on") {
	  var cndValue = true;
	}
	
	//Is the checkboxed checked or not?
	cndObj = findObj(conditionField);
	var checkedValue = (cndObj.checked) ? true : false;
	
	if(checkedValue == cndValue)
	{
	  var conditionMet = true;
	} else {
	  var conditionMet = false;
	}
	
	// alert(checkedValue + " " + cndValue + " = " + conditionMet);
  	return conditionMet;
	
}



