//*******************
//**
//** JavScripts - Validate Form on Submit
//**
//**  Purpose - Validates the fields in a form before it is 
//**            posted to the server 
//**
//*******************

var message = '';
var custom = 'N';

//*******************
//** Function - RequiredCheck
//**            
//** Checks to see if the form element contains data
//**
//*******************

function RequiredCheck(eobj, eid)
{
/*	alert("Entering RequiredCheck Function");  */

    estring = rtrim(eobj.value);
	if (estring.length == 0)	
	{
	  if (message == '')
	    eobj.focus();

	  message = message + '\n  * ' + eid + ' is required.';
	}  
}


//*******************
//** Function - RequiredCheckNonZero
//**            
//** Checks to see if the form element contains data
//**
//*******************

function RequiredCheckNonZero(eobj, eid)
{
/*	alert("Entering RequiredCheckNonZero Function");  */

	for (j=0; j < eobj.value.length; j++) 
		if ((eobj.value.substring(j, j+1) != '.')
		&& (eobj.value.substring(j, j+1) != ',') 
		&& (eobj.value.substring(j, j+1) != '$') 
		&& (eobj.value.substring(j, j+1) != '0'))
		return;

	if (message == '')
	    eobj.focus();

	message = message + '\n  * ' + eid + ' is required to be non-zero value.';
}


//*******************
//** Function - ValidateForm
//**            
//** Loops through the form elements applying validation checks
//** where requested and if errors found displaying all the errors
//** and stopping the form from being posted.
//**
//*******************

