$(function() {

console.log("goldenform-setting.js");

	function reloadCaptcha(){ $("#captchax").attr("src","php/captcha/captcha.php?r=" + Math.random()); }
	$('.captcode').click(function(e){
		e.preventDefault();
		reloadCaptcha();
	});

	function swapTextSending(){
		var changeBtnText = $(".frm-footer button[type='submit']");
		if (changeBtnText.text() == changeBtnText.data("btntext-sending")) {
			changeBtnText.text(changeBtnText.data("btntext-original"));
		} else {
			changeBtnText.data("btntext-original", changeBtnText.text());
			changeBtnText.text(changeBtnText.data("btntext-sending"));
		}
	}

	var bar = $('.bar');
	var percent = $('.percent');

/* the fields */
	$( "#gform-pro" ).validate({
			errorClass: "state-error",
			validClass: "state-success",
			errorElement: "em",
			rules: {
				firstname: {
						required: true,
						minlength: 2
				},
				lastname: {
						required: true,
						minlength: 2
				},
				emailaddress: {
						required: true,
						email: true
				},
				telephone: {
						required: true,
						number: true,
						minlength: 10,
						maxlength: 12
				},
				address: {
						required: true
				},
				ageofhome: {
						required: true
				},


				uploadfile1:{
					required:false,
					extension:"jpeg|jpg|png|pdf"
				},
				uploadfile2:{
					required:false,
					extension:"jpeg|jpg|png|pdf"
				},
				uploadfile3:{
					required:false,
					extension:"jpeg|jpg|png|pdf"
				},
				uploadfile4:{
					required:false,
					extension:"jpeg|jpg|png|pdf"
				},

				captcha:{
					required:true,
					remote:'php/captcha/process.php'
				}
			},
	/* other fields are optional */




	/* input Assist */
			messages:{
				firstname: {
						required: 'Enter first name',
						minlength: 'Firstname must be at least 2 characters'
				},
				emailaddress: {
						required: 'Enter email address',
						email: 'Enter a VALID email address'
				},
				telephone: {
						required: 'Enter your telephone number',
						number: 'Please enter numbers only',
						minlength: 'Enter not less than 10 numbers',
						maxlength: 'Enter not more than 12 numbers'
				},
				uploadfile1:{
					required:'Browse to attach files',
					extension:'Please attach jpeg, jpg, png, pdf files only'
				},
				captcha:{
						required: 'You must enter the captcha code',
						remote:'Captcha code is incorrect'
				}
			},
			highlight: function(element, errorClass, validClass) {
					$(element).closest('.field').addClass(errorClass).removeClass(validClass);
			},
			unhighlight: function(element, errorClass, validClass) {
					$(element).closest('.field').removeClass(errorClass).addClass(validClass);
			},
			errorPlacement: function(error, element) {
			   if (element.is(":radio") || element.is(":checkbox")) {
						element.closest('.option-group').after(error);
			   } else {
						error.insertAfter(element.parent());
			   }
			},


					submitHandler:function(form) {
						$(form).ajaxSubmit({
								target:'.result',
								beforeSubmit:function(){
									swapTextSending();
									var percentVal = '0%';
									bar.width(percentVal);
									percent.html(percentVal);
									$(".uit-progress").show();
								},
								uploadProgress: function(event, position, total, percentComplete) {
									var percentVal = percentComplete + '%';
									bar.width(percentVal);
									percent.html(percentVal);
								},
								error:function(){
									swapTextSending();
									$( ".uit-progress" ).hide(500);
								},
								 success:function(){
									swapTextSending();
									var percentVal = '100%';
									bar.width(percentVal);
									percent.html(percentVal);
									$('.uit-progress').show().delay(5000).fadeOut();
									$('.alert-success').show().delay(10000).fadeOut();
									$('.field').removeClass("state-error, state-success");
									if( $('.alert-error').length == 0){
										$('#gform-pro').resetForm();
										reloadCaptcha();
									}
								 }
							});
					}

	});

});
