
function validate( frm ) {

    var returnValue = false;

    if ( BlankField( frm.name.value ) ) {
        alert( "Please enter your name." );
        frm.name.focus();
    } else if ( BlankField( frm.address.value ) ) {
        alert( "Please enter your address." );
        frm.address.focus();
    } else if ( BlankField( frm.city.value ) ) {
        alert( "Please enter your city." );
        frm.city.focus();
    } else if ( BlankField( frm.state.value ) ) {
        alert( "Please enter your state." );
        frm.state.focus();
    } else if ( BlankField( frm.zip.value ) ) {
        alert( "Please enter your zip." );
        frm.zip.focus();
    } else if ( !ValidEmail( frm.email.value ) ) {
        alert( "Please enter a valid email address." );
        frm.email.focus();
    } else {
        returnValue = true;
        frm.submit.disabled = true;
    }
    return returnValue;
}

function setDatePicker(input) {
	if(($(input).hasClass('date-begin')||$(input).hasClass('date-end'))&&$(input).hasClass('update-blocks')){
		return {
			minDate: ( setDates(input, 1) ), 
			maxDate: ( setDates(input, 0) ),
			onClose: function() { getRange(input); }
		}; 
	} else if(($(input).hasClass('date-begin')||$(input).hasClass('date-end'))&&$(input).hasClass('netbooker')){
		return {
			minDate: ( setDates(input, 1) ), 
			maxDate: ( setDates(input, 0) ),
			onSelect: function() { splitDates(input); }
		}; 
	} else if($(input).hasClass('date-begin')||$(input).hasClass('date-end')) {
		return {
			minDate: ( setDates(input, 1) ), 
			maxDate: ( setDates(input, 0) )
		};				
	} 
} 

function setDates(input, minDate) {
	if( ($(input).hasClass('date-begin') && !$('.date-end', $(input).parent().next()).length) ||
		($(input).hasClass('date-end') && !$('.date-begin', $(input).parent().prev()).length) ) {
		return minDate ? new Date() : null;
	} else if( $(input).hasClass('date-begin') ) {
		return minDate ? new Date() : ($('.date-end', $(input).parent().next()).datepicker("getDate") ? new Date($('.date-end', $(input).parent().next()).datepicker("getDate").getTime() - 1000*60*60*24) : null);
	} else if( $(input).hasClass('date-end') ) {
		return minDate ? ($('.date-begin', $(input).parent().prev()).datepicker("getDate") ? new Date($('.date-begin', $(input).parent().prev()).datepicker("getDate").getTime() + 1000*60*60*24) : '+1d' ) : null;
	}
}

function splitDates(input) {
	var start = $(input).hasClass('date-end')?$('.date-begin', $(input).parent().prev()).datepicker("getDate"):$(input).datepicker("getDate");
	var end = $(input).hasClass('date-end')?$(input).datepicker("getDate"):$('.date-end', $(input).parent().next()).datepicker("getDate");

	if($(input).hasClass('date-begin')) {
		$('#arriveMonth').val( $.datepicker.formatDate('mm', $(input).datepicker("getDate")) );
		$('#arriveDay').val( $.datepicker.formatDate('dd', $(input).datepicker("getDate")) );
		$('#arriveYear').val( $.datepicker.formatDate('yy', $(input).datepicker("getDate")) );
	}
	if($(input).hasClass('date-end')) {
		$('#departMonth').val( $.datepicker.formatDate('mm', $(input).datepicker("getDate")) );
		$('#departDay').val( $.datepicker.formatDate('dd', $(input).datepicker("getDate")) );
		$('#departYear').val( $.datepicker.formatDate('yy', $(input).datepicker("getDate")) );
	}
	
	if(start&&end) {
		var one_day=1000*60*60*24;
		var days = Math.ceil((end.getTime()-start.getTime())/(one_day))	
		$('#numberOfNights').val(days);
	}
}

function getRange(el) {
	var form = $(el).parents('form');
	var start = $(el).hasClass('date-end')?$('.date-begin', $(el).parent().prev()).datepicker("getDate"):$(el).datepicker("getDate");
	var end = $(el).hasClass('date-end')?$(el).datepicker("getDate"):$('.date-end', $(el).parent().next()).datepicker("getDate");

	if(start&&end) {
		var one_day=1000*60*60*24;
		var days = Math.ceil((end.getTime()-start.getTime())/(one_day))	
		$('#room-requirements').show();
		createBlock(start, days, form);
		createMeeting(start, days, form);
	}
}


  $(function() {

	var DOMAIN_TRACKING = "synxis.com";
	if ( DOMAIN_TRACKING ) {
		var trackDomains = DOMAIN_TRACKING.split(',');
		for (var i=trackDomains.length-1;i>=0;i--) {
			$('a[href*="'+trackDomains[i]+'"], form[action*="'+trackDomains[i]+'"]').addClass('track-crossdomain');
		}
	}

	// Tracking email links / set email as the name
	$('a[href^="mailto:"]').each(function(){
		$(this).attr('name',$(this).attr('href')).addClass('track');
	});
	
	$('.track, .track-crossdomain').each(function() {
		var tag = '';
		if ( this.name ) {
			tag = '/'+this.name.replace(/:/g,'/');
		}
		if($(this).is('form')) {
			var a = $(this).attr('action');
			$(this).submit(function() {
				if (tag) { pageTracker._trackEvent('form', 'submit', tag); }
				if ( $(this).hasClass('track-crossdomain') ) {
					$(this).attr({action:a+'?'+$(this).serialize(), method:'post'});
					pageTracker._linkByPost(this);
				}
			});
		} else {
			$(this).click(function(e) {
				if (tag) { pageTracker._trackEvent('link','click', tag); }
				if ( $(this).hasClass('track-crossdomain') ) {
					e.preventDefault();
					var l = pageTracker._getLinkerUrl(this.href);
					$(this).attr('target')=='_blank'?window.open(l,''):window.location=l;
				}
			});
		}
	});
	
  });
  
