/* The following function creates an XMLHttpRequest object... */
function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

var http = createRequestObject(); 
//This is the name of the element whose innerHTML is changed.
var element_id;
var second_function = '';

//This is the generic ajax handler
function handle_functions(){
	if(http.readyState == 4){
		if(element_id != ''){
			var response = http.responseText;
			document.getElementById(element_id).innerHTML = response;
		}
		if(second_function != ''){
			do_this = second_function;
			second_function = '';
			eval(do_this + '()');
		}
	}
}

//This ajaxes in the neighborhood question posted from someone's channel
function neighborhood_question(){
	var f = document.neighborhoodQuestion;
	var qbody = escape(f.comment.value);
	var qsubject = escape(f.topic.value);
	var notify_me = f.notify_me.value;
	var everyone = f.everyone.value;
	
	http.open('get', '/society_process.php?action=neighborhood_post&body='+qbody+'&subject='+qsubject+'&everyone='+everyone+'&notify_me='+notify_me);
	element_id = 'neighborhoodQuestionForm';
	second_function = 'updateRecentPosts';
	http.onreadystatechange = handle_functions; 
	http.send(null);
}

//This function ajaxes in the action feed results
function af_ajax(x){
	http.open('get', '/society_process.php?action=action_feed&limit='+x);
	element_id = 'actionFeed';
	http.onreadystatechange = handle_functions;
	http.send(null);
} 

