function imgswap(img,ref){
  if (document.images) {
        document.images[img].src = ref;
  }
}

function _CloseOnEsc() {
  if (event.keyCode == 27) { window.returnValue=false; window.close(); }
}

function changeStyle(nodeObj, newStyle) {
  var attrMax = nodeObj.attributes.length
  for (var j=0; j<attrMax; j++) {
    if (nodeObj.attributes.item(j).nodeName == 'class') {
      nodeObj.attributes.item(j).nodeValue = newStyle;
    }
  }
}

function popUp() {
  var popLeft = (screen.availWidth - popUp.arguments[1]) / 2;
  var popTop = (screen.availHeight - popUp.arguments[2]) / 2;
  if (this.popUpWindow != null) {
    this.popUpWindow.close();
  }
  this.popUpWindow = window.open(
    popUp.arguments[0],
    "",
    "toolbar=no," +
    "location=no," +
    "directories=no," +
    "status=no," +
    "menubar=no," +
    "personalbar=no," +
    "scrollbars=" + popUp.arguments[3] + "," +
    "titlebar=yes," +
    "resizable=no," +
    "left=" + popLeft + "," +
    "top=" + popTop + "," +
    "width=" + popUp.arguments[1] + "," +
    "height=" + popUp.arguments[2]
  );
  return false;
}

function YCDT_preloadImages() {
  if(!document.YCDT_img)
    document.YCDT_img = new Array();
  var argNum, numImg=document.YCDT_img.length;
  for(argNum=0; argNum<YCDT_preloadImages.arguments.length; argNum++) {
    document.YCDT_img[numImg] = new Image();
    document.YCDT_img[numImg++].src = YCDT_preloadImages.arguments[argNum];
  }
}

function YCDT_swapImages() {
  document.YCDT_swap = new Array();
  var i , numImg=0;
  for(i=0; i<(YCDT_swapImages.arguments.length-1); i+=2, numImg++) {
    document.YCDT_swap[numImg] = YCDT_swapImages.arguments[i];
    document.YCDT_swap[numImg].oSrc = document.YCDT_swap[numImg].src;
    document.YCDT_swap[numImg].src = YCDT_swapImages.arguments[i+1];
  }
}

function YCDT_restoreImages() {
  var i;
  for(i=0; i<document.YCDT_swap.length; i++)
    document.YCDT_swap[i].src = document.YCDT_swap[i].oSrc;
}

function YCDT_swapImages2() {
  document.YCDT_swap2 = new Array();
  document.YCDT_swap2[0] = document.YCDT_swap[0];
  document.YCDT_swap3 = new Image();
  document.YCDT_swap3.src = document.YCDT_swap[0].oSrc;
  document.YCDT_swap[0].oSrc = YCDT_swapImages2.arguments[1];
}

function YCDT_restoreImages2() {
  if(!document.YCDT_swap2)
    return;
  document.YCDT_swap2[0].src = document.YCDT_swap3.src;
  document.YCDT_swap2[0].oSrc = document.YCDT_swap3.src;
}


function trim(myString) {
// myString     : any string
// RETURN VALUE : myString with all beginning and ending whitespace removed

   myString = myString.replace(/^[\s]+/g,"");
   myString = myString.replace(/[\s]+$/g,"");

  return myString;
}


function checkPostalCode(myPC, isRequired) {
// myPC         : Canadian Postal Code
// isRequired   : 1=yes, 0=no
// RETURN VALUE : empty string on valid postal code, text message on invalid postal code

  if (myPC == "") {
    if (isRequired == 1) {
      return "Postal Code is required.";
    } else {
      return "";
    }
  }

  if ((myPC.length < 6) || (myPC.length > 7)) {
    return "Postal Code must contain 6 characters.";
  }

  if (/([DFIOQU])/.test(myPC)) {
    return "Postal Code contains an invalid character.";
  }

  if (/^([WZ])/.test(myPC)) {
    return "Postal Code syntax is incorrect.";
  }

  if (/^([A-Z]{1}\d[A-Z]{1})( |-)?(\d[A-Z]{1}\d)$/.test(myPC)) {
    return "";
  }

  return "Postal Code syntax is incorrect.";
}