(function($) {		  
	$.fn.initializeForm = function(options) {
		var defaults = {
			calText: 'Click to select a date',
			dateFormat: 'mm/dd/yyyy'
		},
		opts = $.extend(defaults, options),
		focusIsSupported = (function(){
		    // Create an anchor + some styles including ':focus'.
		    // Focus the anchor, test if style was applied,
		    // if it was then we know ':focus' is supported.
		     
		    var ud = 't' + +new Date(),
		    anchor = $('<a id="' + ud + '" href="#"/>').css({top:'-999px',position:'absolute'}).appendTo('body'),
		    style = $('<style>#'+ud+'{font-size:10px;}#'+ud+':focus{font-size:1px !important;}</style>').appendTo('head'),
		    supported = anchor.focus().css('fontSize') !== '10px';
		    anchor.add(style).remove();
		return supported;

		})();

		return this.each(function() {
			var form = $(this);

			if ( !focusIsSupported ) {
				$(".textfield")
					.focus(function() { $(this).css({backgroundColor: "#fdfcfa"}); })
					.blur(function() { $(this).css({backgroundColor: "#ffffff"}); });
				$('.required', form).each(function() {
					$(this).prev().addClass('ie-icon-required');
				}); 
			}
			
			$(".date-picker", form).each(function() {
				$(this).datepicker({ 
					beforeShow: setDatePicker,
					buttonImage: "/img/icon-cal.gif", 
					buttonImageOnly: true ,
					buttonText: opts.calText,
					duration: "fast",
					gotoCurrent: true,
					hideIfNoPrevNext: true,
					numberOfMonths: 2,
					showAnim: "blind",
					showOn: "both"
				});
				
				if($(this).val()==opts.dateFormat) {
					$(this).val('');
				}
				else {
					if($(this).hasClass('date-end')&&$(this).hasClass('update-blocks'))
						getRange(this);
				}
			});
			
			// hide all "other" fields
			$("ul.checkboxgroup input.other", form).not(':radio, :checkbox').hide();
			$("ul.checkboxgroup input:not(:text)", form).click(function() {
				$('.other', $(this).parents('ul')).siblings().removeClass('required').filter(':text').hide();
				if($(this).is(':checked')) {
					$('.validation-error', $(this).parents('ul')).removeError(form);
					if($(this).hasClass('other'))
						$(this).siblings().addClass('required').show();
				}
			});
			
			$("div.select-other", form).hide();
			// if "Other" is selected, then show it's "other" field
			$("select.select-other", form).change( function () {
				var el = $(this).parent().next()
				if($(this).val() == "--") {
					el.show().children('label, input').addClass("required");
				} else {
					el.hide().children().removeClass("required").find("input").val("");
					$('.validation-error', el).removeError(form);
				}
			});

		});
	};
	
	$.fn.formValidate = function(options) {
		var defaults = {
			errorStart: 'A valid ',
			errorEnd: ' is required.'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
			var form = $(this);
			$('.submit', form).click(function() {
				var _errors = '';
				//Removes validation errors from previous submit
				$('.validation-error', form).removeError(form);	
				
				$('.required', form).each(function() {
					var valid = true;
					
					if($(this).is(':input')) {
						var o = $.trim($(this).val());
						
						if($(this).hasClass('email')) {
							// if it's an email address make sure the email is valid using both regular expressions
							valid = /^[a-z0-9_+.-]+\@(?:[a-z0-9-]+\.)+[a-z0-9]{2,4}$/i.test(o);
							if($(this).hasClass('confirm-email')&&valid) {
								var prev = $.trim($(this).parents('.field').prev().children('.email').val());
								if(o!=prev)
									valid=!valid;
							}
						} else if (o.replace(/(?:^\s+)|(?:\s+$)/g,'').length < 1) {
							// if not an email address take out funky characters and see if its still blank
							valid = !valid;
						}
					} else if($(this).is('ul')) {
						valid = false;
						
						$(':radio, :checkbox', this).each(function() {
							if($(this).is(':checked'))
								valid = true;
						});
					}
					
					if(!valid) {
						if(form.hasClass('inline')) {
							$(this).parents('.field').children('label').addClass('validation-error');
						} else {
							var warn = $('<img />').attr('src', '/images/icon-warning.gif').addClass('validation-error');
							
							if( $(this).is('ul') )
								$('li:first', this).append(warn);
							else 
								$(this).parent().append(warn);
						}
													
						if(_errors == '')
							_errors = $(this).attr("id");
					}
				});
				if(_errors.length) {
					var lbl = $('#'+_errors).parents('.field').find('label:eq(0)');
					alert(lbl.attr('title').length?lbl.attr('title'):opts.errorStart + lbl.html().replace(/[*:]/g, '') + opts.errorEnd)
					if ($('#'+_errors).is('ul'))
						$('#'+_errors, form).find('label:eq(0)').focus();
					else
						$('#'+_errors, form).focus();
					return false;			
				}
			});
		});
	};
	
})(jQuery);

jQuery(function($){
	$('form').initializeForm();
	$('.validate').formValidate(); 
	
	$('#console-promo-toggle').click(function(){
		$('#promo-codes').toggle();
		$('#console-promo-toggle').toggleClass('active-promo');
	});
});
