/*function doRequest_saveComponentOrder(http_request, user_id, user_table){
	hideSaveMsg();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				//makeRequest('controlpanel', user_id, user_table);
				
				document.getElementById('profile').innerHTML = http_request.responseText;
				init_all_itemvals();
				init_page();
			}else if (http_request.status == 404) {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not find profile.</div>';
				init_page();
			} else {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not save profile component order.</div>';
			}
		}
	}
	http_request.open('POST', 'components/profile_components.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "component_order=";
	var profile = document.getElementById('profile');
	var profile_components = getElementsByClassName('profile_component', 'div', profile);
	for(var i = 0; i < profile_components.length; i++){
		var id = profile_components[i].getAttribute('id');
		var temp = id.split("_"); // id is in the format  component_componentID
		params += temp[1]; // just use componentID
		if(i != (profile_components.length-1)) params += "|";
	}
	http_request.send(params);
	document.getElementById('profile').innerHTML = '<div class="loadingprofile"><img src="/profile/images/loading_components.gif" style="padding-right:5px;" alt=""/>Saving profile component order...</div>';
}*/
function doRequest_saveComponentOrder(http_request, userid){
	hideSaveMsg();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				//makeRequest('controlpanel', userid);
				if(http_request.responseText == 'false')
					alert('There was a problem saving the order of items in your profile');
				//document.getElementById('profile').innerHTML = http_request.responseText;
				//init_all_itemvals();
				//init_page();
			}else if (http_request.status == 404) {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not find profile.</div>';
				init_page();
			} else {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not save profile component order.</div>';
			}
		}
	}
	http_request.open('POST', 'components/profile_components.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "component_order=";
	var profile = document.getElementById('profile');
	var profile_components = getElementsByClassName('profile_component_wrapper', 'div', profile);
	for(var i = 0; i < profile_components.length; i++){
		var id = profile_components[i].getAttribute('id');
		var temp = id.split("_"); // id is in the format  component_componentID
		params += temp[1]; // just use componentID
		if(i != (profile_components.length-1)) params += "|";
	}
	http_request.send(params);
	//document.getElementById('profile').innerHTML = '<div class="loadingprofile"><img src="/profile/images/loading_components.gif" style="padding-right:5px;" alt=""/>Saving profile component order...</div>';
}
/*
function doRequest_saveTabsOrder(http_request, user_id, user_table){
	hideSaveTabsMsg();
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {				
				document.getElementById('tabarea').innerHTML = http_request.responseText;
				init_all_itemvals();
				init_page();
			}else if (http_request.status == 404) {
				document.getElementById('tabarea').innerHTML = '<div class="newtab">Could not load tabs</div>';
				init_page();
			} else {
				document.getElementById('tabarea').innerHTML = '<div class="newtab">Could not load tabs.</div>';
			}
		}
	}
	http_request.open('POST', 'components/tabs.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "tab_order=";
	var tabarea = document.getElementById('tabarea');
	var tabs = getElementsByClassName('t', 'div', tabarea);
	for(var i = 0; i < tabs.length; i++){
		var id = tabs[i].getAttribute('id');
		var temp = id.split("_"); // id is tab_[pageId]
		params += temp[1]; // just use componentID
		if(i != (tabs.length-1)) params += "|";
	}
	http_request.send(params);
	document.getElementById('tabarea').innerHTML = '<div class="newtab"><img src="/profile/images/loading_tabs.gif" style="padding-right:5px;" alt=""/>Saving page order...</div>';
}
*/
function doRequest_saveTabsOrder(http_request, userid){
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {				
				/*document.getElementById('tabarea').innerHTML = http_request.responseText;
				init_all_itemvals();
				init_page();*/
				if(http_request.responseText == 'false')
					alert('There was a problem saving the order of the pages in your profile');
					
			}else if (http_request.status == 404) {
				document.getElementById('tabarea').innerHTML = '<div class="newtab">Could not load tabs</div>';
				init_page();
			} else {
				document.getElementById('tabarea').innerHTML = '<div class="newtab">Could not load tabs.</div>';
			}
		}
	}
	http_request.open('POST', 'components/tabs.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "tab_order=";
	var tabarea = document.getElementById('tabarea');
	var tabs = getElementsByClassName('t', 'div', tabarea);
	for(var i = 0; i < tabs.length; i++){
		var id = tabs[i].getAttribute('id');
		var temp = id.split("_"); // id is tab_[pageId]
		params += temp[1]; // just use componentID
		if(i != (tabs.length-1)) params += "|";
	}
	http_request.send(params);
	//document.getElementById('tabarea').innerHTML = '<div class="newtab"><img src="/profile/images/loading_tabs.gif" style="padding-right:5px;" alt=""/>Saving page order...</div>';
}
function doRequest_newpage(http_request, userid){
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				document.getElementById('tabarea').innerHTML = http_request.responseText;
				
				makeRequest('controlpanel', null, null);
				makeRequest('profile', null, null);
				
				var page = document.getElementById('current_page').innerHTML = document.getElementById('new_page_id').innerHTML;
				document.getElementById('new_page_id').innerHTML = "";
				
				makeRequest(null, page, 'pagename');
				init_edit_pagename();
				
				
			}else if (http_request.status == 404) {
				document.getElementById('tabarea').innerHTML = '<div class="newtab">Could not load tabs</div>';
				//init_page();
			} else {
				document.getElementById('tabarea').innerHTML = '<div class="newtab">Could not load tabs.</div>';
			}
		}
	}
	http_request.open('POST', 'components/tabs.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "newpage=1";
	http_request.send(params);
	document.getElementById('tabarea').innerHTML = '<div class="newtab"><img src="/profile/images/loading_tabs.gif" style="padding-right:5px;" alt=""/>Creating new page...</div>';
}

