﻿
//SubmitComment
var submitComment = Class.create();
submitComment.prototype = {

    articleId : null,
    categoryBreadCrumb : null,
    submitCommentEmail : null, 
    submitCommentName : null, 
    submitCommentComent : null, 
    submitCommentEmailLabel : null, 
    submitCommentNameLabel : null, 
    submitCommentComentLabel : null, 
    submitCommentCaptcha : null,
    commentStatus : null,
     
   
    initialize : function(articleID, categoryID, emailID, nameID, commentID, statusID, emailIDLabel, nameIDLabel, commentIDLabel)
    {
        this.articleId = articleID;
        this.categoryId = categoryID;
        this.commentStatus = statusID;
        
        //this.toggleSubmitCommentPopup(false);
      
        // events
        Event.observe("btnSubmitComment", "click", this.send.bindAsEventListener(this));
        Event.observe("lnkClose","click",this.toggleSubmitCommentPopup.bind(this,false));
        Event.observe("btnClear","click",this.clear.bindAsEventListener(this));
        
        // submitComment Elements
        this.submitCommentName = $(nameID);
        this.submitCommentEmail = $(emailID); 
        this.submitCommentComent = $(commentID);        
        this.submitCommentCaptcha = $(captchaID);
        this.submitCommentNameLabel = $(nameIDLabel);
        this.submitCommentEmailLabel = $(emailIDLabel); 
        this.submitCommentComentLabel = $(commentIDLabel);       
    },
    
    toggleSubmitCommentPopup : function(show)
    {
        if (show)
        {
           $("submitComment").show();
        }
        else
        {
           $("submitComment").hide();       
        }
    },
    
    clear : function()
    {
        //this.toggleSubmitCommentPopup(true);
        this.submitCommentName.value = "";
        this.submitCommentmail.value = "";
        this.submitCommentComent.value = "";
        this.submitCommentCaptcha.value = false;
    },
    send : function()
    { 
        if(validateFormControls("SubmitComment"))
        {
            TwentyFour.Sites.Sport.UI.Site.ArticleAjax.SubmitComment(this.articleId, this.categoryId, 
                $F(this.submitCommentEmail), $F(this.submitCommentName), $F(this.submitCommentComent).replace(/\n/g, '<br/>'), 
                this.commentStatus);
        }
    },
    onSendComplete : function(response)
    {
        if (response.error)
            alert("error");
        else
        {
             this.clear();
             this.toggleSubmitCommentPopup(false);
         }
             
     
    }
};

    function validateFormControls(name, email,comment, lblName, lblEmail, lblComment, captchaText, captchaLabel)
    {
        var javaEmailRegex = new RegExp("^((?:(?:(?:[a-zA-Z0-9][\\.\\-\\+_]?)*)[a-zA-Z0-9])+)\@((?:(?:(?:[a-zA-Z0-9][\\.\\-_]?){0,62})[a-zA-Z0-9])+)\\.([a-zA-Z0-9]{2,6})$");
       
        var validName = document.getElementById(name).value;
        var validEmail = document.getElementById(email).value;
        var validComment = document.getElementById(comment).value;
        var enableName = document.getElementById(lblName);
        var enableEmail= document.getElementById(lblEmail);
        var enableComment = document.getElementById(lblComment);
        var validCaptcha = document.getElementById(captchaText).value;
        var enableCaptcha = document.getElementById(captchaLabel);
        
        var matchValidEmail = javaEmailRegex.exec(validEmail);
        
        var validForm = true;

        if(validName == '')
        {    
            enableName.style.display = "";
            enableName.style.color = "red";
            validForm = false;
        }
        else
        {
            enableName.style.display = 'none';
        }
        
        if(matchValidEmail == null)
        {
            enableEmail.style.display = "";
            enableEmail.style.color = "red";
            validForm = false;         
        }
        else
        {
            enableEmail.style.display = 'none';
        }  
            
        if(validCaptcha == "")
        {
            enableCaptcha.style.display = "";
            enableCaptcha.style.color = "red";
            validForm = false;    
        }
        else
        {
            enableCaptcha.style.display = 'none';
        }
        
        if(validComment == "")
        {
            enableComment.style.display = "";
            enableComment.style.color = "red";
            validForm = false;         
        }
        else
        {
            enableComment.style.display = 'none';
        }
        
      
        if(!validForm)
        {
            return false;
        }
        else
        {
            enableName.style.display = 'none';
            enableEmail.style.display = 'none';
            enableComment.style.display = 'none';
            enableCaptcha.style.display = 'none';
            //openCommentSubmit();
            return true;   
        }    
    }
    
    function openCommentSubmit() {
        var commentWin = window.open(_commentInsertedPopup,'commentWin','width=380,height=210,resizable,status=0,toolbar=0');
    } 