var ErrorListstr;
var NextPageList= new Array();
var GotoList= new Array();
var NoValidation=false;
function noenter(fld,evt) 
{

	if(!evt)
	{
		evt = window.event
	}
	else if(!evt.keyCode)
	{
		evt.keyCode = evt.which
	}
	if(evt.keyCode == 13) 
	{
		return(false)
	}	
	else
		return(true)
	
}

function isEmpty(str)
{

	if(str == null || str == "" || str == "undefined")
	{
		return true;
	}
	
	return false;
}

function isValidUniqueMatrixTestForDuplicates(matrixfield)
{

if(isEmpty(matrixfield))
	return(true)
	
var unifield = document.frmForm.elements[matrixfield];
if(isEmpty(unifield))
	return(true)
var matrixfields = unifield.value.split(",");
var i,x;
var j,y;
var brtn=true;

	for (i=0; i<matrixfields.length; i++)
	{
		//check if there is another field that has this value that is checked
		if(!isEmpty(matrixfields[i]))
		{
			var elementlist = document.frmForm.elements[matrixfields[i]];
			for(var x=0;x<elementlist.length;x++)
			{
				if(elementlist[x].checked)
				{
					//check if other fields
					for (j=i+1; j<matrixfields.length; j++)
					{
						//check if there is another field that has this value that is checked
						if(!isEmpty(matrixfields[j]))
						{
							var otherelementlist = document.frmForm.elements[matrixfields[j]];
							if(otherelementlist[x].checked)
							{
								return(false)
							}
						}
					}


				}
			}
		}
	}
	
	return brtn;

}

function isValidUniqueMatrix(matrixfield)
{
if(isEmpty(matrixfield))
	return(true)
	
var unifield = document.frmForm.elements[matrixfield];
if(isEmpty(unifield))
	return(true)
var matrixfields = unifield.value.split(",");
var i;
var needtobefilled=0;
var countfilled=0;

	for (i=0; i<matrixfields.length; i++)
	{
		//check if there is another field that has this value that is checked
		if(!isEmpty(matrixfields[i]))
		{
			var elementlist = document.frmForm.elements[matrixfields[i]];
			needtobefilled = elementlist.length
			for(var x=0;x<elementlist.length;x++)
			{
				if(elementlist[x].checked)
				{
					countfilled++;
				}
			}
		}
	}
	if(countfilled <needtobefilled) 
		return false;
	else
		return true; 

}

function UniqueMatrix(currentfield,matrixfield)
{
var matrixfields = matrixfield.value.split(",");
var i;
var currentvalue;

	if(currentfield.checked == true)
	{
		if(confirm("Would you like to unselect this value?"))
			currentfield.checked =false
				
		return;
	}
	
	currentvalue = currentfield.value;
	for (i=0; i<matrixfields.length; i++)
	{
		//check if there is another field that has this value that is checked
		if(!isEmpty(matrixfields[i]) && matrixfields[i] != currentfield.id)
		{
			var elementlist = document.frmForm.elements[matrixfields[i]];
			for(var x=0;x<elementlist.length;x++)
			{
				if(currentvalue == elementlist[x].value && elementlist[x].checked)
				{
					alert("Your choice must be unique!");
					currentfield.checked = false;
				}
			}
		}
	}
	return;

}

function TestPercentage(textfield,testfields,totalfield)
{
var total=0;
var dif=0;
var pos=0;
var els = new Array();

	if(!validNumber(textfield.value))
	{
		alert("Please Enter a numeric value!")
		textfield.value = 0;
	}
	else if(textfield.value.length>1)
	{
		if(textfield.value.substring(0,1)=='0' || parseInt(textfield.value)==0)
		{
			alert("Please Enter a numeric value without leading zeros!")
			textfield.value = 0;
		}
	}
		
	if(textfield.value > 100)
		textfield.value= 100;
	if(textfield.value <0)
		textfield.value= 0;

	//I did it this way but does not work in NN4
	//els = document.frmForm.elements[textfield.id];
	var elstestfields = testfields.value.split(",");
	for(var i=0;i<elstestfields.length;i++)
	{
		els[i] = document.frmForm.elements[elstestfields[i]];
	}
	
	for(var i=0;i<els.length;i++)
	{
		if(isEmpty(els[i].value))
			els[i].value = 0;
		total = total + parseInt(els[i].value);
		if(els[i].name == textfield.name) 
		{
			if(els.length == 1 || i == els.length-1)
				pos=0;
			else
				pos = i+1;
		}
			
	}

/* using a total field now 5/12/03
	dif = 100 - total;
	if(	parseInt(els[pos].value) + dif < 0)
	{
		for(var i=0;i<els.length;i++)
		{
			if(els[i].name != textfield.name) 
			{
				els[i].value = parseInt(els[i].value) + dif;
				if(parseInt(els[i].value) < 0)
				{	
					dif = parseInt(els[i].value)
					els[i].value = 0;
				}
				else
					dif = 0;
			}
				
		}
	}
	else
		els[pos].value = parseInt(els[pos].value) + dif;
*/

	totalfield.value = total;
	
}

