/** * addOption() * Takes the option from one SELECT list and adds it to another for user-friendly * search selection. Updates a third field, a hidden field which the values in the * second select list for easier POSTING * @param string targetSelect * @param string sourceSelect * @param string hiddenField * @return void * @author Steven Mapes * @copyright 2007 StevenMapes.com - Do not use without written permisson **/ function addOption(targetSelect,sourceSelect,hiddenField) { var z = document.getElementById(sourceSelect); if (z.selectedIndex > 0 ) { var display = z.options[z.selectedIndex].text; var option = z.options[z.selectedIndex].value; var y = document.createElement('option'); y.text = display; y.value = option; var x = document.getElementById(targetSelect); try { x.add(y,null); } catch(ex) { x.add(y); } x.options[0].text = '* '+(x.length-1)+' selected *'; // Set hidden field var h = document.getElementById(hiddenField); h.value = h.value+option+','; } } /** * removeOption() * Removed the selected option from a SELECT input as well as from an hidden * field. I.E. Reverses addOption() * @param string selectBox * @param string hiddenField * @return void * @author Steven Mapes * @copyright 2007 StevenMapes.com - Do not use without written permisson **/ function removeOption(selectBox,hiddenField) { try { var x = document.getElementById(selectBox); if (x.selectedIndex > 0 ) { // Remove from hidden field var h = document.getElementById(hiddenField); h.value = h.value.replace(','+x.options[x.selectedIndex].value+',',','); x.remove(x.selectedIndex); x.options[0].text = '* '+(x.length-1)+' selected *'; x.selectedIndex = 0; } } catch (err) { // Do nothing } } /** * clearTextInput * Compares the value of the given object against the given string (or default string) * If the comparison matches, then the objects value is set to an empy string * @param object textField The object to compare the value property of * @param string compText The string to compare the value against * @return void * @author Steven Mapes **/ function clearTextInput(textField,compText) { compText = compText || 'Enter search keyword(s)'; try { if (textField.value == compText) { textField.value = ''; } } catch(err) { // Do nothing } } /** * Dynamically redefine the value of a syle * @return void * @author Steven Mapes **/ function setStyleById(elementID, styleParam, styleValue) { try { if (checkElementExists(elementID)) { var n = document.getElementById(elementID); n.style[styleParam] = styleValue; } } catch(err) { handleException(err); } } /** * * @access public * @return void **/ function setCurrentElementStyle(anElement,styleParam, stlyeValue){ try { anElement.style[styleParam] = stlyeValue; } catch(err) { handleException(err); } } /** * Dynamically Toggle the CSS class used against an element based on the ID of that element * @author Steven Mapes * @return void * @access public **/ function toggleDisplay(anElement,aClassName) { try { if (document.getElementById(anElement)) { n = document.getElementById(anElement); n.className = aClassName; } } catch(err) { handleException(err); } } /** * * @access public * @return void * @author Steven Mapes * @access public **/ function handleException(anErr){ var txt = "An error occured!\n\n Please contact transformers@themoon.co.uk with the following debugging information.\n\n"; txt += "\tError message: "+anErr.message+"\n"; txt += "\tError name: "+anErr.name+"\n"; txt += "\tError number: "+anErr.number +"\n"; txt += "\tError description: "+anErr.description+"\n"; txt += "\nClick OK to continue."; txt += "\nDump: "+var_export(anErr); alert(txt); } /** * **/ function moonException(aMessage,aName,aNumber,aDescripton) { aMessage = aMessage || false; aName = aName || false; aNumber = aNumber || false; aDescripton = aDescripton || false; this.message = aMessage; this.name = aName; this.number = aNumber; this.description = aDescripton; } /** * * @access public * @return void **/ function setStatus(statusMsg) { try { window.status = statusMsg; return true; } catch(err) { handleException(err); } } /** * checkElementExists() * Checks to see if a DOM element exists with the given ID * @param string el * @access public * @return void **/ function checkElementExists(el){ var response = false; if (document.getElementById(el)) { response = true; } return response; } /** * function_exists() * Replicates PHP function_exists call * @param mixed functionName {string} The name of the function to look for {object} the function object * @return boolean * @author Steven Mapes * @access public **/ function function_exists(functionName) { var result = false; if (typeof functionName == 'string' ) { if (typeof window[functionName] == 'function') { result = true; } } else { if (functionName instanceof Function) { result = true; } } return result; } /** * is_numeric() * Javascript equivilent of PHP is_numeric * @access public * @return boolean **/ function is_numeric(aNumber){ return !isNaN(aNumber); } /** * clearInput() * Clears down the give input element if its value matches the given text * @param object el The element to check * @param string txt The text to match in order to clear down the value of the object * @return void * @access public * @author Steven Mapes **/ function clearInput(el,txt){ if (el.value == txt) { el.value = ""; } } /** * var_export() * Performs an auction simliar to PHPs var_export($var,true); * @param mixed arr var to dump * @param integer level OPTIONAL level to dump? * @return string The textual representation of the array. * @access Steven Mapes * @version $Id$ **/ function var_export(arr,level) { var dumped_text = ""; if(!level) level = 0; //The padding given at the beginning of the line. var level_padding = ""; for(var j=0;j \"" + value + "\"\n"; } } } else { //Stings/Chars/Numbers etc. dumped_text = arr+" [["+typeof(arr)+"]]"; } return dumped_text; } /** * getNextSibling() * Returns the next sibling element, taking into account IE not treating whitespaces as a valid next element * @param object el The Element to look for the prebious sibling of * @param boolean allowLoops A flag to say if their is no real previous element then use the last sibling as the previous element * @access public * @return object * @author Steven Mapes * @project DOM Manipulation **/ function getNextSibling(el,allowLoops){ allowLoops = allowLoops || false; try { var next = el.nextSibling; if (null != next) { while(next.nodeType != 1) { next = next.nextSibling; if (null == next) { break; } } } // Final check if (null == next) { if (allowLoops != false) { next = getFirstSibling(el); } else { next = el; } } } catch(err) { handleException(err); } return next; } /** * getPreviousSibling() * Returns the previous sibling element, taking into account IE not treating whitespaces as a valid next element * @param object el The Element to look for the prebious sibling of * @param boolean allowLoops A flag to say if their is no real previous element then use the last sibling as the previous element * @access public * @return object * @author Steven Mapes * @project DOM Manipulation **/ function getPreviousSibling(el,allowLoops){ allowLoops = allowLoops || false; try { var prev = el.previousSibling; if (null != prev) { while(prev.nodeType != 1) { prev = prev.previousSibling; if (null == prev) { break; } } } // Final check if (null == prev) { if (allowLoops != false) { prev = getLastSibling(el); } else { prev = el; } } } catch(err) { handleException(err); } return prev; } /** * getFirstSibling() * Returns the first valid sibling for the given element * @param object el The element to look for the first sibling of * @access public * @return object * @author Steven Mapes * @project DOM Manipulation **/ function getFirstSibling(el){ try { var first = el.parentNode.firstChild; while(first.nodeType != 1 && first.nextSibling != null){ first = first.nextSibling; } if (null == first) { first = el; } } catch(err) { handleException(err); } return first; } /** * getLastSibling() * Returns the last valid sibling for the given element * @param object el The element to look for the last sibling of * @access public * @return object * @author Steven Mapes * @project DOM Manipulation **/ function getLastSibling(el){ try { var last = el.parentNode.lastChild; while(last.nodeType != 1 && last.previousSibling != null){ last = last.previousSibling; } if (null == last) { last= el; } } catch(err) { handleException(err); } return last; } /** * * @access public * @return void **/ function toggleDivDisplay(eId){ if (document.getElementById(eId)) { if (document.getElementById(eId).style.display != 'block') { document.getElementById(eId).style.display = 'block'; } else { document.getElementById(eId).style.display = 'none'; } } }