//menuTools

var menuTools = {
  
  start: function() {
    
    $S('.Hidden').each(
      function(element) {
        new fx.Height(element).hide();
      }
    );
    
    $S('dl.Menu dt').action({
      
      onclick: function() {
        menuTools.expand(this);  
      },
      
      onmouseover: function(){
        if (this.id == null || this.id == '') { return; }
        if (this.className.indexOf('Hover') == -1) {
          this.className = this.className + ' Hover';
        }
      },
      
      onmouseout: function(){
        if (this.id == null || this.id == '') { return; }
        this.className = this.className.replace("Hover","");
      }
    });
    
    //new Tips($S('.Menu #DirectLinksMenu'), { maxOpacity: 0.95 });
    
  },
  
  expand: function(MenuTitle) {
    if (MenuTitle == null || MenuTitle == '') { return; }
    if ( MenuTitle ) {
      var MenuBody = $(MenuTitle.id + 'Content');
      if ( MenuBody ) {
        if (MenuBody.className.indexOf('Hidden') != -1) {
          MenuBody.className = MenuBody.className.replace("Hidden", "");
        }
        if (MenuTitle.className.indexOf('Closed') != -1) {
          MenuTitle.className = MenuTitle.className.replace('Closed','Open');
        } else {
          MenuTitle.className = MenuTitle.className.replace('Open','Closed');
        }
        if (MenuBody.offsetHeight > 0) {
          new fx.Height(MenuBody, {duration: 150}).toggle(); // Hide Menu
        } else {
          new fx.Height(MenuBody, {duration: 150, onComplete: function(){ MenuBody.style.height = "auto"; }}).toggle(); // Show Menu
        }
      }
    }
  }

};

//searchTools

var searchTools = {
  
  url:'http://search.newcastle.edu.au/search/index.jsp?QueryText=',
  
  start: function() {
    
    var qt = $('QueryText');
    if (qt.value == '' || qt.value == 'Search') {
      qt.value = 'Search';
      qt.style.color = '#999999';
    }
    
    $('SearchEngine').onclick = function() {
      searchTools.expand();
    };
    
    $E('body').onclick = function() {
      if ($('DirectLinksMenuContent').offsetHeight > 0) {
        if ($('DirectLinksMenu').className.indexOf('Open') != -1) {
          menuTools.expand($('DirectLinksMenu'));
          //HandleMenuExpansion('DirectLinksMenu');
        }
      }
      if ($('SearchOptions').offsetHeight > 0) {
        searchTools.expand();
      }
    };
    
    $S('#SearchOptions ul li a').action({
      
      onclick: function() {
        searchTools.change(this);
        searchTools.submit(1);
        return false;
      }
    });
    
    $('SearchForm').onsubmit = function() {
      searchTools.submit();
      return false;
    };
    
    $('QueryText').onfocus = function() {
      if (this.value == '' || this.value == 'Search') {
        this.value = '';
        this.style.color = '#4E5150';
      }
    };
      
    $('QueryText').onblur = function() {
      if (this.value == '' || this.value == 'Search') {
        this.value = 'Search';
        this.style.color = '#999999';
      }
    };
    
  },
  
  expand: function() {
    var SearchOptions = $('SearchOptions');
    if (SearchOptions) {
      if (SearchOptions.className.indexOf('Hidden') != -1) {
        SearchOptions.className = SearchOptions.className.replace("Hidden","");
      }
      if (SearchOptions.offsetHeight > 0) {
        $('SearchEngine').src = 'http://www.newcastle.edu.au/rsrc/images/menus/search_arrow_right.gif';
      } else {
        $('SearchEngine').src = 'http://www.newcastle.edu.au/rsrc/images/menus/search_arrow_down.gif';
      }
      new fx.Height(SearchOptions, {duration: 150}).toggle();
    }
  },
  
  change: function(element) {
    var linkUrl = element.getAttribute('href');
    if (linkUrl == 'http://search.newcastle.edu.au/staffdirectory/index.jsp') {
      url = "http://search.newcastle.edu.au/staffdirectory/index.jsp?QueryText=";
    } else if (linkUrl == 'http://search.newcastle.edu.au/policy/index.jsp') {
      url = "http://search.newcastle.edu.au/policy/?query=";
    } else if (linkUrl == 'http://www.google.com.au/') {
      url = "http://www.google.com.au/search?q=";
    } else if (linkUrl == 'http://search.newcastle.edu.au/program/index.jsp') {
      url = "http://search.newcastle.edu.au/program/index.jsp?QueryText=";
    } else if (linkUrl == 'http://search.newcastle.edu.au/faq/index.jsp') {
      url = "http://search.newcastle.edu.au/faq/index.jsp?QueryText=";
    } else if (linkUrl == 'http://search.newcastle.edu.au/glossary/index.jsp') {
      url = "http://search.newcastle.edu.au/glossary/index.jsp?QueryText=";
    } else {
      url = "http://search.newcastle.edu.au/search/index.jsp?QueryText=";
    }
  },
  
  submit: function(check) {
    var query = $('QueryText').value;
    if (check == 1) {
      if (query != 'Search' && query != '') {
        window.location = url+query;
      } else {
        $('QueryText').focus();
        if ($('SearchOptions').className.indexOf('Hidden') == 1 ) {
          searchTools.expand();
        }
      }
    }
    else {
      window.location = url+query;
    }
    
  }
  
};