function ShowHelp(helptext,bg,fg,bgimage,font)
{
var posx;
var poxy;
var width = 300;
var height = 300;

	posx = window.screen.width/2 - width/2;
	posy = window.screen.height/2 - height/2;

	var windowoptions = "directories=no,location=no,menubar=no,status=no";
	windowoptions += ",titlebar=no,toolbar=no,scrollbars=yes,resizable=yes";
	windowoptions += ",screenX=" + posx + ",screenY=" + posy;
	windowoptions += ",width=" + width + ",height=" + height;
	
	var nw = window.open('','Help',windowoptions);

	nw.document.write("<html><head><title>Help</title></head><body style=background:" + bg + ";color:" + fg + ";font:" + font + "><table width=100% height=75%><tr><td align='left' valign='top'>");
	nw.document.write(helptext.value);
	nw.document.write("</td></tr></table><center><br><br><a href='javascript:window.close()'>Close</a></center>");
	nw.document.write("</body></html>");
	nw.document.close();

	nw.moveTo(posx,posy)
	nw.focus();
}

function isOtherFieldEmpty(fieldid)
{
//if it is a has a match - if it is an other field
// if no other fields it will return true
//only returns false if related field has data

var rtn	=true;
var typechar = fieldid.substring(0,2);
var onlyid = fieldid.substring(2,fieldid.length);
var strTest = "";
var otherfield
		
	if(typechar != "t_")
	{
		otherfield = GetField("t_" + onlyid);
		if(!isEmpty(otherfield.id))
		{
			if(otherfield.fieldvalue.length > 0)
				strTest = otherfield.fieldvalue[0]
		}
	}
	
	if(!isEmpty(strTest))
		rtn = false;
			
	return(rtn);

}			
function DisplayErrorList(bg,fg,bgimage,font)
{
var posx;
var poxy;
var width = 300;
var height = 400;
return;
	posx = window.screen.width/2 - width/2;
	posy = window.screen.height/2 - height/2;

	var windowoptions = "directories=no,location=no,menubar=no,status=no";
	windowoptions += ",titlebar=no,toolbar=no,scrollbars=yes";
	windowoptions += ",screenX=" + posx + ",screenY=" + posy;
	windowoptions += ",width=" + width + ",height=" + height + ",resizeable=yes";
	
	var nw = window.open('','Errors',windowoptions);

	nw.document.write("<html><head><title>Errors</title>");
	nw.document.write("<SCRIPT LANGUAGE='javascript'>");
	
	nw.document.write("function GotoPageError(pageid,fieldid)");
	nw.document.write("{");

	nw.document.write("		if(window.opener.document.frmForm.elements[fieldid]!= undefined)");
	nw.document.write("		{");
	nw.document.write("		if(window.opener.document.frmForm.elements[fieldid].name == fieldid);");
	nw.document.write("		{");
	nw.document.write("		 window.opener.location.hash = \"field_\" + fieldid.substring(2,fieldid.length);");
	nw.document.write("		}");
	nw.document.write("	}	");
			
	nw.document.write("		window.opener.document.frmForm.nextpage.value = \"/form/page[@id='\" + pageid + \"']\" ; ");
	nw.document.write("		window.opener.document.frmForm.submit();");
	nw.document.write("	}	");
	
	nw.document.write("</SCRIPT>");
	
	nw.document.write("</head><body style=background:" + bg + ";color:" + fg + ";font:" + font + "><table width=100%>");
	nw.document.write("<tr><td align='left' valign='top'>Below is a list of errors, to correct the error click on that error.<hr></td></tr>");
	nw.document.write(ErrorListstr);
	nw.document.write("</table><center><br><br><a href=\"javascript:window.close();\"><b>Close</b></a></center>");
	nw.document.write("</body></html>");
	nw.document.close();

	nw.moveTo(posx,posy)
	nw.focus();

}

function AddToErrorList(pageid,fieldid,errdescription,gotoarea)
{
var javagotocode;

	javagotocode = "href=javascript:GotoPageError('" + pageid + "','" + fieldid + "')";
	ErrorListstr =  ErrorListstr + "<tr><td><a " + javagotocode + ">Error: " + errdescription + "</a></td></tr>";
	//window.document.frmForm.iox_error.value = window.document.frmForm.iox_error.value + "<a " + javagotocode + "><b>Error: " + errdescription + "</b><a><br>"
	if(isEmpty(window.document.frmForm.iox_error.value))
		window.document.frmForm.iox_error.value = pageid + "@" + fieldid + "@" + errdescription
	else
		window.document.frmForm.iox_error.value = window.document.frmForm.iox_error.value + "," + pageid + "@" + fieldid + "@" + errdescription
}

function GotoPageError(pageid,fieldid)
{
/*
	if(window.document.frmForm.elements[fieldid]!= undefined)
	{
		if(window.document.frmForm.elements[fieldid].name == fieldid);
		{
			window.location.hash = "field_" + fieldid.substring(2,fieldid.length);
		}
	}	
	*/		
	window.document.frmForm.nextpage.value = "/form/page[@id='" + pageid + "']" ;
	window.document.frmForm.submit();
}
	
