<!-- Stealth Mode On
//Javascript form validation functions. 
//validateInput V1.0
//Copyright MouseMat Systems
//
//
//ABOUT.
//
//These routines are called from a forms onSubmit event and test for 
//appropriate data values in corresponding fields, if any of the values are 
//missing or of the wrong type the user is prompted and submission halted.
//
//The actual validation functions are intentionally simple, the purpose of
//this code is to provide a generic form validation handler that will work
//in MSIE 3.0x/4.x and NS Navigator 3.x and Communicator.
//
//The handler function - validateInput() accepts two parameters, the name of
//the form to be processed and a string that provides a mask against which 
//each field on the named form can be validated.
//
//This mask is the key to the functionality of this script. For each field on the form
//the corresponding character in the mask string tells the handler how to perform the 
//validation.
//
//The mask string can contain the following characters;
//
//	d = validate value as a date
//	t = validate value as text
//	n = validate value as a whole number (1234567890)
//	p = validate value as a phone number (+,(), 1234567890)
//	e = validate value as an e-mail address (@, .)
//	u = unrequired - do not validate
//
//The function can be used to process multiple forms on a single page, 
//this is useful for ASP developers who commonly generate input and
//confirmation forms within a single .asp file.
//
//----------------------------------------------------------------------
//
//HOW TO USE.
//
//To use the script in a web page simply insert the JavaScript
//section of Default.htm between the <HEAD>... </HEAD> section of your 
//page, then include the following code in the <FORM> tag;
//
//	 onSubmit="return validateInput(formname, 'maskstring')"
//
//where "formname" is the name of the form and "maskstring" is any 
//combination of the above characters. 
//
//NB. The number of characters in the string must be the same as 
//the number of form elements (including select lists, checkboxes 
//and radio buttons) on the form.
//
//An example <FORM> tag should read;
//
//<FORM  NAME="testfrm" METHOD="get" ACTION="success.htm" 
//	onSubmit="return validateInput(testfrm, 'duttnpeuu')">
//
//The mask string in this case covers 9 fields on a form.
//
//	1. d = date
//	2. u = unrequired
//	3. t = text
//	4. t = text
//	5. n = number
//	6. p = phone number
//	7. e = e-mail
//	8. u = unrequired
//	9. u = unrequired
//

function validateInput(frm, mask){

	//Count the number of form elements
	var FrmLen = frm.elements.length;
 
	//Get the validation mask string
	var maskarr = mask;
	
	//Loop through all the form elements
	for (var i = 0; i < FrmLen; i++)
	{
		//obtain the validation mask character
		maskarr = mask.substring(i,i+1)

		//Initialise local variables
		var valdate = 1;
		var valtext = 1;
		var valnum = 1;
		var valtelenum = 1;
		var valemail = 1;

	//Test field against validation mask character
	if (maskarr == "d")
	{
		valdate = validateDate(frm.elements[i].value);
	}
	if (maskarr == "t")
	{
		valtext = validateText(frm.elements[i].value);
	}
	if (maskarr == "n")
	{
		valnum = validateNum(frm.elements[i].value);
	}
	if (maskarr == "p")
	{
		valtelenum = validateTeleNum(frm.elements[i].value);
	}
	if (maskarr == "e")
	{
		valemail = validateEmail(frm.elements[i].value);
	}

	//If the validation fails prompt the user
	if (! valdate)
	{
		alert("Bitte geben Sie ein gültiges Datum in der Form 01.01.2000 ein");
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valtext)
	{
		alert("Die von Ihnen angegebenen Informationen sind unvollständig");
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valnum)
	{
		alert("Bitte geben Sie ein Zahl an.");
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valtelenum)
	{
		alert("Bitte geben Sie eine gültige Telefon- oder Durchwahlnummer an");
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valemail)
	{
		alert("Die von Ihnen eingegebene eMail Adresse ist ungültig");
		putFocus(frm.elements[i]);
		return false;
	}
	}
	return true;
}

//Date validation function
function validateDate(s)
{
	//Test for a string
	if (s.length > 0)
	{
		//Create an array to split the date into (dd/mm/yy)
		strarr = new Array ()

		//Use own split function as JScript does not include JavaScripts split function
		own_split(strarr, s, ".");
		
		//3 array elements means day, month, and year
		if (strarr.length == 3)
		{
			//Test the value of each element falls in an acceptable range
			for (var i = 0; i < strarr.length; i++)
			{
				if ((strarr[0] < 1) || (strarr[0] >31)){
					return false;
				}
				if ((strarr[1] < 1) || (strarr[1] >12)){
					return false;
				}
				if ((strarr[2] <= 2000) || (strarr[2] > 2099)){
					return false;
				}
			}
			return true;
		}
		return false;
	}
	return false;
}

//Text validation function
function validateText(s)
{
	//test for a string
	if (s.length > 0)
	{
		return true;
	}
	return false;
}

//Number validation function
function validateNum(s)
{
	//Test to see if the value converts to a number
	if (parseInt(s) > -1)
	{
		return true;
	}
	return false;
}

//Telephone Number validation function
function validateTeleNum(s)
{
	//Test for a string
	if (s.length > 0)
	{
		for (i = 0;  i < s.length;  i++)
 		{
		    	ch = s.charAt(i);
			if(! own_instring(ch))
			{
			return false;
			}
		}
		return true;
	}
	return false;
}

//E-Mail address validation function
function validateEmail(s)
{
	//Test for a string
	if (s.length > 0)
	{
		// Return false if e-mail field does not contain a '@' and '.' .
		if (s.indexOf ('@',0) == -1 || s.indexOf ('.',0) == -1)
      			{
			return false;
			}
		return true;
	}
	return false;
}

//In string function to test for valid substring, accomodates JavaScripts lack of VBScripts InStr() function
function own_instring(c)
{
	var checkOK = "0123456789-+-. ()\t\r\n\f";
	var ret  = false;

  		for (j = 0;  j < checkOK.length;  j++)
		{
      			if (c != checkOK.charAt(j))
			{
			continue;
			}
			else
			{
			ret = true;
			break;
			}
		}
	return ret;
}

//String split function to accomodate JScripts lack of JavaScripts split function
function own_split(arr, str, delim)
{
	//Initialise local variables
	var pos = 0;
	var num = 0;
	var start = 0;
	
	//Loop while there are characters in the string
	while (pos < str.length)
	{
		//Loop while there are delimiters in the string
		while((str.substring (pos, pos+1) != delim) && (pos < str.length))
		{
		pos++;
		}
		//Add the new characters to the output array
		arr[num] = str.substring(start,pos);
		num++;
		start = pos+1;
		pos++;
	}
}

function putFocus(elementStr)
   {
   elementStr.value="";
   elementStr.focus();
   }

//-->


