/**
* Function to create a new element with all required attributes
*
* @author rick <rick@internetschoon.nl>
* @copyright Copyright 2006, rick
* @package basic bC javascript
* @version 1.0 2006-09-01
* @access public
* @param string parent  (what will be the parent element in which the tag will be inserted)
* @param string beforeWhat (elements are inserted by default at the end of the parent element, if you would like to insert
* the element before another element please specify its id
* @param string name (which element would you like to be created)
* @param array attrs
* @param string text
*/

/**
* @version 1.1 2006-12-31
* @author rick <rick@internetschoon.nl
* created try catch block so IE can handle the creation of input and select elements (since all attributes are read-only
* for an input and select element in IE as soon as the element is created)
*/
function createNewElement(parent, beforeWhat, name, attrs, text) {
  var parent                                    =   document.getElementById(parent);
  var buggyIE                                   =   "No";
  if(name.toUpperCase() == "INPUT" || name.toUpperCase() == "SELECT"){
    try {
      /*
       IE fix for input elements
      */
      if (attrs){
        var attributes                            =   "<" + name +" ";
        for (key in attrs) {
          attributes                              +=  key +"=\""+ attrs[key] +"\" ";
        }
        attributes                                +=  ">";
      }

      var e                                       =   document.createElement(attributes);
      var buggyIE                                 =   "Yep";
    }
    catch (element) {

    }
  }

  if (buggyIE == "No") {
    var e                                       =   document.createElement(name);
    if (attrs) {
      for (key in attrs) {
        switch(key){
          case "class":
            e.className                             =   attrs[key];
          break;

          case "id":
            e.id                                    =   attrs[key];
          break;

          default:
            e.setAttribute(key, attrs[key]);
        }
      }
    }
  }

  if (text) {
    e.appendChild(document.createTextNode(text));
  }

  if(beforeWhat != ""){
    var beforeWhat                                =   document.getElementById(beforeWhat);
    parent.insertBefore(e, beforeWhat);
  }
  else{
    parent.appendChild(e);
  }
}

function removeElement(parent, elementId) {
  var parentContainer = document.getElementById(parent);
  var childtoRemove = document.getElementById(elementId);
  parentContainer.removeChild(childtoRemove);
}


function ahahCall(file, objectId, postVar, loadingImage, loadingText) {
  if (loadingImage === undefined) {
    var loadingImage                              =   mainUrl + 'libs/js/i/loader.gif';
  }

  if (loadingText === undefined) {
    var loadingText                               =   null;
  }

  var defaultAlign                                =   document.getElementById(objectId).style.textAlign;

  document.getElementById(objectId).style.textAlign = "center";
  document.getElementById(objectId).innerHTML     =   "";

  createNewElement(objectId, '', 'img', {src: loadingImage}, '');
  createNewElement(objectId, '', 'p', '', loadingText);

  var myAjax = new Ajax.Updater(objectId, file, {method: 'post', parameters: postVar, evalScripts: true});
  document.getElementById(objectId).style.textAlign = defaultAlign;
  return 1;
}

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
function handleEnter (field, event) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13) {
    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;
    field.form.elements[i].focus();
    return false;
  }
  else{
    return true;
  }
}

