// THIS FILE IS VITAL FOR THE CORRECT VALIDATION AND SUBMITTING PROCESS - DO NOT CHANGE ANYTHING IF YOU ARE NOT SURE EXACTLY WHAT TO DO! ALLWAYS KEEP A COPY OF ORIGINAL FILE BEFORE MADE ANY CHANGES!!!

$(document).ready(function() {
// Localisation of the error messages. If you are not familiar with jQuery, just replace the error messages with your own language expressions
$.tools.validator.localize("custom", {
	'*'			: 'Моля, коригирайте това поле', // General error message
	':email'  	: 'E-mail адресът е невалиден!', // Invalid e-mail error message
	':url' 		: 'Това не е валидно URL', // Invalid URL error message
	'[max]'	 	: '12', // Do not change this!
	'[min]'		: '5',	// Do not change this!
	'[required]'	: 'Това поле е задължително!' // Required field error message
});
// Do not change this!
$("#contact_form").bind("onFail", function(e, errors)  {
// we are only doing stuff when the form is submitted
	if (e.originalEvent.type == 'submit') {

		// loop through Error objects and add the border color
		$.each(errors, function()  {
			var input = this.input;
			input.css({borderColor: '#C00'}).focus(function()  {
				input.css({borderColor: '#F60'});
			});
		});
	}

$.tools.validator.fn("[minlength]", function(input, value) {
	var min = input.attr("minlength");
	
	return value.length >= min ? true : {     
		custom: "Моля, въведете минимум " +min+ " символ" + (min > 1 ? "а" : "") // Minimum simbols required error message
	};
});
});

// VALIDATION AND CHECKING FOR ON.ALL.SUCCESS

// DO NOT CHANGE THIS LINES IF YOU ARE NOT A jQuery specialist!

$("#contact_form").validator({lang: 'custom' 
  // CODE 
  }).bind("onSuccess", function(e, els)  {
    var numSucceeded = els.length,
    numExpected = $(this).data('validator').getInputs().length;

    // have we successfully validated input?		
    if (numSucceeded === numExpected) {  

    // successfull process here
    allowed_to_post = true;
  }
}); // end of bind(onSuccess)

// CODE THAT RUNS ONLY IF ALL VALIDATION WORKS
$("#contact_form").bind('submit',function() {	
  if ( allowed_to_post ) {
	  
	  //hide the form
		$('#contact_form').hide();
		
		//show the loading bar
		$('.loader').append($('.bar')); 
		$('.bar').css({display:'block'});
		
		//send the ajax request  
		$.post('http://menkov.eu/new/wp-content/themes/menkov/mailer.php',{name:$('#name').val(), phone:$('#phone').val(), email:$('#email').val(), comment:$('#message').val()},
	
		//return the data
		function(data){
			//hide the graphic
			$('.bar').css({display:'none'}); 
			$('.loader').append(data);  
		});	
	
		//stay on the page					
		return false;  
	  
	}
});		
}); 
