//Initialize Variables
var tk_search_submtcnt = 0;


// Function Mouse Over
//
// Purpose is to set the mouse pointer and change color of menu item.
// 
// Parameters
//    Object Name, Color nbr.
//
// History:
// 07/15/99 initial development.
// 05/26/04 tpr 59752.  Added popup_onload 

function mOvr(src,clrOver) { 
 if (!src.contains(event.fromElement)) { 
     src.style.cursor = 'hand'; 
     src.bgColor      = clrOver;
 }
}

// Function Mouse Out
//
// Purpose is to set the mouse pointer to default and change color of menu item to default.
// 
// Parameters
//    Object Name, Color nbr.
//
// History:
// 07/15/99 initial development.
 
function mOut(src,clrIn) { 
 if (!src.contains(event.toElement)) { 
     src.style.cursor = 'default';
     src.bgColor      = clrIn; 
 }
}

// Date Format 
// Checks the date of the date object passed.
// callled from the onblur event.
// Format: MM/DD/YYYY. 
//  
// Parameter 
//    object name, display name
//
// History:
// 07/15/99 initial development.
// 09/02/99 RJB default 0 for day and month format.

function datefmt(src, fldmsg, DisFld) {
 curdate    = new Date();
 var err    = 0
 var valchr = "0123456789/";
 var tmp    = "";
 var i;

 dt = src.value  // Get Date value
 
 // Check for valid characters Numeric or forward slash
 for (i=0; i < dt.length; i++) {
     tmp = "" + dt.substring(i, i+1);
     if (valchr.indexOf(tmp) == "-1") err = 1;
 }

 if (dt.length < 1){
   if (DisFld != "")
    document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
   return true  // No Date entered
}

// Added Zeros Month and Day
 if (dt.substring(2, 3) != '/')  {
   if (dt.substring(1, 2) == '/') dt = '0' + dt  // Add zero to month
   if ((dt.length == 5) || (dt.length == 3)) dt = '0' + dt  // Add zero to month
 }
 
 if (dt.substring(4, 5) == '/') {
    dt = dt.substring(0,3) + '0' + dt.substring(3,10)  // Add zero to day
 }

 if (dt.length < 4) err=1

 if (dt.substring(2, 3) != '/') {
   dt = dt.substring(0, 2) + '/' + dt.substring(2, 10)
 }

 if (dt.substring(5,6) != '/') {
   dt = dt.substring(0, 5) + '/' + dt.substring(5, 10)
 }

 // Default year and or Century
 fyy = curdate.getFullYear();
 // Year and Century
 if (dt.length == 6) {
    dt = dt + fyy.toString();
 } 
 // Century
 if (dt.length == 8) {
    cen = fyy.toString(); 
    cen = cen.substring(0, 2)
    dt  = dt.substring(0, 6) + cen + dt.substring(6, 8)
 }

 // must be 10 now..
 if (dt.length != 10) err=1

 mm  = dt.substring(0, 2) // month
 dd  = dt.substring(3, 5) // day
 cc  = dt.substring(6, 8) // Century
 yy  = dt.substring(8, 10) // year

 if (mm.length == 1) mm = '0' + mm
 if (dd.length == 1) dd = '0' + dd

 if (mm < 1 || mm > 12) err = 1

 if (dd < 1 || dd > 31) err = 1

 if (yy < 0 || yy > 99) err = 1

 if (mm == 4 || mm == 6 || mm == 9 || mm == 11){
   if (dd == 31) err=1
 }

 if (mm == 2){
   var g=parseInt(yy / 4)
   if (isNaN(g)) {
     err=1
   }
   if (dd > 29) err=1
   if (dd == 29 && ((yy / 4)!=parseInt(yy / 4))) err=1
 }

 if (err==1) {
    if (DisFld == ""){
     // Error message, set field focus
     msg  = 'E Payment \n\n'
     msg += '_________________________________________________________ \n\n'
     msg += fldmsg + ' is invalid. Format: MM/DD/YYYY \n'
     msg += '_________________________________________________________ \n\n'
     alert(msg);
   }
   else
     document.forms[0].elements[DisFld].src = "../images/messages/InvDate.gif";
   src.focus();
   return false;
 }
 else{
   if (DisFld != "")
     document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
 } 
 
 
 // Set field with format
  src.value = dt

  return true
}


// Init Search Page
//
// Purpose is to perform any initialization to the search page.  Currently
// this function just set the initial field focus.
// 
// Parameters
//    Start Field. (Name of field)
//    Name or value (prompt to the field name or the value of the field.)
//
// History:
// 05/12/00 RJB initial development.
// 10/19/01 RJB added logic for field value.

