var NAV_NEXT = 'NEXT';
var NAV_BACK = 'BACK';
var NAV_SAME = 'SAME';
var NAV_SKIP = 'SKIP';
var NAV_DELETE = 'DELETE';

var CMTETYPE_CANDIDATE = 0;

var DOCTYPE_ANNUAL = 118;
var DOCTYPE_PRE_ELECTION = 130;
var DOCTYPE_POST_ELECTION = 140;
var DOCTYPE_LATE_CONTRIBUTION = 135;
var DOCTYPE_SO = 103;
var DOCTYPE_DISSOLUTION = 119;
var DOCTYPE_MISCELLANEOUS = 150;

var ELECTYPE_PRIMARY = 0;
var ELECTYPE_GENERAL = 1;
var ELECTYPE_SCHOOL = 2;
var ELECTYPE_SPECIAL = 3;
var ELECTYPE_CAUCUS = 4;
var ELECTYPE_CONVENTION = 5;

var JURISDICTION_TYPE_CITY = 0;
var JURISDICTION_TYPE_TOWNSHIP = 1;
var JURISDICTION_TYPE_VILLAGE = 2;
var JURISDICTION_TYPE_COUNTY = 3;
var JURISDICTION_TYPE_SCHOOL_DISTRICT = 4;

var USEABLE_SCREEN_WIDTH_PERCENTAGE = 0.97
var USEABLE_SCREEN_HEIGHT_PERCENTAGE = 0.90;

var SENDMAILTO_CANDIDATE = "CAND";
var SENDMAILTO_COMMITTEE = "CMTE";
var SENDMAILTO_MAILING_ADDRESS = "MAIL";
var SENDMAILTO_TREASURER = "TREAS";
var SENDMAILTO_DESIGNATED_RECORDKEEPER = "DES";

var bPreventSubmit = 0;

/*
import java.awt.*;
import java.applet.*;
public class JavaBeep extends Applet{
  public void oneBeep() {
    Toolkit.getDefaultToolkit().beep();
    }
 }

function beep() {
   java.awt.Toolkit.getDefaultToolkit().beep();
   }
*/ 

function OpenAcrobatWindow(strPath) {
  var iUseableScreenWidth = screen.width * USEABLE_SCREEN_WIDTH_PERCENTAGE;
  var iUseableScreenHeight = screen.height * USEABLE_SCREEN_HEIGHT_PERCENTAGE;
  var iOneHalfWidth = iUseableScreenWidth / 2 + 20;
  var iOneThirdWidth = iUseableScreenWidth / 3 + 20;
  var iTwoThirdsWidth = 2 * iUseableScreenWidth / 3;
  var iScreenHeight = iUseableScreenHeight;
//alert(strPath);

//  var wwg = window.open(strPath,'winImage', 'height=' + iScreenHeight + ',width=' + iOneHalfWidth + ',left=' + iOneHalfWidth + ',top=0,scrollbars=yes,resizable=yes');
var wwg = window.open(strPath,'winImage', 'height=' + iScreenHeight + ',width=' + iTwoThirdsWidth + ',left=' + iOneThirdWidth + ',top=0,scrollbars=yes,resizable=yes,status=yes');
  return( wwg );
}


  function IndexSkipProcess() {
    frmMe.strNavigation.value = NAV_SKIP;
    frmMe.submit();
    return( false );
  }
  
  function IndexDeleteProcess() {
    if ( confirm( 'Are you sure you want to delete this document?' )) {
      frmMe.strNavigation.value = NAV_DELETE;
      frmMe.submit();
      return( false );
    }
    else {
      return( false );
    }
  }
  


//==================================================================
//LTrim(string) : Returns a copy of a string without leading spaces.
//==================================================================
function LTrim(str)
//
//   PURPOSE: Remove leading blanks from our string.
//   IN: str - the string we want to LTrim
//
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

//
//==================================================================
//RTrim(string) : Returns a copy of a string without trailing spaces.
//==================================================================
//
function RTrim(str)
//
//   PURPOSE: Remove trailing blanks from our string.
//   IN: str - the string we want to RTrim
//
//
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

//
//=============================================================
//Trim(string) : Returns a copy of a string without leading or trailing spaces
//=============================================================
//
function Trim(str)
//
//   PURPOSE: Remove trailing and leading blanks from our string.
//   IN: str - the string we want to Trim
//
//   RETVAL: A Trimmed string!
//
{
   return RTrim(LTrim(str));
}