function doRequest_profile(http_request, url, userid){
	//makeRequest('controlpanel', user_id, user_table);
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				document.getElementById('profile').innerHTML = http_request.responseText;
				init_all_itemvals();	
				init_page();
				TB_init();
				
			}else if (http_request.status == 404) {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not find profile.</div>';
				init_page();
			} else {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not load profile.</div>';
			}
		}
	}
	http_request.open('POST', 'components/profile_components.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "userid="+userid;
	http_request.send(null);
	document.getElementById('profile').innerHTML = '<div class="loadingprofile"><img src="/profile/images/loading_components.gif" style="padding-right:5px;" alt=""/>Loading profile components...</div>';
}

function doRequest_tabs(http_request, user_info, page){
	
	/*var temp = user_info.split("|");
	var user_id = temp[0];
	var user_table = temp[1];*/
	
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				document.getElementById('tabarea').innerHTML = http_request.responseText;
				
				document.getElementById('current_page').innerHTML = page;
				
				makeRequest('controlpanel', null, null);
				makeRequest('profile', null, null);
				makeRequest(null, page, 'pagename'); 
				
			}else if (http_request.status == 404) {
				document.getElementById('tabarea').innerHTML = '<div class="newtab">Could not load tabs</div>';
				//init_page();
			} else {
				document.getElementById('tabarea').innerHTML = '<div class="newtab">Could not load tabs.</div>';
			}
		}
	}
	http_request.open('POST', 'components/tabs.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "page="+page;
	http_request.send(params);
	document.getElementById('tabarea').innerHTML = '<div class="newtab"><img src="/profile/images/loading_tabs.gif" style="padding-right:5px;" alt=""/>Loading tabs...</div>';
}

function doRequest_moveComponent(http_request, component_id, np){
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				if(http_request.responseText == "true"){
					
					//document.getElementById('current_page').innerHTML = page;
					TB_remove();
					makeRequest('controlpanel', null, null);
					makeRequest('profile', null, null);
					//makeRequest(null, page, 'pagename'); 
				}else{
					alert('There was a problem moving this component');
				}
				
				
			}else if (http_request.status == 404) {
				alert('There was a problem moving this component because the save page could not be found');
			} else {
				alert('There was an unknown problem moving this component');
			}
		}
	}
	http_request.open('POST', 'components/move_component.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "component_id="+component_id+"&np="+np;
	http_request.send(params);
}

function doRequest_deletePage(http_request, page, np){
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				if(http_request.responseText == "true"){
					
					//if(np != "delete") document.getElementById('current_page').innerHTML = np;
					if(np != "delete") changePage(np);
					TB_remove();
					/*makeRequest('controlpanel', null, null);
					makeRequest('profile', null, null);
					makeRequest(null, page, 'pagename');*/ 
				}else{
					alert(http_request.responseText);
				}
				
				
			}else if (http_request.status == 404) {
				alert('There was a problem deleting this page because the delete page could not be found');
			} else {
				alert('There was an unknown problem deleting this page');
			}
		}
	}
	http_request.open('POST', 'components/delete_page.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	page = document.getElementById('current_page').innerHTML;
	
	var params = "delete="+page+"&np="+np;
	http_request.send(params);
}

