
function setStyle(count,style,value) {
  targettags[count].style[style]= value;
}

//Function toggle allows to hide or show an element based on its ID
function toggle(targetId,ShouldSetCookie) {
  var onDisplay = '';			
  if (document.getElementById) {
    target = document.getElementById(targetId);
    if (target.style.display == "none") {
      var previous = (onDisplay == '')?null:document.getElementById(onDisplay);
      if(previous != null) {
        previous.style.display = "none";
      }
    target.style.display = "";
    onDisplay = targetId;
    } else {
      target.style.display = "none";
    }
  }
  if(ShouldSetCookie == 1) {			
    handleCookie(getCookie(window.document.URL.toString()));
    setCookie(window.document.URL.toString(),targetId);	
  }
}

function setCookie(name, value, expires, path, domain, secure) {
  var today = new Date();
  var expiry = new Date(today.getTime() + 660000); //1 year in miliseconds
  //we need to untoggle the previous list before we set the new value in the cookie
  //handleCookie(value);
  var newDate = expiry.toGMTString(expiry);
  var curCookie = name + "=" + escape(value) +
  ((expiry.toGMTString()) ? "; expires=" + expiry.toGMTString() : "") + 
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function handleCookie(value) {
  if(value != null) {
    toggle(value);
  }
}
	
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
  }	

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

if(document.getElementById){	
  //This part finds all the matches for the selected tag (in this case UL)
  var tagType = document.getElementById("tagType");
  if(tagType) {
	tagName = tagType.value;
	targettags = document.getElementsByTagName(tagName);
	
	//The FOR loop  goes through the tags picked by the above tags by their array [index] 
	//and selects the indeces of those that match the NEEDEDCLASS variable. It then passes
	//the indeces of the matched tags to the SetStyle function to apply the style.
	for (var count = 0; count < targettags.length; count++)  {
		//var withclass = (targettags[count].className).toLowerCase();
		//var needclass = 'js_toggle';
	
		if(hasClass(targettags[count],'js_toggle')){
		//apply style to tag element number X on the page
		setStyle(count, 'display', 'none');
		}
	}
	handleCookie(getCookie(window.document.URL.toString()));
  }
}
