function newvalidate(frm) {
	var errormsg = "";			// Assume we have no errors to start with.
	var accounterror = "N"	
	var postcodeerror = "N"	
	var houseerror = "N"	
	var amounterror = "N"	
	var bagserror = "N"
	var othererror = "N"

//	frm.AccountRef.value = trim(frm.AccountRef.value).toUpperCase();
	frm.AccountRef.value = trim(frm.AccountRef.value);
	frm.AccountRef.value = frm.AccountRef.value.toUpperCase();



// **************************** Garden Waste internal use **************************************************************

	if (frm.FormName.value=="intgreenwaste") {    

		if (IsEmpty(frm.AccountRef.value)) {
			alert("You must enter the reference number, amount and number of bags");
			frm.AccountRef.focus();
			return false;
		}		
		if (IsEmpty(frm.Amount.value)) {
			alert("You must enter the reference number, amount and number of bags");
			frm.Amount.focus();
			return false;
		}		
		if (IsEmpty(frm.Bags.value)) {
			alert("You must enter the reference number, amount and number of bags");
			frm.Bags.focus();
			return false;
		}
		errormsg = checkReference(frm.AccountRef.value,"03");
		errormsg += checkAmount(frm.Amount.value);
		errormsg += IsNumber(frm.Bags.value, "03");
	}		


// **************************************** End of Garden Waste internal use  ********************************************


// **************************************** Garden Waste Internet ********************************************************

	if (frm.FormName.value=="greenwaste") {

	// Trim leading and trailing space characters from the amount, reference and email fields.
	frm.PostCode.value = trim(frm.PostCode.value).toUpperCase();
	frm.HouseName.value = trim(frm.HouseName.value).toUpperCase();



		if (IsEmpty(frm.AccountRef.value + frm.PostCode.value + frm.HouseNo.value + frm.HouseName.value)) {
			alert("You must enter your reference number, postcode, house name or number and amount");
			frm.AccountRef.focus();
			return false;
		}



//  ********** Some data entered    *********************************

//************************************* Check Account Ref                  **************************************************

		if (!(IsEmpty(frm.AccountRef.value))) 
			{       
			  errormsg = checkReference(frm.AccountRef.value, "03");	
			  if (errormsg != "") { accounterror = "Y" ; }
			}

		else { errormsg += "Please enter your reference Number\n"; 
	               accounterror = "Y";
	   	     }

//************************************ Check Postcode                       *************************************************
			

		if (!(IsEmpty(frm.PostCode.value))) 
			{
			errormsg += checkPostCode(frm.PostCode);
			if (errormsg != "") { postcodeerror = "Y"; }
			}
		     
		else { errormsg += "Please enter your PostCode\n"; 
		       postcodeerror = "Y" ;}
		       

//************************************ Check House name /number           ***************************************************

       		if (IsEmpty( frm.HouseNo.value + frm.HouseName.value)) 
			    {
			    errormsg += "Please enter your house name or number\n";
			    houseerror = "Y" ;
			    }

//************************************ Check Amount                       ***************************************************

		if (!(IsEmpty(frm.Amount.value)))
			{
			errormsg += checkAmount(frm.Amount.value);
			if (errormsg != "") { Amounterror = "Y"; } 
			}
 		else	{   errormsg += "Please enter the amount you want to pay\n";
			    Amounterror = "Y" ;
			}	

//************************************ Check Bags                         ***************************************************

		if (!(IsEmpty(frm.Bags.value)))
			{
			errormsg += IsNumber(frm.Bags.value, "03");
			if (errormsg != "") { bagserror = "Y"; } 
			}

//*********************************** Set Focus *****************************************************************************
	if ( accounterror == "Y" )
		{ frm.AccountRef.focus(); }
	else if ( postcodeerror == "Y" )
	        { frm.PostCode.focus(); }
	     else if ( houseerror == "Y" )
		{ frm.HouseName.focus(); }
		  else if ( amounterror == "Y" )
			  { frm.Amount.focus(); }
		     else if ( bagserror == "Y" )
			     { frm.Bags.focus(); }

	}

// **************************************** End of Garden Waste Internet **************************************************

// *************************************** Council Tax Balances ***********************************************************

	if (frm.FormName.value=="ctaxbalances") {
		if (IsEmpty(frm.AccountRef.value)) {
			errormsg = "You must enter the reference number.\n";
			frm.AccountRef.focus();
			alert(errormsg);
			return false;
		}
		else		
		errormsg = checkReference(frm.AccountRef.value,"09");
	}
//*************************************** End of Council Tax Balances ****************************************************
// *************************************** Nndr Balances ***********************************************************

	if (frm.FormName.value=="nrbalances") {
		if (IsEmpty(frm.AccountRef.value)) {
			errormsg = "You must enter the reference number.\n";
			frm.AccountRef.focus();
			alert(errormsg);
			return false;
		}
		else		
		errormsg = checkReference(frm.AccountRef.value,"08");
	}
//*************************************** End of Nndr Balances ****************************************************

//***************************************************************************************************************************

	if (frm.Fund.value=="PF") {	
//alert (" form2 "+frm.FormName.value+" fund "+frm.Fund.value);
	if (frm.FormName.value == "parkingfinesbals") {
//alert("pf1");
	frm.RegistrationNumber.value = trim(frm.RegistrationNumber.value).toUpperCase().replace(/[\ ]/g,"");
		 

	// Don't do any further validation if the user has entered nothing in the reference and registration fields...
	if (IsEmpty(frm.AccountRef.value + frm.RegistrationNumber.value
               ))  {
		alert("You must enter a charge notice number and your vehicle registration");
		frm.AccountRef.focus();
		return false;
	}
	// Alert user when empty amount field, including an error for the reference number if invalid...
	else if (IsEmpty(frm.AccountRef.value) && !(IsEmpty(frm.RegistrationNumber.value)))	{
		errormsg = "Please enter the charge notice number.\n";
		/* can pass from dropdown, e.g. frm.PaymentType.value (instead of "PF")		*/
	}
	// Alert user when empty reference number field, including an error for the amount format if invalid...
	else if (IsEmpty(frm.RegistrationNumber.value) && !(IsEmpty(frm.AccountRef.value)))	{
		errormsg = checkReference(frm.AccountRef.value, "PF");
		if (errormsg != "") {
			frm.AccountRef.focus(); }
		else { frm.RegistrationNumber.focus(); }
		errormsg += "Please enter your vehicle registration.\n";
			} 
	else {
		// Fields filled in, validate format:
		errormsg = checkReference(frm.AccountRef.value, "PF");	
		errormsg += CheckMaxLen(frm.RegistrationNumber, 7);	
  	     }         

	}
	else if (frm.FormName.value == "pfbals2") {
      	 	// Insert validation - check amount entered and in valid format, and either 25.00,30.00 or 35.00
		if (IsEmpty(frm.Amount.value)) {
			alert("You must enter the amount");
			frm.Amount.focus();
			return false; }
		else { errormsg = checkAmount(frm.Amount.value); }
//alert("Checking msg");
		if (errormsg == "") {
//alert("format ok");
//alert("amt "+frm.Amount.value);
			if (frm.Amount.value != "25.00") {
			  if (frm.Amount.value != "30.00") {
			    if (frm.Amount.value != "35.00") { 
					frm.Amount.focus();
					errormsg = "You have entered an invalid amount, please check your Penalty Charge Notice\n";
//alert("msg "+errormsg);
				}}}
//alert("No prior error");				   }
	}
	}
	}

// **************************************** Council Tax *******************************************************************

	if (frm.Fund.value=="09") {    

		if (IsEmpty(frm.AccountRef.value + frm.Amount.value)) {
			alert("You must enter the reference number and amount");
			frm.AccountRef.focus();
			return false;
		}		
		if (IsEmpty(frm.AccountRef.value)) {
			errormsg = "You must enter the reference number.\n";
			errormsg += checkAmount(frm.Amount.value);
			frm.AccountRef.focus();
			alert(errormsg);
			return false;
		}		
		if (IsEmpty(frm.Amount.value)) {
			errormsg = checkReference(frm.AccountRef.value,"09");
			if (errormsg != "") {
				frm.AccountRef.focus(); }
			else
			{ frm.Amount.focus(); }
			errormsg += "Please enter the amount";
			alert(errormsg);
			return false;
		}		

		errormsg = checkReference(frm.AccountRef.value,"09");
		errormsg += checkAmount(frm.Amount.value);

         }

// **************************************** End of Council Tax *************************************************************
// **************************************** NNDR ***************************************************************************

	if (frm.Fund.value=="08") {    

		if (IsEmpty(frm.AccountRef.value + frm.Amount.value)) {
			errormsg = "You must enter the reference number and amount";
			frm.AccountRef.focus();
			alert(errormsg);	
			return false;
		}		
		if (IsEmpty(frm.AccountRef.value)) {
			errormsg = "You must enter the reference number.\n";
			errormsg += checkAmount(frm.Amount.value);
			frm.AccountRef.focus();
			alert(errormsg);
			return false;
		}		
		if (IsEmpty(frm.Amount.value)) {
			errormsg = checkReference(frm.AccountRef.value,"08");
			if (errormsg != "") {
				frm.AccountRef.focus(); }
			else
			{ frm.Amount.focus(); }
			errormsg += "Please enter the amount";
			alert(errormsg);
			return false;
		}		

		errormsg = checkReference(frm.AccountRef.value,"08");
		errormsg += checkAmount(frm.Amount.value);

         }
// *************************************** End of NNDR ********************************************************************

// **************************************** Debtors ***************************************************************************

	if (frm.FormName.value=="debtors") {    

		if (IsEmpty(frm.AccountRef.value + frm.Amount.value)) {
			errormsg = "You must enter the invoice number and amount";
			frm.AccountRef.focus();
			alert(errormsg);	
			return false;
		}		
		if (IsEmpty(frm.AccountRef.value)) {
			errormsg = "You must enter the invoice number.\n";
			errormsg += checkAmount(frm.Amount.value);
			frm.AccountRef.focus();
			alert(errormsg);
			return false;
		}		
		if (IsEmpty(frm.Amount.value)) {
			errormsg = checkReference(frm.AccountRef.value,"23");
			if (errormsg != "") {
				frm.AccountRef.focus(); }
			else
			{ frm.Amount.focus(); }
			errormsg += "Please enter the amount";
			alert(errormsg);
			return false;
		}		

		errormsg = checkReference(frm.AccountRef.value,"23");
		errormsg += checkAmount(frm.Amount.value);

         }
// *************************************** End of Debtors ********************************************************************
// *************************************** Building Control ******************************************************************
	if (frm.FormName.value=="bcpay") {    
		if (IsEmpty(frm.AccountRef.value + frm.Amount.value + frm.OtherRef.value)) {
			alert("You must enter the reference number, amount and surname/site");
			frm.AccountRef.focus();
			return false;
		}		

//  ********** Some data entered    *********************************

//************************************* Check Account Ref                  **************************************************

		if (!(IsEmpty(frm.AccountRef.value))) 
			{       
			if (frm.AccountRef.value.length > 6) {
			errormsg = "Reference Number is too long (max 6 digits)\n";}
			errormsg += IsNumber(frm.AccountRef.value, "06");
			  if (errormsg != "") { accounterror = "Y" ; }
			}

		else { errormsg += "Please enter your reference Number\n"; 
	               accounterror = "Y";
	   	     }


//************************************ Check Amount                       ***************************************************

		if (!(IsEmpty(frm.Amount.value)))
			{
			errormsg += checkAmount(frm.Amount.value);
			if (errormsg != "") { Amounterror = "Y"; } 
			}
 		else	{   errormsg += "Please enter the amount you want to pay\n";
			    Amounterror = "Y" ;
			}	

//************************************ Check Name/Site address                       *************************************************
			

		if (IsEmpty(frm.OtherRef.value)) 
		   { errormsg += "Please enter your Surname & Site address\n"; 
		       othererror = "Y" ;}		

//		if (frm.AccountRef.value.length > 5) {
//		errormsg = "Reference Number is too long (max 5 digits)";}
//		errormsg += IsNumber(frm.AccountRef.value, "06");
//		errormsg += checkAmount(frm.Amount.value);

//*********************************** Set Focus *****************************************************************************
	if ( accounterror == "Y" )
		{ frm.AccountRef.focus(); }
		  else if ( amounterror == "Y" )
			  { frm.Amount.focus(); }
		     else if ( othererror == "Y" )
			     { frm.OtherRef.focus(); }



	}
// *************************************** End of Building Control *********************************************************

// **************************** INSERT NEW FUND VALIDATION BEFORE THIS LINE ***********************************************
//***************************************************************************************************************************


	// If there is an error message, report it to user...
	if (errormsg != "")	{
		alert(errormsg); 
		return false;
	}
	// All values inputted are valid, so submit the valid contents of the form to secure site...
	return true;


}