function checkPhone(myPhone, myExtension, myName, isRequired) {
// myPhone      : phone number
// myExtension  : optional extension number, up to 4 digits
// myName       : optional name for the myPhone field, used for returning messages
// isRequired   : 1=yes, 0=no (this only checks if myPhone is required; myExtension is always optional)
// RETURN VALUE : empty string on valid phone number, text message on invalid phone number

  if (myName == "") { myName = "The phone number"; }

  myPhone = trim(myPhone);
  myExtension = trim(myExtension);

  if (myPhone == "") {
    if (isRequired == 1)        { return myName + " is required."; }
    else if (myExtension != "") { return myName + " Extension can't be specified if " + myName + " is not specified."; }
    else                        { return ""; }
  }

  myPhone = myPhone.replace(/^(\d{3})[ \-=\.]*(\d{4})$/, "$1$2");
  myPhone = myPhone.replace(/^[ (]*(\d{3})[ \-=\.)]*(\d{3})[ \-=\.]*(\d{4})$/, "$1$2$3");
  myPhone = myPhone.replace(/^[ (]*1[ \-=\.(]*(\d{3})[ \-=\.)]*(\d{3})[ \-=\.]*(\d{4})$/, "$1$2$3");

  if ((myPhone.length != 10) && (myPhone.length != 7)) { return myName + " syntax is incorrect."; }
  if (!myPhone.match(/^(\d)+$/)) { return myName + " syntax is incorrect."; }

  if (myExtension != "") {
    if (myExtension.length > 4) { return myName + " Extension can't contain more than 4 digits."; }
    if (!myExtension.match(/^(\d)+$/)) { return myName + " Extension syntax is incorrect.  It can only contain numerical digits."; }
  }

  return "";
}


function checkEmail(myEmail, myName, isRequired) {
// myEmail      : e-mail address
// myName       : optional name for the myEmail field, used for returning messages
// isRequired   : 1=yes, 0=no
// RETURN VALUE : empty string on valid e-mail, text message on invalid e-mail syntax

  if (myName.length < 1) {
    myName = "The e-mail address";
  }

  if (myEmail == "") {
    if (isRequired == 1) {
      return myName + " is required.";
    } else {
      return "";
    }
  }

/*
  // regular expression source: http://www.breakingpar.com/bkp/home.nsf/Doc!OpenNavigator&87256B280015193F87256C40004CC8C6
  if (!(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(myEmail))) {
    return myName + " syntax is incorrect.";
  }
*/

  // regular expression source: http://www.hexillion.com/samples/
  // Matches a limited version of the RFC 2822 addr-spec form.
  if (!(/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/.test(myEmail))) {
    return myName + " syntax is incorrect.";
  }

  return "";
}


function checkText(myText, myName, minLength, maxLength) {
// myText       : some text string to perform a check
// myName       : optional name for the myText field, used for returning messages
// minLength    : non-negative integer (0 means myText can be empty string)
// maxLength    : integer (0 means myText must be empty string, -1 means myText has no max length)
// RETURN VALUE : empty string on valid text, text message on invalid text

  if (myName.length < 1) {
    myName = "The text";
  }

  if (myText == "") {
    if (minLength > 0) {
      return myName + " is required.";
    } else {
      return "";
    }
  }

  if (myText.length < minLength) {
    return myName + " must contain at least " + minLength + " characters.";
  }

  if ((maxLength >= 0) && (myText.length > maxLength)) {
    return myName + " cannot contain more than " + maxLength + " characters.";
  }

  return "";
}


function checkUserID(myUserID, myName, minLength, maxLength) {
// myUserID     : user ID to perform a check
// myName       : optional name for the myUserID field, used for returning messages
// minLength    : non-negative integer (0 means myUserID can be empty string)
// maxLength    : integer (0 means myUserID must be empty string, -1 means myUserID has no max length)
// RETURN VALUE : empty string on valid user ID, text message on invalid user ID

  if (myName.length < 1) {
    myName = "The User ID";
  }

  if (myUserID == "") {
    if (minLength > 0) {
      return myName + " is required.";
    } else {
      return "";
    }
  }

  if (myUserID.length < minLength) {
    return myName + " must contain at least " + minLength + " characters.";
  }

  if ((maxLength >= 0) && (myUserID.length > maxLength)) {
    return myName + " cannot contain more than " + maxLength + " characters.";
  }

  if (/^(([a-z]|\d)*)$/.test(myUserID)) {
    return "";
  }

  return myName + " syntax is incorrect.  It can only contain alphanumeric characters.";
}


function checkPassword(myPassword, myName, minLength, maxLength) {
// myPassword   : password to perform a check
// myName       : optional name for the myPassword field, used for returning messages
// minLength    : non-negative integer (0 means myPassword can be empty string)
// maxLength    : integer (0 means myPassword must be empty string, -1 means myPassword has no max length)
// RETURN VALUE : empty string on valid password, text message on invalid password

  if (myName.length < 1) {
    myName = "The Password";
  }

  if (myPassword == "") {
    if (minLength > 0) {
      return myName + " is required.";
    } else {
      return "";
    }
  }

  if (myPassword.length < minLength) {
    return myName + " must contain at least " + minLength + " characters.";
  }

  if ((maxLength >= 0) && (myPassword.length > maxLength)) {
    return myName + " cannot contain more than " + maxLength + " characters.";
  }

  if (/^(([a-z]|\d)*)$/.test(myPassword)) {
    return "";
  }

  return myName + " syntax is incorrect.  It can only contain alphanumeric characters.";
}


function isUnsignedInteger(myInteger) {
// myInteger    : string representation of an integer
// RETURN VALUE : true on valid integer, false on invalid integer

  if (/^(\d)+$/.test(myInteger)) {
    return true;
  }
  return false;
}


function isDate(myMonth, myDay, myYear) {
// myMonth      : month (1-12)
// myDay        : date of the month (1-31)
// myYear       : 4-digit year
// RETURN VALUE : true on valid date, false on invalid date


  if (!isUnsignedInteger(myMonth)) return false;
  if ((myMonth < 1) || (myMonth > 12)) return false;

  if (!isUnsignedInteger(myDay)) return false;
  if ((myDay < 1) || (myDay > 31)) return false;

  if (!isUnsignedInteger(myYear)) return false;
  if ((myYear < 1000) || (myYear > 9999)) return false;

  if (myMonth == 2) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    if ((myYear % 4 == 0) && ((!(myYear % 100 == 0)) || (myYear % 400 == 0))) {
      // leap year
      if (myDay > 29) return false;
    } else {
      // not a leap year
      if (myDay > 28) return false;
    }
  }

  if ((myMonth == 4) || (myMonth == 6) || (myMonth == 9) || (myMonth == 11)) {
    if (myDay > 30) return false;
  }

  return true;
}