function IsNumeric(strString) {
  //  check for valid numeric strings	
  var strValidChars = "0123456789.-";
  var strChar;
  var blnResult = true;

  if (strString.length == 0) return false;

  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++) {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1) {
      blnResult = false;
    }
  }
  return blnResult;
}

function TidyField(str) {
  return( Trim( str ).toUpperCase());
}


function TidyFormFields(frm) {
  var i;
  
  for ( i=0; i < frm.elements.length; i++ ) {
    if ( frm.elements[i].type.toLowerCase == 'text' ) {
      frm.elements[i].value = TidyField( frm.elements[i].value );
    }
  }
}

function EnsureValidCharsInFormFields(frm) {
  var i;
  
  for ( i=0; i < frm.elements.length; i++ ) {
    if ( frm.elements[i].type.toLowerCase() == 'text' ) {
      //alert(frm.elements[i].name.substr( 0, 6 ));
      if ( frm.elements[i].name.substr( 0, 6 ) != 'strdat' ) {
        if ( ! EnsureValidChars( frm.elements[i] )) {
          return( false );
        }
      }
    }
  }
  return( true );
}



function IsPositiveInteger(strString) {
  //  check for valid numeric strings	
  var strValidChars = "0123456789";
  var strChar;
  var blnResult = true;

  if (strString.length == 0) return false;

  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++) {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1) {
      blnResult = false;
    }
  }
  return blnResult;
}

function EnsurePositiveInteger(inp, strUserError) {
  var strValidChars = "0123456789";
  var strChar;
  var strResult = "";
  var strString = inp.value;
   
  if (strString.length > 0) {
    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length; i++) {
       strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) < 0) {
//         document.JavaBeep.oneBeep();
        if ( strUserError == '' ) {
          alert( 'Please enter a valid number.' );
        }
        else {
          alert( strUserError );
        }
        //inp.select();
        inp.focus();
        return( false );
       }
     }
  }
  return( true );
}


function EnsureValidChars(inp) {
  var strValidChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ -,.'abcdefghijklmnopqrstuvwxyz";
  var strChar;
  var strResult = "";
  var strString = inp.value;
   
  if (strString.length > 0) {
    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length; i++) {
       strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) < 0) {
//         document.JavaBeep.oneBeep();
        alert( 'This system allows only alphanumeric characters, hyphens, periods, commas and apostrophes.' );
        //inp.select();
        inp.focus();
        return( false );
       }
     }
  }
  return( true );
}


function EnsureState( inp ) {
//  var strState = Trim( inp.value);
  var strState = Trim( inp.value.toUpperCase());
  if ( strState.length == 0 ) {
    return( true );
  }
  else {
	  var strValidStates = "MI,AA,AE,AK,AL,AP,AR,AS,AZ,CA,CO,CT,DC,DE,FL,GA,HI,IA,ID,IL,IN,KS,KY,LA,MA,MD,ME,MI,MN,MO,MS,MT,NC,ND,NE,NH,NJ,NM,NV,NY,OH,OK,OR,PA,PR,RI,SC,SD,TN,TX,UT,VA,VI,VT,WA,WI,WV,WY,";
	  if (strValidStates.indexOf(strState + ',' ,0) == -1) {
	    alert( 'Please enter a valid state abbreviation.' );
	    inp.focus();
	    return( false );
	  }
	  else {
	    inp.value = strState;
	    return( true );
	  }
  }
}


function EnsureCurrency(strString) {
  var strValidChars = ".0123456789";
  var strChar;
  var strResult = "";
   
  if (strString.length == 0) return strString;

  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length; i++) {
     strChar = strString.charAt(i);
     if (strValidChars.indexOf(strChar) >= 0)
       strResult += strChar;
  }
  return strResult;
}

function GetRadioGroupValue( radGroup ) {
  //alert('1');
  var iLength = radGroup.length; 
  //alert( iLength );
  var iLoop;
  var strValue = '';
      
	for (iLoop=0; iLoop<iLength; iLoop++){
	  //alert( 'radGroup['+iLoop+'].checked == ' + radGroup[iLoop].checked );
		if (radGroup[iLoop].checked) {
  	  //alert( 'radGroup['+iLoop+'].value == ' + radGroup[iLoop].value );
      strValue = radGroup[iLoop].value;
		  break;
		}
	}
  return( strValue );
}

