<!--hide this script from non-javascript-enabled browsers// to use, place <script src="../dwValid.js"></script> etc, in head of html file/* ------------------------------------------------------   The routine will handle all field types in a form.   To use, extend ONSUBMIT handler in <form> tag. EG:   <form ONSUBMIT="field1.dw='!';   				   field2.dw='#';   				   ...   				   return valid(this);">      EG:      !   - required (any content)   #   - required (numeric only)   @   - required (email address)   &   - required (a-z, A-Z only)   %   - required (a-z, A-Z and 0-9 only)   $   - required (modulo-10 credit card check). Also rejects   		 numbers of only 000000.   1-9 - for checkboxes/radio button groups: one of         these must be selected. EG four checkboxes, each         one has 1 at the name start. Therefore, at least         one of these must be selected.      When it comes to lists/menus, the top row equates to   no selection the same as no selection at all.   The requirement is to make the top row 'Select...'.      function names:   ---------------   isBlank(s) 		return true if s is blank   isEmail(s) 		return true if s is email address   isNum(s)	  		return true if s is numeric value (0-9)   isAlpha(s)   	return true if s contains a-z and A-Z only   isCCNum(s)		modulo-10 check for credit cards   clrSpc(s)		remove spaces from passed string   isSelected(s)	return true if passed checkbox/radio control is selected   chk(s)			looks for validate char (in k) at start of field name. UNUSED   setDesc(f)		process all fields in form f (our form). Look for hidden fields   					with the name 'dw'. When found, the first character of the .value					attribute should be 1-9, identifying a group of checkboxes/radio buttons.					If found, the number is stripped and the description of that number					is set to the remainder of the text in the .value field.					Used to generate an error line specific to the group of					checkboxes/radio buttons.	   valid(f)   		process all form (f) fields; main()         paul harvey (dotweb) [last update: February 6th 2001]   ------------------------------------------------------*///-------------------------------------------------------------------------------------------var i,k,e,g,h,chkd=new Array(10);var mcr=new Array(10);/* validate codes. Place one infront of a field to validate*/k="!#@&";/* ------------------------------------------------------   initialise checkbox/radio button descriptions   We create unique messages to go in the alert error box    when no selection from this group has been chosen.      Precede a group of checkboxes/radio buttons with a number   1-9 and then create the matching error message with the   same number here. That's the one that will display!   ------------------------------------------------------*/function setDesc(f) {	var g,h,ij;		// first, set generic values for all fields	mcr[1]='select a checkbox/radio button';	mcr[2]='select a checkbox/radio button';	mcr[3]='select a checkbox/radio button';	mcr[4]='select a checkbox/radio button';	mcr[5]='select a checkbox/radio button';	mcr[6]='select a checkbox/radio button';	mcr[7]='select a checkbox/radio button';	mcr[8]='select a checkbox/radio button';	mcr[9]='select a checkbox/radio button';	//search form for hidden fields, named 'dw'	for(i=0;i<f.length;i++) {	    g=f.elements[i].name;	    j=f.elements[i].value;		h=f.elements[i].type;		if(h=='hidden' && g=='dw') //found a hidden field that holds a description string			//strip first character, and set description of that number to remaining value			mcr[(j.charAt(0))]=trim(j);	}}  /* ------------------------------------------------------   look for an empty field. Return true if so   ------------------------------------------------------*/function isBlank(s) {if (s==' ' || s.length==0 || s=='')    return true;else    return false;}/* ------------------------------------------------------   Remove spaces   ------------------------------------------------------*/function clrSpc(s) {	var i,j,k;	j="";	k="";	for(i=0;i<s.length;i++) {		j=s.charAt(i);		if(j==' ') 			;		else			k+=j;	}	return k;}/* ------------------------------------------------------   look for an Email field. Return true if so   ------------------------------------------------------*/function isEmail(s) {    var a,d,i,c,l;	a=0;	d=0;    l=s.length;    if (l<8) return false;        for(i=0;i<l;i++) {            c=s.charAt(i);        a=a+(c=='@');        d=d+(c=='.');    }    if (a!=1 || d<1) return false;            return true;}/* ------------------------------------------------------   look for validate character at start of passed string.   Return true if so.   ------------------------------------------------------*/function chk(s) {    if(k.indexOf(s.charAt(0))!=-1) return true;}/* ------------------------------------------------------   look for numeric field data only.   Return true if so.   ------------------------------------------------------*/function isNum(s) {    var i;	if(isBlank(s)) return false;	   	for(i=0;i<s.length;i++)		if("0123456789".indexOf(s.charAt(i))==-1) return false;	return true;	}/* ------------------------------------------------------   look for alpha field data only (a-z and A-Z).   Return true if so.   ------------------------------------------------------*/function isAlpha(s) {    var i;	if(isBlank(s)) return false;   	for(i=0;i<s.length;i++)		if("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".indexOf(s.charAt(i))==-1) return false;	return true;	}/* ------------------------------------------------------   look for a-z, A-Z and 0-9 only.   Return true if so.   ------------------------------------------------------*/function isAlphaNum(s) {    var i;	if(isBlank(s)) return false;   	for(i=0;i<s.length;i++)		if("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".indexOf(s.charAt(i))==-1) return false;	return true;	}/* ------------------------------------------------------   Check a number for Modulo-10 acceptance   (check digit on credit card numbers). Return true if so.   Modulo-10 rule does not apply to Amex cards...   ------------------------------------------------------*/function isCCNum(s) {	var total=0;	var flag=0;	var e=0;		s=clrSpc(s); //remove spaces	if(isBlank(s)) return false;	if(!isNum(s)) return false;		//catch numbers of 0000000, which modulo-10 sees as valid	for(i=0;i<s.length;i++)		if(s.charAt(i)!='0')			e++;	if(e==0) return false;		for (var i=(s.length - 1);i>=0; i--) {		if (flag == 1) {			var digits = s.charAt(i) * 2;			if (digits > 9) digits -= 9;			total += digits;			flag = 0;		} else {			total = total + parseInt(s.charAt(i));			flag = 1;		}	}	if ((total%10) == 0) {		return true;	} else {		return false;	}}/* ------------------------------------------------------   Return card type, based on number passed.   ------------------------------------------------------*/function cardType(s) {	var s1,s2,s3,l;	l=s.length;	s1=s.substring(0,1);	s2=s.substring(0,2);	s3=s.substring(0,3);	s4=s.substring(0,4);		if(s1==4)		if(l==13 || l==16) return "VISA";		if(s2>50 && s2<56 && l==16) return "Mastercard";		if(s2==34 || s2==37)		if(l==15) return "American Express";		if(s2==36 || s2==38 || s3>299 && 23<306)		if(l==14) return "Diner's Club";		if(s4==6011 && l==16) return "Discover";	}/* ------------------------------------------------------   At least one item with the passed no. must be selected.   Return true if so.   ------------------------------------------------------*/function isSelected(f,s) {    var i,g,c,er;	c=0;	er='';	for(i=0;i<f.length;i++) {		g=f.elements[i];		if(g.name.charAt(0)==s && g.checked) c++;		}		if(c==0)		if(chkd[s]<1) {			chkd[s]=1; 	//alert('chkd[1]: '+chkd[s]);			return false;		}	return true;}/* ------------------------------------------------------   main(). Go through each form element and check its 'dw' value.    If found, branch to the relevant function to handle it.   ------------------------------------------------------*/function valid(f) {    var d,e,f,g,h,i,n,er;	setDesc(f); //set up checkbox/radio button descriptions    for(i=0;i<10;i++) chkd[i]=0; //reset checkbox/radio button flags	er="";	    for(i=0;i<f.length;i++) {        d=f.elements[i].dw; //is there a dw value?        e=f.elements[i].value;         g=f.elements[i].name;		h=f.elements[i].type;		j=f.elements[i].selectedIndex; //select lists		//if(d)alert(d);		switch(d) {			case '!': //required field				switch(h) {					case 'textarea':					case 'text': 						if(isBlank(e)) er=er+"'"+g+"' is required\n";						break;					case 'select-one':					case 'select-multiple':						if(j==0 || j==-1) er=er+"'"+g+"' is required\n";						break;					default: 						break;						} //switch(h)				break;			case '@':				if(!isEmail(e)) er=er+"'"+g+"' must be an email address\n";				break;			case '#':				if(!isNum(e)) er=er+"'"+g+"' must be a number\n";				break;			case '&':				if(!isAlpha(e)) er=er+"'"+g+"' is required (letters only)\n";				break;			case '%':				if(!isAlphaNum(e)) er=er+"'"+g+"' is required (a-z, A-Z & 0-9)\n";				break;			case '$': 	er+="card type:"+cardType(e);				if(!isCCNum(e)) er=er+"'"+g+"' must be a valid Credit Card number\n";				break;			case '1':				if(h=='checkbox' || h=='radio')					if(!isSelected(f,1)) er=er+mcr[1]+"\n";				break;			case '2': 				if(h=='checkbox' || h=='radio')					if(!isSelected(f,2)) er=er+mcr[2]+"\n";				break;						case '3': 				if(h=='checkbox' || h=='radio')					if(!isSelected(f,3)) er=er+mcr[3]+"\n";				break;			case '4':				if(h=='checkbox' || h=='radio')					if(!isSelected(f,4)) er=er+mcr[4]+"\n";				break;			case '5':				if(h=='checkbox' || h=='radio')					if(!isSelected(f,5)) er=er+mcr[5]+"\n";				break;			case '6':				if(h=='checkbox' || h=='radio')					if(!isSelected(f,6)) er=er+mcr[6]+"\n";				break;			case '7':				if(h=='checkbox' || h=='radio')					if(!isSelected(f,7)) er=er+mcr[7]+"\n";				break;			case '8':				if(h=='checkbox' || h=='radio')					if(!isSelected(f,8)) er=er+mcr[8]+"\n";				break;			case '9':				if(h=='checkbox' || h=='radio')					if(!isSelected(f,9)) er=er+mcr[9]+"\n";				break;			default: 				break;		} //switch(g.charAt(0))							    } //for(i=0;i<f.length;i++) 		//show error dialog if er contains any errors      if(!isBlank(er)) {		alert("____Oops!____\n"+er);		return false;	}		//return true;}//-------------------------------------------------------------------------------------------// stop hiding -->