var ModelIdentifier = {

	shiftStrings:['0',
		"first",
		"second",
		"third",
		"fourth",
		"fifth",
		"sixth",
		"seventh",
		"eighth",
		"ninth",
		"tenth"
	],
	
	// given the values from the 3 inputs, returns an object with model info
	identify:function( model, date, year ) {
		var validation = this.validate( model, date, year );
		if( ! validation.isValid ) {
			throw new OmegaException( "Invalid input values.", validation );
		}
		var date = ModelIdentifier.dateFor( date, year );
		return {
			model:model.substr(0,4),
			revision:model.substr(4,1),
			productionDate:date,
			productionDateStr:date.toLocaleDateString(),
			shift:year.charAt(1),
			shiftStr:this.shiftStr( year.charAt(1) )
		};
	},
	
	dateFor:function( dateInput, yearInput ) {
		var year = this.yearForDigit( yearInput.charAt(0) );
		var date = new Date( year, this.monthForChar( dateInput.charAt(0) ), dateInput.substr(1) );
		if( date.valueOf() > new Date().valueOf() ) {
			year -= 10;
			date = new Date( year, this.monthForChar( dateInput.charAt(0) ), dateInput.substr(1) );
		}
		return date;
	},
	
	shiftStr:function( digit ) {
		return (this.shiftStrings[ digit ] == null ? digit : this.shiftStrings[ digit ]) + ' shift' ;
	},
	
	validate:function( model, date, year ) {
		var result = { isValid:false, errors:[] };
		if( ! ModelIdentifier.modelInputPattern.test( jQuery.trim( model ) ) ) {
			result.errors.push( "Model" );
		}
		if( ! ModelIdentifier.dateInputPattern.test( jQuery.trim( date ) ) ) {
			result.errors.push( "Date" );
		}
		if( ! ModelIdentifier.yearInputPattern.test( jQuery.trim( year ) ) ) {
			result.errors.push( "Year" );
		}
		if( result.errors.length == 0 ) {
			result.isValid = true;
		}
		return result;
	},
	
	// returns the index of the month represented by the given letter. a = 0, b = 1, c = 2, etc.
	// note that this is zero-based such that a == 0 == january, for consistency with javascript's
	// date objects.
	monthForChar:function( letter ) {
		return (letter.toUpperCase().charCodeAt(0) - 65);
	},
	
	yearForDigit:function( digit ) {
		var curDate = new Date();
		var currentDecade = parseInt( curDate.getFullYear()/10 ) * 10;
		var year = currentDecade + parseInt( digit );
		if( year > curDate.getFullYear() ) {
			year -= 10;
		}
		return year;
	},
	
	modelInputPattern:/^[0-9]{5}$/,
	dateInputPattern:/^[A-L][0-9]{1,2}$/i,
	yearInputPattern:/^[0-9]{2}$/
	
}

