$(document).ready(function() { 
	$("#DropDownTimezone").change(function() {
		$("#p_timezone").html("");
		var new_selection = $("#DropDownTimezone").val();
		$.post("/update_timezone.php", { newValue: new_selection }, function(data) { 
			$("#p_timezone").html(data);
		});
	});
	$("#newemail").keyup(function(event){
    	if(event.keyCode == 13) {
    		update_email();
    	}
    });
	$("#newconfirmation").keyup(function(event){
    	if(event.keyCode == 13) {
    		update_password();
    	}
    });
});

function update_password() {
	var newpassword = $("#newpassword").val();
	var newconfirmation = $("#newconfirmation").val();
	$("#p_re").html("");
	if(newpassword == "") {
		$("#p_re").html('<div style="font-size: 12px; color: red; margin-top: 0px; margin-bottom: 10px;">&nbsp;Please enter a valid password</div>');
	} else if(newpassword != newconfirmation) {
		$("#p_re").html('<div style="font-size: 12px; color: red; margin-top: 0px; margin-bottom: 10px;">&nbsp;The passwords entered do not match. Please try again.</div>');
	} else {
		$.post("/update_password.php", { password: newpassword }, function(data) { 
			var reply = data.split("`");
			if(reply[0] == "success") {
				$("#p_re").html('<div style="font-size: 12px; color: green; margin-top: 0px; margin-bottom: 10px;">&nbsp;Your password has been updated with success! <a href="/settings/">[x] Close this</a></div>');
			} else {
				$("#p_re").html('<div style="font-size: 12px; color: red; margin-top: 0px; margin-bottom: 10px;">&nbsp;There was an error updating your password. Please try again.</div>');
			}
		});
	}
}

function update_email() {
	var newemail = $("#newemail").val();
	$("#p_email").html("");
	if(newemail == "") {
		$("#p_email").html('<div style="font-size: 12px; color: red; margin-top: 0px; margin-bottom: 10px;">&nbsp;Please enter a valid email</div>');
	} else {
		$.post("/update_email.php", { email: newemail }, function(data) { 
			var reply = data.split("~");
			if(reply[0] == "success") {
				$("#p_email").html('<div style="font-size: 12px; color: green; margin-top: 0px; margin-bottom: 10px;">&nbsp;Your email has been updated with success! <a href="/settings/">[x] Close this</a></div>');
			} else {
				$("#p_email").html('<div style="font-size: 12px; color: red; margin-top: 0px; margin-bottom: 10px;">&nbsp;'+ reply[1] +'</div>');
			}
		});
	}
}

function update_twitter(current_username) {
	var twitter_username = $("#twitter_settings").val();
	$("#t_re").html("");
	if(twitter_username == current_username) {
		$("#t_re").html('<div style="font-size: 12px; color: red; margin-top: 0px; margin-bottom: 10px;">&nbsp;Your current username and the new username provided is the same. No need to update!</div>');
	} else {
	$.post("/update_twitter.php", { username: twitter_username }, function(data) { 
		var reply = data.split("~");
		if(reply[0] == "success") {
			$("#t_re").html('<div style="font-size: 12px; color: green; margin-top: 0px; margin-bottom: 10px;">&nbsp;Your Twitter username has been updated with success! <a href="/settings/">[x] Close this</a></div>');
		} else {
			$("#t_re").html('<div style="font-size: 12px; color: red; margin-top: 0px; margin-bottom: 10px;">&nbsp;'+ reply[1] +'</div>');
		}
	});	
	}
}

function update_dateformat(newvalue) {
	$.post("/update_dateformat.php", { newValue: newvalue }, function(data) { 
		$("#p_dateformat").html(data);
	});
}

function deletet(reminder_id) {
	$.post("/delete.php", { reminder_id: reminder_id }, function(data) { 
		$("#" +reminder_id).slideUp("fast");
		$("#reminders").html(data);
	});
}

function removeall(reminder_id) {
	var confirmationstatus = confirm("Are you sure you would like to forever remove this reminder?");
	if(confirmationstatus == true) {
		$.post("/remove.php", { reminder_id: reminder_id }, function(data) { 
			$("#" +reminder_id).slideUp("fast");
			$("#reminders").html(data);
		});	
	}
}

function create_reminder(debug_status) {
	$("#errors").slideUp("fast");
	$("#errors").html("");
	var reminder_text = $("#reminder").val();
	var final_date = $("#finaldate").val();
	var hours = $("#hours").val();
	var minutes = $("#minutes").val();
	var time = $("#time").val();
	var task_about = $("#task_about").val();
	var repeating_status = $("#repeating").val();
	if($('#send_sms').attr('checked')) {
     	var sms_status = "1";
	} else {
		var sms_status = "";
	}
	if(debug_status == 1) {
		alert(repeating_status);
	} else {
	$.post("/createtask.php", { reminder: reminder_text, task_about: task_about, final_date: final_date, hours: hours, minutes: minutes, time: time, sendsms: sms_status, repeating: repeating_status }, function(data) { 
		var status = data.split("~");
		if(status[0] == "success") {
			window.location = "/dashboard/";
		} else {
			$("#errors").html(status[1]);
			$("#errors").slideDown("fast");
		}
	});	
	}
}

function edit_reminder(reminderid) {
	$("#errors").slideUp("fast");
	$("#errors").html("");
	var reminder_text = $("#reminder_edit").val();
	var final_date = $("#finaldate").val();
	var hours = $("#hours").val();
	var minutes = $("#minutes").val();
	var time = $("#time").val();
	var task_about = $("#task_about").val();
	var repeating_status = $("#repeating").val();
	if($('#send_sms').attr('checked')) {
     	var sms_status = "1";
	} else {
		var sms_status = "";
	}	
	$.post("/edittask.php", { reminder: reminder_text, task_about: task_about, final_date: final_date, hours: hours, minutes: minutes, time: time, reminderid: reminderid, sendsms: sms_status, repeating: repeating_status }, function(data) { 
		var status = data.split("~");
		if(status[0] == "success") {
			window.location = "/dashboard/";
		} else {
			$("#errors").html(status[1]);
			$("#errors").slideDown("fast");
		}
	});
}