$(document).ready(function() {
    $('.default-msg').css('display', 'none');

	$('.btn-submit').click(function() {
		// show the "loading image"
		$('.ajax-loader-wrap').html(IMG_LOADER);

		// hide the error message and success message by default
        $('.default-msg').css('display', 'none');

		$('#main-error-message').hide();
		
		// check if default value is same as entered value
		$('.textfield').each(function() {
			$('.div-wrap-' + this.id).removeClass('textfield-error');
			if($(this).attr('title') == $(this).attr('value')) {
				$(this).val('');
			}
		});

		// store the form values in a query string format
        var query_str = $('form#contact-form').serialize();
		
		// initialize ajax
        $.ajax({
            type: 'POST',
            url: 'contact2.php',
            data: query_str,
            success: function(response) {
				$('.ajax-loader-wrap').html('');
  				// response = 1, means successful

				if(response == 1) {
					// do some animations to show and hide success message
                    $('#main-success-message').fadeIn('fast')
                    .animate({opacity: 1.0}, 5000)
                    .fadeOut('slow', function() {
                        $('.textfield').val('');
                        $(this).remove();
						location.href = PAGE_AFTER_2ND_FORM;
                    });
					
                }
                else {
					// we have some errors
                    var errors = response.split('|');
                    for ( var x in errors ) {
						$('.div-wrap-' + errors[x]).addClass('textfield-error');	
					}
                    $('#main-error-message').show();
					}
            }
        });
        // function to manipulate textfield on hover
        $('.textfield').hover(
            function() {
				var tmp_id = 'error-wrap-' + this.id;
                if('#' + tmp_id) {
                    $('#' + tmp_id).fadeOut('fast');
                }
            },
            function() {
                return;
            }
        );
    });

	// $('.contact-form .textfield').labelize({className:'alt-txtfld-textcolor'});
	$('.contact-form input.textfield, .contact-form select ').hover(
		function() {
			$(this).css('border', '2px solid #FFCC00');

		},
		function() {
			$(this).css('border', '2px solid #717165');
		}
	);
	
	$('.contact-form input.textfield').each(function() {
		var v = $(this).attr('title');
		$(this).val(v);
		// alert(v);
	});
	
	$('.contact-form input.textfield').focus(function() {
		var defval_title = $(this).attr('title');
		var defval_value = $(this).attr('value');
		if(defval_title == defval_value) {
			$(this).val('');
		}
	});
});