function checkChoice(field, i) {
  if (i == 0) { // "All" checkbox selected.
    if (field[0].checked == true) {
      for (i = 1; i < field.length; i++)
      field[i].checked = false;
     }
  }  else  {  // A checkbox other than "Any" selected.
    if (field[i].checked == true) {
      field[0].checked = false;
    }
  }
}

function uppercase(t) {
  var oldletter = t.value;
  t.value = t.value.toUpperCase();
}

function lettersonly(t) {
  var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  var ok = "yes";
  var temp;
  for (var i=0; i<t.value.length; i++) {
    temp = "" + t.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") {
      ok = "no";
    }
    if (ok == "no") {
      alert("Invalid entry!  Only letters allowed!");
      t.focus();
      t.select();
    }
  }
}

function numbersonly(t) {
  var valid = "1234567890-,."
  var ok = "yes";
  var temp;
  for (var i=0; i<t.value.length; i++) {
    temp = "" + t.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") {
      ok = "no";
    }
    if (ok == "no") {
      alert("Invalid entry!  Only numbers allowed!");
      t.focus();
      t.select();
    }
  }
}

function wholenumbersonly(t) {
  var valid = "1234567890"
  var ok = "yes";
  var temp;
  for (var i=0; i<t.value.length; i++) {
    temp = "" + t.value.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") {
      ok = "no";
    }
    if (ok == "no") {
      alert("Invalid entry!  Only numbers allowed!");
      t.focus();
      t.select();
    }
  }
}

function textCounter(t,maxlimit) {
  if (t.value.length > maxlimit) {// if too long...trim it!
    t.value = t.value.substring(0, maxlimit);
    alert("255 Characters are the limit to this field.  Please re-phrase your answer.");
    return;
  }
}

function currencymask(t) {
var patt = /(\d*)\.{1}(\d{0,2})/;
var donepatt = /^(\d*)\.{1}(\d{2})$/;
var str = t.value;
var result;
if (!str.match(donepatt))
{result = str.match(patt);
if (result!= null)
{t.value = t.value.replace(/[^\d]/gi,'');
str = result[1] + '.' + result[2] ;
t.value = str;
}else{
if (t.value.match(/[^\d]/gi))
t.value = t.value.replace(/[^\d]/gi,'');}
}
}