function ValidateForm(formname) {
	
 /*	alert("Entering ValidateForm Function");    */
	
	if (custom == 'N')
	    message = '';

	newwinreq = 'no';

	var e = formname.elements
	
	for(var i = 0; i < e.length; i++) 
	{
	    funcfnd = 0
			
		if (e[i].type == "hidden") 
		{	
			for (j=0; j < e[i].name.length; j++) 
			{
				if (e[i].name.substring(j, j+2) == '_*')
				{
					x=j+2;
					funcfnd = 1;
					break;
				}	
			}
		}

		if (funcfnd == 1)
		{

		  FieldName =  e[i].name.substring(0, x-2)
		  FunctionName = e[i].name.substring(x, e[i].name.length)

		  switch(FunctionName) {
			
		  	case 'required':
/*				alert("Calling RequiredCheck Function");  */
				RequiredCheck(formname[FieldName], e[i].value);
				break;

		  	case 'requirednonzero':
/*				alert("Calling RequiredCheckNonZero Function");   */
				RequiredCheckNonZero(formname[FieldName], e[i].value);
				break;
				
			case 'integer':
/*				alert("Calling IntegerCheck Function no commas allowed - Include IntegerCheck.scrp");  */
				IntegerCheck(formname[FieldName], e[i].value, (formname[FieldName].value.length+1), false, false);
				break;

			case 'intwcom':
/*				alert("Calling IntegerCheck Function with commas allowed - Include IntegerCheck.scrp");  */
				for (j=FieldName.length-1; j > 0; j--) 
					if (FieldName.substring(j, j+1) == '_')
					{
						x=j+2;
						j--;
						break;
					}	
				FieldName1 =  FieldName.substring(0, x-2);
				Pos =  FieldName.substring(x-1, FieldName.length);
				IntegerCheck(formname[FieldName1], e[i].value, Pos, true, false);
				break;
				
			case 'date':
/* 				alert("Calling DateCheck Function");    */   
				DateCheck(formname[FieldName], e[i].value);
				break;

			case 'time':
/*				alert("Calling TimeCheck Function - Include TimeCheck.scrp");  */
				TimeCheck(formname[FieldName], e[i].value);
				break;

			case 'float':
/*				alert("Calling FloatCheck Function - Include FloatCheck.scrp");  */
				FloatCheck(formname[FieldName], e[i].value);
				break;

			case 'money':
			case 'moneysign':
/*				alert("Calling MoneyCheck Function no sign / sign allowed - Include MoneyCheck.scrp");  */
				for (j=FieldName.length-1; j > 0; j--) 
					if (FieldName.substring(j, j+1) == '_')
					{
						x=j+2;
						j--;
						break;
					}	
				for (; j > 0; j--) 
					if (FieldName.substring(j, j+1) == '_')
					{
						y=j+2;
						break;
					}	
				FieldName1 =  FieldName.substring(0, y-2);
				LPos =  parseInt(FieldName.substring(y-1, x-2));
				RPos =  parseInt(FieldName.substring(x-1, FieldName.length));

				if (FunctionName == "moneysign")
					MoneyCheck(formname[FieldName1], e[i].value, LPos, RPos, 1);
				else
					MoneyCheck(formname[FieldName1], e[i].value, LPos, RPos, 0);
				break;
				
				
			case 'ABA':
/*				alert("Calling ABACheck Function - Include ABACheck.scrp");  */
				ABACheck(formname[FieldName], e[i].value);
				break;

			case 'datecompare':
/*				alert("Calling DateCompare Function - Include DateCompare.scrp");  */
				for (j=0; j < FieldName.length; j++) 
					if (FieldName.substring(j, j+1) == '_')
					{
						x=j+2;
						break;
					}	
				FieldName1 =  FieldName.substring(0, x-2);
				FieldName2 =  FieldName.substring(x-1, FieldName.length);
				DateCompare(formname[FieldName1], formname[FieldName2], e[i].value);
				break;

			case 'datediff':
/*				alert("Calling DateDiff Function - Include DateDiff.scrp");  */
				for (j=0; j < FieldName.length; j++) 
					if (FieldName.substring(j, j+1) == '_')
					{
						x=j+2;
						break;
					}	
				FieldName1 =  FieldName.substring(0, x-2);
				FieldName2 =  FieldName.substring(x-1, FieldName.length);


				for (j=0; j < FieldName2.length; j++) 
					if (FieldName2.substring(j, j+1) == '_')
					{
						x=j+2;
						break;
					}	

				max =  FieldName2.substring(x-1, FieldName2.length);
				FieldName2 =  FieldName2.substring(0, x-2);

				
				DateDiff(formname[FieldName1], formname[FieldName2], max, e[i].value);
				break;

			case 'text':
/*				alert("Calling TextCheck Function - Include TextCheck.scrp");  */
				for (j=FieldName.length-1; j > 0; j--) 
					if (FieldName.substring(j, j+1) == '_')
					{
						x=j+2;
						j--;
						break;
					}	
				FieldName1 =  FieldName.substring(0, x-2);
				minlen = e[i].length;	
				charset =  FieldName.substring(x-1, FieldName.length);
			
				for (k=x-1; k < FieldName.length; k++)
				{
					if (FieldName.substring(k,k+1) == 'L')
					{
						minlen = FieldName.substring(k+1, FieldName.length);
						charset = FieldName.substring(x-1, k);
						break;
					}
				}
				TextCheck(formname[FieldName1], e[i].value, charset, minlen);
				break;
				
			case 'textarea':
/*				alert("Calling TextCheckArea Function - Include TextAreaCheck.scrp");  */
				for (j=FieldName.length-1; j > 0; j--) 
					if (FieldName.substring(j, j+1) == '_')
					{
						x=j+2;
						j--;
						break;
					}	
				for (; j>0; j--) 
					if (FieldName.substring(j, j+1) == '_')
					{
						y=j+2;
						break;
					}	
				FieldName1 =  FieldName.substring(0, y-2);
				minlen =  parseInt(FieldName.substring(y-1, x-2));
				maxlen =  parseInt(FieldName.substring(x-1, FieldName.length));

				charset =  FieldName.substring(x-1, FieldName.length);
				
				TextAreaCheck(formname[FieldName1], e[i].value, minlen, maxlen);
				break;
		  }
	     }
	}

	if (message != '') {
		message = 'This form has errors.  Please correct and resubmit.' + '\n' + message ;
		alert(message);
		return (false);
	}
	else {
/*	  alert("Leaving ValidateForm Function");   */
		return (true);
	}
}


//*******************
//** Function - rtrim
//**
//** removes trailing spaces from a string.
//**
//*******************
function rtrim(arg)
{

while(''+arg.charAt(arg.length-1)==' ')
	arg=arg.substring(0,arg.length-1);

return arg
}	
