jQuery( document ).ready(function( $ ) {
// model action
$( '.bk-vehicle-input-make' ).change( function() {
var form = $( this ).parents( '.bk-vehicle-search-form' );
$.post( '/wp-admin/admin-ajax.php', { 'action': 'bk_vehicle_get_models', 'make_id': this.value }, function( data ) {
var el = form.find( '.bk-vehicle-input-model' ),
firstOptText = el.find( 'option:first' ).text();
el.empty().append( $( '', { value: '', text: firstOptText } ) );
$.each( data, function( i, model ) {
el.append( $('', { value: model.id, text: model.name } ) );
});
// for selectbox
if (el.selectbox) {
form.find("select").selectbox("detach").selectbox();
}
}, 'json');
});
$( '.bk-vehicle-search-form, .bk-vehicle-sort-form' ).submit( function( e ) {
var args = $( this ).serializeArray(), path, paths = [],
items = [ 'make', 'model', 'fuel_type', 'transmission', 'min_price', 'max_price', 'location', 'keywords', 'order', 'dir', 'limit' ];
for ( var i = 0; i < items.length; i++ ) {
paths[i] = "";
}
if ( $(this).hasClass("bk-vehicle-search-form") ) {
args = args.concat($('.bk-vehicle-sort-form').serializeArray());
} else { // add sort form data onto end of search form data in both cases
args = $('.bk-vehicle-search-form').serializeArray().concat(args);
}
$.each( args, function ( idx, arg ) {
var value = $.trim( arg.value ), name = arg.name, pos;
if ( value.length > 0 && ( idx = $.inArray( name, items ) ) > -1 ) {
paths[idx] = name + '/' + value + '/';
}
} );
path = encodeURI( $( this ).prop( 'action' ) + paths.join('') );
location.href = path;
e.preventDefault();
} );
// helper function for checking length
function checkLength( o, n, min, max ) {
o.next("sup").remove();
if ( (max > 0 && o.val().length > max) || o.val().length < min ) {
o.addClass( "ui-state-error" );
if (max == 0) {
var str = "Length of " + n + " must be at least " + min +'.';
} else {
var str = "Length of " + n + " must be between " + min + " and " + max + ".";
}
$(""+str+"
").insertAfter(o);
return false;
} else {
o.removeClass( "ui-state-error" );
o.next("sup").remove();
return true;
}
}
// helper function for checking regulare expression
function checkRegexp( o, regexp, n ) {
o.next("sup").remove();
if ( !( regexp.test( o.val() ) ) ) {
o.addClass( "ui-state-error" );
var str = "Invalid format of " + n;
$(""+str+"
").insertAfter(o);
return false;
} else {
o.removeClass( "ui-state-error" );
o.next("sup").remove();
return true;
}
}
// enquiry action
$( '.bk-vehicle-enquiry-button' ).click( function(e) {
console.log('bk-vehicle-enquiry form submitted');
var idFields = $( this ).prop('id').split('-'),
id = idFields[idFields.length - 1],
dialog = $( '#bk-vehicle-enquiry-dialog-' + id ),
name = dialog.find( '.bk-vehicle-enquiry-dialog-name' ),
email = dialog.find( '.bk-vehicle-enquiry-dialog-email' ),
msg = dialog.find( '.bk-vehicle-enquiry-dialog-msg' ),
allFields = $( [] ).add( name ).add( email ).add( msg ).add( consent );
dialog.dialog( {
modal: true,
buttons: {
"Send": function() {
allFields.removeClass( "ui-state-error" );
var name_valid = checkLength( name, "name", 1, 120 );
var msg_valid = checkLength( msg, "your message", 20, 0 );
var email_valid = checkLength( email, "email", 6, 80 );
if (email_valid) {
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
email_valid = checkRegexp(email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "email");
}
var bValid = name_valid && email_valid && msg_valid;
if (bValid) {
var params = dialog.find('form').serializeArray();
$.post('/wp-admin/admin-ajax.php', params, function(data) {
dialog.dialog("option", "buttons", {
"OK": function() {
$( this ).dialog( "close" );
}
});
allFields.val( "" ).removeClass( "ui-state-error" );
$( 'form', dialog ).hide(0);
$( 'form', dialog ).before($( '