function _playerAdd(anchor) {
    var url = anchor.href;
    var code = '<object type="application/x-shockwave-flash" data="http://www.collegetoolbars.com/jsScuttle.php/includes/player/musicplayer_f6.swf?song_url=' + url +'&amp;b_bgcolor=ffffff&amp;b_fgcolor=000000&amp;b_colors=0000ff,0000ff,ff0000,ff0000&buttons=http://www.collegetoolbars.com/jsScuttle.php/includes/player/load.swf,http://www.collegetoolbars.com/jsScuttle.php/includes/player/play.swf,http://www.collegetoolbars.com/jsScuttle.php/includes/player/stop.swf,http://www.collegetoolbars.com/jsScuttle.php/includes/player/error.swf" width="14" height="14">';
    var code = code + '<param name="movie" value="http://www.collegetoolbars.com/jsScuttle.php/includes/player/musicplayer.swf?song_url=' + url +'&amp;b_bgcolor=ffffff&amp;b_fgcolor=000000&amp;b_colors=0000ff,0000ff,ff0000,ff0000&amp;buttons=http://www.collegetoolbars.com/jsScuttle.php/includes/player/load.swf,http://www.collegetoolbars.com/jsScuttle.php/includes/player/play.swf,http://www.collegetoolbars.com/jsScuttle.php/includes/player/stop.swf,http://www.collegetoolbars.com/jsScuttle.php/includes/player/error.swf" />';
    var code = code + '</object>';
    anchor.parentNode.innerHTML = code +' '+ anchor.parentNode.innerHTML;

}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
};

var deleted = false;
function deleteBookmark(ele, input){
    var confirmDelete = "<span>Are You Sure? <a href=\"#\" onclick=\"deleteConfirmed(this, " + input + ", \'\'); return false;\">Yes</a> - <a href=\"#\" onclick=\"deleteCancelled(this); return false;\">No</a></span>";
    ele.style.display = 'none';
    ele.parentNode.innerHTML = ele.parentNode.innerHTML + confirmDelete;
}

function deleteCancelled(ele) {
    var del = previousElement(ele.parentNode);
    del.style.display = 'inline';
    ele.parentNode.parentNode.removeChild(ele.parentNode);
    return false;
}

function deleteConfirmed(ele, input, response) {
    if (deleted == false) {
        deleted = ele.parentNode.parentNode.parentNode;
    }
    var post = deleted;
    post.className = 'xfolkentry deleted';
    if (response != '') {
        post.style.display = 'none';
        deleted = false;
    } else {
        loadXMLDoc('http://www.collegetoolbars.com/ajaxDelete.php?id=' + input);
    }
}

function previousElement(ele) {
    ele = ele.previousSibling;
    while (ele.nodeType != 1) {
        ele = ele.previousSibling;
    }
    return ele;
}


function isAvailable(input, response){
    var usernameField = document.getElementById("username");
    var username = usernameField.value;
    username = username.toLowerCase();
    username = username.trim();
    var availability = document.getElementById("availability");
    if (username != '') {
        usernameField.style.backgroundImage = 'url(http://www.collegetoolbars.com/loading.gif)';
        if (response != '') {
            usernameField.style.backgroundImage = 'none';
            if (response == 'true') {
                availability.className = 'available';
                availability.innerHTML = 'Available';
            } else {
                availability.className = 'not-available';
                availability.innerHTML = 'Not Available';
            }
        } else {
            loadXMLDoc('http://www.collegetoolbars.com/ajaxIsAvailable.php?username=' + username);
        }
    }
}
function isAvailable2(input, response){
    var usernameField = document.getElementById("username2");
    var username = usernameField.value;
    username = username.toLowerCase();
    username = username.trim();
    var availability = document.getElementById("availability2");
    if (username != '') {
        usernameField.style.backgroundImage = 'url(http://www.collegetoolbars.com/loading.gif)';
        if (response != '') {
            usernameField.style.backgroundImage = 'none';
            if (response == 'true') {
                availability.className = 'available';
                availability.innerHTML = 'Available';
            } else {
                availability.className = 'not-available';
                availability.innerHTML = 'Not Available';
            }
        } else {
            loadXMLDoc('http://www.collegetoolbars.com/ajaxIsAvailable2.php?username=' + username);
        }
    }
}
function useAddress(ele) {
    var address = ele.value;
    if (address != '') {
        if (address.indexOf(':') < 0) {
            address = 'http:\/\/' + address;
        }
        getTitle(address, null);
        ele.value = address;
    }
}

