

function validateStructureElement(
		subForm,
		structureId,
		structureElementName,
		fieldLabel,
		fieldType,
		fieldSize,
		displayType,
		required, handleRequired,
		minimumValue, maximumValue, decimalPlaces,
		regularExpression, regularExpressionError)
{

	var strError = '';
	var structureElement = subForm[structureElementName];
	/* Required Field Validation */



	
	if(window.document.getElementById('req_' + structureId))
	{
		if(window.document.getElementById('req_' + structureId).innerHTML == '*') { required = true } else { required = false };
	}

	var boolHidden = false;
	if(window.document.getElementById('row_' + structureId))
	{
		if(window.document.getElementById('row_' + structureId).style.display == 'none') boolHidden = true;
	}
	if(boolHidden == true) required = false;
	
	if(required == true && handleRequired == true)
	{
	
		switch(displayType)
		{
		
			case 'Select':
				
				if(structureElement.selectedIndex == 0)
				{
					strError += 'You must select a value for: ' + fieldLabel + '\n';
				}
				break;
			case 'Radio':
				
				radioFound = false;

				for(var i = 0; i < structureElement.length; i++)
				{
					if(structureElement[i].checked) radioFound = true;
				}

				if(radioFound == false)
				{
					strError += 'You must select a value for: ' + fieldLabel + '\n';
				}
				break;
			
			case 'Checkbox':
			
				checkFound = false;
				if(structureElement.length)
				{
				 	for(var i = 0; i < structureElement.length; i++)
					{
						if(structureElement[i].checked) checkFound = true;
				 	}
				}
				else
				{
					if(structureElement.checked) checkFound = true;
				}

				if(checkFound == false)
				{
					if(structureElement.length) {
						strError += 'You must select a value for: ' + fieldLabel + '\n';
					}
					else
					{
						strError += 'The following field must be checked: ' + fieldLabel + '\n';
				 	}
				}
			
				break;
			default:
			
			
				if(structureElement.value == '')
				{
				 strError += 'You must enter a value for: ' + fieldLabel + '\n';
				}
			
				break;
			
		}
		
	}
	
	
	/* Value-based validation */
	if(fieldType == 'Numeric' && displayType != 'Radio' && displayType != 'Checkbox')
	{
	
		if(displayType == 'Currency')
		{
			structureElement.value = replace_norm(structureElement.value, '$', '');
		}
		strError += validate_num(structureElement.value, structureElement.name, fieldLabel, minimumValue, maximumValue, decimalPlaces);
	
	
	}

	if(displayType == 'Memo' || displayType == 'Memo - HTML' || displayType == 'Memo - Expanding')
	{
		strError += validate_memo(structureElement.value, structureElement.name, fieldLabel, fieldSize);
	}

	if(displayType == 'Date/Time')
	{
		strError += validate_date_time(
			structureElement.value,
			subForm[structureElement.name + '_hour'].options[subForm[structureElement.name + '_hour'].selectedIndex].value,
			subForm[structureElement.name + '_minute'].options[subForm[structureElement.name + '_minute'].selectedIndex].value,
			subForm[structureElement.name + '_ampm'].options[subForm[structureElement.name + '_ampm'].selectedIndex].value,
			fieldLabel,
			minimumValue,
			maximumValue,
			structureElement,
			required);
	}

	/* Date Validation */
	if(fieldType == 'Date' && displayType != 'Date/Time')
	{
		strError += validate_date(structureElement.value, structureElement.name, fieldLabel, minimumValue, maximumValue)
	}

	/* Regular Expression Validation */
	if(regularExpression != '' && handleRequired == true)
	{
		var regExObj = new RegExp(regularExpression);
		var patternstructureElement = new String(structureElement.value);
		
		if(!regExObj.test(patternstructureElement) && structureElement.value != '')
		{
			strError += regularExpressionError + '\n';
		}
		
		
	}

	return strError;

}





