// -------------------------------------------------------------------------------------------------
// Package       : formChecks.js
// Path          : #request.webRoot#/_js/formChecks.js
// Beschreibung  : form validations
// History       : C. Schmitz, 2003-01-21, file created
// Functions	   : trim()
//								 checkRequired()
// CVSId         : $Id: $
// -------------------------------------------------------------------------------------------------

// -------------------------------------------------------------------------------------------------
// Function     : trim()
// Description	: cuts off leading and trailing spaces
// History      : C. Schmitz, 2003-01-21, function created
// Parameters   : thisString [string] - string to be trimmed
// Return       : thisString [string] - trimmed string
// -------------------------------------------------------------------------------------------------
function trim(thisString)
{
	// while first char is space: cut off
	while(thisString.length>0 && thisString.charCodeAt(0)==32)
			thisString=thisString.substring(1, thisString.length);

	// while last char is space: cut off
	while(thisString.length>0 && thisString.charCodeAt(thisString.length-1)==32)
			thisString=thisString.substring(0,thisString.length-1);

	// return trimmed string
	return thisString;
}

// -------------------------------------------------------------------------------------------------
// Function     : checkRequired()
// Description  : checks a required form field
// History      : C. Schmitz, 2003-01-31, function created
// Parameters   : field [object] - form field to check
//								dummyValue [optional] - for a select field value of dummy option
// Return       : boolean true (field has value) or false (field has no value)
// -------------------------------------------------------------------------------------------------
function checkRequired(field)
{
	// we assume that there will be no error
	var flag=true;

	// if more than 1 argument has been passed, it must be the dummy value for select boxes
	if(arguments.length > 1)
		dummyValue=arguments[1];
	else
		dummyValue=null;
		
	
	// test the type of the passed form field
	switch(field.type)
	{
		// text, password, file, textarea fields: if field contains something: error
		case "text":
		case "password":
		case "file":
		case "textarea":
			if(trim(field.value)=="")
				flag=false;
				break
			
		// single select: if nothing selected or selected option has dummy value: error
		case "select-one":
			if(field.selectedIndex<0 || field.options[field.selectedIndex].value==dummyValue)
				flag=false;
				break;
				
		// multiple select: if nothing selected: error
		case "select-multiple":
			if(field.selectedIndex<0)
				flag=false;
			else
			{
				// something selected: could be dummy value: assume error
				flag=false;
				for(i=0; i<field.options.length; i++)
				{
					// if 1 selected option has not dummy value: no error
					if(field.options[i].selected && field.options[i].value != dummyValue)
					{
						flag=true;
						break;
					}
				}
			}
			break;

		// remaining types: checkbox and radio button
		default:
			// test type of first sub-element 
			// only added in case we forgot one type that has to be added later
			switch(field[0].type)
			{
		
				case "checkbox":
				case "radio":
					flag=false;
					for(i=0; i<field.length; i++)
					{
						if(field[i].checked)
						{
							flag=true;
							break;
						}
					}
					break;
			}
		break;
	}
		
	return flag;
}
