var cal_refresh = 'yes';

function update_mini_schedule(date){
	$.ajax({
		type: "POST",
		url: BASE_URL + "schedule/upcoming_appointments_mini/",
		data: 'appt_date=' + date,
		dataType: 'json',
		success: function(msg){
			if(msg.error){
				var html = "<span class='ui-state-error ui-corner-all' id='mini-no-appointments'><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 0.3em;'></span>No appointments.</span>";
				$('#appt_result_cont').html(html);
				$('#mini_ajax_load').hide();
				prep_pencilem_bar();
			}else{
				$('#appt_result_cont').html(msg.ul);
				$('#mini_ajax_load').hide();
				mini_cal_preps();
			}
		}
	});
}


function change_contractor_dd(){
	$('#location_contractor_dd').change(function(){
		$('#contractor_drop_down').removeClass('contractor_drop_down').addClass('contractor_drop_down_change');
		var template_vals = 'switch_contractor=' + $(this).val();
		$.ajax({
			type: "POST",
			url: BASE_URL + "salon/change_contractor/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.success){
					location.reload(true);
				}else{
					$('#contractor_drop_down').removeClass('contractor_drop_down_change').addClass('contractor_drop_down');
					alert("An error has occured.  Please try again.");	
				}
			}
		});
	});
}

function update_location_calendar(date){
	$.ajax({
		type: "POST",
		url: BASE_URL + "schedule/location_calendar/" + date,
		dataType: 'json',
		success: function(msg){
			if(msg.error){
				$('#location_ajax_load').hide();
				$('#location_appt_holder').html("<span class='ui-state-error ui-corner-all' id='no-appointments'><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 0.3em;'/>" + msg.message + "</span>");
				$('#location_appt_header').hide();
			}else{
				$('#location_appt_holder').html(msg.ul);
				$('#location_ajax_load').hide();
				$('#location_appt_header').show();
				
				//make the appointments clickable
				$('#location_appt_list_ul li').click(function(){
					if($(this).find('div[class="appt_details"]').css('display') == 'none'){
						$(this).find('div[class="appt_details"]').show();
					}else{
						$(this).find('div[class="appt_details"]').hide();
					}
				});
			}
		}
	});
}

function prep_quickview(){
	$('.client_quickview_link').click(function(){
		//get an instance
		var instance = $(this);
		
		//hide the actual data
		$('#qvinfocont').hide();
		
		//show the loader
		$('#qvload').show();
		
		//position and show the tab
		var x = instance.offset().left;
		var y = instance.offset().top;
		
		var cssObj = {
			'left' : x - 405,
			'top' : y - 10
		};
		
		$('#quickview').css(cssObj).show();
		
		//load the info into the modal
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/get_client_details/" + $(this).attr('rel'),
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert("There was a problem selecting the client details");
				}else{
					var phone = (msg.phone == '' || msg.phone === null) ? '---' : msg.phone;
					var cell = (msg.cell_phone == '' || msg.cell_phone === null) ? '---' : msg.cell_phone;
					var work = (msg.work_phone == '' || msg.work_phone === null) ? '---' : msg.work_phone;
					var notes = (msg.notes == '' || msg.notes === null) ? 'No notes available.' : msg.notes;
					var email = (msg.email == '' || msg.email === null) ? "---" : "<a href='mailto:" + msg.email + "'>" + msg.email + "</a>";
					notes = notes.replace(/\n/g,'<br />');
					
					$('#qv_name').text(msg.fname + ' ' + msg.lname);
					$('#qv_phone').text(phone);
					$('#qv_cellphone').text(cell);
					$('#qv_workphone').text(work);
					$('#qv_email').html(email);
					$('#client_note_place_holder').html(notes);
					
					//show the actual data
					$('#qvinfocont').show();
					
					//hide the loader
					$('#qvload').hide();					
					
					/*
					$('#quickview_modal').dialog('open');
					*/
					
				}
			}
		});
	});
}

function prep_notes(){
	$('.appt_note_show').click(function(){
		var rel = $(this).attr('rel');
		if($('.appt_note_cont' + rel).css('display') == 'none'){
			$(this).text('- Hide Notes');
			$('.appt_note_cont' + rel).show('blind',300);
			$('#note_pointer' + rel).show();
		}else{
			$(this).text('+ Show Notes');
			$('.appt_note_cont' + rel).hide('blind',300);
			$('#note_pointer' + rel).hide();
		}
		return(false);
	});
}

function prep_price_override(){
	$('.service_price_override').click(function(){
		//transform into a text field
		var price;
		if($(this).text() == 'Add Service Price'){
			price =  '';
			$(this).before('$');
		}else{
			price = $(this).text();
		}
		
		var appt_id = $(this).attr('rel');
		var input = "<span id='overide_in_progress'><input type='text' class='overide_price_field' id='overide_price" + appt_id + "' value='" + price + "' /><a class='save_new_price ui-corner-all' rel='overide_price~" + appt_id + "' id='save_new_price" + appt_id + "'>Save</a></span>";
		
		$(this).before(input);
		$(this).hide();
		
		//handle the press of the enter key while on the input field
		$('#overide_price' + appt_id).keydown(function(e){
			var key = e.charCode || e.keyCode || 0;
			if(key == 13){
				//it was an enter button
				$('#save_new_price' + appt_id).click();
			}
		});
		
		//handle the click of the save button
		$('#save_new_price' + appt_id).click(function(){
			var new_price = $(this).parent().find("input[class='overide_price_field']").val();
			if(isNaN(new_price)){
				alert('The service price override can only contain numbers.');
			}else if(new_price == '' || new_price.length === 0){
				alert('The service price is required.');
			}else{
				//ajax the new price to the price override tbl
				var template_vals = 'appt_id=' + appt_id + '&price_override=' + new_price;
				$.ajax({
					type: "POST",
					url: BASE_URL + "services/appt_price_override/", 
					data: template_vals,
					dataType: 'json',
					success: function(msg){
						if(msg.error){
							alert(msg.msg);
						}
					}
				});
				
				//show the .service_price_override
				if($(this).parent().parent().find('span[class="service_price_override"]').text() == 'Add Service Price'){
					//$(this).parent().parent().find('span[class="service_price_override"]').before('$');
					$(this).parent().parent().find('span[class="service_price_override"]').text(new_price).show();
				}else{
					$(this).parent().parent().find('span[class="service_price_override"]').text(new_price).show();
				}
				//hide the override_in_progress span
				$(this).parent().remove();
				prep_pencilem_bar();
				
			}
			
			return(false);
		});
	});
}

function prep_note_edit(){
	var can_edit = true;
	if(can_edit){
		$('.appt_note_style').hover(
			function(){
				$(this).addClass('appt_note_style_dark');
				$(this).parent().find('div[class="note_pointer"]').removeClass('note_pointer').addClass('note_pointer_dark');
				if($(this).hasClass('has_editor')){
					$(this).find('a[class="edit_launch ui-corner-all"]').hide();
				}else{
					$(this).find('a[class="edit_launch ui-corner-all"]').show();
				}
				
			},
			function(){
				$(this).removeClass('appt_note_style_dark');
				$(this).parent().find('div[class="note_pointer_dark"]').removeClass('note_pointer_dark').addClass('note_pointer');
				$(this).find('a[class="edit_launch ui-corner-all"]').hide();
			}
		);
		
		$('.edit_launch').click(function(){
			$(this).hide();
			run_note_edit($(this).parent(),$(this),true);
			return(false);
		});
	}else{
		$('.appt_note_style').css('cursor','auto');
	}
}

function prep_add_note(){
	$('.appt_add_note').click(function(){
		//get the parent object to manipulate
		var parent_obj = $(this).parent().parent().parent();
		var appt_id = $(this).attr('rel');
		
		//is there already an editor available?
		if(!parent_obj.find('div.appt_note_style').hasClass('has_editor')){
			//show the bubble
			parent_obj.find('div.note_pointer').show();	
			parent_obj.find('div.appt_note_style').show();
			
			//trigger the click of the edit button
			parent_obj.find('a.edit_launch').click();
		}else{
			//the editor is already shown, so remove it
			$('#edit_div_' + appt_id).remove();
			parent_obj.find('div.note_pointer').hide();	
			parent_obj.find('div.appt_note_style').hide();
			parent_obj.find('div.appt_note_style').removeClass('has_editor');
		}
		
		return(false);
	});
}

function prep_personal_time(){
	var times_clicked = 0;
	$('a.delete_personal_time').click(function(){
		
		if(times_clicked === 0){
			times_clicked = times_clicked + 1;
			
			//after they click the delete link, confirm the delte with another click "Are you sure?"
			$(this).text('Are you sure?');
			
			//if it's not already in place, add a cancel button
			if($(this).parent().find('#pt' + $(this).attr('rel')).length === 0){
				$(this).parent().append('<a style="font-size:75%;margin-left:5px;" href="#" id="pt' + $(this).attr('rel') + '" rel="' + $(this).attr('rel') + '">or Cancel</a>');
			}
			
			
			$('#pt' + $(this).attr('rel')).click(function(){
				$(this).remove();	
				$('a.delete_personal_time').text("Delete");
				times_clicked = 0;
				return(false);
			});
			
		}else{
			var template_vals = 'appt_id=' + $(this).attr('rel');
			$.ajax({
				type: "POST",
				url: BASE_URL + "schedule/delete_personal_time/", 
				data: template_vals,
				dataType: 'json',
				success: function(msg){
					if(msg.error){
						alert(msg.msg);
					}else{
						//update teh mini schedule
						update_mini_schedule($('#formated_date').val());
					}
				}
			});
			times_clicked = 0;
		}
		return(false);											   
	});
	
	$('tr.personal_time').hover(
		function(){
			$(this).find('td a.delete_personal_time').parent().show();
		},
		function(){
			$(this).find('td a.delete_personal_time').parent().hide();
			$(this).find('#pt' + $(this).attr('rel')).remove();
			$(this).find('a.delete_personal_time').text('Delete');
			times_clicked = 0;
		}
	);
}

function prep_delete_appt(){
	$('.toggle_delete_mini').hover(
		function(){
			$(this).find('span[class="mini_cancel_button"]').addClass('ui-corner-all').show();
		},
		function(){
			$(this).find('span[class="mini_cancel_button ui-corner-all"]').removeClass('ui-corner-all').hide();
		
		}
	);
	
	$('span.mini_cancel_button').click(function(){
		
		
		var x = $(this).offset().left;
		var y = $(this).offset().top;
		
		var rel = $(this).attr('rel');
		
		var cssObj = {
				'position' : 'absolute',
				'top' : y + 10,
				'left' : x - 110,
				'display' : 'block'
		};
			  
		$('#are_you_sure_mini_' + rel).css(cssObj);
		
		$(this).hide();
	});
	
	$('.are_you_sure_mini').hover(
		function(){
				
		},
		function(){
			$(this).hide();
		}
	);
	
	$('.nevermind_cancel').click(function(){
		$(this).parent().parent().hide();									  
	});
	
	$('.yes_delete_id').click(function(){
		var template_vals = 'appt_delete_id=' + $(this).attr('rel');
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/delete_appointment/", 
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
				}else{
					//update teh mini schedule
					update_mini_schedule($('#formated_date').val());
				}
			}
		});
	});
	
}



function prep_appt_confirmations(){
	$('.not_confirmed').hover(
		function(){
			$(this).find('.confirm_appt,.decline_appt').show();	
		},
		function(){
			$(this).find('.confirm_appt,.decline_appt').hide();	
		}
	);
	
	$('.confirm_appt').click(function(){
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/confirm_decline_appt/" + $(this).attr('rel') + "/confirm",
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.message);
				}else{
					//it's been confirmed
					update_mini_schedule($('#formated_date').val());
				}
			}
		});
	});
	
	$('.decline_appt').click(function(){
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/confirm_decline_appt/" + $(this).attr('rel') + "/decline",
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.message);
				}else{
					//it's been confirmed
					update_mini_schedule($('#formated_date').val());
				}
			}
		});
	});
	
	$('.decline_appt,.confirm_appt').click(function(){
		var rel = $(this).attr('rel');
		$('#unconfirmed' + rel).remove();
		//alert($('#ucappt_list').height());
		if($('#ucappt_list').height() < 400){
			$('#ucappt_overflow').css({height:'auto',width:200});
		}
		
	});
	
}



function mini_cal_preps(){
	prep_pencilem_bar();
	prep_quickview();
	prep_notes();
	prep_price_override();
	prep_note_edit();
	prep_add_note();
	prep_personal_time();
	prep_delete_appt();
	prep_appt_confirmations();
}



function show_all_phone(){
	$('#show_all_phone').click(function(){
		//change the button text
		var obj = $('.display_toggle');
		$.each(
			obj,
			function(key,val){
				if($(val).css('display') == 'none'){
					$('#show_all_phone').text('hide');
					$(val).show();
				}else{
					$('#show_all_phone').text('show all');
					$(val).hide();
				}
			}
		);
		return(false);
	});
}

function open_print_window(date){
	var win_name = 'print_window';
	window.open(
				BASE_URL + 'schedule/p/' + date, 
				win_name, 
				'height=600,width=500, toolbar=no, menubar=yes, scrollbars=yes, resizable=yes,location=no, directories=yes, status=no');								
}

function client_details(msg){
	//set the client address
	var client_address_html = '<address>';
	client_address_html += (msg.address != '') ? msg.address + '<br />' : '';
	client_address_html += (msg.city != '')? msg.city + ', ' : '';
	client_address_html += (msg.state != '') ? msg.state : '';
	client_address_html += (msg.zip != '') ? ' ' + msg.zip : '';
	client_address_html += '</address>';
	
	$('#client_address_tbl').html(client_address_html);
	
	//create the hide link for the show all phone
	var obj = $('.display_toggle');
	var hidden = true;
	$.each(obj,function(key,val){
		if($(val).css('display') == 'table-row'){
			hidden = false;	
		}
	});
	var toggle_button;
	if(hidden === true){
		toggle_button = "<a href='#' id='show_all_phone' class='ui-corner-all'>show all</a>";
	}else{
		toggle_button = "<a href='#' id='show_all_phone' class='ui-corner-all'>hide</a>";
	}
	
	$('#client_phone_tbl').html(msg.phone + toggle_button);
	$('#client_cell_tbl').text(msg.cell_phone);
	$('#client_work_tbl').text(msg.work_phone);
	$('#client_email_tbl').text(msg.email);
	$('#client_name_tbl').text(msg.fname + ' ' + msg.lname);
	
	//set up the birthday
	if(msg.birthday != ''){
		$('#client_birthday_tbl').text(msg.birthday);
		$('#birthday_holder').show();
	}else{
		$('#client_birthday_tbl').text(msg.birthday);
		$('#birthday_holder').hide();
	}
	
	show_all_phone();
	update_client_list();

	//set up the edit form, just in case
	$('#editfname').val(msg.fname);
	$('#editlname').val(msg.lname);
	$('#editemail').val(msg.email);
	$('#editphone').val(msg.phone);
	$('#editaddress').val(msg.address);
	$('#editcity').val(msg.city);
	$('#editstate').val((msg.state == '')?'CA':msg.state);
	$('#editzip').val(msg.zip);
	$('#editclient_id').val(msg.client_id);
	$('#editbirthday').val(msg.birthday);
	
	//hide the loader
	//$('#client_details_loading').hide();
}



