////////////////////////////////
// Functions for user profile //
////////////////////////////////
the_key = "fjJ984jdkl49DJ22sdDFJejs39sk3StK";
inbox_checked = 0;

function check_friend_search() {
	if (document.friend_search_form.compose_to.value=="") {
		for (a=0; a<id_array.length; a++) {
			$("#friend-"+id_array[a]).css('display','block');
		}
	}	
}

function update_cc(current_year) {
	var answer = confirm("Are you sure you want update your student's credit card info? Clicking OK will erase the current information.");
	if (answer) {
		$('#cc_num_container').html('<input type="text" class="form-text-input" name="cc_num" maxlength="16" value="">');
		$('#cc_csv_container').html('<input type="text" class="form-text-input" name="cc_csv" maxlength="3" value="">');
	
		month_select = '<select name="cc_month">';
		for (a=1; a<13; a++) {
			the_month = String(a);
			if (the_month.length==1) {
				the_month = "0" + the_month;
			} else {
				the_month = the_month;
			}
			month_select += '<option value="'+the_month+'">'+the_month+'</opnion>';
		}
		month_select += '</select>';
	
		year_select = '<select name="cc_year">';
		current_year = Number(current_year);
		for (a=current_year; a<(current_year+12); a++) {
			the_year = String(a);
			if (the_year.length==1) {
				the_year = "0" + the_year;
			} else {
				the_year = the_year;
			}
			year_select += '<option value="'+the_year+'">'+the_year+'</opnion>';
		}
		year_select += '</select><input type="hidden" name="new_payment" value="yes">';
	
		$('#cc_exp_container').html(month_select + year_select);
		$('#update-link').hide();
	}
}

function status_bar(ajax_url,user_id,task,change_to) {
	switch(task) {
		case "remove_friend1":
			var answer = confirm("Are you sure you want to remove this friend?");
			if (answer) {
				process = true;
			} else {
				process = false;
			}
		break;
		default:
			process = true;
		break;
	}
	if (process) {
		$.ajax({
		   type: "POST",
		   url: ajax_url,
		   data: "task="+task+"&the_key="+the_key+"&user_id="+user_id+"&change_to="+change_to,
		   dataType: "json",
		   success: function(data){
				switch(data.status) {
					case "success":
						switch(task) {
							case "request_friend":
								$("#request-friend").html("Friend request sent!");
							break;
							case "remove_friend1":
								$("#friend-"+user_id).remove();
							break;
							case "load_status":
								switch(data.online) {
									case "1":
										the_text = "visible";
										visible_text = "invisible";
										change_to = "2";
									break;
									case "2":
										the_text = "invisible";
										visible_text = "visible";
										change_to = "1";
									break;
								}
								$("#online-status").html("You are <span class=\""+visible_text+"\">"+the_text+"</span> to students. <a href=\"javascript:void(0);\" onclick=\"status_bar('"+ajax_url+"','"+user_id+"','change_status','"+change_to+"')\">Become "+visible_text+".</a>");
							break;
							case "remove_friend":
								$("#request-friend").html("<a href=\"javascript:void(0);\" onclick=\"status_bar('"+ajax_url+"','"+user_id+"','request_friend','')\">Request to be friends</a>");
							break;
							case "make_friend":
								$("#the-request").html("You are now friends!");
							break;
							case "change_status":
								status_bar(ajax_url,user_id,'load_status','');
							break;
							case "suggest_session":
								$("#session-start-"+user_id).html("done!");
							break;
							case "start_session":
								alert(data.debug);
							break;
							case "cancel_session":
								alert('Your session has been cancelled!');
							break;
							case "check_notifications":
								if (data.users.length>0) {
									for (a=0; a<data.users.length; a++) {
										$.growl.settings.displayTimeout = 7200;
										$.growl.settings.dockCss.width = '225px';
										$.growl.settings.noticeCss = {
										    opacity: '100'
										  };
										$.growl.settings.noticeTemplate = "<div style=\"background-color:#F3F4CD;border:5px #D0CC2E solid;color:#323940;padding:10px;font-size:14px;text-align:center\"><span style=\"font-weight:bold;font-size:16px\">%title%</span><br /><br />%message%<br /><p style=\"text-align:center\"><a target=\"_blank\" href=\""+data.users[a].base_url+"session/join/"+data.users[a].roomid+"\" rel=\"close\">Start</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:void(0);\" rel=\"close\" onclick=\"status_bar('"+ajax_url+"','"+user_id+"','cancel_session','"+data.users[a].session_id+"')\">Reject</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:void(0);\" rel=\"close\">Close</a></p></div>";
										var msg = "<strong>" + data.users[a].first_name + " " + data.users[a].last_name + "</strong> wants to start a tutoring session with you.";
										var title = 'Attention!';
										$.growl(title, msg);
									}
								}
							break;
						}
					break;
				}
		   }
		 });
	}
}

