﻿// rules object
rules = {    
	/* 
		General Rules
		
			maximum characters on a plate
			minimum characters on a plate
			Regex of all invalid characters (in this case just a negative of all valid characters)
	*/
	"maxPlateLength": 7,
	"minPlateLength": 1,
	"invalidChars": /[^A-Z0-9]/,
	/*
		Invalid Combos:
		
		Combos that are never allowed in the PPQ Create a Plate system.
	*/
	"invalidCombos":[
		//All Lnn plates
		{
			"rule": /^[A-Z][A-Z]\d{3}$/,
			"reason": "Personalised plates cannot feature two letters followed by three numbers; please try another combination."
		},
		{
			"rule": /^[A-P,R-Z]\d{2}$/,
			"reason": "Prestige plates cannot consist of one letter followed by 2 numbers; please try another combination."
		},
		{
			"rule": /^[A-P,R-Z]\d{3}$/,
			"reason": "Prestige plates cannot consist of one letter followed by 3 numbers; please try another combination."
		},
		{
			"rule": /^[A-P,R-Z]\d{4}$/,
			"reason": "Prestige plates cannot consist of one letter followed by 4 numbers; please try another combination."
		},
		{
			"rule": /^[A-P,R-Z]\d{5}$/,
			"reason": "Prestige plates cannot consist of one letter followed by 5 numbers; please try another combination."
		},
		//All QPlates
		{
			"rule": /^Q\d{2,5}$/,
			"reason": "Q Plates are not able to be checked or purchased online. Please contact PPQ for more information or try another combination."
		},
		// QLD Government vehicles
		{
		  "rule": /^QG[A-Z]{2}\d{2}$/,
		  "reason": "Prestige plates cannot begin with the letters 'QG' followed by two letters and two numbers; please try another combination."
        },
		// LNNNN where L is A-P or R-Z
		{
		  "rule": /^[A-P,R-Z]\d{4}$/,
		  "reason": "Prestige plates cannot consist of one letter followed by four numbers; please try another combination."
		},

        //		//Taxis
        //		{
        //		  "rule": /\bT\d{4}\b/,
        //		  "reason": "Prestige plates cannot begin with the letter 'T' followed by four numbers; please try another combination."
        //		},
        //		//Dealer plates
        //		{
        //		  "rule": /\bD\d{4}\b/,
        //		  "reason": "Prestige plates cannot begin with the letter 'D' followed by four numbers; please try another combination."
        //		},
		//Special Interest Motorcycle
        //		{
        //		  "rule": /\bS\d{4}\b/,
        //		  "reason": "Prestige plates cannot begin with the letter 'S' followed by four numbers; please try another combination."
        //		},
		//Conditional Motorbike
        //		{
        //		  "rule": /\bC\d{4}\b/,
        //		  "reason": "Prestige plates cannot begin with the letter 'C' followed by four numbers; please try another combination."
        //		},
		//More reserved combos, all in one this time
		{
		  "rule": /^(SL|QV|QX|QT)\d{2}[A-Z]{2}$/,
		  "reason": "Prestige plates cannot begin with the letters 'SL', 'QV', 'QX' or 'QT' followed by two numbers and two letters; please try another combination."
		},
		// Random rule ??
		/*
		{
			"rule": /^[A-Z]{2}\d{2}$/,
			"reason": "Prestige plates featuring two letters followed by two numbers are not able to be checked, purchased, upgraded or remade online. Please contact PPQ for more information or try another combination. "
		},
		{
			"rule": /^\d{2}[A-Z]{2}$/,
			"reason": "Prestige plates featuring two numbers followed by two letters are not able to be checked, purchased, upgraded or remade online. Please contact PPQ for more information or try another combination. "
		},
		*/
		//Euro place holders
		{
			"rule": /^Q[A-Z]{2}\d{2}[A-Y]$/,
			"reason": "Classic Euro plates feature the letter 'Q' followed by two letters, two numbers and the letter 'Z' or 'Y' (if sold out when ending with 'Z'). Please try another combination."
		},
		// 3 # with 2 Letters
		{
			"rule": /^[0-9]{3}[A-Z]{2}$/,
			"reason": "Personalised plates cannot feature three numbers followed by two letters; please try another combination."
		},
		// T + 2 Digits
		{
		  "rule": /^T\d{2}$/,
		  "reason": "Prestige plates cannot feature the letter 'T' followed by two numbers; please try another combination."
		},
		// T + 3 Digits
		{
		  "rule": /^T\d{3}$/,
		  "reason": "Prestige plates cannot feature the letter 'T' followed by three numbers; please try another combination."
		},
		// LLNNNN or 2 letters and 4 #'s
		{
			"rule": /^[A-Z]{2}[0-9]{4}$/,
			"reason": "Personalised plates cannot feature two letters followed by four numbers; please try another combination."
		},
		// LLNNN or 2 letters and 3 # 
		/*
		{
			"rule": /^[A-Z]{2}[0-9]{3}$/,
			"reason": "Personalised plates cannot feature two letters followed by three numbers; please try another combination."
		},
		*/
		/*
		// LL - will argue with too short filter 
		{
			"rule": /^[A-Z][A-Z]$/,
			"reason": "Combinations featuring two characters are not able to be checked, purchased, upgraded or remade online. Please Contact PPQ for more information or try another combination."
		},
		// L - will argue with too short filter 
		{
			"rule": /^[A-Z]$/,
			"reason": "Combinations featuring one character are not able to be checked, purchased, upgraded or remade online. Please Contact PPQ for more information or try another combination."
		},
		// LN - will argue with too short filter 
		{
			"rule": /^[A-Z][0-9]$/,
			"reason": "Combinations featuring two characters are not able to be checked, purchased, upgraded or remade online. Please contact PPQ for more information or try another combination."
		},
		// NL - will argue with too short filter 
		{
			"rule": /^[0-9][A-Z]$/,
			"reason": "Combinations featuring two characters are not able to be checked, purchased, upgraded or remade online. Please contact PPQ for more information or try another combination."
		},
		*/
		// NN  - will argue with too short filter
		{
			"rule": /^[0-9][0-9]$/,
			"reason": "Plate combinations must contain at least two characters including at least one letter; please try again."
		}
	],
	/*
		Special Combos
		
		Valid combos, but only available as this plate type
		i.e. if one of these matches, no other combos will be checked.
	*/
	"specialCombos":[
		{
			"rule":  /\bQ[A-Z]{2}\d{2}Z\b/,
			"style": "Euro Plate"
		}
	],
	"validCombos":[
		{
			"rule": /\b[A-Z0-9]{2,6}\b/,
			"style": "Prestige Plate"
		}
	]
};


