

$(document).ready(function () { 
	function detectBrowser()
{
	if (window.XMLHttpRequest) {
	return true;
	} else {
	return false;
	}
}
$('p#announce').fadeIn(2000);
        // Fullname validation logic
        var validateFullname = $('#validateFullname');
        $('#fullname').blur(function () {
            // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
            
            // only run the check if the username has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
				if(detectBrowser())
				{
                validateFullname.removeClass('error').html('<img src=\"images/formloader.gif\" /> validating field...');
				}
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'contact.php',
                        data: 'action=check_fullname&fullname=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_fullname (php code) in to the validation message
                            validateFullname.html(j.fullname);
                        }
                    });
                }, 200);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
        });
		
		
		 // email validation logic
        var validateEmail = $('#validateEmail');
        $('#email').blur(function () {
            // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
            
            // only run the check if the email has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
				if(detectBrowser())
				{
                validateEmail.removeClass('error').html('<img src=\"images/formloader.gif\" /> validating field...');
				}
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'contact.php',
                        data: 'action=check_email&email=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_phone (php code) in to the validation message
                            validateEmail.html(j.email);
                        }
                    });
                }, 200);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
        });
		
		// subject validation logic
        var validateSubject = $('#validateSubject');
        $('#subject').blur(function () {
            // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
            
            // only run the check if the subject has actually changed - also means we skip meta keys
            if ((this.value != this.lastValue)) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
				if(detectBrowser())
				{
                validateSubject.removeClass('error').html('<img src=\"images/formloader.gif\" /> validating field...');
				}
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'contact.php',
                        data: 'action=check_subject&subject=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_subject (php code) in to the validation message
                            validateSubject.html(j.subject);
                        }
                    });
                }, 200);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
        });
		
		// message validation logic
        var validateMessage = $('#validateMessage');
        $('#message').keypress(function () {
            // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
            
            // only run the check if the message has actually changed - also means we skip meta keys
            if ((this.value != this.lastValue)) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
				if(detectBrowser())
				{
                validateMessage.removeClass('error').html('<img src=\"images/formloader.gif\" /> validating field...');
				}
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'contact.php',
                        data: 'action=check_message&message=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_message (php code) in to the validation message
                            validateMessage.html(j.message);
                        }
                    });
                }, 200);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
        });

		//--------------END FORM VALIDATION-------------//
    });