function getTitle(input, response){
    var title = document.getElementById('titleField');
    if (title.value == '') {
        title.style.backgroundImage = 'url(http://www.collegetoolbars.com/loading.gif)';
        if (response != null) {
            title.style.backgroundImage = 'none';
            title.value = response;
        } else if (input.indexOf('http') > -1) {
            loadXMLDoc('http://www.collegetoolbars.com/ajaxGetTitle.php?url=' + input);
        } else {
            return false;
        }
    }
}
function castvote(vid, vnum, response){
        if (response != '' && response != 'alreadyvoted') {
			 therid = "Rate" + vid;
			 document.getElementById(therid).title = response;
			 Outr(vid);
			 therid = "Ratedeco" + vid;
			 document.getElementById(therid).innerHTML = vnum + ' users rate this:' + response + ' of 5 stars';
        } else if(response != 'alreadyvoted'){
            loadXMLDoc('http://www.collegetoolbars.com/ajaxCastVote.php?vid=' + vid + '&vnum=' + vnum);
        }
}

// input = category id(cid)
// theid = target div tag
// response = what you're getting back
// userid = document.location / numeric userid
var perpage = 10;
var includemore = 0;
function getCategory(input, theid, response, userid) {
	var thecategory;
	if(!input.value){ thecategory = input; }
	else{ thecategory = input.value; }

	var theuserid = '';
	if(userid){
		theuserid = '&userid=' + escape(userid);
	}
	if(perpage){
		theuserid = theuserid + '&perpage=' + escape(perpage);
	}
	if(includemore == 1){
		theuserid = theuserid + '&includemore=1';
	}
       if (response != '') {
		    var sectiontochange = document.getElementById(theid);
			if(sectiontochange != null){
	            sectiontochange.innerHTML = response;
				var testsection = document.getElementById("area52");
				if(testsection != null){
					//testsection.value = response;
				}
			}
	}
	else if(thecategory != '' && thecategory >= 0){
            loadXMLDoc('http://www.collegetoolbars.com/ajaxLoadCategory.php?cid=' + thecategory + '&theid=' + theid + theuserid);

	}
}

function getsubCategory(input, theid, response) {
	var thecategory;
	if(!input.value){ thecategory = input; }
	else{ thecategory = input.value; }
    if (response != '') {
		    var sectiontochange = document.getElementById(theid);
			if(sectiontochange != null){
				//	var tmp = "id=\"" + theid + "\"";
				//	var re = new RegExp(tmp, "ig");
				//	var exists = document.getElementById("subCategory").innerHTML.match(re);
				//	if(!exists){
			            sectiontochange.innerHTML = response;
				//	}
			}
	}
	else if(thecategory != '' && thecategory >= 0){
			//check the sectiontochange before this call to be sure it does not already contain this select box
            loadXMLDoc('http://www.collegetoolbars.com/ajaxLoadSubCategoryList.php?cid=' + thecategory + '&theid=' + theid);
	}
}
// input will be an array
// first element id of target
// second element value to send function
// third element name of function called
function getSCD(input, theid, response) {
	var targetobj = input[0];
	var thecategory = input[1];
	var func = input[2];
	
    if (response != '') {
		    var subcat = document.getElementById(theid);	
            subcat.innerHTML = response;
	}
	else if(thecategory != '' && thecategory >= 0){
            loadXMLDoc('http://www.collegetoolbars.com/' + func + '?cid=' + thecategory + "&theid=" + theid);
	}

}