function check_compose() {
	if (document.compose_form.compose_to.value=="") {
		document.compose_form.to_id.value = "0";	
	}
}

function add_education_row(ajax_url) {
	// Update total count
	new_total = parseInt(document.certifications_form.total_educations.value) + 1
	document.certifications_form.total_educations.value = new_total;
	
	// Get degree html
	the_data = "task=degrees&the_key="+the_key;
	$.ajax({
	   type: "POST",
	   url: ajax_url,
	   data: the_data,
	   dataType: "json",
	   success: function(data){
			// Create new row
			new_row = '<tr id="education' + new_total + '" class="new-entry"><td><select name="degree' + new_total + '" class="form-select" onchange="degree_change(' + new_total + ');">' + data.the_html + '</select></td><td><input class="form-text-input" type="text" name="other_degree' + new_total + '" disabled="disabled"></td><td><input type="text" class="form-text-input" name="institution' + new_total + '"></td><td><input class="form-text-input" type="text" name="major' + new_total + '"></td><td><input type="text" class="form-text-input" name="minor' + new_total + '"></td><td><input class="form-text-input year-text-input" type="text" name="year' + new_total + '" maxlength="4" size="4"></td><td><input type="text" name="gpa' + new_total + '" class="form-text-input year-text-input" size="4" maxlength="4"></td></tr>';
			$("#education"+(new_total-1)).after(new_row);
	   }
	 });
}

function degree_change(row_num) {
	if (document.certifications_form["degree"+row_num].value=="10") {
		document.certifications_form["other_degree"+row_num].disabled = false;
	} else {
		document.certifications_form["other_degree"+row_num].disabled = true;
		document.certifications_form["other_degree"+row_num].value = "";
	}
}

function update_inbox_checked(element_name) {
	if (document.inbox_form[element_name].checked) {
		inbox_checked ++;
	} else {
		inbox_checked --;
	}
}

function confirm_delete(thing,message) {
	var message = (message == null) ? "" : message;
	if (message=="") {
		answer = confirm("Are you sure you want to delete this " + thing + "?");
	} else {
		answer = confirm(message);
	}
	if (answer) {
		return true;
	} else {
		return false;
	}
}

function check_comment_post() {
	comment_text = document.comment_post_form.comment_text.value;
	switch (comment_text) {
		case "Write a comment...":
			document.comment_post_form.comment_text.value = "";
		break;
		case "":
			document.comment_post_form.comment_text.value = "Write a comment...";
		break;
	}
}

function check_text(form,element,message) {
	if (document[form][element].value=="") {
		document[form][element].value = message;
	}
}

function message_delete(type,message_id,ajax_url,user_id) {
	document.inbox_form.inbox_select.value = "none";
	inbox("select",ajax_url,user_id);
	document.inbox_form["checkbox"+message_id].checked = true;
	inbox_checked++;
	switch(type) {
		case "inbox":
			inbox("delete",ajax_url,user_id);
		break;
		case "sent":
			inbox("delete_sent",ajax_url,user_id);
		break;
	}
}