function SetRadioGroupValue( radGroup, strValue ) {
	var iLength = radGroup.length; 
  var iLoop;
  //alert( 'strValue == ' + strValue );
  for (iLoop=0; iLoop<iLength; iLoop++){
		//alert( 'radGroup[' + iLoop + '].value == ' + radGroup[iLoop].value );
    if (radGroup[iLoop].value == strValue) {
		  radGroup[iLoop].checked = true;
		  break;
		}
	}
}

function SetComboBoxValue( cbo, strValue ) {
  var iLength = cbo.length; 
  var iLoop;
	for (iLoop=0; iLoop<iLength; iLoop++){
		if ( cbo.options[iLoop].value == strValue) {
		  cbo.options[iLoop].selected = true;
		  break;
		}
	}
}

function OpenIndexWindow() {
  var iUseableScreenWidth = screen.width * USEABLE_SCREEN_WIDTH_PERCENTAGE;
  var iUseableScreenHeight = screen.height * USEABLE_SCREEN_HEIGHT_PERCENTAGE;
  var iOneHalfWidth = iUseableScreenWidth / 2;
  var iOneThirdWidth = iUseableScreenWidth / 3;
  var iTwoThirdsWidth = 2 * iUseableScreenWidth / 3;
  var iScreenHeight = iUseableScreenHeight;

//  return( window.open('Index00.php','winIndex00', 'height=' + iScreenHeight + ',width=' + iOneHalfWidth + ',left=0,top=0,scrollbars=yes,resizable=yes status=yes' ));
    return( window.open('Index00.php','winIndex00', 'height=' + iScreenHeight + ',width=' + iOneThirdWidth + ',left=0,top=0' + ',scrollbars=yes,resizable=yes status=yes' ));
}


function EnsureZipCode( inp ) {
  var bError = false;
  var strZip = Trim(inp.value);
  
  switch ( strZip.length ) {
  case 5:
    if ( ! (bError = ! IsPositiveInteger( strZip ))) {
      if (strZip == '00000') {
        bError = true;
      }
    }   
  case 0:
    break;
  default:
    bError = true;
  }
  
  if ( bError ) {
    alert( 'Please enter a valid zip code.' );
    inp.focus();
    return( false );
  }
  else {
    inp.value = strZip;
    return( true );
  }
}

function EnsurePlus4( inp ) {
  var bError = false;
  var strZip = Trim(inp.value);
  
  switch ( strZip.length ) {
  case 4:
    bError = ! IsPositiveInteger( strZip );
  case 0:
    break;
  default:
    bError = true;
  }
  
  if ( bError ) {
    alert( 'Please enter a valid Plus4 code.' );
    inp.focus();
    return( false );
  }
  else {
    inp.value = strZip;
    return( true );
  }
}


function EnsureAreaCode( inp ) {
  var bError = false;
  var strZip = Trim(inp.value);
  
  switch ( strZip.length ) {
  case 3:
    if ( ! (bError = ! IsPositiveInteger( strZip ))) {
      if (strZip == '000') {
        bError = true;
      }
    }   
  case 0:
    break;
  default:
    bError = true;
  }
  
  if ( bError ) {
    alert( 'Please enter a valid area code.' );
    inp.focus();
    return( false );
  }
  else {
    inp.value = strZip;
    return( true );
  }
}


function EnsureTx( inp ) {
  var bError = false;
  var strZip = Trim(inp.value);
  
  switch ( strZip.length ) {
  case 7:
    if ( ! ( bError = ! IsPositiveInteger( strZip ))) {
      if ( strZip.substr(0,1) < '2' ) {
        bError = true;
      }
    }
  case 0:
    break;
  default:
    bError = true;
  }
  
  if ( bError ) {
    alert( 'Please enter a valid telephone number.' );
    inp.focus();
    return( false );
  }
  else {
    inp.value = strZip;
    return( true );
  }
}


var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
      if(arr[index] == ele)
        found = true;
      else
        index++;
      return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
      if (input.form[i] == input)index = i;
      else i++;
      return index;
  }
  return true;
}
