jQuery.fn.clearForm = function(){
	return this.each(function(){
		var type = this.type, tag = this.tagName.toLowerCase();

		if (tag == 'form')
			return jQuery(':input',this).clearForm();
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
		this.selectedIndex = -1;
	});
};

jQuery.fn.showError = function(){
	jQuery(this)
		.css('border', '2px solid red')
		.next()
		.show()
		.animate({opacity: 'hide', marginLeft: '60'}, 200)
		.animate({opacity: 'show', marginLeft: '0'}, 100)
		.animate({marginLeft: '+=25'}, 50)
		.animate({marginLeft: '-=15'}, 50);

	jQuery(this)
		.focus(function(){
				jQuery(this)
					.css('border', '1px solid #CCC')
					.next()
					.fadeOut('slow');
			})
		.blur(function(){
				if (jQuery(this).val() == '')
				{
					jQuery(this)
						.css('border', '2px solid red')
						.next()
						.fadeIn('slow');
				}
			});
};

jQuery.fn.clearError = function(){
	jQuery(this)
		.css('border', '1px solid #CCC')
		.focus(function(){
				return false;
			})
		.blur(function(){
				return false;
			})
		.next()
		.hide();
};

jQuery(document).ready(function(){

	var url = location.href;

	// Call back form
	jQuery('.call-back').click(function(){
		jQuery('html, body').animate({scrollTop:0}, 'slow');
		jQuery('#popup-overlay').show();

		var oForm = jQuery('#callback_form');

		oForm
			.show()
			.clearForm();

		oForm.find('.error_message').hide();
		oForm.find('.loading_img').hide();
		oForm.find('input[name="name_F"]').clearError();
		oForm.find('input[name="telephone_F"]').clearError();
		oForm.find('input[name="email_F"]').clearError();

		jQuery('#callback-popup').find('h2.title').next().next().hide();

		jQuery('#callback-popup').fadeIn('slow');

		return false;
	});

	jQuery('#callback-popup').find('.close').click(function(){
		jQuery('#callback-popup').fadeOut('slow',function(){
			jQuery('#popup-overlay').hide();
		});

		return false;
	});

	/*
	jQuery('#callback_form').submit(function(){
		var error_found = false;

		if (jQuery(this).find('input[name="name_F"]').val() == '')
		{
			error_found = true;
			jQuery(this).find('input[name="name_F"]').showError();
		}

		if (jQuery(this).find('input[name="telephone_F"]').val() == '')
		{
			error_found = true;
			jQuery(this).find('input[name="telephone_F"]').showError();
		}

		if (jQuery(this).find('input[name="email_F"]').val() == '')
		{
			error_found = true;
			jQuery(this).find('input[name="email_F"]').showError();
		}

		if (!error_found)
		{
			return true;
		}
		else
		{
			return false;
		}
	});
	*/

	if (url.indexOf('/index.cfm?complete=callback') != -1)
	{
		jQuery('#popup-overlay').show();
		jQuery('#callback_form').hide();
		jQuery('.plate-22-inner h2.title')
				.next()
				.hide()
				.next()
				.show();
		jQuery('#callback-popup').fadeIn();
	}

	// Contact us
	jQuery('#contact-link').click(function(){
		jQuery('html, body').animate({scrollTop:0}, 'slow');
		jQuery('#popup-overlay').show();

		var oForm = jQuery('#contact_form');

		oForm
			.show()
			.clearForm();

		oForm.find('.error_message').hide();
		oForm.find('.loading_img').hide();
		oForm.find('input[name="name_F"]').clearError();
		oForm.find('input[name="telephone_F"]').clearError();
		oForm.find('input[name="email_F"]').clearError();
		oForm.find('textarea[name="enquiry_F"]').clearError();

		jQuery('#contact-popup').find('h2.title').next().next().hide();

		jQuery('#contact-popup').fadeIn('slow');

		return false;
	});

	jQuery('#contact-popup').find('.close').click(function(){
		jQuery('#contact-popup').fadeOut('slow',function(){
			jQuery('#popup-overlay').hide();
		});

		return false;
	});

	jQuery('#contact_form').submit(function(){
		var error_found = false;

		if (jQuery(this).find('input[name="name_F"]').val() == '')
		{
			error_found = true;
			jQuery(this).find('input[name="name_F"]').showError();
		}

		if (jQuery(this).find('input[name="telephone_F"]').val() == '')
		{
			error_found = true;
			jQuery(this).find('input[name="telephone_F"]').showError();
		}

		if (jQuery(this).find('input[name="email_F"]').val() == '')
		{
			error_found = true;
			jQuery(this).find('input[name="email_F"]').showError();
		}

		if (jQuery(this).find('input[name="enquiry_F"]').val() == '')
		{
			error_found = true;
			jQuery(this).find('input[name="enquiry_F"]').showError();
		}

		if (!error_found)
		{
			return true;
		}
		else
		{
			return false;
		}
	});

	if (url.indexOf('/index.cfm?complete=contact') != -1)
	{
		jQuery('#popup-overlay').show();
		jQuery('#contact_form').hide();
		jQuery('.plate-2-inner h2.title')
				.next()
				.hide()
				.next()
				.show();
		jQuery('#contact-popup').fadeIn();
	}
});
