﻿// JScript File
//Reference: 2005BRERGuide.pdf, Section E

function StateBasicCheck(fieldId, stateName)
{
    var vField = document.getElementById(fieldId);
    if (BasicCheck(fieldId))
    {
        var vValue = vField.value;
        if (vValue.substring(0, 2) == stateName)
        {
            return true
        }
        else
        {
            alert("The legal EPA ID number must start with your state name \"" + stateName + "\"");
            vField.focus();
            return false;
        }
    }
    else
    {
        vField.focus();
        return false;
    }
}

function StateAdvanceCheck(fieldId, stateName)
{
    var vField = document.getElementById(fieldId);
    if (RoutineCheck(fieldId))
    {
        var vValue = vField.value;
        if (vValue.substring(0, 2) == stateName)
        {
            return true
        }
        else
        {
            alert("The legal EPA ID number must start with your state name \"" + stateName + "\"");
            vField.focus();
            return false;
        }
    }
    else
    {
        vField.focus();
        return false;
    }
}

function BasicCheck(fieldId)
{
    //1. General check
    var vField = document.getElementById(fieldId);
    var epaIdText = vField.value;
    var vLen = epaIdText.length;

    if (vLen == 0)
    {
        return true;
    }
    else if (vLen < 2)
    {
        alert("ERROR: The EPA ID number does not match requirements.");
        return false;
    }
    else if (epaIdText.substring(0, 2) == "FC")
    {
        return LegalEpaCharacters(epaIdText);
    }
    else if (vLen < 3)
    {
        alert("ERROR: The EPA ID number does not match requirements.");
        return false;
    }
    else
    {
        var firstTwo = epaIdText.substring(0, 2);
        var theThird = epaIdText.substring(2, 3);
        //1. Check state name of EPAID (the first two characteers)
        if (!IsValidState(firstTwo))
        {
            alert("ERROR: The first two characters of the EPA ID does not match the state code.");
            return false;
        }
        if (vLen > 6 && epaIdText.substring(2, 6) == "CESQG")
        {
            return LegalEpaCharacters(epaIdText);
        }
        if ("0123456789DR".indexOf(theThird) > -1) 
        {
            if (epaIdText.length != 12)
            {
                alert("ERROR: The EPA ID number must have 12 characters.");
                return false;
            }
        }
    }

    //2. Check expression of the EPAID
    if (!epaRegExp(epaIdText))
    {
        alert("ERROR: An illegal EPA ID number that does not match requirements.");
        return false;
    }
    else
    {
        return true;
    }
}