function ValidateForm()
{
var rtn = true;
var PassedValidation = true;
var strTest;
var fieldid;
var i=0;
var iElements;
var typechar;
var thisfield;
var iFields;
var OnThisPage;
var lastfieldid="";

	if(NoValidation) 
		return(PassedValidation);

	PassedValidation = ValidatePage();

	for(var testfield in field)
	{
						
		i++;	
		
		thisfield = field[testfield];

		//we do this because we do not want to validate the same field more then once
		if(thisfield!=lastfieldid)
		{
			lastfieldid = thisfield
			
			strTest = "";
			if(thisfield.fieldvalue.length > 0)
				strTest = thisfield.fieldvalue[0]

			OnThisPage = false;
			if(isEmpty(document.frmForm.elements[thisfield.id]))
				OnThisPage = false;
			else if(document.frmForm.elements[thisfield.id].id == thisfield.id)
			{
				OnThisPage= true;
			}
			else
			{ //matrix
				OnThisPage= true;
			}

			
			if(!OnThisPage)
			{
				//check to see if it meets it requirement rule
				if(!isEmpty(thisfield.uniquematrixid))
				{
					if(!isValidUniqueMatrixTestForDuplicates(thisfield.uniquematrixid))
					{
						AddToErrorList(thisfield.pageid,thisfield.id,'Must be unique!',"");
						PassedValidation = false;
					}
				}
				if(isEmpty(strTest) && IsRequired(thisfield)  && isEmpty(thisfield.uniquematrixid) && isOtherFieldEmpty(thisfield.id))
				{
					AddToErrorList(thisfield.pageid,thisfield.id,"Required Field!","");
					PassedValidation = false;
				}
				else if(isEmpty(strTest) && IsRequired(thisfield) && !isEmpty(thisfield.uniquematrixid))
				{
					//validation is shown for whole matrix
				}
				if(!isEmpty(strTest))
				{
					if(strTest.length < thisfield.min)
					{
						AddToErrorList(thisfield.pageid,thisfield.id,'Field is too short!',"");
						PassedValidation = false;
					}
					if(!isEmpty(thisfield.format))
					{
						if(thisfield.format == "1")
						{	
							rtn = validDate(strTest);
							if(rtn == false)
							{
								AddToErrorList(thisfield.pageid,thisfield.id,'Invalid date!',"");
								PassedValidation = false;
							}
						}	
						else if(thisfield.format == "2")
						{
							rtn = validTime(strTest);
							if(rtn == false)
							{
								AddToErrorList(thisfield.pageid,thisfield.id,'Invalid time!',"");
								PassedValidation = false;
							}
						}
						else if(thisfield.format == "3")
						{
							rtn = validNumber(strTest);
							if(rtn == false)
							{
								AddToErrorList(thisfield.pageid,thisfield.id,'Invalid number!',"");
								PassedValidation = false;
							}
						}	
						else if(thisfield.format == "4")
						{
							rtn = validNumber(strTest);
							if(rtn == false)
							{
								AddToErrorList(thisfield.pageid,thisfield.id,'Invalid number!',"");
								PassedValidation = false;
							}
						}	
						if(thisfield.format == "5")
						{	
							if(strTest.length < 10 || !validNumber(strTest))
							{
								AddToErrorList(thisfield.pageid,thisfield.id,'Invalid phone number!',"");
								PassedValidation = false;
							}
						}
						if(thisfield.format == "6" )
						{	
							if(strTest.length < 9 || !validNumber(strTest))
							{
								AddToErrorList(thisfield.pageid,thisfield.id,'Invalid social security number!',"");
								PassedValidation = false;
							}
						}
						if(thisfield.format == "7")
						{
							rtn = validDecimalNumber(strTest);
							if(rtn == false)
							{
								AddToErrorList(thisfield.pageid,thisfield.id,'Invalid decimal number!',"");
								PassedValidation = false;
							}
						}	
						
					}						
				}
			}			
		}
		
	}
		
	return(PassedValidation);
}

