//
// Show the form to add a quick link
//
function addComment(blogpostid) {
	var new_comment_link = $('new_comment_link');
	var entry_comment = $('entry_comment_0');
	
	//window.location='http://www.joehaydenrealtor.com/blog/blog_post_comment_form.asp?action=add&blogpostid='+blogpostid;
	
	var bindArgs = {
	asynchronous: true,
	method: 'get',
	parameters: 'action=add&blogpostid='+blogpostid,	
	onFailure: function(t){
		alert('An error occurred and we could not retrieve the comment form.  Please try again. (' + t.responseText + ')');
		entry_comment.innerHTML = '';
	},
	onSuccess: function(t){	
		entry_comment.innerHTML = t.responseText;
		new Effect.Fade(new_comment_link, {queue: 'end', duration: 0.2});
		new Effect.Appear(entry_comment, {queue: 'end', duration: 0.8});
		new Effect.ScrollTo(entry_comment, {queue: 'end', offset: -24});
		new Effect.Highlight(entry_comment, {queue: 'end', duration: 1.5})	
	}
	};
	
	new Ajax.Request('/blog/blog_post_comment_form.asp', bindArgs);
}

//
// Sends the quick link for editing
//
function saveNewComment(blogpostid) {
	var new_comment_link = $('new_comment_link');
	var entry_comment = $('entry_comment_0');
	var entry_comment_orig_content = entry_comment.innerHTML;
	var params = Form.serialize($('frmComment0'));
	
	//alert(params);
	//window.location='/blog/blog_post_comment_process.asp?'+params.toLowerCase();	
	
	var bindArgs = {
	method: 'post',
	parameters: params,
	onFailure: function(t){
		//alert(t.responseText);
		alert('An error occurred and we could not send your request.  Please try again.');
	},
	onSuccess: function(t){			
		// If the id of the new link was returned successfully
		if (parseFloat(t.responseText) >= 1) {
			entry_comment.innerHTML = '<strong>Success!</strong> Your new comment was added succcessfully. Please note that blog post comments are held for moderation. We will review your comment as quickly as possible.';
			new Effect.Highlight(entry_comment, {queue: 'end', duration: 8.0});
			new Effect.BlindUp(entry_comment, {queue: 'end', duration: 0.3});
			new Effect.Appear(new_comment_link, {queue: 'end', duration: 0.8});
		// Otherwise, report an error
		} else {
			alert('An error occurred and the comment could not be added.  Please try again.');			
		}
	}
	};	
	
	// Validate the form contents before submitting	
	var postedBy = IsEmpty($('commentpostedby0'));
	
	var comment = IsEmpty($('comment0'));

	if (postedBy) {
		alert('Please enter a your name.');
		$('commentpostedby0').focus();
	} else if (comment) {
		alert('Please enter a comment.');
		$('comment0').focus();		
	} else {
		
		new Ajax.Request('/blog/blog_post_comment_process.asp', bindArgs);
	}
}

//
// Cancels the quick link edit
//
function cancelNewComment(blogpostid) {
	var new_comment_link = $('new_comment_link');
	var entry_comment = $('entry_comment_0');
	
	entry_comment.innerHTML = '';
	
	new Effect.Fade(entry_comment, {queue: 'end', duration: 0.2});
	new Effect.Appear(new_comment_link, {queue: 'end', duration: 0.6});
	new Effect.ScrollTo(new_comment_link, {queue: 'end', offset: -24})	

}

// checks whether a field is empty, returns bool
function IsEmpty(element){
	var s = element.value;
	
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) 
			return false;
    }
    return true;
}
// validate an email address, returns bool
function IsValidEmail(element){
	// assume an email address cannot start with an @ or white space, but it
	// must contain the @ character followed by groups of alphanumerics and '-'
	// followed by the dot character '.'
	// It must end with 2 or 3 alphanumerics.
	var e = element.value;
	var alnum="a-zA-Z0-9";
	exp="^[^@\\s]+@(["+alnum+"+\\-]+\\.)+["+alnum+"]["+alnum+"]["+alnum+"]?$";
	emailregexp = new RegExp(exp);

	result = e.match(emailregexp);
	if (result != null)
	{
		return true;
	}
	else
	{
		return false;
	}
}
