// used on forum.tpl for checking all check boxes
	function checkAll(form,check) {
			var object = document.forms[form].elements[check];
			if(document.forms[form].elements[check].length) {
				var count = document.forms[form].elements[check].length;
				for (i=0; i < count; i++) {
					if (object[i].checked==false) {
						object[i].checked=true;
					} else {
						object[i].checked=false;
					}
				}
			} else {
					if (object.checked==false) {
						object.checked=true;
					} else {
						object.checked=false;
					}
			}
	}

// used on forum.tpl for quoting people
	function doQuote(user,label) {
		clearQuickReply();
		document.quickReply.post.value = "[quote="+URLDecode(user)+"]"+URLDecode(label)+"[/quote]";
	}

// used on forum.tpl for quoting people
	function doEdit(post_id, label) {
		clearQuickReply();
		document.quickReply.post.value = URLDecode(label);
		document.quickReply.post_id.value = post_id;
		document.quickReply.opt.value = "edit";
		document.quickReply.Submit.value = "Update";
	}

//used to clear contents of the quickreply textboc
	function clearQuickReply() {
		document.quickReply.post.value = '';
		document.quickReply.opt.value = "quick";
		document.quickReply.Submit.value = "Submit";
		document.quickReply.post_id.value = '';
	}

//clears text (ie, search box)
function clearText(thefield){
if (thefield.defaultValue==thefield.value)
    thefield.value = ""
}

// decode url
function URLDecode(string)
{
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var encoded = string;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2)
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

// encode url
function URLEncode(string)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = string;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	//document.URLForm.F2.value = encoded;
	return encoded;
};

// open pop up window
function openWindow(page,width,height,left,top) {
	newWin = window.open(page,'',"width="+width+",height="+height+",left="+left+",top="+top+",toolbar=0,statusbar=0,locationbar=0,menubar=0,scrollbars=1");
	newWin.focus();
}

// open pop up window for printer friendly lyrics
function openWinPrn(url) {
	win = window.open(url,"Lyrics","height=450,width=400,toolbar=no,statusbar=no,location=no,menubar=yes,scrollbars=yes,resizeable=no");
	win.focus();
}

//Used for the FireFox search plugin install
function errorMsg(name,ext,cat)
{
  alert("Netscape 6, Mozilla, or FireFox is needed to install a sherlock plugin.");
}
function addEngine(name,ext,cat)
{
  if ((typeof window.sidebar == "object") && (typeof
  window.sidebar.addSearchEngine == "function"))
  {
    //cat="Web";
    //cat=prompt("In what category should this engine be installed?","Web")
    window.sidebar.addSearchEngine(
      "http://musicmademe.com/static/en_US/plugins/"+name+".src",
      "http://musicmademe.com/static/en_US/plugins/"+name+"."+ext,
      name,
      cat );
  }
  else
  {
    errorMsg(name,ext,cat);
  }
}

//shows how many letters a user has left when typing a comment
function textChanged() {
    charsleft = 1000 - document.addbox.text.value.length;
    if (charsleft<0) {
        document.addbox.text.value = document.addbox.text.value.substr(0,1000);
        charsleft = 0;
    }
    document.addbox.counter.value = charsleft;
}

//for the add reviews page
function setSkull(value, picwidth) {
       //kick out old browsers
       if (!document.getElementById) { return; }
       var lskull = document.getElementById('lskull').style;
       var rskull = document.getElementById('rskull').style;
       lskull.width= (picwidth/10)*(value)+'px';
       rskull.width= (picwidth/10)*(10-value)+'px';
}

//used in input forms where only X characters can be used
function enforcechar(what,limit){
if (what.value.length>limit)
what.value = what.value.substring(0, limit);
}

//selects a certain option of a select box
function selectOption(name, optValue)
{
    objs = document.getElementsByName(name);

    // object with this name wasn't found...
    if (!objs || objs.length == 0) return;

    // todo: check if this is a select HTML object

    // use only the first found object
    select = objs[0];

    for (i in select.options)
    {
        if (select.options[i].value == optValue)
        {
            select.options[i].selected = true;
            break;
        }
    }
}

//nice div popup boxes on, ie, user_list
function more(link, selected)
{
        // at first get the absolute top,right position of the hovered link on the page
        var x = 5;
	var y = link.offsetHeight + 2;
        var obj = link;
		while (true) {
			x += obj.offsetLeft;
			y += obj.offsetTop;
			// reached the toplevel node?
			if (!obj.offsetParent) break;
			obj = obj.offsetParent;
		}
        
        // now make sure all other div's are hidden
        var index = 0;
        var obj = null;
    	while (obj = document.getElementById('more' + (++index)))
        {
            obj.style.display = "none";
        }
        // finally display and set the size of our div
        obj = document.getElementById('more' + selected);
        if (obj) 
        {
            obj.style.display = "";
            obj.style.left = x + "px";
            obj.style.top = y + "px";
        }
}