function ValidatePage()
{
var rtn = true;
var PassedValidation = true;
var strTest;
var fieldid;
var i;
var iElements;
var typechar;
var thisfield;
var lastfieldid="";
var temppageid;
var perttotal=0;
		
	if(NoValidation) 
		return(PassedValidation);

	ErrorListstr = "";
	
	for(iElements=0;iElements < document.frmForm.elements.length;iElements++)
	{	
		
			
		fieldid = document.frmForm.elements[iElements].name;
		typechar = fieldid.substring(0,2);
		if(fieldid != lastfieldid && (typechar == 'p_' || typechar == 'r_' || typechar == 'c_' || typechar == 't_' || typechar == 's_'))
		{
			//we do this because we do not want to validate the same field more then once
			thisfield = GetField(fieldid);
			if(typechar != 'p_'){
				temppageid = thisfield.pageid
				lastfieldid = fieldid
			}
			strTest = GetFormValue(document.frmForm.elements[iElements]);

			//check to see if it meets it requirement rule
			if(!isEmpty(thisfield.uniquematrixid))
			{
				if(!isValidUniqueMatrixTestForDuplicates(thisfield.uniquematrixid))
				{
					AddToErrorList(thisfield.pageid,thisfield.id,'Must be unique!',"");
					PassedValidation = false;
				}
			}
			if(isEmpty(strTest) && IsRequired(thisfield) && isEmpty(thisfield.uniquematrixid))
			{
				AddToErrorList(thisfield.pageid,thisfield.id,'Required Field!');
				PassedValidation = false;
			}
			else if(isEmpty(strTest) && IsRequired(thisfield) && !isEmpty(thisfield.uniquematrixid))
			{
				
				if(!isValidUniqueMatrix(thisfield.uniquematrixid))
				{
					AddToErrorList(thisfield.pageid,thisfield.id,'Required Field!',"");
					PassedValidation = false;
				}
			}
			if(!isEmpty(strTest))
			{
				if(strTest.length > 0 && strTest.length < thisfield.min)
				{
					AddToErrorList(thisfield.pageid,thisfield.id,'Field is too short!',"");
					PassedValidation = false;
				}
				if(!isEmpty(thisfield.format))
				{
					if(thisfield.format == "1")
					{	
						rtn = validDate(strTest);
						if(rtn == false)
						{
							PassedValidation = false;	
							AddToErrorList(thisfield.pageid,thisfield.id,'Invalid date!',"");
						}
					}	
					else if(thisfield.format == "2")
					{
						rtn = validTime(strTest);
						if(rtn == false)
						{
							PassedValidation = false;	
							AddToErrorList(thisfield.pageid,thisfield.id,'Invalid time!',"");
						}
					}
					else if(thisfield.format == "3")
					{
						rtn = validNumber(strTest);
						if(rtn == false)
						{
							PassedValidation = false;	
							AddToErrorList(thisfield.pageid,thisfield.id,'Invalid number!',"");
						}
					}
					else if(thisfield.format == "4")
					{
						rtn = validNumber(strTest);
						perttotal = perttotal + parseInt(strTest);
						if(rtn == false)
						{
							PassedValidation = false;	
							AddToErrorList(thisfield.pageid,thisfield.id,'Invalid number!',"");
						}
					}
					if(thisfield.format == "5")
					{	
						if(strTest.length < 10  || !validNumber(strTest))
						{
							AddToErrorList(thisfield.pageid,thisfield.id,'Invalid phone number!',"");
							PassedValidation = false;
						}
					}
					if(thisfield.format == "6")
					{	
						if(strTest.length < 9  || !validNumber(strTest))
						{
							AddToErrorList(thisfield.pageid,thisfield.id,'Invalid social security number!',"");
							PassedValidation = false;
						}
					}
					if(thisfield.format == "7")
						{
							rtn = validDecimalNumber(strTest);
							if(rtn == false)
							{
								AddToErrorList(thisfield.pageid,thisfield.id,'Invalid decimal number!',"");
								PassedValidation = false;
							}
						}	
				}					
				else if(typechar == 'p_')
				{
					if(perttotal != 100)
					{
						PassedValidation = false;	
						AddToErrorList(temppageid,lastfieldid,'Percentage must total 100%!',"");
					}
					perttotal=0;
				}
						
			}
		}						
	}
	return(PassedValidation);
}

function validDate(sDate)
{
var rtn = true;
	
	if(sDate.length != 10)
		rtn = false;
	else if(sDate.charAt(2) != "/")
		rtn = false;
	else if(sDate.charAt(5) != "/")
		rtn = false;
	else if(!validNumber(sDate.substring(0,2)))
		rtn = false;
	else if(!validNumber(sDate.substring(3,5)))
		rtn = false;
	else if(!validNumber(sDate.substring(6,10)))
		rtn = false;
	else if(Number(sDate.substring(0,2)) < 1 || Number(sDate.substring(0,2)) > 12)	
		rtn = false;
	else if(Number(sDate.substring(3,5)) < 1 || Number(sDate.substring(3,5)) > 31)	
		rtn = false;
	else if(Number(sDate.substring(6,10)) < 1 || Number(sDate.substring(6,10)) > 2035)	
		rtn = false;
		
	return(rtn);

}

function validTime(sTime)
{
var rtn = true;
	
	if(sTime.length != 5)
		rtn = false;
	else if(sTime.charAt(2) != ":")
		rtn = false;
	else if(!validNumber(sTime.substring(0,2)))
		rtn = false;
	else if(!validNumber(sTime.substring(3,5)))
		rtn = false;
	else if(Number(sTime.substring(0,2)) < 1 || Number(sTime.substring(0,2)) > 24)	
		rtn = false;
	else if(Number(sTime.substring(3,5)) < 0 || Number(sTime.substring(3,5)) > 59)	
		rtn = false;
		
	return(rtn);

}

function validNumber(sNumber)
{
var rtn = true;
var decimalcount = 0;
	for(var i=0;i<sNumber.length;i++)
	{
		var oneChar = sNumber.charAt(i);
		if(i == 0 && oneChar == "-")
		{
			//negative number
		}
		//else if(oneChar == "." && decimalcount==0)
		//{
		//	decimalcount++
		//	//decimal number
		//}
		else if(oneChar < "0" || oneChar > "9")
		{
			return(false);
		}
	}
	
	return(rtn);

}

function validDecimalNumber(sNumber)
{
var rtn = true;
var decimalcount = 0;
	for(var i=0;i<sNumber.length;i++)
	{
		var oneChar = sNumber.charAt(i);
		if(i == 0 && oneChar == "-")
		{
			//negative number
		}
		else if(oneChar == "." && decimalcount==0)
		{
			decimalcount++
		//decimal number
		}
		else if(oneChar < "0" || oneChar > "9")
		{
			return(false);
		}
	}
	
	if(decimalcount>1)
			return(false);
	
	return(rtn);

}


