var NewsletterForm = new Class({
    Implements: Options,
    options: {
        panel_id: 'newsletter_panel',
        url_add: '/ajax_panel/Newsletter/add/',
        url_remove: '/ajax_panel/Newsletter/remove/',
        success_txt: 'Vas pozadavek byl zaznamenan, dekujeme.'
    },
    initialize: function (options ){
        this.setOptions(options);
        if (!$(this.options.panel_id))
            return;
        var add_form_submit, add_form = $(this.options.panel_id).getElement('form');
        add_form_submit = add_form.getElement('.submit_button');
        add_form_submit.addEvent('click', (function (e ){
            e.stop();
            new Request.JSON({
                url: this.options.url_add,
                onComplete: (function (json ){
                    if (json.result){
                        if (json.result === true){
                            add_form.getElements('input[type=text]').set('value', '');
                            alert(this.options.success_txt); 
                        } else {
                            alert(json.message);
                        }
                    } else {
                        alert('Chyba, prosim opakujte pozdeji');
                    }
                }).bind(this)
            }).send(add_form);
        }).bind(this));
    }
    
});



//var ContactForm = new Class({
//    Implements: Options,
//    options: {
//        url: '/ajax_panel/ContactForm/send/',
//        inside: false,
//        classes: '.input_text, .input_textarea',
//        buttonClass: '.button',
//        comunicationErr: 'Opakujte svuj pozadavek pozdeji.',
//        comunicationEnd: 'Dekujeme, Vas pozadavek byl odeslan.',
//        errors: [],
//        validate_el: [], //Array of elements wich we validate
//        result: "Nejsou vyplněny následujicí údaje:\n\n"
//    },
//    
//    initialize: function(form, options,validate_el){
//        this.setOptions(options);
//        this.options.validate_el = validate_el;
//        this.form = form;
//        
//        this.form.getElements(this.options.classes).each(function(element){
//            element.store('orig_name', element.value);
//            element.addEvents({
//                'focus' : function(){
//                    if (this.value === this.retrieve('orig_name')){
//                        this.value = '';
//                    }
//                },
//                'blur' : function(){
//                    if (this.value === ''){
//                        this.value = this.retrieve('orig_name');
//                    }
//                }
//            });
//        });
//        
//        var btn = this.form.getElement(this.options.buttonClass);
//        if (btn){
//            btn.addEvent('click', (function(e){
//                e.stop();
//                var validate_result = this.validate();
//                
//                if(validate_result === true){
//                    new Request.JSON({
//                        url: this.options.url,
//                        onComplete: (function(json){
//                            if (json){
//                                if (json.result === true){
//                                    if(json.data){
//                                        alert(json.data);
//                                    } else {
//                                        alert(this.options.comunicationEnd);
//                                    }
//                                } else {
//                                    alert(json.message);
//                                }
//                            } else {
//                                alert(this.options.comunicationErr);
//                            }
//                        }).bind(this)
//                    }).send(this.form);    
//                }
//                else {
//                    alert(validate_result);
//                }
//                
//            }).bind(this));
//        }
//    },
//    makeResultMsg: function (){
//        return this.options.result + this.options.errors.join("\n");
//    },
//    makeErrorMsg: function (el,item,type){
//        switch(type){
//            case 'required':
//                if(!item.required_text ||  item.required_text === ''){
//                    //return 'Nevyplnily jste pole '+(this.options.inside == true ? el.retrieve('orig_name') : el.getPrevious('label').get('html')).replace(":","").toLowerCase();
//                    return 'Nevyplnily jste pole '+item.name;
//                }
//                else {
//                    return item.required_text;
//                }
//            break;
//            case 'validate':
//                if(!item.validate_text ||  item.validate_text === ''){
//                    //return 'Špatně vyplněno pole '+(this.options.inside == true ? el.retrieve('orig_name') : el.getPrevious('label').get('html')).replace(":","").toLowerCase();
//                    return 'Špatně vyplněno pole '+item.name;
//                }
//                else {
//                    return item.validate_text;
//                }
//            break;
//            default:alert('Nespecifikovany typ'); break;
//        }
//        return true;
//    },
//    validate: function(){
//        var prefix = 'ContactFormInstance';
//        this.options.errors.empty();
//        
//        Object.each(this.options.validate_el,function(item){
//            var el, html_id = prefix+item.contact_form_id.camelCase2()+item.id.camelCase2();
//            el = $(html_id);
//            if(item.required === true){ 
//                switch(item.type){
//                    case 'text':
//                    case 'textarea':
//                        if(el.value === '' || (this.options.inside === true && el.value === el.retrieve('orig_name'))){
//                            this.options.errors.push(' - '+this.makeErrorMsg(el,item,'required'));
//                        }
//                        else if(item.validate !== '' && el.value.test(item.validate) === false){
//                            this.options.errors.push(' - '+this.makeErrorMsg(el,item,'validate'));
//                        }
//                    break;
//                    case 'enum':
//                        if(el.value === ''){this.options.errors.push(' - '+this.makeErrorMsg(el,item,'required'));}
//                    break;
//                    case 'checkbox':
//                        if(!el.checked){this.options.errors.push(' - '+this.makeErrorMsg(el,item,'required'));}
//                    break;
//                }
//            }
//        },this);
//
//        if(this.options.errors.length < 1){
//            return true;
//        }
//        else {
//            return this.makeResultMsg();   
//        }
//    }
//    
//});
var ContactForm = new Class({
    Implements: Options,
    options: {
        url: '/ajax_panel/ContactForm/send/',
        inside: false,
        classes: '.input_text, .input_textarea',
        buttonClass: '.button',
        comunicationErr: 'Opakujte svuj pozadavek pozdeji.',
        comunicationEnd: 'Dekujeme, Vas pozadavek byl odeslan.',
        errors: [],
        validate_el: [], //Array of elements wich we validate
        result: "Nejsou vyplněny následujicí údaje:\n\n"
    },
    
    initialize: function(form, options,validate_el){
        this.setOptions(options);
        this.options.validate_el = validate_el;
        
        this.form = form;
        
        if (!this.form)
            return;

        
        
        this.form.getElements(this.options.classes).each(function(element){
            element.store('orig_name', element.value);
            element.addEvents({
                'focus' : function(){
                    if (this.value === this.retrieve('orig_name')){
                        this.value = '';
                    }
                },
                'blur' : function(){
                    if (this.value === ''){
                        this.value = this.retrieve('orig_name');
                    }
                }
            });
        });
        var btn = this.form.getElement(this.options.buttonClass);
        if (btn){
            btn.addEvent('click', (function(e){
                e.stop();
                var validate_result = this.validate();
//                console.log(validate_result);return false;
                
                if(validate_result === true){
                    new Request.JSON({
                        url: this.options.url,
                        onComplete: (function(json){
                            if (json){
                                if (json.result === true){
                                    if(json.data){
                                        alert(json.data);
                                    } else {
                                        alert(this.options.comunicationEnd);
                                    }
                                } else {
                                    alert(json.message);
                                }
                            } else {
                                alert(this.options.comunicationErr);
                            }
                            this.form.getElements().value = '';
                            location.reload(true);
                        }).bind(this)
                    }).send(this.form);    
                }
                else {
                    alert(validate_result);
                }
                
            }).bind(this));
        }
    },
    makeResultMsg: function (){
        return this.options.result + this.options.errors.join("\n");
    },
    makeErrorMsg: function (el,item,type){
        switch(type){
            case 'required':
                if(!item.required_text ||  item.required_text === ''){
                    //return 'Nevyplnily jste pole '+(this.options.inside == true ? el.retrieve('orig_name') : el.getPrevious('label').get('html')).replace(":","").toLowerCase();
                    return 'Nevyplnily jste pole '+item.name;
                }
                else {
                    return item.required_text;
                }
            break;
            case 'validate':
                if(!item.validate_text ||  item.validate_text === ''){
                    //return 'Špatně vyplněno pole '+(this.options.inside == true ? el.retrieve('orig_name') : el.getPrevious('label').get('html')).replace(":","").toLowerCase();
                    return 'Špatně vyplněno pole '+item.name;
                }
                else {
                    return item.validate_text;
                }
            break;
            default:alert('Nespecifikovany typ');break;
        }
        return true;
    },
    validate: function(){
        
        var prefix = 'ContactFormInstance';
        this.options.errors.empty();
        Object.each(this.options.validate_el,function(item){
            var el, html_id = prefix+item.contact_form_id.camelCase2()+item.id.camelCase2();
            el = $(html_id);
            if(item.required == true){ 
                switch(item.type){
                    case 'text':
                    case 'textarea':
                        if(el.value === '' || (this.options.inside === true && el.value === el.retrieve('orig_name'))){
                            this.options.errors.push(' - '+this.makeErrorMsg(el,item,'required'));
                        }
                        else if(item.validate !== '' && el.value.test(item.validate) === false){
                            this.options.errors.push(' - '+this.makeErrorMsg(el,item,'validate'));
                        }
                    break;
                    case 'enum':
                        if(el.value === ''){this.options.errors.push(' - '+this.makeErrorMsg(el,item,'required'));}
                    break;
                    case 'checkbox':
                        if(!el.checked){this.options.errors.push(' - '+this.makeErrorMsg(el,item,'required'));}
                    break;
                }
            }
        },this);

        if(this.options.errors.length < 1){
            return true;
        }
        else {
            return this.makeResultMsg();   
        }
    }
    
});

window.addEvent('domready',function(){
    new NewsletterForm();    
});
