
// - Left String function

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

// - Right String function

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}


// //////////////////////////////////////////


// - this validates the form using trigger attributes

function validateForm(n)
{

		var x = document.forms[n].elements;
		for (var i=0;i<x.length;i++)
		{
// - set 'required=' and the message to display
			if (x[i].getAttribute('required') && !x[i].value)
			{ 	var msg=x[i].getAttribute('required')
				alert(msg)
				x[i].focus()
				return false;
			}

// - set 'email=true'
			if (x[i].getAttribute('email'))
			{ 	
				if ((x[i].value.indexOf('@') < 1) || 
    					(x[i].value.lastIndexOf('.') <= x[i].value.indexOf('@')+1) ||  
    					(x[i].value.lastIndexOf('.') == x[i].value.length - 1 ) ||  
    					(x[i].value.indexOf(' ')  != -1)) 
   				{  
    	  				alert('Please enter a valid e-mail address!')
					x[i].focus()
    	        			return false;
				}

			}

// - set 'confirm=' and the fieldname to match against
// - useful for confirmation passwords
// - could be adapted to compare numeric values of fields etc
			if (x[i].getAttribute('confirm'))
			{ 	var field=x[i].getAttribute('confirm')
				var ref=document.forms[n].elements[field].value
				if (ref!=x[i].value)
				{
					alert("Your password & confirmation don't match.  Please try again.")
					x[i].focus()
					return false;
				}
			}


// - set 'docupload=' and the message to display
// - checks the file extension of files being uploaded
			if (x[i].getAttribute('docupload') && x[i].value.length >0)
			{
				var fileext=Right(x[i].value,4)		

				if (fileext.toLowerCase() !=".doc" && fileext.toLowerCase() !=".pdf")
				{ 	var msg=x[i].getAttribute('docupload')
					alert(msg)
					x[i].focus()
					return false;
				}

	 		}

// - set 'reqradio' and the message to display
// - checks that at least one of a set of radio buttons is checked

			if (x[i].getAttribute('reqradio'))
			{
				var radio=x[i].name
				var msg=x[i].getAttribute('reqradio')
				var chk='n'

				for (z=0;z<document.forms[n].elements[radio].length;z++)
				{
					if (document.forms[n].elements[radio][z].checked)
					{
						chk='y';
					}

				}

				if(chk!='y')
				{
					chk='n'
					alert(msg)
					x[i].focus()
					return false;
				}	

			}


 		}
}

