
/* ------------------------------------------------------------------
Functions for rule handling
 ------------------------------------------------------------------ */

/* Sets up rule event handlers and also runs rules for the first time */
var rulesForm;
var rulesRunCount = 0;
var rulesLastPost = new String();

function setRulesForm(subForm)
{
	rulesForm = subForm;
}

function rulesAttachElements(subForm, fieldNames)
{
	
	var strFields = new String(';' + fieldNames + ';');
	
	rulesForm = subForm;
	for(var i = 0; i < subForm.length; i++)
	{
		if(strFields.indexOf(';' + subForm.elements[i].name + ';') != -1)
		{
			
			if(window.addEventListener) { // Mozilla, Netscape, Firefox
				
				if(subForm.elements[i].tagName.toLowerCase() == 'select')
				{
					subForm.elements[i].addEventListener('change', rulesRunRules, false);
				}
				else
				{
					subForm.elements[i].addEventListener('click', rulesRunRules, false);
					subForm.elements[i].addEventListener('keyup', rulesRunRules, false);
				}
				
				subForm.elements[i].addEventListener('blur', rulesRunRules, false);
			
			
			} else { // IE
				subForm.elements[i].attachEvent('onclick', rulesRunRules);
				subForm.elements[i].attachEvent('onblur', rulesRunRules);
				subForm.elements[i].attachEvent('onchange', rulesRunRules);
				subForm.elements[i].attachEvent('onkeyup', rulesRunRules);
			}
		}
	}
}

var rulesRunningRules = false;
var rulesTheSource = '';
var rulesMessages = '';

function rulesRunRules(event)
{

	var theevent = window.event || event
	var targ;
	
	if(theevent)
	{
	
		if (theevent.target)
		  {
		  targ=theevent.target;
		  }
		else if (theevent.srcElement)
		  {
		  targ=theevent.srcElement;
		  }
		if (targ.nodeType==3) // defeat Safari bug
		  {
		  targ = targ.parentNode;
		  }
	}
	
	

	if(theevent)
	{
		
		rulesTheSource = targ.name
		
	}
	
	var strPost = new String('');
	var subForm = rulesForm
	
	var addElem;

	
	for(var i = 0; i < subForm.length; i++)
	{
		
		if(subForm.elements[i].name != '__VIEWSTATE')
		{
			addElem = true;
			if(subForm.elements[i].tagName.toLowerCase() == 'input')
			{
				if(subForm.elements[i].type.toLowerCase() == 'checkbox' || subForm.elements[i].type.toLowerCase() == 'radio')
				{
					
					if(!subForm.elements[i].checked)
					{
						addElem = false;
						
					}
				}
			}
			if (subForm.elements[i].tagName.toLowerCase() == 'textarea') {
				if (CKEDITOR.instances[subForm.elements[i].id]) {
					CKEDITOR.instances[subForm.elements[i].id].updateElement()
					// var s = new String(subForm.elements[i].value);
					// alert(s.length);
				}
			}
			if(addElem == true)
			{
				strPost += '&' + subForm.elements[i].name + '=' + encodeURIComponent(subForm.elements[i].value)
			
			}
		}
		
	}
	
	if(strPost != rulesLastPost)
	{
		var url = "runrules.aspx?" + window.location.search.substring(1);
		
		xmlHttpRules = GetXMLHttpObject(rulesRunReturnedActions);
		xmlHttpRules.open("POST", url, true);
		
		xmlHttpRules.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

		var strURL = new String(document.location.href).split("?")[0].toLowerCase()
		var ruleaction
		if (strURL.indexOf("view.aspx", 0) != -1) ruleaction = "view"
		if (strURL.indexOf("edit.aspx", 0) != -1) ruleaction = "edit"
		if (strURL.indexOf("add.aspx", 0) != -1) ruleaction = "add"
		
		xmlHttpRules.send("action=post&rulesaction=" + ruleaction + strPost);
		
		rulesLastPost = strPost;
	}
	
	
}

function rulesRunReturnedActions()
{
	
	if (xmlHttpRules.readyState == 4 || xmlHttpRules.readyState == "complete")
	{
		if(rulesRunningRules == false)
		{
			rulesRunningRules = true;
			// alert(xmlHttpRules.responseText);
			eval(xmlHttpRules.responseText);
			rulesRunCount += 1
			// window.document.title = rulesRunCount; 
			setTimeout("rulesMessages = ''", 5000);
			
			rulesRunningRules = false;
			
			rulesTheSource = '';
			var theRows;
			var theDiv;
			var boolHidden;
			var boolVis;
			var aLink;
			var theTD;
			var theTDleft;
			var theTDright;
			var tabCount = 0;
			var tabHiddenCount = 0;
			for(var i = 0; i < 100; i++)
			{
				if(window.document.getElementById('h' + i))
				{
					theDiv = window.document.getElementById('h' + i);
					
					theRows = theDiv.getElementsByTagName('tr');
					if(theRows)
					{
						boolHidden = false;
						boolVis = false;
						for(var e = 0; e < theRows.length; e++)
						{
							// If the row is IDed as a structure row, and it isn't a subrecord row, then evaluate.
							if(theRows[e].id.indexOf('row_') != -1 && theRows[e].id.split("_").length != 5)
							{
								if(theRows[e].style.display == 'none') boolHidden = true;
								if(theRows[e].style.display != 'none') boolVis = true;
								
							}
	
						}
						
						
						theTD = window.document.getElementById('tab' + i);
						if(theTD)
						{
							tabCount += 1;
							while(theTD.tagName.toLowerCase() != 'div')
							{
								theTD = theTD.parentNode;
							}
							
							if(boolVis == false && boolHidden == true)
							{
							
								theTD.style.display = 'none';
								tabHiddenCount += 1; 
								
							}
							else
							{
								theTD.style.display = '';
								
							}
						}
						
						
						
						
					}
					
					
				}
			}
			
			// Hide the "Save"
			if(document.getElementsByName("submit").length > 0)
			{
				if(tabCount > 0 && tabCount == tabHiddenCount)
				{
					document.getElementsByName("submit")[0].style.display = 'none';
				}
				else
				{
					document.getElementsByName("submit")[0].style.display = '';
				}
			}
			
			if(document.dataform.style.visibility == 'hidden') document.dataform.style.visibility = 'visible';
			
			
		}
	}
	
		
}



