var ContactForm = Class.create();

ContactForm.prototype = {
	initialize : function(form) {
		this.form = $(form);
	}
}

Object.extend(ContactForm, {
    checkRequiredFields : function(){
        
        myValidation.reset();

        $('txtClubName').removeClassName('required');
        $('txtEmail').removeClassName('required');
        $('txtEmail').removeClassName('validate-email');
        $('txtStreet').removeClassName('required');
        $('txtZIP').removeClassName('required');
        $('txtZIP').removeClassName('validate-zip');
        $('txtCity').removeClassName('required');
        $('txtPhone').removeClassName('required');
        $('txtAvailability').removeClassName('required');

        $('txtInfoTopic').removeClassName('required');

        $('txtCustomerID').removeClassName('required');
        $('txtSupportRequest').removeClassName('required');
        
        $('txtCommentText').removeClassName('required');
        
        $('pnlInfoProducts').hide;
        $('pnlInfoTopic').hide;
        $('pnlSupport').hide;
        $('pnlComment').hide;
        $('pnlProductCampaign').hide;
        $('pnlProductMisc').hide;
        
    
        if ($('chkNewsletter').checked) {
            $('txtEmail').addClassName('required');
            $('txtEmail').addClassName('validate-email');
        }
        
        if ($('chkInfoAlbatros').checked) {
            $('txtStreet').addClassName('required');
            $('txtZIP').addClassName('required');
            $('txtZIP').addClassName('validate-zip');
            $('txtCity').addClassName('required');
        }
        
        if ($('chkInfoProducts').checked) {
            $('txtClubName').addClassName('required');
            $('txtEmail').addClassName('required');
            $('txtEmail').addClassName('validate-email');
        }
        
        if ($('chkInfoTopic').checked) {
            $('txtClubName').addClassName('required');
            $('txtPhone').addClassName('required');
            $('txtAvailability').addClassName('required');
            $('txtInfoTopic').addClassName('required');
        }

        if ($('chkSupport').checked) {
            $('txtClubName').addClassName('required');
            $('txtEmail').addClassName('required');
            $('txtEmail').addClassName('validate-email');
            $('txtPhone').addClassName('required');
            $('txtAvailability').addClassName('required');
            $('txtCustomerID').addClassName('required');
            $('txtSupportRequest').addClassName('required');
        }
        
        if ($('chkComment').checked) {
            $('txtEmail').addClassName('required');
            $('txtEmail').addClassName('validate-email');
            $('txtCommentText').addClassName('required');
        }
        
        if ($('chkProductCampaign').checked) {
            $('txtProductCampaignText').addClassName('required');
        }
        
        if ($('chkProductMisc').checked) {
            $('txtProductMiscText').addClassName('required');
        }
        
        ContactForm.foldPanel('pnlInfoProducts', $('chkInfoProducts').checked);
        ContactForm.foldPanel('pnlInfoTopic', $('chkInfoTopic').checked);
        ContactForm.foldPanel('pnlSupport', $('chkSupport').checked);
        ContactForm.foldPanel('pnlComment', $('chkComment').checked);
        ContactForm.foldPanel('pnlProductCampaign', $('chkProductCampaign').checked);
        ContactForm.foldPanel('pnlProductMisc', $('chkProductMisc').checked);

    },
    
    foldPanel : function(panel, show){
        if (show && $(panel).style.display == 'none'){
            new Effect.BlindDown(panel, {duration : 0.5 });
            return;
        }
        if (!show && $(panel).style.display != 'none'){
            new Effect.BlindUp(panel, {duration : 0.5 });
            return;
        }
    }

});

var myValidation;

Event.observe(window, 'load', function() {
	myValidation = new Validation('albatrosContact', {immediate : false, useTitles : true});
	
    Validation.add('validate-zip', 'Geben Sie eine Postleitzahl ein!', function(v) {
        return Validation.get('IsEmpty').test(v) || (!/[^\d]/.test(v) && !(v.length != 5));
    });
    
    Validation.add('validate-one-for-info', 'Wählen Sie bitte mindestens ein Produkt!', function(v,elm) {
       if ($('chkInfoProducts').checked != true) return true;
       var p = elm.parentNode;
       var options = p.getElementsByTagName('INPUT');
       return $A(options).any(function(elm) {
           return $F(elm);
       });
    });  
            
    Validation.add('validate-infotopic', 'Bitte geben Sie an, zu welchen Thema wir Sie informieren sollen.', function(v) {
       if ($('chkInfoTopic').checked == false) return true;
       return !Validation.get('IsEmpty').test(v)
    });   
           
    Validation.add('validate-supportcustid', 'Bitte geben Sie Ihre Kundennummer an.', function(v) {
       if ($('chkSupport').checked == false) return true;
       return !Validation.get('IsEmpty').test(v)
    }); 
             
    Validation.add('validate-supportrequest', 'Bitte geben Sie an, zu welchen Thema Sie Hilfe brauchen.', function(v) {
       if ($('chkSupport').checked == false) return true;
       return !Validation.get('IsEmpty').test(v)
    }); 
             
    Validation.add('validate-comment', 'Bitte geben Sie eine Mitteilung ein.', function(v) {
       if ($('chkComment').checked == false) return true;
       return !Validation.get('IsEmpty').test(v)
    });
    
    Validation.add('validate-product-campaign', 'Bitte geben Sie eine Mitteilung ein.', function(v) {
       if ($('chkProductCampaign').checked == false) return true;
       return !Validation.get('IsEmpty').test(v)
    });
    
    Validation.add('validate-product-misc', 'Bitte geben Sie eine Mitteilung ein.', function(v) {
       if ($('chkProductMisc').checked == false) return true;
       return !Validation.get('IsEmpty').test(v)
    });
    
    Validation.add('validate-contacttype', 'Bitte wählen Sie mindestens einen Kontaktgrund aus.', function(v) {
       if ($('chkNewsletter').checked == true) return true;
       if ($('chkInfoAlbatros').checked == true) return true;
       if ($('chkInfoProducts').checked == true) return true;
       if ($('chkInfoTopic').checked == true) return true;
       if ($('chkSupport').checked == true) return true;
       if ($('chkComment').checked == true) return true;
       return false;
    });
    
    ContactForm.checkRequiredFields();
});

