
// -------------------------------------------------------------------------------------------------
// Function     : trim()
// Description	: cuts off leading and trailing spaces
// 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
// 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;
}


// -------------------------------------------------------------------------------------------------
// Function     : checkMinLen()
// Description  : checks a form field for required minimum length
// Parameters   : field [object] - form field to check
//								minLen [number] - required minimum length
// Return       : boolean true or false
// -------------------------------------------------------------------------------------------------
function checkMinLen(field, minLen)
{
	if(trim(field.value).length < minLen)
		return false;
	else
		return true;
}

// -------------------------------------------------------------------------------------------------
// Function     : checkMinLen()
// Description  : checks a form field for allowed maimum length
// Parameters   : field [object] - form field to check
//								minLen [number] - allowed maximum length
// Return       : boolean true or false
// -------------------------------------------------------------------------------------------------
function checkMaxLen(field, maxLen)
{
	if(trim(field.value).length > maxLen)
		return false;
	else
		return true;
}


// -------------------------------------------------------------------------------------------------
// Function     : checkValidDate()
// Description  : checks if a date is valid
// Parameters   : day [number] - day
//								month [number] - month
//								year [number] - year
// Return       : boolean true or false
// -------------------------------------------------------------------------------------------------
function checkValidDate(day, month, year)
{
	var flag=true;
	// fif year < 1900: add 1900 
	if(year.length && !isNaN(year) && year < 1900)
			year+=1900;

	// if day, month, or year are not empty
	if(day.length || month.length || year.length)
	{
		// if one of the values is numeric
		if(!isNaN(day) || !isNaN(month) || !isNaN(year))
		{
			// create test date
			var testDate = new Date(year, month-1, day);
			if(testDate!='undefined')
			{
				// if year of test date is < 1900: add 1900
			 	testYear=testDate.getYear();
				if(testYear!='undefined' && testYear < 1900)
					testYear+=1900;
			}
			// if test date is not valid or not the same as passed date : error
			if(testDate=='undefined' || testYear != year || testDate.getMonth()+1 != month || testDate.getDate() != day)
				flag = false;
						
		}
		else // all fields are non-numeric: error
			flag = false;
	}
	
	return flag;
}

// -------------------------------------------------------------------------------------------------
// Function     : checkInteger()
// Description  : checks if a field has an integer value
// Parameters   : field [object]
// Return       : boolean true or false
// -------------------------------------------------------------------------------------------------
function checkInteger(field)
{
	reg = /^[0-9]+$/g;
	
	return reg.test(field.value);
}
