function GET(v)
{
	var url = document.location.href;
	var file = (url.split('/'))[url.split('/').length - 1];
	var start = file.indexOf(v+'=') + v.length + 1;

	if(start == v.length)
		return null;
		
	var param = file.substring(start, file.length); param = (param.split('&'))[0]; param = (param.split('#')[0]);
	
	if(param == "")
		return null;
	
	return param;
}

var xmlhttp = {
	Create_XMLHTTP : function()
	{
	//	if(this.http == null)
	//	{
			try { this.http = new XMLHttpRequest();}
			catch(e) {
				try {this.http = new ActiveXObject("Msxml2.XMLHTTP");}
				catch(e) {
					try { this.http = new ActiveXObject("Microsoft.XMLHTTP");}
					catch(e){ return false;}
				}
			}
	//	}
		
		return true;
	},
	
	ReadyState_do : function(func_call, file, params)
	{
		if(this.progress == 1)
		{
			alert('In progress');
			return false;
		}
		
		this.progress = 1;
		
		this.http.onreadystatechange = function()
		{
			if(xmlhttp.http.readyState == 4)
				eval(func_call);
		}
		
		var params_ = null;
		
		if(params != null)
		{
			var params_ = '';
			
			for(var i = 0 ; i < params.length ; i++)
			{
				params_ += ((i+1) % 2 == 1 ? (i == 0 ? params[i] : '&'+params[i]) : '='+escape(params[i]));
			}
		}
		
		this.http.open('POST', file, true);
		this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.http.setRequestHeader("Connection", "close");
		this.http.send(params_);
	},
	
	Response_get : function()
	{
		return this.http.responseText;
	},
	
	End_Progress : function()
	{
		this.progress = 0;
	},
	
	http : null,
	progress : 0
};

