var xmlHttp;

function toggleBox(target) {
	if(document.getElementById(target).style.display == 'none') {
		document.getElementById(target).style.display = 'block';
	} else {
		document.getElementById(target).style.display = 'none';
	}
}

function tShowBox(target) { document.getElementById(target).style.display = 'block'; }
function tHideBox(target) { document.getElementById(target).style.display = 'none'; }

function confirmDelete(target,id,s) {
	var nice = target.replace('_',' ');
	var submsg = '';
	if(target == 'account') { submsg = "\n(this is permanent, there is no restore)"; }
	var r=confirm("Are you sure you want to remove this "+nice+"?"+submsg);
	if (r==true) {
//		alert(target+"_delete.php?id="+id+"&s="+s); return;
		window.location = target+"_delete.php?id="+id+"&s="+s;
	}
}

function confirmAjaxDelete(target,id) {
	if(target.length==0 || id==0 || id.length==0) { alert("Required input missing."); return; }
	
	var r=confirm("Are you sure you want to remove this "+target+" from your account?");
	if (r==true) {
		doAjaxDelete(target,id);
	}
}

function ajaxBegin() {
	try {  // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {  // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {

			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
 		}
	}
	return xmlHttp;
}

function doAjaxDelete(target,id) {
	if(target.length==0 || id==0 || id.length==0) { alert("Required input missing."); return; }

	var tab = '';
	if(target=='photo') { tab = 'photos'; }
	if(target=='video') { tab = 'videos'; }
	if(target=='audio') { tab = 'audio'; }
//	if(target=='message') { tab = 'messages'; }
	
	if(tab.length==0) { alert("Invalid target."); return; }

	var xmlHttp = ajaxBegin();
	if(!xmlHttp) return false;
	
	xmlHttp.onreadystatechange = function() {
	    if(xmlHttp.readyState==4) {
			var result = xmlHttp.responseText;
			if(result.substr(0,1) == '0') {
				var mid = result.substr(1);
				getTabData(mid,tab,0,'','',1);
			} else {
				alert(result);
			}
		}
	}

	xmlHttp.open("GET","ajax_"+target+"_delete.php?id="+id,true);
	xmlHttp.send('');
}

function getFeaturedMember(mid,tab1,tab2,tab3) {
	var xmlHttp = ajaxBegin();
	if(!xmlHttp) return false;
	
	xmlHttp.onreadystatechange = function() {
	    if(xmlHttp.readyState==4) {
			var result = xmlHttp.responseText;
			if(result.length == 0) {
				alert("Error loading featured members.");
			} else {
				// load the tab space with result data
				document.getElementById('featured').innerHTML = result;
			}
			return;
		}
	}

	xmlHttp.open("GET","ajax_featured.php?mid="+mid+"&tab1="+tab1+"&tab2="+tab2+"&tab3="+tab3,true);
	xmlHttp.send('');
}

function updateMyStatus() {
	var xmlHttp = ajaxBegin();
	if(!xmlHttp) return false;

	var status = document.getElementById('status').value;

	xmlHttp.onreadystatechange = function() {
	    if(xmlHttp.readyState==4) {
			var result = xmlHttp.responseText;
			alert(result);
		}
	}
		
	xmlHttp.open("GET","ajax_stat_update.php?status="+status,true);
	xmlHttp.send('');
}

// AJAX tab content loader ... Member ID, Tab Space, File ID
function getTabData(mid,tab,fid,show_popup,pop_msg,edit) {
	if(mid.length == 0 || tab.length == 0) {
		alert("Error - required input missing.");
		return false;
	}
	if(tab != 'profile' && tab != 'photos' && tab != 'videos' && tab != 'audio' && tab != 'messages' && tab != 'network') {
		alert("Error - that tab does not exist.");
		return false;
	}
	
	if(edit != 1 && edit != 0) { alert("Error - edit control flag missing."); return false; }
	
	var is_self = document.getElementById('is_self').value;

	var bg_img = 'bg_member_tabs.jpg';

	if(is_self == 1) {
		if(tab == 'profile') { bg_img = 'bg_member_tabs.jpg'; }
		if(tab == 'photos') { bg_img = 'bg_member_tabs2.jpg'; }
		if(tab == 'videos') { bg_img = 'bg_member_tabs3.jpg'; }
		if(tab == 'audio') { bg_img = 'bg_member_tabs4.jpg'; }
//		if(tab == 'messages') { bg_img = 'bg_member_tabs5.jpg'; }
//		if(tab == 'network') { bg_img = 'bg_member_tabs6.jpg'; }
	} else {
		if(tab == 'profile') { bg_img = 'bg_view_tabs.jpg'; }
		if(tab == 'photos') { bg_img = 'bg_view_tabs2.jpg'; }
		if(tab == 'videos') { bg_img = 'bg_view_tabs3.jpg'; }
//		if(tab == 'audio') { bg_img = 'bg_view_tabs4.jpg'; }
	}
	
	document.getElementById('tab_row').style.backgroundImage = "url('"+bg_img+"')";

	var xmlHttp = ajaxBegin();
	if(!xmlHttp) return false;
	
	xmlHttp.onreadystatechange = function() {
	    if(xmlHttp.readyState==4) {
			var result = xmlHttp.responseText;
			if(result.length == 0) {
				alert("Error loading "+tab+" tab.");
			} else {
				// load the tab space with result data
				document.getElementById('tab_space').innerHTML = result;

				if(show_popup.length > 0) {
					toggleBox(show_popup);
					toggleBox('blur');
					alert(pop_msg.replace(/<br>/g,"\n"));
				}
			}
			return;
		}
	}

	xmlHttp.open("GET","ajax_"+tab+"_tab.php?mid="+mid+"&fid="+fid+"&edit="+edit,true);
	xmlHttp.send('');
}