function initpage(intfld, nv) { 

 // if the parameter is prompt field, prompt to the value of that field.
  if (nv == "V") {
      prmpt = document.forms[0].elements[intfld].value 
      if (prmpt != "") {
         document.forms[0].elements[prmpt].focus();
      }
  }

// Prompt to field name  
  if (intfld != "" && document.forms[0].elements[intfld].type != "hidden") {
   // Set Field focus if passed and not hidden.
     if (intfld != "" ) {
         document.forms[0].elements[intfld].focus();
     }
  }  
  
   return true;
   
}

// Format SSN
//
// This function checks the format of the SSN number entered. This to 
// verify that a valid ssn is entered for searching and case init.
//
//  Parameters
//    Object Pointer for ssn.
//
//  History
//    12/14/2000 RJB Initial coding.

function CheckSSN(src, DisFld) {

ssn = src.value;  // Value of SSN

if (ssn.length < 1) {
    if(DisFld != ""){
       document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";}
    return false;
}

var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
var numDashes = ssn.split('-').length - 1;

  if (matchArr == null || numDashes == 1) {
     if(DisFld == ""){
        msg  = 'E Payment \n\n'
        msg += '_________________________________________________________ \n\n'
        msg += ' SSN is Invalid. Must be 9 digits format: NNN-NN-NNNN.    \n'
        msg += '_________________________________________________________ \n\n'
        alert(msg);
     }
     else{
        document.forms[0].elements[DisFld].src = "../images/messages/InvSSN.gif";
     }     
     src.focus();
     return false;
  }
  else{
    if(DisFld != "")
       document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
  }
  

  if (parseInt(matchArr[1],10)==0) {
       if(DisFld == ""){
        msg  = 'E Payment \n\n'
        msg += '_________________________________________________________ \n\n'
        msg += ' SSN is Invalid. Can not start with 000.                  \n'
        msg += '_________________________________________________________ \n\n'
        alert(msg);
     }
     else{
        document.forms[0].elements[DisFld].src = "../images/messages/InvSSNZero.gif";
     }
     src.focus()
     return false;   
  }
  else{
    if(DisFld != "")
         document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
  }
  
 // Format SSN Number Entered.
 if (ssn.substring(3, 4) != '-') {
    ssn = ssn.substring(0, 3) + '-' + ssn.substring(3, 12)
 }

 if (ssn.substring(6,7) != '-') {
   ssn = ssn.substring(0, 6) + '-' + ssn.substring(6, 12)
 }

 // Format Number
 src.value = ssn;

 return true;    
  
}

// Required Field
// 
// This function checks to see if the current object
// was entered.  An image is loaded in the passed 
// display field
//
// Parameters
// Object
// Display field (Image)
// Display Image
//
// History
// 12/14/2000  RJB Initial development
//
function ReqField(src,DisFld,DisImg) {

 if (src.value == "") {
     document.forms[0].elements[DisFld].src = DisImg    
        
 } else {
     document.forms[0].elements[DisFld].src = "../images/blankimage.gif"
 }
 

 return true;

}

// Verify Statement
//
function VerfyStat() {

 var msg

 if (!document.forms[0].elements["ACCEPT_CHK.PROFILE.CRTV"].checked) {
    msg  = 'E Payment \n\n'
    msg += '_________________________________________________________ \n\n'
    msg += 'Payment of Traffic Ticket has been aborted. The accept \n'
    msg += 'box was not check to indicate acceptance of the statements. \n'
    msg += '_________________________________________________________ \n\n'
    alert(msg);

    history.back();
    return false;
  }

  return true;
 
}


// Phone Number Format 
// Checks the phone number of the object passed.
// callled from the onblur event.
// Format: (999) 999-9999
//  
// Parameter 
//    object name, display name
//
// History:
// 12/28/00 initial development.

function Phonefmt(src,fldmsg, DisFld) {

 var err    = 0
 var valchr = "0123456789-()";
 var tmp    = "";
 var i;

 ph = src.value  // Get Date value
 

 // Check for valid characters Numeric or forward slash
 for (i=0; i < ph.length; i++) {
     tmp = "" + ph.substring(i, i+1);
     if (valchr.indexOf(tmp) == "-1") err = 1;
 }


 if (ph.length < 1){  // No Phone number entered
    if (DisFld != "")
      document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
    return true;  
 }

 if (ph.length < 10) err=1       // Must be 10 characters


// Close area code
 if (ph.substring(0, 1) != '(') {
    ph = '(' + ph.substring(0,14)
 }

 if (ph.substring(4, 5) != ')') {
    ph = ph.substring(0, 4) + ')' + ph.substring(4, 14)
 }

// Set Space between area code and number
// if (ph.substring(5,6) != ' ') {
//   ph = ph.substring(0, 5) + ' ' + ph.substring(5, 14)
// }
// Dash in number format
 if (ph.substring(8,9) != '-') {
     ph = ph.substring(0, 8) + '-' + ph.substring(8, 14)
 }

if (ph.length != 13) err=1       // Must be 14 characters now

 if (err==1) {
   // Error message, set field focus
   if (DisFld == ""){
      msg  = 'E Payment \n\n'
      msg += '_________________________________________________________ \n\n'
      msg += fldmsg + ' is invalid. Format: (999)999-9999 \n'
      msg += '_________________________________________________________ \n\n'
      alert(msg);
   }
   else 
      document.forms[0].elements[DisFld].src = "../images/messages/InvPhn.gif";
   
   src.focus()
   return false
 }
 else {
   if (DisFld != "")
      document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
 }

   // Set field with format
  src.value = ph

  return true
}



// Email Format Validate
//
// This function checks for a valid email address.  This is to 
// insure that a valid email address was entered.
//
// Parameters 
//    object name, display name
//
// History
// 01/16/2001 RJB Initial setup
// 10/24/2001 SLH Added option for image display instead of pop-up
//
function EmailFmt(src, fldmsg, DisFld) {

emailStr = src.value

// No address entered
if (emailStr.length < 1){
   if (DisFld != "")
//     document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";   
   return true;
}

// Address pattern user@domain 
var emailPat=/^(.+)@(.+)$/

// Special Chars pattern
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"

// Range of characters allowed pattern
var validChars="\[^\\s" + specialChars + "\]"

// user name is in quotes "user" pattern   E.g. "jiminy cricket"@disney.com
var quotedUser="(\"[^\"]*\")"

// domains that are IP addresses pattern 
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

// series of non-special characters) 
var atom=validChars + '+'

// Typical username.
//   i.e. john.doe@somewhere.com Basically, a word is either an atom or quoted string.
var word="(" + atom + "|" + quotedUser + ")"

// structure of the user pattern
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

// normal symbolic domain,pattern as opposed to ipDomainPat, shown above. 
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

// Basic Vaildation of format. 
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  if (DisFld == ""){
    msg  = 'E Payment \n\n'
    msg += '_________________________________________________________ \n\n'
    msg += fldmsg + ' is invalid. i.e. John_Doe@aol.com \n'
    msg += '_________________________________________________________ \n\n'
    alert(msg);
  }
  else
    document.forms[0].elements[DisFld].src = "../images/messages/InvMail.gif";

  src.focus()
  return false
}

else {
  if (DisFld != "")
     document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
}


var user=matchArray[1]
var domain=matchArray[2]

// User ID Validation 
if (user.match(userPat)==null) {
   // user is not valid
   if (DisFld == ""){
     msg  = 'E Payment \n\n'
     msg += '_________________________________________________________ \n\n'
     msg += fldmsg + ' user name is invalid. \n'
     msg += '_________________________________________________________ \n\n'
     alert(msg);
   }
   else
     document.forms[0].elements[DisFld].src = "../images/messages/InvMailUsr.gif";
     
   src.focus()
   return false
}
else{
  if (DisFld != "")
     document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
}



// Domain is an IP check format 
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	       if (DisFld == ""){
                  msg  = 'E Payment \n\n'
                  msg += '_________________________________________________________ \n\n'
                  msg += fldmsg + ' estination IP address is invalid. \n'
                  msg += '_________________________________________________________ \n\n'
                  alert(msg);
               }
               else
                  document.forms[0].elements[DisFld].src = "../images/messages/InvMailIP.gif";
           
               src.focus()
	       return false
	    }
	    else{
	       if (DisFld != "")
	         document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
	    }

	    
        }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
    if (DisFld == ""){
      msg  = 'E Payment \n\n'
      msg += '_________________________________________________________ \n\n'
      msg += fldmsg + ' domain name is invalid. \n'
      msg += '_________________________________________________________ \n\n'
      alert(msg);
    }
    else
      document.forms[0].elements[DisFld].src = "../images/messages/InvMailDom.gif";
    src.focus()
    return false
}
else{
   if (DisFld != "")
      document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
}
	         
	         




