
$(document).ready( function() {

	$('#searchbydate_btn').click(function() {
		$('#searchbydate').slideDown('slow');
	});
	$('#searchbydate a.go').click(function() {
		$('#searchbydate').slideUp('slow');
	});

	$('#searchbytext_btn').click(function() {
		$('#searchbytext').slideDown('slow');
	});
	$('#searchbytext a.go').click(function() {
		$('#searchbytext').slideUp('slow');
	});



	$('input, textarea').focus( function() {
		if( $(this).val() == $(this).attr('title') ) {
			$(this).val('');
		}
	}).blur( function() {
		if( $(this).val() == '' ) {
			$(this).val( $(this).attr('title') );
		}
	});

	/// phone input	
	//$("input[name*='phone']").blur( function() { fixPhone(this); } );
	$("input[name*='phone']").keyup(function(e){
		if($(this).val().length >= $(this).attr('maxlength') && e.keyCode != '9') $(this).next('input').focus();
	});
	
	
	/// Disable state validation if not US
	$("select#country").change(function(){
		if($(this).val() != 'US') $("select#state").removeAttr('verify');
	});


	///
	/// expand session details
	///
	$('.expand-btn').click(function() {
		if( $(this).css('backgroundPosition') == '0px 0px') {
			$(this).css('backgroundPosition', '0px -17px');
			$(this).next().next().show('fast');
		} else {
			$(this).css('backgroundPosition', '0px 0px');
			$(this).next().next().hide('fast');
	     }
	});
	$('.sessions h4').click(function() {
		$(this).siblings('.expand-btn').click();
	});


	///
	/// implement datepicker
	///
	$('.dateselect').datepicker();
	$('.daterangeselect').datepicker({rangeSelect: true});


	///
	/// form hide fields based on reg class
	///
	$('fieldset.vendorfields, fieldset.attendeefields').hide().find('*[verify]').attr('verify', 'none');
	if( $('fieldset.vendorfields, fieldset.attendeefields').length )
		if( setRegClass.length )
			$('fieldset.' + setRegClass + 'fields').show();
	$('input.regtype').click(function() {
		if( this.checked ) {
			var type = $(this).attr('regtype');
			var other = (type == 'attendee') ? 'vendor' : 'attendee';
			$('fieldset.' + other + 'fields').hide().find('*[verify]').attr('verify', 'none');
			$('fieldset.' + type + 'fields').show().find('*[verify]').attr('verify', 'nonempty');;
		}
	});

	$('#registrationtype').attr('verify', 'nonempty');
	$("input[name='product']").click( function() {
		$('#registrationtype').val($(this).val())
			.siblings("div.error").hide('slow', function() { $(this).remove(); } );
	});

	$('select.selsess').change( function() {
		$(this).parent('td').prev().html( $(this.options[this.selectedIndex]).attr('title') ) ;
	});

});

var eventCodeUnique = true;
function checkEventCode(code) {
	var url = '/event/checkcode?code=' + code;
	dynamItLoadData(url, 'check', function(resp) {
		if( resp == 'pass' ) {
			if( !eventCodeUnique )
				$('input#code').css('border', '3px solid #00cc00');
			else
				$('input#code').css('border', '1px solid #DBDBDB');

			eventCodeUnique = true;
		} else {
			eventCodeUnique = false;
			$('input#code').css('border', '3px solid #cc0000');
			$('<div class="error">An event already uses this URL code.</div>').insertAfter('input#code').show('slow');
			$('input#code').focus(function() {
				$(this).siblings("div.error").hide('slow', function() { $(this).remove(); } );
			});
		}
	});
}

function aepVerifyEventCode(f, pub) {
	if(!dynamItVerify(f)) {
		return false;
	}

	if(!eventCodeUnique) {
		$('input#code').css('border', '3px solid #cc0000').siblings("div.error").remove();
		$('<div class="error">An event already uses this URL code. Please try a different code.</div>').insertAfter('input#code').show('slow');
		$('input#code').focus(function() {
			$(this).siblings("div.error").hide('slow', function() { $(this).remove(); } );
		});
		return false;
	}

	if(pub) {
		return dynamItPublish(f);
	}

	return true;
}

function populateCompany(name) {
	$('#company').val(name);
}


function fixPhone(o) {
	var phone = o.value;
    var newphone;

	phone = phone.replace(/[^0-9]+/g, '');
	if(phone.length >= 7) {
		newphone = phone.substring(0,3) + '-' + phone.substring(3,6) + '-' + phone.substring(6);
		o.value = newphone;
	} else {
		o.value = '';
		if(phone.length > 1) {
			o.value = 'xxx-xxx-xxxx';
		}
	}

}

function checkExistingReg(form) {
	if(dynamItVerify(form)) {
		var event = $('input#event').val();
		var firstname = $('input#firstname').val();
		var lastname = $('input#lastname').val();
		var email = $('input#email').val();
		dynamItSend('/register/check-existing?first='+firstname+'&last='+lastname+'&email='+email+'&event='+event);
		return false;
	} else {
		return false;
	}
}

