//	This is a library of JavaScript functions.

var digits = "0123456789";	

// See this URL for the State Abbreviations: http://www.usps.gov/ncsc/lookups/abbr_state.txt
var USStateDeliminator ="|";
var USStateCodes = "AL|AK|AZ|AR|CA|CO|CT|DE|DC|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY";

var DaysInMonth = new Array();
DaysInMonth[1] = 31;
DaysInMonth[2] = 29;
DaysInMonth[3] = 31;
DaysInMonth[4] = 30;
DaysInMonth[5] = 31;
DaysInMonth[6] = 30;
DaysInMonth[7] = 31;
DaysInMonth[8] = 31;
DaysInMonth[9] = 30;
DaysInMonth[10] = 31;
DaysInMonth[11] = 30;
DaysInMonth[12] = 31;

var digits = '0123456789';
var amounts = '0123456789.';

//	Functions

function prompt(s)
{
	window.status = s;
}

function promptSelect(s,pField)
{
	pField.focus();
	pField.select();
	window.status = s;
}

function ToUpper(pField)
{
	var text = pField.value;
	var newtext = text.toUpperCase();
	pField.value = newtext;
}


function isLetter(c)
{
	if(((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isDigit(c)
{
	return ((c >= "0") && (c <= "9"));
}

function isInteger(s)
{
	var i;
	for(i=0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if(!isDigit(c)) return false;	
	}
	return true; 	// All characters are numbers
}
		
function ZipCodeValidation(s){
	if(isEmpty(s)) return false; 
	if(s.length == 5) {
		return(isInteger(s));
	}
	else{
		var zipfront = Seperator('-',1,s);
		var zipback = Seperator('-',2,s);
		if(isInteger(zipfront) && zipfront.length == 5){
			if(isInteger(zipback) && zipback.length == 4){
				return true;
			}
			else{
				return false;
		}	}
		else{
			return false;
}	}	}


function PadAmount(s){
	var amountLength = 0;		// Number of charachters in amount 
	var newAmount = 0;
	var dollarLength = 0;
	var centsLength = 0;
	var amountLength = s.length;
	var dollars = Seperator('.',1,s);
	var cents = Seperator('.',2,s);
	dollarLength = dollars.length;
	centsLength = cents.length;
	if(centsLength >= 2){
		cents = cents.substring(0,2);	
	}
	else if(centsLength == 1){
		cents = cents.substring(0,1) + '0';
	}
	else{
		cents = '00'
	}
	newAmount = dollars + '.' + cents;
	return newAmount;
}


function isAmount(s)
{
	var bFound = false;
	var p = ' ';
	var n = ' ';
	for (i=0; i<s.length;i++){
		p = s.substring(i,i+1);
		var bFound = false;
		for (j=0;j<amounts.length;j++){
  		   	n=amounts.substring(j,j+1)                                   
			if (p == n){
				bFound = true;
				break;
		}	}                                        
		if ( bFound == false)
			break;
	}
	if( bFound == true){
		var dollars = Seperator('.',1,s);
		dollarLength = dollars.length;
		if(dollarLength > 7){
			alert(eDollarAmt);
			bFound = false;
	}	}
	return bFound;
}

function isAlphanumeric(s)
{
	var i;
	var bNumeric = false;
	var bAlphabetic = false;

	for(i=0; i < s.length; i ++)
	{
		var c = s.charAt(i);
		if(isLetter(c))
		{
			bAlphabetic = true;
		}		
		
		if(isDigit(c))
		{
			bNumeric = true;
		}
	}

	if((bNumeric == true) && (bAlphabetic == true))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isValidInteger(s)
{
	var i;
	for(i=0; i < s.length; i++)
	{
		var c = s.charAt(i);

		if(i>=1)
		{	// There shouldnt be a negative sign unless it is
			// in front of the number
			if(c=="-") return false;
		}

		if(!isDigit(c)) return false;	
	}
	return true; 	// All characters are valid
}

// Check if the string contains all numbers
function isNumeric(s){
	var bFound = false;
	var p = ' ';var n = ' ';
	for (i=0; i<s.length;i++){
		p = s.substring(i,i+1);
		var bFound = false;
		for (j=0;j<digits.length;j++){
   		   	n=digits.substring(j,j+1)                                   
			if (p == n){
				bFound = true;
				break;
		}	}                                        
		if ( bFound == false)
			break;
	}
	return bFound;
}

function PadDate(s){
	var m = 0;		// Number of charachters in month
	var d = 0;		// Number of charachters in day
	var month = Seperator('/',1,s);
	var day = Seperator('/',2,s);
	var year = Seperator('/',3,s);
	var newdate = "";
	m = month.length;
	if(m == 1){
		month = "0" + month;
	}
	d = day.length;
	if(d == 1){
		day = "0" + day;
	}
	newdate = month + "/" + day + "/" + year;
	return newdate;
}

function DateValidation(s)
{
	if(isEmpty(s)) return false; 
	var month = Seperator('/',1,s);
	var day = Seperator('/',2,s);
	var year = Seperator('/',3,s);
	if (! (CheckMonth(month) && CheckDay(day) && CheckYear(year))) return false; 
	var intMonth = 0;
	// For some reason the August(08) and September(09) are not interpreted correctly when you do a ParseInt.
	if( month == 9){
		intMonth = 9;
	}
	else{
		if(month == 8){
			intMonth = 8;
		}
		else{
			intMonth = parseInt(month);
	}	}
	var intYear = parseInt(year);
	if(day > DaysInMonth[intMonth]) return false;
	if((intMonth == 2) && (day > DaysInFebruary(intYear))) return false; 
	// The Date is valid
	return true;
}

// Make sure this is a valid date before you pass it in. You can use the DateValidation function to validate the date. Get the Days from January 1, 1970
function GetDaysFromDate(date)
{
	var numdays = 0;
	var intDays = 0;
	var msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
	var curtime;
	// Parse out the Month, Day, and Year
	var month = Seperator('/',1,date);
	var day = Seperator('/',2,date);
	var year = Seperator('/',3,date);
	// It seems that the month is already an integer
	intMonth = month;
	// Get the integer values for the Month, Day, and Year
	var intday = day;
	var intyear = parseInt(year);
	// The months recognized by Javascript are 0 to 11
	var newmonth = intMonth - 1;
	canceldate = new Date(); 	// Create a Date object
	canceldate.setYear(intyear);
	canceldate.setMonth(newmonth);
	canceldate.setDate(intday);	
	// Get the number of milliseconds in the Date since 1/1/1970
	curtime = canceldate.getTime();
	curtime = Math.round(curtime);
	intDays = curtime / msPerDay; 	// Find the number of days
	intDays = Math.round(intDays);
	return intDays;  
}

function CheckMonth(s)
{
	return isIntegerInRange(s,1,12);
}
	
function CheckDay(s)
{
	return isIntegerInRange(s,1,31);
}
	
function DaysInFebruary(year)
{
	// February has 29 days in any year that is evenly divisible by four, Except for centurial years which are not also divisible by 400.
	return ( ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0)) ) ? 29 : 28);
}
	
function CheckYear(s)
{
	if(!isInteger(s)) return false;	
	return (s.length == 4);
}

function isIntegerInRange(s, a, b){
	if(!isInteger(s)) return false;
	return ((s >= a) && (s <= b));
}
	
function isIntDec(s)
{
	var i;
	for(i=0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if(!isDigit(c) && c!="." && c!="-") return false;	
	}
	return true; 	// All characters are numbers
}

function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

function Seperator(sep,which,inwhat)
{
	var n = 0;		// Start of the phrase	
	var wstr = ' ';	// Holds substring
	var i = 0;		// Loop counter
	var s = 0;		// Start of string we want
	var f = 0;		// End of string we want
	for(i = 1; i < which ; i++){
		n = inwhat.indexOf(sep,n);		// Look for Seperator
		if( n < 0)						// If you do not find it
		{
			return '';					// return empty string
			break;
		}
		n++
	}
	if ( n >= 0)						// We should be at the right place
	{
		var s = n;						// String starts with n
		var f = inwhat.indexOf(sep,n);	// Get next Seperator
		if( f < 0) f = inwhat.length;	// If there are no more Seperators it is last phrase in string
		wstr = "" + inwhat.substring(s,f);	
	}
	return wstr;						// return string. It will be empty if the Seperator was not found
}

function FormatSSN(s)
{
	var output = "";
	if(isEmpty(s)) return output;
	if(s.length == 9)
	{
		return s;
	}
	else
	{
		var ssnfront = Seperator('-',1,s);
		var ssnmiddle = Seperator('-',2,s);
		var ssnback = Seperator('-',3,s);
		output = ssnfront + ssnmiddle + ssnback;
		return output;
	}	
}

function FormatZip(s){
	var output = "";
	if(isEmpty(s)) return output; 
	if(s.length == 9 || s.length == 5){
		return s;
	}
	else{
		var zipfront = Seperator('-',1,s);
		var zipback = Seperator('-',2,s);
		output = zipfront + zipback;
		return output;
}	}

function FormatPhone(s)
{
	var phoneNum = "";
	if(isEmpty(s)) return phoneNum; 
	var Area = "";
	var phon3 = "";
	var phon4 = "";
	var digitCount = 0;
	for (i=0; i<s.length;i++)
	{
		p = s.substring(i,i+1);
		for (j=0;j<digits.length;j++)
		{
 		n=digits.substring(j,j+1)                                   
			if (p == n)
			{
				if(digitCount <= 2)
				{
				Area = Area + p;
				}
				else if(digitCount <= 5)
				{
				phon3 = phon3 + p;
				}
				else if(digitCount <= 9)
				{
				phon4 = phon4 + p;
				}
				else if(digitCount <= 10)
				{
				Area = Area.substring(1,3) + phon3.substring(0,1);
				phon3 = phon3.substring(1,3) + phon4.substring(0,1);
				phon4 = phon4.substring(1,4) + p; 
				}
				digitCount ++;			
				break;
		}	}                                        
	}
	phoneNum = Area + phon3 + phon4;
	return phoneNum;
}

function SSNValidation(s)
{
	if(isEmpty(s)) return false; 
	if(s.length == 9) {
		return(isInteger(s));
	}
	else{
		var ssnfront = Seperator('-',1,s);
		var ssnmiddle = Seperator('-',2,s);
		var ssnback = Seperator('-',3,s);
		if(ssnfront.length == 3 && isInteger(ssnfront)){
			if(ssnmiddle.length == 2 && isInteger(ssnmiddle)){	
				if(ssnback.length == 4 && isInteger(ssnback)){
					return true;
	}	}	}	}
	return false;
}

// Make sure this is a valid SSN before you pass it in.
function PadSSN(s)
{
	if((s.length == 9) && (isInteger(s)))
	{
		ssnfront = s.substring(0,3);
		ssnmiddle = s.substring(3,5);
		ssnback = s.substring(5,9);	
		newssn = ssnfront + "-" + ssnmiddle + "-" + ssnback;	
	}
	else
	{
		newssn = s;	
	}
	return newssn;
}

function PadMasterPol(s)
{
	var masterpollength = 0;
	masterpollength = s.length;
	if(s.length == 8)
	{
		newmasterpol = s;	
	}
	else
	{
		while (s.length < 8)
		{
			s = s + "0";
		}	
		newmasterpol = s;
	}
	return newmasterpol;		
}

function PadCert(s)
{
	var certlength = 0;
	certlength = s.length;
	if(s.length == 8)
	{
		newcert = s;	
	}
	else
	{
		while (s.length < 8)
		{
			s = "0" + s;
		}	
		newcert = s;
	}
	return newcert;		
}

function CertNumValidation(pField)
{
	var bError = 'true';
	var sCertnumber= pField.value;
	if (sCertnumber != '')
	{
		if (sCertnumber.length == 8)
		{                       
			if (sCertnumber.substring(0,1) == 'a' || sCertnumber.substring(0,1) == 'A' || isNumeric(sCertnumber.substring(0,1)) == true)
			{ 
				if (isNumeric(sCertnumber.substring(1,sCertnumber.length)) == true)
				{                   
					bError = 'false';
				}
			}
		}
	}
	if (bError == 'true') 
	{
		DisplayError("\rInvalid Certificate Number!\r\rPlease retype the Certificate Number again.");
		promptSelect("\rPlease enter a Certificate Number and click the Enter button.",pField);
		pField.value = '';
		return false;
	}
	else 
	{
		return true;				
	}
}

function PadPhone(s)
{
	var Area = "";
	var phon3 = "";
	var phon4 = "";
	var digitCount = 0;
	var phoneNum = "";

	for (i=0; i<s.length;i++)
	{
		p = s.substring(i,i+1);
		for (j=0;j<digits.length;j++)
		{
   		   	n=digits.substring(j,j+1)                                   
			if (p == n)
			{
				if(digitCount <= 2)
				{
					Area = Area + p;
				}
				else if(digitCount <= 5)
				{
					phon3 = phon3 + p;
				}
				else if(digitCount <= 9)
				{
					phon4 = phon4 + p;
				}
				else if(digitCount <= 10)
				{
					Area = Area.substring(1,3) + phon3.substring(0,1);
					phon3 = phon3.substring(1,3) + phon4.substring(0,1);
					phon4 = phon4.substring(1,4) + p; 
				}

				digitCount ++;			
				break;
		}	}                                        
	}
	
	if(Area.length == 0 && phon3.length == 0 && phon4.length == 0)
	{
		Area = "   ";
		phon3 = "   ";
		phon4 = "    ";
	}

	phoneNum = "(" + Area + ") " + phon3 + "-" + phon4;
	return phoneNum;
}

function PhoneValidation(s)
{
	var phoneNum = "";

	if(isEmpty(s)) return false; 

	if(s == "(   )    -    ") return true;

	var Area = "";
	var phon3 = "";
	var phon4 = "";
	var digitCount = 0;

	for (i=0; i<s.length;i++)
	{
		p = s.substring(i,i+1);
		for (j=0;j<digits.length;j++)
		{
   		   	n=digits.substring(j,j+1)                                   
			if (p == n)
			{
				if(digitCount <= 2)
				{
					Area = Area + p;
				}
				else if(digitCount <= 5)
				{
					phon3 = phon3 + p;
				}
				else if(digitCount <= 9)
				{
					phon4 = phon4 + p;
				}
				else if(digitCount <= 10)
				{
					Area = Area.substring(1,3) + phon3.substring(0,1);
					phon3 = phon3.substring(1,3) + phon4.substring(0,1);
					phon4 = phon4.substring(1,4) + p; 
				}

				digitCount ++;			
				break;
		}	}                                        
	}

	if(Area.length == 3 && isInteger(Area))
	{
		if(phon3.length == 3 && isInteger(phon3))
		{
			if(phon4.length == 4 && isInteger(phon4))
			{
				return true;
			}
		}
	}
	
	return false;
}

function StateValidation(s)
{
	if(isEmpty(s)) 
	{
		return false; 
	}
	return((USStateCodes.indexOf(s) != -1) && (s.indexOf(USStateDeliminator) == -1))
}

function SpecialWhiteToPlus(s){
	var i = 0;
	var newstring = "";
	var c = s.length;
	while(c != i){
		if(isAlphaOrNum(s.charAt(i))){
			newstring = newstring + s.charAt(i);
		}
		else{
		if(s.charAt(i) == ')' || s.charAt(i) == '(' || s.charAt(i) == '-' || s.charAt(i) == '/' || s.charAt(i) == '|' || s.charAt(i) == '.')
		{
		newstring = newstring + s.charAt(i);
		}
		else
		{
		newstring = newstring + '+';
		}
		}
		i++;
	}
	return newstring;
}


// used to replace the spaces with plus signs before you submit the info to a Perl script
function WhiteToPlus(s)
{
	var i = 0;
	var newstring = "";
	var c = s.length;
	while(c != i)
	{
		if(isAlphaOrNum(s.charAt(i))){
			newstring = newstring + s.charAt(i);
		}
		else
		{
			if(s.charAt(i) == ")" || s.charAt(i) == "(" || s.charAt(i) == "-" || s.charAt(i) == "/" || s.charAt(i) == "|" || s.charAt(i) == "." || s.charAt(i) == "<" || s.charAt(i) == ">")
			{
				newstring = newstring + s.charAt(i);
			}
			else
			{		
				newstring = newstring + "+";
			}
		}
		i++;
	}
	return newstring;
}

function isAlphaOrNum(s)
{
	var i;
	var bNumeric = false;
	var bAlphabetic = false;
	for(i=0; i < s.length; i ++){
		var c = s.charAt(i);
		if(isLetter(c)){
			bAlphabetic = true;
		}		
		if(isDigit(c)){
			bNumeric = true;
		}
	}
	if((bNumeric == true) || (bAlphabetic == true)){
		return true;
	}
	else{
		return false;
	}
}

// used to replace the spaces with plus signs before you submit the info to a script
function WhiteToPlus(s){
	var i = 0;
	var newstring = "";
	var c = s.length;
	while(c != i){
		if(isAlphaOrNum(s.charAt(i))){
			newstring = newstring + s.charAt(i);
		}
		else{
			newstring = newstring + "+";
		}
		i++;
	}
	return newstring;
}

function BrowserIs()
{
	var agent = navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.nav = ((agent.indexOf('mozilla')!=-1) &&
			   ((agent.indexOf('spoofer')==-1) &&
			   (agent.indexOf('compatible') == -1)));
	this.nav2 = (this.nav && (this.major == 2));
	this.nav3 = (this.nav && (this.major == 3));
	this.nav4 = (this.nav && (this.major == 4));
	this.nav408up = (this.nav && (this.major >= 4 && this.minor >= 4.08))
	this.nav4up = (this.nav && (this.major >= 4));
	this.ie = (agent.indexOf("msie")!=-1);
	this.ie3 = (this.ie && (this.major == 2));
	this.ie4 = (this.ie && (this.major == 4));
	this.ie4up = (this.ie && (this.major >= 4));
	this.ie5 = (this.ie && (this.major == 5));
	this.opera = (agent.indexOf("opera")!=-1);
}


function validEmail(eAddr) 
{ 
      var chkDot = false;
      var usEmail = false;

      var lenSuffix = (usEmail) ? 4 : 3;
      var result = false;
      var ndxAt = ndxDot =  0;
          
      ndxAt  = eAddr.indexOf("@");
      ndxDot = eAddr.indexOf(".") ;
      ndxDot2 = eAddr.lastIndexOf(".") ;
          
      if ((ndxDot < 0) || (ndxAt < 0))
         alert("Your email address lacks '.' or '@'."); 
      else if (chkDot && (ndxDot < ndxAt) )
         chkDot = !(confirm("You entered a 'dot' before the '@'\nAre you sure that is right?") );
      else if ( (ndxDot2 - 3) <= ndxAt)
         alert("You are missing a domain name.\n\nThe format is 'you@ibeast.com'");
      else 
           result=true; 
          
      return result; 
} 