function checkPostCode(ctrl){
 var errormsg = "";

 test = ctrl.value;

 // PostCode is optional
 if (test == "") {
	return errormsg;
	}

 if (test.length < 6 || test.length > 8){ //Code length rule
  errormsg = "- PostCode is wrong length(length)\n";
  ctrl.focus();
  return errormsg;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   errormsg = "- PostCode must start with a letter\n";
   ctrl.focus();
   return errormsg;
  }
 if (isNaN(test.charAt(test.length-3))){ //first character of inward code must be numeric rule
   errormsg = "- PostCode is in wrong format(numeric)\n";
   ctrl.focus();
   return errormsg;
  }
 if (!(isNaN(test.charAt(test.length-2)))){ //second character of inward code must be alpha rule
   errormsg = "- PostCode is in wrong format(secondalpha)\n";
   ctrl.focus();
   return errormsg;
  }
 if (!(isNaN(test.charAt(test.length-1)))){ //third character of inward code must be alpha rule
   errormsg = "- PostCode is in wrong format(thirdalpha)\n";
   ctrl.focus();
   return errormsg;
  }
 if (!(test.charAt(test.length-4) == " ")){//space in position length-3 rule
   errormsg = "- PostCode is in wrong format(space)\n";
   ctrl.focus();
   return errormsg;
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   errormsg = "- PostCode is in wrong format(toomanyspaces)\n";
   ctrl.focus();
   return errormsg;
  }
 return errormsg; 
}