function validate_num(strNum, fldName, fldLabel, minValue, maxValue, decimalPlaces)
{
	var decimalIndex;
	if(!(strNum == ""))
	{
		// CHECK TO MAKE SURE VALUE IS A NUMBER
		if(strNum.indexOf("$") > -1 || strNum.indexOf(",") > 0)
		{
		
			return fldLabel + " must be a Number, No $ or ,\n"
		}
		if(isNaN(strNum)) {

			return fldLabel + " must be a Number.\n";
		}
		
		//CHECK MIN AND MAX VALUES
		if(isNaN(parseFloat(minValue)) && minValue != '')
		{
		
			if(window.document.getElementById(minValue))
			{
				minValue = window.document.getElementById(minValue).value;
			}
		
		}

		if(isNaN(parseFloat(maxValue)) && maxValue != '')
		{
		
			if(window.document.getElementById(maxValue))
			{
				maxValue = window.document.getElementById(maxValue).value;
			}
		
		}

		if(window.document.getElementById(fldName + '_new_min'))
		{
			minValue = window.document.getElementById(fldName + '_new_minimum').value;	
		}

		if(window.document.getElementById(fldName + '_new_max'))
		{
			maxValue = window.document.getElementById(fldName + '_new_minimum').value;	
		}

		
		if(minValue != "")
		{
			if(parseFloat(minValue) > parseFloat(strNum))
			{
				return fldLabel + " must be greater than " + minValue + ".\n";
			}
		}
		
		if(maxValue != "")
		{
			if(parseFloat(maxValue) < parseFloat(strNum))
			{
				return fldLabel + " must be smaller than " + maxValue + ".\n";
			}
		}
		
		decimalIndex = strNum.indexOf(".")
		if(decimalIndex > -1)
		{
			if(decimalPlaces == "0")
			{
				return fldLabel + " must be an integer.\n";
			}
	
			if(parseInt(decimalPlaces) < strNum.length - decimalIndex - 1)
			{
				return fldLabel + " has too many decimal places.\n";
			}
		}		

		
	}
	return "";
}


function validate_memo(strMemo, fldName, fldLabel, fldSize)
{
	if(!strMemo == "")
	{
		if(strMemo.length > fldSize)
		{
			return fldLabel + " must be Less than " + fldSize  + "\n";
		}
	}
	return "";
}


function validate_date(strDate, fldName, fldLabel, minValue, maxValue)
{
	var x
	var strSplit = new String(strDate)
	var date_array = new Array();
	var testDate;
	
	date_array = strSplit.split(" ")
	date_array = date_array[0].split("/")

	if(!strDate == "")
	{
		
		x = Date.parse(strDate);
		

		if(isNaN(x) || date_array.length != 3)
		{
			
			return fldLabel + " must be a date.\n";
		}
		
		if(date_array[0].length < 1 || date_array[0].length > 2)
		{
			return fldLabel + " has an invalid month.\n";
		}

		if(date_array[2].length > 4 || date_array[2].valueOf() < 0)
		{
			return fldLabel + " has an invalid year.\n";
		}
		
		testDate = new Date(date_array[2], date_array[0] - 1, date_array[1]);
		if (testDate.getMonth() != date_array[0] - 1)
		{
			return fldLabel + " must be a date.\n";
		}
		

		
		// Check min/max values
		if(window.document.getElementById(fldName + '_new_min'))
		{
			minValue= window.document.getElementById(fldName + '_new_minimum').value;	
		}

		if(window.document.getElementById(fldName + '_new_max'))
		{
			maxValue = window.document.getElementById(fldName + '_new_minimum').value;	
		}

		if(isNaN(Date.parse(minValue)) && minValue != '')
		{
		
			if(window.document.getElementById(minValue))
			{
				minValue = window.document.getElementById(minValue).value;
			}
		
		}

		if(isNaN(Date.parse(maxValue)) && maxValue != '')
		{
		
			if(window.document.getElementById(maxValue))
			{
				maxValue = window.document.getElementById(maxValue).value;
			}
		
		}



		
		if(x < Date.parse(minValue))
		{
			return fldLabel + " must be on or after " + minValue + ".\n";
		}

		if(x > Date.parse(maxValue))
		{
			return fldLabel + " must be on or before " + maxValue + ".\n";
		}
		
		
		
	}
	
	return "";
}


function validate_date_time(strDate, strHour, strMinute, strAMPM, fldLabel, minValue, maxValue, hidBox, required)
{

	var strError = new String("");
	strError = validate_date(strDate, fldLabel, fldLabel, minValue, maxValue)
	
	if(strError.value != "") return strError;
	
	var strTime = new String(strHour + strMinute + strAMPM)
	
	
	if(strDate == "" || strTime ==  "")
	{
		if(strDate == "" && strTime == "")
		{
			if(required == false ) return "";
			
			return "You must enter a value for: " + fldLabel + ".\n";
		}
		else
		{
			return fldLabel + " has an invalid date or time.\n";	
		}
		
	}
	
	
	
	if(strTime.length != 6)
	{
		return fldLabel + " has an invalid time.\n";
	}
	

	return "";
}