//expands a block of text (ie, users_friends)
function expand(id, max)
{
    	isOpen = false;
        for (i = 1; i <= max; i++)
        {
            if (('expand' + i == id) && (document.getElementById(id).style.display == ""))
                isOpen = true;
            document.getElementById('expand' + i).style.display = "none";
        }
        if (isOpen)
            document.getElementById(id).style.display = "none";
	    else document.getElementById(id).style.display = "";
}

//used to select numerous items at once (ie, revise content)
function doSelect(obj) {
    for(i=0;i<obj.elements['entries[]'].length;i++)
      obj.elements['entries[]'][i].checked = !obj.elements['entries[]'][i].checked;
    obj.elements['entries[]'].checked = !obj.elements['entries[]'].checked;
}

//some sites like to load our content in a frame on their site
function breakout_of_frame()
{
  if (top.location != location) {
    top.location.href = document.location.href ;
  }
}

//clicking a form input field removes the default text.  clicking out without changing anything reverts to default text
//onclick="clickclear(this, 'default text')" onblur="clickrecall(this,'default text')"
function clickclear(thisfield, defaulttext) {
  if (thisfield.value == defaulttext) {
    thisfield.value = "";
  }
}
function clickrecall(thisfield, defaulttext) {
  if (thisfield.value == "") {
    thisfield.value = defaulttext;
  }
}

function AJAXFavesSort(option,id,comment){
    if (window.ActiveXObject)    {
        // user is using IE
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        // user is using Safari/Mozilla/Netscape
        xmlHttp = new XMLHttpRequest();
    } else {
        alert("Your web browser does not support AJAX; you must upgrade your browser to the newest version!");
    }
 
    xmlHttp.onreadystatechange=function(){
      // state change goes here
      if (xmlHttp.readyState == 4){
        document.getElementById("AJAXFavesSort").innerHTML = xmlHttp.responseText;
      }
    }

    // here is the magic:  this uses the xmlHttp object to communicate with the server
    var url = "AJAXFavesSort.php?opt=" + option + "&id=" + id;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function AJAXCountrySwitch(country_id){
    if (window.ActiveXObject)    {
        // user is using IE
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        // user is using Safari/Mozilla/Netscape
        xmlHttp = new XMLHttpRequest();
    } else {
        alert("Your web browser does not support AJAX; you must upgrade your browser to the newest version!");
    }

    xmlHttp.onreadystatechange=function(){
      // state change goes here
      if (xmlHttp.readyState == 4){
        document.getElementById("AJAXCountrySwitch").innerHTML = xmlHttp.responseText;
      }
    }

    // here is the magic:  this uses the xmlHttp object to communicate with the server
    var url = "AJAXCountrySwitch.php?country_id=" + country_id;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function AJAXCountrySwitchVideos(country_id){
    if (window.ActiveXObject)    {
        // user is using IE
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        // user is using Safari/Mozilla/Netscape
        xmlHttp = new XMLHttpRequest();
    } else {
        alert("Your web browser does not support AJAX; you must upgrade your browser to the newest version!");
    }

    xmlHttp.onreadystatechange=function(){
      // state change goes here
      if (xmlHttp.readyState == 4){
        document.getElementById("AJAXCountrySwitchVideos").innerHTML = xmlHttp.responseText;
      }
    }

    // here is the magic:  this uses the xmlHttp object to communicate with the server
    var url = "AJAXCountrySwitchVideos.php?country_id=" + country_id;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function AJAXListSort(option,id,listID){
    if (window.ActiveXObject)    {
        // user is using IE
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (window.XMLHttpRequest) {
        // user is using Safari/Mozilla/Netscape
        xmlHttp = new XMLHttpRequest();
    } else {
        alert("Your web browser does not support AJAX; you must upgrade your browser to the newest version!");
    }
 
    xmlHttp.onreadystatechange=function(){
      // state change goes here
      if (xmlHttp.readyState == 4){
        document.getElementById("AJAXListSort").innerHTML = xmlHttp.responseText;
      }
    }

    // here is the magic:  this uses the xmlHttp object to communicate with the server
    var url = "AJAXListSort.php?opt=" + option + "&id=" + id + "&d=" + listID;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

//used for show/hide
//taken from http://www.killersites.com/mvnforum/mvnforum/viewthread?thread=2900
function showDiv(objectID) {
	var theElementStyle = document.getElementById(objectID);

    if(theElementStyle.style.display == "none") {
	theElementStyle.style.display = "block";
    } else {
	theElementStyle.style.display = "none";
    }
}