function doRequest_pagename(http_request, user_info, page){
	
	/*var temp = user_info.split("|");
	var user_id = temp[0];
	var user_table = temp[1];*/
	
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				document.getElementById('page_name').innerHTML = http_request.responseText;
				
				init_edit_pagename();
				
			}else if (http_request.status == 404) {
				document.getElementById('page_name').innerHTML = 'Could not load page name';
				//init_page();
			} else {
				document.getElementById('page_name').innerHTML = 'Could not load page name';
			}
		}
	}
	http_request.open('POST', 'components/page_name.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "page="+page;
	http_request.send(params);
	document.getElementById('page_name').innerHTML = '<img src="/profile/images/loading_pagename.gif" style="padding-right:5px;" alt=""/>Loading page name...';
}

function doRequest_deleteProfileGroup(http_request, profile_group_id, user_info){
	
	/*var temp = user_info.split("|");
	var user_id = temp[0];
	var user_table = temp[1];*/
	
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				document.getElementById('profile').innerHTML = http_request.responseText;
				makeRequest('controlpanel', null, null);
				init_all_itemvals();	
				init_page();
				
			}else if (http_request.status == 404) {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not find profile.</div>';
				init_page();
			} else {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not load profile.</div>';
			}
		}
	}
	http_request.open('POST', 'components/profile_components.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "delete_component="+profile_group_id;
	http_request.send(params);
	document.getElementById('profile').innerHTML = '<div class="loadingprofile"><img src="/profile/images/indicator.gif" style="padding-right:5px;" alt=""/>Deleting profile component...</div>';
}

function doRequest_addComponent(http_request, category, component){
	
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				document.getElementById('profile').innerHTML = http_request.responseText;
				makeRequest('controlpanel', null, null);
				init_all_itemvals();	
				init_page();
				
			}else if (http_request.status == 404) {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not find profile.</div>';
				init_page();
			} else {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not load profile.</div>';
			}
		}
	}
	http_request.open('POST', 'components/profile_components.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "add_component="+component+"&category="+category;
	http_request.send(params);
	document.getElementById('profile').innerHTML = '<div class="loadingprofile"><img src="/profile/images/loading_components.gif" style="padding-right:5px;" alt=""/>Creating new component...</div>';
}

function doRequest_controlpanel(http_request, url, userid){
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				document.getElementById('visible').innerHTML = http_request.responseText;
				//init_all_itemvals();
				init_cp_editinline();
				init_page();
				TB_init();
				
			}else if (http_request.status == 404) {
				document.getElementById('visible').innerHTML = '<div class="loadingcontrolpanel">Could not find profile.</div>';
				init_page();
			} else {
				document.getElementById('visible').innerHTML = '<div class="loadingcontrolpanel">Could not load profile.</div>';
			}
		}
	}
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "userid="+userid;
	http_request.send(null);
	document.getElementById('visible').innerHTML = '<div class="loadingcontrolpanel"><img src="/profile/images/loading_tabs.gif" style="padding-right:5px;" alt=""/>Loading control panel options...</div>';
}

function doRequest_changeVisible(http_request, which, visible){
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				
				/*var temp = which.split("|");
				user_id = temp[1];
				user_table = temp[2];*/
				makeRequest('controlpanel', null, null);
				
				document.getElementById('profile').innerHTML = http_request.responseText;
				init_all_itemvals();
				init_page();
			}else if (http_request.status == 404) {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not find profile.</div>';
				init_page();
			} else {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not load profile.</div>';
			}
		}
	}
	http_request.open('POST', 'components/profile_components.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "change_visible=1&visible="+visible+"&which="+which;
	http_request.send(params);
	document.getElementById('profile').innerHTML = '<div class="loadingprofile"><img src="/profile/images/loading_components.gif" style="padding-right:5px;" alt=""/>Loading profile components...</div>';
}

