$(document).ready(function(){
   
   $("#various5").fancybox({
		'width'				: 600,
		'height'			: 550,
        'autoScale'     	: true,
        'autoDimensions'    : true,
        'transitionIn'		: 'fadeIn',
		'transitionOut'		: 'fadeOut',
		'type'				: 'iframe',
        'onClosed': function(){
            $("#order_box").expose({color: "#000"});
            $("input[name='first_name']").focus();
        }
	});
    
    
    // expose the form from here
    //$("")
    $("#order_box input[type='text']").bind('focus',function(){
    $("#order_box").expose({color: "#0D3E85"});        
    });
     
     
     // the form validation here
     $("#summary").click(function(){
        $(this).slideUp();
     });
     
     
     
     //Add Postcode Regex Method to Validator Function
	$.validator.addMethod("regex",
		function(value, element, regexp) {
			var check = false;
			var re = new RegExp(regexp);
			return this.optional(element) || re.test(value);
		},
		"Please enter a valid postcode."
	);
 
	//Add Postcode Regular Expression Rule to Postcode Field
    //$("#EventPostcode").rules("add", {});

     	jQuery.validator.messages.required = "";
	var submitted = false;
    $("#summary").hide();
	$("form#orderform").validate({
	   /*
	   showErrors: function(errorMap, errorList) {
        $("#summary").html("Your form contains "
                                   + this.numberOfInvalids() 
                                   + " errors, see details below.");
        this.defaultShowErrors();
    },
    */
    showErrors: function(errorMap, errorList) {
            if (submitted) {
                //var summary = "<h3>You have the following errors:</h3>";
                var summary = "";
                $.each(errorMap, function(key, value) { 
            summary += "<p>"+ value + "<p>";
                });
                //alert(summary);
                $("#summary").html(summary).show();
                submitted = false;
            }
           this.defaultShowErrors();
        },          
        invalidHandler: function(form, validator) { submitted = true; },
        errorPlacement: function(error, element) { },
        errorClass: "errormsg",
		rules:
		{
			//cert_number:{required:true},
            first_name:{required:true},
            last_name:{required:true},
            postcode:{required:true,
             regex: "^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([        ])([0-9][a-zA-z][a-zA-z]){1}$"
             },
            address1:{required:true},
            address2:{required: function(element){
                    return (($("#address1").val() == '') && ($("#address2").val().length == 0));
                  }},
            towncity:{required:true},
            county:{required:true}
		}, // end of rules
		messages: {
            first_name:{required:"Please Fill In Your <strong>Firstname</strong>"},
            last_name:{required:"Please Fill In Your <strong>Lastname</strong>"},
            postcode:{required:"Please Fill In your <strong>UK Postcode</strong>",regex:"Please Enter A Valid <strong>UK Postcode</strong>"},
            address1:{required:"Please Fill In Your <strong>Address</strong>"},
            address2:{required: "Please Fill In <strong>Address1</strong> First"},
            towncity:{required:"Please Fill In Your <strong>Town/City</strong>"},
            county:{required:"Please Fill In Your <strong>County</strong>"}
		},
		onkeyup: false,
        submitHandler: function(form) {
            $("#summary").hide();
            
            var data = $(form).serialize();
       //    alert("the data to submit is " + data);
     
            $.ajax({
url: INSERT_ORDER,
data: data,
type: 'POST',
cache: false,
dataType: 'json',
success: function(html){
//console.log(html);    
    if(html.result == true)
    {
        $("#form_cont").slideUp(800);
        $("#form_notify").addClass('success').html("Thank You Very Much, You Should Recieve Your Pack Within The Next Few Days");
        $("#form_notify_cont").slideDown(800); 
    }
    else
    {
        //$("#form_cont").slideUp(800);
        $("#form_notify").addClass('error').html("Oops, Your Form Was Not Submmited Successfully, Please Try Again");
        $("#click_close").hide();
        $("#form_notify_cont").slideDown(800); 
    }
},
error:function(html)
{
        $("#form_notify").addClass('error').html("Oops, Your Form Was Not Submmited Successfully, Please Try Again");
        $("#click_close").hide();
        $("#form_notify_cont").slideDown(800); 
}
});



//setTimeout()
    }
	});
    
    
});