function inbox(task,ajax_url,user_id) {
	process = true;
	the_data = "task="+task+"&the_key="+the_key+"&user_id="+user_id;
	switch(task) {
		case "compose":
			// VdP - In order to make the compose button in the inbox section to work we allow
			//       document.compose_form.to_id.value=="0" and we send the compose_to field.
			//       The receiving ajax will test the to_id and use compose_to instead.
//			if (document.compose_form.to_id.value=="0" || document.compose_form.compose_subject.value=="" || document.compose_form.compose_message.value=="") {			
			if (document.compose_form.compose_to.value=="" || document.compose_form.compose_subject.value=="" || document.compose_form.compose_message.value=="") {
				process = false;
				alert('Please fill in all fields.');
			} else {
				the_data += "&compose_to=" + document.compose_form.compose_to.value + "&to=" + document.compose_form.to_id.value + "&subject=" + document.compose_form.compose_subject.value + "&message=" + document.compose_form.compose_message.value;
			}
		break;
		case "select":
			process = false;
			switch(document.inbox_form.inbox_select.value) {
				case "all":
					inbox_checked = 0;
					for (a=0; a<document.inbox_form.message_total.value; a++) {
						document.inbox_form["checkbox"+a].checked = true;
						inbox_checked++;
					}
				break;
				case "0":
					inbox_checked = 0;
					for (a=0; a<document.inbox_form.message_total.value; a++) {
						if (document.inbox_form["read"+a].value=="0") {
							if (document.inbox_form["checkbox"+a].checked==false) {
								inbox_checked++;
								document.inbox_form["checkbox"+a].checked = true;
							}
						} else {
							document.inbox_form["checkbox"+a].checked = false;
						}
					}
				break;
				case "1":
					inbox_checked = 0;
					for (a=0; a<document.inbox_form.message_total.value; a++) {
						if (document.inbox_form["read"+a].value=="1") {
							if (document.inbox_form["checkbox"+a].checked==false) {
								inbox_checked++;
								document.inbox_form["checkbox"+a].checked = true;
							}
						} else {
							document.inbox_form["checkbox"+a].checked = false;
						}
					}
				break;
				case "none":
					for (a=0; a<document.inbox_form.message_total.value; a++) {
						document.inbox_form["checkbox"+a].checked = false;
					}
					inbox_checked = 0;
				break;
			}
		break;
		case "delete_sent":
		case "delete":
			if (inbox_checked>0) {
				choice = confirm_delete('','Are you sure you want to delete these messages?');
				if (!choice) {
					process = false;
					break;
				} else {
					total = document.inbox_form.message_total.value;
					mark_total = 0;
					mark_array = Array();
					for (a=0; a<total; a++) {
						if (document.inbox_form["checkbox"+a].checked) {
							mark_array.push(document.inbox_form["id"+a].value);
							mark_total++;
						}
					}
					the_data += "&total="+mark_total;
					for (a=0; a<mark_array.length; a++) {
						the_data += "&id"+a+"="+mark_array[a];
					}
				}
			}
		break;
		case "mark_unread":
			if (inbox_checked>0) {
				total = document.inbox_form.message_total.value;
				mark_total = 0;
				mark_array = Array();
				for (a=0; a<total; a++) {
					if (document.inbox_form["checkbox"+a].checked) {
						if (document.inbox_form["read"+a].value=="1") {
							mark_array.push(document.inbox_form["id"+a].value);
							mark_total++;
						}
					}
				}
				the_data += "&total="+mark_total;
				for (a=0; a<mark_array.length; a++) {
					the_data += "&id"+a+"="+mark_array[a];
				}
			}
		break;
		case "mark_read":
			if (inbox_checked>0) {
				total = document.inbox_form.message_total.value;
				mark_total = 0;
				mark_array = Array();
				for (a=0; a<total; a++) {
					if (document.inbox_form["checkbox"+a].checked) {
						if (document.inbox_form["read"+a].value=="0") {
							mark_array.push(document.inbox_form["id"+a].value);
							mark_total++;
						}
					}
				}
				the_data += "&total="+mark_total;
				for (a=0; a<mark_array.length; a++) {
					the_data += "&id"+a+"="+mark_array[a];
				}
			}
		break;
	}
	
	if (process) {
		$.ajax({
		   type: "POST",
		   url: ajax_url,
		   data: the_data,
		   dataType: "json",
		   success: function(data){		   	
				switch(task) {
					case "delete":
					case "mark_unread":
					case "mark_read":
						switch(data.status) {
							case "success":
								inbox("load",ajax_url,user_id);
							break;
							case "fail":
								alert('FAIL:\n'+data.status);
							break;
						}
					break;
					case "delete_sent":
						switch(data.status) {
							case "success":
								inbox("load_sent",ajax_url,user_id);
							break;
							case "fail":
								alert('FAIL:\n'+data.status);
							break;
						}
					break;
					case "total_unread":
						switch(data.status) {
							case "success":
								switch(data.total_unread) {
									case 0:
										$("#inbox-link").html('Inbox');
										$("#inbox-link").css({"font-weight":"normal","background-color":"#CFCB2A"});
									break;
									default:
										$("#inbox-link").html('Inbox ('+data.total_unread+')');
										$("#inbox-link").css({"font-weight":"bold","background-color":"#F3F4CD"});
									break;
								}
							break;
							case "fail":
								alert('FAIL:\n'+data.status);
							break;
						}
					break;
					case "compose":					
						switch(data.status) {
							case "success":
								ajax_index = ajax_url.indexOf('ajax');
								base_url = ajax_url.substring(0,ajax_index)
								redirect_url = base_url + "message/" + data.message_id + "#" + data.thread_id;
								alert('Your message has been sent!');
								document.location.href = redirect_url;
								/*
								document.compose_form.to_id.value = "0";
								document.compose_form.compose_subject.value = "";
								document.compose_form.compose_message.value = "";
								document.compose_form.compose_to.value = "";
								*/	
							break;
							case "fail":
								alert(data.the_html);
							break;
						}
					break;
					default:
						switch(data.status) {
							case "success":
								$("#inbox-container").html(data.the_html);
								switch(data.total_unread) {
									case 0:
										$("#inbox-link").html('Inbox');
										$("#inbox-link").css({"font-weight":"normal","background-color":"#CFCB2A"});
									break;
									default:
										$("#inbox-link").html('Inbox ('+data.total_unread+')');
										$("#inbox-link").css({"font-weight":"bold","background-color":"#F3F4CD"});
									break;
								}	
							break;
							case "fail":
								alert('FAIL:\n'+data.the_html);
							break;
						}
					break;
				}
		   }
		 });
	}
}