function doRequest_changeColumns(http_request, which, columns){
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				//changeColumnDisplay(which, columns);
				/*var temp = which.split("|");
				user_id = temp[1];
				user_table = temp[2];*/
				makeRequest('controlpanel', null, null);
				
				document.getElementById('profile').innerHTML = http_request.responseText;
				init_all_itemvals();
				init_page();
			}else if (http_request.status == 404) {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not find profile</div>';
				init_page();
			} else {
				document.getElementById('profile').innerHTML = '<div class="loadingprofile">Could not load profile.</div>';
			}
		}
	}
	http_request.open('POST', 'components/profile_components.php', true);
	http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	
	var params = "changeColumns="+columns+"&which="+which;
	http_request.send(params);
	document.getElementById('profile').innerHTML = '<div class="loadingprofile"><img src="/profile/images/loading_components.gif" style="padding-right:5px;" alt=""/>Loading profile components...</div>';
}

function init_cp_editinline(){
	var idstring = document.getElementById('cp_profile_group_ids').innerHTML;
	var profile_group_ids = idstring.split("|");
	for(var i = 0; i < profile_group_ids.length; i++){
		new Ajax.InPlaceEditor($('edit_name_'+profile_group_ids[i]), 'components/edit_group_name.php', {
		  okText: 'save', 
		  okName: 'submit_name_'+profile_group_ids[i],
		  submitOnBlur: false, 
		  formId: 'form_edit_name'+profile_group_ids[i],
		  savingClassName: '',
		  loadingClassName: '',
		  formClassName: '',
		  highlightcolor: '',
		  highlightendcolor: '',
		  onComplete: function(){
				/*id = document.getElementById('profile_group_name_'+profile_group_ids[i]);
				editId = document.getElementById('edit_name_'+profile_group_ids[i]);
				id.innerHTML = editId.innerHTML;*/
				makeRequest('profile');
		  }
		});
	}
}

function changePage(page){
	if(page == null) page = document.getElementById('current_page').innerHTML;
	makeRequest(null, page, 'tabs');
}

/*function changeColumnDisplay(which, columns){
	var temp = which.split("|");
	var component_id = temp[0];
	var user_id = temp[1];
	var user_table = temp[2];

	
	var col1 = document.getElementById('col1_'+component_id);
	var col2 = document.getElementById('col2_'+component_id);
	var col3 = document.getElementById('col3_'+component_id);
	
	if(columns == '1'){
		col1.innerHTML = '<img src="images/one_column_selected.gif" alt="One Column On" />';
		col2.innerHTML = '<a href="#" onclick="javascript:makeRequest(\''+component_id+'|'+user_id+'|'+user_table+'\', \'2\', \'changeColumns\')"><img src="images/two_column_unselected.gif" alt="Two Column Off" /></a>';
		col3.innerHTML = '<a href="#" onclick="javascript:makeRequest(\''+component_id+'|'+user_id+'|'+user_table+'\', \'3\', \'changeColumns\')"><img src="images/three_column_unselected.gif" alt="Three Column Off" /></a>';
	}else if(columns == '2'){
		col1.innerHTML = '<a href="#" onclick="javascript:makeRequest(\''+component_id+'|'+user_id+'|'+user_table+'\', \'1\', \'changeColumns\')"><img src="images/one_column_unselected.gif" alt="One Column Off" /></a>';
		col2.innerHTML = '<img src="images/two_column_selected.gif" alt="Two Column On" />';
		col3.innerHTML = '<a href="#" onclick="javascript:makeRequest(\''+component_id+'|'+user_id+'|'+user_table+'\', \'3\', \'changeColumns\')"><img src="images/three_column_unselected.gif" alt="Three Column Off" /></a>';
	}else if(columns == '3'){
		col1.innerHTML = '<a href="#" onclick="javascript:makeRequest(\''+component_id+'|'+user_id+'|'+user_table+'\', \'1\', \'changeColumns\')"><img src="images/one_column_unselected.gif" alt="One Column Off" /></a>';
		col2.innerHTML = '<a href="#" onclick="javascript:makeRequest(\''+component_id+'|'+user_id+'|'+user_table+'\', \'2\', \'changeColumns\')"><img src="images/two_column_unselected.gif" alt="Two Column Off" /></a>';
		col3.innerHTML = '<img src="images/three_column_selected.gif" alt="Three Column On" />';
	}
}*/