function set_up_client_trigger(el){
	$('#client_details_loading').show();
	$('#client_appt_history').html('');
	$('#client_address').html('');
	$('#client_phone').html('');
	$('#client_email').html('');
	$('#tabs-2').removeClass('group_icon');
	$('.edit_client_link').attr('rel',$(el).attr('rel')).show();
	
	//get the client details
	$.ajax({
		type: "POST",
		url: BASE_URL + "clients/get_client_details/" + $(el).attr('rel'),
		dataType: 'json',
		success: function(msg){
			if(msg.error){
				alert('There was a problem selecting this clients details');
			}else{
				//set the client address
				/*
				var client_address_html = '<address>';
				client_address_html += (msg.address != '') ? msg.address + '<br />' : '';
				client_address_html += (msg.city != '')? msg.city + ', ' : '';
				client_address_html += (msg.state != '') ? msg.state : '';
				client_address_html += (msg.zip != '') ? ' ' + msg.zip : '';
				client_address_html += '</address>';
				
				$('#client_address').html(client_address_html);
				$('#client_phone').html('Phone: <span class="client_color">' + msg.phone + '</span>');
				$('#client_email').html('Email: <span class="client_color">' + msg.email + '</span>');
				
				//set up the edit form, just in case
				$('#editfname').val(msg.fname);
				$('#editlname').val(msg.lname);
				$('#editemail').val(msg.email);
				$('#editphone').val(msg.phone);
				$('#editaddress').val(msg.address);
				$('#editcity').val(msg.city);
				$('#editstate').val(msg.state);
				$('#editzip').val(msg.zip);
				
				//hide the loader
				$('#client_details_loading').hide();
				*/
				client_details(msg);
			}
		}
	});
	
	//get the appoitment log
	$.ajax({
		type: "POST",
		url: BASE_URL + "clients/get_appointment_history/" + $(el).attr('rel'), 
		dataType: 'json',
		success: function(msg){
			if(msg.error){
				$('#client_appt_history').html('<span id="num_appointments_span">0 Appts.</span><h2>Appointment History</h2>' + msg.error);
			}else{
				$('#client_appt_history').html('<span id="num_appointments_span">' + msg.num_appts + ' Appts.</span><h2>Appointment History</h2>' + msg.appt_table);
			}
		}
	});
	return(false);
}

function format_service_length(length){
	//create an object to return
	var return_obj = {};
	
	var length_in_minutes = length;
	
	if(length_in_minutes == '' || length_in_minutes === null){
		return_obj.format_length = 'Service length needed';
	}else{
		var length_in_hours		= length_in_minutes / 60;
		return_obj.format_length = (length_in_hours < 1) ? length_in_minutes + ' minutes' : (length_in_hours > 1) ? length_in_hours + ' hours' : length_in_hours + ' hour';
	}
	
	return_obj.length_in_minutes = length;
	
	return return_obj;
}


function service_trigger(){
	$('.service_trigger').click(function(){
		//apply a style to the selected link
		$('.attention').removeClass('attention');
		$(this).addClass('attention');
		
		
		$('#service_detail_loader').show();
		$('#service_client_loader').show();
		$('#service_edit_block a').show(200);
		
		//get the service details
		$.ajax({
			type: "POST",
			url: BASE_URL + "services/get_service_details/" + $(this).attr('rel'), 
			dataType: 'json',
			success: function(msg){
				//service length
				var service = format_service_length(msg.length);
				
				//email display
				var email_display = (msg.email_display == 'y') ? 'Yes' : 'No';

				$('#service_name').text(msg.service_name);
				$('#service_length').text(service.format_length);
				$('#service_price').text('$' + msg.service_price);
				$('#service_notes').text(msg.service_notes);
				$('#service_email').text(email_display);
				//$('#service_date_created').text(msg.date_created);
				$('#service_detail_loader').hide();
				
				//populate the edit form
				$('#service_name_field').val(msg.service_name);
				$('#service_length_field').val(service.length_in_minutes);
				$('#service_price_field').val(msg.service_price);
				$('#service_notes_field').val(msg.service_notes);
				$('#service_id_field').val(msg.service_id);
				$('#edit_email_display').val(msg.email_display);
			}
		});
				
		//get the client list
		$.ajax({
			type: "POST",
			url: BASE_URL + "services/get_client_services/" + $(this).attr('rel'), 
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					$('#service_client_list').html(msg.error);	
				}else{
					$('#service_client_list').html(msg.client_list);
				}
				$('#service_client_loader').hide();
			}
		});
		
		//return false
		return(false);
		
	});
}




function enable_change_graph(){
	$('#graph_type').change(function(){
		var graph = $(this).val();
		switch(graph){
			case "appt_overview":
				$('#graph_month,#run_pie').hide();
				$.ajax({
					type: "POST",
					url: BASE_URL + "settings/appt_xml/",
					success: function(msg){
						//update the flash graph
						var chart2 = new FusionCharts("charts/FCF_MSLine.swf", "ChId1", "525", "250");
						chart2.setDataXML(msg);
						chart2.render("graph");
						$('#graph_loader').hide();
					}
				});
				break;
			case "month_pie":
				$('#graph_month,#run_pie').show();
				break;
			case "earnings":
				$('#graph_month,#run_pie').hide();
				$.ajax({
					type: "POST",
					url: BASE_URL + "settings/earnings_xml/",
					success: function(msg){
						//update the flash graph						
						var chart1 = new FusionCharts("charts/FCF_Column2D.swf", "ChId1", "525", "250");
						chart1.setDataXML(msg);
						chart1.render("graph");
					}
				});

				break;
		}
	});
}

function toggle_settings_rolldown(){
	
	//get the window height
	var window_height = $(window).height();
	var window_width = $(window).width();
	
	//how tall should the window be?
	var settings_height = (window_height * 1) - 150;
	
	//set the height of the left and right settings boxes
	$('#left_settings, #right_settings').css('height',settings_height);
	
	//toggle the view
	if($('#settings_cont').css('top') == '-15px'){
		//set the body overflow to auto
		$('body').css('overflow','auto');
		
		$('#settings_cont').animate({ top: "-8000px" }, 1000);
		$('#whiteout').hide();
		//reset the settings window?
		$('#graph').html('');
	}else{
		//load the account overview by default
		$('#settings_module').load(BASE_URL + 'settings/account_overview',function(){
			$('#graph_loader').show();
			enable_change_graph();
			$.ajax({
				type: "POST",
				url: BASE_URL + "settings/appt_xml/",
				success: function(msg){
					//update the flash graph
					var chart2 = new FusionCharts("charts/FCF_MSLine.swf", "ChId1", "525", "250");
					chart2.setDataXML(msg);
					chart2.render("graph");
					$('#graph_loader').hide();
				}
			});
		});
		
		//remove the selected item
		$('.selected_setting').removeClass('selected_setting');
				
		//set the body overflow to hidden
		$('body').css('overflow','hidden');
		
		//grey out the screen
		$('#whiteout').css({width:window_width,height:window_height}).show();
		
		//move the setings box into view
		$('#settings_cont').animate({ top: "-15px" }, 400);
		
		//update the percent complete
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/setup_stats/",
			dataType: 'json',
			success: function(msg){
				$('#percent_progress').animate({width:msg.percent + '%'});
				$('#span_num').text(msg.percent);
				
				//update the clients setting
				if(msg.clients.num_clients * 1 > 0){
					$('#setup_list li:contains("Enter your Client List")').removeClass('incomplete').addClass('complete');
				}else{
					$('#setup_list li:contains("Enter your Client List")').removeClass('complete').addClass('incomplete');
				}
				$('#num_client_setting').text(msg.clients.num_clients);
				
				//update the services
				if(msg.services.num_services * 1 > 0){
					$('#setup_list li:contains("Enter your Services List")').removeClass('incomplete').addClass('complete');
				}else{
					$('#setup_list li:contains("Enter your Services List")').removeClass('complete').addClass('incomplete');
				}
				$('#num_services_setting').text(msg.services.num_services);
			}
		});
		
	}
}


function prep_pencilem_bar(){
	//get the day stats
	$.ajax({
		type: "POST",
		url: BASE_URL + "schedule/pencilem_bar_totals/" + $('#formated_date').val(),
		dataType: 'json',
		success: function(msg){
			if(msg.error){
				
			}else{
				$('#daily_appts_today').text(msg.num_appts);
				$('#daily_revenue').text(msg.rev_total);
			}
		}
	});
}

function get_the_blog(){
	$.ajax({
		type: "POST",
		url: BASE_URL + "schedule/get_blog/",
		dataType: 'json',
		success: function(msg){
			$('#current_blog').html('<a target="_blank" class="blog_links" href="' + msg.url + '">' + msg.title + '</a>');
		}
	});
}






function run_note_edit(obj,edit_button,has_editor){
	var appt_id = obj.attr('rel');
	var current_note = obj.find('span[class="note_cont_' + appt_id + '"]').text();
	var cont_width = (obj.width() * 1) - 13;
	var cont_height = 70;
	
	if(has_editor === true){
		obj.addClass('has_editor');
	}
	
	if(obj.hasClass('has_editor')){
		obj.unbind('click');
	}
	
	//set up function vars
	//hide the current notes
	obj.find('span[class="note_cont_' + appt_id + '"]').hide();
	
	//insert the text area
	var text_area = "<div id='edit_div_" + appt_id + "'><textarea id='note_text_" + appt_id + "'>" + current_note + "</textarea><br /><button class='edit_note_button' id='edit_note_button_" + appt_id + "'>Save Note</button></div>";
	obj.find('span[class="note_cont_' + appt_id + '"]').before(text_area);
	
	//catch the enter key
	$('#note_text_' + appt_id).keydown(function(e){
		var key = e.charCode || e.keyCode || 0;
		if(key == 13){
			//it was an enter button
			$('#edit_note_button_' + appt_id).click();
		}
	});
	
	$('#note_text_' + appt_id).css({'height':cont_height,'width':cont_width});
	
	//set up the button edit
	$('#edit_note_button_' + appt_id).click(function(){
		
		//grab the new note
		var new_note = escape($('#note_text_' + appt_id).val());
		
		//ajax the new note the the server
		var template_vals = 'appt_id=' + appt_id + '&new_note=' + new_note;
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/update_note/", 
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
				}
			}
		});
		
		//destroy the text area and button
		$('#edit_div_' + appt_id).hide();
		
		//populate and show the new comments
		obj.find('span[class="note_cont_' + appt_id + '"]').text($('#note_text_' + appt_id).val()).show();
		
		//remove the div
		$('#edit_div_' + appt_id).remove();
		
		//remove the has editor class
		obj.removeClass('has_editor');
		
		//reapply the on click
		edit_button.show();
	});
}

function edit_note_reapply(obj){
	$('.appt_note_style').bind('click',run_note_edit(obj,true));
}



function update_montly_calendar(){
	
	var now = new Date();
	var month = dateFormat(now,"mm");
	var year = dateFormat(now,"yyyy");
	var url = BASE_URL + 'schedule/monthly_calendar/' + month + '/' + year + '/' + dateFormat(now,"yyyymmddHHMMss");
	//var url = BASE_URL + 'schedule/monthly_calendar/06/' + year;
	
	$.ajax({
		type: "GET",
		url: url,
		dataType: "json",
		success: function(msg){
			$('#tabs-4').html(msg.calendar);
			$('#cal_load').hide();
			prep_next_prev();
		}
	});
	
}

function prep_next_prev(){
	//$('#current_day').effect('highlight',{color:'#0A757E'},1500);// FAILS IN IE6 
	$('#cal_load').show();
	$('#cal_next').click(function(){
		cal_refresh = 'no';
		var cal_url = $(this).attr('href');
		$.ajax({
			type: "GET",
			url: cal_url,
			dataType: "json",
			success: function(msg){
				$('#tabs-4').html(msg.calendar);
				$('#cal_load').hide();
				prep_next_prev();
			}
		});
		
		return(false);							  
	});
	
	$('#cal_prev').click(function(){
		cal_refresh = 'no';
		var cal_url = $(this).attr('href');
		$.ajax({
			type: "GET",
			url: cal_url,
			dataType: "json",
			success: function(msg){
				$('#tabs-4').html(msg.calendar);
				$('#cal_load').hide();
				prep_next_prev();
			}
		});
		
		return(false);							  
	});
	
	$('#cal_display td').click(function(){
		//add the cell highlighting
		if($(this).hasClass('go_highlight')){
			//remove the current styling
			//('.cal_cell_highlight').find('span[class="launch_mini_sched"]').hide();
			$('.cal_cell_highlight').removeClass('cal_cell_highlight');
			
			//set up the new styles
			$(this).addClass('cal_cell_highlight');
			//$(this).find('span[class="launch_mini_sched"]').show();
			
			//set the date for the scheduler
			$('#cal_current_cell_date').val($(this).attr('rel'));
			
			//create a date object out of the date
			var sql_date = $(this).attr('rel');
			var temp = sql_date.split('-');
			var year = temp[0];
			var month = temp[1];
			var day	= temp[2];
			var d = new Date();
			d.setFullYear(year,month - 1,day);
			var date_mask = dateFormat(d,"ddd. mmm dd, yyyy.");
			$('#cal_cur_date_clicked').text(date_mask);
			if($('#mini_schedule_box').css('display') == 'none'){
				//finaly show the schedule box
				$('#mini_schedule_box').show();									   
			}
		}
		
	});
	
	$('#cal_display td').hover(
		function(){
			$(this).find('span[class="click_to_add"]').show();
		},
		function(){
			$(this).find('span[class="click_to_add"]').hide();
		}
	);
	
	
	$('.appt_list_node').each(function(i){
		$(this).qtip({
			content : $(this).html() + "<div style='margin-top:5px;'>" + $(this).find("input[name='hiddennotes']").val() + "</div>",
			show: 'mouseover',
			hide: 'mouseout',
			position: {
				corner: {
					target: 'topLeft',
					tooltip: 'bottomLeft'
				}
			},
			adjust: {
				screen: true
			},
			style: { 
				name: 'red',
				tip: 'bottomLeft'
			}
		});
	});
	
	$('div#cal_tools a').click(function(){
		var url = $(this).attr('href');
		$.ajax({
			type: "GET",
			url: url,
			dataType: "json",
			success: function(msg){
				$('#tabs-4').html(msg.calendar);
				$('#cal_load').hide();
				prep_next_prev();
			}
		});
		
		return(false);
	});
	
	$('#cal_go_schedule').click(function(){
		var template_vals = 'service_id=' + $('#cal_service_dd').val() + '&client_email=' + $('#cal_clients').val() + '&formated_date=' + $('#cal_current_cell_date').val() + '&hour=' + $('#cal_hour').val() + '&minute=' + $('#cal_minute').val() + '&suffix=' + $('#cal_suffix').val();									 
		$.ajax({
			type: "POST",
			url: BASE_URL + 'schedule/create/',
			dataType: "json",
			data: template_vals,
			success: function(msg){
				if(msg.error){
					if(msg.dialog == 'dialog'){
						$('#error_dialog').html(msg.message).dialog('open');	
					}else if(msg.dialog == 'existing_appointment'){
						$('#existing_appointment').html(msg.message).dialog('open');
					}else if(msg.dialog == 'already_booked'){
						$('#already_booked').html(msg.message).dialog('open');
					}
				}else{
					//launch the dialog
					//$('#dialog').html(msg.message).dialog('open');
					
					//update the calendar
					var url = $('div#cal_tools a:contains("Refresh Calendar")').attr('href');
					$.ajax({
						type: "GET",
						url: url,
						dataType: "json",
						success: function(msg){
							$('#tabs-4').html(msg.calendar);
							$('#cal_load').hide();
							prep_next_prev();
						}
					});
		
					return(false);
				}
			}
		});
	});
	
}