//onload

window.onload = function() {
  menuTools.start();
  searchTools.start();
};

/* New Form Handler Functions
 *   Requires behaviour.js->document.getElementsBySelector()
 * 
 *   Functions which will allow a form to be validated.
 *   First prototypes for future development of Form Builder Tool.
 */

/* validateForm(form)
 *   Will validate a form's fields based on their css class.
 *     Return: true  - If the form is valid.
 *             false - If otherwise.
 *     Usage:  <form onsubmit="return validateForm(this);">
 */
function validateForm(form) {
  writeFormError(null, form);
  fixTextAreaLines(form);
  return checkRequiredFields(form) && checkEmailFields(form);
}

/* checkRequiredFields(form)
 *   Will verify that all required fields of a given form have values.
 *   Required fields have a "required" class attached to them. Invalid
 *   fields will have a red border outlined around them.
 *     Return: true  - If all required fields are valid.
 *             false - If otherwise.
 */
function checkRequiredFields(form) {
  var fields = document.getElementsBySelector("#" + form.id + " .required");
  var result = true;
  for (var i = 0; i < fields.length; i++) {
    if (fields[i].value == "") {
      result = false;
      fields[i].parentNode.style.border = "1px solid red";
      writeFormError("The " + fields[i].name + " required field must have a value.", form);
    } else {
      fields[i].parentNode.style.border = "none";
    }
  }
  return result;
}

/* checkEmailFields(form)
 *   Will verify that all email fields of a given form are in the correct
 *   syntax of "aaa@aaa.com". Invalid fields will have a red border attached.
 *     Return: true  - If all email fields are valid.
 *             false - If otherwise.
 */
function checkEmailFields(form) {
  var fields = document.getElementsBySelector("#" + form.id + " .email_field");
  var result = true;
  for (var i = 0; i < fields.length; i++) {
    if (fields[i].value.match(".+@.+[\.].+") == null) {
      result = false;
      fields[i].parentNode.style.border = "1px solid red";
      writeFormError("The " + fields[i].name + " field must be in the correct email format (aaa@aaa.com).", form);
    } else {
      fields[i].parentNode.style.border = "none";
    }
  }
  return result;
}

function writeFormError(error, form) {
  var errorTag = document.getElementsBySelector("#" + form.id + " .errorInfo")[0];
  if (error == null) {
    errorTag.innerHTML = "";
    errorTag.style.border = "none";
  } else {
    errorTag.style.border = "1px solid red";
    errorTag.innerHTML += "<p><strong>Error:</strong>&nbsp;" + error + "</p>";
  }
}

function fixTextAreaLines(form) {
  var fields = document.getElementsBySelector("#" + form.id + " textarea");
  for (var i = 0; i < fields.length; i++) {
    fields[i].value = fields[i].value.replace(/\n/g," ");
  }
}

