﻿var CommentAddControl = {
    Init: function(el)
    {
        var  className = (el.id);
        if (ControlManager.Exists(className)) 
        {
            var ctrl = ControlManager.Get(className);
        }
        else 
        {
            var ctrl = new BaseControl(
            el.dom.innerHTML,
            "",
            "",
            eval(CommentAddControl.Validators.Value),
            "action_" + Utils.GetControlName(el),
            CommentAddControl.CurrentPage,
            el
            ); 
        }
        ctrl.init();
        
        return ctrl;
       
    },
    Preview : function(action)
    {               
        
        var Form = action.basicForm;
        
        for (var i = 0; i < Form.items.items.length; i++) 
        {
            var value = Form.items.items[i].getValue();
            if (value != null)
            {
                Form.items.items[i].setValue(value.trim());
            }
        }
        if (!Form.isValid())
        {
            return;
        }
        
        action.dom.disabled= true;
        var FormEl = action.basicForm.el;
        var fullActionName = action.dom.getAttribute("name");
        var parameters = serializeForm(Form); 
        //var parameters = Form.getValues(true);
        var ProgressEl = null;        
        
        Ext.get(FormEl.dom).mask()
        
        AjaxProcessor.Request(
            Form.baseCtrl.ajaxPage,
            fullActionName,
            parameters,
            {ctrl: Form.baseCtrl, formEl: FormEl, progressEl: ProgressEl, submitEl: this}            
        );
        
    },
    Edit : function(action)
    {
        action.dom.disabled= true;
        
        var Form = action.basicForm;        
        var FormEl = action.basicForm.el;
        var fullActionName = action.dom.getAttribute("name");
        var parameters = serializeForm(Form); 
        //var parameters = Form.getValues(true);
        var ProgressEl = null;                       
        
        FormEl.mask();
        
        AjaxProcessor.Request(
            Form.baseCtrl.ajaxPage,
            fullActionName,
            parameters,
            {ctrl: Form.baseCtrl, formEl: FormEl, progressEl: ProgressEl, submitEl: this}
        );
        
    },
    Submit : function(action)
    {
        action.dom.disabled= true;
                     
        var Form = action.basicForm;
        var FormEl = action.basicForm.el;       
        
        //check for agreements
        var xpath = "input[@name^='" + CommentAddControl.LayoutMapper.Agreement + "']";
        //var agreement = new Ext.form.Checkbox(Ext.DomQuery.selectNode(xpath, FormEl.dom));
        var agreement = Ext.DomQuery.selectNode(xpath, FormEl.dom);
        var xpath = "input[@name^='" + CommentAddControl.LayoutMapper.ProperAge + "']";
        //var properAge = new Ext.form.Checkbox(Ext.DomQuery.selectNode(xpath, FormEl.dom));\
        var properAge = Ext.DomQuery.selectNode(xpath, FormEl.dom);
        
        var errors = "";
        if (!agreement.checked)
        {
            errors = "You should agree with usage<br/>";
        }
        if (!properAge.checked)
        {            
            errors =  errors + "You must be older than 13<br/>";
        }

        if (errors)
        {   
            var errorBox = Ext.DomQuery.selectNode("div[@class='" + Utils.ControlErrorBoxClassName +  "']", FormEl.dom);
            errorBox.innerHTML = errors;
            errorBox.style.display = "block"
            action.dom.disabled= false;
            return;
        }        
                
        var fullActionName = action.dom.getAttribute("name");
        var parameters = serializeForm(Form); 
        //var parameters = Form.getValues(true);
        var ProgressEl = null;                       
        
        FormEl.mask();
        
        AjaxProcessor.Request(
            Form.baseCtrl.ajaxPage,
            fullActionName,
            parameters,
            {ctrl: Form.baseCtrl, formEl: FormEl, progressEl: ProgressEl, submitEl: this}
        );
        
    },
    CreateAccount: function(action) {
        var Form = action.basicForm;
        var FormEl = action.basicForm.el;
        var fullActionName = action.dom.getAttribute("name");
        var parameters = "";
        var ProgressEl = null;

        Ext.getBody().mask();

        AjaxProcessor.Request(
            Form.baseCtrl.ajaxPage,
            fullActionName,
            parameters,
            { ctrl: Form.baseCtrl, formEl: FormEl, progressEl: ProgressEl, submitEl: this }
        );
    },
    LoginUser: function(action)
    {
        var Form = action.basicForm;
        var FormEl = action.basicForm.el;
        var fullActionName = action.dom.getAttribute("name");
        var parameters = "";
        var ProgressEl = null;   
        
        Ext.getBody().mask();                   
         
        AjaxProcessor.Request(
            Form.baseCtrl.ajaxPage,
            fullActionName,
            parameters,
            {ctrl: Form.baseCtrl, formEl: FormEl, progressEl: ProgressEl, submitEl: this}
        );    
    }

}
