function removeAllChildNodes(node) {

  if (node && node.hasChildNodes && node.removeChild) { 
    while (node.hasChildNodes()) { 
      node.removeChild(node.firstChild); 
    } 
  } 

}


function setNodeText(node, text) {

  // wipe out all child nodes to be safe
  removeAllChildNodes(node);
  
  textNode = document.createTextNode(text)
  node.appendChild(textNode);

}


function copy_node_values(source_node, dest_node) {
  dest_node.value = source_node.value;
}


function getElementsByClassName(oElm, strTagName, strClassName) {

  var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
  var arrReturnElements = new Array();
  strClassName = strClassName.replace(/\-/g, "\\-");
  var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
  var oElement;
  
  for(var i=0; i<arrElements.length; i++) {
    oElement = arrElements[i];      
    if(oRegExp.test(oElement.className)) {
      arrReturnElements.push(oElement);
    }   
  }
  
  return (arrReturnElements);
    
}


/************************/
/* Validation Functions */

function validate_monetary(candidate) {
  var regex = /^-?\d{1,3}(\d{3})*(\.\d{1,2})?$/;
  return regex.test(candidate);
}


/********************/
/* Search Functions */

function submitSearch() {

  var query = document.getElementById('search_query').value;
  
  new LITBox('/search_results.php?query=' + encodeURI(query),
    { type:'window', overlay:true });
    
  return false;

}


function searchOnBlur(searchElem) {
  if (searchElem.value == '')
    searchElem.value = 'Search';
}


function searchOnFocus(searchElem) {
  if (searchElem.value == 'Search')
    searchElem.value = '';
}


/********************/
/* Delete Functions */

function delete_client_confirm(client_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + client_name + '? You will lose all students, billing records, etc. associated with the client.');
}


function delete_pairing_confirm(pairing_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + pairing_name + '? You will lose all tutoring records associated with the assignment.');
}


function delete_pairing_date_confirm(pairing_date_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + pairing_date_name + '?\n\nIt is NOT recommended that you delete the earliest payment scheme for an assignment. Doing so will invalidate any sessions that are not covered by the new earliest payment scheme.');
}


function delete_session_confirm(session_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + session_name + '? You will lose all tutoring records associated with the session.');
}


function delete_student_confirm(student_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + student_name + '? You will lose all tutoring records associated with the student.');
}


function delete_tutor_confirm(tutor_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + tutor_name + '? You will lose all tutoring records associated with the tutor.');
}


function delete_cl_adjust_confirm(cl_adjust_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + cl_adjust_name + '? You will lose all tutoring records associated with the client adjustment.');
}


function delete_client_note_confirm(client_note_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + client_note_name + '?');
}


function delete_tu_adjust_confirm(tu_adjust_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + tu_adjust_name + '? You will lose all tutoring records associated with the tutor adjustment.');
}


function delete_tutor_note_confirm(tutor_note_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + tutor_note_name + '?');
}


function delete_pairing_note_confirm(pairing_note_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + pairing_note_name + '?');
}

function delete_payment_scheme_confirm(payment_scheme_name) {
  return confirm("WARNING:\nTHIS ACTION IS IRREVERSIBLE\n\nAre you sure you wish to delete " + payment_scheme_name + '?');
}


function delete_self_client(form, client_name, name_submit_type,
  value_submit_type) {
  
  if (delete_client_confirm(client_name))
    submit_form(form, name_submit_type, value_submit_type);
  
}


function delete_self_student(form, student_name, name_submit_type,
  value_submit_type) {
  
  if (delete_student_confirm(student_name))
    submit_form(form, name_submit_type, value_submit_type);
  
}


function delete_self_tutor(form, tutor_name, name_submit_type,
  value_submit_type) {
  
  if (delete_tutor_confirm(tutor_name))
    submit_form(form, name_submit_type, value_submit_type);
  
}