function IsNumber(strng, fund){
	var error = "";				

	//Field is optional
	if (strng == "") {
		return error;
	}
	
	if (isNaN(strng)){
		if (fund=="03") {
			error = "- The number of bags must be numeric (1-9) \n";		
	}
	else {
			error = "- The reference number must be numeric (0-9) \n";		
	}	}
	return error; 	
}

function CheckMaxLen(ctrl, MaxLen){
	var error = "";			

	switch (ctrl.name) {
		case "RegistrationNumber":
     			if (ctrl.value.length > MaxLen){
     	     				error = "- Registration Number cannot be longer than " + MaxLen + " characters\n";
					ctrl.focus();
     	     		}
    	
	}
	return error; 	
}


function IsEmpty(strng) {
	return (strng == "");
}


function checkReference(ref, cat)	{
 
 var error = "";					// Set to no errors as default before testing otherwise 

 
	ref = ref.replace(/[\ ]/g,"");		// Strip spaces from reference number string, if any 			
	document.all.AccountRef.value = ref

	// Now go and check the reference number against the category selected by user... 
	
switch (cat)	{        
	     // Parking Fines: - C or P and 5 Digits or EH/YH and 8 digits

     	     case "PF" :

//    The following check for 'old' parking tickets removed 7/3/05 A.P.Mitchener
//
//		if (ref.length == 6){ 
//    	     		var refFilter = /^['C','P']\d{5}$/;
//     	     		if (!refFilter.test(ref)){
//     	     			error = "- Charge Notice number should be in format P12345 or C54321\n";
//				document.all.AccountRef.focus();
//     	     					 }
//		    }
//		else { if (ref.length == 10) {
                if (ref.length == 10) {
				var refFilter = /^[E,Y]H\d{7}(\d{1}|A)$/;
				if (!refFilter.test(ref)){
     	     				error = "- Charge Notice number should be in format EH or YH12345678\n";
					document.all.AccountRef.focus();
							 }
				else { var x=109;
					x=x+(ref.charAt(2)*7);
					x=x+(ref.charAt(3)*6);
					x=x+(ref.charAt(4)*5);
					x=x+(ref.charAt(5)*4);
					x=x+(ref.charAt(6)*3);
					x=x+(ref.charAt(7)*2);
					x=x+(ref.charAt(8)*1);
		        		var y=parseInt(x/11);
			    		y=y*11;
			    		x=x-y;
					if (x==10) { var cdig="A" ;}
					else { var cdig=x ;}
					if (cdig!=(ref.charAt(9))) {
					error = "- You have entered an invalid reference number, please re-enter\n";
					document.all.AccountRef.focus();
								    }
				     }
					      }
			else	{

				error = "- Reference number is incorrect length, please re-enter\n";
				document.all.AccountRef.focus();
				}
//		     }     removed (old parking tickets) A.P.Mitchener 7/3/05
 
	     break;

	// Green Waste 1 or 2 followed by 10 digits     	     
	     case "03" :
   	     	if (ref.length==14) {               //No validation required
                   return error; }

		var refFilter = /^[1,2]\d{10}$/;
     	     	if (!refFilter.test(ref)){
     	     		error = "- Reference number should be 11 characters long starting with a 1 or 2\n";
			document.all.AccountRef.focus();
			       	     	 }
		else { var x=(ref.charAt(1)*6);
			x=x+(ref.charAt(2)*5);
			x=x+(ref.charAt(3)*4);
			x=x+(ref.charAt(4)*3);
			x=x+(ref.charAt(5)*2);
		        var y=parseInt(x/10);
			    y=y*10;
			    x=x-y;
			if (x==0) { var cdig=0 ;}
			else { var cdig=10-x ;}
			if (cdig!=(ref.charAt(10))) {
				error = "- You have entered an invalid reference number, please re-enter\n";
				document.all.AccountRef.focus();
						    }
		     }
	break;

// ******************************
// ******* Council Tax
       case "09" :
// ******************************
              if (ref.length == 9) {
//				var refFilter = /^9\d{8}$/;
				var refFilter = /^9\d{7}(\d{1}|X)$/;
				if (!refFilter.test(ref)){
     	    				error = "- Account Reference should be in format 912345678\n";
					document.all.AccountRef.focus();
							 }
				else { var x=0;
					x=x+(ref.charAt(0)*7);
					x=x+(ref.charAt(1)*8);
					x=x+(ref.charAt(2)*4);
					x=x+(ref.charAt(3)*6);
					x=x+(ref.charAt(4)*3);
					x=x+(ref.charAt(5)*5);
					x=x+(ref.charAt(6)*2);
					x=x+(ref.charAt(7)*1);
		        		var y=parseInt(x/11);
			    		y=y*11;
					x=x-y;
					var cdig=11-x;
					if (cdig==10) { cdig="X";}
					if (cdig==11) { cdig="0";}
//					if (x==10) { var cdig="X";}
//					else { var cdig=11-x ;}
					if (cdig!=(ref.charAt(8))) {
					error = "- You have entered an invalid Account Reference, please re-enter\n";
					document.all.AccountRef.focus();
								    }
				     }
					      }
			else	{

				error = "- Account Reference is incorrect length, please re-enter\n";
				document.all.AccountRef.focus();
				}

      break;
//
// ******************************
// ******* NNDR
        case "08" :
// ******************************
               if (ref.length == 9) {
				var refFilter = /^7\d{8}$/;
				if (!refFilter.test(ref)){
     	     				error = "- Account Reference should be in format 712345678\n";
					document.all.AccountRef.focus();
							 }
				else { var x=0;
					x=x+(ref.charAt(0)*3);
					x=x+(ref.charAt(1)*7);
					x=x+(ref.charAt(2)*1);
					x=x+(ref.charAt(3)*3);
					x=x+(ref.charAt(4)*7);
					x=x+(ref.charAt(5)*1);
					x=x+(ref.charAt(6)*3);
					x=x+(ref.charAt(7)*7);
		        		var y=parseInt(x/10);
			    		y=y*10;
			    		x=x-y;
					if (x==0) { var cdig="0";}
					else { var cdig=10-x;}
					if (cdig!=(ref.charAt(8))) {
					error = "- You have entered an invalid Account Reference, please re-enter\n";
					document.all.AccountRef.focus();
								    }
				     }
					      }
			else	{

				error = "- Account Reference is incorrect length, please re-enter\n";
				document.all.AccountRef.focus();
				}

       break;

// ******************************
// ******* DEBT
        case "23" :
// ******************************
               if (ref.length == 8) {
//alert("debtors 8 chars "+ref)				
				if (isNaN(ref)) {
     	     				error = "- Account Reference should be numeric\n";
					document.all.AccountRef.focus();
							 }
				else { var x=0;
					x=x+(ref.charAt(0)*8);
					x=x+(ref.charAt(1)*7);
					x=x+(ref.charAt(2)*6);
					x=x+(ref.charAt(3)*5);
					x=x+(ref.charAt(4)*4);
					x=x+(ref.charAt(5)*3);
					x=x+(ref.charAt(6)*2);
		        		var y=parseInt(x/11);
			    		y=y*11;
			    		x=x-y;
					var cdig = 11-x;
					if (cdig==10) { cdig="0";}
					if (cdig==11) { cdig="0";}
					if (cdig!=(ref.charAt(7))) {
					error = "- You have entered an invalid Account Reference, please re-enter\n";
					document.all.AccountRef.focus();
								    }
				     }
					      }
			else	{

				error = "- Account Reference is incorrect length, please re-enter\n";
				document.all.AccountRef.focus();
				}

       break;

// ****************************** ADD NEW FUND CASE BEFORE THIS LINE ***************************************************
     }	
    	return error; 		/* Return the error message, if any */
}