function datestrict(t) {
  var patt = /(\d{2}).*(\d{2}).*(\d{4})/;
  var donepatt = /^(\d{2})\-(\d{2})\-(\d{4})$/;
  var str = t.value;
  var result;
  if (!str.match(donepatt)) {
    result = str.match(patt);
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}


function datemask(t) {
  var patt = /(\d{2}).*(\d{2}).*(\d{4})/;
  var donepatt = /^(\d{2})\-(\d{2})\-(\d{4})$/;
  var str = t.value;
  var sleng = str.length;
  if (sleng < 8) {
    alert("There are 8 digits in a complete date, please enter the entire number including preceding 0's in single digit numbers");
    return;
  }
  var result;
  if (!str.match(donepatt)) {
    result = str.match(patt);
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
      var day = Number(result[2]);
      var month = (Number(result[1]) - 1);
      var year = result[3];
// Add date check here
      if (month > 11) {
	      alert("There are only 12 months in a month, please try again.");
	      t.value = '';
	      return;
      }
      var myDate = new Date(year,month,day);
      var myMonth = myDate.getMonth();
      if (myMonth != month) {
		alert('That date is not valid!');
		t.value = '';
		return;
      }
// End of date verification
      str = result[1] + '-' + result[2] + '-' + result[3];
      t.value = str;
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}

function checkdate(cday, cmonth, cyear) {
	var myDatestr = cday + ' ' + cmonth + ' ' + cyear;
	var myDate = new Date(cyear, cmonth, cday);
	var myDate_String = myDate.toGMTString();
	var myDate_array = myDate_string.split(' ');
	
	if (myDate_array[2] != cmonth) {
		alert('That date is not valid!');
		return;
	}
}

function timestrict(t) {
  var tpatt = /^(\d{2})\:(\d{2}) (\d{2})$/;
  var pattt = /(\d{2}).*(\d{2})/;
  var str = t.value;
  if (!str.match(tpatt)) {
    result = str.match(pattt);
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
      t.value = str;
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}

function timemask(t) {
  var tpatt = /^(\d{2})\:(\d{2}) (\d{2})$/;
  var pattt = /(\d{2}).*(\d{2})/;
  var str = t.value;
  if (str < 10) { str = "000" + str; } else if (str < 100) { str = "00" + str; } else if (str < 1000) { str = "0" + str; }
  if (!str.match(tpatt)) {
    result = str.match(pattt);
    result[1] = parseFloat(result[1]);
    if (result[1] > 23) {
      alert("There are only 23 hours in a Military time clock, please reduce the hour");
      t.value = t.value.replace(/[\d]/gi,'');
      return;
    }
    if (result[2] > 59) {
      alert("There are only 59 minutes in a Military time clock, please reduce the minutes");
      t.value = t.value.replace(/[\d]/gi,'');
      return;
    }
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
      if (result[1] > 11) {
        result[1] = result[1] - 12;
        if (result[1] < 1) {
          str = '12:' + result[2] + ' pm';
        } else {
          str = result[1] + ':' + result[2] + ' pm';
        }
      } else {
        if (result[1] < 1) { 
          str = '12:' + result[2] + ' am';
        } else {
          str = result[1] + ':' + result[2] + ' am';
        }
      }
      t.value = str;
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}

function zipcodestrict(t) {
  var patt = /(\d{5}).*(\d{5})/;
  var donepatt = /^(\d{5})\-(\d{5})$/;
  var str = t.value;
  var result;
  if (!str.match(donepatt)) {
    result = str.match(patt);
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}

function zipcodemask(t) {
  var patt = /(\d{5}).*(\d{5})/;
  var donepatt = /^(\d{5})\-(\d{3})$/;
  var str = t.value;
  var sleng = str.length;
  if (sleng < 5) {
    alert("There are a minimum of 5 digits in a US Zipcode, please enter the entire number");
    return;
  }
  var result;
  if (!str.match(donepatt)) {
    result = str.match(patt);
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
      str = result[1] + '-' + result[2];
      t.value = str;
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}


function phonestrict(t) {
  var patt = /(\d{3}).*(\d{3}).*(\d{4})/;
  var donepatt = /^(\d{3})\-(\d{3})\-(\d{4})$/;
  var str = t.value;
  var result;
  if (!str.match(donepatt)) {
    result = str.match(patt);
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}

function phonemask(t) {
  var patt = /(\d{3}).*(\d{3}).*(\d{4})/;
  var donepatt = /^(\d{3})\-(\d{3})\-(\d{4})$/;
  var str = t.value;
  var sleng = str.length;
  if (sleng < 10) {
    alert("There are 10 digits in a US Phone Number, please enter the entire number");
    return;
  }
  var result;
  if (!str.match(donepatt)) {
    result = str.match(patt);
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
      str = result[1] + '-' + result[2] + '-' + result[3];
      t.value = str;
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}

function ssnstrict(t) {
  var patt = /(\d{3}).*(\d{2}).*(\d{4})/;
  var donepatt = /^(\d{3})-(\d{2})-(\d{4})$/;
  var str = t.value;
  if (!str.match(donepatt)) {
    result = str.match(patt);
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}

function ssnmask(t) {
  var patt = /(\d{3}).*(\d{2}).*(\d{4})/;
  var donepatt = /^(\d{3})-(\d{2})-(\d{4})$/;
  var str = t.value;
  var sleng = str.length;
  if (sleng < 9) {
    alert("There are 9 digits in a SSN, please finish the entry");
    return;
  }
  var result;
  if (!str.match(donepatt)) {
    result = str.match(patt);
    if (result!= null) {
      t.value = t.value.replace(/[^\d]/gi,'');
      str = result[1] + '-' + result[2] + '-' + result[3];
      t.value = str;
    } else {
      if (t.value.match(/[^\d]/gi)) {
        t.value = t.value.replace(/[^\d]/gi,'');
      }
    }
  }
}
