var loaded = false;

function ifIE() {
	var browserName=navigator.appName;
	if (browserName=="Microsoft Internet Explorer") {
		return true;	
	} else {
		return false;	
	}
}
function setTDHeights() { //create the background images on the right and left side of all pages
	var flag = 0;
	var tableHeight = document.getElementById("maintable").clientHeight;
	var blHeight = tableHeight - 689;
	if(blHeight > -190) {
		document.getElementById("leftTD").height = blHeight + 191;
	} else {
		flag = 1;	
	}
	var brHeight = tableHeight - 340;
	if(brHeight > -190) {
		document.getElementById("rightTD").height = brHeight + 191;
		if(flag == 1) {
			document.getElementById("rightTD").height = 361;
		}
	}
}
function shrinkTDHeights() { //secifically for edit_profile
	try {
		document.getElementById("leftTD").height -= 185;
		document.getElementById("rightTD").height -= 185;
	} catch(e) {
	}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function onRCheck(id,img,page,istart,istop) { //custom radio buttons
	if(!istart) {
		istart = 0;	
	}
	if(!istop) {
		istop = 3;	
	}
	var radio = document.getElementById(id);
	
	if(radio.checked != true) {
		for(var i=istart; i<istop; i++) {
			if(document.getElementById("rimg" + (i + 1))) {
				document.getElementById("rimg" + (i + 1)).src = "images/global/radio_unchecked.jpg";
			}
		}
	}
	radio.checked = true;
	img.src="images/global/radio_checked.jpg";
	
	if(page) {
		showAll(page);
	}
	//when you click group, it takes out age. when you click fan, it takes out skill
	if(page=="signup") {
		if(id == 2) {
			changeSkills("plural");
			hide("age",page);
		} else if(id == 3) {
			changeSkills("single");
			hide("skill",page);
		} else {
			changeSkills("single");	
		}
	} else if(page=="edit_info") {
		if(id == 2) {
			changeSkills("plural");
			document.getElementById('age').className = "invis";
		} else if(id == 3) {
			changeSkills("single");
			document.getElementById('age').className = "vis";
			document.getElementById('skill').className = "invis";
		} else {
			changeSkills("single");
			document.getElementById('age').className = "vis";
			document.getElementById('skill').className = "vis";
		}
	}
}
function onCCheck(id,img) { //custom checkboxes
	var checkbox = document.getElementById(id);
	
	if(checkbox.checked != true) {
		checkbox.checked = true;
		img.src="images/global/checkbox_checked.jpg";
	} else {
		checkbox.checked = false;
		img.src="images/global/checkbox_unchecked.jpg";
	}
}
function onCCheckCP(id,img) { //custom checkboxes
	var checkbox = document.getElementById(id);
	
	if(checkbox.checked != true) {
		checkbox.checked = true;
		img.src="images/cpanel/msgs/checkbox_checked.png";
	} else {
		checkbox.checked = false;
		img.src="images/cpanel/msgs/checkbox_unchecked.png";
	}
}
function onCCheckSongs(type,img) {
	var checkbox = document.getElementById("type");	
	if(type == "instrumental") {
		if((parseInt(checkbox.value) % 2) == 0) {
			checkbox.value = parseInt(checkbox.value) - 1;
			img.src = "images/global/checkbox_unchecked.jpg";
		} else {
			checkbox.value = parseInt(checkbox.value) + 1;
			img.src = "images/global/checkbox_checked.jpg";
		}
	} else {
		if(parseInt(checkbox.value) < 3) {
			checkbox.value = parseInt(checkbox.value) + 2;
			img.src = "images/global/checkbox_checked.jpg";
		} else {
			checkbox.value = parseInt(checkbox.value) - 2;
			img.src = "images/global/checkbox_unchecked.jpg";
		}
	}
}
function showAll(page) {
	if(page == "signup") {
		document.getElementById("age").className = "vis";	
		document.getElementById("pinfo").style.marginTop = "54px";
		
		document.getElementById("skill").className = "vis";
	} else if(page == "edit_info") {
		document.getElementById("age").className = "vis";
		document.getElementById("pinfo").style.marginTop = '0px';
		
		document.getElementById("skill").className = "vis";
	}
}
function hide(section,page) {
	if(page == "signup") {
		if(section == "age") {
			document.getElementById("age").className = "invis";	
			document.getElementById("pinfo").style.marginTop = '82px';
		} else if(section == "skill") {
			document.getElementById("skill").className = "invis";
			document.getElementById("pinfo").style.marginTop = '70px';
		}
	} else if(page == "edit_info") {
		if(section == "age") {
			document.getElementById("age").className = "invis";	
			document.getElementById("pinfo").style.marginTop = '34px';
		} else if(section == "skill") {
			document.getElementById("skill").className = "invis";
			document.getElementById("pinfo").style.marginTop = '24px';
		}
	}
}
function changeSkills(target) {
	var rapper = document.getElementById('rapper');
	var singer = document.getElementById('singer');
	var producer = document.getElementById('producer');
	var dj = document.getElementById('dj');
	var instrumentalist = document.getElementById('instrumentalist');
	var extra;
	if(target=="plural") {
		extra = "s";
	} else {
		extra = "";	
	}
	rapper.value = "Rapper"+extra;
	rapper.innerHTML = "Rapper"+extra;
	singer.value = "Singer"+extra;
	singer.innerHTML = "Singer"+extra;
	producer.value = "Producer"+extra;
	producer.innerHTML = "Producer"+extra;
	dj.value = "DJ"+extra;
	dj.innerHTML = "DJ"+extra;
	instrumentalist.value = "Instrumentalist"+extra;
	instrumentalist.innerHTML = "Instrumentalist"+extra;
}
function clearComment() { //clear text in a new comment
	document.leave_comment.textarea.value="";
}
function createTextArea(id,trueid) {
	//hide static comment
	var commentTD = document.getElementById("comment" + id);
	var commentDiv = document.getElementById("cdiv" + id);
	commentDiv.className = "invis";
	
	//create textarea
	try {
		var editTA = document.createElement("<textarea name='edit_comment' class='edit_comment' id='commentTa" + id + "' maxlength='600'></textarea>");
	} catch(e) {
		var editTA = document.createElement("textarea");
		editTA.setAttribute("name", "edit_comment");
		editTA.className = "edit_comment";
		editTA.setAttribute("id", "commentTa" + id);
		editTA.setAttribute("maxlength", "600");
	}
	editTA.onkeyup = editTA.onchange = checkMaxLength;
	var commentText = document.getElementById("comment_content" + id); //get bbcode comment from hidden div (comment_content)
	var nlCT = commentText.innerHTML.replace(/<BR>/g, "\n"); //fill textarea with bbcode comment
	editTA.value = nlCT;
	commentTD.appendChild(editTA); //add textarea
	//create hidden field to store comment id
	try {
		var hidden = document.createElement("<input type='hidden' name='commentid' value='"+trueid+"' />");
	} catch(e) {
		var hidden = document.createElement("input");
		hidden.setAttribute("type", "hidden");
		hidden.setAttribute("name", "commentid");
		hidden.setAttribute("value", trueid);
	}
	commentTD.appendChild(hidden);
	
	//hide edit, delete buttons and pipe separator
	var sideTD = document.getElementById("scomment" + id);
	var editA = document.getElementById("cel" + id);
	var pipeIMG = document.getElementById("pipe" + id);
	var deleteA = document.getElementById("cdl" + id);
	editA.className = "invis";
	pipeIMG.className = "invis";
	deleteA.className = "invis";
	
	//create cancel link and finished button
	var cancelA = document.createElement("a");
	cancelA.setAttribute("href", "javascript:cancelEdit(" + id + ")");
	cancelA.setAttribute("id", "cancel" + id);
	sideTD.appendChild(cancelA);
	var cancelIMG = document.createElement("img");
	cancelIMG.setAttribute("src", "images/profile/cancel.jpg");
	cancelIMG.style.display = "block";
	cancelA.appendChild(cancelIMG);
	//create finished editing link and button
	var finishedDIV = document.createElement("div");
	finishedDIV.setAttribute("id", "finished" + id);
	sideTD.appendChild(finishedDIV);
	try {
		var finishedBUT = document.createElement("<input type='submit' name='edit' value=' ' class='finished' id='myFinished'"+id+"' />");
	} catch(e) {
		var finishedBUT = document.createElement("input");
		finishedBUT.setAttribute("type", "submit");
		finishedBUT.setAttribute("name", "edit");
		finishedBUT.setAttribute("value", " ");
		finishedBUT.className = "finished";
		finishedBUT.setAttribute("id", "myFinished" + id);
	}
	finishedDIV.appendChild(finishedBUT);
}
function cancelEdit(id) {
	//remove textarea
	var commentTD = document.getElementById("comment" + id);
	var editTA = document.getElementById("commentTa" + id);
	commentTD.removeChild(editTA);
	
	//remove cancel and finished editing buttons
	var sideTD = document.getElementById("scomment" + id);
	var cancelA = document.getElementById("cancel" + id);
	sideTD.removeChild(cancelA);
	var finishedBUT = document.getElementById("finished" + id);
	sideTD.removeChild(finishedBUT);
	
	//make static comment visible
	var commentDiv = document.getElementById("cdiv" + id);
	commentDiv.className = "comment"; //.comment has visibility:visible
	
	//make edit, delete buttons and pipe separator visible
	var editA = document.getElementById("cel" + id);
	var pipeIMG = document.getElementById("pipe" + id);
	var deleteA = document.getElementById("cdl" + id);
	editA.className = "vis";
	pipeIMG.className = "vis";
	deleteA.className = "vis";
}
function setBrowseTds() {
	var tableHeight = document.getElementById("browse_content").clientHeight;
	var leftEdge = document.getElementById("br_left_edge");
	leftEdge.height = tableHeight - 32;
}
function setFavTds() {
	var tableHeight = document.getElementById("favs_content").clientHeight;
	var leftEdge = document.getElementById("fav_left_edge");
	leftEdge.height = tableHeight - 32;
}
function selAll(input) { //select all text onfocus
	input.select();
}
function restore(input) { //dont let the input field be empty
	if(input.value == "") {
		input.value = "YOUR TEXT HERE";	
	}
}
function toCaps(input) { //make sure all letters are capital
	var currPos = input.selectionEnd;
	input.value = input.value.toUpperCase();
	setCaretPosition(input,currPos);
}
function setCaretPosition(element, caretPos) {
    var elem = element;

    if(elem != null) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else
                elem.focus();
        }
    }
}
function setMaxLength() { //set max char length of textarea
	var x = document.getElementsByTagName('textarea');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}