function delete_self_pairing(form, pairing_name, name_submit_type,
  value_submit_type) {
  
  if (delete_pairing_confirm(pairing_name))
    submit_form(form, name_submit_type, value_submit_type);
  
}


function delete_record_client(form, client_name, name_submit_type, value_submit_type,
  name_submit_id, value_submit_id) {
  
  if (delete_client_confirm(client_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_pairing(form, pairing_name, name_submit_type, value_submit_type,
  name_submit_id, value_submit_id) {
  
  if (delete_pairing_confirm(pairing_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_pairing_date(form, pairing_date_name, name_submit_type,
  value_submit_type, name_submit_id, value_submit_id) {
  
  if (delete_pairing_date_confirm(pairing_date_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_session(form, session_name, name_submit_type, value_submit_type,
  name_submit_id, value_submit_id) {
  
  if (delete_session_confirm(session_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_student(form, student_name, name_submit_type, value_submit_type,
  name_submit_id, value_submit_id) {
  
  if (delete_student_confirm(student_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_tutor(form, tutor_name, name_submit_type, value_submit_type,
  name_submit_id, value_submit_id) {
  
  if (delete_tutor_confirm(tutor_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_cl_adjust(form, cl_adjust_name, name_submit_type, value_submit_type,
  name_submit_id, value_submit_id) {
  
  if (delete_cl_adjust_confirm(cl_adjust_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_client_note(form, client_note_name, name_submit_type,
  value_submit_type, name_submit_id, value_submit_id) {
  
  if (delete_client_note_confirm(client_note_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_tu_adjust(form, tu_adjust_name, name_submit_type, value_submit_type,
  name_submit_id, value_submit_id) {
  
  if (delete_tu_adjust_confirm(tu_adjust_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_tutor_note(form, tutor_note_name, name_submit_type,
  value_submit_type, name_submit_id, value_submit_id) {
  
  if (delete_tutor_note_confirm(tutor_note_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_pairing_note(form, pairing_note_name, name_submit_type,
  value_submit_type, name_submit_id, value_submit_id) {
  
  if (delete_pairing_note_confirm(pairing_note_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


function delete_record_payment_scheme(form, payment_scheme_name, name_submit_type, value_submit_type,
  name_submit_id, value_submit_id) {
  
  if (delete_payment_scheme_confirm(payment_scheme_name))
    submit_form_with_id(form, name_submit_type, value_submit_type,
      name_submit_id, value_submit_id);
  
}


/************************************/
/* Deactivate/Reactiviate Functions */

function deactivate_tutor(form, tutor_name, name_submit_type, value_submit_type) {

  if (confirm('Are you sure you wish to deactivate ' + tutor_name + '? Receipts for the tutor will no longer be generated automatically. Deactivating a tutor will also deactivate all of that tutor\'s assignments.\n\nYou may reactivate the tutor at any time, although you will need to reactivate the tutor\'s assignments manually.'))
    submit_form(form, name_submit_type, value_submit_type);

}


function reactivate_tutor(form, tutor_name, name_submit_type, value_submit_type) {

  if (confirm('Are you sure you wish to reactivate ' + tutor_name + '? Receipts for the tutor will be generated automatically, although you will need to reactivate the tutor\'s assignments manually.'))
    submit_form(form, name_submit_type, value_submit_type);

}


function deactivate_client(form, client_name, name_submit_type, value_submit_type) {

  if (confirm('Are you sure you wish to deactivate ' + client_name + '? Bills for the client will no longer be generated automatically. Deactivating a client will also deactivate all of that client\'s students (and, in turn, all of those students\' assignments).\n\nYou may reactivate the client at any time, although you will need to reactivate the client\'s students (and those students\' assignments) manually.'))
    submit_form(form, name_submit_type, value_submit_type);

}


function reactivate_client(form, client_name, name_submit_type, value_submit_type) {

  if (confirm('Are you sure you wish to reactivate ' + client_name + '? Bills for the client will be generated automatically, although you will need to reactivate the client\'s students (and those students\' assignments) manually.'))
    submit_form(form, name_submit_type, value_submit_type);

}


function deactivate_student(form, student_name, name_submit_type, value_submit_type) {

  if (confirm('Are you sure you wish to deactivate ' + student_name + '? Deactivating a student will also deactivate all of that student\'s assignments.\n\nYou may reactivate the student at any time, although you will need to reactivate the student\'s assignments manually.'))
    submit_form(form, name_submit_type, value_submit_type);

}


function reactivate_student(form, student_name, name_submit_type, value_submit_type) {

  if (confirm('Are you sure you wish to reactivate ' + student_name + '? You will need to reactivate the student\'s assignments manually.\n\nReactivating a student will also reactivate the student\'s client, if necessary.'))
    submit_form(form, name_submit_type, value_submit_type);

}


function deactivate_pairing(form, pairing_name, name_submit_type, value_submit_type) {

  if (confirm('Are you sure you wish to deactivate ' + pairing_name + '?\n\nYou may reactivate the assignment at any time.'))
    submit_form(form, name_submit_type, value_submit_type);

}


function reactivate_pairing(form, pairing_name, name_submit_type, value_submit_type) {

  if (confirm('Are you sure you wish to reactivate ' + pairing_name + '?\n\nReactivating an assignment will also reactivate the assignment\'s tutor and student (and, in turn, that student\'s client), if necessary.'))
    submit_form(form, name_submit_type, value_submit_type);

}


/***************************/
/* Submit with ID Function */

function submit_form_with_id(form, name_submit_type, value_submit_type,
  name_submit_id, value_submit_id) {
  
  name_submit_id.value = value_submit_id;
  submit_form(form, name_submit_type, value_submit_type);
  
}


/*******************/
/* Submit Function */

function submit_form(form, name_submit_type, value_submit_type) {
  name_submit_type.value = value_submit_type;
  form.submit();
}


/*******************/
/* Cancel Function */

function cancel_process(form, name_submit_type, value_submit_type) {
  if (confirm('Are you sure you wish to cancel? You will lose all changes you have made.'))
    submit_form(form, name_submit_type, value_submit_type);
    
}


/*******************/
/* Popup Functions */

function open_tutor_popup(user_id) {

  window.open('view_tutor_popup.php?user_id=' + user_id, 'tutor_popup',
    'menubar,scrollbars,resizable,status,width=500,height=500');
    
  return false;
  
}


function open_student_popup(student_id) {

  window.open('view_student_popup.php?student_id=' + student_id, 'student_popup',
    'menubar,scrollbars,resizable,status,width=500,height=500');
    
  return false;
  
}


function open_document_popup(url) {
    
    window.open(url, 'document',
      'menubar,scrollbars,resizable,status,width=850,height=750');

    return false;
    
}


/****************************/
/* Suckerfish Dropdown Code */

function sf_hover() {

  // ensure that a Dropdown menu contained in the page
  if (document.getElementById("Dropdown")) {
  
    var sfEls = document.getElementById("Dropdown").getElementsByTagName("LI");
  
    for (var i = 0; i < sfEls.length; i++) {
  
      sfEls[i].onmouseover = function() {
        this.className += " SFhover";
      }
    
      sfEls[i].onmouseout = function() {
        this.className = this.className.replace(new RegExp(" SFhover\\b"), "");
      }
    
    }
  
  }
  
  // process SectionTitleMenus as well
  var sectionMenus = getElementsByClassName(document, 'ul', 'SectionMenu');
  
  for (var i = 0; i < sectionMenus.length; i++) {
  
    menuItems = sectionMenus[i].getElementsByTagName('li');
    for (var j = 0; j < menuItems.length; j++) {
  
      menuItems[j].onmouseover = function() {
        this.className += " SFhover";
      }
    
      menuItems[j].onmouseout = function() {
        this.className = this.className.replace(new RegExp(" SFhover\\b"), "");
      }
  
    }
  
  }
  
}


if (window.attachEvent) window.attachEvent("onload", sf_hover);