var digits = "0123456789";

// whitespace characters
var whitespace = " \t\n\r";

// decimal point character differs by language and culture
var decimalPointDelimiter = "." ;

// non-digit characters which are allowed in 
// Social Security Numbers
var SSNDelimiters = "- ";

// characters which are allowed in Social Security Numbers
var validSSNChars = digits + SSNDelimiters;

// U.S. Social Security Numbers have 9 digits.
// They are formatted as 123-45-6789.
var digitsInSocialSecurityNumber = 9;

// CONSTANT STRING DECLARATIONS
// (grouped for ease of translation and localization)
// m is an abbreviation for "missing"
var mPrefix = "You did not enter a value into the " ;
var mSuffix = " field. This is a required field." ;

var iSSN = "Subscriber Member Number must be a 9 digit number (like 123456789). Please reenter it now." ;



var defaultEmptyOK = false ;

// Notify user that required field theField is empty.
// String s describes expected contents of theField.value.
// Put focus in theField and return false.
function warnEmpty (theField, s)
{   
    alert(mPrefix + s + mSuffix) ;
    theField.focus() ;
    theField.select() ;
    return false ;
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s))
        return true;
    else
        return false;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


function validDate(theField, s, emptyOK){

	var datein = theField.value;	
	var indate=datein;

       if (emptyOK == true) 
           if (isEmpty(theField.value)) 
              return true;

	if (isWhitespace(theField.value)) 
		return warnEmpty (theField, s);
	
	if (indate.indexOf("-")!=-1)
	{
		var sdate = indate.split("-") ;
		indate2=(Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2])) ;
		chkDate=new Date(Date.parse(indate)) ;
		if ((chkDate.getYear() < 100)&&(sdate[2]>100))
		{
			year = 1900 + chkDate.getYear() ;
		}
		else
		{
			year = chkDate.getYear();
		}

	}
	else 
	{
		if (isInteger(indate) && indate.length == 6)
		{
			sdate0 = indate.substring(0,2);
			sdate1 = indate.substring(2,4);
	      		sdate2 = indate.substring(4,6);
			indate2=(Math.abs(sdate0))+"/"+(Math.abs(sdate1))+"/"+(Math.abs(sdate2)) ;
			chkDate=new Date(Date.parse(indate2)) ;
			year = chkDate.getYear() ;
		}
		else
		{
			sdate = indate.split("/") ;
			indate2=(Math.abs(sdate[0]))+"/"+(Math.abs(sdate[1]))+"/"+(Math.abs(sdate[2])) ;
			chkDate=new Date(Date.parse(indate)) ;
			if ((chkDate.getYear() < 100)&&(sdate[2]>100))
			{
				year = 1900 + chkDate.getYear() ;
			}
			else
			{
				year = chkDate.getYear();
			}
		}
	}

	var cmpDate=(chkDate.getMonth()+1)+"/"+(chkDate.getDate())+"/"+ year ;
	

	if (indate2!=cmpDate){
		return warnInvalid( theField, "You've entered an invalid date or date format for " + s + ". Please use the MM/DD/YYYY format.");

	}
	else {
		if (cmpDate=="NaN/NaN/NaN"){
			return warnInvalid( theField, "You've entered an invalid date or date format for " + s + ". Please use the MM/DD/YYYY format.");

		}
		else {
			return true;
		}	
	}

}

function dateFill(field, event){
	var key;
	if (window.event)
			key = window.event.keyCode;
	else if (event)
			key = event.which;
	
	if(key == '8' || key == '46'){
		//Don't do anything, they tried to backspace(8) or delete(46)
	}
	else if(field.value.length > 2 && field.value.lastIndexOf('//') == (field.value.length-2) ){
		field.value = field.value.substr(0, field.value.length-1);
	}
	else if(field.value.length == 2 && field.value.lastIndexOf('/')!= (field.value.length-1)){
		field.value = field.value + '/';	
	}
	else if(field.value.length > 2 &&  field.value.length < 6 
		&& field.value.lastIndexOf('/')!= (field.value.length-1)
		&& field.value.lastIndexOf('/') == (field.value.length-3)){
		field.value = field.value + '/';	
	}
}

