$(document).ready(function() {
   // put all your jQuery goodness in here.
   // Do not instantiate elements until the document has finished loading

   	var cookieState = $.cookie('state');
   	var redirectOnce;
   	//alert(cookieState);
   	   	   	
   	if (cookieState == null) {
   		tb_show("Shipping State Verification","TB_inline?height=150&width=300&inlineId=hiddenModalContent");
   	}
   	
   	else if (cookieState != null) {
   		$("#ship-to").show();
   		$(".ship-to-state").html(cookieState + "&nbsp;<a class='state-change' href='#'>Change state</a>");
   		$("#state option[value='"+ cookieState +"']").attr('selected', 'selected');
   	}
   	
   	else if (cookieState == 'MA' || cookieState == 'CA' && redirectOnce == 'null' ) {
   		location.href = "/ComplianceRedirect.aspx"
   		redirectOnce = '1';
   		$("#ship-to").show();
   	}
   	
   	
   	$(".state-change").click(function () { 
    	tb_show("Shipping State Verification","TB_inline?height=150&width=300&inlineId=hiddenModalContent");
    	return false;
    });


	
	//Accordion functions
	$('.accordion-list:not(:first)').hide();
	
	$('.accordion-list:first').show();
	
	$('.accordion-header:first').addClass('active');	
	$('.accordion-list:first').addClass('content-active');
	
	$('.accordion-header').click(function () {
		$('.accordion-list:visible').slideUp().prev().removeClass('active');
		$('.accordion-list:visible').removeClass('content-active');
		$(this).addClass('active').next().slideDown();
		$('.accordion-list').addClass('content-active');
		
	});
	


		// Accordion - uses jQuery UI includes
		//$("#accordion").accordion({ header: "h3", autoHeight: false });
				
		// Main Navigation Drop Down Menu
		$('li.sitenavTab').hover(function() {
			$(this).find('.sitenavDropDown').show();
			//$(this).find('a.sitenavLink').css('backgroundColor', '#c1d34b');
			$(this).find('a.sitenavLink').addClass("sitenavHover");

		}, function() {
			$(this).find('.sitenavDropDown').hide();
			//$(this).find('a.sitenavLink').css('backgroundColor', '#830136');
			$(this).find('a.sitenavLink').removeClass("sitenavHover");
		});
		
		//Sign Up Form Validation
    	$(".email-signup #submit").click(function(){
        
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var email = $("#email-signup").val();
				
		if(email == '') {
			$(".email-error").replaceWith('<div class="email-error"><p>You forgot to enter an email address.</p></div>');
		} else if(!emailReg.test(email)) {
			$(".email-error").replaceWith('<div class="email-error"><p>Please enter a valid email address.</p></div>');
		} else {
		  		$(".track").html('<img src="http://directwines.bm23.com/public/actionpage/subscriber_add/?fn=Mail_ActionPage_FormResponse&pid=4lweygh2lk1peq9twrr70pojexuh8&ssid=12512&email=' +  email + '&list_data[208133]=checked" width="0" height="0" border="0" alt=""/>');
		  		$(".email-signup").html('<p>Thank you, you have been subscribed.</p>')
		}
		return false;
		});
		
	});
	
	// Wine Shop 4 Panel Feature - uses jQuery UI includes
	$(function() {
		$("#tabs").tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 7000);
		
	// Search Results - Mouseover Image - looks for small image something_s.jpg and shows
	// large image something_l.jpg in the mouse over. Settings can be found in jquery.thumbhover.js
	$("img[src*='_l.jpg']").thumbPopup({
				imgSmallFlag: "_l",
				imgLargeFlag: "_l"
		});
	});
	
	

$(document).ready(function() {

$.validator.setDefaults({
	success: "valid"
});

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number - no spaces");
	
	// Registration Form Validation - using jquery.validate.min.js			
	$(".registration #aspnetForm").validate({
		rules: {
			ctl00$cphMaster$txtFirstName: "required",
			ctl00$cphMaster$txtLastName: "required",
			ctl00$cphMaster$txtEmailID: {
    			email: true,
    			required:true
    		},
    		ctl00$cphMaster$txtConfirmEmailID: {
    			equalTo: "#ctl00_cphMaster_txtEmailID",
    			email: true,
    			required: true
    		},
    		ctl00$cphMaster$txtPassword: {
    			minlength: 6,
    			required: true
    		},
    		ctl00$cphMaster$txtConfirmPassword: {
      			equalTo: "#ctl00_cphMaster_txtPassword",
      			minlength: 6,
      			required: true
    		},
    		ctl00$cphMaster$txtAddress1: {
    			required: true
    		},
    		ctl00$cphMaster$txtCity: {
    			required: true
    		},
    		ctl00$cphMaster$ddlState: {
    			required: true
    		},
    		ctl00$cphMaster$txtZipCode: {
    			required:true,
    			minlength: 5
    		},
    		ctl00$cphMaster$txtPhone: {
    			required: true,
      			phoneUS: true
    		}	
  		},
  		messages: {
  		ctl00$cphMaster$txtFirstName: "Please specify your first name",
  		ctl00$cphMaster$txtLastName: "Please specify your last name",
  		ctl00$cphMaster$txtConfirmEmailID: "Please confirm your email address",
  		ctl00$cphMaster$txtPassword: {
  		required: "Please enter a password",
  		minlength: "Your password needs to be at least 6 letters"
  		},
  		ctl00$cphMaster$txtConfirmPassword: {
  		required: "Please confirm your password",
  		minlength: "Your password needs to be at least 6 letters"
  		},
  		ctl00$cphMaster$txtAddress1: "Please enter your address",
  		ctl00$cphMaster$txtCity: "Please enter your city",
  		ctl00$cphMaster$txtZipCode: "Please enter your zip code"
  		}
	});
	
   
   function fnValidateJquery(){
	$(".shipping-address-form #aspnetForm").validate({debug: true});
   }
	
});	
	