function translateBadChars(myString) {
// myString     : string to check for bad chars (i.e. MS-Word special characters)
// RETURN VALUE : updated string

  myString = myString.replace(/’/g,"'");
  myString = myString.replace(/‘/g,"'");
  myString = myString.replace(/“/g,'"');
  myString = myString.replace(/”/g,'"');
  myString = myString.replace(/…/g,"...");
  myString = myString.replace(/–/g,"-");
  myString = myString.replace(/—/g,"-");
  myString = myString.replace(/Œ/g,"Oe");
  myString = myString.replace(/œ/g,"oe");

  return myString;
}

function getPostData(myForm) {
// myForm       : form from which POST data will be extracted
// RETURN VALUE : URL encoded string of POST data

  var postData = '';
  for (e=0; e<myForm.elements.length; e++) {
    if (myForm.elements[e].name != '') {
      if (((myForm.elements[e].type.toLowerCase() == "radio") || (myForm.elements[e].type.toLowerCase() == "checkbox")) && (!myForm.elements[e].checked)) {
        continue;
      }
      postData += (postData == '') ? '' : '&';
      // plus signs would have been interpreted as space characters, as JavaScript's escape() function doesn't encode it, so we encode it ourselves
      postData += escape(myForm.elements[e].name).replace(/\+/g,"%2B") + '=' + escape(translateBadChars(myForm.elements[e].value)).replace(/\+/g,"%2B");
    }
  }
//alert(postData);
  return postData;
}

