//  PMS postcode entry validation and formatting script
//  - Used in conjuction with postcode.js, pcobdata.js and pcfcdata.js (/weather/javascript/)
//  - Author: Cy Pearce 
//	- Vers: 1.0 29/08/2006

function pcvalidate(code)
{
         var size = code.length;
     //Remove whitespace from string and ensure string is uppercase. - Other spurious characters could be added here.
         var stripped="";
         for (i=0;i<size;i++)
         {
          if (code.charAt(i)!=" ")
          {
           stripped=stripped+code.charAt(i).toUpperCase();
          }
         }
     //Create a representation of the entered postcode with the following conversions:
	 //All alpha characters to "A" & all numerical characters to "N".
         size = stripped.length;
         var formatted="";
         for (i=0;i<size;i++)
         {
          if (!(isNaN(stripped.charAt(i))))
          {
           formatted=formatted+"N";//Character is a number
          }
          if ((isNaN(stripped.charAt(i))))
          {
           formatted=formatted+"A";//Character is alphabetical
          }
         }
     //PERMITTED FORMATS (AS LAID OUT BY http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm)
	 permittedFormat = new Array;//Permitted postcode formats plus permitted outward postcode formats.
     permittedFormat = ["ANNAA","ANNNAA","AANNAA","AANNNAA","ANANAA","AANANAA","AN","ANN","AAN","AANN","ANA","AANA"]
     size = permittedFormat.length;
     var status = "false";//safe default status
     var lbpcode="";//leading block of postcode
     for (i=0;i<size;i++)
         {
           if (formatted==permittedFormat[i])
           {
             status="true";//format matches allowed permutation of alphanumeric characters.
           }
         }
	 //Additonal rules (Also from http://www.govtalk.gov.uk/gdsc/html/frames/PostCode.htm)
	 //The letters Q, V and X are not used in the first position.
	 notPermittedFirst = new Array;
	 notPermittedFirst = ["Q","V","X"];
	 for (i=0;i<notPermittedFirst.length;i++)
         {
           if (stripped.charAt(0)== notPermittedFirst[i]) {status="falseExtra";}
         }
	 //The letters I, J and Z are not used in the second position.
	 notPermittedSecond = new Array;
     notPermittedSecond = ["I","J","Z"];
	 for (i=0;i<notPermittedSecond.length;i++)
         {
           if (stripped.charAt(1)== notPermittedSecond[i]) {status="falseExtra";}
         }
	 //The only letters to appear in the third position are A, B, C, D, E, F, G, H, J, K, S, T, U and W.
	 notPermittedThird = new Array;
     notPermittedThird = ["I","L","M","N","O","P","Q","R","V","X","Y","Z"];
	 for (i=0;i<notPermittedThird.length;i++)
         {
           if (stripped.charAt(2)== notPermittedThird[i]) {status="falseExtra";}
         }
     //The second half of the Postcode is always consistent numeric, alpha, alpha format and the letters C, I, K, M, O and V are never used.
     //This is not tested as only the outward postcode (first half) is referenced
	 if (status=="true")
	 {
           size = formatted.length;
	//Truncate "stripped" to leave only the leading block of the postcode, lbpcode.	   
           if (size<5) {lbpcode=stripped;} else {lbpcode=stripped.substring(0,size-3);}
	 }
	 //Error message display via dialog boxes - 
	 //  The changing of the text colour to red is achieved via the "change" function 
	 //  (within postcode.js)
	 
	 if (status=="false")
     {
           alert("Invalid entry - Please re-enter the postcode.");//Standard dialog box error message.
     }
	 if (status=="falseExtra")
	 {
	  	   alert("Invalid entry - Please re-enter the postcode.");//This allows for a slightly different error message for the second test if so desired. 
	 }					  	
     return lbpcode;
}