 function checkDigit(inputName, emptyFound) {
  var frm=document.forms['eform'];
  var patternSize = /\d/;
  var inputStr = frm.elements[inputName].value.match(patternSize);
  if (inputStr == null) {
    emptyFound = emptyFound + ReturnResponse(inputName) + '\n';
    return emptyFound;
  }
  return emptyFound; 
 }

 // check if form field is empty and return true(==1)
// also add field name to emptyField
 function checkFieldLength(inputName, emptyFound, fieldSize) {
  var frm=document.forms['eform'];
  var patternSize 
  if (fieldSize == 4)
    patternSize = /\d{4}/;
  else if (fieldSize == 5)
    patternSize = /\d{5}/;
  else if (fieldSize == 1)
    patternSize = /\d{1}/;
  else if (fieldSize == 2)
    patternSize = /\d{2}/;
  else
    patternSize = /\d{3}/;
 if (!frm.elements[inputName])
   alert('Error with field: '+inputName);
  var inputStr = frm.elements[inputName].value.match(patternSize);
 if (frm.elements[inputName].type == 'select-one') {
  if (frm.elements[inputName].selectedIndex == 0) {
    emptyFound = emptyFound + ReturnResponse(inputName) + '\n';
    return emptyFound;
   }
 } else {
  if (inputStr == null) {
    emptyFound = emptyFound + ReturnResponse(inputName) + '\n';
    return emptyFound;
  }
 } // else type if statement
    return emptyFound;
 }  // end checkField

function checkAccount2(inputName, emptyFound) {
	var frm=document.forms['eform'];
	var accountcode = frm.elements[inputName].value;
    if ( (accountcode.indexOf('(')==10) && (accountcode.indexOf(')')==14) ){
        accountcode = accountcode.substr(0,10)+accountcode.substr(11,3);
    }
	var mul = 2;
	var result = 0;
	var sum = 0;
	for (i=accountcode.length-2;i>=0;i--) {
		result = mul * (accountcode.charAt(i)/1);
		if (mul == 2) { mul = 1; } else { mul = 2; }
		if (result > 10) {
			sum += result-10+1;
		} else if (result == 10) {
			sum += 1;
		} else {
			sum += result;
		}
	}
	sum = sum % 10;
	if (sum != 0) sum = 10 - sum;

	if ( (sum == (accountcode.charAt(accountcode.length-1)/1) ) && (frm.elements[inputName].value.length!=0) && ((accountcode.length==13)||(accountcode.length==16)) ) {
		return emptyFound
	} else {
		emptyFound = emptyFound + ReturnResponse(inputName)+'\n';
		return emptyFound
	}
}

function doAutoTab(e, maxLength, objNext) {

        var charCode
        var source

        if (document.all) {
                // Get the character code of the key pressed
                charCode = window.event.keyCode
                // Get a handle on the element (control) that raised the event
                source = window.event.srcElement
        } else {
                // Get the character code of the key pressed
                charCode = e.which
                // Get a handle on the element (control) that raised the event
                source = e.target
        }

        // If the key was a TAB or BACKTAB, then select all
        if (charCode == 9 || charCode == 16) {
                source.select()
        }
        // Check If the key is a valid alphanumeric key
        if ((charCode >= 48 && charCode <= 57) || (charCode >= 96 && charCode <= 105)) {
                // If maxLength is exceeded, set focus to next control
                if (source.value.length >= maxLength) {
                        objNext.focus()
                        // If next control is a textbox, select all text inside
                        if (objNext.type == "text") {
                                objNext.select()
                        }
                }
        }
}

function checkPostal(inputName, emptyFound) {
 var frm=document.forms['eform'];
 if (!frm.elements[inputName])
   alert('Error with field: '+inputName);
 var patternPostal = /[A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]/;
 var inputStr = frm.elements[inputName].value.match(patternPostal);
 if (inputStr == null) {
  emptyFound = emptyFound + ReturnResponse(inputName)+'\n';
 }
  return  emptyFound;
} 

function checkExtension(inputName, emptyFound) {
 var frm=document.forms['eform'];
 var extError = '';
  if ( (frm.elements[inputName+'Ext']) && (frm.elements[inputName+'Ext'].value.length >0) ){
    extError = checkFieldLength(inputName+'Ext', extError, frm.elements[inputName+'Ext'].value.length);
  }
  if (extError!='') {
   emptyFound = emptyFound + ReturnResponse(inputName)+'\n';
  }


  return  emptyFound;
}

function checkZip(inputName, emptyFound) {
 var frm=document.forms['eform'];
 if (!frm.elements[inputName])
   alert('Error with field: '+inputName);
 var patternZip5 = /\d{5}/;
 var patternZip10 = /\d{5}\-\d{4}/;
 var inputStr;
 var errorLevel=0;
 if (frm.elements[inputName].value.length==5) {
   inputStr = frm.elements[inputName].value.match(patternZip5);
   if (inputStr == null)
     errorLevel=1;
 } else if (frm.elements[inputName].value.length==10) {
   inputStr = frm.elements[inputName].value.match(patternZip10);
   if (inputStr == null)
     errorLevel=1;
 } else
     errorLevel=1;

 if (errorLevel == 1) {
  emptyFound = emptyFound + ReturnResponse(inputName)+'\n';
 }
  return  emptyFound;
} 