function update_appt_table(date){
	$.ajax({
		type: "POST",
		url: BASE_URL + "schedule/upcoming_appointments/",
		data: date,
		dataType: 'json',
		success: function(msg){
			if(msg.error){
				$('#edit_ajax_load').hide();
				$('#appt_holder').html("<span class='ui-state-error ui-corner-all' id='no-appointments'><span class='ui-icon ui-icon-alert' style='float: left; margin-right: 0.3em;'/>" + msg.message + "</span>");
			}else{
				$('#appt_holder').html(msg.ul);
				$('#edit_ajax_load').hide();
				$('#appt_list_ul li').hover(
					function(){
						$(this).addClass('appt_list_hover');
						//$(this).animate({'backgroundColor':'#E4E4E4'});
					},
					function(){
						$(this).removeClass('appt_list_hover');
						//$(this).animate({'backgroundColor':'#FFFFFF'});
					}
				);
				$('#appt_list_ul li').click(function(){
					//get the appt details
					var cur_date = $('#edit_formated_date').val(); //2009-03-15
					var date_pieces = cur_date.split('-');
					var date_obj = new Date(date_pieces[0],(date_pieces[1] * 1) - 1,date_pieces[2]);
					var notes = $(this).find('span[class="real_notes"]').text();
					var recurring = $(this).find('span[class="recur-right"]');
					
					$('#update_appt_id').val($(this).attr('rel'));
					
					//date_obj.setFullYear(date_pieces[0],(date_pieces[1] * 1) - 1,date_pieces[2]);
					
					//passin the date to the edit calendar
					//$('#update_appt_datepicker').datepicker('option',{defaultDate:date_obj});
					$('#update_appt_datepicker').datepicker({
						showButtonPanel: false,
						changeMonth: true,
						changeYear: true,
						dateFormat: 'DD MM dd, yy',
						altField: '#update_formated_date', 
						altFormat: 'yy-mm-dd',
						defaultDate: date_obj
					});							
					
					//pass in the time
					var time_chunk = $(this).find('span[class="time"]').text();
					var time_pieces = time_chunk.split('~'); //7~00~pm
					$('#update_hour').val(time_pieces[0]);
					$('#update_minutes').val(time_pieces[1]);
					$('#update_suffix').val(time_pieces[2].toLowerCase());
					$('#update_appt_notes').val(notes);
					$('#update_char_tracker').text((250 - notes.length) + ' Characters Left');
					var service_id = ($(this).find('span[class="service_id_for_edit"]').text() == '') ? 0 : $(this).find('span[class="service_id_for_edit"]').text();
					$('#edit_service_id').val(service_id);
					
					//show the recurring logo
					if(recurring.length){//it's recurring, show the recurring logo
						$("#edit_appt_dialog div#info_edit_appt").hide();
						$("#edit_appt_dialog div#recurring_edit").show();
						$('#is_recurring').val('y');
					}else{//not recurring
						$("#edit_appt_dialog div#info_edit_appt").show();
						$("#edit_appt_dialog div#recurring_edit").hide();
						$('#is_recurring').val('n');
					}
										
					//open the dialog
					$("#edit_appt_dialog").dialog('open');							 
				});
			}
		}
	});
}


function update_client_list(){
	$.ajax({
		type: "POST",
		url: BASE_URL + 'clients/client_list/',
		data: 'ispost=yes',
		dataType: "json",
		success: function(msg){
			$('#client_rows').html(msg.client_list);
			client_list_prep();
		}
	});
}

function client_list_prep(){
	$('.load_client_trigger').click(function(){
		//client_id
		var c_id = $(this).attr('rel');
		
		//make sure it's showing history first
		$('#list_type_cont').text('Appointment History');
		$('#get_future_appt').text('View Future');
											 
		//put the attention on selected element
		$('.attention').removeClass('attention');
		$(this).addClass('attention');
		
		//show the loading icons
		$('#client_detail_loader').show();
		$('#client_history_loader').show();
		
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/get_client_details/" + c_id,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert('There was a problem selecting this clients details');
				}else{
					client_details_loader(msg);				
				}
			}
		});
		
		if($('#edit_block').css('display') == 'none'){
			$('#edit_block').show();
		}
		
		//get the appointment history
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/get_appointment_history/" + c_id, 
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					$('#appt_history').html(msg.error);
				}else{
					$('#appt_history').html(msg.appt_table);
				}
					
				$('#get_future_appt').attr('rel',c_id);
				$('#get_future_appt').show();
				
				$('#client_history_loader').hide();
			}
		});
		return(false);
		
	});
}

function gen_service_list(){
	$.ajax({
		type: "POST",
		url: BASE_URL + "services/gen_services_list/",
		data: 'is_ajax=werd',
		dataType: 'json',
		success: function(msg){//send json
			if(msg.error){
				alert('There was a system error while saving this service.  Please try again.');
			}else{
				$('#service_scroller').html(msg.service_list);
				
				//prep the services for click
				service_trigger();
			}
		}
	});
}

function prep_group_list(){
	$('.group_list_trigger').click(function(){
		//attention
		$('.attention').removeClass('attention');
		$(this).addClass('attention');

		load_group_details($(this).attr('rel'));
		
		return(false);
	});
}

function load_group_details(group_id){
//show loading
		$('#service_detail_loader').show();
		$('#service_client_loader').show();
		$('#service_edit_block a').show(200);
		
		//ajax the group info
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/get_group_info/" + group_id, 
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert('There was a system error while selecting this group\'s info.  Please try again.');
				}else{
					$('#group_name').text(msg.group_name);
					$('#group_notes').text(msg.group_notes);
					$('#group_created').text(msg.created);
					$('#num_in_group').text(msg.num_clients);
					
					//set up the edit form
					$('#edit_group_name').val(msg.group_name);
					$('#edit_group_notes').val(msg.group_notes);
					$('#group_id').val(group_id);
				}
				$('#service_detail_loader').hide();
			}
		});
		
		//ajax the client in this group
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/get_client_in_group/" + group_id, 
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					$('#service_client_list').html(msg.msg);
				}else{
					$('#service_client_list').html(msg.cig);
				}
				$('#service_client_loader').hide();
			}
		});}

function prep_manage_groups(){
	$('.manage_group').click(function(){
		//open the dialog
		if($('#manage_group_loader').css('display') == 'none'){
			$('#manage_group_loader').show();
		}
		$('#manage_group_dialog').dialog('open');	  
		
		//load up the client list with check boxes
		var group_id = $(this).attr('rel');
		
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/manage_groups_client_list/", 
			dataType: 'json',
			data: 'group_id=' + group_id,
			success: function(msg){
				if(msg.error){
					alert('There was a problem opening the Group Manager.  Please try again.');
				}else{
					$('#manage_group_loader').hide();
					$('#client_group_placeholder').html(msg.group_client_list).show('blind',{direction:"vertical"},350);
					$('#managing_group').val(group_id);
					prep_group_checkbox();
					
					//open the dialog
					load_group_details(group_id);
				}
			}
		});
	});
}

function prep_group_checkbox(){
	$('.add_remove_group').click(function(){
		var managing_group = $('#managing_group').val();
		var client_id = $(this).attr('rel');
		
		var template_vals = 'group_id=' + managing_group + '&client_id=' + client_id;
		
		if($(this).attr('checked') === true){
			template_vals += '&action=add';	
		}else{
			template_vals += '&action=remove';	
		}
		
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/group_management/", 
			dataType: 'json',
			data: template_vals,
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
				}
			}
		});
	});
}

function update_client_dd(){
	$.ajax({
		type: "POST",
		url: BASE_URL + "clients/email_to_dd/json", 
		dataType: 'json',
		success: function(msg){
			$('#client_drop_down_cont').html(msg.client_dd);
		}
	});
}

function prep_contractor_list(){
	$('.contractor_trigger').click(function(){
		//attention
		$('.attention').removeClass('attention');
		$(this).addClass('attention');
											
		$('#service_detail_loader').show();
		var template_vals = 'contractor_id=' + $(this).attr('rel');
		$.ajax({
			type: "POST",
			url: BASE_URL + "contractors/get_contractor_details/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				$('#contractor_name').text(msg.fname + ' ' + msg.lname);
				$('#contractor_email').text(msg.email);
				$('#contractor_phone').text(msg.phone);
				$('#contractor_date_created').text(msg.account_created);
				
				//is the user active?
				var active;
				if(msg.is_active == 'y'){
					active = 'yes';
				}else{
					active = 'no';	
				}
				
				$('#is_active').text(active);
				$('#service_detail_loader').hide();
				
				//populate the edit form
				$('#edit_contractor_fname').val(msg.fname);
				$('#edit_contractor_lname').val(msg.lname);
				$('#edit_contractor_email').val(msg.email);
				$('#edit_contractor_phone').val(msg.phone);
				$('#editisactive').val(msg.is_active);
				$('#edit_contractor_id').val(msg.contractor_id);
				
				//show the edit
				if($('#service_edit_block a').css('display') == 'none'){
					$('#service_edit_block a').show(200);
				}
			}
		});
		return(false);
	});
}

function load_week_view(date){
	//var start = (date === '' || date === null) ? '' : date;
	var start = '';
	$.ajax({
		type: "POST",
		url: BASE_URL + "schedule/week_view/" + start,
		dataType: 'json',
		success: function(msg){
			$('#week_cont').html(msg.week);
			$('#week_load').hide();
			$('#weekly_start_date_input').val(msg.display_date);
			prep_weekly_rows();
		}
	});
}

function prep_weekly_rows(){
	$('.week_highlight').hover(
		function(){
			$(this).css('background-color','#F4D7CA');	
		},
		function(){
			$(this).css('background-color','#FFFFFF');
		}
	);
	
	//grab all the divs that are labeled "parent_border" and add a border to the parent
	var highlightObj = {
		'background-color' : '#AFD6DC',
		'border-bottom'    : 'none'
	};
	var phighlightObj = {
		'background-color' : '#F4D7CA',
		'border-bottom'    : 'none'
	};
	var uchighlightObj = {
		'background-color' : '#FFCC66',
		'border-bottom'    : 'none'
	};
	
	var text = $('.parent_border').parent();
	$.each(text,function(i,val){
		if($(val).hasClass('highlight')){
			$(val).removeClass('highlight').css(highlightObj);
		}else if($(val).hasClass('personal_highlight')){
			$(val).removeClass('personal_highlight').css(phighlightObj);
		}else if($(val).hasClass('unconfirmed_highlight')){
			$(val).removeClass('unconfirmed_highlight').css(uchighlightObj);
		}
	});
	
	$('.bookable').hover(
		function(){
			$(this).html('<span style="color:#ff0000;">Book Now</span>');	
		},
		function(){
			$(this).html('---');
		}
	);
	
	//make empty cells clickable 
	//once clicked, the create appointment tab should be selected, and the date and time should be filled out
	$('.bookable').click(function(){
		var lets_book = $(this).attr('rel');
		var temp = lets_book.split(' ');
		var date = temp[0];
		
		//figure the month day and year
		var date_temp = date.split('-');
		var year = date_temp[0];
		var month = (date_temp[1] * 1) - 1;
		var day = date_temp[2];
		
		//figure the time
		var time_temp = temp[1].split(':');
		var hour = time_temp[0];
		var minute = time_temp[1];
		var suffix = temp[2];
		
		//set up the form
		$('#hour').val(hour);
		$('#minute').val(minute);
		$('#suffix').val(suffix);
		
		var new_date = new Date(year,month,day);
		$("#datepicker").datepicker('setDate',new_date);
		
		//set the minischedule date
		var mini_format_date = dateFormat(new_date,"dddd mmmm d, yyyy");
		$('#minisched h3 span#currentscheduledate').text(mini_format_date);
		
		//select the first tab
		$("#tabs").tabs('select',0);
		
	});
		
}

function date_picker_select(){
	var temp = $('#formated_date').val().split('-');
	var new_date = new Date(temp[0],(temp[1] * 1) - 1,temp[2]);

	
	var mini_cal_header = dateFormat(new_date,"dddd mmmm d, yyyy");
	$('#minisched h3 span#currentscheduledate').text(mini_cal_header);
	
	$('#mini-no-appointments').hide();
	$('#appt_list_ul_mini').hide();
	$('#mini_ajax_load').show();
	//select the upcoming stuff
	update_mini_schedule($('#formated_date').val());
}