function LegalEpaCharacters(epaId)
{
    var pattern = /^(~|!|#|\$|%|\^|\*|&|@|\(|\)|_|\+|\`|\-|\\|\/|=|:|;|\?|,|\.|\'|\"|\[|\]|\||\w| )*$/;
    if (!pattern.test(epaId))
    {
        alert("ERROR: " + epaId + " has illegal characters.");
        return false;
    }
    else
    {
        return true;
    }
}

function AdvanceCheck(fieldId)
{	      
	if (!BasicCheck(fieldId))
	{
	    return false;
	}
	
    var vMessage = "";
    var vField = document.getElementById(fieldId);
    var epaIdText = vField.value;
    var vLen = epaIdText.length;
    var vReturn = true;
	
    //3. Check the third character
    //If the third character of the EPA ID is 'D' or 'R', the EPA ID must pass the Modulus 10 check digit routine below.
    //If the third character of the EPA ID is 0-9, the EPA ID must pass the Modulus 10 check digit routine or the alternate check digit routine below.
    var theThird = epaIdText.substring(2, 3);
    if (("0123456789DR").indexOf(theThird) > -1) 
    {
        var fourthToEleven = epaIdText.substring(3, 11);
        var theTwelve = epaIdText.substring(11, 12);
        var total = GetSumValue(fourthToEleven);   
        
        //4. Modulus 10 Check Digit Routine
        //BRState or Waste Reporter Software identifies the number in the 4th through 11th positions of the ID.
        //The combination of digits used to create the check digit is 12121212.
        //Sum the products, treating each one as a separate number (i.e. 18 becomes 1 and 8).
        //Subtract this total (22) from the next highest multiple of 10 (i.e. 30 in this case).
        vReturn = RoutineCheck(total, theTwelve); //Routine Check
        if (!vReturn)
        {
            if (("0123456789").indexOf(theThird) > -1) //Alternate Check
            {
            //5. Alternate Check Digit Routine:
            //The resulting sum is subtracted from the next higher multiple of 10.
            //The result of the subtraction is compared to the value in the 3rd position in the EPA ID.
                vReturn = AlternateCheck(total, theThird, theTwelve);
                if (!vReturn)
                {
                    vMessage = "The EPA ID number does not match the Alternate Check Digit Routine";
                }
            }
            else
            {
                vMessage = "The EPA ID number does not match the Modulus 10 Check Digit Routine";
            }
        }
    }
    
    if (!vReturn)
    {
        if (vMessage == "")
        {
            vMessage = "It is an illegal EPA ID number that does not match requirements.";
        }
        alert(vMessage);
        vField.focus();
    } 

    return vReturn;
}

//Validate whole epa strinb.  At least three characters to maxmun 12 characters
//E.1 Characteristics of an EPA ID: All EPA ID's must meet the following requirements:
//1. Only characters A-Z, 0-9 and blank are allowed
//2. The first two characters must be a valid state code
//3. The EPA ID must be at least three (3) characters in length (not including blanks).
function epaRegExp(epaIdText)
{
	var pattern = /^([A-Z]{2})([A-Z]|[0-9])(([A-Z]|[0-9]){0,9})$/;
	return pattern.test(epaIdText);
}

var aryStates = new Array( "AK","AL","AR","AS","AZ","CA","CO","CT","DC","DE","FC","FL","GA","GU","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NN","NV","NY","OH","OK","OR","PA","PR","RI","SC","SD","TN","TT","TX","UT","VA","VI","VT","WA","WI","WV","WY");
var vStates = "AK,AL,AR,AS,AZ,CA,CO,CT,DC,DE,FC,FL,GA,GU,HI,IA,ID,IL,IN,KS,KY,LA,MA,MD,ME,MI,MN,MO,MS,MT,NC,ND,NE,NH,NJ,NM,NN,NV,NY,OH,OK,OR,PA,PR,RI,SC,SD,TN,TT,TX,UT,VA,VI,VT,WA,WI,WV,WY";
//Check the state name
function IsValidState(strState)
{
	return (vStates.indexOf(strState.toUpperCase()) >= 0);
}


//The 4-11 characters must be digit if the third character is D, R, or 0-9
//Each caracter digit multiple with 1 or 2 with coresponding position
//Sum all results
//Substract from the next highst multiple of 10
//Compare with the 12th character digit
function RoutineCheck(total, theTwelve)
{
    if (total < 0)
        return false;
    var vSum = total;
    var vDigit = 0;
    while ((vSum % 10) > 0)
    {
        vDigit = vDigit + 1;
        vSum = vSum + 1;
    }
    return (vDigit == eval(theTwelve));
}


//Get total of sum and theTwelve digit
//Substract from the next highst multiple of 10
//Compare with the 3rd character digit
function AlternateCheck(total, theThird, theTwelve)
{
    if (total < 0)
        return false;
    var vDigit = 0;
    var vSum = total + eval(theTwelve);
    while ((vSum % 10) > 0)
    {
        vDigit = vDigit + 1;
        vSum = vSum + 1;
    }
    return (vDigit == eval(theThird));
}

//BRState or Waste Reporter Software identifies the number in the 4th through 11th positions of the ID.
//  Example ID: MDD001947308 4th - 11th positions: 00194730
//The combination of digits used to create the check digit is 12121212.
//In order to determine / verify the 12th check digit:
//  a. Multiply each digit in the check combination by its positional mate.
//  b. Sum the products, treating each one as a separate number (i.e. 18 becomes 1 and 8).
function GetSumValue(fourthToEleven)
{
    var aryCombi = [1,2,1,2,1,2,1,2];
    var total = 0;
    var vDigit = 0;
    for (i = 0; i < fourthToEleven.length; i++)
    {
        var aChar = fourthToEleven.charAt(i);  
        //in this case, 4-11 must digits
        if (("0123456789").indexOf(aChar) < 0)
        {
		    return -1;
		}
		var subValue = eval(aChar) * aryCombi[i];
		//If value > 9, calculate sum of digits
		if (subValue > 9)
		    subValue = 1 + subValue % 10;
        total = total + subValue;
    }
    return total;
}
