﻿Wipfli.Blog = function()
{
    Ext.onReady(this.Initialize.createDelegate(this));
}

Ext.extend(Wipfli.Blog, Ext.util.Observable,
{
    Initialize : function()
    {
        this.addEvents({"onData": true});    
    },
    
    
    BlogPostCommentValidate: function()
    {
        var msg = "";
       
        if (document.getElementById('username').value == ""){msg += "-- User Name\n";}
        if (document.getElementById('realname').value == ""){msg += "-- Real Name\n";}
        if (this.ValidateBlogCommentEmail(document.getElementById('email')) == false){msg += "-- E-mail Address\n";}
        if (document.getElementById('headline').value == ""){msg += "-- Headline\n";}
        if (document.getElementById('comment').value == ""){msg += "-- Comment\n";}

        if (msg != ""){
            alert("The following required field(s) are missing:\n\n" + msg + "\nPlease complete the form and resubmit.");
            
        }else{
            this.BlogPostCommentCreate();     
        }
    },
    
    BlogPostCommentCreate : function()
    {
        var blogPostID = document.getElementById('blogPostID').value;
        var un = document.getElementById('username').value;
        var rn = document.getElementById('realname').value;
        var email = document.getElementById('email').value;
        var comp = document.getElementById('company').value;
        var hl = document.getElementById('headline').value;
        var comment = document.getElementById('comment').value;
        
        Wipfli.AjaxAPI.WipfliAjax.BlogPostCommentCreate(blogPostID, un, rn, email, comp, hl, comment, this.BlogPostCommentsRetrieve_Process.createDelegate(this))
    },
    BlogPostCommentsRetrieve: function()
    {     
        var blogPostID = document.getElementById('blogPostID').value;
        Wipfli.AjaxAPI.WipfliAjax.BlogPostCommentsRetrieve(blogPostID, this.BlogPostCommentsRetrieve_Process.createDelegate(this));
    },
    BlogPostCommentsRetrieve_Process : function(response)
    {
        if (response.error)
        {
            //alert(response.error.Message);
        }
        else
        {
            var divCommentsMC = Ext.get("divCommentsMC");
            var a = Ext.DomQuery.selectNode('commentsData', response.value);
            divCommentsMC.update(a.firstChild.nodeValue);
            
            var divCCount = Ext.get("divCommentCount");
            var cCount = Ext.DomQuery.selectNode('commentCount', response.value);
            divCCount.update(cCount.firstChild.nodeValue);

        }    
    },
    
    BlogCommentCountsRetrieve:function(BlogID)
    {
        Wipfli.AjaxAPI.WipfliAjax.BlogCommentCountsRetrieve(BlogID, this.BlogCommentCountsRetrieve_Process.createDelegate(this));
    },
    BlogCommentCountsRetrieve_Process:function(res)
    {
        var x = $(res.value);
        
        $(x).find("BlogPost").each(function()
        {
            var bpID = $(this).attr("BlogPostID");
            var cc = $(this).attr("CommentCount");
            $('#BlogPostCC' + bpID).html('<strong>Comments (' + cc + ')</strong>');
        });
    },
        
    ValidateBlogCommentEmail:function(obj)
    {
        var str = obj.value;
        if (str == "")
            return false;
            
        var at="@";
        var dot=".";
        var lat=str.indexOf(at);
        var lstr=str.length;
        var ldot=str.indexOf(dot);
        if (str.indexOf(at)==-1){
            //EmailAlert(obj);
           return false;
        }

        if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
            //EmailAlert(obj);
           return false;
        }

        if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
            //EmailAlert(obj);
            return false;
        }

         if (str.indexOf(at,(lat+1))!=-1){
            //EmailAlert(obj);
            return false;
         }

         if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
            //EmailAlert(obj);
            return false;
         }

         if (str.indexOf(dot,(lat+2))==-1){
            //EmailAlert(obj);
            return false;
         }

         if (str.indexOf(" ")!=-1){
            //EmailAlert(obj);
            return false;
         }

         return true;
    }
    
});
