// prepare the form when the DOM is ready 
$(document).ready(function() { 
    var options = { 
        target:        '#note',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest  // pre-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm' 
    $('#main-contact-form').ajaxForm(options); 
}); 
      
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
    
    $('.error').hide();
		
		// change input elements background color to white 
		$("input.input").css({backgroundColor:"#ffffff"});

		// check if the value for name is not empty or not equal to default value
		/*var filename = $("input#filename").val();
		if (filename == "") {
			$("label#filename_error").fadeIn('slow');
			$("input#filename").animate({ backgroundColor: '#ffffbb'}, 1000);
			$("input#filename").focus();
			return false;
		} */
		
    var name = $("input#name").val();
		if (name == ""  || name =="Име") {
			$("label#name_error").fadeIn('slow');
			$("input#name").animate({ backgroundColor: '#ffffbb'}, 1000);
			$("input#name").focus();
			return false;
		}

		// check if the value for email is not empty or not equal to default value
		var email = $("input#email").val();	
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;	
		
		if (email == "" || email =="Имейл адрес") {
			$("label#email_error").fadeIn('slow');
			$("input#email").animate({ backgroundColor: '#ffffbb'}, 400);
			$("input#email").focus();
			return false;
		}
		// test if the value for email is a valid email address
		else if(!emailReg.test(email)) {
			$("label#email_error2").fadeIn('slow');
			$("input#email").animate({ backgroundColor: '#ffffbb'}, 400);
			$("input#email").focus();
    		return false;
		}
		
		// check if the value for subject is not empty or not equal to default value
		var subject = $("input#subject").val();
		if (subject == "" || subject =="Относно") {
		    $("label#subject_error").fadeIn('slow');
		    $("input#subject").animate({ backgroundColor: '#ffffbb'}, 400);
		    $("input#subject").focus();
			return false;
		}
		
		// check if the value for message is not empty or not equal to default value
		var message = $("textarea#message").val();
		if (message == "" || message =="Съобщение") {
		    $("label#message_error").fadeIn('slow');
		    $("textarea#message").animate({ backgroundColor: '#ffffbb'}, 400);
			$("textarea#message").focus();
			return false;
		}
		
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 

    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    $.ajax({
			type: "POST",
			url: "contact.php",
			data: queryString,
			
			success: function(msg){
				$("#note").ajaxComplete(function(event, request, settings){
					$("#note").show();
					if(msg == 'OK'){
						result = '<span class="notification_ok">Your message has been sent. We will be in touch soon.</span>';
						$("#fields").hide();
					} 
					else {
						result = msg;
					}
					$(this).html(result);
				});
			}
		});
		return false;

} 

