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();
};

function SetVehicleAndConditionType(condition_type, vehicle_type)
{
	if (condition_type == 'used')
	{
		jQuery('#vehicle_used').attr('checked', 'checked');
		jQuery('#vehicle_new').removeAttr('checked');
		jQuery('#vehicle_condition_type').val('0');
	}
	else if (condition_type == 'new')
	{
		jQuery('#vehicle_new').attr('checked', 'checked');
		jQuery('#vehicle_used').removeAttr('checked');
		jQuery('#vehicle_condition_type').val('1');
	}

	// Set make/model
	SetMakeModel(vehicle_type, condition_type);
}

function SetMakeModel(vehicle_type, condition_type)
{
	jQuery('select[name="Make"]').html('<option value="Any" selected="SELECTED">Any Make</option>');
	jQuery('select[name="Model"]').html('<option selected="SELECTED" value="Any Model">Any Model</option>');

	if (vehicle_type == 'car')
	{
		jQuery('select[name="Make"]').change(function(){
			if (condition_type == 'new')
			{
				NewChangeModels();
			}
			else
			{
				ChangeModels();
			}
		});

		ActiveModel = tempActiveModel;

		if (condition_type == 'new')
		{
			NewMakesInit();
		}
		else
		{
			MakesInit();
		}
	}
	else
	{
		jQuery('select[name="Make"]').change(function(){
			if (condition_type == 'new')
			{
				NewChangeModelsVan();
			}
			else
			{
				UsedChangeModelsVan();
			}
		});

		ActiveModelVan = tempActiveModelVan;

		if (condition_type == 'new')
		{
			NewMakesInitVan();
		}
		else
		{
			UsedMakesInitVan();
		}
	}

	if (currentVehicleType != vehicle_type || (currentVehicleType == vehicle_type && currentConditionType != condition_type))
	{
		jQuery('select[name="Make"] option[value="Any Make"]').attr('selected', 'selected');
		jQuery('select[name="Model"] option[value="Any Model"]').attr('selected', 'selected');
	}
}

var currentVehicleType = 'van';
var currentConditionType = 'used';
var selectedVehicleType = 'van';

