/*	Example call to ValidateForm()
	Developed by Peter Wardle
	Copyright (C) 2008 Peter Wardle
	PO Box 457
	Moss Vale NSW 2577
	Australia
	Phone: +61 2 4869 1323
	Fax:   +61 2 4869 2038
	
	USE OF THIS CODE IS PERMITTED PROVIDING THE DEVELOPER
	INCLUDES IT AS IS WITH ALL DETAILS SHOWN
	onClick="return ValidateForm('formname','name','Customer Name','R','age','Customer Age','RisNum','email','Email Address','RisEmail');
	first arg				= "name" attribute of the form
	first arg of triplet 	= field name
	second arg of triplet 	= field description (for message reporting)
	third arg of triplet	=	'R' (required) for string type
								'RisEmail' for email type
								'RisDate' for date type (if successful converts date to dd/mm/yyyy format)
								'RisNum' for number type (do not use this for phone numbers)
								'RinRange5:8' for a number type in the range of 5 to 8 inclusive 
								'RminLength:4' type in at lease 4 characters 
								'RinString:abcABC67' only accept characters abcABC67 
								'RstdString' only accept standard characters a-z,A-Z,0-9 and space
								'RexclString:*"{]' do not accept characters *"{] (Note: excluded characters must be escaped (rawurlencoded) when passed to this routine)  
								'RisPassword' This is a password field and is to be confirmed by 'isConfirm'
								'RisConfirm' This is a password field which confirms 'isPassword'
								'isChecked' Only accept if this check box is checked'
*/