function send_reply(ajax_url) {
	reply_text = document.reply_form.reply.value;
	thread_id = document.reply_form.thread_id.value;
	rec_id = document.reply_form.rec_id.value;
	the_data = "task=add_message&the_key="+the_key+"&rec="+rec_id+"&thread="+thread_id+"&text="+reply_text;
	if (reply_text!="") {
		$.ajax({
		   type: "POST",
		   url: ajax_url,
		   data: the_data,
		   dataType: "json",
		   success: function(data){
				switch(data.status) {
					case "success":
				  		$("#replies_container").prepend(data.the_html);
						$("#message-"+data.message_id).css({'display' : 'none'});
						$("#message-"+data.message_id).vkfade("CFCB2A");
						$("#message-"+data.message_id).slideDown(1000);
						document.reply_form.reply.value = "";
					break;
					case "fail":
						alert(data.the_html);
					break;
				}
		   }
		 });
	}
}

function comment(ajax_url) {
	comment_text = document.comment_post_form.comment_text.value;
	author_id = document.comment_post_form.author_id.value;
	rec_id = document.comment_post_form.rec_id.value;
	if (comment_text!="" && comment_text!="Write a comment...") {
		$.ajax({
		   type: "POST",
		   url: ajax_url,
		   data: "task=add_comment&the_key="+the_key+"&text="+comment_text+"&author="+author_id+"&rec="+rec_id,
		   dataType: "json",
		   success: function(data){
				switch(data.status) {
					case "success":
						document.comment_post_form.comment_text.value = "Write a comment...";
						if ($("#comments-container").html()=="<p>There are no comments.</p>") {
							$("#comments-container").html("");
						}
				  		$("#comments-container").prepend(data.the_html);
						$("#comment-"+data.comment_id).css({'display' : 'none'});
						$("#comment-"+data.comment_id).vkfade("CFCB2A");
						$("#comment-"+data.comment_id).slideDown(1000);
					break;
					case "fail":
						alert(data.the_html);
					break;
				}
		   }
		 });
	}
}

function delete_comment(comment_id, ajax_url){
	if(confirm("Are you sure you want to delete this comment?")){
		$.ajax({
		   type: "POST",
		   url: ajax_url,
		   data: "task=delete_comment&the_key="+the_key+"&comment_id="+comment_id,
		   dataType: "json",
		   success: function(data){
				switch(data.status) {
					case "success":
						$('#comment-' + data.comment_id).remove();
					break;
					case "fail":
						alert("Sorry there was an error deleting the comment.");
					break;
				}
		   }
		 });
	}
}