function IsRequired(thisfield)
{
var i;

	//switched optional and required 8-1-04 because was not working correct
	if(thisfield.optional.length > 0) return(false);//optional overrides default required setting
	if(thisfield.require.length > 0) return(true);//required overrides optional
	if(thisfield.IsRequired) return(true);
	
	return(false);

}
function GetFormValue(thefield)
{
// returns the value or for select,check,radio returns has value
var rtn="";
var iElements;
var typechar;
	
	typechar = thefield.name.substring(0,2);
	if(typechar == 't_')
		rtn = thefield.value;
	else if(typechar == 'p_')
		rtn = thefield.value;
	else if(typechar == 'h_'){}	//do nothing value static
	else if(typechar == 's_')
	{
		if(thefield.selectedIndex > 0)
			rtn = "HASVALUE";
		else //flip through family
		{
			for(iElements=0;iElements < document.frmForm.elements.length;iElements++)
			{	
				if(document.frmForm.elements[iElements].name.substring(2,document.frmForm.elements[iElements].name.length) == thefield.name.substring(2,thefield.name.length))
				{
					typechar = document.frmForm.elements[iElements].name.substring(0,2);
					if(typechar == 't_')
						rtn = document.frmForm.elements[iElements].value;
				}
				if(rtn.length > 0 ) 
					break;
			}
		}
	}
	else if(typechar == 'r_' || typechar == 'c_')
	{
		if(thefield.checked)
			rtn = "HASVALUE";
		else //flip through family
		{
			for(iElements=0;iElements < document.frmForm.elements.length;iElements++)
			{	
				if(document.frmForm.elements[iElements].name.substring(2,document.frmForm.elements[iElements].name.length) == thefield.name.substring(2,thefield.name.length))
				{
					typechar = document.frmForm.elements[iElements].name.substring(0,2);
					if(typechar == 't_')
						rtn = document.frmForm.elements[iElements].value;
					else if(document.frmForm.elements[iElements].checked)
						rtn = "HASVALUE";
				}
				if(rtn.length > 0 ) 
					break;
			}
		}
	}
	return(rtn);
				
}

function GetField(fieldid)
{
var iFields;
var rtn = new FieldType("","","","");

	rtn = field[fieldid];
	if(isEmpty(rtn))
		var rtn = new FieldType("","","","");
				
	return(rtn);		
			
}
		
function SetRuleArray(a,value,settype)
{
var i;

	if(settype)
	{	
		a[a.length] = value;
	}
	else
	{
		//remove one
		if(a.length >0) 
			a.length--;
		
		if(a.length == 0)
			settype=false;
				
	}
		
	return(settype);
}

function SetAsRequired(fieldruleid,fieldid,settype)
{
//field id should have no key
var thefield;

	thefield = GetField("r_" + fieldid);
	if(thefield.name.length > 0) SetRuleArray(thefield.require,fieldruleid,settype);

	thefield = GetField("c_" + fieldid);
	if(thefield.name.length > 0) SetRuleArray(thefield.require,fieldruleid,settype);

	thefield = GetField("t_" + fieldid);
	if(thefield.name.length > 0) SetRuleArray(thefield.require,fieldruleid,settype);

	thefield = GetField("s_" + fieldid);
	if(thefield.name.length > 0) SetRuleArray(thefield.require,fieldruleid,settype);
	
	return;
}
function SetAsOptional(fieldruleid,fieldid,settype)
{

	thefield = GetField("r_" + fieldid);
	if(thefield.name.length > 0) SetRuleArray(thefield.optional,fieldruleid,settype);

	thefield = GetField("c_" + fieldid);
	if(thefield.name.length > 0) SetRuleArray(thefield.optional,fieldruleid,settype);

	thefield = GetField("t_" + fieldid);
	if(thefield.name.length > 0) SetRuleArray(thefield.optional,fieldruleid,settype);

	thefield = GetField("s_" + fieldid);
	if(thefield.name.length > 0) SetRuleArray(thefield.optional,fieldruleid,settype);

	return;
}

function SetAsDisabled(fieldruleid,fieldid,settype)
{
var iElements;
var rtnsettype = settype;
var e;
var iBreak;

	thefield = GetField("r_" + fieldid);
	if(thefield.name.length > 0)
	{
		rtnsettype = SetRuleArray(thefield.disable,fieldruleid,settype);
		DoDisable(thefield.id,settype);
	}

	thefield = GetField("c_" + fieldid);
	if(thefield.name.length > 0)
	{
		rtnsettype = SetRuleArray(thefield.disable,fieldruleid,settype);
		DoDisable(thefield.id,settype);
	}

	thefield = GetField("t_" + fieldid);
	if(thefield.name.length > 0)
	{
		rtnsettype = SetRuleArray(thefield.disable,fieldruleid,settype);
		DoDisable(thefield.id,settype);
	}

	thefield = GetField("s_" + fieldid);
	if(thefield.name.length > 0)
	{
		rtnsettype = SetRuleArray(thefield.disable,fieldruleid,settype);
		DoDisable(thefield.id,settype);
	}
	
	iBreak=0;
	return;
	//below has been replaced by findobj
	for(iElements=0;iElements < document.frmForm.elements.length;iElements++)
	{
		e = document.frmForm.elements[iElements]
		
		if(e.name.substring(2,e.name.length) == fieldid)
		{
			iBreak=1;
			ShowDisabled(e,settype)
		}
		else if(iBreak==1)
		{
			iElements = document.frmForm.elements.length;
		}
		
	}
	return;
}

function findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}


function ShowDisabled(e,settype)
{

	if(isNavOld())
	{ //old netscape does not have this functionality 7-21-04
		return;
	}
	e.disabled = settype;
	if(settype)
		e.style.backgroundColor = 'e9e5e5';
	else
		e.style.backgroundColor = "";

}

function DoDisable(f,settype)
{
var e;
var i;

	if ((e=findObj(f))!=null)
	{
		if(e.length>1)
		{
			for(i=0;i<e.length;i++)
			{
				ShowDisabled(e[i],settype);
			}
		}
		else
			ShowDisabled(e,settype);
	}
	
}

function SetGoto(fieldid,gotourl,settype)
{
var sGoto ="";
var i;

	if(settype)
	{
		if(gotourl.length>1)
		{
			document.frmForm.gotoURL.value = gotourl;
			GotoList[fieldid] = gotourl;
		}
	}
	else
	{
		//remove from list
			
		GotoList[fieldid] = "";

		//if unset and value is next page then put back to default
		if(document.frmForm.gotoURL.value == gotourl)
		{
			for(i in GotoList)
			{
					
				if(!isEmpty(GotoList[i]))
				{
					sGoto = GotoList[i];
				}	
			}
				
			if(isEmpty(sGoto))
			{
				sGoto = document.frmForm.defaultgotoURL.value;
			}
			document.frmForm.gotoURL.value = sGoto
		}
	}
	return;
}

function SetNextPage(pageid,fieldid,nextpage,settype)
{
var iElements;
var i;
var sNextPage="";

//alert(nextpage + " is " + settype);
	if(pageid == document.frmForm.thispage.value)
	{
		if(settype)
		{
			if(!isEmpty(nextpage))
			{
				document.frmForm.nextpage.value = "/form/page[@id='" + nextpage + "']";
				//add to next page array
				NextPageList[fieldid] = "/form/page[@id='" + nextpage + "']";
			}
		}
		else
		{
			//remove from list
			
			NextPageList[fieldid] = "";
			
			//if this page is the set next page then we need to take the last from the list
			if(document.frmForm.nextpage.value == "/form/page[@id='" + nextpage + "']")
			{
					for(i in NextPageList)
					{
					
						if(!isEmpty(NextPageList[i]))
						{
							sNextPage = NextPageList[i];
						}	
					}
				
					if(isEmpty(sNextPage))
					{
						sNextPage = document.frmForm.defaultnextpage.value;
					}
						
					document.frmForm.nextpage.value = sNextPage;
			}

		}

			
	}
	return;

}

function SetRule(fieldid,testvalue,settype)
{
var thefield = GetField(fieldid);
var i;
var j;
var thistestvalue;
var thissettype;
var bThisFieldHasAny=false;
var bThisFieldHasEmpty=false;

	//alert("****** test value is [" + testvalue + "]  setto " + settype + "rules:" + thefield.rule.length +" *******");
	for(i=0;i<thefield.rule.length;i++)
	{
		//look for match
		thistestvalue = testvalue
		thissettype = settype
		//alert(thefield.rule[i].testvalue)
		if(thefield.rule[i].testvalue == 'Any Response Value')
		{
			//handle set type for this rule type
			thissettype = false
			thistestvalue = testvalue
			bThisFieldHasAny=true;
			
			if(!isEmpty(thistestvalue) || bThisFieldHasEmpty==true)
				thissettype = true
		}
		else if(thefield.rule[i].testvalue == 'Is Empty Response Value')
		{
			//for empty test
			//handle set type for this rule type
			//alert(testvalue + " SET: " + settype);
			bThisFieldHasEmpty=true;
			
			thistestvalue = testvalue
			if(settype && isEmpty(thistestvalue) || bThisFieldHasAny==true)
				thissettype = true;
			else if(settype && !isEmpty(thistestvalue))
			{
				thissettype = false; //reset
				thistestvalue="";
			}	
			else if(isEmpty(thistestvalue))
				thissettype=true; //this would be a testbox
			else
				thissettype=false;
				
			//alert(" Now SET: " + settype);	
		}

		//alert("rule is (" + thefield.rule[i].testvalue + ") test value is (" + thistestvalue + ")  setto " + thissettype);

		if(thefield.rule[i].testvalue == thistestvalue || thefield.rule[i].testvalue == 'Any Response Value' || (thefield.rule[i].testvalue == 'Is Empty Response Value' && isEmpty(thistestvalue)))
		{
			//for empty test
			//now est rules 
			for(j=0;j<thefield.rule[i].disable.length;j++)
			{
				SetAsDisabled(fieldid,thefield.rule[i].disable[j],thissettype);
			}
			for(j=0;j<thefield.rule[i].optional.length;j++)
			{
				SetAsOptional(fieldid,thefield.rule[i].optional[j],thissettype);
			}
			for(j=0;j<thefield.rule[i].require.length;j++)
			{
				SetAsRequired(fieldid,thefield.rule[i].require[j],thissettype);
			}
			
			SetGoto(fieldid,thefield.rule[i].gotourl,thissettype);
			SetNextPage(thefield.pageid,fieldid,thefield.rule[i].gotopage,thissettype);
			
		}
	}
	
	return;

}	