function ValidateForm() {
	var i,p1,p2,q,description,TestType,num,min,max;
	var ch,teststr;
	var first=0;
	var firstfield,PWvalue,PWdesc;
	var errors='';
	var error=false;
	var argu=ValidateForm.arguments;
	var args=argu;
	var fieldval,slength;
	if ( args.length == 1 ) {
		// the arguments have been passed as a string and therefore need to be split up
		args = argu[0].split(',');
		for (i=0; i<(args.length); i++) {
			args[i] = args[i].replace(/'/g,"");
		}
	}
	var formname = args[0];
	//alert(args.length);
	for (i=1; i<(args.length-2); i+=3) {
		// loop through all argument triplets
		// "TestType" is the test condition (eg. RisNum, 3rd parameter in each triplet)
		//alert(args[i]+"\n"+args[i+2]);
		TestType = args[i+2]; 
		// get this form field object
		o = eval('document.'+formname+'.'+args[i]);
		if (o) { 
			// it is a form field
			// get the field's description (2nd parameter in each triplet)
			description=args[i+1];
			// find the input value of the field
			fieldval = o.value;
			// get the field length
			slength = o.value.length;
			if (fieldval!="") {
				/*
				if ( fieldval.indexOf('"') != -1 ) {
					errors+='* " character is not allowed in '+description+'.\n';
				}
				*/
				if (TestType.indexOf('isEmail') != -1) { 
					p1=o.value.indexOf('@');
					if (p1<1 || p1==(slength-1)) 
						errors+='* '+description+' must contain an e-mail address.\n';
				} 
				if (TestType.indexOf('isDate') != -1) { 
					// date format must be (d/m/y)
					// day may be either "d" or "dd"
					// month may be either "m" or "mm"
					// year may be either "yy" or "yyyy"
					// if it is a valid date it will be converted to "dd/mm/yyyy"
					// test for first slash
					error=false;
					p1=o.value.indexOf('/');
					if (p1<1 || p1 >2) {
						errors+='* '+description+' must be in dd/mm/yyyy format.\n';
						error=true;
					} else if (p1==1){
						// pad the field left with a zero
						o.value = "0"+o.value;
						p1++;
					}
					if ( !error ) {
						// first slash is ok
						// test for second slash
						p2=o.value.indexOf('/',p1+1);
						if (p2<4 || p2>5) {
							errors+='* '+description+' must be in dd/mm/yyyy format.\n';
							error=true;
						} else if (p2==4) {
							// the month needs padding left
							o.value = o.value.substr(0,3)+"0"+o.value.substr(3);
						} 
					}
					if ( !error ) {
						// second slash is ok
						if ( o.value.length < 8 || o.value.length == 9 ) {
							errors+='* '+description+' must be in dd/mm/yyyy format.\n';
						} else if ( o.value.length == 8 ) {
							// the year needs padding left
							o.value =  o.value.substr(0,6)+"20"+o.value.substr(6);
						}					
					}
				} 
				if (TestType.indexOf('inString') != -1) { 
					// test that each field character is a valid one
					p1=TestType.indexOf(':');
					teststr=TestType.substr(p1+1);
					for ( ch = 0;ch < slength;ch++ ) {
						if ( teststr.indexOf(fieldval.substr(ch,1)) == -1 ) {
							errors+='* only "'+teststr+'" characters are allowed in '+description+'.\n';
							break;
						}
					}
				} 
				if (TestType.indexOf('stdString') != -1) { 
					// test each field character is a standard character
					for ( ch = 0;ch < slength;ch++ ) {
						p2 = fieldval.charCodeAt(ch);
						if ( (p2 < 48 && p2 != 32) || (p2 > 57 && p2 < 65) || (p2 > 90 && p2 < 97) || p2 > 122 ) {
							errors+='* only characters A-Z,a-z and 0-9 and spaces are allowed in '+description+'.\n';
							break;
						}
					}
				} 
				if (TestType.indexOf('exclString') != -1) { 
					// test that each field character is not an excluded one
					p1=TestType.indexOf(':');
					teststr=unescape(TestType.substr(p1+1));
					for ( ch = 0;ch < slength;ch++ ) {
						if ( teststr.indexOf(fieldval.substr(ch,1)) != -1 ) {
							errors+='* "'+teststr+'" characters are not allowed in '+description+'.\n';
							break;
						}
					}
				} 
				if (TestType.indexOf('minLength') != -1) { 
					p1=TestType.indexOf(':');
					min=TestType.substr(p1+1);
					if ( o.value.length < min ) {
						errors+='* '+description+' must be at least '+min+' characters long.\n';
					}						
				} 
				if (TestType.indexOf('isNum') != -1) { 
					num = parseFloat(fieldval);
					if (isNaN(fieldval)) 
						errors+='* '+description+' must be numeric.\n';
				}
				if (TestType.indexOf('inRange') != -1) { 
					num = parseFloat(fieldval);
					p1=TestType.indexOf('inRange');
					p2=TestType.indexOf(':');
          			min=TestType.substring(p1+7,p2); 
          			max=TestType.substr(p2+1);
					if (num<min || max<num) 
						errors+='* '+description+' must be a number between '+min+' and '+max+'.\n';
				}
				if (TestType.indexOf('isPassword') != -1) {
					// this is a password field
					PWvalue = o.value;
					PWdesc = description;
				}
				if (TestType.indexOf('isConfirm') != -1 ) {
					if ( o.value != PWvalue ) {
						// this is a confirm password field
						errors+='* '+PWdesc+' is not confirmed.\n';
					}
				}
				if (TestType.indexOf('isChecked') != -1) { 
					if ( !o.checked ) {
						errors+='* '+description+'\n';
					}						
				} 
			} else if (TestType.charAt(0) == 'R') {
				errors += '* '+description+' can not be left blank.\n';
			}
		} 
		if ( errors && first == 0 ) {
			first = 1;
			firstfield=o;
		}
	}
	if (errors) {
		alert('*** FORM INCOMPLETE ***\n\n'+errors);
		if ( firstfield.type=="text" || firstfield.type=="textarea" || firstfield.type=="password" ) {
			firstfield.select();
		}
		return false;
	} else {
		return true;
	}
}