/*
This function validates dates based on individually passed in date components.
*/
function validate_date_dd(strMonth, strDay, strYear, fldLabel, hidBox, required, minValue, maxValue)
{
	var x
	var y
	var strDate = new String(strMonth + "/" + strDay + "/" + strYear)
	
	var z = Date.parse(strDate);
	
	// alert(strDate)
	if(strDate.length == 10)
	{

		x = new Date(strYear, strMonth - 1, strDay)

		if(x.getMonth() != strMonth - 1)
		{
			hidBox.value = "";
			return fldLabel + " must be a date.\n";
		}
		
		// CHECK MIN/MAX VALUES
		if(z < Date.parse(minValue))
		{
			hidBox.value = "";
			return fldLabel + " must be on or after " + minValue + ".\n";
		}

		if(z > Date.parse(maxValue))
		{
			hidBox.value = "";
			return fldLabel + " must be on or before " + maxValue + ".\n";
		}		

		
		hidBox.value = strDate;
		
		
		
		
	}
	else
	{
		if(strDate.length != 2) {

			hidBox.value = "";
			return "Invalid " + fldLabel + "\n";
		}
		
		if(required == true) {
			hidBox.value = "";
			return "You must enter a value for: " + fldLabel + "\n";
		}
	}


	return "";
}








function isCurrency(str){
	var isFlag=0;
    var isDollarSign=0;
    var isPeriod = 0;
	var isStr=str;
	
	for (var i=0; i <isStr.length ; i++) {
		if (isStr.charAt(i) == "$" ) {
			isDollarSign=isDollarSign + 1;
		}else if (isStr.charAt(i) == "." ) {
			isPeriod=isPeriod + 1;
		}else if (isStr.charAt(i) == "," ) {
			//do nothing
		} else {
			if (isNumber(isStr.charAt(i)) == false ) {
				isFlag=1;
			}
		}
	}	
	if (isFlag == 0 && isDollarSign < 2 && isPeriod < 2 ) { 
		return true;
	} else {
		return false
	}
}

function isNumber (tmp) {  
	var i; 
	for (i=0;i<tmp.length;i++) { 
		c = tmp.charAt(i) 
		if (c < "0" || c > "9") return false; 
	} 
	return true; 
} 



function getCurrencyNumber(sstr) {

	sstr = replace_norm(sstr,'$','');
	sstr=sstr.replace(/,/gi,'');
	return sstr;

}





function textSelect_autoscroll(textElement, selectElement, prevH, prevK)
{


	var lngMatches = 0;
	var lngIndex = -1;
	var strOption = new String("");
	var strValue = new String("");
	var strElValue = new String(textElement.value);
	
	
	if(document.all)
	{
		
		var strKeyCode = new String(event.keyCode)
		
		if(document.getElementById(prevK).value != strKeyCode)
		{

			return false;
		}
	
	
		if(window.event.keyCode == 16)
		{
			return false;
		}	
		if(window.event.keyCode == 8)
		{

			if(document.getElementById(prevH).value == "true")
			{
				textElement.value = strElValue.substr(0, strElValue.length - 1);
			}
			// return false;
		}
	}
	if(textElement.value == "") 
	{	
		selectElement.options[0].selected = true;
		return false;
	}
	
	for(i = 0; i < selectElement.options.length; i++)
	{
		strOption = selectElement.options[i].text
		strOption = strOption.toUpperCase();
		strValue = textElement.value;
		strValue = strValue.toUpperCase();
		
		if(strOption.indexOf(strValue) == 0)
		{
			if(lngIndex == -1)
			{
				selectElement.options[i].selected = true;
				lngIndex = i;
			}
			lngMatches++;
		}
	}

	if(lngMatches == 0)
	{
		selectElement.options[0].selected = true;
		document.getElementById(prevH).value = "false";
		return false;
		
	}
	else
	{
		if(lngMatches == 1)
		{
			strValue = textElement.value;
			var oldLength = strValue.length
			
			if(document.all)
			{
				textElement.value = selectElement.options[lngIndex].text;
				
				strValue = textElement.value;
				var newLength = strValue.length
				
				var tRange = textElement.createTextRange();
				
				
				
				tRange.moveStart("character", oldLength)
				if(tRange.text != "")
				{
					document.getElementById(prevH).value = "true";
				}
				else
				{
					document.getElementById(prevH).value = "false";
				}
			
				tRange.select();
			}
		}
		else
		{
			document.getElementById(prevH).value = "false";
		}
		return true;
	}
}	

