/*
 * belk.bridal.js
 * Belk.com - Bridal javascript
 * Copyright Belk, Inc.
 */
belk.web.bridal = {
	init: function(){
		belk.web.bridal._setupAccordion();
		belk.web.bridal._setupRadioButtonDependencies();
		belk.web.bridal._setupAjaxStoreDropdowns();
		belk.web.bridal._setupDateValidation();
	},
	_setupAccordion:function(){
		if (jQuery.fn.accordion) {
			$("#registry_accordion").accordion({
				header: "h2",
				autoheight: false,
				active: false
			});
			$("#registry_accordion").accordion('activate', 0);
		}
	},
	_setupRadioButtonDependencies:function(){
		$('#rad_bride,#rad_groom').click(function(){
			var isChecked=$(this).blur().is(':checked');
			if(isChecked){
				$('#spn_registry_num').hide();
				$('#spn_first,#spn_last').show();
			}
		});
		$('#rad_registry_num').click(function(){
			var isChecked=$(this).blur().is(':checked');
			if(isChecked){
				$('#spn_registry_num').show();
				$('#spn_first,#spn_last').hide();
			}
		});
	},
	_setupAjaxStoreDropdowns:function(){
		belk.web.config.ajax_methods.getMyBridalStores.handlers.onAjaxReturn=function(resp){
			if(resp.success && resp.stores  && resp.stores.length>0){
				belk.util.clearDD('#sel_your_store');
				jQuery.each(resp.stores,function(i,store){
					belk.util.addValue2DD(store.value,store.text,'#sel_your_store');
				});
			}
			return false;
		};
		belk.web.config.ajax_methods.getMyBridalStores.handlers.onPreAjax=function(){ // make sure "city AND state" OR "zip" are filled
			var city=$('#txt_your_city').val();
			var state=$('#sel_your_state').val();
			var zip = $('#txt_your_zip').val();
			if((city && city!='' && state && state!='') || (zip && zip!='') ){
				return true;
			}
			return false;
		};
		
		belk.web.config.ajax_methods.getOtherBridalStores.handlers.onAjaxReturn=function(resp){
			if(resp.success && resp.stores  && resp.stores.length>0){
				belk.util.clearDD('#sel_other_store');
				jQuery.each(resp.stores,function(i,store){
					belk.util.addValue2DD(store.value,store.text,'#sel_other_store');
				});
			}
			return false;
		};
		belk.web.config.ajax_methods.getOtherBridalStores.handlers.onPreAjax=function(){ // make sure "city AND state" OR "zip" are filled
			var city=$('#txt_other_city').val();
			var state=$('#sel_other_state').val();
			var zip = $('#txt_other_zip').val();
			if((city && city!='' && state && state!='') || (zip && zip!='') ){
				return true;
			}
			return false;
		};
	},
	_setupDateValidation: function(){
		$('form').each(function(){
			var $dateFields = $('input.datefield', $(this));
			if($dateFields.length>0){
				$(this).submit(function(ev){
					$dateFields.each(function(){
						var $this = $(this);
						var val = $this.val();
						if(val && val!=''  && val!='MM/DD/YYYY') {
							// validate the date
							var date = Date.fromString(val); // this assumes that we are using jquery.datepicker which defines this
							var now = new Date();
							now.setHours(0,0,0,0);
							if(!date || date<now){
								belk.util.alert('Please enter a valid future date.','Invalid Date');
								ev.preventDefault();
								return false;
							}
						}
					});
					return true;
				});
			}
		});	
	}
};

$(document).ready(belk.web.bridal.init);