(function() { /** * Fills a combo box with values from an XML file, further specified * by using the query */ jQuery.fn.fillFromXML = function(xmlDocument, query) { var elementToUpdate = $(this); $(this).empty(); $(elementToUpdate).append(''); $(xmlDocument).find(query).each(function() { if ( $(elementToUpdate).find("option:contains('" + $(this).text() + "')").length == 0 ) { $(elementToUpdate).append(''); } }); } /** * Appends an option to an existing combo box */ jQuery.fn.appendOption = function(value, label, empty) { if (empty) $(this).empty(); if ( $(this).find(" option[@value='" + value + "']").length == 0 ) { $(this).append(''); } } /** * Loads an XML file into memory and returns it * as a jQuery object */ jQuery.load = function(xmlURL) { var xmlDocument; $.ajax({type:"GET", async:false, url:xmlURL, dataType:"xml", success: function(xml) { xmlDocument = xml; }}); return xmlDocument; } /** * Sorts a collection of XML elements into a specific order * denoted by the query */ jQuery.fn.sort = function(query) { var list = jQuery.makeArray(this); list = jQuery.makeArray(this).sort(function(a,b) { if ( Number($(a).attr(query)) < Number($(b).attr(query)) ) return -1; else if ( Number($(a).attr(query)) > Number($(b).attr(query)) ) return 1; else return 0; }); return list; } /** * Determines whether two entities are equal to each other */ jQuery.fn.equals = function(other) { var value = $(this).text(); if ( value != other ) { return false; } return true; } /** * Returns the count of how many times a value * is repeated in the specified array */ jQuery.howManyInArray = function(value, arr) { var count = 0; $(arr).each(function() { if (this == value) count++; }); return count; } /** * Returns an array of parameter values inside the URL * string of the current browser page with parameter name * of 'param' */ jQuery.getHttpGetParameters = function(param) { // split the URL at the ? character var hrefArray = document.location.href.split('?'); // if the split produces only 1 array element then return an error if (hrefArray.length == 1) { alert('No parameters detected.'); return ''; } // split the 2nd part of the array at the & character var parameterPairs = hrefArray[1].split('&'); var parameterValues = ''; // iterate over the pairings (param1=a,param2=b) for (var pair in parameterPairs) { // get the param and value into array var bits = parameterPairs[pair].split('='); // if the param equals our chosen param, then add it to the list if (bits[0] == param) { parameterValues += bits[1] + ','; } } return parameterValues.substring(0, parameterValues.length-1).split(','); } /** * finds all images specified by the query and if they * don't physically exist then replace them with another * image */ jQuery.fn.checkImages = function(replacement) { $(this).each(function() { if ( !$.isImageOK(this) ) { if (replacement == null) { $(this).hide(); } else { $(this).attr('src', replacement); } } }); } /** * Checks an image to see if it exists */ jQuery.isImageOK = function(img) { // During the onload event, IE correctly identifies any images that // weren't downloaded as not complete. Others should too. Gecko-based // browsers act like NS4 in that they report this incorrectly. if (!img.complete) { return false; } // However, they do have two very useful properties: naturalWidth and // naturalHeight. These give the true size of the image. If it failed // to load, either of these should be zero. if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) { return false; } // No other way of checking: assume it's ok. return true; } /** * Renders a dropdown combo box on screen, with a label and * default option of 'Please Select' */ jQuery.fn.renderDropdown = function (label, id, onchange) { $(this).append('
' + '' + '' + '

'); $('.childCombo').show(); $("#" + id).appendOption('Please Select', 'Please Select', false); } })(jQuery);