//Javascript form validation functions. 
//validateInput V1.0
//Copyright MouseMat Systems
//
//-----------------------------------------------------------------------
//INSTALLATION.
//
//Unzip these files into a suitable directory, open Default.htm in a web browser
//the source code for this script contains the following comments.
//
//-----------------------------------------------------------------------
//
//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 <SCRIPT>... </SCRIPT>
//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
//
//-----------------------------------------------------------------------
//
//COPYRIGHT & DISTRIBUTION.
//
//MouseMat Systems have made this script available to anybody that
//would like to use it, however we reserve all copyright to this script, 
//if redistributed or used on any Internet site or intranet system please keep 
//this code and it's accompanying comments complete.
//
//New with this release is a small (3kb) gif file, it should be displayed on
//any Internet site that makes use of this script.
//
//If anybody would like to improve on this implementation then please feel free, 
//but please ensure that a copy of any modifications are sent to MouseMat 
//Systems by e-mail at mm@mousematsystems.com.
//
//Copyright MouseMat Systems 04/06/98.
//
//--------------------------------------------------------------------------------------------------------------------
function validateInput(frm, mask){

	//Count the number of form elements
	var FrmLen = frm.elements.length;
 
	//Get the validation mask string
	var maskarr = mask;
	
	var ph = 0;
	
	//Loop through all the form elements
	for (var i = 0; i < FrmLen; i++)
	{
		//Phone numbers test
		
		if ((i == 17) && (frm.elements[17].value == ""))
			{
			ph = ph + 1;
			}
		if ((i == 18) && (frm.elements[18].value == ""))
			{
			ph = ph + 1;
			}
		if ((i == 19) && (frm.elements[19].value == ""))
			{
			ph = ph + 1;
			}
				
		if (ph == 3)
		{
			alert("Please enter at least one valid phone number.");
			putFocus(frm.elements[17]);
			return false;
		}
		
		
		//Test for Policy confirm
		if (i == 32)
		{
			if (frm.confirm.checked == false )
			{
			alert("Please check the box confirming you have read the Product Disclosure Statement and the Financial Services Guide.");
			putFocus(frm.elements[i]);
			return false;
			}
		}
		
		
		//Test for 2nd pet
		//if (i == 32)
		//{
		//	if (frm.elements[32].value == "")
		//		{
		//			i = 60;
		//		}
		//}
		
		//Test for 3rd pet
		//		if (i == 44)
		//		{
		//			if (frm.elements[44].value == "")
		//				{
		//					i = 60;
		//				}
		//}
		
		//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;
		var valtextselect = 1;
		var valnumselect = 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 (maskarr == "s")
		{
			valtextselect = validateText(frm.elements[i].value);
	}
	if (maskarr == "x")
		{
			valnumselect = validateNum(frm.elements[i].value);
	}
	
	

	//If the validation fails prompt the user
	if (! valdate)
	{
		alert("The date you have entered is invalid.");
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valtext)
	{
		alert("The details you have entered are incomplete.");
		//alert(i);
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valnum)
	{
		alert("Please enter a valid number.");
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valtelenum)
	{
		alert("Please enter a valid telephone number.");
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valemail)
	{
		alert("Please enter a valid email address.");
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valtextselect)
		{
		alert("Please make a selection from the list.");
		putFocus(frm.elements[i]);
		return false;
	}
	if (! valnumselect)
	{
		alert("Please make a selection from the list.");
		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] < 0) || (strarr[0] >31)){
					return false;
				}
				if ((strarr[1] < 0) || (strarr[1] >12)){
					return false;
				}
				if ((strarr[2] < 0) || (strarr[2] >99)){
					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) > 0)
	{
		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++;
	}
}
//--------------------------------------------------------------------------------------------------------------------
//
//JavaScript form focus handler
//Copyright MouseMat Systems 12/03/98.
//
//IMPORTANT: as of the authoring date this section of the validateInput
//module only works in Navigator 3.x, 4.x (inc Communicator) and IE 4.x.
//
//IE 3's flawed JScript/JavaScript implementation ignores the .focus() function
//but in testing does not cause any errors to be reported. 
//
//This function is called by the page onLoad event, the forms
//reset button and the validateInput() function. 
//
//It sets focus on either the first text box on the form (in the onLoad and form reset),
//or in the text box that fails the validation test (from the call in validateInput()).
//
//--------------------------------------------------------------------------------------------------------------------
function putFocus(elementStr)
   {
   elementStr.value="";
   elementStr.focus();
   }
   

