// general.js
// Useful JavaScript code used throughout the website...

// A utility to add an event handler to the documents onload handler without clobering an existing handler...
function addLoadEvent(func) {
  var oldonload= window.onload;
  if (typeof(window.onload) != 'function') {
    window.onload= func;
  }else {
    window.onload= function() {
      if (oldonload) { oldonload(); }
      func();
    }
  }
}


// This function requires that the jsWindows JavaScript file be loaded prior to use...
function openModalDialog(html,n,w,h,t,c) {
  if (typeof(html) == 'undefined' || html == null) return false;
  if (typeof(n) == 'undefined' || n == null) n= 'modal_dialog';
  if (typeof(w) == 'undefined' || w == null) w= 350;
  if (typeof(h) == 'undefined' || h == null) h= 300;
  if (typeof(t) == 'undefined' || t == null) t= '';
  if (typeof(c) == 'undefined' || c == null) c= 'default';
  var myWin= new Window(n, {
    className: c, title: t, zIndex:-1, height: h , width: w , opacity: 1, resizable: false
  });
  myWin.setDestroyOnClose();
  myWin.showCenter(true);
  myWin.setHTMLContent(html);

  return myWin;
}


// This function requires that the jsWindows JavaScript file be loaded prior to
// use and that a dialog window was created using the openModalDialog function...
function closeDialog(dlg) {
  if (typeof(dlg) == 'undefined' || dlg == null) dlg= 'modal_dialog';
  Windows.close(dlg);
}


// A utility to reset the form data and reset the focus to the first non-hidden field in the form...
function resetForm(frm) {
  if (typeof(frm) == 'undefined') return false;
  frm.reset();
  for(var j= 0; j < frm.length; j++) {
    if (frm.elements[j].type != "hidden") {
      frm.elements[j].focus();
      break;
    }
  }  // for(j= 0; j < frm.length; j++)
}


// A utility for checking if a string has special characters or whitespace...
function hasSpecialChars(s) {
  var pattern= /[^a-zA-Z0-9_]/;
  return pattern.test(s);
}


// A utility function returns true if a string contains only white space characters...
function isBlank(s) {
  if (typeof(s) == 'undefined') return true;
  for(var i= 0; i < s.length; i++) {
    var c= s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '')) return false;
  }
  return true;
}


// A utility for verifing that an email address is formatted correctly...
function isValidEmail(elementValue) {
//  /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/
  var emailPattern= /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
  return emailPattern.test(elementValue);   
}


// A utility for verifing that a phone number is formatted correctly...
function isValidPhone(elementValue) {
  var result= elementValue.match(/^([1-9]\d{2})[-|\.]([1-9]\d{2})[-|\.](\d{4})$/);
  var areacode= result[1];
  var prefix= result[2];
  var house= result[3];
  if ((result == null) || (result.length < 4) || (result[1] < 100 || result[2] < 100 || result[3] < 1) ||
    result[1].match(/111|222|333|444|555|666|777|888|999|000|123/) ||
    result[2].match(/111|222|333|444|555|666|777|888|999|000|123/) ||
    result[3].match(/1111|2222|3333|4444|5555|6666|7777|8888|9999|0000|1234/))
  {
    return false;
  }
  return true;
}


// A utility function to hide/display the required star on a field
// based on whether or not the checked property is set on the chkFld...
function toggleRequired(chkFld,flg_id) {
  if (typeof(chkFld) == 'undefined' || typeof(flg_id) == 'undefined') return false;
  if (chkFld.checked) {
    document.getElementById(flg_id).style.display= 'inline';
  }else {
    document.getElementById(flg_id).style.display= 'none';
  }
  return true;
}