$(document).ready(function(){	
	
	var now = new Date();
	
	//write to the mini schedule
	if($('#currentscheduledate').length > 0){
		$('#currentscheduledate').text(dateFormat(now,"dddd mmmm d, yyyy"));
		$('#mini_ajax_load').show();
		//update the mini schedule
		update_mini_schedule(dateFormat(now,"isoDate"));
	}
		
	//ui settings
	$.ui.dialog.defaults.bgiframe = true;
	
	
	$("#edit_contractor_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		width: 450,
		close: function(){
			$('#edit_contractor_error').html('').hide();
		},
		buttons: {
			'Save Changes': function() {
				var template_vals = $('#edit_contractor').serialize();
				
				$.ajax({
					type: "POST",
					url: BASE_URL + "contractors/edit/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){
						if(msg.error){
							$('#edit_contractor_error').html(msg.msg).show(200);
						}else{
							//update the page
							$('#contractor_name').text($('#edit_contractor_fname').val() + ' ' + $('#edit_contractor_lname').val());
							$('#contractor_email').text($('#edit_contractor_email').val());
							$('#contractor_phone').text($('#edit_contractor_phone').val());
							
							var activity;
							if($('#editisactive').val() == 'y'){
								activity = 'yes';	
							}else{
								activity = 'no';
							}
							$('#is_active').text(activity);
							
							//reload the Contractor LIst
							$.ajax({
								type: "POST",
								url: BASE_URL + "contractors/contractor_list/json",
								dataType: 'json',
								success: function(msg){
									$('#service_scroller').html(msg.contractor_list);
									prep_contractor_list();
								}
							});
							//close the dialog
							$("#edit_contractor_dialog").dialog('close');
						}
					}
				});
				
				
			},
			'Cancel' : function(){
				$(this).dialog('close');	
			}
		}
	});

	$("#add_contractor_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		width: 450,
		close: function(){
			$('#add_contractor_error_cont').html('').hide();
		},
		buttons: {
			'Add Contractor': function() {
				var template_vals = $('#add_contractor').serialize();
				$.ajax({
					type: "POST",
					url: BASE_URL + "contractors/add_contractor/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){
						if(msg.error){
							$('#add_contractor_error_cont').html(msg.message).show('blind',200);
						}else{
							//update the page 
							$.ajax({
								type: "POST",
								url: BASE_URL + "contractors/contractor_list/json",
								data: template_vals,
								dataType: 'json',
								success: function(msg){
									$('#service_scroller').html(msg.contractor_list);
									prep_contractor_list();
								}
							});
							
							//close the dialot
							$("#add_contractor_dialog").dialog('close');
						}
					}
				});
			},
			'Cancel' : function() {
				$(this).dialog('close');	
			}
		}
	});
	
	
	$("#recurring_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		width: 460,
		buttons: {
			'Create Recurring' : function(){
				//set the proper form values
				var int = $('#recur_interval').val();
				var tp	= $('#recur_time_period').val();
				$('#recurring_appt').val('y');
				$('#recurring_interval').val(int);
				$('#recurring_time_period').val(tp);
				
				//update the page to reflect the change
				var plurality = (int > 1) ? 's' : '';
				$('#recurring_cont').text('This appt. will recur every ' + int + ' ' + tp.toLowerCase() + plurality + '.');
				
				$(this).dialog('close');
			},
			'Cancel' : function(){
				$(this).dialog('close');
			}
		}
	});
	
	$("#quickview_modal").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		width: 400,
		buttons: {
			'Close' : function(){
				$(this).dialog('close');
			}
		}
	});
	
	$("#client_notes_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		width: 600,
		close: function(){
			$('#client_note_saved').hide();
			$('#client_note_saving').hide();
			$('#client_note_error').hide();
		},
		buttons: {
			'Save Notes' : function(){	
				//show the loading
				$('#client_note_saving').show();
				$('#client_note_saved').hide();
				$('#client_note_error').hide();
				
				//ajax the notes to the server along with the client ID for the update
				//var template_vals = "notes=" + $('#client_notes_input').val() + "&client_id=" + $('#editclient_id').val();
				var template_val = $('#client_notes_form').serialize();
				template_vals = template_val + "&client_id=" + $('#editclient_id').val();
				$.ajax({
					type: "POST",
					url: BASE_URL + "clients/save_notes/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){
						if(msg.error){
							alert(msg.msg);
							$('#client_note_saved').hide();
							$('#client_note_saving').hide();
							$('#client_note_error').html(msg.msg).show();
						}else{
							$('#client_note_saved').show();
							$('#client_note_saving').hide();
							$('#client_note_error').hide();
						}
					}
				});
			},
			'Close' : function(){
				$(this).dialog('close');
			}
		}
	});
	
	$("#manage_group_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		width: 450,
		height:500,
		close: function(){
			load_group_details($('#managing_group').val());
			$('#client_group_placeholder').html('');
		},
		buttons: {
			'Save Changes': function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#delete_this_group_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		buttons: {
			'Go Ahead' : function(){
				$.ajax({
					type: "POST",
					url: BASE_URL + "clients/delete_group/",
					data: 'group_id=' + $('#group_id').val(),
					dataType: 'json',
					success: function(msg){
						if(msg.error){
							alert(msg.msg);
							$("#delete_this_group_dialog").dialog('close');
						}else{
							//we have a group list
							$("#service_scroller").html(msg.group_list);
							$("#service_edit_block a").hide();
							$("#delete_this_group_dialog").dialog('close');
							
							//set up the page
							$('#group_name').text('');
							$('#group_notes').text('');
							$('#group_created').text('');
							$('#num_in_group').text('');
							$('#service_client_list').text('Select a group from the list at the left.');
							
							prep_group_list();
						}
					}
				});
			},
			'Cancel': function() {
				$(this).dialog('close');
			}
		}
	});
	
	
	$("#edit_group_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		width:400,
		modal: false,
		buttons: {
			'Save Changes' : function(){
				var template_vals = $('#edit_group').serialize();
				$.ajax({
					type: "POST",
					url: BASE_URL + "clients/edit_group/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){
						if(msg.error){
							$('#edit_group_error').html(msg.msg).show(200);
						}else{
							if(msg.group_list != 'no_groups'){
								//we have a group list
								$("#service_scroller").html(msg.group_list);
								$("#edit_group_dialog").dialog('close');
								
								//set up the page
								$('#group_name').text($('#edit_group_name').val());
								$('#group_notes').text($('#edit_group_notes').val());
								
								prep_group_list();
								prep_manage_groups();
							}
						}
					}
				});
			},
			'Cancel': function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#create_group_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		close: function(){
			$('#add_group_error').html('').hide();
			$('#add_group_name').val('');
			$('#add_group_notes').val('');
		},
		width:400,
		buttons: {
			'Create Group' : function(){
				var template_vals = $('#add_group').serialize();
				$.ajax({
					type: "POST",
					url: BASE_URL + "clients/create_group/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){
						if(msg.error){
							$('#add_group_error').html(msg.msg).show(200);
						}else{
							if(msg.group_list != 'no_groups'){
								//we have a group list
								$("#service_scroller").html(msg.group_list);
								$("#create_group_dialog").dialog('close');
								prep_group_list();
								prep_manage_groups();
								
							}
						}
					}
				});
			},
			'Cancel': function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#confirm_service_delete").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		buttons: {
			'Go ahead': function(){
				$.ajax({
					type: "POST",
					url: BASE_URL + "services/delete_service/",
					data: 'service_id=' + $('#service_id_field').val(),
					dataType: 'json',
					success: function(msg){
						if(msg.error){
							alert('There was a problem deleting this service. Please try again.');
						}else{
							gen_service_list();
							$('#service_name').text('');
							$('#service_length').text('');
							$('#service_price').text('');
							$('#service_notes').text('');
							//$('#service_date_created').text('');
							$('#service_edit_block a').hide();
							$('#service_client_list').text('Select a service from your service list at the left to view clients that book a specific type of service.');
						}
					}
				});
				$('#confirm_service_delete').dialog('close');
			},
			'Cancel': function() {
				$(this).dialog('close');
			}
		}
	});

	$("#edit_service_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		width:400,
		modal: false,
		buttons: {
			'Save Changes' : function(){
				var template_vals = $('#edit_service').serialize();
				
				$.ajax({
					type: "POST",
					url: BASE_URL + "services/edit_service/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){//send json
						if(msg.error){
							$('#edit_service_error').html(msg.msg).show('blind',200);
						}else{
							//reset the service details
							$('#service_name').text($('#service_name_field').val());
							$('#service_price').text('$' + $('#service_price_field').val());
							$('#service_notes').text($('#service_notes_field').val());
							$('#service_length').text(format_service_length($('#service_length_field').val()).format_length);
							
							//reload the services list
							gen_service_list();
							$("#edit_service_dialog").dialog('close');
						}
					}
				});

			},
			'Cancel': function() {
				$(this).dialog('close');
			}
		}
	});
	
	$('#service_h1 span').click(function(){
		$("#add_service_dialog").dialog('open');									 
	});
	
	$('#group_h1 span').click(function(){
		$("#create_group_dialog").dialog('open');									 
	});

	
	$("#add_service_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		width: 400,
		modal: false,
		close: function(){
			$('#add_service_error').hide();
		},		
		buttons: {
			'Add Service': function(){
				var template_vals = $('#add_service').serialize();
				
				$.ajax({
					type: "POST",
					url: BASE_URL + "services/add_service/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){//send json
						if(msg.error){//there was a problem
							$('#add_service_error').html(msg.msg).show('blind',200);	
						}else{
							gen_service_list();
							$("#add_service_dialog").dialog('close');
							
							//clear the add service dialog
							$('#add_service input[type="text"]').val('');
							$('#add_service_notes').val('');
							
							//hide any errors
							$('#add_service_error').hide();
						}
					}
				});
				
			},
			'Cancel': function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#settings_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		height: 200,
		modal: false,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		height: 200,
		modal: false,
		close: function(){
			update_mini_schedule($('#formated_date').val());
		},
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#welcome_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		height: 610,
		width: 600,
		modal: false,
		buttons: {
			"Close Window": function() {
				$(this).dialog('close');
			},
			"Don't show again": function(){
				$.ajax({
					type: "POST",
					url: BASE_URL + "settings/no_welcome_display/",
					dataType: 'json'
				});
				$(this).dialog('close');	
			}
		}
	});
	
	$("#already_booked").dialog({
		autoOpen: false,
		bgiframe: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	
	
	$("#edit_client_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		width:450,
		modal: false,
		close: function(){
			$('#modal_client_edit_error').hide();
		},
		buttons: {
			"Save Changes": function() {
				var template_vals = $('#edit_client_form_new').serialize();
				$.ajax({
					type: "POST",
					url: BASE_URL + "clients/run_edit/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){//send json
						if(msg.error){//there was a problem
							$('#modal_client_edit_error').html(msg.msg).show('blind',{direction:'vertical'},250);
						}else{
							//update the page with the new data
							var new_data = {};
							new_data.fname = $('#editfname').val();
							new_data.lname = $('#editlname').val();
							new_data.email = $('#editemail').val();
							new_data.phone = $('#editphone').val();
							new_data.cell_phone	= $('#editcellphone').val();
							new_data.work_phone = $('#editworkphone').val();
							new_data.address = $('#editaddress').val();
							new_data.city = $('#editcity').val();
							new_data.state = $('#editstate').val();
							new_data.zip = $('#editzip').val();
							new_data.client_id = $('#editclient_id').val();
							new_data.birthday = $('#editbirthday').val();
							client_details(new_data);
							
							//close the dialog
							$("#edit_client_dialog").dialog('close');
						}
					}
				});
			},
			"Cancel": function(){
				$(this).dialog('close');	
			}
		}
	}); 
	
	$("#cancel_account_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		height: 200,
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	}); 
	
	$("#client_edit_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	}); 

	$("#delete_appointment_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		close: function(){
			$('#delete_all_recur').attr('checked',false);
		},
		buttons: {
			"Delete This Appointment": function() {
				var template_vals = 'appt_delete_id=' + $('#delete_appt_id').val();
				
				//are we deleting all upcoming appointments as well?
				if($('#delete_all_recur').is(':checked')){
					template_vals += '&delete_all_recur=yes';	
				}else{
					template_vals += '&delete_all_recur=no';	
				}
				
				$.ajax({
					type: "POST",
					url: BASE_URL + "schedule/delete_appointment/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){//send json
						if(msg.error){//there was a problem
							alert(msg.message);
						}else{
							var template_vals = 'appt_date=' + $('#edit_formated_date').val();						
							$('#edit_ajax_load').show();
							$('#no-appointments').remove();
							$('#appt_list_ul').remove();
							
							//update the table
							update_appt_table(template_vals);
							
							//close the dialog
							$("#delete_appointment_dialog").dialog('close');
						}
					}
				});
				$(this).dialog('close');
			},
			"Cancel": function() {
				$(this).dialog('close');
			}
		}
	}); 
	
	$("#icon_help").dialog({
		autoOpen: false,
		bgiframe: true,
		width:500,
		modal: true,
		buttons: {
			"Close Glossary": function() {
				$(this).dialog('close');
			}
		}
	}); 
	
	$('#new_contractor').dialog({
		autoOpen: false,
		bgiframe: true,
		width:500,
		modal: true,
		buttons: {
			"Add Contractor": function(){
				//save the contractor
				var template_vals = $('#add_contractor_form').serialize();
				$.ajax({
					type: "POST",
					url: BASE_URL + "contractors/add_contractor/",
					data: template_vals,
					dataType: 'json',
					success: function(msg){//send json
						if(msg.error){
							$('#add_contractor_error').html(msg.msg).show("blind",{direction:'vertical'},500);
						}else{
							//$('#location_contractor_dd').append("<option value='" + msg.cid + "'>" + msg.fname + " " + msg.lname + "</option>").val(msg.cid);
							$('#new_contractor').dialog('close');
							location.reload(true);
						}
					}
				});
				$(this).dialog('close');
				return(false);
			},
			"Cancel": function(){
				$(this).dialog('close');	
			}
		}
	});
	
	$("#new_client_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		width:490,
		modal: false,
		close: function(event,ui){
			$('#modal_client_add_error').html('').hide();
		},
		buttons: {
			'Add New Client': function() {
				var template_vals = 'fname=' + $('#fname').val() + '&lname=' + $('#lname').val() + '&email=' + $('#email').val() + '&phone=' + $('#phone').val() + '&address=' + $('#address').val() + '&city=' + $('#city').val() + '&state=' + $('#state').val() + '&zip=' + $('#zip').val() + '&cell_phone=' + $('#cell_phone').val() + '&work_phone=' + $('#work_phone').val();
				$.ajax({
					type: "POST",
					url: BASE_URL + "clients/run_add/",
					data: template_vals,
					dataType: 'json', 
					success: function(msg){
						if(msg.error){
							$('#modal_client_add_error').html(msg.message).addClass('ui-state-error ui-corner-all').show();
						}else{
							var new_client_id = msg.client_id;
							var fname = msg.fname;
							var lname = msg.lname;
							var email = msg.email;
							$('#client_email').append('<option value="' + new_client_id + '" SELECTED>' + fname + ' ' + lname + '</option>');
							/*$('#email_verification').text('@ ' + email);*/
							$("#new_client_dialog").dialog('close');
							
							//reset the form fields for next use
							$('#fname').val('');
							$('#lname').val('');
							$('#email').val('');
							$('#phone').val('');
						}
					}
				});
				return(false);
			},
			'Cancel': function(){
				$(this).dialog('close');	
			}
		}
	}); 
	
	$('#error_dialog').dialog({
		autoOpen: false,
		bgiframe: true,
		height: 200,
		modal: true,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		}
	});
	
	$("#existing_appointment").dialog({
		autoOpen: false,
		bgiframe: true,
		width:300,
		modal: false,
		close: function(){
			update_mini_schedule($('#formated_date').val());
		},
		buttons: {
			"Go Ahead": function(){
				var template_vals;
				if($('#curtab').val() == 'schedule'){
					template_vals = $('#create_new_appointment').serialize();
					$.ajax({
						type: "POST",
						url: BASE_URL + "schedule/create/true",
						data: template_vals,
						dataType: 'json',
						success: function(msg){//send json
							if(msg.error){
								$('#existing_appointment').html('<div id="dialogpad">' + msg.message + '</div>');
								$("#existing_appointment").dialog("open"); 
							}else{
								//close the dialog
								$("#existing_appointment").dialog('close');
								$('#create_new_appointment input[type="text"],#create_new_appointment select').val('');
								$('#email_verification').text('');
							}
							//reset all recurring fields
							$('#recurring_cont').text('');
							$('#recurring_appt').val('n');
							$('#recurring_interval').val('');
							$('#recurring_time_period').val('');
						}
					});
					return(false);
				}else{
					template_vals = 'service_id=' + $('#cal_service_dd').val() + '&client_email=' + $('#cal_clients').val() + '&formated_date=' + $('#cal_current_cell_date').val() + '&hour=' + $('#cal_hour').val() + '&minute=' + $('#cal_minute').val() + '&suffix=' + $('#cal_suffix').val();									 
					$.ajax({
						type: "POST",
						url: BASE_URL + 'schedule/create/true',
						dataType: "json",
						data: template_vals,
						success: function(msg){
							if(msg.error){
								$('#existing_appointment').dialog('close');
								if(msg.dialog == 'dialog'){
									$('#error_dialog').html(msg.message).dialog('open');	
								}else if(msg.dialog == 'existing_appointment'){
									$('#existing_appointment').html(msg.message).dialog('open');
								}
							}else{
								$('#existing_appointment').dialog('close');
								//launch the dialog
								//$('#dialog').html(msg.message).dialog('open');
								
								//update the calendar
								var url = $('div#cal_tools a:contains("Refresh Calendar")').attr('href');
								$.ajax({
									type: "GET",
									url: url,
									dataType: "json",
									success: function(msg){
										$('#tabs-4').html(msg.calendar);
										$('#cal_load').hide();
										prep_next_prev();
									}
								});
					
								return(false);
							}
						}
					});
				}
			},
			"Cancel": function() {
				$(this).dialog('close');
			}
		}
	}); 
	
	$("#edit_appt_dialog").dialog({
		autoOpen: false,
		bgiframe: true,
		width: 600,
		modal: false,
		close: function(event,ui){
			$('#update_appt_datepicker').datepicker('destroy');
			$('#apply_to_all_recur').attr('checked',false);
		},
		buttons: {
			"Save Changes": function() {
				//save the changes
				var template_vals = 'update_service_id=' + $('#edit_service_id').val() + '&update_appt_id=' + $('#update_appt_id').val() + '&update_formated_date=' + $('#update_formated_date').val() + '&update_hour=' + $('#update_hour').val() + '&update_minutes=' + $('#update_minutes').val() + '&update_suffix=' + $('#update_suffix').val() + '&appt_notes=' + escape($('#update_appt_notes').val());
				
				if($('#apply_to_all_recur').is(':checked')){
					template_vals += '&apply_to_all=yes';		
				}else{
					template_vals += '&apply_to_all=no';		
				}
							
				$.ajax({
					type: "POST",
					url: BASE_URL + "schedule/update_appointment/",
					data: template_vals,
					success: function(msg){
						//update the appointment table in view
						var template_vals = 'appt_date=' + $('#edit_formated_date').val();						
						$('#edit_ajax_load').show();
						$('#no-appointments').remove();
						$('#appt_list_ul').remove();
						
						//destroy the date picker and close the dialog
						$('#update_appt_datepicker').datepicker('destroy');
						$('#edit_appt_dialog').dialog('close');
						
						//update the table
						update_appt_table(template_vals);
					}
				});
				
			},
			"Delete Appointment": function(){
				//set the id to delete
				$('#delete_appt_id').val($('#update_appt_id').val());
				
				//close the edit dialog
				$('#update_appt_datepicker').datepicker('destroy');
				$(this).dialog('close');
				
				//open the confirm delete dialog
				//if it's recurring, show the delete all recurring 
				if($('#is_recurring').val() == 'y'){
					$('#delete_all_recurring').show();
				}else{
					$('#delete_all_recurring').hide();
				}
				
				$('#delete_appointment_dialog').dialog('open');
			},
			"No Show" : function(){
				var template_vals = 'update_appt_id=' + $('#update_appt_id').val();
				$.ajax({
					type: "POST",
					url: BASE_URL + "schedule/update_no_show/",
					data: template_vals,
					success: function(msg){
						if(msg.error){
							//there is a problem setting the no show	
						}else{
							//change to no show
							$('#edit_appt_dialog').dialog('close');
							update_appt_table('appt_date=' + $('#edit_formated_date').val());
						}
					}
				});
			},
			"Cancel" : function(){
				$(this).dialog('close');	
			}
		}
	}); 
	
	$('#delete_client_modal_success').dialog({
		autoOpen: false,
		bgiframe: true,
		modal: false,
		buttons: {
			Ok: function() {
				//regen the client drop down
				$.ajax({
					type: "POST",
					url: BASE_URL + "clients/gen_client_dropdown/",
					success: function(msg){
						//update the edit client drop down
						$('#client_edit').html(msg);
						$('#client_edit_cont_hidden').show(); 
						$('#noclients_err').hide();
						
						//reset the form
						$('#edit_client_form input[type="text"]').val('');
					}
				});				
				//close the dialog
				$(this).dialog('close');
			}
		}
	});	
	
	$('#delete_client_modal').dialog({
		autoOpen: false,
		bgiframe: true,
		resizable: false,
		height:140,
		modal: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: {
			'Delete This Client': function() {
				$.getJSON(BASE_URL + "clients/delete_client/" + $('#editclient_id').val(),
				function(data){
					if(data.error){
						alert(data.error);	
						return(false);
					}else{
						//$('#delete_client_modal_success').dialog('open');
						
						//reset the page to default state
						$('#client_name_tbl').html('');
						$('#client_phone_tbl').html('');
						$('#client_email_tbl').html('');
						$('#client_address_tbl').html('');
						$('#client_birthday_tbl').html('');
						$('#appt_history').html('Select a client from your client list at the left to view an appointment history.');
						$('#edit_block').hide();
						
						update_client_list();
						
					}
				});
				$(this).dialog('close');
				return(false);
			},
			Cancel: function() {
				$(this).dialog('close');
				return(false);
			}
		}
	});
	
	//char counter for appt notes field
	var str_len = 0;
	$('#appt_notes').keyup(function(){
		//track the keystrokes
		str_len = $(this).val().length;
		
		//trim the keystrokes if to long
		if(str_len > 250){
			$('#char_tracker').text('0 Characters Left');
			$(this).val($(this).val().substring(0,250));	
		}else{
			//update the onscreen char tracker
			$('#char_tracker').text((250 - str_len) + ' Characters Left');
		}
	});
	
	var update_str_len = 0;
	$('#update_appt_notes').keyup(function(){
		//track the keystrokes
		update_str_len = $(this).val().length;
		
		//trim the keystrokes if to long
		if(update_str_len > 250){
			$('#char_tracker').text('0 Characters Left');
			$(this).val($(this).val().substring(0,250));	
		}else{
			//update the onscreen char tracker
			$('#update_char_tracker').text((250 - update_str_len) + ' Characters Left');
		}
	});
	
	//create the tabs
	var $tabs = $("#tabs").tabs(); 
	
	$('#tabs').bind('tabsselect', function(event, ui) {
		// Objects available in the function context:
		//ui.tab     // anchor element of the selected (clicked) tab
		//ui.panel   // element, that contains the selected/clicked tab contents
		//ui.index   // zero-based index of the selected (clicked) tab
		
		var template_vals;
		
		var url = ui.tab;
		
		var regex = /schedule#tabs-1/;
		if(regex.test(url)){
			$('#curtab').val('schedule');
		}
		
		regex	= /schedule\/#tabs-1/;
		if(regex.test(url)){
			$('#curtab').val('schedule');
		}
		
		regex	= /schedule#tabs-2/;
		var regex2	= /schedule\/#tabs-2/;
		if(regex.test(url) || regex2.test(url)){
			template_vals = 'appt_date=' + $('#edit_formated_date').val();						
			$('#edit_ajax_load').show();
			$('#no-appointments').remove();
			$('#appt_list_ul').remove();
			
			//destroy the date picker and close the dialog
			$('#update_appt_datepicker').datepicker('destroy');
			$('#edit_appt_dialog').dialog('close');
			
			//update the table
			update_appt_table(template_vals);
		}
		
		regex	= /schedule#tabs-3/;
		if(regex.test(url)){
			template_vals = $('#location_formated_date').val();						
			$('#location_ajax_load').show();
			$('#no-appointments').remove();
				
			//update the table
			update_location_calendar(template_vals);
		}
		
		regex	= /schedule\/#tabs-3/;
		if(regex.test(url)){
			template_vals = $('#location_formated_date').val();						
			$('#location_ajax_load').show();
			$('#no-appointments').remove();
				
			//update the table
			update_location_calendar(template_vals);
		}
		
		regex	= /schedule#tabs-1/;
		if(regex.test(url)){
			update_mini_schedule($('#formated_date').val());
		}
		
		regex = /schedule#tabs-4/;
		regex2 = /schedule\/#tabs-4/;
		if(regex.test(url) || regex2.test(url)){
			if(cal_refresh == 'yes'){
				$('#cal_load').show();
				update_montly_calendar();
				
			}
			$('#curtab').val('cal');
		}
		
		regex	= /clients#tabs-2/;
		if(regex.test(url)){
			update_client_list();
		}
		
		regex	= /clients#tabs-3/;
		if(regex.test(url)){
			update_client_dd();
		}
		
		regex = /schedule#tabs-5/;
		if(regex.test(url)){
			//show the loader
			$('#week_load').show();	
			
			//empty out the week cont
			$('#week_cont').html('');			
						
			//load the calendar
			load_week_view();
		}
		
	});
	
	
	
	
	// clients/edit populate the edit form
	$('#client_edit').change(function(){
		$.getJSON(BASE_URL + "clients/get_client_details/" + $(this).val(),
		function(data){
			if(data.error){
					
			}else{
				$('#editclient_id').val(data.client_id);
				$('#editfname').val(data.fname);
				$('#editlname').val(data.lname);
				$('#editemail').val(data.email);
				$('#editphone').val(data.phone);
			}
		});
	});
		
	$('#delete_client').click(function(){
			$('#delete_client_modal').dialog('open');
			return(false);
	});
	
	$('#client_email').change(function(){
		var client_email = $('#client_email').val();
		if(client_email == 'personal'){
			$('#is_personal').val('y');
			$('#personal_time_length').show();
			$('#service_holder').hide();
			$('#personal_label').show();
			$('#create_appt_button').val('Create Personal Time');
			
			//remove recurring options
			$('#recurring_appt').val('n');
			$('#recurring_cont').text('');
			
		}else if(client_email == 'new'){
			
			$('#is_personal').val('n');
			$('#email_verification').text('');
			$("#new_client_dialog").dialog('open');
			$('#personal_time_length').hide();
			$('#personal_label').hide();
			$('#service_holder').show();
			$('#create_appt_button').val('Schedule Appointment');
		}else{
			$('#time_select').show();
			$('#is_personal').val('n');
			$('#personal_time_length').hide();
			$('#personal_label').hide();
			$('#service_holder').show();
			$('#create_appt_button').val('Schedule Appointment');
		}
	});
	
	//show the email type descrition
	$('#mail_type').change(function(){
		if($(this).val() == 'text'){
			$('#type_desc').text('Your logo will not be displayed.');
			$('#type_desc').addClass('ui-state-error ui-corner-all');
			$('#your_logo_here').css({'display':'none'});
		}else{
			$('#your_logo_here').css({'display':'block'});
			$('#type_desc').text('Your logo (or the default logo) will be displayed in all reminders.');
		}
	});
	
	
	$('#weekly_start_date_input').datepicker({
		showButtonPanel: false,
		dateFormat: 'D M d yy',
		changeMonth: true,
		changeYear: true,
		altField: '#hidden_weekly_start', 
		altFormat: 'yy-mm-dd'
	});
	
	$('#editbirthday').datepicker({
		showButtonPanel: false,
		dateFormat: 'mm-dd-yy',
		changeMonth: true,
		changeYear: true,
		altField: '#edit_format_birthday', 
		altFormat: 'yy-mm-dd',
		yearRange: '1910:2010'
	});
	
	$('#birthday').datepicker({
		showButtonPanel: false,
		dateFormat: 'mm-dd-yy',
		changeMonth: true,
		changeYear: true,
		altField: '#format_birthday', 
		altFormat: 'yy-mm-dd',
		yearRange: '1910:2010'
	});
		
	$("#datepicker").datepicker({
		showButtonPanel: false,
		dateFormat: 'MM dd, yy',
		changeMonth: true,
		changeYear: true,
		altField: '#formated_date', 
		altFormat: 'yy-mm-dd',
		onSelect: function(dateText){
			date_picker_select();
			$('#quickview').hide();
			
			
			
			
			/*
			var temp = $('#formated_date').val().split('-');
			var new_date = new Date(temp[0],(temp[1] * 1) - 1,temp[2]);

			
			var mini_cal_header = dateFormat(new_date,"dddd mmmm d, yyyy");
			$('#minisched h3 span#currentscheduledate').text(mini_cal_header);
			
			$('#mini-no-appointments').hide();
			$('#appt_list_ul_mini').hide();
			$('#mini_ajax_load').show();
			//select the upcoming stuff
			update_mini_schedule($('#formated_date').val());
			*/
		}
	});
	
	$("#edit_datepicker").datepicker({
		showButtonPanel: false,
		dateFormat: "DD M dd, yy",
		altField: '#edit_formated_date', 
		changeMonth: true,
		changeYear: true,
		altFormat: 'yy-mm-dd',
		onSelect: function(dateText){
			//populate the date to the header and show loading icon
			$('#appt_list h1').text(dateText).show();
			$('#edit_ajax_load').show();
			$('#no-appointments').remove();
			$('#appt_list_ul').remove();
			
			//load all appt. for that date
			var template_vals = 'appt_date=' + $('#edit_formated_date').val();
			update_appt_table(template_vals);
			return(false);
		}
	});
	
	$("#location_datepicker").datepicker({
		showButtonPanel: false,
		dateFormat: "DD M dd, yy",
		altField: '#location_formated_date', 
		changeMonth: true,
		changeYear: true,
		altFormat: 'yy-mm-dd',
		onSelect: function(dateText){
			//populate the date to the header and show loading icon
			$('#location_appt_list h1').text(dateText).show();
			$('#location_ajax_load').show();
			$('#no-appointments').hide();
			$('#location_appt_list_ul').hide();
			$('#location_appt_header').hide();
			
			//load all appt. for that date
			var template_vals = $('#location_formated_date').val();
			
			update_location_calendar(template_vals);
			return(false);
		}
	});	
	
	$("#datepicker").keyup(function(){
		$("#datepicker").val('');								
	});
	
	/*
	$('#datepicker').change(function(){
		if($('#update_appt_list').val() == 'true'){
			$('#appt_list').html('');
			//show loading icon
			
			//load the json
			$.getJSON(BASE_URL + "dashboard/update_schedule/" + $('#formated_date').val(),
			function(data){
				if(data.error){
					alert('You must select a date first!');
				}else{
					jQuery.each(data, function(i,val){
						$('#app_list').append('<div class="sched_list"><h2 class="sched_list_header">' + val.fname + ' ' + val.lname + '</h2></div><div id="seperator"></div>');						   
					});
				}
			});
		}
	});
	*/
	
	//ajaxify the email template
	$('#email_template_form').submit(function(){
		var template_vals = $(this).serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/update_email_template/",
			data: template_vals,
			success: function(msg){
				$('#dialog').html('<div id="dialogpad">' + msg + '</div>');
				$("#dialog").dialog("open"); 
			}
		});
		return(false);
	});
	
	$('#update_template').live('click',function(){
		var template_vals = $('#email_template_form').serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/update_email_template/",
			dataType: 'json',
			data: template_vals,
			success: function(msg){
				//change the status to complete
				$('#setup_list li:contains("Create your Email Template")').removeClass('incomplete').addClass('complete');
				
				//change the percent complete img
				$('#span_num').text(msg.percent_complete);
				
				//animate the status bar
				$('#percent_progress').animate({width : msg.percent_complete + '%'},500);
				
				//scroll to the top
				$('#right_settings').scrollTo('a[name="settings_top"]',200);
								
				//show the updated message
				$('#setting_updated').show();
				
			}
		});
		return(false);
	});
	
	$('#appointment_length_update').submit(function(){
		var template_vals = $(this).serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/update_default_appointment_time/",
			data: template_vals,
			success: function(msg){
				$('#settings_dialog').html('<div id="dialogpad">' + msg + '</div>');
				$("#settings_dialog").dialog("open"); 
			}
		});
		return(false);
	});
	
	$('#client_appt_list_sel').change(function(){
		var template_vals = 'client_appt_list_sel=' + $(this).val();
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/upcoming_appointments/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					$('#dialog').html('<div id="dialogpad">' + msg.message + '</div>');
					$("#dialog").dialog("open"); 
				}else{
					$('#appt_ul').append(msg.lis);
					$('div#appt_list').show();
				}
			}
		});
		return(false);
	});
	
	$('#login_form').submit(function(){
		var template_vals = $(this).serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "login/run_login/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.login == 'good'){
					window.location = BASE_URL + 'schedule';
				}else{
					if(msg.reason == 'bad_login'){
						$('#badlogin').html(msg.message).show('blind',{direction:'vertical'},300);
					}else if(msg.reason == 'trial_expired'){
						//redirect to page to collect credit card info
						window.location = 'https://app.pencilem.com/sign_up/billing_info/' + msg.left12;
					}
				}
			}
		});
		return(false);
	});
	
	$('#account_settings_forms').submit(function(){
		var template_vals = $(this).serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/update_settings/",
			data: template_vals,
			success: function(msg){
				$('#dialog').html('<div id="dialogpad">' + msg + '</div>');
				$("#dialog").dialog("open"); 
			}
		});
		return(false);
	});
	
	$('#create_new_appointment').submit(function(){
		var template_vals = $(this).serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/create/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){//send json
				if(msg.error){
					if(msg.dialog == 'dialog'){
						$('#error_dialog').html('<div id="dialogpad">' + msg.message + '</div>');
						$("#error_dialog").dialog("open"); 
					}else if(msg.dialog == 'existing_appointment'){
						$('#existing_appointment').html('<div id="dialogpad">' + msg.message + '</div>');
						$("#existing_appointment").dialog("open"); 
					}else if(msg.dialog == 'already_booked'){
						$('#already_booked').html('<div id="dialogpad">' + msg.message + '</div>');
						$("#already_booked").dialog("open"); 
					}
				}else{
					//$('#dialog').html('<div id="dialogpad">' + msg.message + '</div>');
					//$("#dialog").dialog("open"); 
					//$('#email_verification').text('');
					if(msg.future_conflicts){
						//create the list of recur conflicts
						var recur_conflict_list = '';
						$.each(msg.future_conflicts, function(i, val) {//i = the date / time of the appointment , val = service id
							recur_conflict_list += '<li>' + i + '</li>';
						});					
						
						//add them to the list
						$('#recur_conflict_list').html(recur_conflict_list);
						
						//position the notification
						var x = $('#recur_button').offset().left;
						var y = $('#recur_button').offset().top;
						
						var cssObj = {
							'left'	  : x,
							'top'	  : y + 28
						};
						
						$('#recur_conflict').show();
						$('#recur_controls').hide();
						$('#recur_popup').show();
						
					}
					
					//update the mini schedule
					update_mini_schedule($('#formated_date').val());
					$('#create_new_appointment input[type="text"],#create_new_appointment select,#create_new_appointment textarea').val('');
					
					//reset all recurring fields
					$('#recurring_cont').text('');
					$('#recurring_appt').val('n');
					$('#recurring_interval').val('');
					$('#recurring_time_period').val('');
					
					$('#time_select').show();
				}
			}
		});
		return(false);
	});
	
	$('#add_new_client_form').submit(function(){
		var template_vals = $(this).serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/run_add/",
			data: template_vals,
			dataType: 'json', 
			success: function(msg){
				if(msg.error){
					$('#client_add_error').html(msg.message).show(200);	
				}else{
					$('#client_add_error').html('').hide();	
					//let them know it worked
					//$('#dialog').html('<div id="dialogpad">' + msg.message + '</div>');
					//$("#dialog").dialog("open");
					$('#add_new_client_form input[type="text"]').val('');
					
					//show inline message of new client saved
					$('#client_added').text('Client Details Saved!').show('blind',200);
					setTimeout("$('#client_added').text('').hide(200)",5000);
				}
			}
		});
		return(false);
	});
	
	$('#edit_client_form').submit(function(){
		var template_vals = $(this).serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/run_edit/",
			data: template_vals,
			success: function(msg){
				$('#dialog').html('<div id="dialogpad">' + msg + '</div>');
				$("#dialog").dialog("open");
				$('#edit_client_form input[type="text"]').val('');
				$('#client_edit').val('');
			}
		});
		return(false);
	});
	
	$('#send_email_form').submit(function(){
		var template_vals = $(this).serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/send_email/",
			data: template_vals,
			success: function(msg){
				$('#dialog').html('<div id="dialogpad">' + msg + '</div>');
				$("#dialog").dialog("open");
			}
		});
		return(false);
	});
	
	$('#upload_restrictions').click(function(){
		if($('#upload_restrictions_cont').css('display') == 'none'){
			$(this).text('- Upload Restrictions');
		}else{
			$(this).text('+ Upload Restrictions');
		}
		$('#upload_restrictions_cont').toggle("blind");
	});	
	
	$('#delappt').click(function(){
		alert('hey ladie ladie low');
		return(false);
	});
	
	change_contractor_dd();
	
	$('#launch_icon_help').click(function(){
		$('#icon_help').dialog('open');
	});
	
	$('#launch_icon_help').hover(
		function(){
			$(this).addClass('ui-corner-all icon_glossary_hover');	
		},
		function(){
			$(this).removeClass('ui-corner-all icon_glossary_hover');	
		}
	);
	
	$('#location_contractor_edit').change(function(){
		if($(this).val() != ''){						   
			var template_vals = 'contractor_id=' + $(this).val();
			$.ajax({
				type: "POST",
				url: BASE_URL + "contractors/get_contractor_details/",
				data: template_vals,
				dataType: 'json',
				success: function(msg){
					if(msg.error){
						alert(msg.message);
					}else{
						$('#editfname').val(msg.fname);
						$('#editlname').val(msg.lname);
						$('#editphone').val(msg.phone);
						$('#editemail').val(msg.email);
						$('#editisactive').val(msg.is_active);
						
					}
				}
			});
		}
	});
	
	$('#editcontractor').submit(function(){
		var template_vals = $(this).serialize() + '&contractor_id=' + $('#location_contractor_edit').val();
		$.ajax({
			type: "POST",
			url: BASE_URL + "contractors/edit/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					$('#client_edit_dialog').html(msg.message).dialog('open');
				}else{
					$('#location_contractor_edit').find('option[value="' + $('#location_contractor_edit').val() + '"]').text($('#editfname').val() + ' ' + $('#editlname').val());
					$('#client_edit_dialog').html($('#editfname').val() + "'s Profile has been updated.").dialog('open');
				}
			}
		});
		return(false);
	});
	
	$('#cancel_accont_button').click(function(){
		$(this).hide();
		$('#cancel-account-loading').show();
		$.ajax({
			type: "POST",
			url: BASE_URL + "account/cancel/",
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					$('#cancel_accont_button').show();
					$('#cancel_account_dialog').html(msg.msg).dialog('open');
					$('#cancel-account-loading').hide();
				}else{
					$('#cancel_account_dialog').html(msg.msg).dialog('open');
					$('#cancel-account-loading').hide();
					$('#your_account_is_cancelled').show();
				}
			}
		});
	});
	
	//show the remember me disclaimer on the login page
	$('#rememberme').click(function(){
		if($(this).attr('checked')){
			$('#cookie_info').show('blind',{direction:'vertical'},300);
		}else{
			$('#cookie_info').hide('blind',{direction:'vertical'},300);
		}
	});
	
	//print button on the edit schedule page
	$('.print_schedule').click(function(){
		var date = $('#edit_formated_date').val();
		open_print_window(date);
	});
	
	//print button on main schedule page
	$('.print_schedule_mini').click(function(){
		var date = $('#formated_date').val();
		open_print_window(date);
	});
	
	//search button functionality
	$('#search').click(function(){
		alert('Search function coming soon');							
	});
	
	//launch the update payment info page
	$('#update_payment_method').click(function(){
		var win_name = 'update_payment_info';
		window.open(
					'https://app.pencilem.com/account/update/', 
					win_name, 
					'height=450,width=500, toolbar=no, menubar=yes, scrollbars=yes, resizable=yes,location=no, directories=yes, status=no');								
	});
	
	//launch the client details trigger
	$('.client_detail_trigger').click(function(){
		set_up_client_trigger($(this));										   
	});
	
	$('.edit_client_link').click(function(){
		$("#edit_client_dialog").dialog('open');
		return(false);
	});
	
	$('#edit_client_form_new').submit(function(){
		return(false);										   
	});	
	
	$('#time_unit').change(function(){
		if($(this).val() != 1){
			$('.time_interval_opt').each(function(){
				var text_len = $(this).text().length;
				var text = $(this).text();
				if(text.substring(text_len - 1) != 's'){
					$(this).text($(this).text() + 's');
				}
			});	
		}else{
			$('.time_interval_opt').each(function(){
				var text_len = $(this).text().length;
				var text = $(this).text();
				$(this).text(text.substring(0,text_len - 1));
			});	
		}
	});
	
	$('#run_client_report').click(function(){
		var template_vals = 'time_unit=' + $('#time_unit').val() + '&time_interval=' + $('#time_interval').val();
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/client_report/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				$('#report_holder').html(msg.msg);
			}
		});
	});
	
	$('#right_col_report ul li').hover(
		function(){
			$(this).addClass('report_hover');
		},
		function(){
			$(this).removeClass('report_hover');
		}
	);
	
	$('#right_col_report ul li').click(function(){
		//the li holds a rel attribute of the report to run, build the url using it
		var url = BASE_URL + 'reports/' + $(this).attr('rel');	
		var win_name = $(this).attr('rel') + '_report';
		
		//open a popup window containing the report
		window.open(
					url, 
					win_name
					);								
		
	});
	
	$('#edit_this_client').click(function(){
		$('#edit_client_dialog').dialog('open');
		return(false);
	});
	
	$('#delete_this_client').click(function(){
		$('#delete_client_modal').dialog('open');
		return(false);
	});
	
	client_list_prep();		
	
	$('#add_new').click(function(){
		$tabs.tabs('select',1);					 
	});
	
	$('#service_price_field, #add_service_price').keyup(function(){
		var string = $(this).val();
		var formatted_string = string.replace('$','');
		$(this).val(formatted_string);
	});
	
	service_trigger();
		
	$('#service_edit_block a#edit_this_service').click(function(){
		$('#edit_service_dialog').dialog('open');
		return(false);
	});
	
	$('#delete_this_service').click(function(){
		$('#confirm_service_delete').dialog('open');
		return(false);
	});
	
	prep_group_list();
	
	$('#edit_this_group').click(function(){
		$("#edit_group_dialog").dialog('open');
		return(false);
	});
	
	$("#delete_this_group").click(function(){
		$("#delete_this_group_dialog").dialog('open');
		return(false);
	});
	
	prep_manage_groups();
	
	prep_contractor_list();	
	
	$('#contractor_h1 span').click(function(){
		$("#add_contractor_dialog").dialog('open');									 
	});
	
	$('#edit_this_contractor').click(function(){
		$('#edit_contractor_dialog').dialog('open');										  
	});
	
	$('#friend_emails').keydown(function(){
									   
	});

	$('#friend_emails').bind('keyup', function(e) {
		if(e.keyCode==13){
			var list = $(this).val();
			var eList = list.replace(/\n/g,'<br />');
			$('#all_friend_email').html(eList).css('padding-bottom','5px');
		}
	});
	$('#refer_message').focus(function(){
		$(this).val('');								   
	});
	
	$('#refer_message').bind('keyup', function(e) {
		var msg = $(this).val();
		var eMsg = msg.replace(/\n/g,'<br />');
		$('#preview_message').html(eMsg);
	});
	
	$('#friend_emails').blur(function(){
		var msg = $(this).val();
		var eMsg = msg.replace(/\n/g,'<br />');
		$('#all_friend_email').html(eMsg).css('padding-bottom','10px');
	});
	
	$('#send_referral_email_button').click(function(){
		var template_vals = 'email_list=' + $('#friend_emails').val() + '&msg=' + $('#refer_message').val();
		$.ajax({
			type: "POST",
			url: BASE_URL + "refer/send/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
				}else{
					alert(msg.msg);
				}
			}
		});
	});
	
	prep_quickview();
	
	$('#client_notes').click(function(){
		$('#client_notes_dialog').dialog('open');								  
	});
	
	$('#client_filter').focus(function(){
		if($(this).val() == 'Client Filter'){
			$(this).val('');	
		}
	});
	
	$('#client_filter').blur(function(){
		if($(this).val() == ''){
			$(this).val('Client Filter');	
		}
	});
	
	$('#client_filter').keyup(function(){
		$('#client_query_loader').show();
		var query = $('#client_filter').val();
		query = query.replace(/ /g,'');
		
		var filter = $('#filter_on').val();
		if($('#client_filter').val() == ''){
			$.ajax({
				type: "POST",
				url: BASE_URL + "clients/client_list/",
				data: 'ispost=yes',
				dataType: 'json',
				success: function(msg){
					$('#client_query_loader').hide();
					$('#client_rows').html(msg.client_list);
					client_list_prep();
				}
			});
		}else{
			$.ajax({
				type: "POST",
				url: BASE_URL + "clients/client_list/" + query + '/' + filter,
				data: 'ispost=yes',
				dataType: 'json',
				success: function(msg){
					$('#client_query_loader').hide();
					$('#client_rows').html(msg.client_list);
					client_list_prep();
				}
			});
		}
	});
	
	$('#reset_client_filter').click(function(){
		$('#client_query_loader').show();							 
		$('#client_filter').val('');
		$.ajax({
			type: "POST",
			url: BASE_URL + "clients/client_list/",
			data: 'ispost=yes',
			dataType: 'json',
			success: function(msg){
				$('#client_query_loader').hide();
				$('#client_rows').html(msg.client_list);
				client_list_prep();
			}
		});
	});
	
	$('#filter_on').change(function(){
		$('#client_query_loader').show();
		var filter = $(this).val();
		var query = $('#client_filter').val();
		if(	query != '' && query != 'Client Filter'){
			$.ajax({
				type: "POST",
				url: BASE_URL + "clients/client_list/" + $('#client_filter').val() + '/' + filter,
				data: 'ispost=yes',
				dataType: 'json',
				success: function(msg){
					$('#client_query_loader').hide();
					$('#client_rows').html(msg.client_list);
					client_list_prep();
				}
			});
		}
	});
	
	$('#recur_button').click(function(){
		//$('#recurring_dialog').dialog('open');	
		var x = $(this).offset().left;
		var y = $(this).offset().top;
		
		var cssObj = {
			'left'	  : x,
			'top'	  : y + 28
		};

		if($('#recur_popup').css('display') == 'block'){
			$('#recur_popup').hide();
		}else{
			$('#recur_conflict').hide();
			$('#recur_controls').show();
			$('#recur_popup').css(cssObj).show();
		}
		
		return(false);
	});
	
	$('#get_weekly_cal').hide();
	
	$('#weekly_start_date_input').change(function(){
		//$('#week_cont').html('');
		$('#week_load_small').show();
		
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/week_view/" + $('#hidden_weekly_start').val(),
			dataType: 'json',
			success: function(msg){
				$('#week_load_small').hide();
				$('#week_cont').html(msg.week);
				$('#previous_week').attr('rel',msg.previous_week);
				$('#next_week').attr('rel',msg.next_week);
				
				prep_weekly_rows();
			}
		});
		return(false);
	});
	
	$('#get_future_appt').click(function(){
		var client_id = $(this).attr('rel');
		$('#client_history_loader').show();
		
		if($(this).hasClass('has_future')){ //it's showing future, switch to history
			//change the header
			$('#list_type_cont').text('Appointment History');
			$(this).removeClass('has_future');
			$(this).text('View Future');
			
			//ajax the future appointments
			$.ajax({
				type: "POST",
				url: BASE_URL + "clients/get_appointment_history/" + client_id,
				dataType: 'json',
				success: function(msg){
					if(msg.error){
						$('#appt_history').html(msg.error);
					}else{
						$('#appt_history').html(msg.appt_table);
					}
					$('#client_history_loader').hide();
				}
			});
		}else{ //it's show hisotry, switch to future
			//change the header
			$('#list_type_cont').text('Future Appointments');
			$(this).addClass('has_future');
			$(this).text('View History');
			
			//ajax the future appointments
			$.ajax({
				type: "POST",
				url: BASE_URL + "clients/get_appointment_history/" + client_id + '/future',
				dataType: 'json',
				success: function(msg){
					if(msg.error){
						$('#appt_history').html(msg.error);
					}else{
						$('#appt_history').html(msg.appt_table);
					}
					$('#client_history_loader').hide();
				}
			});
		}
		return(false);
		
	});
	
	$('#next_week, #previous_week').click(function(){
		$('#week_load_small').show();
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/week_view/" + $(this).attr('rel'),
			dataType: 'json',
			success: function(msg){
				$('#weekly_start_date_input').val(msg.display_date);
				$('#week_cont').html(msg.week);
				$('#week_load').hide();
				$('#week_load_small').hide();
				
				//update next and previous
				$('#next_week').attr('rel',msg.next_week);
				$('#previous_week').attr('rel',msg.previous_week);
				prep_weekly_rows();
			}
		});
					   
	});
	
	$('#date_options').click(function(){
		var date_options = $('#quick_date_links');
		
		var x = $(this).offset().left;
		var y = $(this).offset().top;
		
		//set some css for the date options
		if(date_options.css('display') == 'block'){
			//hide it
			date_options.hide();
			
			//change the + to a -
			$(this).find('span').text('+');
		}else{
			var cssObj = {
				'postion' : 'absolute',
				'left'	  : x - 20,
				'top'	  : y + 28,
				'z-index' : 4000
			};
			
			//apply the css
			date_options.css(cssObj);
			
			date_options.show();
			
			//change the + to a -
			$(this).find('span').text('-');

		}
		
		
		
	});
	
	$('#quick_date_links ul li.qjump').click(function(){
		//since we are switching days, hide the quickview
		$('#quickview').hide();
		
		//get the date to jump to
		var jump = $(this).attr('rel');
		
		//split it up into pieces we can create a date with
		var temp = jump.split('-');
		
		//create the jump to date
		var jumpTo = new Date(temp[0],temp[1] - 1,temp[2]);
		
		//set the date
		$("#datepicker").datepicker('setDate',jumpTo);
		
		//since the setDate does not trigger "select" run date_picker_select to update the daily schedule
		date_picker_select();

	});
	
	$('#quick_date_links ul li.qjump').hover(
		function(){
			var cssObj = {
				'text-decoration' : 'underline'
			};
			$(this).css(cssObj);

		},
		function(){
			var cssObj = {
				'text-decoration' : 'none'
			};
			$(this).css(cssObj);
		}
	);
	
	$('#quick_date_links').hover(
		function(){
			
		},
		function(){
			//$(this).hide();	
			//$('#quickjump_toggle').text('+');
		}
	);
	
	$('#jump_vals').change(function(){
		if($(this).val() == '+7'){
			$('#jump_week_label').text('Week');	
		}else{
			$('#jump_week_label').text('Weeks');	
		}
	});
	
	$('#close_quickjump').click(function(){
		$('#quick_date_links').hide();
		$('#quickjump_toggle').text('+');
	});
	
	$('#jump_button').click(function(){
		//date to jump from
		var new_time = new Date($('#formated_date').val());
		
		//how far we are jumping
		var jumpval = $('#jump_vals').val();
		
		//set the new time
		new_time.setDate(new_time.getDate()+ (jumpval * 1));
		
		//set the date
		$("#datepicker").datepicker('setDate',new_time);
		
		//update the minical
		date_picker_select();
		
		return(false);
	});
	
	$('#recur_vals').change(function(){
		var val = $(this).val();
		if(val == 1){
			$('#recur_weeks').text('Week');
		}else{
			$('#recur_weeks').text('Weeks');
		}
	});
	
	$('#close_recur').click(function(){
		$(this).parent().hide();								 
	});
	
	$('#recur_duration').change(function(){
		if($(this).val() == 1){
			$('#recur_months').text('Month');	
		}else{
			$('#recur_months').text('Months');
		}
	});
	
	$('#create_recur').click(function(){
		var int = $('#recur_vals').val();
		var tp	= $('#recur_time_period').val();
		var rd	= $('#recur_duration').val();
		
		$('#recurring_appt').val('y');
		$('#recurring_duration').val(rd);
		$('#recurring_interval').val(int);
		$('#recurring_time_period').val(tp);
		
		//update the page to reflect the change
		var plurality = (int > 1) ? 's' : '';
		var month_plurality = (rd > 1) ? 's' : '';
		$('#recurring_cont').text('This appt. will recur every ' + int + ' ' + tp.toLowerCase() + plurality + ' for ' + rd + ' month' + month_plurality + '.');
		
		//close the popup
		$('#recur_popup').hide();
		
		return(false);								  
	});
	
	$('#close_recur_conflict').click(function(){
		$('#recur_conflict').hide();										  
	});
	
	$('#close_qv').click(function(){
		$(this).parent().hide();							  
	});
	
	$('#client_qv_tab').click(function(){
		$('#quickview').hide();								   
	});
	
	$('#personal_time_length_select').change(function(){
		if($(this).val() == '1440'){
			$('#hour').val('12');
			$('#minute').val('00');
			$('#suffix').val('am');
			$('#time_select').hide();
		}else{
			$('#time_select').show();	
		}
	});
	
	$('#hide_bar').click(function(){
		$('#pencilem_bar').hide();
		$('#unconfirmed_appt_list').hide();
		$('#show_bar').show();
		$('#pencilem_bar_info').hide();
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/manage_bar/hide",
			dataType: 'json',
			success: function(msg){
				return(true);
			}
		});
	});
	
	$('#show_bar').click(function(){
		$('#pencilem_bar').show();
		$('#show_bar').hide();
		$.ajax({
			type: "POST",
			url: BASE_URL + "schedule/manage_bar/show",
			dataType: 'json',
			success: function(msg){
				return(true);
			}
		});
	});
	
	$('#bar_help').click(function(){
		var x = $(this).offset().left;
		var y = $(this).offset().top;
		var box_height = $('#pencilem_bar_info').height();

		if($('#pencilem_bar_info').css('display') == 'block'){
			$('#pencilem_bar_info').hide();
		}else{
			$('#pencilem_bar_info').show();
		}
		
	});
	
	$('#help_tab').click(function(){
		$('#pencilem_bar_info').hide();							  
	});
	
	get_the_blog();
	
	$('#settings_button').click(function(){
		toggle_settings_rolldown();
		return(false);									 
	});
	
	$('#close_settings').click(function(){
		toggle_settings_rolldown();
		return(false);
	});
	
	$('#left_settings ul#launch_module li a').click(function(){
		$(this).parent().next().addClass('ui-corner-tr');
		//show the loading module
		$('#settings-loader').show();
		$('#settings_module').html('');
		
		//which mod are we loading?
		var setting_mod = $(this).attr('id');
		
		//remove the selected setting
		$('.selected_setting').removeClass('selected_setting');
		
		//add the selected setting to the newly clicked link
		$(this).addClass('selected_setting');
		
		var is_default = false;
		var page;
		switch(setting_mod){
			case "email_template":
				page = BASE_URL + 'settings/email_template/';
				break;
			case "change_username":
				page = BASE_URL + 'settings/change_username/';
				break;
			case "change_password":
				page = BASE_URL + 'settings/change_password/';
				break;
			case "update_phone":
				page = BASE_URL + 'settings/change_phone/';
				break;
			case "appt_settings":
				page = BASE_URL + 'settings/appt_settings/';
				break;
			case "logo_upload_setting":
				page = BASE_URL + 'settings/logo_form/';
				break;
			case "manage_contractors":
				page = BASE_URL + 'settings/manage_contractors/';
				break;
			default:
				page = BASE_URL + 'settings/account_overview';
				is_default = true;
				break;
		}
		
		//load the page into the settings module
		$('#settings_module').load(page,function(){
			//hide the loading module
			$('#settings-loader').hide();
			
			if(is_default){
				enable_change_graph();
				$.ajax({
					type: "POST",
					url: BASE_URL + "settings/appt_xml/",
					success: function(msg){
						$('#graph_loader').show();
						//update the flash graph
						var chart2 = new FusionCharts("charts/FCF_MSLine.swf", "ChId1", "525", "250");
						chart2.setDataXML(msg);
						chart2.render("graph");
						$('#graph_loader').hide();
					}
				});
			}
			
		});
		
		return(false);										   
	});
	
	$('#update_username').live("click",function(){
		var new_username = 'new_username=' + $('#settings_username').val();
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/update_username/",
			data: new_username,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
				}else{
					$('#setting_updated').show();
				}
			}
		});
		return(false);										
	});
	
	$('#update_password').live("click",function(){
		var new_password = 'new_password=' + $('#settings_password').val() + '&retype_password=' + $('#settings_retype_password').val();
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/update_password/",
			data: new_password,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
				}else{
					$('#setting_updated').show();
				}
			}
		});
		return(false);										
	});
	
	$('#update_phone').live("click",function(){
		var new_phone = 'new_phone=' + $('#settings_phone').val();
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/update_phone/",
			data: new_phone,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
				}else{
					$('#setting_updated').show();
				}
			}
		});
		return(false);										
	});
	
	$('#update_settings').live("click",function(){
		var update_settings = $('#update_settings_form').serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/update_default_appointment_time/json",
			data: update_settings,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
				}else{
					$('#setting_updated').show();
				}
			}
		});
		return(false);										
	});
	
	$('#no_startup').click(function(){
		if($(this).attr('checked') === true){
			$.ajax({
				type: "POST",
				url: BASE_URL + "settings/no_welcome_display/",
				dataType: 'json'
			});
		}else{
			$.ajax({
				type: "POST",
				url: BASE_URL + "settings/welcome_display/",
				dataType: 'json'
			});
		}
	});
	
	$('.account_overview').live("click",function(){
		$('.selected_setting').removeClass('selected_setting');
		$('#settings-loader').show();
		$('#settings_module').load(BASE_URL + 'settings/account_overview',function(){
			$('#settings-loader').hide();
			enable_change_graph();
			$.ajax({
				type: "POST",
				url: BASE_URL + "settings/appt_xml/",
				success: function(msg){
					$('#graph_loader').show();
					//update the flash graph
					var chart2 = new FusionCharts("charts/FCF_MSLine.swf", "ChId1", "525", "250");
					chart2.setDataXML(msg);
					chart2.render("graph");
					$('#graph_loader').hide();
				}
			});
		});
		return(false);
	});
	
	$('#settings_username,#edit_contractor_email,#add_contractor_email').live('keyup',function(){
		var template_vals = 'desired_username=' + $(this).val();
		$.ajax({
			type: "POST",
			data: template_vals,
			dataType: 'json',
			url: BASE_URL + "settings/username_available/",
			success: function(msg){
				if(msg.error){
					$('#username_availablity').html("<span style='color:red;'>" + msg.msg + "</span>");	
					$('.username_button').attr('disabled', 'disabled');
				}else{
					$('#username_availablity').html("<span style='color:green;'>" + msg.msg + "</span>");
					$('.username_button').attr('disabled', false);
				}
			}
		});
	});
	
	$('#run_pie').live('click',function(){
		$.ajax({
			type: "POST",
			dataType: 'json',
			url: BASE_URL + "settings/month_xml/" + $('#graph_month').val(),
			success: function(msg){
				var chart1 = new FusionCharts("charts/FCF_Pie2D.swf", "ChId1", "525", "250");
				chart1.setDataXML(msg.xml);
				chart1.render("graph");
			}
		});
	});
				
	$('#launch_email_template').live('click',function(){
		$('#email_template').click();
		return(false);
	});
	
	$('#launch_logo_upload').live('click',function(){
		$('#logo_upload_setting').click();
		return(false);
	});
	
	$('.edit_contractor_button').live("click",function(){
		var contractor_id = $(this).attr('rel');
		$('#settings_module').load(BASE_URL + 'settings/edit_contractor/' + contractor_id,function(){
			$('#edit_contractor').submit(function(){
				return(false);									  
			});
		});
		
		
	});
	
	$('#back_to_manage_contractor').live('click',function(){
		$('#manage_contractors').click();
		return(false);
	});
	
	$('#update_contractor').live('click',function(){
		var template_vals = $('#edit_contractor').serialize();
		$.ajax({
			type: "POST",
			url: BASE_URL + "contractors/edit/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
				}else{
					$('#setting_updated').show();
					
					//if the user is not active, remove from drop down
					if($('#editisactive').val() == 'n'){
						$('#location_contractor_dd option:contains("' + $('#edit_contractor_fname').val() + ' ' + $('#edit_contractor_lname').val() + '")').remove();
					}else{
						$('#contractor_drop_down').load(BASE_URL + "settings/refresh_contractor_list/",function(){
							change_contractor_dd($('#location_contractor_dd'));																						
						});
					}
				}
				return(false);
			}
		});	
		return(false);
	});
	
	$('#add_new_contractor').live('click',function(){
		//show the add new form
		$('#add_new_contractor_form').show('blind',200);
		
		//apply the submit catch
		$('#add_contractor').submit(function(){
			$('#addcbutton').attr('disabled','disabled');
			var template_vals = $('#add_contractor').serialize();
			$.ajax({
				type: "POST",
				url: BASE_URL + "contractors/add_contractor/",
				data: template_vals,
				dataType: 'json',
				success: function(msg){
					if(msg.error){
						$('#addcbutton').attr('disabled',false);
						alert(msg.message);
					}else{
						//clear the form
						$('#add_contractor table tr td input[type!="submit"]').val('');
						$('#username_availablity').html('');
						$('#addcbutton').attr('disabled',false);
						
						//update the page 
						$.ajax({
							type: "POST",
							url: BASE_URL + "settings/get_contractor_tbl/",
							dataType: 'json',
							success: function(msg){
								$('#ctable').html(msg.contractor_tbl);
							}
						});
						
						//update the contractor drop down
						$('#location_contractor_dd').append('<option value="' + msg.contractor_id + '">' + msg.fname + ' ' + msg.lname + '</option>');
					}
				}
			});
			return(false);
		});
		return(false);
	});
	
	$('#cancel_contractor_add').live('click',function(){
		$('#add_new_contractor_form').hide('blind',200);
		$('#add_contractor table tr td input[type!="submit"]').val('');
		$('#username_availablity').html('');
		$('#addcbutton').attr('disabled',false);
	});
	
	
	$('.mini_cal_jump').live('click',function(){
		//set the date of the datepicker
		var date = $(this).attr('rel');
		var date_temp = date.split('-');
		var year = date_temp[0];
		var month = (date_temp[1] * 1) - 1;
		var day = date_temp[2];
		var new_date = new Date(year,month,day);
		$("#datepicker").datepicker('setDate',new_date);	
		
		//select the tab
		//set the minischedule date
		var mini_format_date = dateFormat(new_date,"dddd mmmm d, yyyy");
		$('#minisched h3 span#currentscheduledate').text(mini_format_date);
		
		//select the first tab
		$("#tabs").tabs('select',0);
	});
	
	$('.client_self_booking').live('click',function(){
		$('#settings_module').load(BASE_URL + 'settings/client_self_booking/',function(){
			//set up some classes and make the form not submittable
			$('.selected_setting').removeClass('selected_setting');
			$('#setting_booker_form').submit(function(){return false;});
			
			//add the change event to the enabled drop down
			$('#csb_enabled').change(function(){
				if($(this).val() == 'y'){
					$('#booker_settings_cont').show();
					$('#enable_settings').hide();
				}else{
					$('#booker_settings_cont').hide();
					$('#enable_settings').show();
				}
			});
			
		});
	});
	
	$('#csb_username').live('keyup',function(){
		var template_vals = 'desired_username=' + $(this).val();
		$.ajax({
			type: "POST",
			url: BASE_URL + "settings/valid_username/",
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					$('#booker_username_availability').html("<div style='color:red;font-size:.6em;margin-top:5px;font-weight:bold;'>" + msg.msg + "</div>");
				}else{
					$('#booker_username_availability').html("<div style='color:green;font-size:.6em;margin-top:5px;font-weight:bold;'>" + msg.msg + "</div>");
				}
			}
		});
		return(false);
	});
	
	$('#update_booker_settings').live('click',function(){
		//hide the button and show the loader 
		$(this).hide();
		$('#booker_loading').show();
		
		var template_vals = $('#setting_booker_form').serialize();
		var url = $('#setting_booker_form').attr('action');
		
		$.ajax({
			type: "POST",
			url: url,
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.msg);
					$('#update_booker_settings').show();
					$('#booker_loading').hide();
				}else{
					if(msg.is_disabled){
						$('#right_settings').scrollTo('a[name="settings_top"]',200);
						$('#setting_updated').text("The client self booking feature has been disabled, and you will no longer be able to accept online bookings.").show();
						$('#update_booker_settings').show();
						$('#your_url').hide();
						$('#booker_loading').hide();
					}else{
						$('#username_holder').text($('#csb_username').val());
						$('#your_url').show();
						$('#right_settings').scrollTo('a[name="settings_top"]',200);
						$('#setting_updated').show();
						$('#update_booker_settings').show();
						$('#booker_loading').hide();
					}
				}
			}
		});
		
		return(false);
	});
	
	$('#unconfirmed_appts').click(function(){
		//show the list
		if($('#unconfirmed_appt_list').css('display') == 'block'){
			//hide the box
			$('#unconfirmed_appt_list').hide();
			$(this).removeClass('close_unconfirmed').addClass('open_unconfirmed');
			
			//remove the taggedforremoval
			$('.taggedforremoval').remove();
			if($('#ucappt_list tr').length == 0 && $('#default_nouc').length < 1){
				$('#ucappt_list').append("<tr class='nounconfirmed_msg'><td><div style='padding:3px;font-size:1.1em;color:#4F8C93;font-weight:bold;'>You have no unconfirmed appointments.</div></td></tr>");	
			}
		}else{
			//set up the height of the box if it's to tall
			var max_height_overflow = 400;
			var overflow_width = 225;
			
			//show the box
			$('#unconfirmed_appt_list').show();
			
			if($('#ucappt_list').height() > max_height_overflow){
				$('#ucappt_overflow').css({height:max_height_overflow,overflow:'auto',width:overflow_width});
			}
			
			$(this).removeClass('open_unconfirmed').addClass('close_unconfirmed');
		}
	});
	
	$('.ucappt_action').click(function(){
		//show the working icon
		$('#ucappts_working').css('visibility','visible');
		
		//grab the action type
		var action = $(this).attr('rel');
		
		//grab all checked boxes
		var checked_boxes = $('.approve_checkbox:checked');
		
		if(checked_boxes.length > 0){		
			//create the template vals
			var template_vals = 'action=' + action + '&' + checked_boxes.serialize();
			
			//set up the URL
			var url = BASE_URL + "schedule/confirm_decline_post";
			
			//ajax to the server and confirm or decline the appts
			$.ajax({
				type: "POST",
				url: url,
				data: template_vals,
				dataType: 'json',
				success: function(msg){
					if(msg.error){
						
					}else{
						$.each(checked_boxes,function(i,el){
							//$('#unconfirmed' + $(el).val()).remove();						  
							//change the check box to a check mark
							var checkbox = $('.approve_checkbox[value="' + $(el).val() + '"]');
							
							var img = (action == 'confirm') ? 'confirm_mark.gif' : 'decline_mark.gif';
							
							//append the checkbox
							checkbox.parent().append("<img src='" + BASE_URL + "images/" + img + "' />").parent().addClass('taggedforremoval');
							
							//remove the checkbox
							checkbox.remove();
							
						});
						$('#ucappts_working').css('visibility','hidden');
						
						//change the number of unconfirmed appts
						var numunconfirmed = ($('#num_unconfirmed').text() * 1) - checked_boxes.length;
						$('#num_unconfirmed').text(numunconfirmed);
						$('#unconfirmed_badge,#large_unconfirmed').text(numunconfirmed);
												
						//update the mini cal
						update_mini_schedule($('#formated_date').val());
						
						
					}
				}
			});
		}else{
			alert("You must select at least one appointment to " + action + ".");
			$('#ucappts_working').css('visibility','hidden');
		}
		
		//return false
		return(false);
	});
	
	$('#close_unconfirmed').click(function(){
		$('#unconfirmed_appt_list').hide();
		$('#unconfirmed_appts').removeClass('close_unconfirmed').addClass('open_unconfirmed');
		
		//remove the taggedforremoval
		$('.taggedforremoval').remove();
				
		if($('#ucappt_list tr').length == 0 && $('#default_nouc').length < 1){
			$('#ucappt_list').append("<tr class='nounconfirmed_msg'><td><div style='padding:10px;font-size:1.1em;color:#4F8C93;font-weight:bold;'>You have no unconfirmed appointments.</div></td></tr>");	
		}
		
	});
	
	$('.block').live('click',function(){
		$(this).attr('src',BASE_URL + 'images/dateisblocked.gif');
		$(this).attr('title','Unblock this day from public booking.');
		$(this).removeClass('block').addClass('unblock');
		var template_vals = 'action=block&date_blocked=' + $(this).attr('rel');
		var url = BASE_URL + 'schedule/block_public_booking/';
		$.ajax({
			type: "POST",
			url: url,
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
					alert(msg.message);
				}
			}
		});
		return(false);
	});
	
	$('.unblock').live('click',function(){
		$(this).attr('src',BASE_URL + 'images/blockpublic.gif');
		$(this).attr('title','Block this day from public booking.');
		$(this).removeClass('unblock').addClass('block');
		var template_vals = 'action=unblock&date_blocked=' + $(this).attr('rel');
		var url = BASE_URL + 'schedule/block_public_booking/';
		$.ajax({
			type: "POST",
			url: url,
			data: template_vals,
			dataType: 'json',
			success: function(msg){
				if(msg.error){
				
				}
			}
		});
		return(false);
	});
});





