function keystrokes(keycodes,cond)
/*
Author Chayne Walsh programming@chayne.net
Modified 9th Dec 2004
HELP Example : onkeypress="return keystrokes(event.keyCode,'EM');"
This function validates user input from the keyboard
No dependant functions
*/
{
   var returnboolean=false;
   
   switch (cond) 
   {
      //alert(keycodes);
      //SC is for safe characters
      case 'SC':
         if(keycodes>=32 && keycodes<=126 && keycodes!=34 && keycodes!=44 && keycodes!=39 && keycodes!=60 && keycodes!=62 || keycodes==8)
         {
            returnboolean=true;
         }else{
            alert('This Character is not allowed: [ ' + String.fromCharCode(keycodes) + ' ] ');
         }
         break;
      //PC is for post code
      case 'PC':
         if(keycodes>=48 && keycodes<=57)
         {
            returnboolean=true;
         }else{
            alert('This Character is not allowed: [ ' + String.fromCharCode(keycodes) + ' ] ');
         }
         break;
      //PN is for Phone Number
      case 'PN':
         if(keycodes>=48 && keycodes<=57 || keycodes==32)
         {
            returnboolean=true;
         }else{
            alert('This Character is not allowed: [ ' + String.fromCharCode(keycodes) + ' ] ');
         }
         break;
      //N is for Numbers only
      case 'N':
         if(keycodes>=48 && keycodes<=57)
         {
            returnboolean=true;
         }else{
            alert('This Character is not allowed: [ ' + String.fromCharCode(keycodes) + ' ]\n Numbers only please! ');
         }
         break;
      //D is for date only
      case 'D':
         //alert(keycodes);
         if(keycodes>=48 && keycodes<=57 || keycodes==47)
         {
            returnboolean=true;
         }else{
            alert('This Character is not allowed: [ ' + String.fromCharCode(keycodes) + ' ]\n Date format is dd/mm/yy! ');
         }
         break;
      //EM is for EMail
      case 'EM':
	     if(keycodes>=45 && keycodes<=122 && keycodes!=47 && keycodes!=58 && keycodes!=59 && keycodes!=60 && keycodes!=62 && keycodes!=63 && keycodes!=91 && keycodes!=92 && keycodes!=93 && keycodes!=94 && keycodes!=96 || keycodes==8)
         {
            returnboolean=true;
         }else{
            alert('This Character is not allowed: [ ' + String.fromCharCode(keycodes) + ' ] ');
         }
         break;
   }
   
   return returnboolean;
}
function valid(formfield,desc,validation,textDefault,textAlert,backgroundDefault,backgroundAlert) {
/*
Author Chayne Walsh programming@chayne.net
Modified 9th Dec 2004

HELP (form field , message , type of validation, text heading default colour, text alert colour, form field default colour, form field alert colour)
E.G.<input type="Submit" value="Submit" onclick="return valid('fullName|address|contactPhone|bestTime|arrivalDate1|departDate1','Full Name|Address|Contact Phone|Best Time to Contact|Arrival|Departure','R|R|R|R|D1|D1','#FFFF99','brown','#ffffff','#eba9a7');">
multiple validation can be done by using | as a delimiter.

Text descriptions for the form fields can have their colour changed by put them inside a div like so
<div id="divemail">E-mail Address:</div>
The div id name must start with "div"
 and then have the some name as the form field to be validated appended to "div" like so.
<div id="divemail">E-mail Address:</div>
<input type="text" name="email">

This function is dependant on the changecolour and findObj functions at the end of this function
*/
	var returnvalue=true; errors=''; num=0; 
	
	formfield = formfield.split("|");
	desc = desc.split("|");
	validation = validation.split("|");

	for (i=0; i<(validation.length); i+=1){
		
		var obj = new Object(findObj(formfield[i]));
		
		changecolour("div"+formfield[i],textDefault);
        obj.style.backgroundColor=backgroundDefault;
		
	  	var f = obj.value;
		var p=obj.value.indexOf('@');
		var e=obj.value.length-1;
		var v=validation[i];
		var d=desc[i];
		var n=Number(obj.value);
	
		//required field
		if (v=="R"){
			if (!f){
				num+=1;
				errors+=num+". "+d+ " is a required field.\n\n";
				changecolour("div"+formfield[i],textAlert);
				obj.style.backgroundColor=backgroundAlert;
			}
		}
		//email field
		if (v=="E"){
		  	if (!f) {
				num+=1;
				errors+=num+". Please enter your email address.\n\n";
				changecolour("div"+formfield[i],textAlert);
				obj.style.backgroundColor=backgroundAlert;
		  	}else if(p<1 || p==(e)){
				num+=1;
				errors+=num+". A valid email address is Required.\n\n";
				changecolour("div"+formfield[i],textAlert);
				obj.style.backgroundColor=backgroundAlert;
			}
		}
		//numbers only field
		if (v=="N"){
			if (!f){
				num+=1;
				errors+=num+". "+d+ " is a required field.\n\n";
				changecolour("div"+formfield[i],textAlert);
				obj.style.backgroundColor=backgroundAlert;
		  	}else if(!n){
				num+=1;
				errors+=num+". "+d+ " must contain numbers only.\n\n";
				changecolour("div"+formfield[i],textAlert);
				obj.style.backgroundColor=backgroundAlert;
			}
		}
		//URL illegal characters
		var matchthis = new RegExp("[\/><\,!\^\$\*\~\`\+\|\?@#%&\(\)\;:\{\}=\']","gi");
		if (v=="url"){
		  	if (f.match(matchthis)){
				num+=1;
				errors+=num+". "+f.match(matchthis)+ " is a restricted character.\n\n";
				changecolour("div"+formfield[i],textAlert);
				obj.style.backgroundColor=backgroundAlert;
			}
		}
		//illegal database characters
		var matchd = new RegExp("[\']","gi");
		if (v=="D"){
		  	if (f.match(matchd)){
				num+=1;
				errors+=num+". [ "+f.match(matchd)+ " ] is a restricted character.\n   Please avoid using these characters.\n\n";
				changecolour("div"+formfield[i],textAlert);
				obj.style.backgroundColor=backgroundAlert;
			}
		}
		//date format dd/mm/yyyy
		if (v=="D1"){
    		var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
		  	if (!f.match(objRegExp)){
				num+=1;
				errors+=num+". [ "+f+ " ] is not correct.\n   Please use this format dd\\mm\\yyyy.\n\n";
				changecolour("div"+formfield[i],textAlert);
				obj.style.backgroundColor=backgroundAlert;
			}
		}
	}
	if (errors){
	    alert('Requirements are as follows\n\n' + errors);
    	returnvalue=false;
	}
	
	return returnvalue;
}
function changecolour(objStr, colour) {
	var lyrStr = "";
	if (objStr.indexOf(",") != -1) {
		var arObjStr = objStr.split(",");
		for (var i = 0; i < arObjStr.length; i++)
			if (i == (arObjStr.length - 1))
				lyrStr += "document.layers[\"" + arObjStr[i] + "\"]";
			else
				lyrStr += "document.layers[\"" + arObjStr[i] + "\"].";
	}else{
		var arObjStr = new Array();
		arObjStr[0] = objStr;
	}
	
	var dc = arObjStr[arObjStr.length - 1];	
	if (document.getElementById) {
		if(document.getElementById(dc))document.getElementById(dc).style.color = colour;
	}else if (document.all){
		if(document.all[dc])document.all[dc].style.color = colour;
	}else if (document.layers){
		// untested ? 
		dc = eval(lyrStr);
		// untested ? 
		if(dc)dc.style.color = colour;
	}
}

function findObj(n, d) { //v3.0
  var p,i,x;  
  if(!d) d=document; 
  	if((p=n.indexOf("?"))>0&&parent.frames.length) {
  		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
  if(!(x=d[n])&&d.all) x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) 
  	x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
  	x=findObj(n,d.layers[i].document); 
	
	//alert(x);
	return x;
}