function createXMLHttpRequest() {
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("MSXML2.XMLHTTP.5.0"); } catch (e) {}
   try { return new ActiveXObject("MSXML2.XMLHTTP.4.0"); } catch (e) {}
   try { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch (e) {}
   try { return new ActiveXObject("MSXML2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("MICROSOFT.XMLHTTP.1.0"); } catch (e) {}
   try { return new ActiveXObject("MICROSOFT.XMLHTTP.1"); } catch (e) {}
   try { return new ActiveXObject("MICROSOFT.XMLHTTP"); } catch (e) {}
   return null;
}

function sendRequestAsynch(myPostData, mySection) {
// myPostData   : URL encoded string of POST data to send to server
// mySection    : keyword determining which part of the database we are updating
// RETURN VALUE : response from server, or error message

  var serverScript = "";
  if      (mySection == 'admin_weekly_bulletins')           { serverScript = 'weekly_bulletins_update.php'; }
  else if (mySection == 'admin_events_calendar')            { serverScript = 'events_calendar_update.php'; }
  else if (mySection == 'admin_users')                      { serverScript = 'users_update.php'; }
  else if (mySection == 'admin_parishes')                   { serverScript = 'parishes_update.php'; }
  else if (mySection == 'admin_retreat_centres')            { serverScript = 'retreat_centres_update.php'; }
  else if (mySection == 'admin_archdiocesan_organizations') { serverScript = 'archdiocesan_organizations_update.php'; }
  else if (mySection == 'admin_provincial_organizations')   { serverScript = 'provincial_organizations_update.php'; }
  else if (mySection == 'admin_national_organizations')     { serverScript = 'national_organizations_update.php'; }
  else if (mySection == 'admin_catholic_schools')           { serverScript = 'catholic_schools_update.php'; }
  else if (mySection == 'admin_apc_minutes')                { serverScript = 'apc_minutes_delete.php'; }
  else if (mySection == 'admin_apc_reports')                { serverScript = 'apc_reports_delete.php'; }
  else if (mySection == 'admin_dpc_minutes')                { serverScript = 'dpc_minutes_delete.php'; }
  else if (mySection == 'admin_members')                    { serverScript = 'members_update.php'; }
  else if (mySection == 'admin_news')                       { serverScript = 'news_delete.php'; }
  else if (mySection == 'admin_stewardship_reflections')    { serverScript = 'stewardship_reflections_delete.php'; }
  else if (mySection == 'admin_stewardship_conferences')    { serverScript = 'stewardship_conferences_update.php'; }
  else if (mySection == 'admin_archbishop_letters')         { serverScript = 'archbishop_letters_delete.php'; }
  else if (mySection == 'admin_awcs_news')                  { serverScript = 'awcs_news_delete.php'; }
  else if (mySection == 'admin_catechetics_workshops')      { serverScript = 'catechetics_workshops_update.php'; }
  else if (mySection == 'admin_catechetics_newsletters')    { serverScript = 'catechetics_newsletters_delete.php'; }
  else if (mySection == 'admin_hr_jobs')                    { serverScript = 'hr_jobs_delete.php'; }
  else if (mySection == 'admin_youth_reflections')          { serverScript = 'youth_reflections_delete.php'; }
  else if (mySection == 'admin_youth_events')               { serverScript = 'youth_events_update.php'; }
  else if (mySection == 'admin_youth_resources')            { serverScript = 'youth_resources_delete.php'; }
  else if (mySection == 'admin_directory')                  { serverScript = 'directory_delete.php'; }
  else if (mySection == 'members')                          { serverScript = 'members_update.php'; }
  else    { return "Error: Section not specified."; }

  var xmlhttp = createXMLHttpRequest();
  if (xmlhttp == null) {
    return "Error: XMLHttpRequest is not supported by your browser.";
  }

  if (window.ActiveXObject) { // IE
    xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200) {
          validateFormResponse(xmlhttp.responseText);
        } else {
          validateFormResponse("Error: Incorrect server response (" + xmlhttp.status + " - " + xmlhttp.statusText + ").");
        }
      }
    }
  } else { // Mozilla
    xmlhttp.addEventListener("load", function() {
      if (xmlhttp.status == 200) {
        validateFormResponse(xmlhttp.responseText);
      } else {
        validateFormResponse("Error: Incorrect server response (" + xmlhttp.status + " - " + xmlhttp.statusText + ").");
      }
    }, false);
  }

  xmlhttp.open('POST', serverScript, true);

  xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlhttp.setRequestHeader('Connection', 'close'); // without this header, the connection hangs every now and then, at least in IE 6
  try {
    xmlhttp.send(myPostData);
  } catch(e) {
    return "Error: Data not sent to server due to a connection problem.";
  }

  return "OK";
}