/*  
	checkCombo

	takes a given combination
	Checks to see if it's valid, if it's not, it will return a string with an error message.
	If combination is valid, it will return an array of plate-types it falls into.
*/
function checkCombo(combo){
	combo = combo.toUpperCase();
	combo = combo.replace(/^\s+|\s+$/g,"");
	
	//check for invalid plate length
	if(combo.replace(" ","").length > rules.maxPlateLength){
		return "Combination too long";
	}
	
	//check for invalid characters
//	var patt=new RegExp(rules.invalidChars);
//	var icPos = patt.exec(combo);
//	if(icPos != null){
//		badChars ="";
//		for (i = 0; i < icPos.length; i++) {
//			if(icPos[i]== " "){
//				badChars = badChars + "Please do not uses spaces as any gaps are plate design specific,";
//			}else{
//				badChars = badChars + icPos[i] + ",";
//			}
//			
//		} 
//		return "Invalid character detected: " + badChars;
//	}
	//check for invalid plate combos
	for(var i=0; i< rules.invalidCombos.length; i++){
		result = isPlateInvalid(combo, rules.invalidCombos[i]);
		if(result){
			return result;
		}
	}
	
	if(combo.length < rules.minPlateLength){
		return "Plate combinations must contain at least three characters; please try again.";
	}
	
	var plateTypes = new Array;
	//check for special plates
    //	for(var i=0; i<rules.specialCombos.length; i++){
    //		result = isPlateOfType(combo, rules.specialCombos[i]);
    //		if(result){
    //			plateTypes.push(result);
    //		}
    //	}
	//If there is a special plate combo matched, return values as special plate combos are not valid for other plate types
    //	if(result.length >0){
    //			return plateTypes;
    //	}
	//check for standard plates
	//for(var i=0; i<rules.validCombos.length; i++){
	//	result = isPlateOfType(combo, rules.validCombos[i]);
	//	if(result){
	//		plateTypes.push(result);
	//	}
	//}
	
	return plateTypes;
}

function isPlateInvalid(combo, plateObj){
	var patt=new RegExp(plateObj.rule);
	var result = patt.exec(combo);
	// console.log('isInvalid result for '+combo+ ': '+result);
	if(result == null){
		return false;
	}else{
		return plateObj.reason;
	}
}

function isPlateOfType(combo, plateObj){
	var patt=new RegExp(plateObj.rule);
	var result = patt.exec(combo);
	if(result != null){
		return plateObj.style;
	}else{
		return false;
	}
}
// JScript File