function ApplyRules()
{
var iFields;
var i;

	for(iFields in field)
	{
		if(isEmpty(field[iFields].fieldvalue.length))
		{
			//do not invoke the empty rules when loading only when they come to a page because default values do not work then
			//SetRule(field[iFields].id,"",true); //for empty test
		}
		//alert(field[iFields].fieldvalue.length);
		for(i=0;i<field[iFields].fieldvalue.length;i++)
		{
			SetRule(field[iFields].id,field[iFields].fieldvalue[i],true);
		}
	}			

}
function FieldRule(testvalue,gotourl,gotopage)
{
				
	this.testvalue =testvalue;
	this.gotourl =gotourl;
	this.gotopage = gotopage;
	this.disable = new Array();				
	this.optional = new Array();
	this.require = new Array();
			
}
function FieldType(pageid,name,id,min,req,textformat,uniquematrixid)
{
			
	this.pageid = pageid;		
	this.name = name;
	this.id = id;
	this.min = min;
	this.format = textformat;
	
	this.uniquematrixid = uniquematrixid;
	
	this.require = new Array();
	this.disable = new Array();
	this.optional = new Array();
	
	if(req == 'true')
		this.IsRequired = true;
	else
		this.IsRequired = false;
		
	this.rule = new Array();
	this.fieldvalue = new Array();
	//set field value initially to blank allows for is empty rule to work on load
	this.fieldvalue[0] = '';
	
}

function LoadSavedValues()
{
var iElements;
var iFields;
var typechar;
var ioptions;
var thefield;
var fieldid;
	
	for(iElements=0;iElements < document.frmForm.elements.length;iElements++)
	{
		typechar = document.frmForm.elements[iElements].name.substring(0,2);
		fieldid = document.frmForm.elements[iElements].name.substring(2,document.frmForm.elements[iElements].name.length);

		thefield = GetField(document.frmForm.elements[iElements].name);
		
		if(thefield.name.length > 0) 
		{
			for(iFields=0;iFields < thefield.fieldvalue.length;iFields++)
			{	
			
				if(typechar == 't_')
				{
					document.frmForm.elements[iElements].value = thefield.fieldvalue[iFields];
					SetRule(thefield.id,thefield.fieldvalue[iFields],true);
					if(thefield.format == '4')
					{
						document.frmForm.elements[iElements].fireEvent("onChange",event);
					}
					if(thefield.format == '5')
					{
						document.frmForm.elements['areacode'+fieldid].value = thefield.fieldvalue[iFields].substring(0,3);
						document.frmForm.elements['prefix'+fieldid].value = thefield.fieldvalue[iFields].substring(3,6);
						document.frmForm.elements['sufix'+fieldid].value = thefield.fieldvalue[iFields].substring(6,10);
					}
					else if(thefield.format == '6')
					{
						document.frmForm.elements['prefix'+fieldid].value = thefield.fieldvalue[iFields].substring(0,3);
						document.frmForm.elements['middle'+fieldid].value = thefield.fieldvalue[iFields].substring(3,5);
						document.frmForm.elements['sufix'+fieldid].value = thefield.fieldvalue[iFields].substring(5,9);
					}
				}
				else if(typechar == 'h_')
				{
					document.frmForm.elements[iElements].value = thefield.fieldvalue[iFields];
					SetRule(thefield.id,thefield.fieldvalue[iFields],true);
				}
				else if(typechar == 's_')
				{
					for(ioptions=0;ioptions < document.frmForm.elements[iElements].options.length;ioptions++)
					{
						if(thefield.fieldvalue[iFields] == document.frmForm.elements[iElements].options[ioptions].value && !isEmpty(document.frmForm.elements[iElements].options[ioptions].value))
						{
							document.frmForm.elements[iElements].options[ioptions].selected=true;
							SetRule(thefield.id,thefield.fieldvalue[iFields],true);
						}
					}
				}
				else //r_,c_
				{
					if(thefield.fieldvalue[iFields] == document.frmForm.elements[iElements].value)
					{
						document.frmForm.elements[iElements].checked=true;
						SetRule(thefield.id,thefield.fieldvalue[iFields],true);
					}
				}
			}
		}
	}

}		

function debugalert(str)
{
//		debugalert(field[iFields].id + " == " + fieldid);

	if(!confirm(str)) kill_this;

}

function isNav() {
	return (navigator.appName == "Netscape")
}

function isIE() {
	return (navigator.appName == "Microsoft Internet Explorer")
}

// operating system platforms
function isWindows() {
	return (navigator.appVersion.indexOf("Win") != -1)
}

function isWin95NT() {
	return (isWindows() && (navigator.appVersion.indexOf("Win16") == -1 && navigator.appVersion.indexOf("Windows 3.1") == -1))
}

function isMac() {
	return (navigator.appVersion.indexOf("Mac") != -1)
}

function isMacPPC() {
	return (isMac() && (navigator.appVersion.indexOf("PPC") != -1 || navigator.appVersion.indexOf("PowerPC") != -1))
}

function isUnix() {
	return (navigator.appVersion.indexOf("X11") != -1)
}

// browser versions
function isGeneration2() {
	return (parseInt(navigator.appVersion) == 2)
}

function isGeneration3() {
	return (parseInt(navigator.appVersion) == 3)
}

function isGeneration3Min() {
	return (parseInt(navigator.appVersion.charAt(0)) >= 3)
}

function isNavOld() {
	return (isNav() && parseFloat(navigator.appVersion) < 5)
}