var xmlhttp;

function loadXMLDoc(url) {
    // Native
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = processStateChange;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    // ActiveX
    } else if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp) {
            xmlhttp.onreadystatechange = processStateChange;
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }
    }
}

function processStateChange() {
    if (xmlhttp.readyState == 4) {
	  	if(xmlhttp.status == 200){
	        response = xmlhttp.responseXML.documentElement;
			var method = '';
			var result = '';
			var input = '';
			if(response.getElementsByTagName('method')[0]){
	    	    method = response.getElementsByTagName('method')[0].firstChild.data;
			}
			if(response.getElementsByTagName('result')[0]){
	        	result = response.getElementsByTagName('result')[0].firstChild.data;
			}
			if(response.getElementsByTagName('input')[0]){
	        	input = response.getElementsByTagName('input')[0].firstChild.data;
			}
			if(method == "getCategory"){  //our special little baby function that needs special attention
				theid = response.getElementsByTagName('theid')[0].firstChild.data;
		        testresponse = xmlhttp.responseText;
				var myresult = testresponse.match(/<result>(.*?)<\/result>/);
				var mystuff = xmlunescape(myresult[1]);
			    eval(method + '(input, theid, mystuff)');
			}
			else if(response.getElementsByTagName('theid')[0] != null){   //means that there is a target document item theid
				theid = response.getElementsByTagName('theid')[0].firstChild.data;
				if(theid != '' ){
				    eval(method + '(input, theid, result)');
				}
			}
			else{   //just make the call with the result
		    	eval(method + '(input, result)');
			}
		}
    }
}

function playerLoad() {
    var anchors = document.getElementsByTagName('a');
    var anchors_length = anchors.length;
    for (var i = 0; i < anchors_length; i++) {
        if (anchors[i].className == 'taggedlink' && anchors[i].href.match(/\.mp3$/i)) {
            _playerAdd(anchors[i]);
        }
    }
}


// Escape characters
function xmlescape (string) {
  string = string.replace ("&", "&amp;");
  string = string.replace ("<", "&lt;");
  string = string.replace (">", "&gt;");
  string = string.replace ("\"", "&quot;");
  return string;
}
// unEscape characters
function xmlunescape (string) {
  string = string.replace (/&amp;/gi,"&");
  string = string.replace (/&lt;/gi,"<");
  string = string.replace (/&gt;/gi,">");
  string = string.replace (/&quot;/gi,"\"");
  string = string.replace (/&#039;/gi,"'");
  return string;
}

// Encode string to UTF-8
function encode_utf8(str) {
    str = escape (str);
    nc = "";
    for (ic = 0; ic < str.length; ic++) {
        s = str.substr(ic, 6);
        if (arr = s.match(/%u[0-9A-F][0-9A-F][0-9A-F][0-9A-F]/i)) {
            c = arr[0].substr(2, 4);
            c = "&#" + parseInt(c, 16) + ";";
            ic += 5;
        } else if (arr = s.match(/^%[0-9A-F][0-9A-F]/i)) {
            c = arr[0].substr(1, 2);
            c = "&#" + parseInt(c, 16) + ";";
            ic += 2;
        } else {
            c = str.charAt(ic);
        };
        nc += c;
    };
    return nc;
}

function goUniversity() {
	var thelocation = document.getElementById('gouniversity');
	var theuser = document.getElementById('gouser');
	if(thelocation){
		var finalloc = '';
		if(theuser){
			finalloc = thelocation.value + theuser.value;
		}
		else{
			finalloc = thelocation.value;
		}
		document.location = finalloc;
	}
}

function goUser() {
	//if goschool exists take that into account
	//go to location
	var thelocation = document.getElementById('gouniversity');
	var theuser = document.getElementById('gouser');
	if(theuser){
		var finalloc = '';
		if(thelocation){
			finalloc = thelocation.value + theuser.value;
		}
		else{
			finalloc = 'http://www.collegetoolbars.com/' + theuser.value;
		}
		document.location = finalloc;
	}
}

