﻿<!--
var root = "/";
$(document).ready(function () {
	
	$('.menu #topNav li').not('ul.submenu').hover(function () {
		$(this).children('ul').css('display', 'block');
	},
	function () {
		$(this).children('ul').css('display', 'none');
	});
	
	$('.menu #topNav li ul li').hover(function () {
		$(this).children('ul').css('display', 'block');
	},
	function () {
		$(this).children('ul').css('display', 'none');
	});
	
	$('.menu #topMenu li').hover(function () {
		$(this).children('ul').stop().css('height','auto').slideDown(400);
	},
	function () {
		$(this).children('ul').stop().css('height','auto').slideUp(500);
	});
	
	$('.menu #topMenu .dealer').append('<div class="dealer_ro"><\/div>');
	$('.menu #topMenu .dealer').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});

	$('.menu #topMenu .outlet').append('<div class="outlet_ro"><\/div>');
	$('.menu #topMenu .outlet').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});

	$('.menu #topMenu .pro').append('<div class="pro_ro"><\/div>');
	$('.menu #topMenu .pro').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});

	$('.menu #topMenu .apparel').append('<div class="apparel_ro"><\/div>');
	$('.menu #topMenu .apparel').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});

	$('.menu #topMenu .warranty').append('<div class="warranty_ro"><\/div>');
	$('.menu #topMenu .warranty').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});

	$('.menu #topNav .home').append('<div class="home_ro"><\/div>');
	$('.menu #topNav .home').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});
	
	$('.menu #topNav .about').append('<div class="about_ro"><\/div>');
	$('.menu #topNav .about').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});
	
	$('.menu #topNav .products').append('<div class="products_ro"><\/div>');
	$('.menu #topNav .products').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});
	
	$('.menu #topNav .latest').append('<div class="latest_ro"><\/div>');
	$('.menu #topNav .latest').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});
	
	$('.menu #topNav .brochures').append('<div class="brochures_ro"><\/div>');
	$('.menu #topNav .brochures').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});
	
	$('.menu #topNav .faqs').append('<div class="faqs_ro"><\/div>');
	$('.menu #topNav .faqs').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});
	
	$('.menu #topNav .application').append('<div class="application_ro"><\/div>');
	$('.menu #topNav .application').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});
	
	$('.menu #topNav .contact').append('<div class="contact_ro"><\/div>');
	$('.menu #topNav .contact').hover(function () {
			$(this).children('div').fadeIn(750).css('cursor', 'pointer');
		}, 
		function() {
			$(this).children('div').fadeOut(200).css('cursor', 'default');	
	});
	

	var zip = $('#zip_code');
	var name = $("#name"),
			email = $("#email"),
			id = $("#productid"),
			phone = $("#phone"),
			allFields = $([]).add(name).add(email).add(zip).add(phone),
			tips = $("#dialog-form .retMsg");

	function updateTips(t) {
		tips
			.text(t)
			.addClass('ui-state-highlight');
			setTimeout(function() {
			tips.removeClass('ui-state-highlight', 1500);
		}, 500);
	}
	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be 5 digits.");
			return false;
		} else {
			return true;
		}

	}
	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}
	$("#dialog-form").dialog({
		autoOpen: false,
		height: 250,
		width: 350,
		resizable: false,
		modal: true,
		buttons: {
			'Find Closest Dealer': function() {
				var bValid = true;

				bValid = bValid && checkLength(zip,"zip_code",5,5);
				bValid = bValid && name != '';
				bValid = bValid && checkRegexp(zip,/^[0-9]+$/,"Please enter the 5 digit zip code containing numbers 0-9");
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. example@example.com");

				if (bValid) {
					$.get(root+'_scripts/find-dealer.php?z='+zip.val()+'&e='+email.val()+'&n='+name.val()+'&p='+phone.val()+'&id='+id.val(), function(data) {
						window.location.href = 'dealer.php';
					});

					//$(this).dialog('close');
				}
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});

	$('.orderDealer').click(function() {
		$("#dialog-form").dialog('open');
	});
	$('.back a').hover(function () {
		$(this).css('background', "url('_images/lArrow_ro.png') no-repeat");
	},
	function() {
		$(this).css('background', "url('_images/lArrow.png') no-repeat");
	});
	$('.forward a').hover(function () {
		$(this).css('background', "url('_images/rArrow_ro.png') no-repeat");
	},
	function() {
		$(this).css('background', "url('_images/rArrow.png') no-repeat");
	});
	$.get(root+'_scripts/year.php', function(data) {
		$('#year').html(data);
		$.get(root+'_scripts/make.php?yr=NULL', function(data) {
			$('#make').html(data);
			$.get(root+'_scripts/model.php?yr=NULL&mk=NULL', function(data) {
				$('#model').html(data);
			});
		});
		if($('#model').val() != "" && $('#make').val() != "" && $('#year').val() != "")
			$('#goSearch').css('display','block');
		else
			$('#goSearch').css('display','none');
	});
	$('#year').change(function() {
		$('#make').html('<option>Loading makes...</option>');
		$.get(root+'_scripts/make.php?yr='+$('#year').val(), function(data) {
			$('#make').html(data);
			$.get(root+'_scripts/model.php?yr=NULL&mk=NULL', function(data) {
				$('#model').html(data);
			});
		});
		if($('#model').val() != "" && $('#make').val() != "" && $('#year').val() != "")
			$('#goSearch').css('display','block');
		else
			$('#goSearch').css('display','none');
	});
	$('#make').change(function() {
		$('#model').html('<option>Loading models...</option>');
		$.get(root+'_scripts/model.php?yr='+$('#year').val()+'&mk='+$('#make').val(), function(data) {
			$('#model').html(data);
		});
	});
	$('#model').change(function() { 
		if($('#model').val() != "" && $('#make').val() != "" && $('#year').val() != "")
			$('#goSearch').css('display','block');
		else
			$('#goSearch').css('display','none');
	});
});
function goSearch()
{
	var search = 'http://www.ranchhand.com/search.php?year='+$('#year').val()+'&make='+$('#make').val()+'&model='+$('#model').val();
	window.location.href=search
}

//-->

