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

function toggleCategory(item) {
  var status = document.getElementById(item + '_sub').style.display;
  if (status == 'block') {
    document.getElementById(item + '_sub').style.display = 'none';
    document.getElementById(item + '_icon').src = 'images/arrowRed.gif';
  } else {
    document.getElementById(item + '_sub').style.display = 'block';
    document.getElementById(item + '_icon').src = 'images/arrowRed2.gif';
  }
}

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 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 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 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 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");
    }
  }
  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_users')                { serverScript = 'users_update.php'; }
  else if (mySection == 'admin_news')                 { serverScript = 'news_update.php'; }
  else if (mySection == 'admin_newsletters')          { serverScript = 'newsletters_delete.php'; }
  else if (mySection == 'admin_events')               { serverScript = 'events_update.php'; }
  else if (mySection == 'admin_publications')         { serverScript = 'publications_delete.php'; }
  else if (mySection == 'admin_jobs')                 { serverScript = 'jobs_delete.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";
}
