/* CUSTOM.JS - Common Javascript functions that are specific to the tracc project and contain references specific to it's architecture */


// serializeContainer: given a container object (such as a div) extracts all form elements within that container and serializes them in an associative array
function serializeContainer(container) {
    var results = new Array();
        //return an array of form elements
    var obj = getElementsByTagNames('input,select,textarea',document.getElementById(container));
    
    if (obj) {
        for (var i=0;i<obj.length;i++) { results[obj[i].name] = obj[i].value; }
    }
      return results;
}


// enterKeyPostback : passed an event object, it checks to see which character code was pressed onKeyPress
//and if that character was the 'Enter' key - triggers a postback.

function enterKeyPostback(e) { 
    var characterCode // literal character code will be stored in this variable

    characterCode = (typeof(e.which) != "undefined") ? e.which : window.event.keyCode;
    /*if(e && e.which){ // NN4
        e = e;
        characterCode = e.which; //character code
    }   
    else { // IE
        e = event;
        characterCode = e.keyCode; //character code
    }*/

    if(characterCode == 13){ // if enter was pressed
          var ajax = new Ajax.Updater('content', document.location.search , {onComplete:function(){ new Effect.Highlight('content');},asynchronous:true,evalScripts:true,method:'post',parameters:Form.serialize(document.postback)});
          return false;
    }
    else {
        return true;  //needed otherwise keypresses arent registered!
    }
}

function ajaxPostback() {
      var ajax = new Ajax.Updater('content', document.location.search , {onComplete:function(){ new Effect.Highlight('content');},asynchronous:true,evalScripts:true,parameters:Form.serialize(document.postback)});
      return false;
}



// ajaxCall : a more generic function that can be hooked into DOM events to trigger ajax calls
// params   is a list of key-value pairs 'var=value&var=value'
// url            is the url to request
// div           is the div to be updated with content from the call

function ajaxCall(div,url,params) {
      new Ajax.Updater(div, url , {
        onComplete:function(){ new Effect.Highlight();
},
        onSuccess:function(){ alert(request.responseText); },
        asynchronous:true,
        evalScripts:true,
        method: 'post',
        parameters:params
        }
        );
      return false;
}

// ajaxContainer : a more specialised version of ajaxCall - given a div, submits all field elements within that div and then updates it
// url            is the url to request
// div           is the div to be updated with content from the call

function ajaxContainer(div,url) {
      new Ajax.Updater(div, url , {
        onComplete:function(){ new Effect.Highlight(div); },
        asynchronous:true,
        evalScripts:true,
        method: 'post',
        parameters:serializeContainer(div)
        }
        );
      return false;
}

function ajaxModalContainer(div,url) {
Modalbox.show(url, {
    width:700,
    evalScripts:true,
    method: 'post',
    params: serializeContainer(div)
});
}

function modalLink(obj,url) {
    Modalbox.show(url, {title: obj.title, width: 700});
    return false;
}


function addElement(container, counter) {
  var containerElement = document.getElementById(container);
  var counterElement = document.getElementById(counter);
  var count = parseFloat(counterElement.value) + 1;


  var element = document.createElement('li');
  element.setAttribute('id','li'+ count);

  element.innerHTML = '<label for="item_' + count +'">Full Name</label><input type="text" id="item_' + count +'" name="item_' + count +'" value="" />&nbsp;<a href="#" onclick="removeElement(\'' + container + '\', \'li'+count+'\')"><img src="assets/images/icons/delete.png" alt="delete" /></a>';

  containerElement.appendChild(element);
  counterElement.value = count;
}




function removeElement(container,element) {
  var parentNode = document.getElementById(container);
  var childNode = document.getElementById(element);
  parentNode.removeChild(childNode);
}

