// Newsletter Subscription Form Validation

$(document).ready(function() {

	//Newsletter Subscription Form Validation
	var v = $("#frmNewsletterSubscription").validate({
		rules: {
			SubscriberEmail: { 
				required: true,
				email: true
			}
		},
		messages: {
			SubscriberEmail: {
				required: "Email Address is required",
				email: "Invalid Email Address"
			}
		},
		errorPlacement: function(error, element) {			
			if ( element.is(":radio") )
				error.appendTo( element.parent().parent().find('.validation-status') );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent().find('.validation-status') );
		}
	});
	
	// Contact Form Validation
	var vContact = $("#frmContact").validate({
		rules: {
			name: { 
				required: true 
			},
			emailaddress: { 
				required: true,
				email: true
			},
			telephone: {
				required: true,
				digits: true,
				rangelength: [5,12]
			},
			address1: {
				required: true
			},
			town: {
				required: true
			},
			county: {
				required: true
			},
			postcode: {
				required: true,
				rangelength: [5,10]
			},
			message: {
				required: true
			},
			verificationcode: {
				required: true
			}			
		},
		messages: {
			name: { 
				required: "" 
			},
			emailaddress: { 
				required: "",
				email: "Invalid Email Address"
			},
			telephone: {
				required: "",
				digits: "Must be digits",
				rangelength: $.format("Must be {0} to {1} characters long")
			},
			address1: {
				required: ""
			},
			town: {
				required: ""
			},
			county: {
				required: ""
			},
			postcode: {
				required: "",
				rangelength: $.format("Must be {0} to {1} characters long")
			},
			message: {
				required: ""
			},
			verificationcode: {
				required: ""
			}			
		},
		errorPlacement: function(error, element) {			
			if ( element.is(":radio") )
				error.appendTo( element.parent().parent().find('.validation-status') );
			else if ( element.is(":checkbox") )
				error.appendTo ( element.next() );
			else
				error.appendTo( element.parent().find('.validation-status') );
		}
	});
			
	$('#btnReset').click(function() {
		vContact.resetForm();
		vContact.prepareForm();
		vContact.hideErrors();		
	});


	//Draw Page Authentication
	$("#frmLogin").submit(function()
	{
		//Remove all the class add the messagebox classes and start fading
		$("#msg-box").removeClass().addClass('msg-box-process').text('Validating! Please wait....').fadeIn(1000);
		//Check the Access Code if exists or not from ajax
		$.post("contents/draw_login_process.asp",{ AccessCode:$('#accesscode').val(),rand:Math.random() } ,function(data)
		{
			  if(data=='yes') //if correct login detail
			  {
					$("#msg-box").fadeTo(200,0.1,function()  //start fading the messagebox
					{ 
						//Add message and change the class of the box and start fading
						$(this).html('Redirecting! Please wait....').addClass('msg-box-valid').fadeTo(900,1,
						function()
						{ 
							//Redirect to secure page
							document.location='draw/';
						});			  
					});
			  } else {
					$("#msg-box").fadeTo(200,0.1,function() //start fading the messagebox
					{ 
						$("#msg-box").removeClass()
						//Add message and change the class of the box and start fading
						$(this).html('Sorry! Access failed.').addClass('msg-box-error').fadeTo(900,1);
					});		
			  }
					
		});
		return false; //Not to post the form physically
	});

});

