$(document).ready(function() {

	$("#formPost").validate({
		errorPlacement: function(error, element) {
			offset = element.offset();
            error.insertBefore(element);
        }
	});
 
	if(!String.prototype.startsWith){
		String.prototype.startsWith = function (str) {
			return !this.indexOf(str);
		}
	}

	function sendMessage(params){
		$.ajax({
			type: 'POST',
			url: '/includes/process-contact.php',
			data: params,
			cache: false,
			success: function(html){
 				if (html.startsWith("<Error>")) {
					$('#contactResponse').html(html);					
					$('#contactResponse').fadeIn('slow');
				}
				else {
					$('#contactResponse').html(html);
					$('#contactResponse').fadeIn('slow');
					$('#contactResponse').fadeOut(2000);
					$("#formPost")[0].reset();
				}
			},
			error: function(html) {
				$('#contactResponse').fadeIn('slow');
				$('#contactResponse').html(html);				
				$("#contactResponse").fadeOut("slow");
			}
		});
	}
	
	function postComment(params) {
		$.ajax({
			type: 'POST',
			url: '/includes/process-comment.php',
			data: params,
			cache: false,
			success: function(html) {
 				if (html.startsWith("<Error>")) {
					$('#commentResponse').html(html);					
					$('#commentResponse').fadeIn('slow');
				}
				else {
					$('#commentResponse').html('Comment added successfully');
					$("#formPost")[0].reset();
					var elem = $('.postComment').last();
					if (elem.length != 0) {
						$('.postComment').last().parent().append(html);
					}
					else {
						$('#postComments').append(html);
					}
					$('.postComment').last().css({'background-color':'#ffffff', 'border':'1px solid #7f230c', 'display':'none'});
					$('.postComment').last().fadeIn(3000);
					$('.postComment').last().css({'background-color':'#ffca9b', 'border':'0'});
					$('#commentResponse').fadeIn('slow');
					$("#commentResponse").fadeOut(5000);
				}

			},
			error: function(html) {
				$('#commentResponse').fadeIn(1000);
				$('#commentResponse').html('There was a problem submitting the comment.  Please try again.');				
			}
		});
	}

	$('#submitContact').click(function(){
		//if the form is valid, call the send function
		if ($('#formPost').valid()) {
			var datastr = 'contactName=' + $('#contactName').val() + '&contactEmail=' + $('#contactEmail').val() + '&contactWebsite=' + $('#contactWebsite').val() + '&contactMessage=' + $('#contactMessage').val() + '&contactPassword=' + $('#contactPassword').val();
			$('#contactResponse').css('display', 'block');
			$('#contactResponse').html('<img src="/images/loading.gif" />&nbsp;Sending message...');
			$('#contactResponse').fadeIn('fast');
			sendMessage(datastr);
		}
		else {
			$('#contactResponse').fadeIn('fast');
			$('#contactResponse').html('Please enter all required data.');
			$('#contactName').focus();
		}
		return false;
	});

	$('#submitComment').click(function(){
		$('#commentResponse').fadeOut('fast');
		//if the form is valid, call the send function
		if ($('#formPost').valid()) {
			var datastr = 'postID=' + $('#postID').val() + '&commentName=' + $('#commentName').val() + '&commentEmail=' + $('#commentEmail').val() + '&commentURL=' + $('#commentURL').val() + '&commentText=' + $('#commentText').val();
			$('#commentResponse').css('display', 'block');
			$('#commentResponse').html('Posting comment...');
			$('#commentResponse').fadeIn('slow');
			postComment(datastr);
		}
		else {
			$('#commentResponse').fadeIn('slow');
			$('#commentResponse').html('Please enter all required data.');
			$('#commentName').focus();
		}
		return false;
	});

});
