$(document).ready(function(){
	$('#frm').validate({
		ignore: [], 
		highlight: function(element) {
			$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); 
		},
		success: function(element) {
			element.closest('.form-group').removeClass('has-error').addClass('has-success');
			element.remove();
		}
	});
	$('#recover_pass_frm').validate({
		ignore: [], 
		highlight: function(element) {
			$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); 
		},
		success: function(element) {
			element.closest('.form-group').removeClass('has-error').addClass('has-success');
			element.remove();
		},rules: {
			new_password: {
				required: true,
				pwcheck: true,
				minlength: 8	
			},	
			confirm_password: {
				equalTo: "#new_password",
			}	
		},
		messages: {
			new_password: {
				required: "Password required",
				pwcheck: "Password must contain at least one uppercase (A-Z) and one lowercase (a-z) and one (0-9) and one special character and at least eight characters.",
				minlength: "Password must contain at least eight characters."
			}
		}
	});
	$('#login_frm').validate({
		ignore: [], 
		highlight: function(element) {
			$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); 
		},
		success: function(element) {
			element.closest('.form-group').removeClass('has-error').addClass('has-success');
			element.remove();
		},
		submitHandler: function(form){
			var site_base_url = $('#h_base_url').val();
			var form = $('#login_frm');
			var url = site_base_url+form.attr('action');
			$.ajax({
				type: "POST",
				url: url,
				data: form.serialize(), // serializes the form's elements.
				beforeSend: function(){
					$('#result_display').html('<img src="'+site_base_url+'assets/backend/images/ajax-loading.gif" alt="" />');
				},
				success: function(result, status, xhr){//alert(result);
					if(result=='success'){
						var h_redirect_url = $('#h_redirect_url').val();
						if(h_redirect_url!=''){
							window.location=site_base_url+h_redirect_url;
						}else{
							window.location=site_base_url+'messages/';
						}
					}else{
						$("html, body").animate({ scrollTop: 0 }, "slow");
						$('#result_display').html('<div class="alert alert-danger">'+result+'</div>');
					}
				},
				error: function(xhr, status, error_desc){				
					$("html, body").animate({ scrollTop: 0 }, "slow");
					$('#result_display').html('<div class="alert alert-danger">'+error_desc+'</div>');
				}
			});		
			return false;
		}
	});
	$('#register_frm').validate({
		ignore: [], 
		highlight: function(element) {
			$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); 
		},
		success: function(element) {
			element.closest('.form-group').removeClass('has-error').addClass('has-success');
			element.remove();
		},rules: {
			new_password: {
				required: true,
				pwcheck: true,
				minlength: 8	
			},	
			confirm_password: {
				equalTo: "#new_password",
			}	
		},
		messages: {
			new_password: {
				required: "Password required",
				pwcheck: "Password must contain at least one uppercase (A-Z) and one lowercase (a-z) and one (0-9) and one special character and at least eight characters.",
				minlength: "Password must contain at least eight characters."
			}
		},
		errorElement: 'span',
		errorClass: 'error',
		errorPlacement: function (error, element) {
			if (element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
				error.insertAfter(element.parent().parent().parent());
			}else {
				error.insertAfter(element);
			}
		},
		submitHandler: function(form){
			var capchta_word = $('#capchta_word').val();
			var enter_captcha_txt = $('#enter_captcha_txt').val();
			if(capchta_word==enter_captcha_txt){				
				var site_base_url = $('#h_base_url').val();
				var form = $('#register_frm'); 
				var url = site_base_url+form.attr('action');
				var formData = new FormData($('#register_frm').get(0));
				formData.append('upload_doc', $('#upload_doc')[0].files[0]);				
				$.ajax({
					type: "POST",
					url: url,
					enctype: 'multipart/form-data',
					data: formData,
					processData: false,
					contentType: false,
					beforeSend: function(){
						$('#result_display').html('');
						$('#loader_img').html('<img src="'+site_base_url+'assets/backend/images/ajax-loading.gif" alt="" /> -- please wait --');
					},
					success: function(result, status, xhr){//alert(result); 
						if(result=='success'){						
							$('#result_display').html('<div class="alert alert-success"><strong>Thankyou : </strong> Your registration has been done successfully.</div>');
							$("html, body").animate({ scrollTop: 0 }, "slow");
							$('#register_frm')[0].reset();
							$("div").removeClass("has-error");
							$("div").removeClass("has-success");
							$('#loader_img').html('');					
						}else{						
							$('#result_display').html('<div class="alert alert-danger">'+result+'</div>');
							$("html, body").animate({ scrollTop: 0 }, "slow");
							$('#loader_img').html('');						
						}
					},
					error: function(xhr, status, error_desc){				
						$('#result_display').html('<div class="alert alert-danger"><strong>Error : </strong> '+error_desc+'</div>');
						$("html, body").animate({ scrollTop: 0 }, "slow");
						$('#loader_img').html('');
					}
				});
			}else{
				$('#result_display').html('<div class="alert alert-danger"><strong>Sorry, </strong> The captcha field does not match the Capchta Word field.</div>');
				$("html, body").animate({ scrollTop: 0 }, "slow");
				$('#loader_img').html('');
			}		
			return false;
		}
	});
	$.validator.addMethod("pwcheck", function(value) {
	   return /^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z]).{8,}$/.test(value);
	});
	$('#internship_frm').validate({
		ignore: [], 
		highlight: function(element) {
			$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); 
		},
		success: function(element) {
			element.closest('.form-group').removeClass('has-error').addClass('has-success');
			element.remove();
		},
		errorElement: 'span',
		errorClass: 'error',
		errorPlacement: function (error, element) {
			if (element.prop('type') === 'checkbox' || element.prop('type') === 'radio') {
				error.insertAfter(element.parent().parent().parent());
			}else {
				error.insertAfter(element);
			}
		},
		submitHandler: function(form){	
			var capchta_word = $('#capchta_word').val();
			var enter_captcha_txt = $('#enter_captcha_txt').val();
			if(capchta_word==enter_captcha_txt){
				var site_base_url = $('#h_base_url').val();
				var form = $('#internship_frm'); 
				var url = site_base_url+form.attr('action');
				var formData = new FormData($('#internship_frm').get(0));
				formData.append('upload_doc', $('#upload_doc')[0].files[0]);
				$.ajax({
					type: "POST",
					url: url,
					enctype: 'multipart/form-data',
					data: formData,
					processData: false,
					contentType: false,
					beforeSend: function(){
						$('#result_display').html('');
						$('#loader_img').html('<img src="'+site_base_url+'assets/backend/images/ajax-loading.gif" alt="" /> -- please wait --');
					},
					success: function(result, status, xhr){//alert(result);
						if(result=='success'){
							$('#result_display').html('');
							window.location=site_base_url+'internships/thankyou';
						}else{						
							$('#result_display').html('<div class="alert alert-danger">'+result+'</div>');
							$("html, body").animate({ scrollTop: 0 }, "slow");
							$('#loader_img').html('');
						}
					},
					error: function(xhr, status, error_desc){						
						$('#result_display').html('<div class="alert alert-danger">'+error_desc+'</div>');
						$("html, body").animate({ scrollTop: 0 }, "slow");
						$('#loader_img').html('');
					}
				});
			}else{
				$('#result_display').html('<div class="alert alert-danger"><strong>Sorry, </strong> The captcha field does not match the Capchta Word field.</div>');
				$("html, body").animate({ scrollTop: 0 }, "slow");
				$('#loader_img').html('');
			}
			return false;		
		}
	});
	$('#avail_start_date').datepicker({
		format: "mm/dd/yyyy",
		autoclose: true,
		todayHighlight: true
	});
	$('#avail_end_date').datepicker({
		format: "mm/dd/yyyy",
		autoclose: true,
		todayHighlight: true
	});
	$('#forgot_pass').validate({
		ignore: [], 
		highlight: function(element) {
			$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); 
		},
		success: function(element) {
			element.closest('.form-group').removeClass('has-error').addClass('has-success');
			element.remove();
		},
		submitHandler: function(form){
			var capchta_word = $('#capchta_word').val();
			var enter_captcha_txt = $('#enter_captcha_txt').val();
			if(capchta_word==enter_captcha_txt){
				var site_base_url = $('#h_base_url').val();
				var form = $('#forgot_pass');
				var url = site_base_url+form.attr('action');
				$.ajax({
					type: "POST",
					url: url,
					data: form.serialize(), // serializes the form's elements.
					beforeSend: function(){
						$('#result_display').html('<img src="'+site_base_url+'assets/backend/images/ajax-loading.gif" alt="" />');
					},
					success: function(result, status, xhr){//alert(result);
						if(result=='success'){
							$('#result_display').html('<div class="alert alert-success"><strong>Success!</strong> We have sent you recovery link to reset your password, please check your mail and click the link sent to you.</div>');
							$("html, body").animate({ scrollTop: 0 }, "slow");
							$('#forgot_pass')[0].reset();
							$("div").removeClass("has-error");
							$("div").removeClass("has-success");
						}else{
							$("html, body").animate({ scrollTop: 0 }, "slow");
							$('#result_display').html('<div class="alert alert-danger">'+result+'</div>');
						}
					},
					error: function(xhr, status, error_desc){				
						$("html, body").animate({ scrollTop: 0 }, "slow");
						$('#result_display').html('<div class="alert alert-danger">'+error_desc+'</div>');
					}
				});
			}else{
				$('#result_display').html('<div class="alert alert-danger"><strong>Sorry, </strong> The captcha field does not match the Capchta Word field.</div>');
				$("html, body").animate({ scrollTop: 0 }, "slow");
				$('#loader_img').html('');
			}		
			return false;
		}
	});
	$('#newsletter_frm').validate({
		ignore: [], 
		highlight: function(element) {
			$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); 
		},
		success: function(element) {
			element.closest('.form-group').removeClass('has-error').addClass('has-success');
			element.remove();
		},
		errorElement: 'div',
		errorClass: 'error',
		errorPlacement: function (error, element) {
			error.insertAfter(element.parent());
		},
		submitHandler: function(form){
			var site_base_url = $('#h_news_base_url').val();
			var form = $('#newsletter_frm');
			var url = site_base_url+form.attr('action');
			$.ajax({
				type: "POST",
				url: url,
				data: form.serialize(), // serializes the form's elements.
				beforeSend: function(){
					$('.home_feedback_msg').html('<div class="loader_icon" style="margin-top:10px;"><i class="fa fa-spinner fa-spin" aria-hidden="true" style="font-size:20px;"></i> -- please wait --</div>');
				},
				success: function(result, status, xhr){//alert(result);
					if(result=='success'){
						$('.home_feedback_msg').html('<div class="alert alert-success">Thanks for subscribing. You will receive a regular newsletter from MyShoetips.com.</div>');
						$('#newsletter_frm')[0].reset();
						$("div").removeClass("has-error");
						$("div").removeClass("has-success");
					}else{
						$('.home_feedback_msg').html('<div class="alert alert-danger">'+result+'</div>');
					}
				},
				error: function(xhr, status, error_desc){				
					$('.home_feedback_msg').html('<div class="alert alert-danger">'+error_desc+'</div>');
				}
			});		
			return false;
		}
	});
});
function get_farmer_type(are_you_id){
	if(are_you_id==2){
		$('#farmer_type_field').hide();
		$('#company_fields').show();
		$('#company_name').addClass(' required ');
		$('#upload_doc').addClass(' required ');		
		$('.farmer_types_case').removeClass(' required ');
	}else{
		$('#company_fields').hide();
		$('#farmer_type_field').show();
		$('.farmer_types_case').addClass(' required ');
		$('#company_name').removeClass(' required ');
		$('#upload_doc').removeClass(' required ');
	}
}
$(document).ready(function(){
	var max_desc = parseInt($("#bio_content").attr("maxlength"));
	$("#bio_content").keyup(function(e){
		$("#bio_content_count_desc").text("Characters left: " + (max_desc - $(this).val().length));
	});	
});
function redirect_custom_script(redirect_path,target_check){	
	if(target_check!=''){
		window.open(redirect_path, '_blank');
	}else{
		window.location=redirect_path;
	}
}