﻿// JScript File

function validate(suffix)
{
	var args = validate.arguments
	var nargs = args.length
	var reversechk = false
	var aform = document.forms[0]
	
	// badcolor 
	if ( nargs > 1 )
	{
		badcolor = args[1]
	}
	// goodcolor 
	if ( nargs > 2 )
	{
		goodcolor = args[2]
	}
	// reverse chk 
	if ( nargs > 3 )
	{
		reversechk = args[3]
	}
	// form 
	if ( nargs > 4 )
	{
		aformstr = 'document.forms["' + args[4] + '"]'
		aform = eval(aformstr)
	}
	
	 var alist = ''
	 var typelist = ''
	// Get form elements
	var eles = aform.elements
	var neles = eles.length
		var checkabletypes = 'text,radio,hidden,select-one,password,textarea'
	// parse each element
	for ( var ii = 0 ; ii < neles ; ii++ )
	{
		var anele = eles[ii]	// get the element
		var eletype = anele.type // get element type
		var eleid = anele.id // get element id
		var t = checkabletypes.indexOf(eletype)
		typelist += ii + '  ' + eletype + ' : ' + eleid + '  ' + t  +'\n'
		if ( t == -1 ) { continue } // we cannot check this
		var x = eleid.indexOf(suffix) // test if we should check it
		if ( x != -1 ) // yes, we should check it
		{
			if ( ( eletype == 'text' ) || ( eletype == 'radio' ) || ( eletype == 'password' ) || (eletype == 'textarea' ) )
			{
				var val = anele.value
				val = trim( val,'both' )
				anele = document.getElementById(eleid)
				if ( val == '' )
				{
					anele.style.backgroundColor = badcolor
					alist += eleid + ' : ' + eletype + ' is empty \n'
				}
				else
				{
					anele.style.backgroundColor = goodcolor
				}
			}
			else if ( eletype == 'select-one' ) // check selected index for > 0
			{
				//alert( 'found a select box')
				var selx = anele.selectedIndex
				if ( selx < 1 )
				{
					anele.style.backgroundColor = badcolor
					alist += eleid + ' : ' + eletype + ' No choice was made \n'
				}
				else
				{
					anele.style.backgroundColor = goodcolor
				}
			}
			else if ( eletype == 'radio' ) // make sure that one is selected
			{
				// Let's not go there yet; this is tricky and I usually use a
				// text box so I don't have to do this.
			}
		}
		else
		{
			continue	// Not to be checked
		}
	}
	//alert( typelist )
	//alert( alist )
	if ( alist == '' )
	{
		return true
	}
	else
	{
		return false
	}
}	
