


// this is a modified version of the Macromedia form validation that is tweaked to
// allow dropdowns to be validated


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); return x;
}

function validateform() { //v3.0
	var i,p,q,nm,test,num,min,max,pos,remainder,errors='',args=validateform.arguments;
  	
	for (i=0; i<(args.length-2); i+=3)
  	{
		test=args[i+2];
		val=findobj(args[i]);
    	
		if (val)
		{
			nm=val.name;
			
			if ((val=val.value)!="")
			{
      	
				if (test.indexOf('isEmail')!=-1)
				{
					p=val.indexOf('@');
					if (p<1 || p==(val.length-1)) errors+='- '+replacechars(nm)+' must contain an e-mail address.\n';
				} 
				
			else if (test!='R')
			{ 
			
				// remove any trailing zeros from val as this causes it to not recognise it as a number
				pos=val.indexOf(".");
								
				if (pos!=-1)
				{
					// dot found, now get whats after it
					remainder=val.substr(pos+1,val.length-1-pos);
					
					// now remove any trailing zeros
					if (remainder.length==1)
					{
					
						// see if the length is one and the value is zero then remove it and also the decimal point
						if (remainder=="0")
						{
							val=val.substr(0,pos-2);
						}
						
					}
					

					// see if this has 2 dps and the last 2 digits are zero.  if so then remove both and the decimal point
					if ((remainder.length==2) && (remainder=="00"))
					{
						val=val.substr(0,val.length-3);
					}
					else
					{
					// see if this has 2 dps and the last one is a zero and remove if so
					if ((remainder.length==2) && remainder.substr(remainder.length-1,remainder.length-1)=="0")
					{
						val=val.substr(0,val.length-1);
					}
					}

						
						
				}
				num = parseFloat(val);
						
	        	if (val!=''+num) errors+='- '+replacechars(nm)+' must contain a number.\n';
	        	
				if (test.indexOf('inRange') != -1)
				{
					p=test.indexOf(':');
	          		min=test.substring(8,p); 
					max=test.substring(p+1);
	          		if (num<min || max<num) errors+='- '+nm.replace('_',' ')+' must contain a number between '+min+' and '+max+'.\n';
				}
			}
		} 
		
		else if (test.charAt(0) == 'R') errors += '- '+replacechars(nm)+' is required.\n'; }
	  } 
	  if (errors) alert('The following error(s) occurred:\n'+errors);
	  document.returnvalue = (errors == '');
	}




function replacechars(sinput) {  // written by Hawkins to convert any names with underscores to spaces
	sfind = "_"; // replace this
	sreplace = " "; // with this
	soutput = "" + sinput; // temporary holder

	while (soutput.indexOf(sfind)>-1)
		{
		ipos= soutput.indexOf(sfind);
		soutput = "" + (soutput.substring(0, ipos) + sreplace + 
		soutput.substring((ipos + sfind.length), soutput.length));
		}
	return soutput;
}