// Domain name must end with a three-letter word (like com, edu, gov) or a two-letter word,
// representing country (uk, nl)
   
// Break up the domain to get a count of how many atoms it consists of.
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   if (DisFld == ""){
      msg  = 'E Payment \n\n'
      msg += '_________________________________________________________ \n\n'
      msg += fldmsg + ' must end with a three letter domain or a two \n'
      msg += 'letter country. \n'
      msg += '_________________________________________________________ \n\n'
      alert(msg);
   }
   else
      document.forms[0].elements[DisFld].src = "../images/messages/InvMailEnd.gif";
   
   src.focus()
   return false
}
else{
   if (DisFld != "")
      document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
}
	         


// Check for host name preceding the domain.
if (len<2) {
   if (DisFld == ""){
     msg  = 'E Payment \n\n'
     msg += '_________________________________________________________ \n\n'
     msg += fldmsg + ' host name is missing. \n'
     msg += '_________________________________________________________ \n\n'
     alert(msg);
   }
   else
      document.forms[0].elements[DisFld].src = "../images/messages/InvMailHst.gif";
   
   src.focus()
   return false
}
else{
   if (DisFld != "")
      document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
}


// Return True Valid
return true;
}

// Accepted Credit Card Types Check
//
// This function checks the first digit of the CC number 
// and insurances that the credit card number enterd is 
// for an accepted card type.
//
// History
// 03/02/2001 RJB Initial development
function chk_nbr(src) {

 var fdigit = "";
 ccnbr = src.value;
 
 
 fdigit = ccnbr.substring(0, 1);
 cclst  = document.forms[0].elements["CC_LIST.PROFILE.CRTV"].value; 
 
 if (fdigit == "") return true;
 if (cclst.indexOf(fdigit) == "-1") {
   msg  = 'E Payment \n\n'
   msg += '_________________________________________________________ \n\n'
   msg += 'Credit card is not accepted.  Please enter an accepted    \n'
   msg += 'type.                                                     \n'
   msg += '_________________________________________________________ \n\n'
   alert(msg);
   src.focus();

   return false;
 }
     
// Luhn Check (Mod 10)
// 
// This section applies the luhn check t the number to 
// verify that a vaild number was entered.
  
  var nbrdigits = ccnbr.length;
  var oddnbr = nbrdigits & 1;   // Bitwise op (set to true if odd number of digits)
  var tot = 0;
  var dig;
  var sub = 0;
  
  // Spin through digits
  for (sub = 0; sub < nbrdigits; sub++) {
    dig = parseInt(ccnbr.charAt(sub));
    if (!((sub & 1) ^ oddnbr)) {
        dig *= 2;
        if (dig > 9) dig -= 9;
    }
   tot += dig;
  }
  
  // Remanider Falied Mod 10 Test.
  if (tot % 10 != 0) {
     msg  = 'E Payment \n\n'
     msg += '_________________________________________________________ \n\n'
     msg += 'Credit card number entered is not valid. Please check     \n'
     msg += 'account number.                                           \n'
     msg += '_________________________________________________________ \n\n'
     alert(msg);
     src.focus();

   return false;   
  
  }
  
  
  return true;

}

