
/* app.js version of the latest refapps */

var appbase = (function(jQuery){

  if (!jQuery) {
    alert('Missing jQuery Library');
    return null;
  }
  
  return {
	  
    util : {
	    // disables browser auto completion for the given element
	    disableAutoComplete : function(elemId) {
	      jQuery("#"+elemId).attr("autocomplete", "off");
	    },
	
	    // trims a prefix from a given string, this can be used to trim
	    // a certain prefix from DOM element IDs for further processing on the ID
	    trimPrefix : function(str, prefix) {
	      return str.substring(prefix.length);
	    },
	
	    // appends the parameter with the given name and
	    // value to the given url and returns the changed url
	    appendParamToURL : function(url, name, value) {
	      var c = "?";
	      if(url.indexOf(c) != -1) {
	        c = "&";
	      }
	      return url + c + name + "=" + encodeURIComponent(value);
	    },
	
	    // dynamically loads a CSS file
	    loadCSSFile : function(url) {
	      var elem = document.createElement("link");
	      elem.setAttribute("rel", "stylesheet");
	      elem.setAttribute("type", "text/css");
	      elem.setAttribute("href", url);
	
	      if(typeof elem != "undefined") {
	        document.getElementsByTagName("head")[0].appendChild(elem);
	        appbase.util.loadedCSSFiles.push(url);
	      }
	    },
	
	    // array to keep track of the dynamically loaded CSS files
	    loadedCSSFiles : [],
	
	    // removes all dynamically loaded CSS files
	    clearDynamicCSS : function() {
	      for(var i=0; i<appbase.util.loadedCSSFiles.length; i++) {
	        appbase.util.unloadCSSFile(appbase.util.loadedCSSFiles[i]);
	      }
	    },
	
	    // dynamically unloads a CSS file
	    unloadCSSFile : function(url) {
	      var candidates = document.getElementsByTagName("link");
	      for(var i=candidates.length; i>=0; i--) {
	        if(candidates[i] && candidates[i].getAttribute("href") != null && candidates[i].getAttribute("href").indexOf(url) != -1) {
	          candidates[i].parentNode.removeChild(candidates[i]);
	        }
	      }
	    },
	
	    // checks if cookies are enabled
	    cookiesEnabled : function() {
	      var currentCookie = document.cookie;
	      document.cookie = "Enabled=true";
	      var cookieValid = document.cookie;
	      var result = false;
	
	      if(cookieValid.indexOf("Enabled=true") != -1) {
	        result = true;
	      }
	
	      document.cookie = currentCookie;
	      return result;
	    }
	  }    
  }
})(jQuery);



