/**
 * BrowserMetaData provides info about a clients browser
 */
function BrowserMetaData() {
  this.ver      = navigator.appVersion;
  this.agent    = navigator.userAgent;
  this.dom      = document.getElementById?1:0;
  this.opera5   = this.agent.indexOf("Opera 5")>-1;
  this.ie4      = document.all && !this.dom && !this.opera5;
  this.ie5      = this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5;
  this.ie6      = this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5;
  this.ie7      = this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5;
  this.ie       = this.ie4||this.ie5||this.ie6||this.ie7;
  this.mac      = this.agent.indexOf("Mac")>-1;
  this.ns6      = this.dom && parseInt(this.ver) >= 5;
  this.ns4      = document.layers && !this.dom ;
  this.platform = navigator.platform;
}

BrowserMetaData.prototype.isIE = function() {
  return this.ie ;
}

var BrowserMetaData = new BrowserMetaData() ;

var siteRoot = "" ;

/**
 * Locates the parent form for a given element
 *
 * @param element The element to find the parent form for.
 * @return The parent form for element. If no parent form
 * is found an alert will be shown and null returned.
 */
function getElementsForm(element) {
  var parentForm = element ;
  while (parentForm != null && parentForm.tagName.toLowerCase() != "form") {
    parentForm = parentForm.parentNode ;
  }
  if (parentForm == null) {
    alert("Cannot find form for this field: " + element.id) ;
  }

  return parentForm;

}

/**
 * Updates all textarea fields with an FCK editor.
 *
 * @param toolbar The name of the toolbar to use.
 * @param height The height of each editor.
 */
function replaceAllTextareas(toolbar, height, autoFocus, customStyleSheet, width) {

  var undefined ;

  // replace all of the textareas
  var allTextAreas = document.getElementsByTagName("textarea");
  for (var i=0; i < allTextAreas.length; i++) {
    var oFCKeditor = new FCKeditor( allTextAreas[i].name ) ;
    oFCKeditor.Config["CustomConfigurationsPath"] = siteRoot + "/js/fckConfig.js"  ;
    oFCKeditor.BasePath = siteRoot + "/FCKeditor/" ;
    oFCKeditor.ToolbarSet = toolbar ;
    oFCKeditor.Height = height ;
    if (autoFocus && i == 0) {
      oFCKeditor.Config[ "StartupFocus" ] = true ;
    } else {
      oFCKeditor.Config[ "StartupFocus" ] = false ;
    }

    if ( !(undefined === width)) {
      oFCKeditor.Width = width ;
    }

    if ( !(undefined === customStyleSheet)) {
      oFCKeditor.Config[ "EditorAreaCSS" ] = customStyleSheet ;
    }
    oFCKeditor.ReplaceTextarea() ;
  }
}

function updateCalendarButton(formName) {
  var formElement = document.getElementById(formName) ;
  if (formElement != null) {
    var formDivs = formElement.getElementsByTagName("div") ;
    for (var i = 0; i < formDivs.length; i++) {
      if (formDivs[i].className == "inputCalendar") {
        var calendarInputs = formDivs[i].getElementsByTagName("input") ;
        for (var j = 0; j < calendarInputs.length; j++) {
          calendarInputs[j].className += " inputCalendarInput" ;
          if (calendarInputs[j].value == "...") {
            calendarInputs[j].className += " inputCalendarButton" ;
          }
        }
      }
    }
  } else {
    alert("Cannot find form " + formName) ;
  }
}

/**
 * Ensures that the two password fields are the same, and if they are different
 * update the message field with the msg.
 *
 * @param passwordFieldId The id of the password element.
 * @param repeatFieldId   The id of the repeat password element.
 * @param messageFieldId  The id of the element to display the message in.
 * @param msg             The message to display if the passwords do not match.
 */
function checkPasswords(passwordFieldId, repeatFieldId, messageFieldId, msg) {
  var mode = "none" ;
  var msgElement = document.getElementById(messageFieldId) ;

  var p1 = document.getElementById(passwordFieldId) ;
  var p2 = document.getElementById(repeatFieldId) ;

  if (p1 != null && p2 != null) {
    if (p1.value != null && p1.value != "" &&
        p2.value != null && p2.value != "") {

      if (p1.value != p2.value) {
        mode = "block" ;
        p2.value = "" ;
      } else {
        msg = "" ;
      }
      msgElement.innerHTML = msg ;
      msgElement.style.display = mode ;
    }
  }

}

function getURLQueryParams() {
  var url = document.URL ;
  var queryIndex = url.indexOf("?") ;
  var params = new Array() ;
  if (queryIndex > -1) {
    var queryPart = url.substring(queryIndex+1) ;
    var paramPairs = queryPart.split("&") ;
    for (var i = 0; i < paramPairs.length; i++) {
      var paramPair = paramPairs[i].split("=") ;
      if (paramPair.length > 1) {
        params[paramPair[0]] = paramPair[1] ;
      } else {
        params[paramPair[0]] = "" ;
      }
    }
  }
  return params ;
}

function submitParentForm(element) {
  submitForm( getElementsForm(element) ) ;
}

function submitForm(form) {
  if (form.onsubmit) {
    if (form.onsubmit()) {
      form.submit() ;
    }
  } else {
    form.submit() ;
  }
}

function getSiblingElement(currentElement, elementName) {
  var form = currentElement.form ;
  var absElementName = new RegExp(form.id + "[:_]" + elementName + "$")
  for (var i = 0; i < form.elements.length; i++) {
    var child = form.elements[i] ;
    if ( child.id.match(absElementName) ) {
      return child ;
    }
  }
  return null ;
}

function confirmInactive(requiresWarning, jobCount) {
  var msg = "Warning: " + jobCount + " active job" + (jobCount == 1 ? '' : 's') + " will be set to inactive. Continue anyway?" ;
  return requiresWarning ? confirm(msg):true ;
}

function initGoogle() {
  var q = document.getElementById('googleSearchText');
  if (q) {
    var n = navigator;
    var l = location;
    if (n.platform == 'Win32') {
      q.style.cssText = 'border: 1px solid #7e9db9; padding: 2px;';
    }
    var blurBackground = function() {
      if (q.value == '') {
        q.style.background = '#FFFFFF url(http://www.google.com/coop/images/google_custom_search_watermark.gif) left no-repeat';
      }
    };
    var focusBackground = function() {
      q.style.background = '#ffffff';
    };
    q.onfocus = focusBackground;
    q.onblur = blurBackground;
/*
    if (!/[&?]q=[^&]/.test(l.search)) {
      blurBackground();
    }
*/    
    blurBackground();
  
  }
}