(function($){
"use strict";
var ajaxUrl=pfilter_select_opt.ajaxUrl;
function updateURL(data,url){
var shopURL=url;
$.each(data, function(key, value){
if(typeof(value)!='undefined'&&value!=null){
value=value.toString();
if(value.length){
shopURL +='&'+key+'='+value;
}}
});
shopURL=encodeURI(shopURL);
return shopURL;
}
$('div.select-filter').each(function(){
var $this=$(this),
shopURL=$this.attr('data-shop')+'?sel=true',
filter={},
request=false;
$this.find('select').on('change',function(){
if(request){return;}
var select=$(this),
data={},
atts={};
if(!$(this).val()){return;}
$this.addClass('loading');
select.parent().nextAll().find('select option:not(".default")').remove();
select.parents('form').find('select').each(function(){
if($(this).val()){
atts[$(this).attr('name')]=$(this).val();
}});
data['action']='filter_select';
data['atts']=JSON.stringify(atts);
data['next']=select.parent().next().children().attr('name');
if(select.attr('name')=='category'){
filter['product_cat']=select.val();
}else{
filter['filter_'+select.attr('name')]=select.val();
}
var url=updateURL(filter,shopURL);
$this.find('button').attr('data-url',url);
request=true;
$.ajax({
url:ajaxUrl,
type: 'POST',
data: data,
success: function(output){
if(output){
output=JSON.parse(output);
if(output['terms']){
select.parent().next().children().find('option').not('.default').remove();
select.parent().next().children().append(output['terms']);
}}
},
complete: function(){
$this.removeClass('loading');
request=false;
},
error:function (){
alert(pfilter_select_opt.error);
}});
});
$this.find('button').on('click',function(e){
e.preventDefault();
if($this.find('input[type="text"]').val()){
window.location.replace($this.attr('data-shop')+'?post_type=product&s='+$this.find('input[type="text"]').val());
} else
if($(this).attr('data-url')){
window.location.replace($(this).attr('data-url'));
}})
});
})(jQuery);
(function($){
var valid="invalid";
function validateValue($value, $target, $placeholder,$email){
if($email==true){
var n=$value.indexOf("@");
var r=$value.lastIndexOf(".");
if(n < 1||r < n + 2||r + 2 >=$value.length){
valid="invalid";
}else{
valid="valid";
}
if($value==null||$value==""||valid=="invalid"){
$target.addClass('visible');
}else{
$target.removeClass('visible');
}}else{
if($value==null||$value==""||$value==$placeholder){
$target.addClass('visible');
}else{
$target.removeClass('visible');
}}
};
$('.et-mailchimp-form').each(function(){
var $this=$(this);
$this.submit(function(event){
event.preventDefault();
var formData=$this.serialize();
var email=$this.find("input[name='email']"),
fname=$this.find("input[name='fname']"),
lname=$this.find("input[name='lname']"),
phone=$this.find("input[name='phone']"),
list=$this.find("input[name='list']");
validateValue(email.val(), email.next(".alert"), email.attr('data-placeholder'), true);
if(fname.length&&fname.attr('data-required')=="true"){validateValue(fname.val(), fname.next(".alert"), fname.attr('data-placeholder'), false);}
if(lname.length&&lname.attr('data-required')=="true"){validateValue(lname.val(), lname.next(".alert"), lname.attr('data-placeholder'), false);}
if(phone.length&&phone.attr('data-required')=="true"){validateValue(phone.val(), phone.next(".alert"), phone.attr('data-placeholder'), false);}
if(email.val()!=email.attr('data-placeholder')&&valid=="valid"){
if(fname.length&&fname.attr('data-required')=="true"&&fname.val()==fname.attr('data-placeholder')){event.preventDefault();}
if(lname.length&&lname.attr('data-required')=="true"&&lname.val()==lname.attr('data-placeholder')){event.preventDefault();}
if(phone.length&&phone.attr('data-required')=="true"&&phone.val()==phone.attr('data-placeholder')){event.preventDefault();}
$this.find(".sending").addClass('visible');
$.ajax({
type: 'POST',
url: $this.attr('action'),
data: formData
})
.done(function(response){
$this.find(".sending").removeClass('visible');
$this.find(".et-mailchimp-success").addClass('visible');
setTimeout(function(){
$this.find(".et-mailchimp-success").removeClass('visible');
},2000);
})
.fail(function(data){
$this.find(".sending").removeClass('visible');
$this.find(".et-mailchimp-error").addClass('visible');
setTimeout(function(){
$this.find(".et-mailchimp-error").removeClass('visible');
},2000);
})
.always(function(){
setTimeout(function(){
$this.find("input[name='email']").val(email.attr('data-placeholder'));
$this.find("input[name='fname']").val(fname.attr('data-placeholder'));
$this.find("input[name='lname']").val(lname.attr('data-placeholder'));
$this.find("input[name='phone']").val(phone.attr('data-placeholder'));
},2000);
});
}});
$this.find('input').on('focus',function(){
$(this).next('.visible').removeClass('visible');
});
});
})(jQuery);