if(window['console'] === undefined) {
	window.console = {
		log: function(){}
	};
}

var saving = false;
var form_changed = false;
var redirect = '';

var sendToUrl = function() {
	window.location = redirect;
};

function isNumber(string) {
	var validCharacters = "0123456789.";
	var isNumber = true;
	var character;
	for (i = 0; i < string.length && isNumber == true; i++) { 
		character = string.charAt(i); 
		if (validCharacters.indexOf(character) == -1) {
			isNumber = false;
		}
	}
	return isNumber;
}


function saveMessage(responseText) {
	form_changed = false;
	if(responseText === 'existing') {
		var event_exists = true;
	}
	else {
		var event_exists = false;
	}
	var number = isNumber(responseText);
	
	if(number || event_exists) {
		saving = false;
		if(number) {
			var form_action = $('form').attr('action');
			var save_action = $('#save-continue-tab').attr('rel');
			$('#save-continue-tab').attr('rel', save_action+'/'+responseText);
			$('#basics-tab').attr('href', $('#basics-tab').attr('href')+'/'+responseText);
			$('#substance-tab').attr('href', $('#substance-tab').attr('href')+'/'+responseText);
			$('#info-tab').attr('href', $('#info-tab').attr('href')+'/'+responseText);
			$('#rundown-tab').attr('href', $('#rundown-tab').attr('href')+'/'+responseText);
			$('#addons-tab').attr('href', $('#addons-tab').attr('href')+'/'+responseText);
			$('form').attr('action', form_action+'/'+responseText);
		}
		
		$('#saved-loading').hide();
		$('#saved').fadeIn(500, function() {
			$("#saved").fadeOut(3500);
		});
	}
	else {
		saving = false;
		$('#saved-loading').hide();
		$('#errors').html(responseText).dialog('open');
	}
	
	if($('#feedback-sent').exists()) {
	    $('dd.feedback').html('Complete').removeClass('incomplete').addClass('complete');
	    $('#feedback-sent').dialog('open');
	}
}
    
function saveLoading() {
	saving = true;
	$('#saved-loading').show();
}
    
function saveForm() {
	var options = {
        beforeSubmit: saveLoading,
        success: saveMessage
    };
    
	$('form').ajaxSubmit(options);
}

// make panels even height -DN
function setPanelHeight() {
	var leftPanel = $('#left-panel');
	var rightPanel = $('#right-panel');
	var leftHeight = leftPanel.height();
	var rightHeight = rightPanel.height();
	
	if(leftHeight > rightHeight) {
		rightPanel.css('height', leftHeight - 1);
	}
	else {
		leftPanel.css('height', rightHeight);
	}
}

// Make accordion handles the height of the accordion -DN
function setHandleHeight() {
	$('#haccordion .handle').css('height', $('#haccordion').height());
}

