// -------------------------------------------------------------------------------------------------
// Package       : formChecks.js
// Path          : #request.webRoot#/_js/formChecks.js
// Beschreibung  : form validations
// History       : C. Schmitz, 2003-01-21, file created
// Functions	   : trim()
//								 checkRequired()
//								 checkMinLen()
//								 checkMaxLen()
//								 checkValidDate()
//								 checkInteger()
// 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(myString)
{
	return myString.replace(/^\s+|\s+$/gi, '');
}

// -------------------------------------------------------------------------------------------------
// 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)
{
	// 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":
		case "hidden":
			if(trim(field.value)=="" || trim(field.value) == dummyValue)
				return 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)
				return false;
				break;
				
		// multiple select: if nothing selected: error
		case "select-multiple":
			if(field.selectedIndex<0)
				return 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;
					}
				}
			return flag;
			}
			break;

		
		case "checkbox":
		case "radio":
			if(field.checked && field.value != dummyValue)
				return true;
			else
				return false;
			break;
			
		// remaining types: multiple checkbox and radio buttons are arrays
		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 && field[i].value != dummyValue)
						{
							flag=true;
							break;
						}
					}
					return flag;
					break;
			}
		break;
	}
	return true;
}


// -------------------------------------------------------------------------------------------------
// Function     : checkMinLen()
// Description  : checks a form field for required minimum length
// History      : C. Schmitz, 2003-01-31, function created
// Parameters   : field [object] - form field to check
//								minLen [number] - required minimum length
// Return       : boolean true or false
// -------------------------------------------------------------------------------------------------
function checkMinLen(field, minLen)
{
	// if more than 2 arguments have been passed, it must be the dummy value for select boxes
	if(arguments.length > 2)
		dummyValue=arguments[1];
	else
		dummyValue=null;

	// check the field type
	switch(field.type)
	{
		// text, password, file, textarea: check length of input
		// compare input to dummy value
		case "text":
		case "password":
		case "file":
		case "textarea":
			if(trim(field.value).length < minLen || trim(field.value) == dummyValue)
				return false;
			else
				return true;
			break;
			
		// multiple select: count number of selected options 
		// don't count dummy value option
		case "multi-select":
			var counter=0;
			for(i=0;i<field.options.length;i++)
				if(field.options[i].selected && field.options[i].value != dummyValue)
					counter++;
					
			if(counter<minLen)
				return false;
			break;
			
		default:
			// we have an array, test the type of the first element
			switch(field[0].type)
			{
				// if it's a checkbox, count checked elements that don't have dummy value
				case "checkbox":
					var counter=0;
					for(i=0; i<field.length; i++)
					{
						if(field[i].checked && field[i].value != dummyValue)
							counter++;
					}		
					if(counter<minLen)
						return false;
					break;
					
			}
	}
	return true;
}

// -------------------------------------------------------------------------------------------------
// Function     : checkMaxLen()
// Description  : checks a form field for allowed maimum length
// History      : C. Schmitz, 2003-01-31, function created
// Parameters   : field [object] - form field to check
//								minLen [number] - allowed maximum length
// Return       : boolean true or false
// -------------------------------------------------------------------------------------------------
function checkMaxLen(field, maxLen)
{
	// if more than 2 arguments have been passed, it must be the dummy value for select boxes
	if(arguments.length > 2)
		dummyValue=arguments[1];
	else
		dummyValue=null;

	// test the field type
	switch(field.type)
	{
		// text, password, file, textarea: if input lenth > maxLen: error
		case "text":
		case "password":
		case "file":
		case "textarea":
			if(trim(field.value).length > maxLen || trim(field.value) == dummyValue)
				return false;
				break;
			
		// multiple select: if more than allowed numer of options selected: error
		case "select-multiple":
			var counter=0;
			for(i=0; i<field.options.length; i++)
				if(field.options[i].selected && field.options[i].value != dummyValue)
					counter++;
					
			if(counter>maxlen)
				return false;
				
			break;
			
		case "checkbox":
			if(!field.checked)
				return false;
				
			break;
			
		// the remaining type is checkbox
		default:
			switch(field[0].type)
			{
				// count the checked options that have not dummy value
				// if too many options checked: error
				case "checkbox":
					var counter=0;
					for(i=0; i<field.length; i++)
					{
						if(field[i].checked && field[i].value != dummyValue)
							counter++;
					}
					if(counter>maxLen)
						return false;
					break;
			}
	}
	return true;
}


// -------------------------------------------------------------------------------------------------
// Function     : checkValidDate()
// Description  : checks if a date is valid
// History      : C. Schmitz, 2003-02-04, function created
// 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
// History      : C. Schmitz, 2003-02-04, function created
// Parameters   : field [object]
// Return       : boolean true or false
// -------------------------------------------------------------------------------------------------
function checkInteger(field)
{
	reg = /^[0-9]+$/g;
	
	return reg.test(field.value);
}


function checkEmail(field)
{
	// reg = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/gi;
	var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return reg.test(trim(field.value));
}