function hideStructureRows(structureId)
{

	var theRow = document.getElementById('row_' + structureId)
	
	if(theRow)
	{
        
		theRow.style.display = 'none';
		theRow = theRow.nextSibling;
		while(theRow) {
		    if (theRow.nodeType == 1) {
		        if (theRow.id == 'row_' + structureId || theRow.id == '') {
				
				theRow.style.display = 'none';
				
		        }
		        else {

		            break;
		        }

		    }
		    theRow = theRow.nextSibling;
		}
	}
	
}



function showStructureRows(structureId)
{

	var theRow = document.getElementById('row_' + structureId)
	if(theRow)
	{
		theRow.style.display = '';

		theRow = theRow.nextSibling;
		
		while(theRow)
		{
			if (theRow.nodeType == 1)
			{
				if (theRow.id == 'row_' + structureId || theRow.id == '')
				{
					
					theRow.style.display = '';
					
				}
				else
				{
			            break;
				}
			}
			theRow = theRow.nextSibling;
		}
		
	}
	
}





function setRequiredStructureField(structureId)
{
	var theRow = document.getElementById('req_' + structureId)
	if(theRow) theRow.innerHTML = '*';	
}

function setNotRequiredStructureField(structureId)
{
	var theRow = document.getElementById('req_' + structureId)	
	if(theRow) theRow.innerHTML = '';	
}



function setRecommendedStructureField(structureId)
{
	var theRow = document.getElementById('rec_' + structureId)
	if(theRow) theRow.innerHTML = '+';	
}

function setNotRecommendedStructureField(structureId)
{
	var theRow = document.getElementById('rec_' + structureId)	
	if(theRow) theRow.innerHTML = '';	
}


function focusOnStructureElementViaTR(parentElement)
{

	var trCol;
	var inputCol;

	
	trCol = parentElement.getElementsByTagName('tr')
	
	for(var e = 0; e < trCol.length; e++)
	{
		if(trCol[e].id.indexOf('row_') != -1)
		{
			if(trCol[e].style.display !=  'none')
			{
				inputCol = trCol[e].getElementsByTagName('input');
				if(inputCol.length != 0)
				{
					try
					{
						if(inputCol[0])
						{	
							inputCol[0].focus();
							break;
						}
					}
					catch(er)
					{
					
					}
				}
				inputCol = trCol[e].getElementsByTagName('textarea');
				if(inputCol.length != 0)
				{
					try
					{
						if(inputCol[0])
						{	
							inputCol[0].focus();
							break;
						}
					}
					catch(er)
					{
					
					}
				}				
	
				inputCol = trCol[e].getElementsByTagName('select');
				if(inputCol.length != 0)
				{
					try
					{
						if(inputCol[0])
						{	
							inputCol[0].focus();
							break;
						}
					}
					catch(er)
					{
					
					}
				}
				
			}
		}
	}
}



 function setSelectOptions(fieldName, displayType, valueArray)
 {

	var fn = fieldName;
	
	var sel = rulesForm.elements[fn];
	if(!sel) { return false; }
	if(!sel.options) { return false; }
	var currentValue = '';
	var oOption;
	var cGroup;
	var cGroupName = '';
	
	
	// Get the current value
	if(sel.selectedIndex >= 0)
	{
		currentValue = sel.options[sel.selectedIndex].value;
	}
	else
	{
		currentValue = '';
	}
	
	
	if((sel.options.length - 1) * 3 == valueArray.length)
	{
		var doReturn = true;
		for(var i = 1; i < sel.options.length; i++)
		{
			if(sel.options[i].value != valueArray[(3 * (i -1)) + 2] || sel.options[i].text != valueArray[(3 * (i -1)) + 1])
			{
				doReturn = false;
			} 
		}	if(doReturn == true) { return false };
	
	}

	
	sel.selectedIndex = 0;
	
	if(sel.children)
	{
		var selLength = sel.children.length;
		for(var i = selLength - 1; i > 0; i--)
		{
			if(sel.children[i].nodeType == 1)
			{
				sel.removeChild(sel.children[i]);
			}
		}
	}
	else
	{
		sel.options.length = 1;
	}
	
	

	// Add the new values from the value array
	if(valueArray.length > 2)
	{
		for(var i = 0; i < valueArray.length; i += 3)
		{
			oOption = document.createElement("OPTION");

			
			if(valueArray[i] != '' && valueArray[i] != cGroupName)
			{
				cGroup = document.createElement("OPTGROUP");
				sel.appendChild(cGroup);
				cGroup.label = valueArray[i];
				cGroupName = valueArray[i];
				
			}
			
			if(cGroupName == '')
			{
				sel.options.add(oOption);
			}
			else
			{
				cGroup.appendChild(oOption);
			}
			oOption.text = valueArray[i + 1]
			oOption.value = valueArray[i + 2]
		
			
			if(oOption.value == currentValue) { oOption.selected = true; }
		}
	}

 }

