// Function To Add Order By Icons to Table Thead Colums
function addOrderbyLink(tableID,headingText,orderby,searchFunction) {
   var orderbyLink = ' <span onclick=\"orderBy(\'' + orderby + '\');\" id=\"order-by-span-' + orderby + '\" >' + headingText + ' <img src=\"icons/arrowup.gif\" id=\"order-by-' + orderby + '\" /><\span>'; 
   $$('table#' + tableID + ' thead > tr:first').each(function(s) {
      s.childElements().each(function(d) {
         if(d.innerHTML == headingText) {
            d.innerHTML = '';
            Element.insert(d, {bottom: orderbyLink});
            Event.stopObserving($('order-by-span-' + orderby), 'click', searchFunction);
            Event.observe($('order-by-span-' + orderby), 'click', searchFunction);
            d.style.cursor="pointer";
         }
      });         
   });
   if($('nav_order_by') != undefined) {
      var orderByID = 'nav_order_by';
   }
   else {
      var orderByID = 'order_by';
   } 
   if($(orderByID) == undefined) {
      alert('error: expecting a form field id ' + orderByID);
   }
   if($(orderByID + '_asc') == undefined) {
      alert('error: expecting a form field id ' + orderByID + '_asc');
   }

   if($(orderByID).value == orderby) {
       if($(orderByID + '_asc').value == 'true') {
         $('order-by-' + orderby).src = 'icons/arrowasc.gif';
      }
      else {
         $('order-by-' + orderby).src = 'icons/arrowdesc.gif';
      }
   }
}

// Function to Set orderby fields and Update Icons
function orderBy(orderby,searchFunction) {
   if($('nav_order_by') != undefined) {
      var orderByID = 'nav_order_by';
   }
   else {
      var orderByID = 'order_by';
   } 
   if($(orderByID) == undefined) {
      alert('error: expecting a form field id ' + orderByID);
   }
   if($(orderByID + '_asc') == undefined) {
      alert('error: expecting a form field id ' + orderByID + '_asc');
   }
   var oldorderby =  $(orderByID).value;
   $(orderByID).value = orderby;
   if(oldorderby == orderby) {
       if($(orderByID + '_asc').value == 'true') {
         $(orderByID + '_asc').value = '';
         $('order-by-' + orderby).src = 'icons/arrowdesc.gif';
      }
      else {
         $(orderByID + '_asc').value = 'true';
         $('order-by-' + orderby).src = 'icons/arrowasc.gif';
      }
   }
   else {
      $(orderByID + '_asc').value = '';
      $('order-by-' + orderby).src = 'icons/arrowdesc.gif';
      if(oldorderby != '') {
         $('order-by-' + oldorderby).src = 'icons/arrowup.gif';
      }
   }
}

function addPagenationOnclick(tableID,searchFunction,container) {
   if($('nav_index') == undefined) {
      alert('error: expecting a form field id \'nav_index\'');
   }
   if(container == undefined) {
      container = 'span';
   }
   $$('table#' + tableID +  ' thead > tr > td > ' + container).each(function(s) {
      s.style.cursor="pointer"
      Event.stopObserving(s, 'click', function() {
            $('nav_index').value=s.title;
            searchFunction();
         }
      );
      Event.observe(s, 'click', function() {
            $('nav_index').value=s.title;
            searchFunction();
         }
      );
   });
}

function addRowHeadingCSS(tableID,headingText,css) {
   $$('table#' + tableID + ' tbody > tr').each(function(s) {
      s.childElements().each(function(d) {
         if(d.innerHTML == headingText) {
            d.addClassName(css);
         }
      });         
   });
};

function addAddLink(elementID,addFunction) {
   var addLink = ' <img src=\"icons/add.gif\" id=\"' + elementID + 'AddIcon\"/> ';
   if($(elementID + 'AddIcon') == undefined) {
      Element.insert(elementID, {top: addLink});
      Event.stopObserving($(elementID), 'click', addFunction);
      Event.observe($(elementID), 'click', addFunction);
   }
};

function addEditLink(elementID,editFunction) {
   var editLink = ' <img src=\"icons/edit.gif\" id=\"' + elementID + 'EditIcon\"/> ';
   if($(elementID + 'EditIcon') == undefined) {
      Element.insert(elementID, {top: editLink});
      Event.stopObserving($(elementID), 'click', editFunction);
      Event.observe($(elementID), 'click', editFunction);
   }
};

function addUpdateLink(elementID,updateFunction) {
   var updateLink = ' <img src=\"icons/save2.gif\" id=\"' + elementID + 'UpdateIcon\"/> ';
   if($(elementID + 'UpdateIcon') == undefined) {
      Element.insert(elementID, {top: updateLink});
      Event.stopObserving($(elementID + 'UpdateIcon'), 'click', updateFunction);
      Event.observe($(elementID + 'UpdateIcon'), 'click', updateFunction);
   }
};

function addDeleteLink(elementID,deleteFunction) {
   var deleteLink = ' <img src=\"icons/delete.gif\" id=\"' + elementID + 'DeleteIcon\"/> ';
   if($(elementID + 'DeleteIcon') == undefined) {
      Element.insert(elementID, {top: deleteLink});
      Event.stopObserving($(elementID + 'DeleteIcon'), 'click', deleteFunction);
      Event.observe($(elementID + 'DeleteIcon'), 'click', deleteFunction);
   }
};

function addUndoLink(elementID,undoFunction) {
   var undoLink = ' <img src=\"icons/cancel2.gif\" id=\"' + elementID + 'UndoIcon\"/> ';
   if($(elementID + 'UndoIcon') == undefined) {
      Element.insert(elementID, {top: undoLink});
      Event.stopObserving($(elementID + 'UndoIcon'), 'click', undoFunction);
      Event.observe($(elementID + 'UndoIcon'), 'click', undoFunction);
   }
};

function addSearchIcon(elementID,searchFunction) {
   var searchIcon = ' <img src=\"icons/search2.gif\" id=\"' + elementID + 'SearchIcon\"/> ';
   if($(elementID + 'SearchIcon') == undefined) {
      Element.insert(elementID, {bottom: searchIcon});
      Event.stopObserving($(elementID + 'SearchIcon'), 'click', searchFunction);
      Event.observe($(elementID + 'SearchIcon'), 'click', searchFunction);
   }
};


// Load Type Dropdown
function typeDropdownLoad(inputID,parentID) {
   var feildName = $(inputID).name;
   var typeID = $(inputID).value;
   new Ajax.Request('ajaxproxy.html',{
		method:'get',
		parameters: '?function=types&element=types_dropdown&field_name=' + feildName + '&default=' + typeID + '&parent_id=' + parentID,
		onSuccess: function(transport){
		   var response = transport.responseText;
		   $(inputID).up().update(response);
  		},
		onFailure: function(){ alert('failed to get type dropdown.') }
	});      
}
