// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var TopicForm = {
  editNewTitle: function(txtField) {
    $('new_topic').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Topic';
  }
};

var LoginForm = {
  setToPassword: function() {
    $('openid_fields').hide();
    $('password_fields').show();
  },
  
  setToOpenID: function() {
    $('password_fields').hide();
    $('openid_fields').show();
  }
};

var EditForm = {
    // show the form
    init: function(postId) {
        $('edit-post-' + postId + '_spinner').show();
        this.clearReplyId();
    },

    // sets the current post id we're editing
    setReplyId: function(postId) {
        $('edit').setAttribute('post_id', postId.toString());
        $('post_' + postId + '-row').addClassName('editing');
        if ($('reply')) $('reply').hide();
    },

    // clears the current post id
    clearReplyId: function() {
        var currentId = this.currentReplyId();
        if (!currentId || currentId == '') return;

        var row = $('post_' + currentId + '-row');
        if (row) row.removeClassName('editing');
        $('edit').setAttribute('post_id', '');
    },

    // gets the current post id we're editing
    currentReplyId: function() {
        return $('edit').getAttribute('post_id');
    },

    // checks whether we're editing this post already
    isEditing: function(postId) {
        if (this.currentReplyId() == postId.toString())
        {
            $('edit').show();
            $('edit_post_body').focus();
            return true;
        }
        return false;
    },

    // close reply, clear current reply id
    cancel: function() {
        this.clearReplyId();
        $('edit').hide();
    }
};

var ReplyForm = {
    // yes, i use setTimeout for a reason
    init: function() {
        EditForm.cancel();
        $('reply').toggle();
        $('post_body').focus();
        // for Safari which is sometime weird
        //    setTimeout('$(\"post_body\").focus();',50);
    }
};
var CarbEntryForm = {
    // yes, i use setTimeout for a reason
    init: function() {
        $('addcarb').toggle();
        $('addcarb').focus();
        // for Safari which is sometime weird
        //    setTimeout('$(\"post_body\").focus();',50);
    }
};
var CarbSiteForm = {
    // yes, i use setTimeout for a reason
    init: function() {
        $('addsite').toggle();
        $('addsite').focus();
        // for Safari which is sometime weird
        //    setTimeout('$(\"post_body\").focus();',50);
    }
};

var SearchEntryForm = {
    init: function() {
        $('searchcarb').toggle();
        $('searchcarb').focus();
    }
};

Event.addBehavior({
    '#search,#monitor_submit': function() {
        this.hide();
    }
});

function validate_new_carbs(new_carb_entry)
{
	if (""==document.forms.new_carb_entry.new_carb_count_item_description.value)
	{
			alert("Please enter an description of the carb item.");
			document.forms.new_carb_entry.new_carb_count_item_description.focus();
			return false;
	}
	if (""==document.forms.new_carb_entry.new_carb_count_cp.value)
	{
			alert("Please enter a CP value.");
			document.forms.new_carb_entry.new_carb_count_cp.focus();
			return false;
	}
}
function validate_new_site(new_carb_site)
{
	if (""==document.forms.new_carb_site.new_carb_site_site_description.value)
	{
			alert("Please enter an description of the carb site.");
			document.forms.new_carb_site.new_carb_site_site_description.focus();
			return false;
	}
	if (""==document.forms.new_carb_site.new_carb_site_site_url.value)
	{
			alert("Please enter a link to the site.");
			document.forms.new_carb_site.new_carb_site_site_url.focus();
			return false;
	}
}

function add_today_to_text_field(field_id)
{
    var currentTime = new Date();
    var day = currentTime.getDate().toString();
    if (day.length == 1)
        day = '0' + day;
    var month = (currentTime.getMonth() + 1).toString()
    if (month.length == 1)
        month = '0' + month;
    document.getElementById(field_id).value = "" + day +
        month + currentTime.getFullYear().toString().substring(2,4);
         
}

function add_yesterday_to_text_field(field_id)
{
    var currentTime = new Date();
    var yesterday = new Date(currentTime - 86400000);
    var day = yesterday.getDate().toString();
    if (day.length == 1)
        day = '0' + day;
    var month = (yesterday.getMonth() + 1).toString()
    if (month.length == 1)
        month = '0' + month;
    document.getElementById(field_id).value = "" + day +
        month +  yesterday.getFullYear().toString().substring(2,4);

}