function findFriend() {
	// search via ajax to return results to the lightboxed add a friend form
	var name = document.getElementById('find_name').value;
	var email = document.getElementById('find_email').value;
	
	if(name.length == 0 && email.length == 0) {
		alert("You must enter a Name or Email to search for.");
		return;
	}

	var xmlHttp = ajaxBegin();
	if(!xmlHttp) return false;
	
	xmlHttp.onreadystatechange = function() {
	    if(xmlHttp.readyState==4) {
			var results = xmlHttp.responseText;
			if(results.length == 0) {
				alert("No new friends found matching those details.");
			} else {
				// parse returned data and populate a select drop-down list
				var data1 = results.split('_D1_');
				var data2;
				var count = data1.length;
				var x = 0;
				var fid = 0;
				var name = '';
				var band = '';
				var select_data = "<select name='fid' id='fid'><option value='0'>click to choose</option>";
								
				for(x = 0; x < count; x++) {
					data2 = data1[x].split('_D2_');

					fid = data2[0];
					name = data2[1];
					band = data2[2];
					
					select_data += "<option value='"+fid+"'>"+name+" ["+band+"]</option>";
				}
				select_data += "</select>";
				document.getElementById('add_a_friend1').style.display = 'none';
				document.getElementById('add_a_friend2').style.display = 'block';
				document.getElementById('friend_list').innerHTML = select_data;
			}
		}
	}
		
	xmlHttp.open("GET","ajax_find_friend.php?name="+name+"&email="+email,true);
	xmlHttp.send('');
}

function preparePrivateMsg(friend_count,author_id,subject) {
	// if this is a reply, set fields accordingly in messageAdd form
	if(friend_count == 0 || author_id == 0) { return; }

	var x;

	for(x = 1; x <= friend_count; x++) {
		if(document.getElementById('pm_r_id').options[x].value == author_id) {
			document.getElementById('pm_r_id').selectedIndex = x;
			break;
		}
	}

	document.getElementById('pm_subject').value = subject;
}

function prepareForumPost(parent_id,subject) {
	// if this is a reply, set fields accordingly in forumpostAdd form
	document.getElementById('forum_parent').value = parent_id;
	document.getElementById('forum_subject').value = subject;
}

function getForumData(page,post_id) {
	var ss = document.getElementById('forum_search').value;

	var xmlHttp = ajaxBegin();
	if(!xmlHttp) return false;
	
	xmlHttp.onreadystatechange = function() {
	    if(xmlHttp.readyState==4) {
			var result = xmlHttp.responseText;
			if(result.length == 0) {
				alert("Error loading forum data.");
			} else {
				// load the tab space with result data
				document.getElementById('the_forum').innerHTML = result;
			}
			return;
		}
	}

	xmlHttp.open("GET","ajax_forum.php?page="+page+"&pid="+post_id+"&ss="+ss,true);
	xmlHttp.send('');
}

function validatePhotoAdd() {
	var form = document.getElementById('formPhotoAdd');
	if(form.photo.value == '') { alert("You must browse and choose the file to be uploaded."); form.photo.focus(); return false; } 
	if(!form.agree.checked) { alert("You must certify that you have the right to upload these photos and that they do not violate the Community’s Terms and Conditions (by checking the box next to that line)."); form.agree.focus(); return false; }

	toggleBox('loader'); toggleBox('loadblur');
	return true;
}

function validateAudioAdd() {
	var form = document.getElementById('formAudioAdd');
	if(form.audio.value == '') { alert("You must browse and choose the file to be uploaded."); form.audio.focus(); return false; } 
	if(!form.agree.checked) { alert("You must certify that you have the right to upload these songs and that they do not violate the Community’s Terms and Conditions (by checking the box next to that line)."); form.agree.focus(); return false; }

	toggleBox('loader'); toggleBox('loadblur');
	return true;
}

function ajaxAudioMove(mid,aid,direction) {
	if(mid.length == 0 || mid == 0) { return false; }
	if(aid.length == 0 || aid == 0) { return false; }
	if(direction != 'left' && direction != 'right') { return false; }

	var xmlHttp = ajaxBegin();
	if(!xmlHttp) return false;
	
	xmlHttp.onreadystatechange = function() {
	    if(xmlHttp.readyState==4) {
			var result = xmlHttp.responseText;
			if(result.length == 0) {
//				alert("made it here1");
				getTabData(mid,'audio',0,'','',1);
//				alert("made it here2");
			} else {
				alert(result);
			}
			return;
		}
	}

//	alert("ajax_audio_move.php?aid="+aid+"&direction="+direction); return false;
	xmlHttp.open("GET","ajax_audio_move.php?aid="+aid+"&direction="+direction,true);
	xmlHttp.send('');
}

function loginRequired() {
	alert("You must be a logged in user to access this\nsite feature. Please login or Create an account now");
}