//This function ajaxes in the society feed results
function sf_ajax(category, zip, radius){
	http.open('get', '/society_process.php?action=sector_feed&category='+category+'&zip='+zip+'&radius='+radius);
	element_id = 'sectorFeed';
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function show_connection_search(x){
	http.open('get', '/society_process.php?action=connection_search_form&search_type=' + x);
	element_id = 'connection_search_div';
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function display_connection_search_results(zip, radius, name, user_type, order_by, page){
	var good = 'no';
	var end_string = '&user_type=' + user_type + '&order_by' + order_by + '&page=' + page;
	if(zip != "" && radius != ""){
		if(zip == '' || zip.length != 5 || radius == ''){
			alert('All Fields are Required');
		}
		else{
			http.open('get', '/society_process.php?action=display_connection_search_results&zip=' + zip + '&radius=' + radius + end_string);
			good = 'yes';
		}
	}
	else if(name != ""){
		if(name == ''){
			alert('All Fields are Required');
		}
		else{
			http.open('get', '/society_process.php?action=display_connection_search_results&name=' + name + end_string);
			good = 'yes';
		}
	}
	if(good == 'yes'){
		element_id = 'connectionContent';
		second_function = 'reset_submit_button';
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
	else{
		document.connection_search.this_button.disabled='';	
	}
}

function reset_submit_button(){
	document.connection_search.this_button.disabled='';	
}

function display_connection_list(user_id, user_type, order_by, page){
	var end_string = '&user_id=' + user_id + '&user_type=' + user_type + '&order_by' + order_by + '&page=' + page;
	http.open('get', '/society_process.php?action=display_connection_list' + end_string);
	element_id = 'connectionContent';
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function submit_connection_search(x){
	var zip='';
	var radius='';
	var name='';
	document.connection_search.this_button.disabled='disabled';
	
	if(x=='area'){
		zip = document.connection_search.zip.value;
		radius = document.connection_search.radius.value;
	}
	else if(x=='name'){
		name = document.connection_search.name_search.value;
	}
	display_connection_search_results(zip, radius, name, '', '', '');
}

function remove_connection(x){
	if(confirm('Are you sure that you want to remove this connection?')){
		http.open('get', '/society_process.php?action=remove_connection&friend_id=' + x);
		element_id = 'friend_link_' + x;
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}

function add_connection(x){
	if(confirm('Are you sure that you want to add this connection?')){
		http.open('get', '/society_process.php?action=add_connection&friend_id=' + x);
		element_id = 'friend_link_' + x;
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}

function confirm_connection(x){
	if(confirm('Are you sure that you want to add this connection?')){
		http.open('get', '/society_process.php?action=confirm_connection&friend_id=' + x);
		element_id = x;
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}

function deny_connection(x){
	if(confirm('Are you sure that you want to remove this connection?')){
		http.open('get', '/society_process.php?action=remove_connection&friend_id=' + x);
		element_id = x;
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}

function change_show_groups(show, order_by, page){
	http.open('get', '/society_process.php?action=change_show_groups&show=' + show + '&order_by=' + order_by + '&page=' + page);
	element_id = 'group_list';
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function join_group(x){
	if(confirm('Would you like to join this network?')){
		http.open('get', '/society_process.php?action=join_group&group_id=' + x);
		element_id = 'join_group_' + x;
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}

function join_private_group(x){
	if(confirm('This Network has been designated invite-only. Would you like to request to join anyway?')){
		http.open('get', '/society_process.php?action=join_group&group_id=' + x);
		element_id = 'join_group_' + x;
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}

function leave_group(x){
	if(confirm('Are you sure that you want to leave this network?')){
		http.open('get', '/society_process.php?action=leave_group&group_id=' + x);
		element_id = 'join_group_' + x;
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}	

function remove_group_member(group_id, user_id){
	if(confirm('Are you sure that you want to remove this member?')){
		http.open('get', '/society_process.php?action=remove_group_member&group_id=' + group_id + '&user_id=' + user_id);
		element_id = user_id;
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}

function remove_group_photo(x){
	http.open('get', '/society_process.php?action=remove_group_photo&group_id=' + x);
	element_id = 'group_photo';
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function accept_group_invitation(x){
	http.open('get', '/society_process.php?action=accept_group_invitation&group_id=' + x);
	element_id = 'accept_invite_' + x;
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function change_group_members(group_id, my_page, order_by, user_type){
	http.open('get', '/society_process.php?action=change_group_members&group_id=' + group_id + '&page=' + my_page + '&order_by=' + order_by + '&user_type=' + user_type);
	element_id = 'groupMain';
	http.onreadystatechange = handle_functions;
	http.send(null);	
}

function addEmailInvites(){
	var my_value = document.emInvites.emailInvite.value;
	document.getElementById('myInviteMessage').style.display = '';
	if(my_value.length > 0){
		http.open('get', '/society_process.php?action=add_email_invites&string=' + my_value);
		element_id = 'myInviteList';
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}

function add_to_invite_list(my_value){
	document.getElementById('myInviteMessage').style.display = '';
	http.open('get', '/society_process.php?action=add_to_invite_list&string=' + my_value);
	element_id = 'myInviteList';
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function accept_group_member_join(group_id, tcm_id){
	http.open('get', '/society_process.php?action=accept_group_member_join&group_id=' + group_id + '&tcm_id=' + tcm_id);
	element_id = tcm_id;
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function dec_group_member_join(group_id, tcm_id){
	http.open('get', '/society_process.php?action=decline_group_member_join&group_id=' + group_id + '&tcm_id=' + tcm_id);
	element_id = tcm_id;
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function accept_client_invite(rec_id){
	http.open('get', '/society_process.php?action=accept_client_invite&rec_id=' + rec_id);
	element_id = 'client_' + rec_id;
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function dec_client_invite(rec_id){
	http.open('get', '/society_process.php?action=decline_client_invite&rec_id=' + rec_id);
	element_id = 'client_' + rec_id;
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function accept_group_invite(group_id){
	http.open('get', '/society_process.php?action=accept_group_invite&group_id=' + group_id);
	element_id = group_id;
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function dec_group_invite(group_id){
	if(confirm('Are you sure that you want to decline your invitation to this network?')){
		http.open('get', '/society_process.php?action=leave_group&group_id=' + group_id);
		element_id = group_id;
		http.onreadystatechange = handle_functions;
		http.send(null);
	}
}	

function sendGroupInvites(group_id){
	http.open('get', '/society_process.php?action=send_group_invites&group_id=' + group_id);
	element_id = 'myInviteMessage';
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function updateRecentPosts(){
	http.open('get', '/society_process.php?action=update_recent_posts');
	element_id = 'recentPosts';
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function other_group_type(x){
	if(x == "Other"){
		document.getElementById('other_field_div').style.display='';
		document.tc_form.other_field.value='';
	}
	else{
		document.getElementById('other_field_div').style.display='none';	
	}
}

function change_member_type(member_id, member_type){
	http.open('get', '/society_process.php?action=change_member_type&mid=' + member_id + '&mt=' + member_type);
	element_id = "member_change_" + member_id;
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function update_preview_pane(){
	var pane = document.getElementById('preview_pane').contentWindow.document;
	var my_form = document.theme_changer;
	
	/* REMOVED : document.getElementById('detailsTextColorSample').style.backgroundColor = '#' + my_form.detailsTextColor.value; */
	//
	/* REMOVED : document.getElementById('descriptionTextColorSample').style.backgroundColor = '#' + my_form.descriptionTextColor.value; */
	//
	document.getElementById('moreInfoBackgroundColorSample').style.backgroundColor = '#' + my_form.moreInfoBackgroundColor.value;
	pane.getElementById('moreInfoBar').style.backgroundColor = '#' + my_form.moreInfoBackgroundColor.value;
	//updates background color
	pane.getElementById('container').style.backgroundColor = '#' + my_form.backgroundColor.value;
	document.getElementById('backgroundColorSample').style.backgroundColor = '#' + my_form.backgroundColor.value;
	//updates text color
	pane.body.style.color = '#' + my_form.textColor.value;
	document.getElementById('textColorSample').style.backgroundColor = '#' + my_form.textColor.value;
	//updates font
	pane.body.style.fontFamily = my_form.fontStyle.options[my_form.fontStyle.selectedIndex].value;
	document.getElementById('textSample').style.fontFamily = my_form.fontStyle.options[my_form.fontStyle.selectedIndex].value;
	//updates headline color
	var links = pane.getElementsByTagName("h1");
	for (var i=0;i<links.length;i++){
		links[i].style.color = '#' + my_form.headlineColor.value;
	}
	document.getElementById('headlineColorSample').style.backgroundColor = '#' + my_form.headlineColor.value;
	//updates link color
	var links = pane.getElementsByTagName("a");
	for (var i=0;i<links.length;i++){
		links[i].style.color = '#' + my_form.linkColor.value;
		if(!(my_form.navigationLinkUnderline.checked)){
			links[i].style.textDecoration = 'none';
		}
		else{
			links[i].style.textDecoration = '';
		}
	}
	document.getElementById('linkColorSample').style.backgroundColor = '#' + my_form.linkColor.value;
	//does headline bar
	document.getElementById('headlineBarColorSample').style.backgroundColor = '#' + my_form.headlineBarColor.value;
	pane.getElementById('vid_headline').style.backgroundColor = '#' + my_form.headlineBarColor.value;
	//does search bar
	document.getElementById('searchBarColorSample').style.backgroundColor = '#' + my_form.searchBarColor.value;
	pane.getElementById('searchBar').style.backgroundColor = '#' + my_form.searchBarColor.value;
	//does more info link color
	document.getElementById('moreInfoLinkColorSample').style.backgroundColor = '#' + my_form.moreInfoLinkColor.value;
	var links = pane.getElementById('moreInfoBar').getElementsByTagName("a");
	for (var i=0;i<links.length;i++){
		links[i].style.color = '#' + my_form.moreInfoLinkColor.value;
		if(!(my_form.moreInfoLinkUnderline.checked)){
			links[i].style.textDecoration = 'none';
		}
		else{
			links[i].style.textDecoration = '';
		}
	}
	//does info block color
	document.getElementById('informationBlockColorSample').style.backgroundColor = '#' + my_form.informationBlockColor.value;
	pane.getElementById('right_top_portion_body').style.backgroundColor = '#' + my_form.informationBlockColor.value;
	//does nav on state
	document.getElementById('navigationOnColorSample').style.backgroundColor = '#' + my_form.navigationOnColor.value;
	pane.getElementById('nav1').style.backgroundColor = '#' + my_form.navigationOnColor.value;
	//does nav off state
	document.getElementById('navigationOffColorSample').style.backgroundColor = '#' + my_form.navigationOffColor.value;
	pane.getElementById('nav2').style.backgroundColor = '#' + my_form.navigationOffColor.value;
	pane.getElementById('nav3').style.backgroundColor = '#' + my_form.navigationOffColor.value;
	pane.getElementById('nav4').style.backgroundColor = '#' + my_form.navigationOffColor.value;
	//does thumbnail on color
	document.getElementById('thumbnailOnColorSample').style.backgroundColor = '#' + my_form.thumbnailOnColor.value;
	pane.getElementById('thumb_on').style.borderColor = '#' + my_form.thumbnailOnColor.value;
	//does navigation text
	document.getElementById('navigationTextOnSample').style.backgroundColor = '#' + my_form.navigationTextOn.value;
	document.getElementById('navigationTextOffSample').style.backgroundColor = '#' + my_form.navigationTextOff.value;
	pane.getElementById('myNavOn').style.color = '#' + my_form.navigationTextOn.value;
	pane.getElementById('myNavOff1').style.color = '#' + my_form.navigationTextOff.value;
	pane.getElementById('myNavOff2').style.color = '#' + my_form.navigationTextOff.value;
}

function change_theme_selected(theme_id, enc_id){
	http.open('get', '/society_process.php?action=change_theme_selected&theme_id=' + theme_id + '&enc_id=' + enc_id);
	element_id = "";
	second_function ='window.location.reload()';
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function change_player_theme_selected(theme_id, enc_id, user_network){
	http.open('get', '/society_process.php?action=change_player_theme_selected&theme_id=' + theme_id + '&enc_id=' + enc_id + '&user_network=' + user_network);
	element_id = "";
	second_function = 'window.location.reload()';
	//alert(theme_id + enc_id);
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function include_exclude(video_id,network_id){
	http.open('get', '/society_process.php?action=exclusion&vid='+video_id+'&nid='+network_id);	
	element_id = 'vid_' + video_id;
	http.onreadystatechange = handle_functions;
	http.send(null);
}

function change_network_video_player(enc_id){
	http.open('get', '/society_process.php?action=change_network_video_player&encid=' + enc_id);
	element_id = "my_video_player";
	http.onreadystatechange = handle_functions;
	http.send(null);
}