function load_areas_levels(subject_num,ajax_url,area_array,level_array) {
	error = false;
	for (a=1; a<4; a++) {
		if ((document.save_account_form["subject"+subject_num].value==document.save_account_form["subject"+a].value) && (a!=subject_num) && (document.save_account_form["subject"+subject_num].value!="-")) {
			error = true;
			break;
		}
	}
	if (error) {
		alert("You've already selected this subject. Please choose another.");
		document.save_account_form["subject"+subject_num].value = "-";
		$("#area"+subject_num+"-container").html('<select name="level'+subject_num+'" disabled="disabled" class="form-select"><option value="-" selected="selected">Select a subject first</option></select>');
		$("#level"+subject_num+"-container").html('<select name="level'+subject_num+'" disabled="disabled" class="form-select"><option value="-" selected="selected">Select a subject first</option></select>');
	} else {
		the_data = "task=load_areas_levels&the_key="+the_key+"&num="+subject_num+"&subject="+document.save_account_form["subject"+subject_num].value;
		$.ajax({
		   type: "POST",
		   url: ajax_url,
		   data: the_data,
		   dataType: "json",
		   success: function(data){
				switch(data.status) {
					case "success":
						$("#area"+subject_num+"-container").html(data.area_html);
						$("#level"+subject_num+"-container").html(data.level_html);
						if (area_array.length>0 && level_array.length>0) {
							for(i = 0; i < document.save_account_form["area"+subject_num+"[]"].length; i++) {
								for (a=0; a<area_array.length; a++) {
									if (area_array[a]==document.save_account_form["area"+subject_num+"[]"][i].value) {
										document.save_account_form["area"+subject_num+"[]"][i].selected = true;
									}
								}
							}
							for(i = 0; i < document.save_account_form["level"+subject_num+"[]"].length; i++) {
								for (a=0; a<level_array.length; a++) {
									if (level_array[a]==document.save_account_form["level"+subject_num+"[]"][i].value) {
										document.save_account_form["level"+subject_num+"[]"][i].selected = true;
									}
								}
							}
						}
					break;
					case "fail":
						alert(data.the_html);
					break;
				}
		   }
		 });
	}
}

// -----------------------------------------------------------------------------
//                               STUDENT  SESSIONS
// -----------------------------------------------------------------------------
function check_sessions(ajax_url,user_id,task,change_to) {
//	the_data = "the_key="+the_key;
//alert(ajax_url + " - " + the_data);
	$.ajax({
		type: "POST",
		url: ajax_url,
//		data: the_data,
		data: "task="+task+"&the_key="+the_key+"&user_id="+user_id+"&change_to="+change_to,
		dataType: "json",
		success: function(data){	
//alert(data.status);	

//var property, propCollection = "";
//for(property in data.users){
//	propCollection += (property + ": " + data.users[property] + "\n");
//}
//alert(propCollection);	
			
			switch(data.status) {
				case "success":
					if (typeof(data.users) != 'undefined' && data.users.length>0) {
						for (a=0; a<data.users.length; a++) {
							$.growl.settings.displayTimeout = 7200;
							$.growl.settings.dockCss.width = '225px';
							$.growl.settings.noticeCss = {
							    opacity: '100'
							  };
							$.growl.settings.noticeTemplate = "<div style=\"background-color:#F3F4CD;border:5px #D0CC2E solid;color:#323940;padding:10px;font-size:14px;text-align:center\"><span style=\"font-weight:bold;font-size:16px\">%title%</span><br /><br />%message%<br /><p style=\"text-align:center\"><a target=\"_blank\" href=\""+data.users[a].base_url+"session/join/"+data.users[a].roomid+"/"+data.users[a].session_id+"\" rel=\"close\">Start</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:void(0);\" rel=\"close\" onclick=\"status_bar('"+ajax_url+"','"+user_id+"','cancel_stsession','"+data.users[a].session_id+"')\">Reject</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:void(0);\" rel=\"close\">Close</a></p></div>";
							var msg = "<strong>" + data.users[a].first_name + " " + data.users[a].last_name + "</strong> wants to start a tutoring session with you.";
							var title = 'Attention!';
							$.growl(title, msg);
						}
					}
				break;
			}
		}
	})
}