function textSelect_select2text(textElement, selectElement)
{

	if(selectElement.selectedIndex > 0)
	{	

		textElement.value = selectElement.options[selectElement.selectedIndex].text
		selectElement.focus();	
	}
	else
	{

		textElement.value = "";
		selectElement.focus();
	}
}


/*
This function clears a field's value.
*/
function clearField(theField)
{

	if(!theField.length)
	{

		if(theField.checked)
			theField.checked = false;
		

		if(theField.value)
			theField.value = '';
			
		
		
	}
	else
	{
		for(var i = 0; i < theField.length; i++)
		{
			if(theField[i].checked)
				theField[i].checked == false;
		}
	
		if(theField.selectedIndex)
		{
			theField.selectedIndex = 0;
		}

	
	}

}


/*
This function validates a structure element as it appears on the search screen.
*/
function validateSearchField(y, theform, ssid, fldname, fldlbl, fldtype, fldsize, flddisplaytype)
{

   if(fldtype == "Numeric")
   {
   	if(flddisplaytype == "Text" || flddisplaytype == "Currency")
	{

		xx = theform["f1_"+ ssid].value
		xx2 = theform["f2_" + ssid].value
		xxst = theform["st_" + ssid].options[theform["st_" + ssid].selectedIndex].value
		
		xx = ltrim(xx)
		xx = rtrim(xx)
					
		xx2 = ltrim(xx2)
		xx2 = rtrim(xx2)
		
	       if(xx != "")
		{
		
			if(isNaN(xx))
			{
				alert(fldlbl + " must be a Number.")
				y = 1;
			}
	
			if(xxst == "notbetween" || xxst == "between")
			{
				if(isNaN(xx2) || xx2 == "")
				{
					alert("The second value for " + fldlbl + " must be a Number.")
					y = 1;
				}
			}
		}
	}
	else
	{

		if(theform[fldname].tagName.toLowerCase() != "select")
		{

			if(!theform[fldname].value == "")
			{

				xx = theform[fldname].value
				
				xx = ltrim(xx)
				
				if(isNaN(xx))
				{
					alert(fldlbl + " must be a Number.")
					y = 1;
				}
			
			       xx = theform[fldname].value;
				theform[fldname].value = replace_norm(xx, " ", "")
			}

		}
		else
		{
			
			theform[fldname].options[0].selected = false;
		}
	}
   }

   
   if(fldtype == "Date")
   {
	xx = theform["f1_" + ssid].value
	xx2 = theform["f2_" + ssid].value
	xxst = theform["st_" + ssid].options[theform["st_" + ssid].selectedIndex].value
	
	xx = ltrim(xx)
	xx = rtrim(xx)
				
	xx2 = ltrim(xx2)
	xx2 = rtrim(xx2)
	
	if(xx != "")
	{
		/* Check against the days counter */
		if(xx.indexOf("days") > -1)
		{
			xx = xx.replace("days", "")
			xx = xx.replace(" ", "")
			if(isNaN(xx) || xx == "")
			{
				window.alert(fldlbl + " has an invalid number of days specified.");
				y = 1;

			}
			return y;
		}
		
		if (xx != 'today' && xx != 'yesterday' && xx.indexOf("day") > -1) {
			xx = xx.replace("day", "")
			xx = xx.replace(" ", "")
			if (isNaN(xx) || xx == "") {
				window.alert(fldlbl + " has an invalid number specified.");
				y = 1;
			}
			return y;
		}

		
		/* Check against month number */
		if (xx.indexOf("month") > -1) {
			xx = xx.replace("month", "")
			xx = xx.replace(" ", "")
			if (isNaN(xx) || xx == "") {
				window.alert(fldlbl + " has an invalid month specified.");
				y = 1;
			}
			return y;
		}

		/* Check against year number */
		if (xx.indexOf("year") > -1) {
			xx = xx.replace("year", "")
			xx = xx.replace(" ", "")
			if (isNaN(xx) || xx == "") {
				window.alert(fldlbl + " has an invalid year specified.");
				y = 1;
			}
			return y;
		}
		
		
		/* Check against any of the standard keywords */
		if (xx != "today" && xx != "yesterday" && xx != "tomorrow" && xx != "ytd" && xx != "mtd")
		{
			x = validate_date(xx, fldname,  fldlbl)
			if(x != "")
			{
				window.alert(x);
				y = 1;
						
			}
		}
		
		if(xxst == "notbetween" || xxst == "between")
		{
			x = validate_date(xx2, fldname, 'The second value for ' + fldlbl)
			if(x != "")
			{
				window.alert(x);
				y = 1;
						
			}
		}
	}
    }

    return y;
}