var comments = {
	Post_Comment : function()
	{
		var create_http = xmlhttp.Create_XMLHTTP();
		
		if(create_http == false)
		{
			alert('Could not set up XML HTTP request! Please update your browser to use this feature.');
			return false;
		}
		
		var commentText = vprofile.$('userComment').value;
		var currentCount = vprofile.$('commentCount').value;
		if(this.post_anyway == false)
			xmlhttp.ReadyState_do('comments.Check_Post();', 'ajaxphp/comments.php', ['o', 'post', 'id', GET('id'), 'n', currentCount, 'c', commentText]);
		else
			xmlhttp.ReadyState_do('comments.Check_Post();', 'ajaxphp/comments.php', ['o', 'post', 'id', GET('id'), 'n', currentCount, 'b', 'true', 'c', commentText]);
	},
	
	Check_Post : function()
	{
		var res = xmlhttp.Response_get();
		var res_s = (res.split(','))[0];
		
		if(res_s == 's1')
		{
			var res_m = (res.split(','))[1];
			var res_u = (res.split(','))[2];
			var res_d = (res.split(','))[3];
			
			vprofile.$('userPostNewComment').style.display = 'none';
			
			try
			{
				vprofile.$('userNewCommentBy').innerHTML = res_u.replace(/"/g, ',');
				vprofile.$('userNewCommentDate').innerHTML = res_d.replace(/"/g, ',');
				vprofile.$('userNewCommentText').innerHTML = res_m.replace(/"/g, ',');
				vprofile.$('userNewCommentMain').style.display = '';
			}
			catch(e)
			{
				xmlhttp.End_Progress();
				
				this.Change_Page(1, GET('id'), vprofile.$('commentPageA').value);return true;
			}
					
			vprofile.$('userComment').value = '';
			
			try{ vprofile.$('noComments').style.display = 'none';}
			catch(e){}
			
			try{ vprofile.$('userCommentWarn').style.display = 'none';}
			catch(e){}
			
			xmlhttp.End_Progress();
		//	this.Change_Page(1, GET('id'), vprofile.$('commentPageA').value);
		}
		else if(res_s == 'e8')
		{
			this.post_anyway = true;
			vprofile.$('userCommentWarn').style.display = '';
			xmlhttp.End_Progress();
		}
		else
			alert('There was an error. Error Code: '+res_s);
	},
	
	QuoteUser : function(user)
	{
		if(vprofile.$('userComment').value == '')
			vprofile.$('userComment').value = '@'+user+"\n";
		else
			vprofile.$('userComment').value += "\n"+'@'+user+"\n";
		
		vprofile.$('userComment').focus();
	},
	
	Report : function(id, click)
	{
		var create_http = xmlhttp.Create_XMLHTTP();
		
		if(create_http == false)
		{
			alert('Could not set up XML HTTP request! Please update your browser to use this feature.');
			return false;
		}
		
		xmlhttp.ReadyState_do('comments.Check_Report(\''+click+'\');', 'ajaxphp/comments.php', ['o', 'report', 'id', id]);
	},
	
	Check_Report : function(click)
	{
		res = xmlhttp.Response_get();
		res_s = (res.split(','))[0];
		
		if(res_s == 's1')
		{
			vprofile.$(click).style.color = '#aa0000';
			vprofile.$(click).innerHTML = 'Reported';
		}
		else
			alert('There was an error. Error Code: '+res_s);
		
		xmlhttp.End_Progress();
	},
	
	Delete_Comment : function(id, click)
	{
		var create_http = xmlhttp.Create_XMLHTTP();
		
		if(create_http == false)
		{
			alert('Could not set up XML HTTP request! Please update your browser to use this feature.');
			return false;
		}
		
		xmlhttp.ReadyState_do('comments.Check_Delete(\''+click+'\');', 'ajaxphp/comments.php', ['o', 'delete', 'id', id]);
	},
	
	Check_Delete : function(click)
	{
		var res = xmlhttp.Response_get();
		var res_s = (res.split(','))[0];
		
		if(res_s == 's1')
		{
			vprofile.$(click).style.color = '#555';
			vprofile.$(click).innerHTML = 'Deleted';
		}
		else
			alert('There was an error. Error Code: '+res_s);
		
		xmlhttp.End_Progress();
	},
	
	Change_Page : function(page, id, amount)
	{
		var create_http = xmlhttp.Create_XMLHTTP();
		
		if(create_http == false)
		{
			alert('Could not set up XML HTTP request! Please update your browser to use this feature.');
			return false;
		}
		
		xmlhttp.ReadyState_do('comments.Page_Change('+page+');', 'ajaxphp/comments.php', ['o', 'page', 'id', id, 'p', page, 'a', amount]);
	},
	
	Page_Change : function(page)
	{
		var res = xmlhttp.Response_get();
		var res_s = (res.split(','))[0]
		var template = [];
		
		if(res_s == 's1')
		{
			var data = res.split(',');
			var ch = 0;
			for(var i = 1 ; i < data.length -1; i += 10)
			{
				res_rname = data[i].replace(/"/g, ',');
				res_date = data[i + 1];
				res_comment = data[i + 2].replace(/"/g, ',');
				res_id = data[i + 3];
				res_report = data[i + 4];
				res_loggedin = data[i + 5];
				res_delete = data[i + 6];
				res_userid = data[i + 7];
				res_username = data[i + 8];
				res_photo = data[i + 9];
				
				template[i - (ch * 10) + ch] = '<div style="margin-bottom:10px;">'+"\n"+
							  '    <div class="commentBy"><div class="commentSepLine"></div>Comment by <a href="index.php?proc=disprofile&id='+res_userid+'" class="commentUserLinks">'+res_rname+'</a> at '+res_date+'</div>'+"\n"+
								(res_photo != null && res_photo != '' ? '<div class="commentAva"><img src="images/'+res_username+'/'+res_photo+'" style="width:40px;height:40px;" /></div>' : '')+
							  '    <div class="commentText">'+res_comment+'</div>'+"\n"
				
				if(res_loggedin == '1')
				{
					template[i - (ch * 10) + ch] +=  '    <div class="commentMenuBar">'+"\n"+
						'        <a href="#" onclick="comments.QuoteUser(\''+res_rname+'\');return false;">Reply</a>'+"\n"+
						'        | <a href="#" id="report_comment_'+res_id+'" onclick="'+(res_report == '0' ? 'comments.Report(\''+res_id+'\', this.id);' : '')+'return false;"'+
						(res_report == '1' ? ' style="color:#aa0000;"' : '')+'>'+(res_report == '1' ? 'Reported' : 'Report')+'</a>'+"\n";
						
						if(res_delete == '1')
						{
							template[i - (ch * 10) + ch] += '        | <a href="#" id="delete_comment_'+res_id+'" onclick="comments.Delete_Comment(\''+res_id+'\', this.id);return false;">Delete</a>'+"\n";
						}
					template[i - (ch * 10) + ch] += '</div>'+"\n";
				}
				
				template[i - (ch * 10) + ch] += '</div>'+"\n";
				
				ch++;
			}
			
			var data = '';
			for(var i = 1 ; i < template.length; i++)
			{
				data += template[i];
			}
			
			var go = 0;
			var i = 0;
			while(go != 1)
			{
				try
				{
					if(i+1 == page)
						vprofile.$('change_page_'+i).style.color = '#555';
					else
						vprofile.$('change_page_'+i).style.color = '#01a1ce';
				}
				catch(e)
				{
					go = 1;
				}
				
				i++;
			}
			
			vprofile.$('CommentsArea').innerHTML = data;
		}
		else if(res_s == 'e4')
			alert('There are no comments on this page.');
		else
			alert('There was an error. Error Code: '+res_s);
		
		try
		{
			vprofile.$('commentsScroll').scrollTop = 0;
			vprofile.$('userComment').focus();
		}
		catch(e){}
		
		xmlhttp.End_Progress();
	},
	
	No_Go : function()
	{
		alert('Page refresh needed.');
	},
	
	post_anyway : false
};

var cprofile = {
	setup : function()
	{
		this.$('cprofile_tabbar').style.display = '';
		
		for(var i = 0 ; i < this.tabs.length ; i++)
		{
			if(GET('album') == 'true')
			{
				if(i != 5)
					this.$(this.tabs[i]).style.display = 'none';
				else
					this.$(this.tabs[i]).style.display = '';
				
				if(this.set == 0)
				{
					if(GET('artist_opt') != null)
						this.$('updateForum').action = '?artist_opt='+GET('artist_opt')+'&album=true';
					else if(GET('label_opt') != null)
						this.$('updateForum').action = '?label_opt='+GET('label_opt')+'&album=true';
					else if(GET('admin_opt') != null)
						this.$('updateForum').action = '?admin_opt='+GET('admin_opt')+'&album=true';
					else if(GET('staff_opt') != null)
						this.$('updateForum').action = '?staff_opt='+GET('staff_opt')+'&album=true';
					else if(GET('fan_opt') != null)
						this.$('updateForum').action = '?fan_opt='+GET('fan_opt')+'&album=true';
					
					this.$('updateProfileB').value = 'Modify Albums';
						
					this.set = 1;
				}
				
				continue;
			}
			
			if(i != 0)
			{
				this.$(this.tabs[i]).style.display = 'none';
			}
			else
			{
			//	loc = ''+window.location;
			//	loc = loc.replace('#_'+this.tabs[i], '');
			//	window.location = loc + '#_' + this.tabs[i];
			//	this.act = i;
			}
		}
		
		//this.$('cprofile_ava2').style.display = '';
	},
	
	changeTab : function(index)
	{
		if(index == 5 || index == 6)
		{
			var opt = GET('artist_opt') != null ? 'artist_opt' : GET('label_opt') != null ? 'label_opt' : null;
			
			if(opt != null)
				window.location = 'dashboard.php?' + opt + (index == 5 ? '=profile_sales' : '=profile_albums');
			
			return false;
		}
		
		for(var i = 0 ; i < this.tabs.length ; i++)
		{
			if(i == index)
			{
				this.$(this.tabs[i]).style.display = '';
				
			//	loc = ''+window.location;
			//	loc = loc.replace('#_'+this.tabs[this.act], '');
			//	window.location = loc + '#_' + this.tabs[i];
				
			//	this.act = i;
			
				if(i == 5)
				{
					if(GET('artist_opt') != null)
						this.$('updateForum').action = '?artist_opt='+GET('artist_opt')+'&album=true';
					else if(GET('label_opt') != null)
						this.$('updateForum').action = '?label_opt='+GET('label_opt')+'&album=true';
					else if(GET('admin_opt') != null)
						this.$('updateForum').action = '?admin_opt='+GET('admin_opt')+'&album=true';
					else if(GET('staff_opt') != null)
						this.$('updateForum').action = '?staff_opt='+GET('staff_opt')+'&album=true';
					else if(GET('fan_opt') != null)
						this.$('updateForum').action = '?fan_opt='+GET('fan_opt')+'&album=true';
				}
				/*else if(i == 6)
				{
					if(GET('artist_opt') != null){
						this.$('salseReportsF1').action = '?artist_opt='+GET('artist_opt')+'&report=true';
						this.$('salseReportsF2').action = '?artist_opt='+GET('artist_opt')+'&report=true';
						this.$('salseReportsF3').action = '?artist_opt='+GET('artist_opt')+'&report=true';}
					else if(GET('label_opt') != null){
						try{this.$('salseReportsF1').action = '?label_opt='+GET('label_opt')+'&report=true'}catch(e){};
						try{this.$('salseReportsF2').action = '?label_opt='+GET('label_opt')+'&report=true'}catch(e){};
						try{this.$('salseReportsF3').action = '?label_opt='+GET('label_opt')+'&report=true'}catch(e){};}
					else if(GET('admin_opt') != null){
						this.$('salseReportsF1').action = '?admin_opt='+GET('admin_opt')+'&report=true';
						this.$('salseReportsF2').action = '?admin_opt='+GET('admin_opt')+'&report=true';
						this.$('salseReportsF3').action = '?admin_opt='+GET('admin_opt')+'&report=true';}
					else if(GET('staff_opt') != null){
						this.$('salseReportsF1').action = '?staff_opt='+GET('staff_opt')+'&report=true';
						this.$('salseReportsF2').action = '?staff_opt='+GET('staff_opt')+'&report=true';
						this.$('salseReportsF3').action = '?staff_opt='+GET('staff_opt')+'&report=true';}
					else if(GET('fan_opt') != null){
						this.$('salseReportsF1').action = '?fan__opt='+GET('fan_opt')+'&report=true';
						this.$('salseReportsF2').action = '?fan__opt='+GET('fan_opt')+'&report=true';
						this.$('salseReportsF3').action = '?fan__opt='+GET('fan_opt')+'&report=true';}
				}*/
				else
				{
					if(GET('artist_opt') != null)
						this.$('updateForum').action = '?artist_opt='+GET('artist_opt');
					else if(GET('label_opt') != null)
						this.$('updateForum').action = '?label_opt='+GET('label_opt');
					else if(GET('admin_opt') != null)
						this.$('updateForum').action = '?admin_opt='+GET('admin_opt');
					else if(GET('staff_opt') != null)
						this.$('updateForum').action = '?staff_opt='+GET('staff_opt');
					else if(GET('fan_opt') != null)
						this.$('updateForum').action = '?fan_opt='+GET('fan_opt');
				}
				
				if(i == 0)
				{
					this.$('updateProfileB').value = 'Update Information';
					this.$('updateProfileB').style.display = '';
				}
				else if(i == 1)
				{
					this.$('updateProfileB').value = 'Update Contact';
					this.$('updateProfileB').style.display = '';
				}
				else if(i == 2)
				{
					this.$('updateProfileB').value = 'Update About';
					this.$('updateProfileB').style.display = '';
				}
				else if(i == 3)
				{
					this.$('updateProfileB').value = 'Update Projects';
					this.$('updateProfileB').style.display = '';
				}
				else if(i == 4)
				{
					this.$('updateProfileB').style.display = 'none';
				}
				else if(i == 5)
				{
					this.$('updateProfileB').value = 'Modify Albums';
					this.$('updateProfileB').style.display = '';
				}
				else if(i == 6)
				{
					this.$('updateProfileB').style.display = 'none';
				}
			}
			else
				this.$(this.tabs[i]).style.display = 'none';
		}
	},
	
	showVideo : function(id)
	{
		vidh = this.$('vidh_'+id+'_');
		vid1 = this.$('vid_'+id+'_');
		vid2 = this.$('vid_'+id+'__');

		if(vid2.style.display == 'none')
		{
		
			vid1.style.display = '';
			vid2.style.display = '';
		
			if(vidh.innerHTML == 'YouTube Video. Click to open.')
				vidh.innerHTML = 'YouTube Video. Click to close.';
			else if(vidh.innerHTML == 'MySpace Video. Click to open.')
				vidh.innerHMTL = 'MySpace Video. Click to view.';
		}
		else
		{
			vid1.style.display = 'none';
			vid2.style.display = 'none';
		
			if(vidh.innerHTML == 'YouTube Video. Click to close.')
				vidh.innerHTML = 'YouTube Video. Click to open.';
			else if(vidh.innerHTML == 'MySpace Video. Click to close.')
				vidh.innerHMTL = 'MySpace Video. Click to open.';
		}
	},
	
	note : function(id, dis)
	{
		if(dis == 1)
			this.$(id).style.display = '';
		else
			this.$(id).style.display = 'none';
	},
	
	$ : function(i)
	{
		return document.getElementById(i);
	},
	
	tabs : ['cprofile_info', 'cprofile_coninfo', 'cprofile_aboutme', 'cprofile_projects', 'cprofile_privacy', 'cprofile_sales'],
	act : 0,
	set : 0
};

var vprofile = {
	setup : function()
	{
		for(var i = 0 ; i < this.fields.length ; i++)
		{
			this.$(this.fields[i]).style.cursor = 'pointer';
		}
	},
	
	enlarge : function(s, t)
	{
		if(this.no == 0)
		{
			for(var i = 0 ; i < this.field_rows.length - 1 ; i++)
				this.$(this.field_rows[i]).style.display = 'none';
			
			
			if(s != 'vprofile_r4_1')
			{
				this.$('vprofile_abttitle').innerHTML = t;
				this.$('vprofile_abt').innerHTML = this.$(s).innerHTML;
				this.$('vprofile_rb').style.display = '';
				this.$(s).innerHTML = '';
			}
			else
			{
				this.$('vprofile_comments').style.display = '';
				
				try
				{
					vprofile.$('commentsScroll').scrollTop = 0;
					vprofile.$('userComment').focus();
				}
				catch(e){}
			}
			
			this.sid = s;
		}
	},
	
	small : function()
	{
		if(this.no == 0)
		{
			for(var i = 0 ; i < this.field_rows.length - 1 ; i++)
				this.$(this.field_rows[i]).style.display = '';
			
			this.$('vprofile_abttitle').innerHTML = '';
			this.$('vprofile_rb').style.display = 'none';
			if(this.sid != 'vprofile_r4_1') this.$(this.sid).innerHTML = this.$('vprofile_abt').innerHTML;
			this.$('vprofile_abt').innerHTML = '';
			
			this.$('vprofile_comments').style.display = 'none';
		}
	},
	
	noGo : function(m)
	{
		if(m == 1)
			this.no = 1;
		else
			this.no = 0;
	},
	
	$ : function(i)
	{
		return document.getElementById(i);
	},
	
	fields : ['vprofile_r1_1', 'vprofile_r1_2', 'vprofile_r2_1', 'vprofile_r2_2', 'vprofile_r3_1', 'vprofile_r4_1'],
	field_rows : ['vprofile_r1', 'vprofile_r2', 'vprofile_r3', 'vprofile_r4', 'vprofile_rb'],
	no : 0,
	sid : ''
};

function validate_upc(e, idt)
{
	if(e.value.replace(/[0-9]/g, '') != '')
		return false;
	
	if(e.value.length != 11)
		return false;
	
	var upc_rand_bit = e.value;
	
	var upc_rand_buffer = '';
	
	for(var i = 0 ; i < upc_rand_bit.length ; i++) {
		upc_rand_buffer += upc_rand_bit[i];
	}
	
	var tmp = new Array();
	
	for(var i = 0 ; i < upc_rand_bit.length ; i++) {
		if ((i+1) % 2 == 0)
		{
			tmp[i] = parseInt(upc_rand_bit[i]);
		}
		else
		{
			tmp[i] = (parseInt(upc_rand_bit[i]) * 3);
		}
	}
	
	upc_rand_sum = 0;
	
	for(i = 0 ; i < tmp.length ; i++)
		upc_rand_sum += parseInt(tmp[i]);
	
	upc_check_digit = Math.ceil(upc_rand_sum / 10) * 10 - upc_rand_sum;
	
	cprofile.$('upc_checkd_'+idt).value = upc_check_digit;
}

function toggleLogin()
{
	if(document.getElementById('watLoginPopup').style.display == 'none')
	{
		document.getElementById('bodyM').style.marginTop = '91px';
		document.getElementById('watLoginPopup').style.display = '';
	}
	else
	{
		document.getElementById('bodyM').style.marginTop = '40px';
		document.getElementById('watLoginPopup').style.display = 'none';
	}
}