// Check that string theField.value is a valid SSN.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function checkSSN (theField, emptyOK)
{   if (checkSSN.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  var normalizedSSN = stripCharsInBag(theField.value, SSNDelimiters)
       if (!isSSN(normalizedSSN, false)) 
          return warnInvalid (theField, iSSN);
       else 
       {  // if you don't want to reformats as 123-456-7890, comment next line out
//          theField.value = reformatSSN(normalizedSSN)
          return true;
       }
    }
}

// isSSN (STRING s [, BOOLEAN emptyOK])
// 
// isSSN returns true if string s is a valid U.S. Social
// Security Number.  Must be 9 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isSSN (s)
{   if (isEmpty(s)) 
       if (isSSN.arguments.length == 1) return defaultEmptyOK;
       else return (isSSN.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInSocialSecurityNumber)
}



// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.
function warnInvalid (theField, s)
{
    alert(s) ;
    theField.focus() ;
    theField.select() ;
    return false ;
}



// Removes all characters which appear in string bag from string s.
function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

// Check whether string s is empty.
function isEmpty(s)
{
   if ( s == null )
    return true;
   if ( s.length == 0 )
    return true;

   return false;
}



function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}



// isFloat (STRING s [, BOOLEAN emptyOK])
// 
// True if string s is an unsigned floating point (real) number. 
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


function checkMoney( theField, s, emptyOK )
{
    var normalizedMoney;

    if ( emptyOK )
        if ( isEmpty( theField.value ) )
            return true;

    normalizedMoney = stripCharsInBag( theField.value, '$ ' );
    if ( isFloat( normalizedMoney, emptyOK ) )
        return true;
    else
        return warnInvalid(theField, 'Please enter a dollar amount for ' + s + '.' );
}


var toothCodes = new Array(73);
toothCodes[1]  = "01";
toothCodes[2]  = "02";
toothCodes[3]  =  "03";
toothCodes[4]  =  "04";
toothCodes[5]  =  "05";
toothCodes[6]  =  "06";
toothCodes[7]  =  "07";
toothCodes[8]  =  "08";
toothCodes[9]  =  "09";
toothCodes[10] =  "1";
toothCodes[11] =  "2";
toothCodes[12] =  "3";
toothCodes[13] =  "4";
toothCodes[14] =  "5";
toothCodes[15] =  "6";
toothCodes[16] =  "7";
toothCodes[17] =  "8";
toothCodes[18] =  "9";
toothCodes[19] =  "10";
toothCodes[20] =  "11";
toothCodes[21] =  "12";
toothCodes[22] =  "13";
toothCodes[23] =  "14";
toothCodes[24] =  "15";
toothCodes[25] =  "16";
toothCodes[26] =  "17";
toothCodes[27] =  "18";
toothCodes[28] =  "19";
toothCodes[29] =  "20";
toothCodes[30] =  "21";
toothCodes[31] =  "22";
toothCodes[32] =  "23";
toothCodes[33] =  "24";
toothCodes[34] =  "25";
toothCodes[35] =  "26";
toothCodes[36] =  "27";
toothCodes[37] =  "28";
toothCodes[38] =  "29";
toothCodes[39] =  "30";
toothCodes[40] =  "31";
toothCodes[41] =  "32";
toothCodes[42] =  "A";
toothCodes[43] =  "B";
toothCodes[44] =  "C";
toothCodes[45] =  "D";
toothCodes[46] =  "E";
toothCodes[47] =  "F";
toothCodes[48] =  "G";
toothCodes[49] =  "H";
toothCodes[50] =  "I";
toothCodes[51] =  "J";
toothCodes[52] =  "K";
toothCodes[53] =  "L";
toothCodes[54] =  "M";
toothCodes[55] =  "N";
toothCodes[56] =  "O";
toothCodes[57] =  "P";
toothCodes[58] =  "Q";
toothCodes[59] =  "R";
toothCodes[60] =  "S";
toothCodes[61] =  "T";
toothCodes[62] =  "U";
toothCodes[63] =  "V";
toothCodes[64] =  "W";
toothCodes[65] =  "X";
toothCodes[66] =  "Y";
toothCodes[67] =  "Z";
toothCodes[68] =  "UL";
toothCodes[69] =  "LL";
toothCodes[70] =  "UR";
toothCodes[71] =  "LR";
toothCodes[72] =  "UA";
toothCodes[73] =  "LA";