/*
This function determines whether a field is empty.
*/
function validateIsEmpty(passed_form, fldname, displaytype, boolEmpty)
{
	
	if(displaytype == "Select" || displaytype == "Select Type-In")
	{
		if(passed_form[fldname].selectedIndex > 0) boolEmpty = false;
	}
	
	else if(displaytype == "Radio")
	{
		radioFound = false;

		for(var i = 0; i < passed_form[fldname].length; i++)
		{

			if(passed_form[fldname][i].checked) radioFound = true;
		}

		if(radioFound == true) boolEmpty = false;
	}
	
	else if(displaytype == "Checkbox")
	{
		checkFound = false;
		if(passed_form[fldname].length)
		{
			for(var i = 0; i < passed_form[fldname].length; i++)
			{
				if(passed_form[fldname][i].checked) checkFound = true;
			}
		}
		else
		{
			if(passed_form[fldname].checked) checkFound = true;
		}
		
		
		if(checkFound == true) boolEmpty = false;
	}
	
	
	else
	{
		
		if(passed_form[fldname].value != "")
		{
			boolEmpty = false;
		}
	}
	return boolEmpty;
}


/*
This function sets a field's value
*/
function setFieldValue(passed_form, fldname, displaytype, newValue, newValuePlainText)
{

	/* Code for Select Popups has not been written yet */
	
	
	if(!passed_form[fldname]) return false;
	if ((displaytype == "Select" || displaytype == "Select Type-In") && passed_form[fldname].options) {

	    for (e = 0; e < passed_form[fldname].options.length; e++) {
	        if (passed_form[fldname].options[e].value == newValue) { passed_form[fldname].options[e].selected = true; } else { passed_form[fldname].options[e].selected = false; }
	    }

	    if (displaytype == "Select Type-In") {

	        passed_form[fldname + '_typein'].value = newValuePlainText;
	    }

	}
	else if (displaytype == 'Type-In Lookup') {
	    passed_form[fldname].value = newValue;
	    passed_form[fldname + '_textbox'].value = newValuePlainText;
	}
	
	else if (displaytype == 'Select Popup') {
	    passed_form[fldname].value = newValue;
	    passed_form[fldname + '_label'].value = newValuePlainText;

	}

	else if (displaytype == "Radio") {
	    for (var i = 0; i < passed_form[fldname].length; i++) {

	        if (passed_form[fldname][i].value == newValue) {
	            passed_form[fldname][i].checked = true;
	        }
	        else {
	            passed_form[fldname][i].checked = false;
	        }
	    }

	}

	else if (displaytype == "Checkbox") {

	    if (passed_form[fldname].length) {
	        for (var i = 0; i < passed_form[fldname].length; i++) {
	            if (passed_form[fldname][i].value = newValue) {
	                passed_form[fldname][i].checked = true;
	            }
	            else {
	                passed_form[fldname][i].checked = false;
	            }
	        }

	    }
	    else {
	        if (newValue == '1') { passed_form[fldname].checked = true } else { passed_form[fldname].checked = false };
	    }

	}
	else {

	    passed_form[fldname].value = newValue;
	}
	

}