$(document).ready(function() {
	
	$('#overview-continue').live('click', function() {
		$('#haccordionHandle1').trigger('click');
	});
	
	window.onbeforeunload = function () {
		if(form_changed) {
			return 'You have unsaved changes, are you sure you wish to navigate away from this page?';
		}
	};
	
	$('#errors-dialog').dialog({
		autoOpen: false
	});
	
	
	var hidden_errors = $('#hidden-errors');
	if(hidden_errors.length > 0) {
		$('#error-message').html($('#hidden-errors').html());
		$('#errors-dialog').dialog('open');
	}
	
	$('#feedback-form').submit(function(e) {
		e.preventDefault();
		saveForm();
	});		
	
	$('#feedback-sent').dialog({
    	autoOpen: false
    });
	$('#send-feedback').click(function() {
		form_changed = false;
		saveForm();		
	});
	
	if($('#basics-form').exists()) {
		$('#event-title-input').live('keyup', function() {
			form_changed = true;
		});
		
		$('.event-audience-input').live('click', function() {
			form_changed = true;
		});
		
		$('select[name="timezone"]').live('click', function() {
			form_changed = true;
		});
		
		$('select[name="hour"]').live('click', function() {
			form_changed = true;
		});
		
		$('select[name="minute"]').live('click', function() {
			form_changed = true;
		});
		
		$('input[name="meridian"]').live('click', function() {
			form_changed = true;
		});
		
		$('#location-address').live('keyup', function() {
			form_changed = true;
		});
		
		$('#location-address2').live('keyup', function() {
			form_changed = true;
		});
		
		$('#location-city').live('keyup', function() {
			form_changed = true;
		});
		
		$('#location-state').live('click', function() {
			form_changed = true;
		});
		
		$('#location-zip').live('click', function() {
			form_changed = true;
		});
		
		$('textarea[name="notes_step_one"]').change(function() {
			form_changed = true;
		});
	}
	
	if($('#info-form').exists()) {
		$('#max-registrants-input').live('keyup', function() {
			form_changed = true;	
		});
		
		$('#event-url-input').live('keyup', function() {
			form_changed = true;	
		});
		
		$('#company-url-input').live('keyup', function() {
			form_changed = true;	
		});
		
		$('textarea[name="stepthreenotes"]').change(function() {
			saveForm();
		});
	}
	
	$('textarea[name="steptwonotes"]').change(function() {
		form_changed = true;
	});
	
	if($('#feedback-form').exists()) {
		$('textarea[name="comments"]').live('keyup', function() {
			form_changed = true;	
		});
		
		$('input[name="usefulness"]').live('click', function() {
			form_changed = true;	
		});
	}
	
	if($('#errors').exists()) {
		$('#errors').dialog({
			minHeight: 80,
			minWidth: 400,
			width: 350,
			autoOpen: false
		});
	}
	
	if($('a.flowplayer-video').exists()) {
		flowplayer("a.flowplayer-video", "../../flash/flowplayer-3.1.3.swf", {
			clip: {
				autoPlay: false
			},
			cachebusting: $.browser.msie
		});
	}
	
	/*$('.haccordion .handle').live('click', function() {
		saveForm();
	});*/
		
	if($('#demo-nav').exists()) {
		$('#demo-nav li').each(function() {
			$(this).click(function() {
				$('#demo-nav li.active').removeClass('active');
				$(this).addClass('active');	
			});	
		});
	}
	
	if($('#view-webinar').exists() ) {
		flowplayer("a.flowplayer-video", "../../../flash/flowplayer-3.1.3.swf", {
			clip: {
				autoPlay: false
			}	
		});	
	}
	
	if($('#services-list').exists()) {
		$('#lists').live('click', function() {
			$('#haccordionHandle1').trigger('click');
		});
		
		$('#telemarketing').live('click', function() {
			$('#haccordionHandle2').trigger('click');
		});
		
		$('#promotions').live('click', function() {
			$('#haccordionHandle3').trigger('click');
		});
		
		$('#marketing').live('click',function() {
			$('#haccordionHandle4').trigger('click');
		});
	}
	
	if($('#event-title-1').exists()) {
		$('#event-title-1').live('click', function() {
			if($('#event-location-1').css('display') == 'none') {
				$('#event-title-2').removeClass('open');
				$('#event-location-2').slideUp('normal', function() {
					$('#event-title-3').removeClass('open');
					$('#event-location-3').slideUp('normal', function() {
						$('#event-location-1').slideDown();
						$('#event-title-1').addClass('open');	
					});
				});				
			}
			else {
				$('#event-location-1').slideUp();
				$('#event-title-1').removeClass('open');	
			}
		});
	}
	
	if($('#event-title-2').exists()) {
		$('#event-title-2').live('click', function() {
			if($('#event-location-2').css('display') == 'none') {
				$('#event-title-1').removeClass('open');
				$('#event-location-1').slideUp('normal', function() {
					$('#event-title-3').removeClass('open');
					$('#event-location-3').slideUp('normal', function() {
						$('#event-location-2').slideDown();
						$('#event-title-2').addClass('open');
					});
				});	
			}
			else {
				$('#event-location-2').slideUp();
				$('#event-title-2').removeClass('open');	
			}
		});
	}
	
	if($('#event-title-3').exists()) {
		$('#event-title-3').live('click', function() {
			if($('#event-location-3').css('display') == 'none') {
				$('#event-title-1').removeClass('open');
				$('#event-location-1').slideUp('normal', function() {
					$('#event-title-2').removeClass('open');
					$('#event-location-2').slideUp('normal', function() {
						$('#event-location-3').slideDown();
						$('#event-title-3').addClass('open');
					});
				});	
			}
			else {
				$('#event-location-3').slideUp();
				$('#event-title-3').removeClass('open');	
			}
		});	
	}
    
    /*window.onbeforeunload = function () {
	   if(saving) {
	   	 return 'If you click OK your changes will NOT be saved';
   		}
	}*/
            
	// Enable login form -DN
	if($('#enter').exists()) {
		$('#enter').click(function(e) {
			e.preventDefault();
			$('#welcome').hide();
			$('#login-form').fadeIn();
		});
	}
	
	if($('input#is_saved').val() == 'true') {
       $('#saved').fadeIn(2000, function() {
           $("#saved").fadeOut(3000);
       });
   	}
	
	// Enable forgot password -DN
	if($('.forgot-password').exists()) {
		$('.forgot-password').click(function(e) {
			e.preventDefault();
			$('#login-form').hide();
			$('#forgot-password').fadeIn();
		});
	}
	
	// Cancel forgot password action
	if($('#cancel-forgot').exists()) {
		$('#cancel-forgot').click(function(e) {
			e.preventDefault();
			$('#forgot-password').hide();
			$('#login-form').fadeIn();
		});
	}
	
	// Tooltips for home page -DN
	if($('#paint-daubs').exists()) {
		$('#paint-daubs a').mytooltip();
	}
	
	// Validate registration form -DN
	if($('#register-form').exists()) {
		$('#register-form').submit(function() {
			$('#registration-errors').dialog({ minHeight: 80 }).dialog('close');
			var errors = false;
			var error_message = '<p>';

			$('input[rel="required"]').each(function() {
				if($(this).val() == '') {
					error_message += $(this).attr('error')+'<br />';
					$(this).css({'border' : 'solid 1px #C00', 'height' : '18px'});
					errors = true;
				}
				else {
					$(this).css({'border' : 'solid 1px #a5acb2', 'height' : '18px'});
					if($(this).attr('name') == 'email') {
						if($(this).val() != $('input[name="confirm_email"]').val()) {
							$(this).css({'border' : 'solid 1px #C00', 'height' : '18px'});
							$('input[name="confirm_email"]').css({'border' : 'solid 1px #C00', 'height' : '18px'});
							error_message += 'The email addresses you entered do not match<br />';
							errors = true;
						}	
					}
					
					if($(this).attr('name') == 'password') {
						if($(this).val() != $('input[name="confirm_password"]').val()) {
							$(this).css({'border' : 'solid 1px #C00', 'height' : '18px'});
							$('input[name="confirm_password"]').css({'border' : 'solid 1px #C00', 'height' : '18px'});
							error_message += 'The passwords you entered do not match<br />';
							errors = true;
						}	
					}
					
					if($(this).attr('name') == 'hpid') {
						if($('input[name="confirm_hpid"]').exists()) {
							if($(this).val() != $('input[name="confirm_hpid"]').val()) {
								$(this).css({'border' : 'solid 1px #C00', 'height' : '18px'});
								$('input[name="confirm_hpid"]').css({'border' : 'solid 1px #C00', 'height' : '18px'});
								error_message += 'The IDs you entered do not match<br />';
								errors = true;
							}
						}
					}	
				}
			});			
			
			if(errors) {
				error_message += '</p>';
				$('#registration-errors').html(error_message).dialog('open');
				return false;
			}
			else {
				return true;	
			}
		});	
	}
	
	// Basics accordion -DN
	if($('#haccordion.basics').exists()) {
		$('#haccordion.basics').hrzAccordion({eventTrigger:"click",
											  openOnLoad:"1",
											  cycle: false,
											  fixedWidth: '441'										  
											});
	}
	
	// Substance accordion -DN
	if($('#haccordion.substance').exists()) {
		$('#haccordion.substance').hrzAccordion({eventTrigger:"click",
											openOnLoad:"1",
											cycle: false,
											fixedWidth: '464'}
										  );
		setHandleHeight();
		setPanelHeight();
	}
    	
	// Info accordion -DN
	if($('#haccordion.info').exists()) {
		$('#haccordion.info').hrzAccordion({eventTrigger:"click",
											openOnLoad:"1",
											cycle: false,
											fixedWidth: '441'}
										  );	
	}
	
	// Rundown accordion -DN
	if($('#haccordion.rundown').exists()) {
		var is_noob = $('#is_noob').val();
		if(is_noob == 'true') {
		    $('#haccordion.rundown').hrzAccordion({eventTrigger:"click",
											       openOnLoad:"1",
											       cycle: false,
											       fixedWidth: '487'}
											     );
		}
		else {
		    $('#haccordion.rundown').hrzAccordion({eventTrigger:"click",
											       openOnLoad:"1",
											       cycle: false,
											       fixedWidth: '510'}
											     );
		}
		setHandleHeight();
		setPanelHeight();
	}
	
	// Addons accordion -DN
	if($('#haccordion.addons').exists()) {
	    $('#haccordion.addons').hrzAccordion({eventTrigger:"click",
											  openOnLoad:"1",
											  cycle: false,
											  fixedWidth: '464'
											});
		setHandleHeight();
		setPanelHeight();
	}
	
	setHandleHeight();
	
	// Make the vertical accordion -DN
    if($('#accordion').exists()) {
    	$('#accordion dt').myaccordion();
    	setHandleHeight();
		setPanelHeight();
    }
	
	if($('.products').exists()) {
		$('.products .product').productform();
	}
	
	// AJAX for forgot password -DN
	if($('#forgot-pass').exists()) {
    	$('#forgot-button').click(function(e) {
    		e.preventDefault();
    		email = $('#forgot-email').val();
    		
    		if(email == '') {
    			$('#forgot-error').html('Your email is required').fadeIn();
    		}
    		else {
    			$.ajax({
	    			type: "POST",
	    			url: "../../welcome/get_password",
	    			data: "email="+email,
	    			success: function(msg) {
	    				$('#forgot-error').html(msg).fadeIn();
				    }
				});
    		}
    	});
    }
    
    // AJAX for reset password -DN
    $('#home #reset-password a').click(function(e) {
    		e.preventDefault();
    		email = $('#email-input').val();
    		
			$.ajax({
    			type: "POST",
    			url: "../../welcome/get_password",
    			data: "email="+email
			});
    	});
    
    // Step One B
    if($('.event-location').exists()) {
    	$('#location-address-errors').dialog().dialog('close');
    	$('#online-live-errors').dialog().dialog('close');
    	
    	$('#save-online-live').click(function(e) {
    		e.preventDefault();
			var invalidFields = false;
			
			$('#online-live-errors').empty();
			if($('input#firstname').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not enter your first name<br />');
			}
			if($('input#lastname').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not enter your last name<br />');
			}
			if($('input#company_name').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not enter your company name<br />');
			}
			if($('input#phone').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not enter your phone number<br />');
			}
			if($('input#email').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not enter your email address<br />');
			}
			if($('input#confirm_email').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not confirm your email address<br />');
			}
			else if($('input#confirm_email').val() != $('input#email').val()) {
				$('#online-live-errors').append('The email addresses you entered do not match<br />');
			}
			if($('input#address').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not enter an address <br />');
			}
			if($('input#city').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not enter a city <br />');
			}
			if($('input#online-live-state').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not select a state <br />');
			}
			if($('input#zip').val() == '') {
				invalidFields = true;
				$('#online-live-errors').append('You did not enter a zip code <br />');
			}
						
			if(invalidFields) {
				$('#online-live-errors').dialog('open');
			}
			else {
				var id = $(this).attr('rel');
				var data = 'firstname='+$('input#firstname').val();
				data += '&lastname='+$('input#lastname').val();
				data += '&company_name='+$('input#company_name').val();
				data += '&phone='+$('input#phone').val();
				data += '&email='+$('input#email').val();
				data += '&address='+$('input#address').val();
				data += '&city='+$('input#city').val();
				data += '&state='+$('input#online-live-state').val();
				data += '&zip='+$('input#zip').val();
				
				$.ajax({
					type: "POST",
					url: "../../event/send_live_webinar_request/"+id,
					data: data,
					success: function(response) {
						$('#event-location-input').val('3');
						$('input#location-address').val('');
						$('input#location-city').val('');
						$('input#location-state').val('');
						$('input#location-zip').val('');
						$('#location-address2').val('');
						tb_remove();
						$('#event-location span.selected').hide();
		    			$('#select-location-3').css('display', 'inline-block');
				    	saveForm();			    	
			    		$('dl.basics dd.location').html('Complete').removeClass('incomplete').addClass('complete');
						$('#haccordionHandle2').trigger('click');
					}
				});
			}
		});
		
		$('#online-event-select').click(function(e) {
			e.preventDefault();
			var id = $(this).attr('rel');
			var invalidFields = false;
			
			if(invalidFields) {
				$('#online-live-errors').dialog('open');
			}
			else {
				$('#location-address-errors').empty();
				$('#event-location-input').val(id);
				$('input#location-address').val('');
				$('input#location-city').val('');
				$('input#location-state').val('');
				$('input#location-zip').val('');
				$('#location-address2').val('');
				$('#event-location span.selected').hide();
				$('#select-location-'+id).css('display', 'inline-block');
		    	saveForm();			    	
	    		$('dl.basics dd.location').html('Complete').removeClass('incomplete').addClass('complete');
				$('#haccordionHandle2').trigger('click');
			}
		});
		
    	$('.event-location').each(function() {
    		$(this).click(function(e) {
    			e.preventDefault();
    			var id = $(this).attr('rel');
    			$('#event-location-input').val(id);
    			
    			switch($('#event-location-input').val()) {
    				case '1':
    					$('input#location-address').val('');
	    				$('input#location-city').val('');
	    				$('input#location-state').val('');
	    				$('input#location-zip').val('');
	    				$('#location-address2').val('');
	    				$('#event-location span.selected').hide();
		    			$('#select-location-'+id).css('display', 'inline-block');
				    	saveForm();			    	
			    		$('dd.location').html('Complete').removeClass('incomplete').addClass('complete');
	    				$('#haccordionHandle2').trigger('click');
	    				break;
	    				
	    			case '2':
	    				var invalidFields = false;
	    				$('#location-address-errors').empty();
	    				if($('input#location-address').val() == '') {
	    					invalidFields = true;
	    					$('#location-address-errors').append('You did not enter an address <br />');
	    				}
	    				if($('input#location-city').val() == '') {
	    					invalidFields = true;
	    					$('#location-address-errors').append('You did not enter a city <br />');
	    				}
	    				if($('input#location-state').val() == '') {
	    					invalidFields = true;
	    					$('#location-address-errors').append('You did not enter a state <br />');
	    				}
	    				if($('input#location-zip').val() == '') {
	    					invalidFields = true;
	    					$('#location-address-errors').append('You did not enter a zip code <br />');
	    				}
	    				
	    				var validZip = isNumber($('input#location-zip').val());
	    				var zip = $('input#location-zip').val();
						if(zip.length < 5) {
							validZip = false;
						}
	    				if(!validZip) {
							invalidFields = true;
							$('#location-address-errors').append('Please enter a valid zip code<br />');
						}
	    				
	    				if(invalidFields) {
		    				$('#location-address-errors').dialog('open');
		    			}
		    			else {
		    				$('#event-location span.selected').hide();
			    			$('#select-location-'+id).css('display', 'inline-block');
					    	saveForm();			    	
				    		$('dd.location').html('Complete').removeClass('incomplete').addClass('complete');
		    				$('#haccordionHandle2').trigger('click');
		    			}
	    				break;
	    				
	    			case '3':
						$('#location-address-errors').empty();
						$('#event-location-input').val(id);
						$('input#location-address').val('');
						$('input#location-city').val('');
						$('input#location-state').val('');
						$('input#location-zip').val('');
						$('#location-address2').val('');
						$('#event-location span.selected').hide();
						$('#select-location-'+id).css('display', 'inline-block');
				    	saveForm();			    	
			    		$('dd.location').html('Complete').removeClass('incomplete').addClass('complete');
						$('#haccordionHandle2').trigger('click');
						break;
    			}
    		});
    	});	
    }    
    
    // Step One C
            
    if($('.event-topic').exists()) {
    	$('.event-topic').each(function() {
    		$(this).click(function(e) {
    			e.preventDefault();
    			var id = $(this).attr('rel');
    			$('#event-topic-input').val(id);
    			saveForm();
				$('#accordion span.selected').hide();
    			$('#select-topic-'+id).css('display', 'inline-block');
    			$('dd.event-type').html('Complete').removeClass('incomplete').addClass('complete');
    			$('#haccordionHandle3').trigger('click');    				
    		});
    	});	
    }
    
    // Step One D
	if($('#event-title').exists()) {
    	$('#event-title').click(function(e) {
    		e.preventDefault();
    		if($('#event-title-input').val() != '') {
    			saveForm();
    			$('dd.title').html('Complete').removeClass('incomplete').addClass('complete');
    			$('#haccordionHandle4').trigger('click');
    		}
    		else {
    			$("#no-title").dialog({ minHeight: 80 });
    		}    			
    	});	
    }
    
    // Step One E
    if($('#event-audience').exists()) {
    	$('#no-audience').dialog({ minHeight: 80 }).dialog('close');
    	$('#event-audience').click(function(e) {
    		e.preventDefault();
    		if($('.event-audience-input:checked').exists()) {
    			saveForm();
    			$('dd.audience').html('Complete').removeClass('incomplete').addClass('complete');
    			$('#haccordionHandle5').trigger('click');    					
    		}
    		else {
    			$('#no-audience').dialog('open');	
    		}
    	});	
    }
    
    // Step One F
    if($('#event-date-input').exists()) {

    	if($('.hidden-date').text() != '') {
    		var date = $('.hidden-date').text();
	    	var year = '20'+date.substring(6);
	    	var month = date.substring(2, 0);
	    	month = month - 1;
	    	var day = date.substring(3, 5);
    		var eventDate = new Date(year, month, day);
    	}
    	else {
    		eventDate = new Date();	
    	}
    	
    	$('#date-picker-box').datepicker(
    		{
    			changeYear: true,
    			nextText: '&rsaquo;',
    			prevText: '&lsaquo;',
    			minDate: new Date(),
    			altField: '#event-date-input',
    			defaultDate: eventDate,
    			changeMonth: true,
    			onSelect: function() {
    				saveForm();
    				$('#event-date-input').effect("highlight", {}, 1000);
    				$('dd.date').html('Complete').removeClass('incomplete').addClass('complete');
    			}
    		}
    	 );
    	 
    	 $('#hour-input, #minute-input, input[name="meridian"]').change(function() {
    	 	saveForm();
    	 	$('dd.date').html('Complete').removeClass('incomplete').addClass('complete');
    	 });
    	 
    	 $('select[name="timezone"]').click(function() {
    	 	saveForm();
    	 	$('dd.date').html('Complete').removeClass('incomplete').addClass('complete');
    	 });
	}
	
	// Step Two B
	$('input.format').each(function() {
		var id = $(this).attr('id');
		$(this).change(function() {
			$('.format-list').hide();
			$('dt[rel="'+id+'"], dd[rel="'+id+'"]').fadeIn();
		});
	});
	
	$('#customize_support_materials').click(function() {
		var event_id = $(this).attr('rel');
		if($(this).is(':checked')) {
			$.ajax({
				type: "POST",
				url: "../../event/send_customize_support_request/"+event_id,
				data: 'customize_support_materials='+$('#customize_support_materials').val(),
				success: function() {
					$('#customize-materials-message').dialog('open');
					saveForm();
				}
			});
		}
		else {
			saveForm();
		}
		
	});
	
	$('#customize_webinar').click(function() {
		var event_id = $(this).attr('rel');
		if($(this).is(':checked')) {
			$('dd.webinars ').html('Complete').removeClass('incomplete').addClass('complete');
			$.ajax({
				type: "POST",
				url: "../../event/send_customize_webinar_request/"+event_id,
				success: function() {
					$('#pre-recorded-webinar-message').dialog('open');	
				}
			});
			saveForm();	
		}
		else {
			$('dd.webinars ').html('Incomplete').removeClass('complete').addClass('incomplete');
    		saveForm();
		}
	});
	
	$('#record_my_own').click(function() {
		var event_id = $(this).attr('rel');
		if($(this).is(':checked')) {
			$('dd.webinars ').html('Complete').removeClass('incomplete').addClass('complete');
			$.ajax({
				type: "POST",
				url: "../../event/send_live_webinar_request/"+event_id,
				success: function() {
					$('#record-webinar-message').dialog('open');	
				}
			});
			saveForm();	
		}
		else {
			$('dd.webinars ').html('Incomplete').removeClass('complete').addClass('incomplete');
    		saveForm();
		}
	});
	
	if($('#event-date').length !== 0) {
		if($('#event-date').val() !== '') {
			var cutoff_date = $('#event-date').val();
			var year = '20'+cutoff_date.substring(6);
			var month = cutoff_date.substring(2, 0);
			month = month - 1;
			var day = cutoff_date.substring(3, 5);
		    var cutoffDate = new Date(year, month, day);
		}
		else {
			var cutoffDate = null;
		}
	}
		
	$('#cutoff-calendar').datepicker(
		{
			changeYear: true,
			nextText: '&rsaquo;',
			prevText: '&lsaquo;',
			minDate: new Date(),
			maxDate: cutoffDate,
			changeMonth: true,
			defaultDate: cutoffDate,
			onSelect: function() {
				var date = $(this).datepicker('getDate');
				var month = (date.getMonth() * 1) + 1;
				var day = date.getUTCDate();
				var year = date.getUTCFullYear();
				var new_date = month+'/'+day+'/'+year.toString().substr(-2);
				$('#registration-date-input').effect("highlight", {}, 1500).val(new_date);
				saveForm();
				$('dl.registration dd.registration').html('Complete').removeClass('incomplete').addClass('complete');
			}
		}
	);
    
    // Step Three B
    if($('#registration-date-input').exists()) {    
	    var event_date = $('input[name="event_date"]').val();
		var eyear = '20'+event_date.substring(6);
		var emonth = event_date.substring(2, 0);
		emonth = emonth - 1;
		var eday = event_date.substring(3, 5);
	    var eventDate = new Date(eyear, emonth, eday);
    	
    	$('#no-registration-date').dialog({ minHeight: 80 }).dialog('close');
    	
    	$('#general-info-continue').click(function(e) {
    		e.preventDefault();
    		if($('#registration-date-input').val().length == 0) {
    			$('#no-registration-date').dialog('open');
    		}
    		else {
    			$('dl.registration dd.registration').html('Complete').removeClass('incomplete').addClass('complete');
    			saveForm();
    			$('#haccordionHandle2').trigger('click');
    		}
    	});
    }
    
    $('#custom-image-delete').click(function(e) {
		e.preventDefault();
		var event_id = $(this).attr('rel');
		$.ajax({
			type: "POST",
			url: "../removeInfoCustomImage/"+event_id,
			success: function() {
				window.location = 'http://www.hpeventdesinger.com/event/info/'+event_id+'/image';
		    }
		});
	});
	
	$('#custom-logo-delete').click(function(e) {
		e.preventDefault();
		var event_id = $(this).attr('rel');
		$.ajax({
			type: "POST",
			url: "../removeInfoCustomLogo/"+event_id,
			success: function() {
				window.location = 'http://www.hpeventdesinger.com/event/info/'+event_id+'/logo';
		    }
		});
	});
    
    // Step Three D
    if($('#custom-logo-input').exists()) {
    	$('#no-custom-logo').dialog({ minHeight: 80 }).dialog('close');
    	$('#custom-logo-continue').click(function(e) {
    		e.preventDefault();
    		if($('#custom-logo-input').val().exists()) {
    			saveForm();
    			$('#haccordionHandle4').trigger('click');
    		}
    		else {
    			$('#no-custom-logo').dialog('open');	
    		}
    	});
    }
    
    // Step Three D
    if($('#event-url-input').exists()) {
    	
    	function checkEventUrl() {
			$.ajax(
				{type: "POST",
				 url: '../../misc/checkRegURL/'+$('#event-url-input').val(),
				 beforeSend: function() {
				 	$('#available-url').removeClass('not-available');
				 	$('#available-url').removeClass('available');
				 	$('#available-url').addClass('loading');
				 },
				 success: function(msg) {
				 	if(msg == 'available') {
				 		$('#available-url').removeClass('loading');
				 		$('#available-url').removeClass('not-available');
				 		$('#available-url').addClass('available');
				 		return true;
				 	}
				 	else {
				 		$('#available-url').removeClass('loading');
				 		$('#available-url').removeClass('available');
				 		$('#available-url').addClass('not-available');
				 		return false;
				 	}
				}
			});	
    	}
    	
    	$('#no-event-url').dialog({ minHeight: 80 }).dialog('close');
    	$('#url-not-available').dialog({ minHeight: 80 }).dialog('close');
    	$('#url-available').dialog({ minHeight: 80 }).dialog('close');
    	$('#no-company-url').dialog({ minHeight: 80}).dialog('close');
    	$('#disallowed-characters').dialog().dialog('close');
    	
    	function disallowedCharacters(string) {
			var invalid = false;
			if(string.match(/^(a-z|A-Z|0-9|\_)*[^#$%^&*()'@"<>|\s\/\\\.]*$/)) {
				invalid = false;
			}
			else
			{
				invalid = true;
			}
			return invalid;
		}
			
    	function checkUrl() {
    		var event_url = $('#event-url-input').val();			
    		var invalid = disallowedCharacters(event_url);
    		
    		if(!invalid) {
    			if(event_url.length == 0) {
	    			$('#available-url').removeClass('available');
	    			$('#available-url').removeClass('not-available');
	    			$('#no-event-url').dialog('open');
	    			return false;		
	    		}
	    		else {
	    			checkEventUrl();
	    		}
    		}
    		else {
    			$('#disallowed-characters').dialog('open');	
    			return false;
    		}
    		return true;
    	}
    	
    	$('#check-url').click(function(e) {
    		e.preventDefault();
    		checkUrl();
    	});
    	
    	$('#company-url-continue').click(function(e) {
    		e.preventDefault();
    		var brandflexLink = $('.brandflex-link').attr('href');
    		
			if($('#company-url-input').val().length == 0) {
				$('#no-company-url').dialog('open');
    		}
    		else {
    			$('dd.urls').html('Complete').removeClass('incomplete').addClass('complete');
    			form_changed = false;
    			saveForm();
    			var event_id = $(this).attr('rel');
    			
    			var companyUrl = $('#company-url-input').val();
    			var eventUrl = $('#event-url-input').val();
    			if(brandflexLink) {
    				brandflexLink = brandflexLink.replace('company_url=', 'company_url='+companyUrl);
    				brandflexLink = brandflexLink.replace('http://www.hpeventdesinger.com/Events/', 'rurl=http://www.hpeventdesinger.com/Events/'+eventUrl);	
    			}
    			$('#available-url').removeClass('not-available').addClass('available');
    			$('#check-url').remove();
    			$('.brandflex-link').attr('href', brandflexLink);
    			$('#haccordionHandle5').trigger('click');
    		}	
    	});
    }
    
    // Step Three E
    if($('input[name="reminders_postcards[]"]').exists()) {
    	$('#outbound-communication-continue').click(function() {
    		$('input#save-continue-tab').trigger('click');
    	});
    }
    
    // Rundown
    
    // Admin
    if($('#admin-table').exists()) {
    	function tableBorders() {
    		$('#admin-table tbody td ').css('border-bottom', 'solid 1px #EB5F01');
    		$('#admin-table th:last').css('border-right', '#EB5F01');
    		$('#admin-table tr:last td').css('border-bottom', 'none');
    	}
    	
    	tableBorders();
    	$("#admin-table").tablesorter();
    	$("#admin-table").bind("sortStart",function() { 
	        // do nothing -DN
	    }).bind("sortEnd",function() { 
	        tableBorders();
	    }); 
    	
    	$('.topic-table .edit a').each(function() {
			var item_key = $(this).attr('rel');
    		$(this).click(function() {
    			$('.topic-edit #title-input').val($('tr[rel="'+item_key+'"] td.topic-name').text());
    			$('.topic-edit #emails').text($('tr[rel="'+item_key+'"] input.email_templates').val());    			
    			$('.topic-edit #sort-input').val($('tr[rel="'+item_key+'"] td.sort-order').text());
    			$('.topic-edit #description-input').val($('tr[rel="'+item_key+'"] input.detail').val());
    			$('.topic-edit #country-input').val($('tr[rel="'+item_key+'"] input.country_id').val());    			
    			$('#topic-id-input').val(item_key);    			
    		});
    	});
    	
    	$('.new-topic').click(function() {
			$('#admin-edit-pane #title-input').val('').focus();
			$('#admin-edit-pane #sort-input').val('');
			$('#admin-edit-pane #emails').text('');			
			$('#admin-edit-pane #description-input').val('');
			$('#admin-edit-pane #country-input').val('');			
			$('#topic-id-input').val('');
		});
		
		$('.webinar-table .edit a').each(function() {
			var item_key = $(this).attr('rel');
    		$(this).click(function() {
    			$('.webinar-edit #title-input').val($('tr[rel="'+item_key+'"] td.webinar-title').text());
				$('.webinar-edit #material_type').val($('tr[rel="'+item_key+'"] input[name="type_id"]').val()); 						
				$('.webinar-edit #url-input').val($('tr[rel="'+item_key+'"] input[name="url"]').val()); 										
				$('.webinar-edit #topic').val($('tr[rel="'+item_key+'"] input[name="topic_id"]').val()); 										
    			$('.webinar-edit #location-input').val($('tr[rel="'+item_key+'"] input.location').val());    				
    			//$('.webinar-edit #country-input').val($('tr[rel="'+item_key+'"] input.country_id').val());        			
    			//if($('input[name="webinar_live"]').attr('checked') == true) { //KK adjusted as to rel as well as clearing it when another edit is clicked that does not have it active
    			//if($('tr[rel="'+item_key+'"] input[name="webinar_live"]').attr('checked') == true) {
    			//	$('.webinar-edit #live-input').attr('checked', 'checked');	
    			//}
    			//else {
    			//	$('.webinar-edit #live-input').attr('checked', '');	    			
    			//}
    			$('.upload-webinar').text('Re-upload Webinar');
    			$('.webinar-edit #sort-input').val($('tr[rel="'+item_key+'"] td.sort-order').text());
    			$('#webinar-id-input').val(item_key);
    		});
    	});
    	
    	$('.new-webinar').click(function() {
			$('#admin-edit-pane #title-input').val('').focus();
			$('#admin-edit-pane #sort-input').val('');
			$('#admin-edit-pane #material_type').val('');
			$('#admin-edit-pane #topic').val('');			
			$('#admin-edit-pane #url-input').val('');						
			$('.upload-webinar').text('Upload Webinar');
			//$('#admin-edit-pane #country-input').val('');						
			//$('.webinar-edit #live-input').attr('checked', '');
			$('#webinar-id-input').val('');
		});
		$('.oc-table .edit a').each(function() {
			var item_key = $(this).attr('rel');
    		$(this).click(function() {
    			$('.oc-edit #flexdoc-input').val($('tr[rel="'+item_key+'"] input.flexdoc_id').val());    				    			
				$('.oc-edit #type').val($('tr[rel="'+item_key+'"] input[name="type_id"]').val()); 						
				$('.oc-edit #topic').val($('tr[rel="'+item_key+'"] input[name="topic_id"]').val()); 										
    			$('.oc-edit #medium').val($('tr[rel="'+item_key+'"] input.medium_id').val());    				    			
    			$('.oc-edit #sort-input').val($('tr[rel="'+item_key+'"] td.sort-order').text());
    			//$('.oc-edit #country-input').val($('tr[rel="'+item_key+'"] input.country_id').val());        			    			
    			$('#oc-id-input').val(item_key);
    		});
    	});
    	
    	$('.new-oc').click(function() {
			$('#admin-edit-pane #flexdoc-input').val('').focus();
			$('#admin-edit-pane #sort-input').val('');
			$('#admin-edit-pane #type').val('');
			$('#admin-edit-pane #topic').val('');			
			$('#admin-edit-pane #medium').val('');						
			//$('#admin-edit-pane #country-input').val('');									
			$('#oc-id-input').val('');
		});
		
		$('.email-table .edit a').each(function() {
			var item_key = $(this).attr('rel');
    		$(this).click(function() {
    			$('.email-edit #name-input').val($('tr[rel="'+item_key+'"] td.email-name').text());
    			$('.topic-edit #description-input').val($('tr[rel="'+item_key+'"] input.detail').val());    			
    			$('.email-edit #subject-input').val($('tr[rel="'+item_key+'"] input.subject').val());    				
    			$('.email-edit #body-input').val($('tr[rel="'+item_key+'"] input.body').val());    				    			
    			$('.email-edit #country-input').val($('tr[rel="'+item_key+'"] input.country_id').val());    
    			$('.email-edit #email-type-input').val($('tr[rel="'+item_key+'"] input.email_template_type_id').val());				    			
    			
    			if($('tr[rel="'+item_key+'"] input[name="email_active"]').attr('checked') == true) {
    				$('.email-edit #active-input').attr('checked', 'checked');	
    			}
    			else {
    				$('.email-edit #active-input').attr('checked', '');	    			
    			}
    			$('#email-id-input').val(item_key);
    		});
    	});		
    	
    	$('.new-email').click(function() {
			$('.email-edit #name-input').val('').focus();
			$('.email-edit #subject-input').val('');
			$('.email-edit #body-input').val('');			
			$('.email-edit #country-input').val('');	
			$('.email-edit #email-type-input').val('');					
			$('.email-edit #active-input').attr('checked', '');
			$('.email-edit #email-id-input').val('');
		});
    	
    	if($('#admin #reset-password a').exists()) {
    		$('#password-reset').dialog().dialog('close');
    		$('#admin #reset-password a').click(function(e) {
	    		e.preventDefault();
	    		user_id = $(this).attr('rel');	
				$.ajax({
	    			type: "POST",
	    			url: "../../admin/resetUserPass/"+user_id,
	    			success: function() {
	    				$('#password-reset').dialog('open');	
	    			}
				});
			});
    	}
		
		$('.users-table .edit a').each(function() {
			var item_key = $(this).attr('rel');
    		$(this).click(function() {
    			$('#reset-password a').attr('rel', item_key);
    			$('#reset-password').show();
    			$('#user-form').slideDown();
    			$('.users-edit #first-name').val($('tr[rel="'+item_key+'"] td.first-name').text());
    			$('.users-edit #last-name').val($('tr[rel="'+item_key+'"] td.last-name').text());
    			$('.users-edit #company').val($('tr[rel="'+item_key+'"] td.company').text());
    			$('.users-edit #email-input').val($('tr[rel="'+item_key+'"] td.email').text());   			
				$('.users-edit #role').val($('tr[rel="'+item_key+'"] input[name="role_id"]').val());    			
    			$('.users-edit #address').val($('tr[rel="'+item_key+'"] input[name="address"]').val());
    			$('.users-edit #address2').val($('tr[rel="'+item_key+'"] input[name="address2"]').val());
    			$('.users-edit #city').val($('tr[rel="'+item_key+'"] input[name="city"]').val());
    			$('.users-edit #state').val($('tr[rel="'+item_key+'"] input[name="state"]').val());
    			$('.users-edit #country-input').val($('tr[rel="'+item_key+'"] input[name="country_id"]').val());
    			$('.users-edit #zip').val($('tr[rel="'+item_key+'"] input[name="zip"]').val());
    			$('.users-edit #phone').val($('tr[rel="'+item_key+'"] input[name="phone"]').val());
    			$('.users-edit #company_url').val($('tr[rel="'+item_key+'"] input[name="company_url"]').val());
    			$('.users-edit #internalid').val($('tr[rel="'+item_key+'"] input[name="internal_id"]').val());
    			
    			//if($('tr[rel="'+item_key+'"] td.is-flp').text() == 'True') {
    			//	$('.users-edit #flp').attr('checked', 'checked');
    			//}
    			//if($('tr[rel="'+item_key+'"] input[name="hp"]').val() == 'true') {
    			//	$('.users-edit #hp').attr('checked', 'checked');
    			//}
    			//if($('tr[rel="'+item_key+'"] td.requires-approval').text() == 'True') {
    			//	$('.users-edit #approval').attr('checked', 'checked');
    			//}
    			//alert($('tr[rel="'+item_key+'"] input[name="requires-approval"]').attr('checked'));    			
    			if($('tr[rel="'+item_key+'"] input[name="requires-approval"]').attr('checked') == true) {
    				$('.users-edit #approval').attr('checked', 'checked');	
    			}
    			else {
    				$('.users-edit #approval').attr('checked', '');	    			
    			}
    			
    			$('.users-edit input#user-id-input').val(item_key);
    		});
    	});
    	
    	$('#add-user').click(function() {
    		$('#reset-password').hide();
    		$('#user-form').slideDown();
    		$('.users-edit #first-name').val('').focus();
			$('.users-edit #last-name').val('');
			$('.users-edit #company').val('');
			$('.users-edit #email-input').val('');
			$('.users-edit #address').val('');
			$('.users-edit #address2').val('');
			$('.users-edit #city').val('');
			$('.users-edit #state').val('');
			$('.users-edit #country-input').val('');			
			$('.users-edit #zip').val('');
			$('.users-edit #phone').val('');
			$('.users-edit #company_url').val('');
			$('.users-edit #hpid').val('');
			$('.users-edit #flp').attr('checked', '');
			$('.users-edit #hp').attr('checked', '');
			$('.users-edit #approval').attr('checked', '');
			$('.users-edit input#user-id-input').val('');	
    	});
    	
    // Registry    	
    
		$('.attendees-table .edit a').each(function() {
			var item_key = $(this).attr('rel');
    		$(this).click(function() {
    			$('#attendee-form').slideDown();
    			$('.attendees-edit #first-name').val($('tr[rel="'+item_key+'"] td.first-name').text());
    			$('.attendees-edit #last-name').val($('tr[rel="'+item_key+'"] td.last-name').text());
    			$('.attendees-edit #title').val($('tr[rel="'+item_key+'"] td.title').text());   			    			    			
    			$('.attendees-edit #company').val($('tr[rel="'+item_key+'"] td.company').text());
    			$('.attendees-edit #phone').val($('tr[rel="'+item_key+'"] td.phone').text());   			    			    			
    			$('.attendees-edit #email-input').val($('tr[rel="'+item_key+'"] td.email').text());   			
    			$('.attendees-edit #address').val($('tr[rel="'+item_key+'"] input[name="address"]').val());
    			$('.attendees-edit #address2').val($('tr[rel="'+item_key+'"] input[name="address2"]').val());
    			$('.attendees-edit #city').val($('tr[rel="'+item_key+'"] input[name="city"]').val());
    			$('.attendees-edit #status').val($('tr[rel="'+item_key+'"] input[name="status_id"]').val());
    			$('.attendees-edit #state').val($('tr[rel="'+item_key+'"] input[name="state_id"]').val());    			    			
    			
    			$('.attendees-edit #zip').val($('tr[rel="'+item_key+'"] input[name="zip"]').val());


    			$('.attendees-edit #internalid').val($('tr[rel="'+item_key+'"] input[name="internal_id"]').val());
    			$('.attendees-edit input#attendee-id-input').val(item_key);
    			$('.attendees-edit #attendee-prev-status').val($('tr[rel="'+item_key+'"] input[name="status_id"]').val());    			
    		});
    	});
    	
    	$('#add-attendee').click(function() {
    		$('#attendee-form').slideDown();
    		$('.attendees-edit #first-name').val('').focus();
			$('.attendees-edit #last-name').val('');
			$('.attendees-edit #title').val('');			
			$('.attendees-edit #company').val('');
			$('.attendees-edit #email-input').val('');
			$('.attendees-edit #address').val('');
			$('.attendees-edit #address2').val('');
			$('.attendees-edit #city').val('');
			$('.attendees-edit #status').val('');
			$('.attendees-edit #state').val('');				
			$('.attendees-edit #zip').val('');
			$('.attendees-edit #phone').val('');
			$('.attendees-edit input#attendee-id-input').val('');	
			$('.attendees-edit input#attendee-prev-status').val('');				
    	});    
    }
    
    if($('#admin-edit-pane').exists()) {
    	$('#admin-edit-pane #user-form').submit(function() {
    		$('#errors').dialog().dialog('close');
    		var errors = false;
    		
    		if($('#user-form #first-name').val() == '') {
				$('#errors').append($('#first-name').attr('error')+'<br />');
				errors = true;
    		}
    		
    		if($('#user-form #last-name').val() == '') {
    			$('#errors').append($('#last-name').attr('error')+'<br />');
				errors = true;
    		}
    		
    		if($('#user-form #company').val() == '') {
    			$('#errors').append($('#company').attr('error')+'<br />');
				errors = true;
    		}
    		
    		if($('#user-form #email').val() == '') {
    			$('#errors').append($('#email').attr('error')+'<br />');
				errors = true;
    		}
    		
    		if($('#user-form #address').val() == '') {
    			$('#errors').append($('#address').attr('error')+'<br />');
				errors = true;
    		}
    		
    		if($('#user-form #city').val() == '') {
    			$('#errors').append($('#city').attr('error')+'<br />');
				errors = true;
    		}
    		
    		if($('#user-form #state').val() == '') {
    			$('#errors').append('You did not select a state<br />');
				errors = true;
    		}
    		
    		if($('#user-form #zip').val() == '') {
    			$('#errors').append($('#zip').attr('error')+'<br />');
				errors = true;
    		}
    		
    		if($('#user-form #internalid').val() == '') {
    			$('#errors').append($('#internalid').attr('error')+'<br />');
				errors = true;
    		}
    		
    		if($('#user-form #role').val() == '') {
    			$('#errors').append($('#role').attr('error')+'<br />');
				errors = true;
    		}
    		
    		if(errors) {
    			$('#errors').dialog('open');
    			return false;
    		}
    		else {
    			return true;	
    		}
    	});
    }
    
    var lastpane = $('.handle').length - 1;
    
	$('.handle').live('click', function() {
    	var handle = $(this).attr('id');
		handle = handle.replace('haccordionHandle', '');
		$('#current-pane').val(handle).change();
    });
    
    $('#save-continue-tab').bind('click', function(e) {
    	e.preventDefault();
    	form_changed = false;
    	if($('#basics-page').val() == 'true') {
    		if($('#location-zip').val() != '') {
    			var validZip = isNumber($('#location-zip').val());
    			var zip = $('input#location-zip').val();
				if(zip.length < 5) {
					validZip = false;
				}
    			if(!validZip) {
    				$('#location-address-errors').html('Please enter a valid zip code');
    				$('#location-address-errors').dialog('open');
    				return false;
    			}
    			else {
    				saveForm();
    			}
    		}
    	}
    	else {
    		saveForm();
    	}	
    });
    
    $('#current-pane').change(function() {
    	if($('#current-pane').val() == lastpane) {
    		$('#save-continue-tab').css('visibility', 'visible');
    		image = $('#save-continue-tab img').attr('src');
    		image = image.replace('save.jpg', 'save-proceed.jpg');
    		$('#save-continue-tab img').attr('src', image);
    		
    		if($('body .addons').length !== 0) {
    			$('#save-continue-tab').css('width', '133px');
    		}
    		else {
    			$('#save-continue-tab').css('width', '232px');
    		}
    		
    		$('#save-continue-tab').unbind('click');
    		$('#save-continue-tab').bind('click', function(e) {
    			
    			e.preventDefault();
		    	form_changed = false;
    			redirect = $(this).attr('rel');
    			var options = {
			        beforeSubmit: saveLoading,
			        success: sendToUrl
			    };
			    
			    if($('#basics-page').val() == 'true') {
		    		if($('#location-zip').val() != '') {
		    			var validZip = isNumber($('#location-zip').val());
		    			var zip = $('input#location-zip').val();
						if(zip.length < 5) {
							validZip = false;
						}
		    			if(!validZip) {
		    				$('#location-address-errors').html('Please enter a valid zip code');
		    				$('#location-address-errors').dialog('open');
		    				return false;
		    			}
		    			else {
		    				$('form').ajaxSubmit(options);
		    			}
		    		}
		    		else {
		    			$('form').ajaxSubmit(options);
		    		}
		    	}
		    	else if($('body .addons').length !== 0) {
		    		window.location = redirect;
		    	}
		    	else {
		    		$('form').ajaxSubmit(options);
		    	}
		    });
    	}
    	else {
    		$('#save-continue-tab').css('visibility', 'hidden');
    		image = $('#save-continue-tab img').attr('src');
    		image = image.replace('save-proceed.jpg', 'save.jpg');
    		$('#save-continue-tab img').attr('src', image);
    		$('#save-continue-tab').css('width', '62px');
    		
    		$('#save-continue-tab').unbind('click');
    		$('#save-continue-tab').bind('click', function(e) {
    			e.preventDefault();
    			form_changed = false;
		    	if($('#basics-page').val() == 'true') {
		    		if($('#location-zip').val() != '') {
		    			var validZip = isNumber($('#location-zip').val());
		    			var zip = $('input#location-zip').val();
						if(zip.length < 5) {
							validZip = false;
						}
		    			if(!validZip) {
		    				$('#location-address-errors').html('Please enter a valid zip code');
		    				$('#location-address-errors').dialog('open');
		    				return false;
		    			}
		    			else {
		    				saveForm();
		    			}
		    		}
		    		else {
		    			saveForm();
		    		}
		    	}
		    	else {
		    		saveForm();
		    	}
		    });
    	}
    });
    
    $('#et-errors').dialog().dialog('close');
    
    $('#email-templates').submit(function() {
    	
    	$('#et-errors').html('');
    	var errors = false;
    	if($('#et-topic').val() == '') {
    		errors = true;
    		$('#et-errors').append('You did not select a topic<br />');
    	}
    	if($('#et-type').val() == '') {
    		errors = true;
    		$('#et-errors').append('You did not select a template type<br />');
    	}
    	if($('#et-comm').val() == '') {
    		errors = true;
    		$('#et-errors').append('You did not select a communication type<br />');
    	}
    	if($('#et-file').val() == '') {
    		errors = true;
    		$('#et-errors').append('You did not choose a file to upload<br />');
    	}
    	if(errors) {
    		$('#et-errors').dialog('open');
    		return false;	
    	}
    });
    
    $('#et-topic').change(function() {
    	var topic_id = $(this).val();
    	var associated_templates = $('input[rel="'+topic_id+'"]').val();
    	$('#template-topics .templates').text(associated_templates);
    });
    
    $('#admin-email-errors').dialog({
    	autoOpen: false
    });
    $('#save-admin-email').live('click', function() {
    	$('#admin-email-errors').html('');
    	/*if($('#admin-email').val() == '') {
    		$('#admin-email-errors').html('You did not enter an email address');
    		$('#admin-email-errors').dialog('open');
    	}
    	else {*/
    		var email = $('#admin-email').val();
    		var event_id = $(this).attr('rel');
    		$.ajax({
    			type: "POST",
    			url: "../../event/create_event_admin/"+event_id,
    			data: 'admin_email='+email,
    			success: function(response) {
    				if(response.match('@error')) {
    					response = response.replace('@error', '');
    					$('#email-saved').hide();
    					$('#email-error').show();
    					$('#admin-email-errors').html(response);
    					$('#admin-email-errors').dialog('open');
    				}
    				else {
    					$('#email-error').hide();
    					$('#email-saved').show();
    				}
			    }
			});	
    	/*}*/
    });
    
    $('#promotions-list').css({
    	'list-style-type' : 'disc',
		'list-style-position' : 'outside',
		'display' : 'block'
    });
    
    $('#tac').click(function() {
    	var event_id = $(this).attr('rel');
    	$.ajax({
    			type: "POST",
    			url: "../../../product/accept_tac/"+event_id
			});	
    });
    
    $('#record-webinar-message').dialog({
    	autoOpen: false
    });
    $('#pre-recorded-webinar-message').dialog({
    	autoOpen: false
    });
    $('#customize-materials-message').dialog({
    	autoOpen: false
    });
    
    $('#customize-webinar').click(function(e) {
    	e.preventDefault();
    	var event_id = $(this).attr('rel');
    	if($(this).is(':checked')) {
    		$.ajax({
				type: "POST",
				url: "../../event/send_customize_webinar_request/"+event_id,
				success: function() {
					$('#pre-recorded-webinar-message').dialog('open');	
				}
			});
    	}
    });
    
    $('#record-webinar').click(function(e) {
    	e.preventDefault();
    	var event_id = $(this).attr('rel');
    	if($(this).is(':checked')) {
    		$.ajax({
				type: "POST",
				url: "../../event/send_live_webinar_request/"+event_id,
				success: function() {
					$('#record-webinar-message').dialog('open');	
				}
			});
    	}
    });
    
    var refreshBF = false;
    $('#ob-comm-heading a').click(function() {
    	refreshBF = true;
    	alert('refreshBF is set');
    });
    
    $('#terms-error').dialog({
    	autoOpen: false
    });
        
    $('#survey-event-date').datepicker(
		{
			changeYear: true,
			nextText: '&rsaquo;',
			prevText: '&lsaquo;',
			changeMonth: true
		}
	);
	
	$('#no-webinars').click(function() {
		$('#webinar-materials input[name="material[]"]').attr('checked', '');
	});
	
	$('#webinar-materials input[name="material[]"]').click(function() {
		$('#no-webinars').attr('checked', '');
	});
	
	$('#no-ppts').click(function() {
		$('#ppt-materials input[name="material[]"]').attr('checked', '');
	});
	
	$('#ppt-materials input[name="material[]"]').click(function() {
		$('#no-ppts').attr('checked', '');
	});
	
	$('input[name="custom_image"]').click(function() {
		$('dd.image').html('Complete').removeClass('incomplete').addClass('complete');
	});
	
	$('dd.logo').html('Complete').removeClass('incomplete').addClass('complete');
	
	$('input[name="custom_image"]').click(function () {
		var type = $(this).val();
		var image = $('#cust-img');
		var loaded_image = $('#cust-img').attr('src');
		var default_image = 'http://www.hpeventdesinger.com/images/application/no-image.jpg';
		switch(type) {
			case 'no':
				$('#cust-img, #custom-image-buttons').hide();
				break;
			case 'default':
				image.attr('src', default_image);
				$('#custom-image-view').attr('href', default_image);
				$('#cust-img, #custom-image-buttons').show();
				$('#custom-image-delete').hide();
				break;
			case 'yes':
				image.attr('src', loaded_image);
				$('#cust-img, #custom-image-buttons').hide();
				$('#upload-custom').trigger('click');
				break;
		}
	});
	
	$('#custom-img-continue').click(function() {
		saveForm();
		$('#haccordionHandle4').trigger('click');
	});
	
	$('#custom-logo-continue').click(function() {
		saveForm();
		$('#haccordionHandle3').trigger('click');
	});
	
	setHandleHeight();
	setPanelHeight();
	
	$('.event-audience-input').click(function() {
		saveForm();
	});
	
	$('input[name="material[]"], input[name="no_ppts"], input[name="no_webinars"]').click(function() {
		saveForm();
	});
	
	$('#events-list table tr td a.delete').click(function(e) {
		e.preventDefault();
		var url = $(this).attr('href');
		var confirmed = confirm('Are you sure you want to delete this event?');
		if(confirmed) {
			window.location = url;
			return;
		}
		else {
			return;
		}
	});
});
