/**
 *  Document   : SampleForm
 *  Created on : 15-Jul-2010, 13:11:02
 *  Author     : Michal Wrzesniewski <mw@faktumpro.net>
 */

var SamplesForm = {
    lb: false,
    show:function(product_name){
        this.lb = new Lightbox({
            url:'/site_includes/samples_form.php?pn='+product_name+'&site_folder='+$("site_folder").value,
            width:515,
            height:500,
            hideClose:true,
            hideReload:true
        })
        this.lb.show();

    },
    hide:function(){
        this.lb.hide();
    },
    resetValidation:function(){
        var form = $('product-samples');

        $$('#product-samples input').each(function(el){
            el.removeClassName('invalid')
            if(el.next() && el.next().hasClassName('validation')){
                el.next().hide();
            }
        })
    },
    validateForm:function(){
        this.resetValidation();

        var form = $('product-samples');
        var inputs = $$('#product-samples input');

        var isFormValid = true;
        inputs.each(function(el){
            el.removeClassName('invalid');
            el.addClassName(el.getValue()=="" ? 'invalid':'')

            if(el.name=='email' && (!/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(el.getValue()))){
                el.addClassName('invalid')
            }
            if(el.hasClassName('invalid')){
                isFormValid = false;
                if(el.next() && el.next().hasClassName('validation')){
                    el.next().show();

                }
            }
        })

        if(!isFormValid){
            return;
        }
        $("product-samples").request({
            onSuccess:function(resp){

                var json = resp.responseText.evalJSON();

                if(json.success){
                    $("product-samples").remove();
                    $("siteLightboxInner").down().insert($("thankYouMessage"));
                    setTimeout(function(){
                        this.hide();
                    }.bind(this),2000)
                }else{
                    alert('Error occured during sending');
                }
            }.bind(this)
        })
    //                    new Ajax.Request('/site_includes/samples_process.php',{
    //                        onSuccess:function(resp){
    //                            var json = resp.responseJSON;
    //                            if(json.success){
    //
    //                            }else{
    //                                alert('Error occured during sending');
    //                            }
    //                        }
    //                    })

    },
    submit:function(){
        this.validateForm();
    }


}