function checkToothCode(theField, emptyOK)
{
    var i;

    if ( emptyOK )
        if ( isEmpty( theField.value ) )
            return true;

    for ( i = 1; i <= 73; i++ )
    {
        if ( theField.value.toUpperCase() == toothCodes[i] )
            return true;
    }

    return warnInvalid(theField, 'Check Tooth Code');
}


//This function checks to see if the date entered has 2 digit or 4 digit year and formats it accordingly
//so 03/03/01 returns as 03/03/2001 and 03/03/51 returns as 03/03/1951
//years less than 10 are considered as 2001 to 2010
//years greater than 10 are considered as 1910 to 2000
//Added 05/24/2001 --- Pankaj Vij

function returnFormattedDate(newdate)
{
	year = newdate.getYear();
	if ((year > 10) && (year<1900))
	{
	   	newYear = "19" + year ;
	   	newdate.setYear(newYear);
	}
	else
	{
	   	if (year < 10)
	   	{
	   		year = "0" + year ;
	   		newYear = "20" + year ;
	   		newdate.setYear(newYear);
	   	}
	   	else
	   	{
			if ((year > 10) && (year <1900))
			{
				newYear = "20" + year ;
				newdate.setYear(newYear);
			}

	   	}
			   
	}
	return newdate;
				
}


//This function checks if the date is a valid date and its less than todays date
//Added    05/21/2001 --- Pankaj Vij
//Modified 02/08/2006 --- Brian Smith  Require a date field in the format MM/DD/YYYY
//
function isValidDate(enteredDate,dateInfoString,emptyOK)
{

	indate = enteredDate.value ;
	var stmp1 = " ";

      if (emptyOK == true) 
      	if (isEmpty(indate)) {
            	return true;
 	};



	if (enteredDate == null) 
		return warnEmpty (enteredDate, dateInfoString);

	if (isWhitespace(enteredDate.value)) 
		return warnEmpty (enteredDate, dateInfoString);

      if (indate.length != 10) {
      	return warnInvalid( enteredDate, "You've entered an invalid date or date format for " + dateInfoString + ". Please use the MM/DD/YYYY format.");
      }
      else {
		var sdate = indate.split("/");
	   	if(sdate.length == 3) {
			if(sdate[0]>0 && sdate[0]<13){
				if(sdate[1]>0 && sdate[1]<32){
					if(sdate[2].length >3){
						if('Invalid Date' != new Date(Date.parse(enteredDate.value))){

							chkDate=new Date(Date.parse(indate)) ;
							todaysDate = new Date() ;
							diff = todaysDate.getTime() - chkDate.getTime() ;

							if (diff > 0)
							{
								return true ;
							}
							else
							{
						      	return warnInvalid( enteredDate, dateInfoString + " cant be greater than todays date");

							}

						}
					}
				}
			} 
		}

      }

    	return warnInvalid( enteredDate, "You've entered an invalid date or date format for " + dateInfoString + ". Please use the MM/DD/YYYY format.");
}


//This function will automagically insert the forward slash during data entry
//Added 02/08/2006 --- Ernie Mecham

function dateFill(field, event){
	var key;
	if (window.event)
			key = window.event.keyCode;
	else if (event)
			key = event.which;
	
	if(key == '8' || key == '46'){
		//Don't do anything, they tried to backspace(8) or delete(46)
	}
	else if(field.value.length > 2 && field.value.lastIndexOf('//') == (field.value.length-2) ){
		field.value = field.value.substr(0, field.value.length-1);
	}
	else if(field.value.length == 2 && field.value.lastIndexOf('/')!= (field.value.length-1)){
		field.value = field.value + '/';	
	}
	else if(field.value.length > 2 &&  field.value.length < 6 
		&& field.value.lastIndexOf('/')!= (field.value.length-1)
		&& field.value.lastIndexOf('/') == (field.value.length-3)){
		field.value = field.value + '/';	
	}
}






function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
  window.open(theURL,winName,features);
}