jQuery(document).ready(function(){

	var url = location.href;

	// Set Van search by default (vehicletype == '7' or vehicletype == 'Any')
	jQuery('#van-search-tab').addClass('act');
	jQuery('#car-search-tab').removeClass('act');
	jQuery('#vehicle_type').val('7');

	jQuery('#vehicle_text_used').html('Used vans');
	jQuery('#vehicle_text_new').html('New vans');

	SetVehicleAndConditionType('used', currentVehicleType);

	// Set menu/breadcrumbs, etc.
	if (url.indexOf('/search.cfm') != -1 || url.indexOf('/vehicle.cfm') != -1)
	{
		jQuery('#item-2').addClass('act');

		if (url.indexOf('/search.cfm') != -1)
		{
			jQuery('#breadcrumb').html('<a href="http://www.vanandcartrader.co.uk/index.cfm">Van and Car Trader</a> > <span class="b">Showroom</span>');
			jQuery('#stock').find('.aeStdTable tr td.aeText1').each(function(){
				if (jQuery(this).html() == '&nbsp;')
				{
					jQuery(this).parent().parent().remove();
				}
			});

			// Set car/van tabs
			var arrVehicleType = url.match(/vehicletype=(.*?)\&/i);
			
			if (arrVehicleType != null && arrVehicleType.length > 0)
			{
				var vehicleType = unescape(arrVehicleType[1]);
				
				// Car search
				if (vehicleType == '1')
				{
					jQuery('#car-search-tab').addClass('act');
					jQuery('#van-search-tab').removeClass('act');

					jQuery('#vehicle_type').val('1');
					jQuery('#vehicle_text_used').html('Used cars');
					jQuery('#vehicle_text_new').html('New cars');
					
					currentVehicleType = 'car';
					selectedVehicleType = 'car';
				}
			}

			// Set used/new vehicle
			var arrConditionType = url.match(/new=(.*?)\&/i);
			
			if (arrConditionType != null && arrConditionType.length > 0)
			{
				var conditionType = arrConditionType[1];
				
				// New cars/vans search
				if (conditionType == '1')
				{
					currentConditionType = 'new';

					if (jQuery('#aeSk1CatHeader h1 a').attr('name') == 'l_Used Cars' || jQuery('#aeSk1CatHeader h1 a').text() == 'Used Cars')
					{
						jQuery('#aeSk1CatHeader h1 a').text('New Cars');
					}
					else if (jQuery('#aeSk1CatHeader h1 a').attr('name') == 'l_Used Vans' || jQuery('#aeSk1CatHeader h1 a').text() == 'Used Vans')
					{
						jQuery('#aeSk1CatHeader h1 a').text('New Vans');
					}
					
					jQuery('#vehicle_new').attr('checked', 'checked');
					jQuery('#vehicle_used').removeAttr('checked');
				}
			}

			SetVehicleAndConditionType(currentConditionType, currentVehicleType);
		}
		else
		{
			jQuery('#breadcrumb').html('<a href="http://www.vanandcartrader.co.uk/index.cfm">Van and Car Trader</a> > <a href="search.cfm">Showroom</a> > <span class="b">Vehicle Details</span>');
		}
	}
	else if (url.indexOf('/finance.cfm') != -1)
	{
		jQuery('#item-3').addClass('act');
		jQuery('#breadcrumb').html('<a href="http://www.vanandcartrader.co.uk/index.cfm">Van and Car Trader</a> > <span class="b">Finance</span>');
	}
	else if (url.indexOf('/warranty.cfm') != -1)
	{
		jQuery('#item-4').addClass('act');
		jQuery('#breadcrumb').html('<a href="http://www.vanandcartrader.co.uk/index.cfm">Van and Car Trader</a> > <span class="b">Warranty</span>');
	}
	else if (url.indexOf('/partexchange.cfm') != -1)
	{
		jQuery('#item-5').addClass('act');
		jQuery('#breadcrumb').html('<a href="http://www.vanandcartrader.co.uk/index.cfm">Van and Car Trader</a> > <span class="b">Part Exchange</span>');
	}
	else if (url.indexOf('/blog.cfm') != -1)
	{
		jQuery('#item-6').addClass('act');
		jQuery('#breadcrumb').html('<a href="http://www.vanandcartrader.co.uk/index.cfm">Van and Car Trader</a> > <span class="b">Blog</span>');
	}
	else if (url.indexOf('/contact.cfm') != -1)
	{
		jQuery('#item-7').addClass('act');
		jQuery('#breadcrumb').html('<a href="http://www.vanandcartrader.co.uk/index.cfm">Van and Car Trader</a> > <span class="b">Contact</span>');
	}
	else if (url.indexOf('/googlemap.cfm') != -1)
	{
		jQuery('#breadcrumb').html('<a href="http://www.vanandcartrader.co.uk/index.cfm">Van and Car Trader</a> > <span class="b">Location</span>');
	}
	else if (url.indexOf('/index.cfm') != -1 || url.indexOf('/vanandcartrader/') != -1 || url == '/')
	{
		jQuery('#item-1').addClass('act');
		jQuery('#breadcrumb').html('<a href="http://www.vanandcartrader.co.uk/index.cfm">Van and Car Trader</a>');
	}

	jQuery('#car-search-tab-link').click(function(){
		selectedVehicleType = 'car';
		jQuery(this).parent().addClass('act');
		jQuery('#van-search-tab').removeClass('act');

		jQuery('#vehicle_type').val('1');
		jQuery('#vehicle_text_used').html('Used cars');
		jQuery('#vehicle_text_new').html('New cars');

		// Set condition type
		if (currentVehicleType == 'car')
		{
			SetVehicleAndConditionType(currentConditionType, 'car');
		}
		else
		{
			SetVehicleAndConditionType('used', 'car');
		}
		
		if (jQuery('#van_scripts'))
		{
			jQuery('#van_scripts').remove();
		}

		return false;
	});
	jQuery('#van-search-tab-link').click(function(){
		selectedVehicleType = 'van';
		jQuery(this).parent().addClass('act');
		jQuery('#car-search-tab').removeClass('act');

		jQuery('#vehicle_type').val('7');
		jQuery('#vehicle_text_used').html('Used vans');
		jQuery('#vehicle_text_new').html('New vans');

		// Set condition type
		if (currentVehicleType == 'van')
		{
			SetVehicleAndConditionType(currentConditionType, 'van');
		}
		else
		{
			SetVehicleAndConditionType('used', 'van');
		}

		return false;
	});

	jQuery('#vehicle_used').click(function(){
		jQuery('#vehicle_condition_type').val('0');
		
		SetVehicleAndConditionType('used', selectedVehicleType);
	});
	jQuery('#vehicle_new').click(function(){
		jQuery('#vehicle_condition_type').val('1');
		
		SetVehicleAndConditionType('new', selectedVehicleType);
	});

	// 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 .plate-2-inner h2.title').next().next().hide();

		jQuery('#callback-popup').fadeIn('slow');

		return false;
	});

	jQuery('#callback-popup .plate-2-inner .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-2-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 .plate-2-inner h2.title').next().next().hide();

		jQuery('#contact-popup').fadeIn('slow');

		return false;
	});

	jQuery('#contact-popup .plate-2-inner .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();
	}

	//location
	if (url.indexOf('/googlemap.cfm') != -1)
	{
		var content = jQuery('#ae1Column1Container').html();
		var body = '<div id="aeBodyContainer" class="aeBodyContainer">';
		body += content;
		body += '	</div>';

		jQuery('#ae1Column1Container').remove();
		jQuery('#header').after(body);
	}
});