function checkDate (inputYear, inputMonth, inputDay, emptyFound) {
 var frm=document.forms['eform'];
 var year = frm.elements[inputYear].value/1;
 var month = frm.elements[inputMonth].selectedIndex;
 var day = frm.elements[inputDay].value/1;
 var errorLevel=0;
 var pattern = /\D/;
 var inputStr;
 if (frm.elements[inputYear].selectedIndex == 0) {
  emptyFound = emptyFound + ReturnResponse(inputYear)+'\n';
  errorLevel=1;
 }
 if (frm.elements[inputMonth].selectedIndex == 0) {
  emptyFound = emptyFound + ReturnResponse(inputMonth)+'\n';
  errorLevel=1;
 }
 inputStr = frm.elements[inputDay].value.match(pattern);
 if ( (inputStr != null) || (frm.elements[inputDay].value.length==0) ){
  emptyFound = emptyFound + ReturnResponse(inputDay)+'\n';
  errorLevel=1;
 }

 if (errorLevel == 0) {
   if ( ((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30) ) {
     errorLevel=1;
     emptyFound = emptyFound + ReturnResponse(inputDay)+'\n';
   } else if (day < 1) {
     errorLevel=1;
     emptyFound = emptyFound + ReturnResponse(inputDay)+'\n';
   } else if ( (month == 2) && (day > 28) ) {
     errorLevel=1;
     emptyFound = emptyFound + ReturnResponse(inputDay)+'\n';
   } else if ( (day > 31) ) {
     errorLevel=1;
     emptyFound = emptyFound + ReturnResponse(inputDay)+'\n';
   }
   if ( (month > 12) || (month < 1) ) {
     errorLevel=1;
     emptyFound = emptyFound + ReturnResponse(inputMonth)+'\n';
   }

 }
   if (frm.elements[inputDay].value.length == 1)
     frm.elements[inputDay].value = '0' + frm.elements[inputDay].value;

 return emptyFound;
}

function checkAccount(inputName, emptyFound) {
 var frm=document.forms['eform'];
 var pattern13 = /\d{13}/;
 var pattern15 = /\d{10}\(\d{3}\)/;
 var pattern16 = /\d{16}/;
 var inputStr;
 var errorLevel=0;
 if (frm.elements[inputName].value.length==13) {
   inputStr = frm.elements[inputName].value.match(pattern13);
   if (inputStr == null)
     errorLevel=1;
 } else if (frm.elements[inputName].value.length==16) {
   inputStr = frm.elements[inputName].value.match(pattern16);
   if (inputStr == null)
     errorLevel=1;
 } else if (frm.elements[inputName].value.length==15) {
   inputStr = frm.elements[inputName].value.match(pattern15);
   if (inputStr == null)
     errorLevel=1;
 } else {
     errorLevel=1;
 }
 if (errorLevel == 1) {
  emptyFound = emptyFound + ReturnResponse(inputName)+'\n';
 }
  return  emptyFound;
} 

function ltrim ( s )
{
	return s.replace( /^\s*/, "" )
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

function trim ( inputName )
{
  var frm=document.forms['eform'];
  if (!frm.elements[inputName])
    alert('Trim Error with field: '+inputName);
  frm.elements[inputName].value=rtrim(ltrim(frm.elements[inputName].value));
}

// checkPhoneAllorNone
// if anything is placed in the phone number field then it validates that it's all
// digits, also if an extension field exists it validates that too.

 function checkPhoneAllorNone(inputName, emptyFound) {
  var frm=document.forms['eform'];
  var phoneError='';
  var returnMessage = '';
  var patternSize = /\D/;
  if ( (frm.elements[inputName+'Area'].value.length!=0) || (frm.elements[inputName+'1'].value.length!=0) || (frm.elements[inputName+'2'].value.length!=0) ) {
    phoneError = checkFieldLength(inputName+'Area', phoneError, 3);
    phoneError = checkFieldLength(inputName+'1', phoneError, 3);
    phoneError = checkFieldLength(inputName+'2', phoneError, 4);
  }
  if (phoneError != '') {
   emptyFound = emptyFound + ReturnResponse(inputName)+'\n';
  }
//  emptyFound = checkExtension(inputName, emptyFound);
  
  return emptyFound;
 }
 
 // this is a more rigorous check and validates that the phone number fields
// contain digits only and are of proper length.
 function checkPhone2(inputName, emptyFound) {
   var frm=document.forms['eform'];
   var empty = '';
   empty = checkFieldLength(inputName+'Area',empty,3);
   empty = checkFieldLength(inputName+'1',empty,3);
   empty = checkFieldLength(inputName+'2',empty,4);
   if (empty != '') emptyFound = emptyFound + ReturnResponse(inputName) + '\n';

//   emptyFound = checkExtension(inputName, emptyFound);
   
   return emptyFound;
 }
