//
// Browser Sniffing
//

ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
dom = (document.getElementById)? true:false;

//
// Onload Registry
//

var onloadRegistry = Array();

function registerOnload(onloadHandler)
{
  onloadRegistry.push(onloadHandler);
}

function runOnload()
{
  for( index = 0; index < onloadRegistry.length; index++ ) {
    onloadHandler = onloadRegistry[index];
    onloadHandler();
  }
}

//
// Javascript XML HTTP Request
//

function xmlHttpRequest()
{
  var xmlHttpReq = false;

  // Mozilla/Safari
  if (window.XMLHttpRequest)
    xmlHttpReq = new XMLHttpRequest();

  // IE
  else if (window.ActiveXObject)
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");

  return xmlHttpReq;
}

//
// Javascript Form URL Encoder Function
//

function createStringFromForm(docForm) {

  var strSubmitContent = '';
  var formElem;
  var strLastElemName = '';

  for (i = 0; i < docForm.elements.length; i++) {

    formElem = docForm.elements[i];
    switch (formElem.type) {
      case 'text':
      case 'hidden':
      case 'password':
      case 'textarea':
      case 'select-one':
        strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
        break;
      case 'radio':
        if (formElem.checked) {
          strSubmitContent += formElem.name + '=' + escape(formElem.value) + '&'
        }
        break;
      case 'checkbox':
        if (formElem.checked) {
          if (formElem.name == strLastElemName) {
            if (strSubmitContent.lastIndexOf('&') == strSubmitContent.length-1) {
              strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
            }
            strSubmitContent += ',' + escape(formElem.value);
          }
          else {
            strSubmitContent += formElem.name + '=' + escape(formElem.value);
          }
          strSubmitContent += '&';
        }
        break;

    }
    strLastElemName = formElem.name;
  }
  strSubmitContent = strSubmitContent.substr(0, strSubmitContent.length - 1);
  return strSubmitContent;
}

//
// Javascript XMLHTTPRequest Form Submit
//

function doAjaxFormSubmit(encoded_data, url, ch_id, sav_id, form)
{

      if (dom)
      {
          clearDiv(ch_id);
          savingDiv(sav_id);

          var xmlreq = xmlHttpRequest();

          xmlreq.open("GET", url + encoded_data, true);
          xmlreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
          xmlreq.send(null);
          xmlreq.onreadystatechange = function () {
                if(xmlreq.readyState==4)
                {
                    clearSavingDiv(sav_id);
                    replaceDiv(ch_id);
                }
          }
      }
      else {
         document.form.submit();
      }
}

/*

function ajaxFriendsRequest(encoded_data)
{
      var xmlreq = xmlHttpRequest();


      if (dom)
      {
          xmlreq.open("GET", url + encoded_data, true);
          xmlreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
          xmlreq.send(null);
          xmlreq.onreadystatechange = function () {
                if(xmlreq.readyState==4)
                {

                }
          }
      }
      else
      {
         //document.submit
      }
}
*/
//
// Image Rollovers
//
// Add an hsrc attribute to an img tag to change on hover
// Add a dsrc attribute to change the image on click
//

function rolloverSetup()
{
  var img, sh, sn, sd
  for (var i = 0; (img = document.images[i]); i++) {
    if (img.getAttribute) {

      sn = img.getAttribute("src");
      sh = img.getAttribute("hsrc");
      sd = img.getAttribute("dsrc");

      if (sn != "" && sn != null) {
        img.n = new Image();
        img.n.src = img.src;

        if (sh != "" && sh != null) {
          img.h = new Image();
          img.h.src = sh;
          img.onmouseover = rolloverSwapOn
          img.onmouseout  = rolloverSwapOff
        }

        if (sd != "" && sd != null) {
          img.d = new Image();
          img.d.src = sd;
          img.onmousedown = rolloverSwapDown
        }
      }
    }
  }
}

// Load at Startup
registerOnload(rolloverSetup);

function rolloverSwapOn() {
  this.src = this.h.src;
}

function rolloverSwapOff() {
  this.src  = this.n.src;
}

function rolloverSwapDown() {
  this.src  = this.d.src;
  this.temp = typeof(document.onmouseup) != 'undefined' && typeof(document.onmouseup) != 'unknown' ? document.onmouseup : "";
  rolloverSwapUp.img = this;
  document.onmouseup = rolloverSwapUp;
}

function rolloverSwapUp() {
  var ths = rolloverSwapUp.img;
  ths.src = ths.n.src;
  if (ths.temp) document.onmouseup = ths.temp;
}

//
// Placeholder Text
//
// Add the placeholder attribute to input text fields to give them grey
// placeholder text. Like this <input type="text" placeholder="Search"/>
//

function placeholderSetup() {
  var inputs = document.getElementsByTagName('input');
  var input;

  for( var i = 0; input = inputs[i]; i++ ) {
    // Only Apply to Text Inputs
    if( input.type != "text" )
      continue;

    // Look for placeholder attribute and install focus and blur handlers
    ph = input.getAttribute("placeholder")
    if( ph && ph != "" ) {
      input.value = ph;
      input.style.color = 'gray';
      input.onfocus = placeholderFocus;
      input.onblur = placeholderBlur;
    }
  }
}

// Load at Startup
registerOnload(placeholderSetup);

function placeholderFocus() {
  if( this.style.color == 'gray' ) {
    this.value = '';
    this.style.color = 'black';
  }
}

function placeholderBlur() {
  ph = this.getAttribute("placeholder")
  if( ph && this.value == "" ) {
    this.value = ph;
    this.style.color = 'gray';
  }
}

//
// Element Hide/Show/SetStyle
//

function getElement(id) {
  if (ns4) return document.layers[id];
  if (ie4) return document.all[id];
  if (dom) return document.getElementById(id);
}

function showElement(id) {
  if (ns4) document.layers[id].display = "block";
  else if (ie4) document.all[id].style.display = "block";
  else if (dom) document.getElementById(id).style.display = "block";
}

function hideElement(id) {
  if (ns4) document.layers[id].display = "";
  else if (ie4) document.all[id].style.display = "none";
  else if (dom) document.getElementById(id).style.display = "none";
}

function setElementStyle(id, style) {
  if (ns4) document.layers[id].className = style;
  else if (ie4) document.all[id].className = style;
  else if (dom) document.getElementById(id).className = style;
}

//
// set select index by value
//
// LN 08/25

function setSelectedText(obj, val) {
  if (obj) {
    var len = obj.options.length;
    for(var i=0; i<len; i++){
      if (obj.options[i].text == val){
        obj.selectedIndex = i;
        break;
      }
    }
  }
}

//
// return the next field in a form
//

function getNextFormField(field) {
  var i;
  for (i = 0; i < field.form.elements.length; i++) {
    if (field == field.form.elements[i]) {
      break;
    }
  }
  i = (i + 1) % field.form.elements.length;

  return(field.form.elements[i]);
}

//
// Copyright Notice
//

function checkAgree() {
  if (document.frm.pic.value) {
    if (document.frm.agree.checked) {
      document.frm.submit();
    } else {
      alert ("You must certify that the image is not copyrighted before you upload.");
    }
  }
}

