﻿var MT = MT || {};   // project namespace;
$(document).ready(function(){
    //Start code 
    
    MT.validate = (function() {
        return {
            validateAll: function() {
                var required = $('.required'), valid = [], that = this;
                required.each(function() {
                    var errors = that.validate.call(that,this);
                    if (errors.length === 0) {
                        valid.push(this);
                    }
                });
                return valid.length === required.length;
            },
            validate: function(el) {
                var errors = validator.validate(el.value, el.className, el);
                this.render(el, errors);
                return errors;
            }, 
            render: function(el, errors) {
                return $(el).closest('.field')
                    .find('.errorMessage').html(errors.length>0?errors[0]:'').show();
            }
        };
    })();


     // validation handlers;
    // complete form validation binding;
    $('.uiLondonTicketsSubmit').live('click', function () {
        return MT.validate.validateAll.call(MT.validate);
    });

    // inline validation bindings;
    $('.required').each(function() {
        if (this.type === 'radio' || this.type === 'checkbox') {
            $(this).change(function(){
                MT.validate.validate.call(MT.validate, this);
            });
        }
        else if (this.type === 'text' || this.nodeName.toLowerCase() === 'select') {
            $(this).blur(function(){
                MT.validate.validate.call(MT.validate, this);
            });
        }
    });
    
    //bind click to show and hide the form
    $("#winAFamilyTicket").bind("click", function(){
        $("#uiLondonTicketsPopup").removeClass("hide");
        return false;
    });
    
     $("#uiLondonTicketsPopupClose").bind("click", function(){
        $("#uiLondonTicketsPopup").addClass("hide");
        return false;
    });

 //end code 
 });
