
// ---------------------------------------------------------------
//	Make the various Ajax calls

$(document).ready(function() {
     
    // -----------------------
    // 	Submit Registration
    $('#submit_reg').submit(function() {
    	// Current DIV name
    	var this_div_name = "show_register";
    	// -----
        $('#loading').show();
	// -----
        // Get & organize data
        var fields = new Array('action','username','password','repeat_password','email','first_name','last_name','location');
        var send_data = get_fields(fields);
	// alert(send_data);
    	// -----
    	$.post("functions/register.php", send_data, function(theResponse) {
    		complete_task(theResponse,this_div_name);
    	});
        return false;
    });
    
    // -----------------------
    // 	Welcome Registration
    $('#welcome_reg_submit').click(function () {
    	// Current DIV name
    	var this_div_name = "show_register";
    	// -----
        $('#loading').show();
	// -----
        // Get & organize data
        var fields = new Array('action','username','password','repeat_password','email','first_name','last_name','location');
        var send_data = get_fields(fields);
	// alert(send_data);
    	// -----
    	$.post("functions/welcome_reg.php", send_data, function(theResponse) {
    		complete_task(theResponse,this_div_name);
    	});
        return false;
    });
    
    // -----------------------
    // 	Submit Login
    $('#submit_login').click(function () {
    	// Current DIV name
    	var this_div_name = "show_login";
    	// -----
        $('#loading').show();
	// -----
        // Get & organize data
        var fields = new Array('login_user','login_password');
        var send_data = get_fields(fields);
	// alert(send_data);
    	// -----
    	$.post("functions/login.php", send_data, function(theResponse) {
    		complete_task(theResponse,this_div_name);
    	});
        return false;
    });
    
    // -----------------------
    // 	Submit Lost Password
    $('#submit_lost_pass').click(function () {
    	// Current DIV name
    	var this_div_name = "show_lostpass";
    	// -----
        $('#loading').show();
	// -----
        // Get & organize data
        var fields = new Array('lp_user');
        var send_data = get_fields(fields);
	// alert(send_data);
    	// -----
    	$.post("functions/lost_pass.php", send_data, function(theResponse) {
    		complete_task(theResponse,this_div_name);
    	});
        return false;
    });

});



// -----------------------
// 	Upvote/Downvote
function vote(id,type) {
	$('#loading').show();
	// -----
	// Get & organize data
	// type = 1 (upvote) 2 (downvote)
	var send_data = "id=" + id + "&type=" + type;
	// -----
	$.post("functions/upvote.php", send_data, function(theResponse) {
      		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			$('#score' + id).html(returned['1']);
			if (type == "1") {
				$('#up' + id).addClass('upvoted');
				$('#down' + id).removeClass('downvoted');
			} else {
				$('#up' + id).removeClass('upvoted');
				$('#down' + id).addClass('downvoted');
			}
		} else {
			handle_error(returned['1']);
		}
		$('#loading').hide();
	});
	return false;
};


function addToFeed(id,type,remove) {
	send_data = type + "=" + id;
	if (remove == '1') {
		send_data += "&remove=1";
	}
    	$.post("functions/feed.php", send_data, function(theResponse) {
    		var returned = theResponse.split('+++');
    		if (returned['0'] == '1') {
    			$('#add_to_group').html(returned['1']);
    		} else {
    			handle_error(returned['1']);
    		}
    	});
}


// ---------------------------------------------------------------
//	Group user functions

function approveGroupUser(status,group_id,username) {
	send_data = "status=" + status + "&id=" + group_id + "&username=" + username;
    	$.post("functions/group_approve_users.php", send_data, function(theResponse) {
    		var returned = theResponse.split('+++');
    		if (returned['0'] == '1') {
    			divname = "user" + username;
    			if (status == '1') {
    				$('#' + divname).css('background-color','#FFFDCC');
    			} else {
    				$('#' + divname).fadeOut('200');
    			}
    		} else {
    			handle_error(returned['1']);
    		}
    	});
}


// ---------------------------------------------------------------
//	Handles ajax errors
function handle_error(error) {
	document.getElementById('error_put').innerHTML = error;
	$("#error_show").show('fast');
	setTimeout("close_error()",3000);
}

function close_error() {
	$("#error_show").hide('fast');
}

function show_div(id,closediv) {
	if (closediv) {
		divs = closediv.split(',');
		length = divs.length;
		for (i=0;i<=length;i++) {
			$('#' + divs[i]).hide();
		}
	}
	$("#" + id).show('fast');
}

function hide_div(id) {
	$("#" + id).hide('fast');
}


// ---------------------------------------------------------------
//	Compile fields into a URL-encoded list.

function get_fields(fields) {
	var data = "";
	var total = fields.length;
	for(i=0; i<total; i++) {
		value = $('#' + fields[i]).val();
		data += "&";
		data += fields[i];
		data += "=";
		data += value;
		$('#' + fields[i]).removeClass('errorHightlight');
		var info_div = fields[i] + "_info";
		if (document.getElementById(info_div)) {
			document.getElementById(info_div).innerHTML = "";
		}
	}
	data.substr(1);
	return data;
}

// ---------------------------------------------------------------
//	Complete the task.

function complete_task(theResponse,original_div_name) {
      	$('#loading').hide();
      	var returned = theResponse.split('+++');
      	
      	// alert(original_div_name);
      	
      	// Success!
      	// Format: 1+++message
      	if (returned['0'] == "1") {
      		if (returned['1'] == "reload") {
      			window.location.reload();
      		}
      		else if (returned['1'] == "redirect") {
      			window.location = returned['2'];
      		}
      		else {
  			$('#' + original_div_name).html(returned['1']);
      			$('#' + original_div_name).show('fast');
  		}
      	}
      	// Failed
      	// Format: 0+++field_name--message||field_name--message
      	else {
      		var allIssues = '';
       		var issues = returned['1'].split('||');
      		var total = issues.length;
      		for(i=0; i<total; i++) {
      			var this_issue = issues[i].split('--');
      			if (this_issue['1'] && this_issue['1'] != undefined) {
	      			allIssues += '<li>' + this_issue['1'] + '</li>';
	      			// var div1 = this_issue['0'] + "_info";
	      			// $('#' + this_issue['0']).addClass('errorHightlight');
	      			// $('#' + div1).show();
	      			// document.getElementById(div1).innerHTML = this_issue['1'];
      			}
      		}
      		final_issues = '<ul>' + allIssues + '</ul>';
      		handle_error(final_issues);
        }
}




// ---------------------------------------------------------------
//	Join a Group (ajax feature)

function joinGroup(id) {
	var send_data = "ajax=1&id=" + id;
	// -----
	$.get("functions/group_join.php", send_data, function(theResponse) {
      		var returned = theResponse.split('+++');
		if (returned['0'] == "1") {
			theDiv = "group" + id;
			$('#' + theDiv).css('color','green');
			$('#' + theDiv).fadeTo('fast', 0.3);
		} else {
			handle_error(returned['1']);
		}
		$('#loading').hide();
	});
	return false;
}