// Function To Capitalize
//
// Purpose is capitalize an input string
// 
// Parameters
//    String Source
//
// History:
// 10/08/01 Initial Development SLH

function ToCaps(src) { 
  fld =  src.value 
  src.value = fld.toUpperCase();
}

// Zip Code Format Validation
//
// Validate that the zip code was entered in the correct format.
//
// Parameters 
//   String Source
//
// History   
// 10/17/01 RJB Initial Development
// 10/24/01 SLH Added option for image file instead of pop-up
//
function FmtZip(zip, DisFld) 

{
  var valid = "0123456789-";
  var hyphencount = 0;

  src = zip.value;
  if (src.length == 0){
     if (DisFld != "")
        document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
     return true;
  }
 
 
  if (src.length != 5 && src.length != 10){	
   if (DisFld == ""){
     msg  = 'E Payment \n\n'
     msg += '_________________________________________________________ \n\n'
     msg += 'Zip Code format is not valid,  NNNNN or NNNNN-NNNN        \n'
     msg += '_________________________________________________________ \n\n'
     alert(msg);
   }    
   else{ 
     document.forms[0].elements[DisFld].src = "../images/messages/InvZipFmt.gif";
   }   
   zip.focus();
   return false; 
 }
 else{ 
  if (DisFld != "")
    document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
 }


for (var i=0; i < src.length; i++) {
temp = "" + src.substring(i, i+1);
if (temp == "-") hyphencount++;
if (valid.indexOf(temp) == "-1") {
  if (DisFld == ""){
    msg  = 'E Payment \n\n'
    msg += '_________________________________________________________ \n\n'
    msg += 'Invalid characters entered,  NNNNN or NNNNN-NNNN        \n'
    msg += '_________________________________________________________ \n\n'
    alert(msg);
  }
  else
    document.forms[0].elements[DisFld].src = "../images/messages/InvZipChar.gif";
  
  zip.focus();
  return false;
}


if ((hyphencount > 1) || ((src.length ==10) && ""+ src.charAt(5) !="-")){
  if (DisFld == ""){
    msg  = 'E Payment \n\n'
    msg += '_________________________________________________________ \n\n'
    msg += 'Invalid format with hyphen entered,  NNNNN or NNNNN-NNNN  \n'
    msg += '_________________________________________________________ \n\n'
    alert(msg);
  }
  else {
    document.forms[0].elements[DisFld].src = "../images/messages/InvZipHyp.gif";
       }
  zip.focus();
  return false;

}


}
if (DisFld != "")
     document.forms[0].elements[DisFld].src = "../images/messages/blankimage.gif";
return true;
}