function isNav4_7() {
	return (isNav() && parseFloat(navigator.appVersion) == 4.7)
}

function isMSIE4Min() {
	return (isIE() && navigator.appVersion.indexOf("MSIE") != -1)
}

function isMSIE5_5() {
	return (navigator.appVersion.indexOf("MSIE 5.5") != -1)
}

function isNN6Min() {
	return (isNav() && parseInt(navigator.appVersion) >= 5)
}


//
//  Cookie Functions -- "Night of the Living Cookie" Version (25-Jul-96)
//
//  Written by:  Bill Dortch, hIdaho Design <bdortch@hidaho.com>
//  The following functions are released to the public domain.
//
//  This version takes a more aggressive approach to deleting
//  cookies.  Previous versions set the expiration date to one
//  millisecond prior to the current time; however, this method
//  did not work in Netscape 2.02 (though it does in earlier and
//  later versions), resulting in "zombie" cookies that would not
//  die.  DeleteCookie now sets the expiration date to the earliest
//  usable date (one second into 1970), and sets the cookie's value
//  to null for good measure.
//
//  Also, this version adds optional path and domain parameters to
//  the DeleteCookie function.  If you specify a path and/or domain
//  when creating (setting) a cookie**, you must specify the same
//  path/domain when deleting it, or deletion will not occur.
//
//  The FixCookieDate function must now be called explicitly to
//  correct for the 2.x Mac date bug.  This function should be
//  called *once* after a Date object is created and before it
//  is passed (as an expiration date) to SetCookie.  Because the
//  Mac date bug affects all dates, not just those passed to
//  SetCookie, you might want to make it a habit to call
//  FixCookieDate any time you create a new Date object:
//
//    var theDate = new Date();
//    FixCookieDate (theDate);
//
//  Calling FixCookieDate has no effect on platforms other than
//  the Mac, so there is no need to determine the user's platform
//  prior to calling it.
//
//  This version also incorporates several minor coding improvements.
//
//  **Note that it is possible to set multiple cookies with the same
//  name but different (nested) paths.  For example:
//
//    SetCookie ("color","red",null,"/outer");
//    SetCookie ("color","blue",null,"/outer/inner");
//
//  However, GetCookie cannot distinguish between these and will return
//  the first cookie that matches a given name.  It is therefore
//  recommended that you *not* use the same name for cookies with
//  different paths.  (Bear in mind that there is *always* a path
//  associated with a cookie; if you don't explicitly specify one,
//  the path of the setting document is used.)
//  
//  Revision History:
//
//    "Toss Your Cookies" Version (22-Mar-96)
//      - Added FixCookieDate() function to correct for Mac date bug
//
//    "Second Helping" Version (21-Jan-96)
//      - Added path, domain and secure parameters to SetCookie
//      - Replaced home-rolled encode/decode functions with Netscape's
//        new (then) escape and unescape functions
//
//    "Free Cookies" Version (December 95)
//
//
//  For information on the significance of cookie parameters, 
//  and on cookies in general, please refer to the official cookie
//  spec, at:
//
//      http://www.netscape.com/newsref/std/cookie_spec.html    
//
//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid. If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function MM_findObj(n, d)
{
  var p,i,x;  
  
	if(isNN6Min())
		return document.getElementById(n);

  if(!d) d=document; 
  
  if((p=n.indexOf("?"))>0&&parent.frames.length)
  {
		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
  
  if(!(x=d[n])&&d.all) 
		x=d.all[n]; 
	
	for (i=0;!x&&i<d.forms.length;i++) 
		x=d.forms[i][n];
  
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=MM_findObj(n,d.layers[i].document); 
		
	return x;
	
}		
function MM_swapImage()
{
  var i,j=0,x,a=MM_swapImage.arguments; 
  
  document.MM_sr=new Array; 
  
  for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null)
   {
		document.MM_sr[j++]=x; 
		if(!x.oSrc) 
			x.oSrc=x.src; 
			x.src=a[i+2];
		}
}
function DoImageButtonTest(btnname)
{
alert(btnname);
alert(window.document.frmForm.r_EFNJXH[1].value);
var btn = findObj(btnname)
alert(window.document.frmForm.r_EFNJXH[1].name);
	alert(btn[1].name);

  //MM_swapImage(currentSwatch.name ,'',imgsrc,1);
  
}
function DoImageButton(btnname,imgname,index,imgcheckedsrc,imgnotcheckedsrc,btype)
{
	//return;

var btn = findObj(btnname)
var img = findObj(imgname)

	//alert(btn);

	btn[index].checked=!btn[index].checked;
	if(btn[index].checked)
	{
		img[index].src=imgcheckedsrc;
	}
	else
	{
		img[index].src=imgnotcheckedsrc;
	}
	
	if(btype=='r')
	{
		for(var i=0;i<img.length;i++)
		{
			if(i != index)
				img[i].src=imgnotcheckedsrc;
		}
	}				
}

function ChangeThisImageButton(btnname,imgname,index,imgcheckedsrc,imgnotcheckedsrc,btype)
{
	//return;

var btn = findObj(btnname)
var img = findObj(imgname)

	//alert(btn);

	if(btn[index].checked)
	{
		img[index].src=imgcheckedsrc;
	}
	else
	{
		img[index].src=imgnotcheckedsrc;
	}

}