﻿        
        
        function validateForm(id){
                                   
            var isValid = true;
            $(".invalid").removeClass("invalid");
           // $(".required").each(function(){
           jQuery('#'+id).find('.required, .optional').each(function(){
           
                if ($(this).hasClass("required")) {
                    if ($(this).val()== ""){
                        isValid = false;
                        processField($(this));
                    }
                    else {
                        if ($(this).hasClass("isEmail")) {
                            if (!isValidEmail($(this).val())) {
                                isValid = false;
                                processField($(this));
                            }
                            
                        }
                        
                    }
                }
                else {
                    if ($(this).val()!= ""){
                            if ($(this).hasClass("isEmail")) {
                                if (!isValidEmail($(this).val())) {
                                    isValid = false;
                                    processField($(this));
                                }
                                
                            } 
                        }
                }
           
           

           
            });
            
            

            
          if (!isValid)
            {
                
                if (id == "guaranteeForm") {
                    $("#step2 .error").show();
                }
                else {
                    $("#" + id + " .error").show();
                }
                return false;
            }
            //Ajax post serialisation back
                
                var url;
                
                switch(id)
                    {
                    case "brochure":
                      url = "/forms/brochure-postback.aspx";
                      redirect = "/page.aspx?pointerid=7d24a712751943908cb631a1e6cb5db7";
                      break;
                    case "step1":
                        return true;   
                    case "guaranteeForm":
                      url = "/forms/retailers-postback.aspx";
                      redirect = "/page.aspx?pointerid=8e2fa8728efd4899adc2fadc86323450";
                    }
                    
                    $.ajax({
                      url: url + "?" + serialize(id),
                      success: function(data) {
                        if (data == "SUCCESS") {
                            window.location.href = redirect;
                        }
                        else {
                            if (id == "guaranteeForm") {
                                $("#step2 .error2").show();
                            }
                            else {
                                $("#" + id + " .error2").show();
                            }
                        }
                      }
                    });


        }
        
        function processField(Object) {
            var labelFor = $(Object).attr("id");
            $(Object).addClass("invalid");
            $("label").each(function(){
                 if ($(this).attr("for")==labelFor)
                 {
                    $(this).addClass("invalid");                 
                 
                 }
                
                 
            
            });
        }
        
        function isValidEmail(email) {
        // do regex
        var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

            return filter.test(email);
        
        }
        
        function serialize(id){
            $("#purchasedate").attr("disabled", "");
            return $('#'+id + ' *').serialize();
        }