// Traffic Ticket  Search Criteria Submission Check
//
// This function checks to see if the search criteria 
// has already been submited. The 
// button text is also changed.
//
// SLH 01/15/2002
//
function tk_search_subchk() {

// Change Message
document.forms[0].elements["TK_SEARCH.PAPROFILE.PAM"].value = " Searching "; 

// Submitted Search Criteria
if (tk_search_submtcnt > 0)  {
  return false;
}
   
// Up count   
 tk_search_submtcnt++;

 return true;
 
 
}

// Currency Format
//
// This functions formats the characters enterd to a 
// currency format.
//
//  RJB 04/05/2002
//
function currencyFormat(src) {

 var nbr = src.value;
 var valchr = "0123456789., ";
 var tmp    = "";
 var i;
 var temp;

// Remove any special characters
  nbr = nbr.toString().replace(/\$|\,/g,'');

 // Check for valid characters Numeric or . ,
 for (i=0; i < nbr.length; i++) {
     tmp = "" + nbr.substring(i, i+1);
     if (valchr.indexOf(tmp) == "-1") {
         return false;
      }
 }


  if(isNaN(nbr)) nbr = "0";
  
  
// Decimal
//nbr = Math.floor(nbr*100+0.50000000001);


nbr = nbr * 100;
 
//  SLH TPR  44910  Added following line because of errors
//  caused by nature of floating point arithmetic.
//  The above calcuation (nbr * 100) approproximates certain numbers
//  ex. 9.20 * 100 = 919.999999
//  Therefore, we will round number to 'correct' these approximations.
//  This will replace the floor function below which was incorrectly
//  rounding down numbers such as 919.99999.

nbr = Math.round(nbr);

// Round down
//nbr = Math.floor(nbr);


// Split the cents so we can added the decimal point

  cents = nbr % 100;

  nbr = Math.floor(nbr/100).toString();

// Pad
  if (cents < 10) cents = "0" + cents;
  
  

// Insert commas
  for (var i = 0; i < Math.floor((nbr.length-(1+i))/3); i++) {
     nbr = nbr.substring(0,nbr.length-(4*i+3)) +',' + nbr.substring(nbr.length-(4*i+3));
  }

  src.value = nbr + '.' + cents;

  return true;
 
}



// Trim leading spaces from field
//
//
// SLH 04/18/2002
//
function trim_leading(field) {
  
  while (field.value.substring(0,1) == " ") {
  
     field.value = field.value.substring(1);
  
  }
   

  return true;
 
 
}


// Issue Pop-Up Message
//
//
// SLH 05/26/2004
//
function popup_msg(field) {
  
  var msg;
  var answer;

 
  msg = document.forms[0].elements[field].value;
   
  
  if (msg!= ""){
  
    if (window.confirm(msg) == false) history.back();  // Cancel or Close            
    
  };
  
  //Clear out value of field so that will not redisplay
  document.forms[0].elements[field].value = ""
  
  //Prompt to correct field
  focusFirstFormElement();
  
  return true;
 
}


/* 
 * This function brings focus to the first available element in
 * the first form on the page.
 * onLoad="focusFirstFormElement()"
 *
 * History:
 * 05/20/2004 ERG Initial Development.
 *
 */
function focusFirstFormElement() {
	if (document.forms[0] != null) {
  	for (i = 0; i < document.forms[0].elements.length; i++) {
    	if ((document.forms[0].elements[i].type != "hidden") &&
      	  (document.forms[0].elements[i].disabled != true) &&
      	  (document.forms[0].elements[i].readOnly != true)) {
        document.forms[0].elements[i].focus();
        return;
      }
    }
  }	
}


