
//入力フォーム
var smtForm=Class.create();
Object.extend(smtForm.prototype,EventDispatcher.prototype);
Object.extend(smtForm.prototype,{

    initialize:function(o){
        this.prepare(this.checkList);
        this.errors=(this.errors||[]);
        if(!o) return;
        this.src=o;
        o.onsubmit=function(e){
            Event.stop(e||event);
            this.submit();
        }.bind(this);
    },

    checkList:{},
    submit:function(e){
        if(!this.check()) return;
        var myAjax=new Ajax.Request(
            "/cgi-bin/mailrequest.cgi",{
            postBody:Form.serialize(this.src),
            onComplete:this.onSendComplete
        });
    },

    onSendComplete:function(response){
        if(response.responseText=="OK") return alert("送信されました。\n詳細は追ってご連絡さしあげますので、\nそれまでお待ちください。");
        alert("送信に失敗しました。\nお手数をおかけしますが、\n電話他の方法にてお申し込みください。");
    },

    check:function(){
        this.customFields.each(function(o){o.check();});
        return ((this.errors||[]).length<=0);
    },

    prepare:function(items){
        if(!items) return;
        for(var i in items){
            var temp=new smtField(this.src.elements[i]);
            with(temp){
                rule=items[i];
                addEventListener("onFieldError",this);
                addEventListener("onFieldValid",this);
            }
            this.customFields=(this.customFields||[]);
            this.customFields.push(temp);
        }
    },

    onFieldError:function(obj){
        this.errors=this.errors.without(obj);
        this.errors.push(obj);
        this.dispatchEvent({type:"onError"},obj);
    },
    onFieldValid:function(obj){
        this.errors=this.errors.without(obj);
        this.dispatchEvent({type:"onValid"},obj);
    },

    terminate:function(){}
});


//入力項目
var smtField=Class.create();
Object.extend(smtField.prototype,EventDispatcher.prototype);
Object.extend(smtField.prototype,{
    initialize:function(o){
        this.src=o;
        new Form.Element.EventObserver(this.src,this.check.bind(this));
    },
    rule:function(){ return true; },
    check:function(){
        var v=Form.Element.getValue(this.src);
        if(this.rule(v)){
            this.dispatchEvent({type:"onFieldValid"},this);
            return;
        }
        this.dispatchEvent({type:"onFieldError"},this);
    },
    getName:function(){
        for(var o=this.src.parentNode;o;o=o.parentNode){
            var temp=document.getElementsByClassName("label",o);
            if(temp.length>0) return getText(temp.last());
        }
    },
    terminate:function(){}
});

function getText(obj){
    return (obj.textContent||obj.innerText);
}