/* This function determines the current value of a field. */
function getFieldValue(passed_form, fldname, displaytype)
{

	var strValue = new String('');
	
	if(displaytype == "Select" || displaytype == "Select Type-In")
	{
		if(passed_form[fldname].selectedIndex > 0)
		{
			strValue = passed_form[fldname].options[passed_form[fldname].selectedIndex].value;
		}
	}
	
	else if(displaytype == "Radio")
	{
		radioFound = false;

		for(var i = 0; i < passed_form[fldname].length; i++)
		{

			if(passed_form[fldname][i].checked) { strValue = passed_form[fldname][i].value }
		}

		if(radioFound == true) boolEmpty = false;
	}
	
	else if(displaytype == "Checkbox")
	{
		s
		strValue = '';
		if(passed_form[fldname].length)
		{
			for(var i = 0; i < passed_form[fldname].length; i++)
			{
				if(passed_form[fldname][i].checked) strValue += passed_form[fldname][i].value + ';';
			}
			if(strValue != '') strValue = strValue.substr(0, strValue.length - 1)
		}
		else
		{
			if(passed_form[fldname].checked) strValue = '1';
		}
		
	}
	else if(displaytype == "Date/Time")
	{
		strValue = passed_form[fldname].value + " " + passed_form[fldname + '_hour'].options[passed_form[fldname + '_hour'].selectedIndex].value + ":" + passed_form[fldname + '_minute'].options[passed_form[fldname + '_minute'].selectedIndex].value + ":00 " + passed_form[fldname + '_ampm'].options[passed_form[fldname + '_ampm'].selectedIndex].value
	
	}
	
	else
	{
		
		strValue = passed_form[fldname].value;
	}
	
	return strValue;
	
}




function subNavValidate()
{

	if(editOccured == true) {
		return confirm('Clicking on this link will result in your current edits not being saved.');
	} else {
		return true
	}

}



/*
Functions for rule handling
*/

/* Sets up rule event handlers and also runs rules for the first time */
var rulesForm;
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('blur', rulesRunRules, false);
			
			
			} else { // IE
				subForm.elements[i].attachEvent('onclick', rulesRunRules);
				subForm.elements[i].attachEvent('onblur', rulesRunRules);
				subForm.elements[i].attachEvent('onchange', rulesRunRules, false);
			}
		}
	}
	 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
	rulesRunningRules = true;
	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(addElem == true)
			{
				strPost += '&' + subForm.elements[i].name + '=' + encodeURIComponent(subForm.elements[i].value)
			
			}
		}
		
	}
	
	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");
	xmlHttpRules.send("action=post&rulesaction=" + rulesForm.name + strPost);

	
	
}

function rulesRunReturnedActions()
{
	
	if (xmlHttpRules.readyState == 4 || xmlHttpRules.readyState == "complete")
	{
		
		eval(xmlHttpRules.responseText);
		
		setTimeout("rulesMessages = ''", 5000);
		
		rulesRunningRules = false;
		rulesTheSource = '';
		var theRows;
		var theDiv;
		var boolHidden;
		var boolVis;
		var aLink;
		var theTD;
		var theTDleft;
		var theTDright;
		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(theRows[e].id.indexOf('row_') != -1)
						{
							if(theRows[e].style.display == 'none') boolHidden = true;
							if(theRows[e].style.display != 'none') boolVis = true;
							
						}

					}
					
					
					
					
					aLink = window.document.getElementById('tab' + i);
					if(aLink)
					{
						
						theTD = aLink.parentNode;
						while(theTD.tagName.toLowerCase() != 'td')
						{
							
							theTD = theTD.parentNode;
						}
						
						if(boolVis == false && boolHidden == true)
						{
						
							theTD.style.display = 'none';
							
							
							theTDright = theTD.nextSibling
							if(theTDright)
							{
								while(theTDright.nodeType!=1)
								{
									
									theTDright= theTDright.nextSibling;
									if(!theTDright) break;
								}
								if(theTDright) theTDright.style.display = 'none'
							}
							
							

							theTDleft = theTD.previousSibling
							if(theTDleft )
							{
								while(theTDleft.nodeType!=1)
								{
									
									theTDleft= theTDleft.previousSibling;
									if(!theTDleft) break;
								}
								if(theTDleft) theTDleft.style.display = 'none'
							}
							
						}
						else
						{
							theTD.style.display = '';
						
						
							
							theTDright = theTD.nextSibling
							if(theTDright)
							{
								while(theTDright.nodeType!=1)
								{
									
									theTDright= theTDright.nextSibling;
									if(!theTDright) break;
								}
								if(theTDright) theTDright.style.display = ''
							}
							
							

							theTDleft = theTD.previousSibling
							if(theTDleft )
							{
								while(theTDleft.nodeType!=1)
								{
									
									theTDleft= theTDleft.previousSibling;
									if(!theTDleft) break;
								}
								if(theTDleft) theTDleft.style.display = ''
							}
							
							
						}
					}
					
					
					
					
				}
				
				
			}
		}
	}
	
	
}



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 setRequiredStuctureField(structureId)
{
	var theRow = document.getElementById('req_' + structureId)
	if(theRow) theRow.innerHTML = '*';	
}