function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		this.value = this.value.slice(0,maxLength);
	}
}
function countStr(source,pattern) { //count how many occurrences of pattern are in are in string
	var count = 0;
	if (source!=null) {
		var len = pattern.length;
		var found = 1;
		var start = 0;
		while( (found = source.indexOf(pattern,start) ) != -1) {
			start = found + len;
			count++;
		}
		return count;
	}
	else return 0;
}
function clearMsg() { //clear text in a message from control panel
	document.compose.message.value="";
}
function setRating(newRating,pgtype) {
	input = document.getElementById(pgtype+"_rating");
	input.value = newRating;
	for(var i=10;i>newRating;i--) {
		document.getElementById(pgtype+"_star"+i).src = "images/global/star_unchecked.png";
	}
	for(var i=1;i<(newRating+1);i++) {
		document.getElementById(pgtype+"_star"+i).src = "images/global/star_checked.png";
	}
}
function restRating(origRating,pgtype) {
	input = document.getElementById(pgtype+"_rating");
	input.value = origRating;
	for(var i=10;i>origRating;i--) {
		document.getElementById(pgtype+"_star"+i).src = "images/global/star_unchecked.png";
	}
	for(var i=1;i<(origRating+1);i++) {
		document.getElementById(pgtype+"_star"+i).src = "images/global/star_checked.png";
	}
}
function setSpecRating(targetId, newRating) {
	input = document.getElementById(targetId+"rating");
	input.value = newRating;
	for(var i=10;i>newRating;i--) {
		document.getElementById(targetId+"star"+i).src = "images/global/star_unchecked.png";
	}
	for(var i=1;i<(newRating+1);i++) {
		document.getElementById(targetId+"star"+i).src = "images/global/star_checked.png";
	}
}
function restSpecRating(targetId, origRating) {
	input = document.getElementById(targetId+"rating");
	input.value = origRating;
	for(var i=10;i>origRating;i--) {
		document.getElementById(targetId+"star"+i).src = "images/global/star_unchecked.png";
	}
	for(var i=1;i<(origRating+1);i++) {
		document.getElementById(targetId+"star"+i).src = "images/global/star_checked.png";
	}
}
function pointer(tag) {
	tag.style.cursor = "hand";	
}
function nopointer(tag) {
	tag.style.cursor = "pointer";	
}
function clearMe(input,value) {
	if(input.value==value) {
		input.value = "";
	}
}
function restoreMe(input,value) {
	if(input.value=="") {
		input.value = value;
	}
}
function showReview() {
	document.getElementById("writereview").className = "vis";
	document.getElementById("writereview").style.zIndex = "10";
}
function hideReview() {
	Effect.Fade('writereview', { duration: 0.5 });
	setTimeout(removeCode, 600);
	function removeCode() {
		document.getElementById("review").innerHTML = "";
	}
}
function clearReview() { //clear text in a new review
	document.addreview.reviewta.value="";
}
//load reviews
var xmlHttp
function getReview(tid) {
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return
	}
	var url="write_review.php";
	url=url+"?tid="+tid;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		document.getElementById("review").innerHTML = "";
		document.getElementById("review").innerHTML=xmlHttp.responseText;
		showReview();
		setMaxLength();
		if(loaded==false) {
			replacement("writereview");
		}
		loaded = false;
		if(document.getElementById('review_content')) {
			CSBfleXcroll('review_content');
		}
		if(document.getElementById('review_content_guest')) {
			CSBfleXcroll('review_content_guest');
		}
	} 
}
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
//delete photos from edit photos page
function setDelete() {
	var input = document.getElementById("delete");
	input.value = "delete";
	document.edit_photos.submit();
}
function setSubmit() {
	var input = document.getElementById("edit_titles");
	input.value = "edit_titles";
	document.edit_photos.submit();
}
function setMove() {
	var input = document.getElementById("move_photos");
	input.value = "move_photos";
	document.edit_photos.submit();
}
//suggest contacts
var xmlHttpS
function suggestContact(contact) {
	if(contact) {
		xmlHttpS=GetXmlHttpObjectS();
		if (xmlHttpS==null) {
			alert ("Browser does not support HTTP Request");
			return
		}
		var url="suggest_contacts.php";
		url=url+"?contact="+contact;
		xmlHttpS.onreadystatechange=stateChangedS;
		xmlHttpS.open("GET",url,true);
		xmlHttpS.send(null);
	} else {
		var sugBox = document.getElementById('suggest_box');
		var sugList = document.getElementById('suggest_list');
		if(sugList.hasChildNodes()) {
			while(sugList.childNodes.length >= 1) {
				sugList.removeChild(sugList.firstChild);       
			} 
		}
		sugBox.style.display = "none";
	}
}
function stateChangedS() { 
	if (xmlHttpS.readyState==4 || xmlHttpS.readyState=="complete") {
		var len = xmlHttpS.responseText.length;
		var res = xmlHttpS.responseText.substring(0,(len-1));
		var arr_res = res.split(",");
		var sugBox = document.getElementById('suggest_box'); 
		var sugList = document.getElementById('suggest_list');
		if(sugList.hasChildNodes()) {
			while(sugList.childNodes.length >= 1) {
				sugList.removeChild(sugList.firstChild);       
			} 
		}
		for(var i=0;i<arr_res.length;i++) {
			var li = document.createElement("li");
			var a = document.createElement("a");
			a.href = "javascript:setTo('"+arr_res[i]+"')";
			a.innerHTML = arr_res[i];
			sugList.appendChild(li);
			li.appendChild(a);
		}
		sugBox.style.display = "block";
	} 
}
function GetXmlHttpObjectS() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function setTo(name) {
	var toField = document.getElementById('to');
	toField.value = name;
	var sugBox = document.getElementById('suggest_box');
	sugBox.style.display = "none";
}
function hideSugBox() {
	var sugBox = document.getElementById('suggest_box');
	sugBox.style.display = "none";
}
function changePass(input) {
	input.type = "password";
}