function checkAmount(amt) {

 var error = "";		// Set to no errors as default before testing otherwise 

 
	amt = amt.replace(/[\ ]/g,"");				// Strip spaces characters, if any			
	
	var amtFilter = /^\d{1,4}\.\d{2}$/; 			// Format must be 12.00, 23.45, 9999.99 etc.
	
	// Don't allow user to enter an amount less than five pounds...
	if (amt < 0.01)	{
		error = "- The minimum payment permitted is £0.01 (1p).\n"; 
	}
	// If amount >= five pounds, now check the format and don't permit any alphabetical characters...
	else if (!amtFilter.test(amt))	{
		error = "- You must enter the payment amount in digits with pounds and pence ";
		error += "and a decimal point - e.g. 12.00, 34.23, 2133.90 etc., up to 9999.99.\n";
	}		
 	return error;
}


function addDecimals(inputField) {
	
	inputField.value = trim(inputField.value);
	
	var strAmount = inputField.value;
	
	if (strAmount != "") {
		if (strAmount.indexOf(".") < 0) {
			strAmount += ".00";
		}
		else if (strAmount.indexOf(".") == strAmount.length - 1) {
			strAmount += "00";
		}
		else if (strAmount.indexOf(".") == strAmount.length - 2) {
			strAmount += "0";
		}
	}
	
	inputField.value = strAmount;
	
	return true;
}


function checkEmail(strAddress) {
	var strErrMsg = "";
	if (strAddress == "") {
		return strErrMsg;
	}
	else {
		if (!emailFormatIsValid(strAddress)) {
			strErrMsg += "- Please enter a valid email address or leave the email box empty.\n"
		}
		return strErrMsg;
	}
}

function emailFormatIsValid(strAddr) {
	var emailFormat = /\S+\@\S+\.\S+/;
	if (emailFormat.test(strAddr)) {
		return true;
	}
	else {
		return false;
	}
}

function trim(strText) {
 
    // Get rid of leading spaces 
    while (strText.substring(0,1) == ' ') {
        strText = strText.substring(1, strText.length);
		}

    // Get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ') {
        strText = strText.substring(0, strText.length-1);
		}

   return strText;
} 