function setNotRequiredStuctureField(structureId)
{
	var theRow = document.getElementById('req_' + 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 structureTypeInLookupChange(structureid, fieldName, boolAddItem)
{
    var hidElement = window.document.getElementById(fieldName);
    var txtElement = window.document.getElementById(fieldName + '_textbox');

	var strPost = new String('');
	
	var addElem;
	var strAddItem;
	if (boolAddItem == true) { strAddItem = 'true'; } else { strAddItem = 'false'; }
	
	if(txtElement.value == '') 
	{
	
		hidElement.value = '';
		structureTypeInLookupHideAdd(fieldName);
		structureTypeInLookupHide(fieldName);
		txtElement.className = 'structureTypeInLookupNotSelected';
	}
	else
	{
	    strPost = 'structureid=' + structureid + '&fieldname=' + encodeURIComponent(fieldName) + '&textvalue=' + encodeURIComponent(txtElement.value) + '&additem=' + strAddItem
		var url = "ajax_handletypeinlookup.aspx?" + window.location.search.substring(1);
		xmlHttpTypeInLookup = GetXMLHttpObject(handleStructureTypeInLookupChange);
		xmlHttpTypeInLookup.open("POST", url, true);
		xmlHttpTypeInLookup.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttpTypeInLookup.send("action=post&" + strPost);
	}	

	

}

function handleStructureTypeInLookupChange()
{

	if (xmlHttpTypeInLookup.readyState == 4 || xmlHttpTypeInLookup.readyState == "complete")
	{
	
		eval(xmlHttpTypeInLookup.responseText);

	}

}

function structureTypeInLookupShow(fieldName, strString) {

   
     var textElem = window.document.getElementById(fieldName + '_textbox');
     var divElem = window.document.getElementById(fieldName + '_lookuplistdiv');

 

     var xer = 0; var yer = 0;
     var cOffset = textElem;
     while (cOffset.offsetParent) {

         xer += cOffset.offsetLeft
         yer += cOffset.offsetTop
         cOffset = cOffset.offsetParent

     }
     
     // divElem.style.width = textElem.clientWidth;
     divElem.innerHTML = strString;

     divElem.style.position = 'absolute';
     divElem.style.display = 'inline';
     
     divElem.style.top = (yer + textElem.clientHeight) + 'px'
     divElem.style.left = (xer) + 'px'
     divElem.style.zIndex= 2000
 }

 function structureTypeInLookupHide(fieldName) {
    
     var divElem = window.document.getElementById(fieldName + '_lookuplistdiv');
 
     divElem.innerHTML = '';
     divElem.style.display = 'none';

 }


 function structureTypeInLookupSetValueAndTextBox(fieldName, newValue, newLookupValue) {


     var hidElement = window.document.getElementById(fieldName);
     var txtElement = window.document.getElementById(fieldName + '_textbox');

     hidElement.value = newValue;
     txtElement.value = newLookupValue;

     if (newValue != '') {
         txtElement.className = 'structureTypeInLookupSelected';
     }
     else {
         txtElement.className = 'structureTypeInLookupNotSelected';
     }
     structureTypeInLookupHideAdd(fieldName);
 }

 function structureTypeInLookupSetValue(fieldName, newValue) {


     var hidElement = window.document.getElementById(fieldName);
     var txtElement = window.document.getElementById(fieldName + '_textbox');
     hidElement.value = newValue;

     if (newValue != '') {
         txtElement.className = 'structureTypeInLookupSelected';
     }
     else {
         txtElement.className = 'structureTypeInLookupNotSelected';
     }
     structureTypeInLookupHideAdd(fieldName);
 }

 function structureTypeInLookupShowAdd(fieldName)
 {

     var aElement = window.document.getElementById(fieldName + '_addlookupitem');
     aElement.style.display = 'inline';
     
 }

 function structureTypeInLookupHideAdd(fieldName)
 {

     var aElement = window.document.getElementById(fieldName + '_addlookupitem');
     aElement.style.display = 'none';
 }