function client_details_loader(msg){
	//hide the cell and work phone
	$('.display_toggle').hide();
	$('#client_cell_tbl').text('');
	$('#client_work_tbl').text('');
	
	//set the client address
	var client_address_html = '<address>';
	client_address_html += (msg.address != '') ? msg.address + '<br />' : '';
	client_address_html += (msg.city != '')? msg.city + ', ' : '';
	client_address_html += (msg.state != '') ? msg.state : '';
	client_address_html += (msg.zip != '') ? ' ' + msg.zip : '';
	client_address_html += '</address>';
	
	//set up the phones
	var phones;
	if(msg.phone == ''){
		phones = "";
	}else{
		phones = msg.phone + "<a href='#' id='show_all_phone' class='ui-corner-all'>show all</a>";
	}
	var cell_phone = (msg.cell_phone != '') ? msg.cell_phone : '---';
	var work_phone = (msg.work_phone != '') ? msg.work_phone : '---';
	var birthday = (msg.format_bday === null) ? '---' : msg.format_bday;
	
	//create the view
	$('#client_cell_tbl').text(cell_phone);
	$('#client_work_tbl').text(work_phone);
	$('#client_address_tbl').html(client_address_html);
	$('#client_phone_tbl').html(phones);
	$('#client_email_tbl').text(msg.email);
	$('#client_name_tbl').text(msg.fname + ' ' + msg.lname);
	
	//handle the birthday, hide the row if not in use
	if(birthday == '---'){
		$('#birthday_holder').hide();
		$('#client_birthday_tbl').text(birthday);
	}else{
		$('#birthday_holder').show();
		$('#client_birthday_tbl').text(birthday);
	}
	
	//$('#client_detail_h1').text(msg.fname + ' ' + msg.lname);
	
	//make the show / hide button toggle the visibility of the cell and work phone
	show_all_phone();
	
	//set up the edit form, just in case
	$('#editfname').val(msg.fname);
	$('#editlname').val(msg.lname);
	$('#editemail').val(msg.email);
	$('#editphone').val(msg.phone);
	$('#editcellphone').val(msg.cell_phone);
	$('#editworkphone').val(msg.work_phone);
	$('#editaddress').val(msg.address);
	$('#editcity').val(msg.city);
	$('#editstate').val((msg.state == '')?'CA':msg.state);
	$('#editzip').val(msg.zip);
	$('#editclient_id').val(msg.client_id);
	$('#editbirthday').val(birthday);
	$('#edit_format_birthday').val(msg.birthday);
	
	//set up the client notes
	$('#client_notes_input').val(msg.notes);
	
	//hide the loader
	$('#client_detail_loader').hide();
}


function uploadDone(msg,new_img,percent){
	//$('#current_logo_img').attr('src',BASE_URL + 'contractor_logos/' + new_img);
	//$('#dialog').html('<div id="dialogpad">' + msg + '</div>');
	//$("#dialog").dialog("open");
	
	//show the upload success
	$('#setting_updated').show();
	
	//mark the upload complete
	$('#setup_list li:contains("Upload your Logo")').removeClass('incomplete').addClass('complete');
	
	//animate the percentage complete
	$('#percent_progress').animate({width : percent + '%'},500);
	
	//update the number percent complete
	$('#span_num').text(percent);
	
	//load in the new image
	$('#current_logo_img').attr('src',BASE_URL + 'contractor_logos/' + new_img);
	
}

function uploadError(msg){
	//$('#dialog').html('<div id="dialogpad">' + msg + '</div>');
	//$("#dialog").dialog("open"); 
	alert(msg);
}




