/* The drag and drop functionality in draggable.js is an adaptation of the basic code written on webreference.com at
 * http://www.webreference.com/programming/javascript/mk/column2/index.html
 * with modifications to allow for left and right resizing of the div in use, time calcuations,
 * snapping dragged items to a grid, moving non-focused items as you drag, includes popup window(s),
 * provide boundaries for dragging, etc etc etc
 * 
 * additions to be made in the future:
 * - auto-detect which divs are used for resizing, if there are none, create them
 * - create style classes to accompany divs
 * - perhaps make this one big object
 */



var employees = new Array();
/*
employees[1] = new Array();
employees[1][0] = [1,'Waiter'];
employees[1][1] = [2,'Bartender'];
employees[2] = new Array();
employees[2][0] = [2,'Bartender'];
employees[2][1] = [1,'Waiter'];
employees[3] = new Array();
employees[3][0] = [2,'Bartender'];
employees[3][1] = [1,'Waiter'];
employees[3][2] = [3,'Manager'];
employees[4] = new Array();
employees[4][0] = [3,'Manager'];
employees[5] = new Array();
employees[5][0] = [2,'Bartender'];
*/

var oldWidth = null;
var dragObject  = null;
var mouseOffset = null;
var sizeObject = null;
var diff = null;
var resizeObject = null;
var resizeLeftObject = null;
var x_max = 9999999999999;
var y_max = 9999999999999;
var x_min = -9999999999999;
var y_min = -9999999999999;
var maxObj = null;
var max_coords = null;
var max_grab_w = 5; //px of width of your grabber div(s)
var div_height = 30;
var px_per_min = 1;
var minute_increment = 15 * px_per_min;
var compObject = null;
var original_y = null;
var info_container = null;
var container_str = 'emp_container_';
var popup = new Popup();
popup.setWidth(275);
var click_pause = 0;
var emp_class = 'emp_type_';
var base_mins = null;
var child_holder = null;
var assoc_array = new Array();
var id_array = new Array();
var delete_array = new Array();
var glob_mousepos_y = null;
var glob_mousepos_x = null;
var glob_y_loc = null;
var glob_x_loc = null;
var IE = navigator.userAgent.search('MSIE');
var old_top = null;
var last_e_time = null;
var last_s_time = null;
var date_array = new Array();
var sql_date_array = new Array();
var update_ol_status = 0;
var unsavedchanges = 0;

setTimeout("updateOnlineStatus(1)", 30000);
document.onmousemove = function() {
	update_ol_status = 1;
}

function setMaxObj(obj, y_scroll_obj_in) {
	document.onmousemove = mouseMove;
	document.onmouseup   = mouseUp;
	maxObj = obj;
	//max_coords = getPosition(maxObj);
	
	x_scroll_obj = document.getElementById('x_scroll');
	y_scroll_obj = y_scroll_obj_in;
	
	
	max_coords = getPosition(maxObj);
	window_size = windowSize();
	//max_coords.x += ((window_size.x/2) - (document.body.offsetWidth/2));
	//max_coords.y += ((window_size.y/2) - (document.body.offsetHeight/2));

	maxObj.style.height = y_scroll_obj.scrollHeight + 'px';
	
	y_scroll_obj.onscroll = function(ev) {
		/*if(IE != -1) {
			maxObj.style.top = (this.scrollTop * -1) - document.body.scrollTop + 'px';
			alert('hi');
		}
		else {*/
			maxObj.style.top = (this.scrollTop * -1) + 'px';
		//}
	}
	
	maxObj.ondblclick = function(ev) {
		//alert(glob_mousepos_x+' '+glob_mousepos_y);
		if(!click_pause) {
			var y_loc = glob_y_loc;
			/*if(IE != -1) {
				y_loc += document.documentElement.scrollTop;
			}*/
			
			var lg = id_array.length;
			for(i=0; i<lg; i++) {
				if(tmp_obj = assoc_array[id_array[i]][0]) {
					tmp_top = parseInt(tmp_obj.style.top,10);
					tmp_bot = tmp_top + tmp_obj.offsetHeight;
					if(y_loc >= tmp_top && y_loc <= tmp_bot) {
						emp_id = id_array[i];

						var initial_time = mouseTime(glob_x_loc);//glob_mousepos_x

						var start_day_sel_index = Math.floor((initial_time.h / 24));
						var end_day_sel_index = Math.floor(((initial_time.h + 1) / 24));

						/*initial_time_str = demilitarize(initial_time.h+':'+initial_time.m);
						tmp_split = initial_time_str.split(':');*/
						initial_time.h %= 24;


						var pm_selected = 0;
						var end_pm_selected = 0;
						
						if(initial_time.h >= 12) {
							pm_selected = 1;
							if(start_day_sel_index == end_day_sel_index) {
								end_pm_selected = 1;
							}
							end_hr_var = initial_time.h - 11;
							if(initial_time.h > 12) {
								initial_time.h -= 12;
							}
						}
						else {
							end_hr_var = initial_time.h + 1;
							if(end_hr_var == 12 && start_day_sel_index == end_day_sel_index) {
								end_pm_selected = 1;
							}
						}
						
						if(initial_time.h == 0) {
							initial_time.h = 12;
						}
						
						if(initial_time.h < 10) {
							var tmp_pre = '0';
						}
						else {
							var tmp_pre = '';
						}
						if(initial_time.h < 9) {
							var tmp_pretwo = '0';
						}
						else {
							var tmp_pretwo = '';
						}
						if(initial_time.m < 10) {
							var tmp_pre_min = '0';
						}
						else {
							var tmp_pre_min = '';
						}
						
						end_min_var = tmp_pre_min +initial_time.m;
						
						/*if(last_e_time) {
							last_e_time = demilitarize(last_e_time);
							tmp_time_arr = last_e_time.split(':');
							end_hr_var = tmp_time_arr[0];
							end_min_var = tmp_time_arr[1];
							tmp_pretwo = '';
							//alert(end_hr_var+' '+end_min_var);
						}*/
						
						popup.closeWindow();
						emp_type = tmp_obj.getAttribute('emptype');
						filler_html = document.getElementById('emp_container_'+emp_id+'_leftinfo').innerHTML;
	
						dia_store = '<div class="blank-dialog"><select id="classSel">';
						if(!employees[emp_id]) {
							alert('This employee does not have any positions listed for him/her.  Go to the People page and specify all positions for all employees in order to assign them to work hours.');
						}
						else{
							var lg = employees[emp_id].length;
							for(i=0; i<lg; i++) {
								if(employees[emp_id][i][2]) {
									wage_str = '  '+employees[emp_id][i][2];
								}
								else {
									wage_str = '';
								}
								
								if(employees[emp_id][i][1]) {
									if(employees[emp_id][i][0] == emp_type) {
										dia_store += '<option value="'+employees[emp_id][i][0]+'" selected>'+employees[emp_id][i][1]+wage_str+'</option>';
									}
									else {
										dia_store += '<option value="'+employees[emp_id][i][0]+'">'+employees[emp_id][i][1]+wage_str+'</option>';
									}
	
								}
								else {
									if(employees[emp_id][i][0] == emp_type) {
										dia_store += '<option value="'+employees[emp_id][i][0]+'" selected>'+employees[emp_id][i][0]+wage_str+'</option>';
									}
									else {
										dia_store += '<option value="'+employees[emp_id][i][0]+'">'+employees[emp_id][i][0]+wage_str+'</option>';
									}
								}
							}
							lg = date_array.length;
							s_day_sel = '';
							e_day_sel = '';
							if(lg) { //multiple dates, build dropdowns
								s_day_sel = '<select id="start_date_drop">';
								for(i=0; i<lg; i++) {
									if(i != start_day_sel_index) {
										s_day_sel += '<option value="'+i+'">'+date_array[i]+'</option>';
									}
									else {
										s_day_sel += '<option value="'+i+'" selected>'+date_array[i]+'</option>';
									}
								}
								s_day_sel += '</select>';
								e_day_sel = '<select id="end_date_drop">';
								for(i=0; i<lg; i++) {
									if(i != end_day_sel_index) {
										e_day_sel += '<option value="'+i+'">'+date_array[i]+'</option>';
									}
									else {
										e_day_sel += '<option value="'+i+'" selected>'+date_array[i]+'</option>';
									}
								}
								e_day_sel += '</select>';
								
							}
							dia_store += '</select><BR /><BR /><label><input type="checkbox" onclick="fullShiftShuffle(this);" id="popup_fullshift"/>Schedule For Full Shift</label><BR /><div class="dia-line"></div><BR />';
							dia_store += '<div id="popup_times"><div class="dp-left">Start:</div><input type="text" value="'+ tmp_pre +initial_time.h+'" id="start_hr" size="2" maxlength=2 onblur="updateDraggableTime(\'start\');" /> : <input type="text" value="'+ tmp_pre_min +initial_time.m+'" id="start_min" size="2" maxlength=2 onblur="updateDraggableTime(\'start\');" /><select id="start_ampm"><option value="0">am</option><option value="12">pm</option></select>&nbsp'+s_day_sel+'<BR class="floater-cleanup"/><div class="dp-left">End:</div><input type="text" value="'+ tmp_pretwo +(end_hr_var)+'" id="end_hr" size="2" maxlength=2 onblur="updateDraggableTime(\'end\');"/> : <input type="text" value="'+ end_min_var +'" id="end_min" size="2" maxlength=2 onblur="updateDraggableTime(\'end\');"/><select id="end_ampm"><option value="0">am</option><option value="12">pm</option></select>&nbsp'+e_day_sel+'<BR class="floater-cleanup"/><BR/><input type="hidden" id="start_storage" /><input type="hidden" id="end_storage" /></div>';
							dia_store += '<div class="dialog-buttons"><input type="button" class="save_button" value=" " onclick="/*updateDraggableTime(\'start\'); updateDraggableTime(\'end\');*/ createShift('+emp_id+','+glob_mousepos_x+',\''+tmp_obj.id+'\', \'dialogWindow'+popup.printCount()+'\'); " /> <input type="button" class="cancel_button" value=" " onclick="document.getElementById(\'dialogWindow'+popup.printCount()+'\').style.display=\'none\';" /></div></div>';	
							popup.blankDialogNoBg(filler_html+dia_store, '<img src="/lifefyle/new_images/chris_clock.gif" />&nbsp;<span style="position:relative; top:-8px;">Create a Shift</span>');
							y_in = glob_y_loc;
							if(IE != -1) {
								y_in += document.documentElement.scrollTop;
							}
							//cc_mouse_in = glob_mousepos_x - document.getElementById('tcontent').offsetLeft - 120; //120 = width of shift_names_head
							//popup.changeCoords(cc_mouse_in, y_in);//glob_x_loc
							
							//if you end up uncommenting the above, beware of a bug on ie6 that makes the popup display at the bottom of the screen (likely has to do w/ our mouse pos, also find and uncomment the same lines for item.ondblclick further down
							
							if(pm_selected) {
								document.getElementById('start_ampm').value = 12;
							}
							if(end_pm_selected) {
								document.getElementById('end_ampm').value = 12;
							}
							click_pause = 0;
							return false;
						}//end error check else statement
					}//end y_loc check if statement
				}//end tmp_obj if statement
			}//end for loop
		click_pause = 0;
		}//end if click_pause
	}//end dblclick function
	
	
	if(add_left = parseInt(maxObj.style.borderLeftWidth,10)) {
		max_coords.x += add_left;
	}
	if(add_right = parseInt(maxObj.style.borderRightWidth,10)) {
		max_coords.x += add_right;
	}
	if(add_top = parseInt(maxObj.style.borderTopWidth,10)) {
		max_coords.y += add_top;
	}
	if(add_bottom = parseInt(maxObj.style.borderBottomWidth,10)) {
		max_coords.y += add_bottom;
	}
	
	if(IE != -1) { //IE 6 7
		var base = parseInt(document.getElementById('x_scroll').style.height,10);
		//document.getElementById('x_scroll').style.height = (base + add_top + add_bottom) + 'px';
	}
	

}

function setMax(obj) {

	if(maxObj && obj) {
		x_max = parseInt(maxObj.style.width,10) - (obj.offsetWidth - mouseOffset.x);
		x_min = 0 + mouseOffset.x;
		y_max = parseInt(maxObj.offsetHeight,10) - (obj.offsetHeight - mouseOffset.y);
		y_min = 0 +  mouseOffset.y;
	}
	else if(maxObj) {
		obj = null;
		x_max = parseInt(maxObj.style.width,10);
		x_min = mouseOffset.x;
		y_max = parseInt(maxObj.offsetHeight,10);
		y_min = 0;
	}
	else {
		x_max = document.body.offsetWidth;
		y_max = document.body.offsetHeight;
		x_min = 0;
		y_min = 0;	
	}

}


/*function mouseMove(ev){
	ev           = ev || window.event;
	var mousePos = mouseCoords(ev);
}*/

function mouseCoords(ev){
	//document.getElementById('STATBOX').innerHTML = 'limit pos: '+max_coords.y+' scrolltop: '+y_scroll_obj.scrollTop+' ev.clientY: ' + ev.clientY+'<BR/>de.scrolltop: '+document.documentElement.scrollTop;/*'<BR/>limit posx: '+max_coords.x+' xscrollleft: '+x_scroll_obj.scrollLeft+' ev.clientX: ' + ev.clientX;*/
	if(!ev) { ev = window.event; }
	if(ev.pageX || ev.pageY){
		var x_coord = ev.pageX - max_coords.x + x_scroll_obj.scrollLeft;
		var y_coord = ev.pageY - max_coords.y + y_scroll_obj.scrollTop;
	}
	else {
		var x_coord = (ev.clientX - max_coords.x + x_scroll_obj.scrollLeft) + document.documentElement.scrollLeft;
		var y_coord = (ev.clientY - max_coords.y + y_scroll_obj.scrollTop) + document.documentElement.scrollTop;
	}

	switch(true) {
		case (x_coord > x_max):
			x_coord = x_max;
			break;
		case (x_coord < x_min):
			x_coord = x_min;
			break;
	}

	switch(true) {
		case (y_coord > y_max):
			y_coord = y_max;
			break;
		case (y_coord < y_min):
			y_coord = y_min;
			break;
	}

	return {x:x_coord, y:y_coord};
}

function actualMouseCoords(ev){
	if(!ev) { ev = window.event; }
	if(ev.pageX || ev.pageY){
		var x_coord = ev.pageX;
		var y_coord = ev.pageY;
	}
	else {
		var x_coord = ev.clientX;
		var y_coord = ev.clientY;
	}

	return {x:x_coord, y:y_coord};
}

/*
function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y};
}*/

function getMouseOffset(target, ev){
	ev = ev || window.event;

	var docPos_x    = parseInt(target.style.left,10);
	var docPos_y    = parseInt(target.style.top,10);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - docPos_x, y:mousePos.y - docPos_y};
}

//may need to use this later (instead of offsetLeft) - set glob variables to result so we only run it once
function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}
	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function mouseMove(ev){
	update_ol_status = 1;
	ev = ev || window.event;
	var mousePos = mouseCoords(ev);
	glob_y_loc = mousePos.y;
	glob_x_loc = mousePos.x;
	//document.getElementById('STATBOX').innerHTML = glob_y_loc;
	var actualMousePos = actualMouseCoords(ev);
	glob_mousepos_y = actualMousePos.y;
	glob_mousepos_x = actualMousePos.x;
	if(dragObject){
		new_y = mousePos.y - mouseOffset.y;
		info_container.style.position = dragObject.style.position = 'absolute';
		info_container.style.top = dragObject.style.top = new_y + 'px';
		dragObject.style.left = mousePos.x - mouseOffset.x + 'px';

		var lg = maxObj.childNodes.length;
		for(i=0; i<lg; i++) {
			tmp_obj = maxObj.childNodes[i];
			if(tmp_obj.nodeType == 1 && tmp_obj.getAttribute('empid') == dragObject.getAttribute('empid')) {
				tmp_obj.style.top = dragObject.style.top;
			}
		}
		
		if(new_y <= original_y) { //moving up
			moveBelow(dragObject);
			click_pause = 1;
		}
		else if(new_y > original_y) { //moving down
			moveAbove(dragObject);
			click_pause = 1;
		}
		
		determineTime(dragObject);
		//document.getElementById('STATBOX').innerHTML = mouseOffset.y;
		//document.getElementById('STATBOX').innerHTML=dragObject.style.top;
		return false;
	}
	else if(resizeObject) {
		//off_coords = getPosition(sizeObject);
		off_coords_x = parseInt(sizeObject.style.left,10);
		
		//document.getElementById('STATBOX').innerHTML = mouseOffset.x;
		sizeObject.style.width = (mousePos.x - (off_coords_x + sizeObject.offsetWidth)) + sizeObject.offsetWidth + 'px';
		resizeObject.style.left = sizeObject.offsetWidth - resizeObject.offsetWidth + 'px';
		// - mouseOffset.x
		//document.getElementById('STATBOX').innerHTML = sizeObject.style.width;
		determineTime(sizeObject);
		return false;
	}
	else if(resizeLeftObject) {
		//off_coords = getPosition(sizeObject);
		//off_coords_x = parseInt(sizeObject.style.left,10);
		sizeObject.style.position = 'absolute';
		
		var old_left = parseInt(sizeObject.style.left,10);
		var new_left = Math.ceil(mousePos.x - mouseOffset.x);
		old_width = parseInt(sizeObject.style.width,10);
		new_width = old_width + (old_left - new_left);
		
		if(new_width < minute_increment) {
			new_left = old_left;
			new_width = old_width;
		}
		
		//sizeObject.style.width = ((off_coords_x + sizeObject.offsetWidth) - (mousePos.x - mouseOffset.x));
		sizeObject.style.width = new_width + 'px';
		

		sizeObject.style.left = new_left + 'px';
		//document.getElementById('STATBOX').innerHTML = mouseOffset.x;

		//resizeLeftObject.style.left = sizeObject.offsetLeft - mouseOffset.x;
		resizeLeftObject.style.left = 0 + 'px';
		resizeRight.style.left = sizeObject.offsetWidth - resizeRight.offsetWidth + 'px';
		determineTime(sizeObject);
		//document.getElementById('STATBOX').innerHTML = sizeObject.style.width;
		return false;
	}
}

function mouseUp(){
	if(resizeLeftObject) { //shrink width add left, add width shrink left
		unsavedchanges = 1;
		diff = sizeObject.offsetWidth-oldWidth; //not currently using "diff" but may need it.
		oldWidth = sizeObject.offsetWidth; //not currently using "oldWidth" but may need it.
		var left_int = parseInt(sizeObject.style.left, 10);
		var remainder = (left_int)% minute_increment;
			if(remainder > (minute_increment/2)) {
				sizeObject.style.left = left_int + (minute_increment - remainder) + 'px';
				sizeObject.style.width = sizeObject.offsetWidth - (minute_increment - remainder) + 'px';
				resizeRight.style.left = sizeObject.offsetWidth - resizeRight.offsetWidth + 'px';
				resizeLeftObject.style.left = 0 + 'px';
				//document.getElementById('STATBOX').innerHTML = sizeObject.style.left;
			}
			else {
				sizeObject.style.left = left_int - remainder + 'px';
				sizeObject.style.width = sizeObject.offsetWidth + remainder + 'px';
				resizeRight.style.left = sizeObject.offsetWidth - resizeRight.offsetWidth + 'px';
				resizeLeftObject.style.left = 0 +'px';
				//document.getElementById('STATBOX').innerHTML = sizeObject.style.left;
			}
		determineTime(sizeObject);
		makeDraggable(sizeObject);
		updateHours(sizeObject.getAttribute('empid'));
	}
	else if(resizeObject) {
		unsavedchanges = 1;
		diff = sizeObject.offsetWidth-oldWidth;
		oldWidth = sizeObject.offsetWidth;
		
		var w_int = sizeObject.offsetWidth;
		var remainder = w_int % minute_increment;
			if(remainder > (minute_increment/2)) {
				sizeObject.style.width = w_int + (minute_increment - remainder) + 'px';
				//document.getElementById('STATBOX').innerHTML = sizeObject.style.width;
				resizeObject.style.left = sizeObject.offsetWidth - resizeObject.offsetWidth + 'px';
			}
			else {
				sizeObject.style.width = w_int - remainder + 'px';
				//document.getElementById('STATBOX').innerHTML = sizeObject.style.width;
				resizeObject.style.left = sizeObject.offsetWidth - resizeObject.offsetWidth + 'px';
			}
		determineTime(sizeObject);
		makeDraggable(sizeObject);
		updateHours(sizeObject.getAttribute('empid'));
	}
	
	else if(dragObject) {
		unsavedchanges = 1;
		var left_int = parseInt(dragObject.style.left, 10);
		var remainder = (left_int)% minute_increment;
			if(remainder > (minute_increment/2)) {
				dragObject.style.left = left_int + (minute_increment - remainder) + 'px';
				//document.getElementById('STATBOX').innerHTML = dragObject.style.left;
			}
			else {
				dragObject.style.left = left_int - remainder + 'px';
				//document.getElementById('STATBOX').innerHTML = dragObject.style.left;
			}
		determineTime(dragObject);
		var top_int = parseInt(dragObject.style.top,10);
		remainder = (top_int) % (dragObject.offsetHeight);
		var halfsies = (dragObject.offsetHeight / 2);
		if(remainder > halfsies) {
			info_container.style.top = dragObject.style.top = top_int + (dragObject.offsetHeight - remainder) + 'px';
		}
		else {
			info_container.style.top = dragObject.style.top = top_int - remainder + 'px';
		}
		
		var lg = maxObj.childNodes.length;
		for(i=0; i<lg; i++) {
			tmp_obj = maxObj.childNodes[i];
			if(tmp_obj.nodeType == 1 && tmp_obj.getAttribute('empid') == dragObject.getAttribute('empid')) {
				tmp_obj.style.top = dragObject.style.top;
			}
		}
		
//		dragObject.style.cssText += "filter:alpha(opacity=100);opacity:1;";
		dragObject.style.opacity = '1';
		dragObject.style['filter'] = 'alpha(opacity=100)';
		cleanUp(dragObject,1);
		dragObject.style.zIndex = '5';
		info_container.style.zIndex = '';
	}
	resizeRight = null;
	dragObject = null;
	sizeObject = null;
	resizeObject = null;
	resizeLeftObject = null;
	mouseOffset = null;
	info_container = null;
	click_pause = 0;
//	left_int = null;
//	off_coords = null;
	x_max=y_max=99999;
	x_min=y_min=0;
	
}

function updateHours(id) {
if(!id) { return false; }
	//var id = inObj.getAttribute('empid');
	var h_wk = document.getElementById('hours_week_'+id);
	var h_day = document.getElementById('hours_day_'+id);
	var go_base = 1;
	if(h_wk.getAttribute('basemins') == null) {
		var wk_hr = timeToMins(h_wk.innerHTML);
		h_wk.setAttribute('basemins', wk_hr);
	}
	else {
		var wk_hr = parseInt(h_wk.getAttribute('basemins'),10);
	}
	if(h_day.getAttribute('basemins') == null) {
		var day_hr = timeToMins(h_day.innerHTML);
		h_day.setAttribute('basemins', day_hr);
	}
	else {
		var day_hr = parseInt(h_day.getAttribute('basemins'),10);
	}

	var lg = assoc_array[id].length;
	//alert(lg+' '+id);
	for(j=0; j<lg; j++) {
		if(duration = parseInt(assoc_array[id][j].getAttribute('emp_mins'),10)) {
			//duration /= 60;
			//duration = parseFloat(duration.toFixed(2),10);
	
			wk_hr +=duration;
			h_wk.innerHTML = minsToTime(wk_hr);
	
			day_hr += duration;
			h_day.innerHTML = minsToTime(day_hr);
			go_base = 0;

			if(wk_hr <= 1800) {
				h_wk.className = 'green-small';
			}
			else if(wk_hr < 2400) {
				h_wk.className = 'yellow-small';
			}
			else {
				h_wk.className = 'red-small';
			}
			
			if(day_hr <= 300) {
				h_day.className = 'green-small';
			}
			else if(day_hr < 480) {
				h_day.className = 'yellow-small';
			}
			else {
				h_day.className = 'red-small';
			}
		}
	}
	if(go_base) {
			h_wk.innerHTML = minsToTime(wk_hr);
	
			h_day.innerHTML = minsToTime(day_hr);
			
			if(day_hr <= 300) {
				h_day.className = 'green-small';
			}
			else if(day_hr < 480) {
				h_day.className = 'yellow-small';
			}
			else {
				h_day.className = 'red-small';
			}

			if(wk_hr <= 1800) {
				h_wk.className = 'green-small';
			}
			else if(wk_hr < 2400) {
				h_wk.className = 'yellow-small';
			}
			else {
				h_wk.className = 'red-small';
			}

	}
}

function minsToTime(mins_in){
	var ret_time = '00:00';
	
	if(mins_in > 0) {
		if(tmp_time = (mins_in/60)) {
			hrs = Math.floor(tmp_time);
			mins = (tmp_time - hrs) * 60;
			mins = Math.round(mins);
		}
		else {
			hrs = 0;
			mins = mins_in;
		}
		if(hrs < 10) {
			hrs = '0'+hrs;
		}
		if(mins < 10) {
			mins = '0'+mins;
		}
		ret_time = hrs+':'+mins;
	}	
	//ret_time += ':00';	//make sure to include this when ajaxing to database!
	return ret_time;
}

function timeToMins(time_in) {
	var time_arr = time_in.split(':');
	if(!time_arr || time_arr.length < 2){return false;}
	var ret_mins = (parseInt(time_arr[0],10) * 60) + parseInt(time_arr[1],10);
	return ret_mins;
}

function determineTime(inObj) {
	if(!inObj || !maxObj) { return false;}
	
	setBaseMins();

	var tmp_start = parseInt(inObj.style.left,10);

/*	if(isNaN(tmp_start)) {
		tmp_start = 0;
	}
	else {
		tmp_start -= max_coords.x;
	}
*/
	tmp_start = Math.round(tmp_start/px_per_min);

	var start_mins = base_mins + tmp_start;
	var end_mins = base_mins + tmp_start + Math.round(inObj.offsetWidth/px_per_min);
	inObj.setAttribute('emp_start_time', minsToTime(start_mins));
	inObj.setAttribute('emp_end_time', minsToTime(end_mins));
	var duration = end_mins - start_mins;
	inObj.setAttribute('emp_mins', duration);
	if(duration > (44 * px_per_min)) {
		document.getElementById(inObj.id+'_contents').innerHTML = demilitarize(minsToTime(start_mins))+' - '+demilitarize(minsToTime(end_mins));
	}
	else {
		document.getElementById(inObj.id+'_contents').innerHTML = '';
	}
	//alert(inObj.getAttribute('emp_start_time'));
	//alert(inObj.getAttribute('emp_end_time'));
}

function demilitarize(time_in) {
	var time_arr = time_in.split(':');
	var hr = parseInt(time_arr[0],10)
	
	tmp_hr = hr % 24;
	switch(tmp_hr) {
		case 0:
			hr = 12;
			break;
		case 12:
			hr = 12;
			break;
		default:
			if(tmp_hr < 12) {
				hr = tmp_hr;
			}
			else {
				hr = tmp_hr - 12;
			}
	}

	return hr+':'+time_arr[1];
}

function mouseTime(x_coord) {
	setBaseMins();

	//var tmp_pos = getPosition(maxObj);
	//var tmp_pos = max_coords;

	var mins = x_coord;// - tmp_pos.x;
	if(mins <= 0) {
		return {h:0, m:0};
	}

	mins = Math.round(mins/px_per_min);
	mins += base_mins;

	if(remainder = mins % (minute_increment/px_per_min)) {
		if(remainder >= ((minute_increment/px_per_min)/2)) {
			mins += ((minute_increment/px_per_min) - remainder);
		}
		else {
			mins -= remainder;
		}
	}
	var hrs = Math.floor(mins/60);
	mins %= 60;

	return {h:hrs, m:mins};
}

function makeDraggable(item){
	if(!item) return false;

	//item_coords = getPosition(item);
	var tmp_str = '';
	item.style.zIndex = '5';
	//item.style.top = item_coords.y + 'px';
	//item.style.left = item_coords.x + 'px';
	item.style.position = 'absolute';
	div_contents = document.getElementById(item.id+'_contents');
	
	if(!div_contents) {
		item.innerHTML = '<div style="padding-left:4px;" id="'+item.id+'_contents" style="position:absolute;"></div>'+item.innerHTML;
	}
	determineTime(item);
	
	item.onmousedown = function(ev){
		dragObject  = this;
		sizeObject = null;
		resizeObject = null;
		resizeLeftObject = null;
		original_y = parseInt(this.style.top,10);
		mouseOffset = getMouseOffset(this, ev);
		setMax(this);
//		item.style.cssText += "filter:alpha(opacity=75);opacity:0.75;";
		item.style.opacity = '0.75'; //FF & Safari
		item.style['filter'] = 'alpha(opacity=75)'; //IE
		item.style.zIndex = '9999';
		if(tmp_str = item.getAttribute('empid')) {
			info_container = document.getElementById(container_str+tmp_str);
		}
		info_container.style.zIndex = '20';
		click_pause = 0;




		return false;
	}

	
	item.ondblclick = function(ev) {
		if(!click_pause && glob_x_loc > 0) {
			click_pause = 1;
			popup.closeWindow();
			emp_id = item.getAttribute('empid');
			emp_type = item.getAttribute('emptype');
			
			emp_start = item.getAttribute('emp_start_time');
			emp_end = item.getAttribute('emp_end_time');
			
			emp_start = emp_start.split(':');
			emp_end = emp_end.split(':');
			
			emp_start[0] = parseInt(emp_start[0],10);
			emp_end[0] = parseInt(emp_end[0],10);
			
			var start_day_sel_index = Math.floor((emp_start[0] / 24));
			var end_day_sel_index = Math.floor((emp_end[0] / 24));

			/*initial_time_str = demilitarize(initial_time.h+':'+initial_time.m);
			tmp_split = initial_time_str.split(':');*/
			emp_end[0] %= 24;
			emp_start[0] %= 24;
			
			var pm_selected = 0;
			var end_pm_selected = 0;
						
			if(emp_start[0] >= 12) {
				pm_selected = 1;

				if(emp_start[0] > 12) {
					emp_start[0] -= 12;
				}
			}
			if(emp_start[0] == 0) {
				emp_start[0] = 12;
			}
			if(emp_start[0] < 10){
				emp_start[0] = '0'+emp_start[0];
			}
				
			if(emp_end[0] >= 12) {
				end_pm_selected = 1;

				if(emp_end[0] > 12) {
					emp_end[0] -= 12;
				}
			}
			if(emp_end[0] == 0) {
				emp_end[0] = 12;
			}			
			if(emp_end[0] < 10){
				emp_end[0] = '0'+emp_end[0];
			}
			
			emp_id = this.getAttribute('empid');
			filler_html = document.getElementById('emp_container_'+emp_id+'_leftinfo').innerHTML;
			dia_store = '<div class="blank-dialog"><select id="classSel">';
			var lg = employees[emp_id].length;
			for(i=0; i<lg; i++) {
				if(employees[emp_id][i][2]) {
					wage_str = '  '+employees[emp_id][i][2];
				}
				else {
					wage_str = '';
				}
				
				if(employees[emp_id][i][1]) {
					if(employees[emp_id][i][0] == emp_type) {
						dia_store += '<option value="'+employees[emp_id][i][0]+'" selected>'+employees[emp_id][i][1]+wage_str+'</option>';
					}
					else {
						dia_store += '<option value="'+employees[emp_id][i][0]+'">'+employees[emp_id][i][1]+wage_str+'</option>';
					}
					
				}
				else {
					if(employees[emp_id][i][0] == emp_type) {
						dia_store += '<option value="'+employees[emp_id][i][0]+'" selected>'+employees[emp_id][i][0]+wage_str+'</option>';
					}
					else {
						dia_store += '<option value="'+employees[emp_id][i][0]+'">'+employees[emp_id][i][0]+wage_str+'</option>';
					}
				}
			}
			
			lg = date_array.length;
			s_day_sel = '';
			e_day_sel = '';
			if(lg) { //multiple dates, build dropdowns
				s_day_sel = '<select id="start_date_drop">';
				for(i=0; i<lg; i++) {
					if(i != start_day_sel_index) {
						s_day_sel += '<option value="'+i+'">'+date_array[i]+'</option>';
					}
					else {
						s_day_sel += '<option value="'+i+'" selected>'+date_array[i]+'</option>';
					}
				}
				s_day_sel += '</select>';
				e_day_sel = '<select id="end_date_drop">';
				for(i=0; i<lg; i++) {
					if(i != end_day_sel_index) {
						e_day_sel += '<option value="'+i+'">'+date_array[i]+'</option>';
					}
					else {
						e_day_sel += '<option value="'+i+'" selected>'+date_array[i]+'</option>';
					}
				}
				e_day_sel += '</select>';
			}
			
			dia_store += '</select><BR /><div class="dia-line"></div><BR />';
			dia_store += '<div class="dp-left">Start:</div><input type="text" value="'+emp_start[0]+'" id="start_hr" size="2" maxlength=2 onblur="updateDraggableTime(\'start\');"/> : <input type="text" value="'+emp_start[1]+'" id="start_min" size="2" maxlength=2 onblur="updateDraggableTime(\'start\');"/><select id="start_ampm"><option value="0">am</option><option value="12">pm</option></select>&nbsp'+s_day_sel+'<BR class="floater-cleanup"/><div class="dp-left">End:</div><input type="text" value="'+emp_end[0]+'" id="end_hr" size="2" maxlength=2 onblur="updateDraggableTime(\'end\');"/> : <input type="text" value="'+emp_end[1]+'" id="end_min" size="2" maxlength=2 onblur="updateDraggableTime(\'end\');"/><select id="end_ampm"><option value="0">am</option><option value="12">pm</option></select>&nbsp'+e_day_sel+'<BR class="floater-cleanup"/><BR/><input type="hidden" id="start_storage" /><input type="hidden" id="end_storage" />';
			dia_store += '<div class="dialog-buttons"><input type="button" class="save_button" value=" " onclick="updateTime(\'start\'); updateTime(\'end\'); if(verifySqlTimes(document.getElementById(\'start_storage\').value,document.getElementById(\'end_storage\').value)) { updateDraggableTime(\'start\',1); updateDraggableTime(\'end\',1); classifyEmpById(document.getElementById(\'classSel\').value,\''+item.id+'\'); changeTimes(\''+item.id+'\'); popup.closeWindow(); click_pause = 0; }" /> <input type="button" class="delete_button" value=" " onclick="popup.closeWindow(); deleteObj(\''+item.id+'\'); click_pause = 0;" /> <input type="button" class="cancel_button" value=" " onclick="popup.closeWindow(); click_pause = 0;" /></div></div>';
			popup.blankDialogNoBg(filler_html+dia_store, 'Edit Shift');
			//cc_mouse_in = glob_mousepos_x - document.getElementById('tcontent').offsetLeft - 120; //120 = width of shift_names_head
			//popup.changeCoords(cc_mouse_in, glob_y_loc);
			
			if(pm_selected) {
				document.getElementById('start_ampm').value = 12;
			}
			if(end_pm_selected) {
				document.getElementById('end_ampm').value = 12;
			}
		}
		
	}
	
		
}


function makeResizer(item){
	if(!item) return;
	
	item.style.top = '0px';
	item.style.left = item.parentNode.offsetWidth - item.offsetWidth + 'px';
	item.style.height = item.parentNode.offsetHeight + 'px';
	
	oldWidth = item.parentNode.offsetWidth;
	item.onmousedown = function(ev){
		sizeObject  = this.parentNode;
		resizeObject = this;
		dragObject = null;
		mouseOffset = getMouseOffset(this, ev);
		item.parentNode.onmousedown = '';
		setMax();
		return false;
	}
}

function makeLeftResizer(item) {
	if(!item) return;
	
	item.style.left = item.style.top = '0px';
	item.style.height = item.parentNode.offsetHeight + 'px';
	

	oldWidth = item.parentNode.offsetWidth;
	item.onmousedown = function(ev){
		var tmp_par = this.parentNode;
		var lg = tmp_par.childNodes.length;
		for(k=0; k<lg; k++) {
			if(tmp_par.childNodes[k].nodeType == 1 && tmp_par.childNodes[k].className == 'right_grab') {
				itemright = item.parentNode.childNodes[k];
			}
		}
		
		sizeObject  = this.parentNode;
		resizeLeftObject = this;
		resizeRight = itemright;
		dragObject = null;
		mouseOffset = getMouseOffset(this, ev);
		if(mouseOffset.x > max_grab_w) {mouseOffset.x = Math.floor(max_grab_w/2);}
		item.parentNode.onmousedown = '';
		setMax();
		return false;
	}

}

function deleteObj(id) {
	unsavedchanges = 1;
	var obj = document.getElementById(id);
	obj.style.display = 'none';
	obj.id = 'del_'+id;
	var emp_id = obj.getAttribute('empid');
	
	if(tmp_var = obj.getAttribute('hours_entry')) {
		obj.setAttribute('delete','1');
		obj.setAttribute('emp_mins', '');
	}
	else {
		obj.setAttribute('empid', '');
		obj.setAttribute('emp_mins', '');
		obj.setAttribute('emptype', '');
		obj.setAttribute('emp_end_time', '');
		obj.setAttribute('emp_start_time', '');
	}

	var lg = obj.childNodes.length;
	for(i=0; i<lg; i++) {
		if(obj.childNodes[i].nodeType == 1) {
			obj.childNodes[i].id = 'del_'+obj.childNodes[i].id;
		}
	}
	
	associateObjects();
	updateHours(emp_id);
	
	if(assoc_array[emp_id].length < 2) {
		document.getElementById('quick_add_'+emp_id).src = '/lifefyle/new_images/plus.gif';
	}
	
	//determine times
}

function createShift(emp_id, mouse_x, obj_id, pop_id) {
	unsavedchanges = 1;
	updateTime('start');
	updateTime('end');

	if(!obj_id) { obj_id = 'new_shift_'+emp_id; }
	var ct = 0;
	while(document.getElementById(obj_id+'_'+ct)) {
		ct++;
	}
	//var pos = getPosition(maxObj);
	var pos = max_coords;
	
	var c_val = document.getElementById('classSel').value;
	//var s_time = document.getElementById('start_hr').value + ':' + document.getElementById('start_min').value;
	//var e_time = document.getElementById('end_hr').value + ':' + document.getElementById('end_min').value;


	
	if(document.getElementById('popup_fullshift').checked) {
		var s_time = time_start; //global vars set by php
		var e_time = time_end;
	}
	else {
		var s_time = document.getElementById('start_storage').value;
		var e_time = document.getElementById('end_storage').value;
	}
	
//	last_s_time = s_time;
//	last_e_time = e_time;
	
	var start_time = timeToMins(s_time);
	var end_time = timeToMins(e_time);
	
	de = date_array.length;
	if(de) {
		add_day = parseInt(document.getElementById('start_date_drop').value,10);
		start_time += (1440 * add_day);
		add_day_e = parseInt(document.getElementById('end_date_drop').value,10);
		end_time += (1440 * add_day_e);
		s_time = minsToTime(start_time);
		e_time = minsToTime(end_time);	
	}
	
	var duration = end_time - start_time;
	
	if(duration <= 0) {
		alert('Invalid times: End time must come after start time.');
		return false;
	}
	else {
		if(updateDraggableTime('start',1)) {
			updateDraggableTime('end',1);
		}
		else {
			updateDraggableTime('end');
		}
	}

	if(document.getElementById('popup_fullshift').checked) {
		s_time = time_start; //global vars set by php
		e_time = time_end;
	}
	else {
		s_time = document.getElementById('start_storage').value;
		e_time = document.getElementById('end_storage').value;
	}

	start_time = timeToMins(s_time);
	end_time = timeToMins(e_time);

/*	de = date_array.length;
	if(de) {
		add_day = parseInt(document.getElementById('start_date_drop').value,10);
		start_time += (1440 * add_day);
		add_day_e = parseInt(document.getElementById('end_date_drop').value,10);
		end_time += (1440 * add_day_e);
		s_time = minsToTime(start_time);
		e_time = minsToTime(end_time);
	}*/
	
	duration = end_time - start_time;
	
	var top_val = document.getElementById(container_str+emp_id).style.top;
	
	var tmp_left = ((start_time * px_per_min)) - base_mins;

	maxObj.innerHTML += '<div style="height:'+div_height+'px; width:'+ (duration * px_per_min) +'px; position:absolute; top:'+top_val+'; left:'+tmp_left+'px;" id="'+obj_id +'_'+ ct+'" class="'+emp_class + c_val+'" emp_start_time="'+s_time+'" emp_end_time="'+e_time+'" emptype="'+c_val+'" emp_mins="'+duration+'" empid="'+emp_id+'"><div id="'+obj_id +'_'+ ct+'_eresize" class="right_grab"></div><div id="'+obj_id +'_'+ ct+'_wresize" class="left_grab"></div>&nbsp;</div>';
	makeAllDraggable();
	updateHours(emp_id);

	document.getElementById('quick_add_'+emp_id).src = '/lifefyle/new_images/minus.gif';

	//document.getElementById(pop_id).style.display='none';
	popup.closeWindow();
}

function makeAllDraggable() {
	var lg = maxObj.childNodes.length;
	for(i=0; i<lg; i++) {
		if(maxObj.childNodes[i].nodeType == 1 && maxObj.childNodes[i].getAttribute('empid')) {
			tmp_obj = maxObj.childNodes[i];
			tmp_lg = tmp_obj.childNodes.length;
			if(tmp_obj.className != 'emp_holder' && tmp_obj.className != 'still') {
				makeDraggable(tmp_obj);

				for(j=0; j<=tmp_lg; j++) {
					if(tmp_obj.childNodes[j] && tmp_obj.childNodes[j].nodeType == 1) {
						if(tmp_obj.childNodes[j].className == 'left_grab') {
							makeLeftResizer(tmp_obj.childNodes[j]);
						}
						else if(tmp_obj.childNodes[j].className == 'right_grab') {
							right_grab = tmp_obj.childNodes[j];
							makeResizer(right_grab);
						}
					}
				}
			}
		}
	}
	associateObjects();
}

function makeAllDoubleClickable() {
	//return false;
	var lg = id_array.length;
	for(i=0; i<lg; i++) {
		makeDoubleClickable(document.getElementById(container_str+id_array[i]));
		updateHours(id_array[i]);
	}
}

function changeTimes(id) {
	updateDraggableTime('start');
	updateDraggableTime('end');
	var obj = document.getElementById(id);
	//var s_time = document.getElementById('start_hr').value + ':' + document.getElementById('start_min').value;
	//var e_time = document.getElementById('end_hr').value + ':' + document.getElementById('end_min').value;
	var s_time = document.getElementById('start_storage').value;
	var e_time = document.getElementById('end_storage').value;
	
	if(s_time != obj.getAttribute('emp_start_time')) {
		var diff = timeToMins(s_time) - timeToMins(obj.getAttribute('emp_start_time'));
		obj.style.left = parseInt(obj.style.left) + (diff * px_per_min)+'px';
	}
	var duration = timeToMins(e_time) - timeToMins(s_time);
	obj.style.width = (duration * px_per_min) + 'px';
	
	obj.setAttribute('emp_start_time',s_time);
	obj.setAttribute('emp_end_time',e_time);
	determineTime(obj);
	
	var lg = obj.childNodes.length;
	for(i=0; i<lg; i++) {
		if(obj.childNodes[i].nodeType == 1 && obj.childNodes[i].style.left && parseInt(obj.childNodes[i].style.left,10) != 0) {
			obj.childNodes[i].style.left = obj.offsetWidth - obj.childNodes[i].offsetWidth  + 'px';
		}
	}
}

function makeDoubleClickable(item) {
if(!item) { return false; }
	if(emp_id = item.getAttribute('empid')) {
		item.onmouseover = function(ev) {
			this.style.backgroundColor = '#FFFFCD';
			this.style.opacity = '0.75'; //FF & Safari
			this.style['filter'] = 'alpha(opacity=75)'; //IE
		}
		item.onmouseout = function(ev) {
			this.style.backgroundColor = '';
			this.style.opacity = '1'; //FF & Safari
			this.style['filter'] = 'alpha(opacity=100)'; //IE
		}
/*		item.ondblclick = function(ev) {
		//alert(glob_mousepos_x+' '+glob_mousepos_y);
		if(!click_pause && glob_x_loc > 0) {
			var y_loc = glob_y_loc;
			var lg = id_array.length;
			for(i=0; i<lg; i++) {
				if(tmp_obj = assoc_array[id_array[i]][0]) {
					tmp_top = parseInt(tmp_obj.style.top,10);
					tmp_bot = tmp_top + tmp_obj.offsetHeight;
					if(y_loc >= tmp_top && y_loc <= tmp_bot) {
						emp_id = id_array[i];
						var initial_time = mouseTime(glob_mousepos_x);
						var pm_selected = 0;
						var end_pm_selected = 0;
						
						if(initial_time.h >= 12) {
							pm_selected = 1;
							end_pm_selected = 1;
							end_hr_var = initial_time.h - 11;
							if(initial_time.h > 12) {
								initial_time.h -= 12;
							}
						}
						else {
							end_hr_var = initial_time.h + 1;
							if(end_hr_var == 12) {
								end_pm_selected = 1;
							}
						}
						
						if(initial_time.h < 10) {
							var tmp_pre = '0';
						}
						else {
							var tmp_pre = '';
						}
						if(initial_time.h < 9) {
							var tmp_pretwo = '0';
						}
						else {
							var tmp_pretwo = '';
						}
						if(initial_time.m < 10) {
							var tmp_pre_min = '0';
						}
						else {
							var tmp_pre_min = '';
						}
						popup.closeWindow();
						emp_type = tmp_obj.getAttribute('emptype');
						filler_html = document.getElementById('emp_container_'+emp_id+'_leftinfo').innerHTML;
	
						dia_store = '<div class="blank-dialog"><select id="classSel">';
						if(!employees[emp_id]) {
							alert('This employee does not have any positions listed for him/her.  Go to the People page and specify all positions for all employees in order to assign them to work hours.');
						}
						else{
							var lg = employees[emp_id].length;
							for(i=0; i<lg; i++) {
								if(employees[emp_id][i][2]) {
									wage_str = '  '+employees[emp_id][i][2];
								}
								else {
									wage_str = '';
								}

								if(employees[emp_id][i][1]) {
									if(employees[emp_id][i][0] == emp_type) {
										dia_store += '<option value="'+employees[emp_id][i][0]+'" selected>'+employees[emp_id][i][1]+wage_str+'</option>';
									}
									else {
										dia_store += '<option value="'+employees[emp_id][i][0]+'">'+employees[emp_id][i][1]+wage_str+'</option>';
									}
	
								}
								else {
									if(employees[emp_id][i][0] == emp_type) {
										dia_store += '<option value="'+employees[emp_id][i][0]+'" selected>'+employees[emp_id][i][0]+wage_str+'</option>';
									}
									else {
										dia_store += '<option value="'+employees[emp_id][i][0]+'">'+employees[emp_id][i][0]+wage_str+'</option>';
									}
								}
							}
							dia_store += '</select><BR /><BR />';
							dia_store += '<div class="d-left">Start:</div><input type="text" value="'+ tmp_pre +initial_time.h+'" id="start_hr" size="2" maxlength=2 onblur="updateDraggableTime(\'start\');" /> : <input type="text" value="'+ tmp_pre_min +initial_time.m+'" id="start_min" size="2" maxlength=2 onblur="updateDraggableTime(\'start\');" /><select id="start_ampm"><option value="0">am</option><option value="12">pm</option></select><BR/><div class="d-left">End:</div><input type="text" value="'+ tmp_pretwo +(end_hr_var)+'" id="end_hr" size="2" maxlength=2 onblur="updateDraggableTime(\'end\');"/> : <input type="text" value="'+ tmp_pre_min +initial_time.m+'" id="end_min" size="2" maxlength=2 onblur="updateDraggableTime(\'end\');"/><select id="end_ampm"><option value="0">am</option><option value="12">pm</option></select><BR/><BR/><input type="hidden" id="start_storage" /><input type="hidden" id="end_storage" />';
							dia_store += '<div class="dialog-buttons"><input type="button" class="save_button" value=" " onclick="/*updateDraggableTime(\'start\'); updateDraggableTime(\'end\'); createShift('+emp_id+','+glob_mousepos_x+',\''+tmp_obj.id+'\',\'dialogWindow'+popup.printCount()+'\');" /> <input type="button" class="cancel_button" value=" " onclick="document.getElementById(\'dialogWindow'+popup.printCount()+'\').style.display=\'none\';" /></div></div>';
							popup.blankDialog(filler_html+dia_store, 'Create Shift');
							cc_mouse_in = glob_mousepos_x - document.getElementById('tcontent').offsetLeft - 120; //120 = width of shift_names_head
							popup.changeCoords(cc_mouse_in, glob_y_loc);
							if(pm_selected) {
								document.getElementById('start_ampm').value = 12;
							}
							if(end_pm_selected) {
								document.getElementById('end_ampm').value = 12;
							}
							click_pause = 0;
							return false;
						}//end error check else statement
					}//end y_loc check if statement
				}//end tmp_obj if statement
			}//end for loop
		click_pause = 0;
		}//end if click_pause
	}//end dblclick function */
	}
}

//		maxObj.setAttribute('smins', timeToMins(tmp_mins));
//	 parseInt(maxObj.getAttribute('smins'),10);

function displayType(cbox,lim_obj) { //use maxObj here instead of lim_obj!!!
	if(!lim_obj) {
		lim_obj = maxObj;
	}
	var lg = lim_obj.childNodes.length;
	var emp_type = cbox.value;

	if(!cbox.checked) {
		for(i=0; i<lg; i++) {
			tmp_obj = lim_obj.childNodes[i];
			if(tmp_obj.nodeType == 1 && tmp_obj.getAttribute('emptype') == emp_type) {
				tmp_obj.style.display = 'none';
				//code here for unchecking box/unsetting value that signifies this person is working?
			}
			
		}
		lg = maxObj.childNodes.length;
		for(i=0; i<lg; i++) {
			tmp_obj = maxObj.childNodes[i];
			if(tmp_obj.nodeType == 1 && tmp_obj.getAttribute('emptype') == emp_type) {
				tmp_obj.style.display = 'none';
			}
		}
	}
	else {
		for(i=0; i<lg; i++) {
			tmp_obj = lim_obj.childNodes[i];
			
			if(tmp_obj.nodeType == 1 && tmp_obj.getAttribute('emptype') == emp_type && !tmp_obj.getAttribute('delete')) {
				tmp_obj.style.display = '';
				//code here for unchecking box/unsetting value that signifies this person is working?
			}
		}
		lg = maxObj.childNodes.length;
		for(i=0; i<lg; i++) {
			tmp_obj = maxObj.childNodes[i];
			if(tmp_obj.nodeType == 1 && tmp_obj.getAttribute('emptype') == emp_type && !tmp_obj.getAttribute('delete')) {
				tmp_obj.style.display = '';
			}
		}
	}
}


function addToEmpArray(id, value, name, wage) {
	if(!wage) {
		wage = 0;
	}
	if(!employees[id]) {
		employees[id] = new Array();
	}

	if(!name) { 
		name = false;
		employees[id].push(value);
	}
	else {
		employees[id].push([value, name, wage]);
	}
}

function associateObjects() {
	assoc_array = new Array();
	id_array = new Array();
	delete_array = new Array();
	var lg = maxObj.childNodes.length;
	for(i=0; i<lg; i++) {
		tmp_obj = maxObj.childNodes[i];
		if(tmp_obj.nodeType == 1 && tmp_obj.offsetHeight) {
			emp_id = parseInt(tmp_obj.getAttribute('empid'),10);
			if(!assoc_array[emp_id]) {
				assoc_array[emp_id] = new Array();
				assoc_array[emp_id].push(tmp_obj);
				id_array.push(emp_id);
			}
			else {
				assoc_array[emp_id].push(tmp_obj);
			}
		}
		else if(tmp_obj.nodeType == 1 && tmp_obj.getAttribute('delete')) { //create array of objs that need to be deleted
			emp_id = parseInt(tmp_obj.getAttribute('empid'),10);
			if(!delete_array[emp_id]) {
				delete_array[emp_id] = new Array();
				delete_array[emp_id].push(tmp_obj);
			}
			else {
				delete_array[emp_id].push(tmp_obj);
			}
		}
	}
}


//there may be a way to combine move above and move below, but they work for now.
function moveBelow(obj) {
	var lg = id_array.length;
	var obj_top = parseInt(obj.style.top,10);
	var obj_bottom = obj_top + obj.offsetHeight;

	var clean = 0;
	for(i=0; i<lg; i++) {
		if(tmp_obj = assoc_array[id_array[i]][0]) {
		
			tmp_top = parseInt(tmp_obj.style.top,10);
			tmp_mid = tmp_top + (tmp_obj.offsetHeight/2);
			tmp_bottom = tmp_top + tmp_obj.offsetHeight;
			
			if((obj_top < tmp_mid) && (obj_top > tmp_top)) {
			
				//final_top = tmp_top + tmp_obj.offsetHeight + 'px';
				final_top = original_y + 'px';
				document.getElementById(container_str+id_array[i]).style.top = tmp_obj.style.top = final_top;
				original_y = tmp_top;//obj_top;
				tmp_lg = assoc_array[id_array[i]].length;
				for(j=0; j<lg; j++) {
					if(tmper_obj = assoc_array[id_array[i]][j]) {
						tmper_obj.style.top = final_top;
					}
				}
				clean = 1;
			}
		}
	}
	if(clean) {
		//cleanUp(obj,1);
	}
}

function moveAbove(obj) {
	var lg = id_array.length;
	var obj_top = parseInt(obj.style.top,10);
	var obj_bottom = obj_top + obj.offsetHeight;

	var clean = 0;
	for(i=0; i<lg; i++) {
		if(tmp_obj = assoc_array[id_array[i]][0]) {
		
			tmp_top = parseInt(tmp_obj.style.top,10);
			tmp_mid = tmp_top + (tmp_obj.offsetHeight/2);
			tmp_bottom = tmp_top + tmp_obj.offsetHeight;

			if((obj_bottom > tmp_mid) && (obj_bottom < tmp_bottom)) {
				
				//final_top = tmp_top + tmp_obj.offsetHeight + 'px';
				final_top = original_y + 'px';
				document.getElementById(container_str+id_array[i]).style.top = tmp_obj.style.top = final_top;
				original_y = tmp_top;//obj_top;
				tmp_lg = assoc_array[id_array[i]].length;
				for(j=0; j<lg; j++) {
					if(tmper_obj = assoc_array[id_array[i]][j]) {
						tmper_obj.style.top = final_top;
					}
				}
				clean = 1;
			}
		}
	}
	if(clean) {
		//cleanUp(obj,1);
	}
}

function classifyEmp(emptype, obj) {
	obj.className = emp_class+emptype;
	obj.setAttribute('emptype', emptype);
}

function classifyEmpById(emptype, id) {
	obj = document.getElementById(id);
	obj.className = emp_class+emptype;
	obj.setAttribute('emptype', emptype);
}

/* Begin excessively sloppy clean up functions (built to prevent speed dragging bug)*/
function cleanUp(obj,opt) {

	var lg = id_array.length;
	var tmp_arr = new Array();
	var rpt_arr = new Array();
	//var tmp = getPosition(maxObj);
	var tmp = max_coords;

	var min_top = tmp.y+1;
	
	for(i=0; i<lg; i++) {
		if(tmp_obj = assoc_array[id_array[i]][0]) {
			
			tmp_top = parseInt(tmp_obj.style.top,10);
			if(!tmp_arr[tmp_top]) {
				tmp_arr[tmp_top] = id_array[i];
				if(!min_top || (tmp_top < min_top)) {
					min_top = tmp_top;
				}
			}
			else {
				rpt_arr.push([tmp_top,id_array[i]]);

			}
		}
			//document.getElementById('STATBOX').innerHTML = tmp.y+' - '+min_top;
	}
	if(rpt_arr.length/* && opt == 1*/) {

		followDown(tmp_arr, rpt_arr, min_top);
	}
	/*if(rpt_arr.length && opt == 2) {
		followUp(tmp_arr, rpt_arr);
	}*/
}

function followDown(tmp_arr, rpt_arr, min_top) {
	var lg = rpt_arr.length;
	var tmp_str = '';
	var tmp_ct = 0;

	for(i=0; i<lg; i++) {

		tmp_obj = assoc_array[rpt_arr[i][1]][0];
		tmp_top = rpt_arr[i][0];

		while(tmp_arr[tmp_top]) {
			if(!tmp_ct) {
				tmp_top = min_top;
				tmp_ct++;
			}
			else {
				tmp_top += tmp_obj.offsetHeight;
			}
		}
		tmp_ct = 0;
		tmp_obj.style.top = tmp_top + 'px';
		
		tmp_lg = assoc_array[rpt_arr[i][1]].length;
		for(j=0; j<tmp_lg; j++) {
			if(tmp_obj_sib = assoc_array[rpt_arr[i][1]][j]) {
				tmp_obj_sib.style.top = tmp_top + 'px';
				document.getElementById(container_str+rpt_arr[i][1]).style.top = tmp_top + 'px';
			}
		}
	}
}

/*
function followUp(tmp_arr, rpt_arr) {
	var lg = rpt_arr.length;
	var tmp_str = '';
	for(i=0; i<lg; i++) {
		tmp_obj = rpt_arr[i][1];
		tmp_top = rpt_arr[i][0];
		
		while(tmp_arr[tmp_top]) {
			tmp_top -= tmp_obj.offsetHeight;
		}
		tmp_obj.style.top = tmp_top + 'px';
		tmp_str = tmp_obj.getAttribute('empid');
		document.getElementById(container_str+tmp_str).style.top = tmp_top + 'px';
	}
}
End excessively sloppy clean up functions*/

function saveSchedule(publish) {
	if(!publish) {
		publish = 0;
	}
	//loop thru each employee, if start time, add ':00' to end of start and end, add to form, submit form
	var emp_obj = null;
	var ct = 0;
	var lg = id_array.length;
	for(i=0; i<lg; i++) {
		tmp_lg = assoc_array[id_array[i]].length;
		for(j=0; j<tmp_lg; j++) {
			if(assoc_array[id_array[i]][j].getAttribute('emp_start_time') && assoc_array[id_array[i]][j].className != 'still') {
				//empid="2" emptype="2" emp_end_time="05:00" emp_start_time="03:00"
				emp_obj = assoc_array[id_array[i]][j];
				document.submitemps.innerHTML += '<input type="hidden" name="employee['+id_array[i]+']['+ct+'][t_start]" value="'+emp_obj.getAttribute('emp_start_time')+':00" /><input type="hidden" name="employee['+id_array[i]+']['+ct+'][type]" value="'+emp_obj.getAttribute('emptype')+'" /><input type="hidden" name="employee['+id_array[i]+']['+ct+'][t_end]" value="'+emp_obj.getAttribute('emp_end_time')+':00" />';				
				
				if(tmp_attrib = assoc_array[id_array[i]][j].getAttribute('hours_entry')) { //used for updating entry
					document.submitemps.innerHTML += '<input type="hidden" name="employee['+id_array[i]+']['+ct+'][hrs_entry]" value="'+tmp_attrib+'" />';
					if(tmp_attrib = assoc_array[id_array[i]][j].getAttribute('delete')) { //used for "deleting" entry w/ update
						document.submitemps.innerHTML += '<input type="hidden" name="employee['+id_array[i]+']['+ct+'][delete]" value="'+tmp_attrib+'" />';
					}
				}
				ct++;
			}
		}
		
		if(delete_array[id_array[i]]) {
			tmp_lg = delete_array[id_array[i]].length;
			for(j=0; j<tmp_lg; j++) {
				if(tmp_attrib = delete_array[id_array[i]][j].getAttribute('delete') && delete_array[id_array[i]][j].className != 'still') {
					emp_obj = delete_array[id_array[i]][j];
					document.submitemps.innerHTML += '<input type="hidden" name="employee['+id_array[i]+']['+ct+'][delete]" value="'+tmp_attrib+'" />';
					if(tmp_attrib = delete_array[id_array[i]][j].getAttribute('hours_entry')) { //used for updating entry
						document.submitemps.innerHTML += '<input type="hidden" name="employee['+id_array[i]+']['+ct+'][hrs_entry]" value="'+tmp_attrib+'" />';
					}
					ct++;
				}
			}
		}
		ct = 0;
	}
	if(publish) {
		document.submitemps.innerHTML += '<input type="hidden" name="publish" value="'+publish+'" />';
	}
	//alert(document.submitemps.innerHTML);
	document.submitemps.submit();
}

function quickAdd(emp_id, img) {
	var add = 1;
	var lg = maxObj.childNodes.length;

	for(var i=0; i<lg; i++) {
		tmp_obj = maxObj.childNodes[i];
		if(tmp_obj.nodeType == 1 && tmp_obj.getAttribute('empid') == emp_id && tmp_obj.id) {
			deleteObj(tmp_obj.id);
			add = 0;
		}
	}
	if(add) {
		img.src = '/lifefyle/new_images/minus.gif';
		createBlankShift(emp_id, time_start, time_end);
		
	}
	else {
		img.src = '/lifefyle/new_images/plus.gif';
	}

}

function createBlankShift(emp_id, s_time, e_time) {
	setBaseMins();
	
	obj_id = 'new_shift_'+emp_id;
	var ct = 0;
	while(document.getElementById(obj_id+'_'+ct)) {
		ct++;
	}
	var pos = max_coords;
	
	var c_val = employees[emp_id][0][0];
	
	var start_time = timeToMins(s_time);
	var end_time = timeToMins(e_time);
	
	var duration = end_time - start_time;
	
	if(duration <= 0) {
		alert('Invalid times: End time must come after start time.');
		return false;
	}
		
	var top_val = document.getElementById(container_str+emp_id).style.top;
	
	var tmp_left = ((start_time * px_per_min)) - base_mins;

	maxObj.innerHTML += '<div style="height:'+div_height+'px; width:'+ (duration * px_per_min) +'px; position:absolute; top:'+top_val+'; left:'+tmp_left+'px;" id="'+obj_id +'_'+ ct+'" class="'+emp_class + c_val+'" emp_start_time="'+s_time+'" emp_end_time="'+e_time+'" emptype="'+c_val+'" emp_mins="'+duration+'" empid="'+emp_id+'"><div id="'+obj_id +'_'+ ct+'_eresize" class="right_grab"></div><div id="'+obj_id +'_'+ ct+'_wresize" class="left_grab"></div>&nbsp;</div>';
	makeAllDraggable();
	updateHours(emp_id);

}

function setBaseMins() {
	if(base_mins == null) {
		if(!maxObj.getAttribute('smins')) {
			var tmp_mins = maxObj.getAttribute('stime');
			maxObj.setAttribute('smins', timeToMins(tmp_mins));
		}

		base_mins = parseInt(maxObj.getAttribute('smins'),10);
	}
}

function fullShiftShuffle(cb) {
	if(cb.checked) {
		document.getElementById('popup_times').style.display = 'none';	
	}
	else {
		document.getElementById('popup_times').style.display = '';
	}
}

/*
 * Begin Popup object - modified from LifeFyle (no rounded corners, added blank dialog, added clearing to closeWindow)
 * Popup dialog - if you want a basic, text only dialog with an okay button, use Popup.dialog('My Dialog Message Here')
 * basic text-only dialogs also support HTML, but you should probably store the string in a variable first.
 * if you want the popup dialog's contents to be ajax output, use Popup.dialog('ajax','/my/url.html','myajaxparameters=here')
 * parameters are optional.
 *
 * if you load an image with ajax, wrap it in a div of a fixed width and specify the dWidth when you create your new popup object 
 * to prevent inconsistencies in IE6.
 * 
 
 
 	BLANK DIALOG - TAKE AN EXTRA ARGUMENT, IF THAT ARGUMENT IS TRUE=>
 		ADD APPROPRIATE HTML TO THE BODY OF THE DOCUMENT AND DISPLAY THE NEW POPUP WINDOW (TO FIX Z-INDEX ISSUES) AND MAKE THIS POPUP THE OBJECT REFERRED TO BY POPUP.  OR INSTEAD OF ADDING THE HTML JUST CREATE A NEW OBJECT (obj = new Object('div');) or something.
 
 
 
 */
function Popup(optWidth) {
	if(!optWidth) {
		optWidth=0;
		dWidth=optWidth;
	}
	else {
		dWidth=optWidth + 14;
	}

	document.onkeydown = function(evt) {
	
 		if (document.all)	{ 
 			evt = window.event;
 			keypressed = evt.keyCode; 
 			if(evt.keyCode == 27) {
				popup.closeWindow();
			}
 			
 		}
 		else {
 			if(evt.which == 27) {
				popup.closeWindow();
			}
 		}

	}

	this.initDialogHTML = function() {
		diagCt = 0;
		while(document.getElementById('dialogWindow'+diagCt)){
			diagCt++;
		}
		

		var dialoghtml = '<div id="dialogWindow'+diagCt+'" class="mainDialogWindow" style="display:none;"><div class="dialog-close" onclick="document.getElementById(\'dialogWindow'+diagCt+'\').style.display=\'none\'; if(click_pause){click_pause = 0;}" >X</div><table width="100%" border=0 cellpadding=0 cellspacing=0><tr><td colspan="5" class="dia-table-vert dia-table-light">&nbsp;</td></tr><tr><td class="dia-table-dot dia-table-light" width="1" height="1"><div style="width:1px;height:1px;"></div></td><td colspan="3" class="dia-table-vert dia-table-dark">&nbsp;</td><td class="dia-table-dot dia-table-light" width="1" height="1"><div style="width:1px;height:1px;"></div></td></tr><tr><td class="dia-table-side dia-table-light" width="1" rowspan="2"><div style="width:1px;"></div></td><td class="dia-table-side dia-table-dark" width="1"><div style="width:1px;"></div></td><td class="" width="100%"><div class="dialog-content-body"><div class="popup-top" id="popup-top'+diagCt+'">&nbsp;</div><div id="dialogContent'+diagCt+'" class="dialog-content"></div></div></td><td class="dia-table-side dia-table-dark" width="1"><div style="width:1px;"></div></td><td class="dia-table-side dia-table-light" width="1" rowspan="2"><div style="width:1px;"></div></td></tr><tr><td colspan="3" class="dia-table-vert dia-table-dark">&nbsp;</td></tr><tr><td colspan="5" class="dia-table-vert dia-table-light">&nbsp;</td></tr></table></div>';

		document.getElementById('popup-bg-2').innerHTML += dialoghtml;
	}


	this.dialog = function(message, ajaxurl, ajaxparams) {
		dContent.innerHTML = ''; //clear content
		var IE = navigator.userAgent.search('MSIE');

		if(message.search(/^ajax$/i) != 0) {
			var ajaxparams= 0;
			var ajaxurl = 0;
			dContent.innerHTML= '<div class="dialog-message">'+message+'</div><div class="dialog-okay"><input type="button" class="ok_button" value=" " onclick="document.getElementById(\'dialogWindow'+diagCt+'\').style.display=\'none\'" /></div>';
			dialogWrapper.style.display='';
		
			if(IE != -1){
				dContent.style.cssFloat='none';
				dialogWrapper.style.width=dContent.offsetWidth+dialogPadding+'px';
			}
			dialogWrapper.style.left=(dialogPageBody-((dContent.offsetWidth+dialogPadding)/2))+'px'; //testp.x+
	 		var dTop = getScrollXY();
		 	dialogWrapper.style.top=dTop[1]+0+'px';
		}
		else {
			if(!ajaxparams) {
				ajaxparams = 0;
			}
		
			var ajaxCall = new Ajax.Request(ajaxurl, {asynchronous: true, parameters: ajaxparams,
			onComplete: function(request){ dContent.innerHTML=request.responseText; dialogWrapper.style.display=''; dLeft=dContent.offsetWidth; if(IE != -1) {dContent.style.cssFloat='none';dialogWrapper.style.width=dLeft+dialogPadding+'px';} } } );
		 	
		 	if(dWidth) {
	 			dLeft=dWidth;
	 		}
		 	dialogWrapper.style.left=(dialogPageBody-((dLeft+dialogPadding)/2))+'px';//testp.x+
		 	var dTop = getScrollXY();
	 		dialogWrapper.style.top=dTop[1]+0+'px';

		 }	
	}

	this.blankDialog = function(html, title, top) {
		//top is optional - if set to 1, dialog will appear in uniform area on screen, if set to zero
		//or ignored, dialog will need to be placed by you (possibly by using this.changeCoords)
		if(!top) { top = 0; }
		dContent.innerHTML = ''; //clear content
		var IE = navigator.userAgent.search('MSIE');
		if(!title) {
			title = false; 
		}
		else {
			dContent.innerHTML += '<div class="dialog-title">'+title+'</div>';
		}
		dContent.innerHTML += html;

		dialogWrapper.style.display='';

		var ff_gap = 5;

		if(dWidth) {
			dialogWrapper.style.width = dWidth+25+'px';
		}

		if(IE != -1){
			dContent.style.cssFloat='none';
			//dialogWrapper.style.width=dContent.offsetWidth+dialogPadding+'px';
			ff_gap = 0;
			
		}
		//dialogWrapper.style.left=(dialogPageBody-((dContent.offsetWidth+dialogPadding)/2))+'px';
		
		dialogWrapper.style.left=((document.documentElement.offsetWidth/2)-((dContent.offsetWidth+dialogPadding)/2))+'px';
		
		//if(top) {
			var dTop = getScrollXY();
	 		//dialogWrapper.style.top=dTop[1]+0+'px';
	 		dialogWrapper.style.top=dTop[1]+100+'px';

	 	
	 	//}
	 	
	 	var bg_obj = document.getElementById('popup-bg');
	 	bg_obj.style.display = '';
	 	bg_obj.style.height = document.documentElement.scrollHeight + 'px';
	}
	
	this.blankDialogNoBg = function(html, title, top) {
		if(!top) { top = 0; }
		dContent.innerHTML = ''; //clear content
		var IE = navigator.userAgent.search('MSIE');
		if(!title) {
			title = false; 
		}
		else {
			dContent.innerHTML += '<div class="dialog-title">'+title+'</div>';
		}
		dContent.innerHTML += html;

		dialogWrapper.style.display='';

		var ff_gap = 5;

		if(dWidth) {
			dialogWrapper.style.width = dWidth+25+'px';
		}

		if(IE != -1){
			dContent.style.cssFloat='none';
			//dialogWrapper.style.width=dContent.offsetWidth+dialogPadding+'px';
			ff_gap = 0;
			
		}
		//dialogWrapper.style.left=(dialogPageBody-((dContent.offsetWidth+dialogPadding)/2))+'px';
		
		dialogWrapper.style.left=((document.documentElement.offsetWidth/2)-((dContent.offsetWidth+dialogPadding)/2))+'px';
		
		var dTop = getScrollXY();
	 	//dialogWrapper.style.top=dTop[1]+0+'px';
	 	dialogWrapper.style.top=dTop[1]+100+'px';
	}

	this.closeWindow = function() {
		document.getElementById('popup-bg').style.display = 'none';
		dialogWrapper.style.display='none';
		dContent.innerHTML = '';
	}
	
	this.initDialogHTML();

	dialogWrapper = document.getElementById('dialogWindow'+diagCt);
	dContent = document.getElementById('dialogContent'+diagCt);
	dialogPadding = 0;//52; //this is left and right padding+2px for borders, used when calculating width using offsetWidth
	dialogPageBody = 400;//document.body.offsetWidth/2; //this is half the page body. formerly 390 (780px)
	//testp = getPosition(document.getElementById('header'));
	dLeft=1;

	this.changeCoords = function(left_val,top_val) {
		var dTop = getScrollXY();
		top_val += dTop[1];
		if(left_val < 0) {left_val = 0;}
		if(top_val < (dialogWrapper.offsetHeight + 10)) { top_val = dialogWrapper.offsetHeight + 20;}
		dialogWrapper.style.left = left_val+ 145 +'px';
		dialogWrapper.style.top = top_val - dialogWrapper.offsetHeight - 10 +'px';
	}

	this.printCount = function() {
		return diagCt;
	}
	
	this.setWidth = function (width) {
		dWidth = width;
	}

}
//End Popup object

/* 
	Begin Gjax - adapated from "Professional Ajax" 
	Synchronus GET only
*/

function Gjax() {
	if(typeof XMLHttpRequest != 'undefined') {
		ajax = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		var vers = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0"];
		var lg = vers.length;
		for(i = 0; i < lg; i++) {
			try {
				ajax = new ActiveXObject(vers[i]);
			}
			catch(err)  { }
		}
	}
	
	this.getResponse = function(type, path, async) {
		ajax.open(type,path,async);
		ajax.send(null);
		if(ajax.status == 200) {
			return ajax.responseText;
		}
		else {
			return false;
		}
	}
		
}

//end Gjax

/* 
	Begin Gjax2 - adapated from "Professional Ajax" 
	GET requests only at the moment
*/

function Gjax2(type, path, async, eval_txt) {
	if(!async) {
		async = false;
	}
	if(!eval_txt) {
		eval_txt = false;
	}

	if(typeof XMLHttpRequest != 'undefined') {
		ajax = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		var vers = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0"];
		var lg = vers.length;
		for(i = 0; i < lg; i++) {
			try {
				ajax = new ActiveXObject(vers[i]);
			}
			catch(err)  { }
		}
	}
	
	responseText = null;
	probing = false;
	this.onComplete = function() { };
	
	if(!async) {
		ajax.open(type,path,async);
		ajax.send(null);
		if(ajax.status == 200) {
			responseText = ajax.responseText;
		}
		else {
			responseText = 'Error';
		}
	}
	
	if(async) {
		ajax.open(type,path,async);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					responseText = ajax.responseText;
					if(eval_txt) {
						eval(eval_txt);
					}
				}
				else {
					responseText = 'Error';
					if(eval_txt) {
						eval(eval_txt);
					}
				}
			}
		}
		ajax.send(null);
	}
	
	this.probe = function() {
		probing = true;
		if(responseText) {
			return responseText;
		}
		else {
			alert('probing...');
			setTimeout("this()",200);
		}
	}
	
/*	this.probe = function() {
		probing = true;
		if(ajax.readyState == 4) {
			if(ajax.status == 200) {
				responseText = ajax.responseText;
				return ajax.responseText;
			}
			else {
				responseText = 'Error';
				return ajax.responseText;
			}
		}
		else {
			alert('probing...');
			setTimeout("this.probe()",200);
		}
	}*/
	
	/*this.asyncReturn = function(ajax) {
		if(ajax.readyState == 4) {
			if(ajax.status == 200) {
				return ajax.responseText;
			}
		}
	}*/
	
	this.getResponse = function() {
		if(responseText) {
			return responseText;
		}
		else {
			if(!probing) {
				this.probe();
			}
			setTimeout("this.getResponse",250);
		}
	}
		
}

//end Gjax2


//Sjax Beta - formerly Gjax
function SjaxBeta(type, path, params, async) {
	type = type.toLowerCase();
	if(!params) {
		params = null;
	}
	else if(type == 'get') {
		path += '?'+params;
		params = null;
	}

	if(typeof(XMLHttpRequest) != 'undefined') {
		var ajax = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		var vers = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0"];
		for(var i = 0; i < vers.length; i++) {
			try {
				var ajax = new ActiveXObject(vers[i]);
			}
			catch(err) { }
		}
	}
	
	this.responseText = null;
	
	var sj = this;
	
	if(!async) {
		async = null;
		ajax.open(type,path,async);
		if(type == 'post') {
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		}
		ajax.send(params);
		if((ajax.status >= 200 && ajax.status < 300) || ajax.status == 304 || ajax.status == 1223) {
			this.responseText = ajax.responseText;
		}
		else {
			this.onFailure(ajax.status, ajax.statusText); //statusText not consistent/crossbrowser
		}
	}
		
	if(async) {
		ajax.open(type,path,async);
		if(type == 'post') {
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		}
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if((ajax.status >= 200 && ajax.status < 300) || ajax.status == 304 || ajax.status == 1223) {
					sj.responseText = ajax.responseText;
					try {
						sj.onComplete(ajax.responseText);
					}
					catch(err) { }
				}
				else {
					sj.onFailure(ajax.status, ajax.statusText);
				}
			}
		
		}
		ajax.send(params);
	}
	
}
//end Sjax beta


function fetchSmallShiftCal(date, id, noshow) {
	if(!noshow) {
		noshow = false;
	}
	if(!noshow) {
		toggleVis(id);
		document.getElementById(id).innerHTML = 'Loading...';
	}
	test = new SjaxBeta('post','get_shiftcal.html','date='+date,true);
	test.onComplete = function(response) {
		document.getElementById(id).innerHTML = response;
	}
}

function fetchPositions(u, id) {
	document.getElementById(id).style.display = '';
	document.getElementById(id).innerHTML = '<img src="loading.gif" />';
	/*fjax = new Gjax();
	res = fjax.getResponse('get','get_pos.html?u='+u,false);*/
	oc_text = "document.getElementById('"+id+"').innerHTML = responseText";
	fjax = new Gjax2('get','get_pos.html?u='+u,true, oc_text);
	/*fjax.onComplete = function() {
		document.getElementById(id).innerHTML = fjax.responseText;
	}*/
	//document.getElementById(id).innerHTML = fjax.probe();
	//res = fjax.getResponse();
	//document.getElementById(id).innerHTML = res;
}

function shiftReleaseWarning(description,id) {
	var html = '<div style="height:100%;">Are you sure you want to release the following shift?<BR/><BR/> '+description+' <BR/><BR/><b>Notice:</b>You will still be obligated to fulfill this shift until another employee picks up this shift AND it <b>receives manager approval</b>.<BR/><BR/></div>';
	html += '<div class="dialog-buttons"><input type="button" class="release_button" value=" " onclick="document.getElementById(\'releaseid\').value='+id+'; document.releaseform.submit();" />&nbsp;<input type="button" class="cancel_button" value=" " onclick="popup.closeWindow();" /></div>';
	
	popup.setWidth(250);
	popup.blankDialog(html,'Release Shift?',1);
}


function deleteWarning(form_in) {
	var warn = 0;
	var dur = form_in.elements.length;
	for(i=0; i<dur; i++) {
		if(form_in.elements[i].checked && (form_in.elements[i].name.search('delete') != -1)) {
			warn = 1;
		}
	}
	if(warn) {
		var html = '<div style="height:100px;">You have selected one or more employees to delete.  Are you sure you want to permanently delete these employees?<BR/><BR/><b>You cannot undo this action.</b></div>';
		html += '<div class="dialog-buttons"><input type="button" class="delete_button" value=" " onclick="document.'+form_in.name+'.submit();" />&nbsp;<input type="button" class="cancel_button" value=" " onclick="popup.closeWindow();" /></div>';
	
		popup.setWidth(250);
		popup.blankDialog(html,'Delete Employee(s)?',1);
	}
	else {
		form_in.submit();
	}
}

function delposWarning(name,id) {
	var html = '<div style="height:100px;">Are you sure you want to permanently delete '+name+' as a position?<BR/><BR/><b>You cannot undo this action.</b></div>';
	html += '<div class="dialog-buttons"><input type="button" class="delete_button" value=" " onclick="document.getElementById(\'delposid\').value='+id+'; document.delposform.submit();" />&nbsp;<input type="button" class="cancel_button" value=" " onclick="popup.closeWindow();" /></div>';
	
	popup.setWidth(250);
	popup.blankDialog(html,'Delete '+name+'?',1);
}

function delTemplateWarning(name,id) {
	var html = '<div style="height:100px;">Are you sure you want to permanently delete the "'+name+'" preset?<BR/><BR/><b>You cannot undo this action.</b></div>';
	html += '<div class="dialog-buttons"><input type="button" class="delete_button" value=" " onclick="deleteTemplate('+id+');" />&nbsp;<input type="button" class="cancel_button" value=" " onclick="popup.closeWindow();" /></div>';
	
	popup.setWidth(250);
	popup.blankDialog(html,'Delete '+name+'?',1);
}

function deleteHour(id) {
	document.deleteform.innerHTML = '<input type="hidden" name="delete_hour" value="'+id+'" />';
	document.deleteform.submit();
}
function deleteTemplate(id) {
	document.deleteform.innerHTML = '<input type="hidden" name="delete_template" value="'+id+'" />';
	document.deleteform.submit();
}

function editDate(id) {
	var dte = id.split('-');
	var obj = document.getElementById(id);
	var html = '';
	
	var ct = 0;
	var new_name = 'holiday['+ct+']';
	while(document.getElementById(new_name)) {
		if(document.getElementById(new_name+'[date]').value == id) {
			if(!head_ct) {
				html+= '<table class="dia-table"><tr><td colspan="3">Current Shifts for today:</td></tr>';
				var head_ct = 1;
			}
			html += '<tr id="tr_'+ct+'"><td>'+document.getElementById(new_name+'[holiday]').value+'</td>';
			if(document.getElementById(new_name+'[s_time]')) {
				html += '<td>'+document.getElementById(new_name+'[s_time]').value+' - '+document.getElementById(new_name+'[e_time]').value+'</td>';
			}
			else {
				html += '<td>No working hours today</td>';
			}
			html += '<td> <a href="#" onclick="deleteDate(\''+new_name+'\',\''+id+'\'); document.getElementById(\'tr_'+ct+'\').style.display=\'none\'; return false">delete</a></td></tr>';
		}
		ct++;
		new_name = 'holiday['+ct+']';
	}
	
	if(head_ct) {
		html += '</table>';
	}
	
	html += '<BR/>Holiday Name: <input type="text" id="holiday_name" size="15" /> <BR/><BR/>What are the working hours for this holiday?<BR><label><input type="checkbox" id="full_holiday" onclick="hideHours(this);" checked="checked"/> No working hours</label><BR><div id="hourstohide" style="display:none;"> <div class="dp-left">Start:</div><input type="text" id="start_hr" size="2" maxlength=2 onblur="updateTime(\'start\');" /> : <input type="text" id="start_min" size="2" maxlength=2 onblur="updateTime(\'start\');" /><select id="start_ampm"><option value="0">am</option><option value="12">pm</option></select><BR class="floater-cleanup"/><div class="dp-left">End:</div><input type="text" id="end_hr" size="2" maxlength=2 onblur="updateTime(\'end\');" /> : <input type="text" id="end_min" size="2" maxlength=2 onblur="updateTime(\'end\');" /><select id="end_ampm"><option value="0">am</option><option value="12">pm</option></select></div><BR class="floater-cleanup"/><BR/><div class="dialog-buttons"><input type="button" class="save_button" value=" " onclick="addDate(\''+id+'\');" />&nbsp;<input type="button" class="cancel_button" value=" " onclick="popup.closeWindow();" /></div><BR/><BR/><input type="hidden" id="start_storage" /><input type="hidden" id="end_storage" />';
	
	//obj.className = 'selected_day';
	//borrowing the popup from above - make your own (outside of function) if you take above away (line 55)
	popup.setWidth(250);
	popup.blankDialog(html,'Edit Date '+dte[1]+'/'+dte[2]+'/'+dte[0],1);
}

function addDate(id) {
	error_str = '';
	if(!document.getElementById('holiday_name').value) {
		error_str += 'You must enter a holiday name\n\r';
	}
	if(!document.getElementById('full_holiday').checked && (!document.getElementById('start_hr').value || !document.getElementById('start_min').value || !document.getElementById('end_hr').value || !document.getElementById('end_min').value)) {
		error_str += 'You must enter holiday working hours or check the box if there are none.\n\r';
	}
	if(error_str) {
		alert(error_str);
		return false;
	}
	
	var str = '';
	var ct = 0;
	var new_name = 'holiday['+ct+']';
	var holiday = document.getElementById('holiday_name').value;
	
	while(document.getElementById(new_name)) {
		ct++;
		new_name = 'holiday['+ct+']';
	}

	str += '<div id="'+new_name+'"><input type="hidden" name="'+new_name+'[date]" id="'+new_name+'[date]" value="'+id+'" /><input type="hidden" name="'+new_name+'[holiday]" id="'+new_name+'[holiday]" value="'+holiday+'" />';
	
	if(!document.getElementById('full_holiday').checked) {
		updateTime('start');
		updateTime('end',1);
		//parse int, add am/pm
		//var s_time = document.getElementById('start_hr').value + ':' + document.getElementById('start_min').value;
		//var e_time = document.getElementById('end_hr').value + ':' + document.getElementById('end_min').value;
		var s_time = document.getElementById('start_storage').value;
		var e_time = document.getElementById('end_storage').value;
		str += '<input type="hidden" name="'+new_name+'[s_time]" id="'+new_name+'[s_time]" value="'+s_time+'" /><input type="hidden" name="'+new_name+'[e_time]" id="'+new_name+'[e_time]" value="'+e_time+'" />';
	}
	str += '</div>';
	
	document.holidayform.innerHTML += str;
	document.getElementById(id).className = 'modified_day';
	popup.closeWindow();

	//date, e_time, s_time, holiday
}

function deleteDate(name, old_date) {
	var obj = document.getElementById(name);
	if(id = obj.getAttribute('deletable')) {
		document.holidayform.innerHTML += '<input type="hidden" name="delete['+id+']" value="1"/>';
	}
	obj.innerHTML = '';
	obj.id = '';
	i=0;
	no_dates = 0;
	while(dte = document.getElementById('holiday['+i+'][date]')) {
		if(dte.value == old_date) {
			no_dates = 1;
		}
		i++;
	}
	if(!no_dates) {
		document.getElementById(old_date).className = '';
	}
}

function hideHours(obj) {
	if(obj.checked) {
		document.getElementById('hourstohide').style.display = 'none';
	}
	else {
		document.getElementById('hourstohide').style.display = '';	
	}
}

function addShift(dte, opt) {
	if(!opt) {
		opt = 0;
	}
	if(!dte) {
		dte = false;

		error_str = '';
		if(!document.getElementById('ns_title').value) {
			error_str += 'You must enter a title.\n\r';
		}
		if(!document.getElementById('start_hr').value || !document.getElementById('start_min').value || !document.getElementById('end_hr').value || !document.getElementById('end_min').value || (!verifySqlTimes(document.getElementById('start_storage').value, document.getElementById('end_storage').value, 1) && !document.getElementById('end_dur').value) ) {
			error_str += 'You must enter a valid start and end time.\n\r';
		}
		if(error_str) {
			alert(error_str);
			return false;
		}
		else {
			document.newshift.submit();
		}
	}
	else {
		chk_txt = '';
		if(document.getElementById('closed_'+dte)) {
			chk_txt = 'checked="checked"';
		}
		bo_chk_txt = '';
		if(document.getElementById('bo_'+dte)) { ///////dont forget!!!!
			bo_chk_txt = 'checked="checked"';
		}		

		if(!opt) {
			html = '<span style="font-weight:bold; cursor:pointer;" onclick="toggleVis(\'ns_div\'); document.getElementById(\'close_div\').style.display=\'none\'; document.getElementById(\'nso_img\').style.display=\'none\'; document.getElementById(\'nsc_img\').style.display=\'\'; toggleVis(\'dcc_img\'); toggleVis(\'dco_img\');">Add New Shift</span>&nbsp;<img id="dco_img" src="/lifefyle/images/arrow_opened.gif" style="display:none;" /><img id="dcc_img" src="/lifefyle/images/arrow_closed.gif" /><BR/><div id="ns_div" style="display:none;"><form method="post" name="newshift">Title: <input type="text" name="title" id="ns_title" size="15" /> <BR/><BR/> <div class="dp-left">Start:</div><input type="text" id="start_hr" size="2" maxlength=2 onblur="updateTime(\'start\');" /> : <input type="text" id="start_min" size="2" maxlength=2 onblur="updateTime(\'start\');" /><select id="start_ampm" onchange="updateTime(\'start\');"><option value="0">am</option><option value="12">pm</option></select><BR class="floater-cleanup"/><div class="dp-left">End:</div><input type="text" id="end_hr" size="2" maxlength=2 onblur="updateTime(\'end\');" /> : <input type="text" id="end_min" size="2" maxlength=2 onblur="updateTime(\'end\');" /><select id="end_ampm" onchange="updateTime(\'end\');"><option value="0">am</option><option value="12">pm</option></select>&nbsp;<select name="end_duration" id="end_dur" onchange="if(this.value==2) { document.getElementById(\'hide_duration\').style.display=\'\'; } else { document.getElementById(\'hide_duration\').style.display=\'none\'; }"><option value="0">Same Day</option><option value="1">Day After</option><option value="2">Custom</option></select><div style="display:none;width:100%;text-align:right;" id="hide_duration"><input type="text" maxlength="3" style="width:30px;" name="end_duration_custom" value="0" /> DAYS LATER</div><BR class="floater-cleanup"/><BR/><BR/><div class="dialog-buttons"><input type="button" class="save_button" value=" " onclick="addShift();" /></div><input type="hidden" name="time_start" id="start_storage" /><input type="hidden" name="time_end" id="end_storage" /><input type="hidden" name="date_start" value="'+dte+'" /></form></div><BR/>';
			//&nbsp;<input type="button" class="cancel_button" value=" " onclick="document.getElementById(\'dialogWindow'+popup.printCount()+'\').style.display=\'none\';" />
		}
		else {
			html = '<div id="ns_div"></div><div id="dco_img"></div><div id="dcc_img"></div>';
		}
		
		html += '<span style="font-weight:bold; cursor:pointer;" onclick="toggleVis(\'close_div\'); document.getElementById(\'ns_div\').style.display=\'none\'; document.getElementById(\'dco_img\').style.display=\'none\'; document.getElementById(\'dcc_img\').style.display=\'\'; toggleVis(\'nsc_img\'); toggleVis(\'nso_img\');">Closed/Blacked Out Today</span>&nbsp;<img id="nso_img" src="/lifefyle/images/arrow_opened.gif" style="display:none;" /><img id="nsc_img" src="/lifefyle/images/arrow_closed.gif" /><BR/><div id="close_div" style="display:none;"><form method="post" name="closeform"><label><input type="checkbox" name="closed_day" '+chk_txt+' />Closed today</label> <BR/> <label><input type="checkbox" name="bo_day" '+bo_chk_txt+' />Blacked Out today</label> <BR/><BR/><div class="dialog-buttons"><input type="submit" class="save_button" value=" " /></div><input type="hidden" name="date_start" value="'+dte+'" /><input type="hidden" name="closedayhidden" value="1" /></form></div><BR/><BR/><div class="dialog-buttons"><input type="button" class="cancel_button" value=" " onclick="popup.closeWindow();" /></div>';
		
		//&nbsp;<input type="button" class="cancel_button" value=" " onclick="document.getElementById(\'dialogWindow'+popup.printCount()+'\').style.display=\'none\';" />
		
		popup.setWidth(310);
		dte = dte.split('-');
		popup.blankDialog(html,'New Shift '+dte[1]+'/'+dte[2]+'/'+dte[0],1);
	}

}

/**************Availability**********************/


function editAvail(id) {
	var dte = id.split('-');
	var obj = document.getElementById(id);
	
	if(h_day = document.getElementById('pub_holiday_'+id)) {
		var html = '<b>'+h_day.value+'</b><BR/><BR/>';
	}
	else {
		var html = '';
	}
	
	var ct = 0;
	var new_name = 'holiday['+ct+']';
	while(document.getElementById(new_name)) {
		if(document.getElementById(new_name+'[date]') && document.getElementById(new_name+'[date]').value == id) {
			if(!head_ct) {
				html+= '<table class="dia-table" width="100%"><tr><td colspan="3">Listed availability for today:</td></tr>';
				var head_ct = 1;
			}
			html += '<tr id="tr_'+ct+'"><td width="35%">'+document.getElementById(new_name+'[title]').value+'</td>';
			if(document.getElementById(new_name+'[s_time]')) {
				html += '<td>'+document.getElementById(new_name+'[ts_time]').value+' - '+document.getElementById(new_name+'[te_time]').value+'</td>';
			}
			else {
				html += '<td>&nbsp;</td>';
			}

			html += '<td width="10%"> <a href="#" onclick="deleteAvail(\''+new_name+'\',\''+id+'\'); document.getElementById(\'tr_'+ct+'\').style.display=\'none\'; return false">delete</a></td></tr>';
		}
		ct++;
		new_name = 'holiday['+ct+']';
	}
	
	if(head_ct) {
		html += '</table>';
	}
	//<div class="dia-line"></div>
	html += 'I am:<BR/><label><input type="radio" value="0" name="m_e" id="not_avail" />not available</label><BR/><label><input type="radio" value="1" name="m_e" id="is_avail" />available</label><BR/><BR/>Title: <input type="text" id="avail_title" size="15" /><BR/><BR/><label><input type="checkbox" onclick="toggleVis(\'sched_times\')" id="all_day_cb" value="1" />All Day</label> <BR/><BR/><div id="sched_times"><div class="dp-left">Start:</div><input type="text" id="start_hr" size="2" maxlength=2 onblur="updateTime(\'start\');" /> : <input type="text" id="start_min" size="2" maxlength=2 onblur="updateTime(\'start\');" /><select id="start_ampm"><option value="0">am</option><option value="12">pm</option></select><BR class="floater-cleanup"/><div class="dp-left">End:</div><input type="text" id="end_hr" size="2" maxlength=2 onblur="updateTime(\'end\');" /> : <input type="text" id="end_min" size="2" maxlength=2 onblur="updateTime(\'end\');" /><select id="end_ampm"><option value="0">am</option><option value="12">pm</option></select></div><BR class="floater-cleanup"/><BR/><div class="dialog-buttons"><input type="button" class="save_button" value=" " onclick="addAvail(\''+id+'\');" />&nbsp;<input type="button" class="cancel_button" value=" " onclick="popup.closeWindow();" /></div><input type="hidden" id="start_storage" /><input type="hidden" id="end_storage" />';
	
	//obj.className = 'selected_day';
	//borrowing the popup from above - make your own (outside of function) if you take above away (line 55)
	popup.setWidth(300);
	popup.blankDialog(html,'Edit Date '+dte[1]+'/'+dte[2]+'/'+dte[0],1);
}

function addAvail(id) {
	error_str = '';
	if(!document.getElementById('avail_title').value) {
		error_str += 'You must enter a title\n\r';
	}
	if(!document.getElementById('not_avail').checked && !document.getElementById('is_avail').checked) {
		error_str += 'You must list your availability\n\r';
	}
	if(!document.getElementById('start_storage').value && !document.getElementById('end_storage').value && !document.getElementById('all_day_cb').checked) {
		error_str += 'You must provide a start time and an end time\n\r';
	}
	
	if(error_str) {
		alert(error_str);
		return false;
	}
	
	var str = '';
	var ct = 0;
	var new_name = 'holiday['+ct+']';
	var holiday = document.getElementById('avail_title').value;
	
	if(document.getElementById('is_avail').checked) {
		var avail_val = 1;
	}
	else if(document.getElementById('not_avail').checked) {
		var avail_val = 0;	
	}
	
	while(document.getElementById(new_name)) {
		ct++;
		new_name = 'holiday['+ct+']';
	}

	str += '<div id="'+new_name+'"><input type="hidden" name="'+new_name+'[date]" id="'+new_name+'[date]" value="'+id+'" /><input type="hidden" name="'+new_name+'[title]" id="'+new_name+'[title]" value="'+holiday+'" /><input type="hidden" name="'+new_name+'[availability]" id="'+new_name+'[availability]" value="'+avail_val+'" />';
	

	updateTime('start');
	updateTime('end');

	var s_time = document.getElementById('start_storage').value;
	var e_time = document.getElementById('end_storage').value;
	str += '<input type="hidden" name="'+new_name+'[s_time]" id="'+new_name+'[s_time]" value="'+s_time+'" /><input type="hidden" name="'+new_name+'[e_time]" id="'+new_name+'[e_time]" value="'+e_time+'" />';

	if(document.getElementById('all_day_cb').checked) {
		str += '<input type="hidden" name="'+new_name+'[sched_all_day]" id="'+new_name+'[sched_all_day]" value="1" />'
	}

	str += '</div>';
	
	document.holidayform.innerHTML += str;
	document.getElementById(id).className = 'modified_day';
	popup.closeWindow();
	document.holidayform.submit();
}

function deleteAvail(name, old_date) {
	var obj = document.getElementById(name);
	if(id = obj.getAttribute('deletable')) {
		document.holidayform.innerHTML += '<input type="hidden" name="delete['+id+']" value="1"/>';
	}
	document.holidayform.submit();
	obj.innerHTML = '';
	obj.setAttribute('deletable', 0);
	//obj.id = '';
	i=0;
	no_dates = 0;
	while(dte = document.getElementById('holiday['+i+'][date]')) {
		if(dte.value == old_date) {
			no_dates = 1;
		}
		i++;
	}
	if(!no_dates) {
		document.getElementById(old_date).className = '';
	}
}



/************************************/


function updateTime(id, error_chk) {
	if(!error_chk){ error_chk = 0; }
	tmpAMPM = parseInt(document.getElementById(id+'_ampm').value,10);
	tmpHours = parseInt(document.getElementById(id+'_hr').value,10);
	tmpMinutes = parseInt(document.getElementById(id+'_min').value,10);

	
	if(!tmpMinutes) {
		tmpMinutes = '00';	
	}
	else if(tmpMinutes < 10) {
		tmpMinutes = '0'+tmpMinutes;	
	}
	else if(tmpMinutes > 59) {
		tmpMinutes = '59';
		document.getElementById(id+'_min').value = 59;
	}
	
	upHours = tmpHours + tmpAMPM;

	if(upHours > 24) {
		upHours = 23;
		document.getElementById(id+'_hr').value = 11;
		document.getElementById(id+'_ampm').value = 12;
	}

	if(!tmpHours || upHours == 12 || upHours <= 0) { //12am needs to be 00
		upHours = '00';
	}

	if(upHours == 24) {
		upHours = 12;
	}

	document.getElementById(id+'_storage').value = upHours+':'+tmpMinutes;//+':00';

}

function updateDraggableTime(id, error_chk) {
	if(!error_chk){ error_chk = 0; }
	
/*	if(id == 'start') {
	}*/
	if(id == 'end') {
		test_end = 1;
	}
	
	tmpAMPM = parseInt(document.getElementById(id+'_ampm').value,10);
	tmpHours = parseInt(document.getElementById(id+'_hr').value,10);
	tmpMinutes = parseInt(document.getElementById(id+'_min').value,10);

	
	//time_start and time_end global vars declared in php file
	if(!time_start) {
		time_start = '0:0';
	}
	if(!time_end) {
		time_end = '0:0';
	}

	time_start_arr = time_start.split(':');
	time_end_arr = time_end.split(':');
	time_start_hr = parseInt(time_start_arr[0],10);
	time_start_min = parseInt(time_start_arr[1],10);
	time_end_hr = parseInt(time_end_arr[0],10);
	time_end_min = parseInt(time_end_arr[1],10);
	
	switch(tmpHours) {
		case 12:
			if(tmpAMPM == 0) {
				upHours = 0;
			}
			else {
				upHours = 12;
			}
			break;
		default:
			upHours = tmpHours + tmpAMPM;
	}
	


	de = date_array.length;
	if(de) {
		if(id == 'start') {
			add_day = parseInt(document.getElementById('start_date_drop').value,10);
		}
		else if(id == 'end') {
			add_day = parseInt(document.getElementById('end_date_drop').value,10);
		}
		//upHours %= 24;
		upHours += (24 * add_day);
	}
	/*if(upHours == 0) {
		upHours = 12;
	}*/
/*	if(upHours == 24) {
		upHours = 12;
	}

	if(!tmpHours || (tmpHours == 12 && tmpAMPM == 0) || upHours <=0) { //12am needs to be 00
		upHours = 0;
	}*/

	if(upHours > time_end_hr) {
		if(id == 'start') {
			upHours = time_end_hr - 1;
		}
		else {
			upHours = time_end_hr;
		}
		if(error_chk) {
			error_chk = 2;
		}
	}
	
	if(upHours == time_end_hr && tmpMinutes >= time_end_min && id == 'start') {
		tmpMinutes = time_end_min

		upHours = time_end_hr - 1;

		if(error_chk) {
			error_chk = 2;
		}
	}
	else if(upHours == time_end_hr && tmpMinutes > time_end_min) {
		tmpMinutes = time_end_min
		if(error_chk) {
			error_chk = 2;
		}
	}
	
	if(upHours < time_start_hr) {
		if(id == 'end') {
			upHours = time_start_hr + 1;
		}
		else {
			upHours = time_start_hr;
		}
		if(error_chk) {
			error_chk = 2;
		}
	}

	if(upHours == time_start_hr && tmpMinutes < time_start_min) {
		if(id == 'end') {
			upHours = time_start_hr + 1;
		}
		tmpMinutes = time_start_min;
		if(error_chk) {
			error_chk = 2;
		}
	}
	else if(upHours == time_start_hr && tmpMinutes <= time_start_min && id == 'end') {
		upHours = time_start_hr + 1;
		tmpMinutes = time_start_min;
		if(error_chk) {
			error_chk = 2;
		}
	}
	
/*	if(upHours > 24) {
		upHours = 23;
		document.getElementById(id+'_hr').value = 11;
		document.getElementById(id+'_ampm').value = 12;
	}*/

	if(upHours == 0) {
		upHours = '00';
	}

	if(!tmpMinutes) {
		tmpMinutes = '00';	
	}
	else if(tmpMinutes < 10) {
		tmpMinutes = '0'+tmpMinutes;	
	}
	else if(tmpMinutes > 59) {
		tmpMinutes = '59';
		document.getElementById(id+'_min').value = 59;
	}
		
	document.getElementById(id+'_storage').value = upHours+':'+tmpMinutes;//+':00';
	if(error_chk == 2) {
		alert('The time you entered did not fit inside the shift.  It has been trimmed to fit.');
		return false;
	}
	return true;
}

function toggleVis(id) {
	var obj = document.getElementById(id);
	if(obj.style.display == 'none') {
		obj.style.display = '';
	}
	else {
		obj.style.display = 'none';	
	}
}


function deSqlDate(date_in) {
	var tmp_arr = date_in.split('-');
	return tmp_arr[1]+'/'+tmp_arr[2];
}

function verifySqlTimes(start, end, no_err) {
	if(!no_err) { no_err = false; }
	start = start.split(':');
	end = end.split(':');
		
	start_hour = parseInt(start[0]);
	end_hour = parseInt(end[0]);
	start[1] = parseInt(start[1]);
	end[1] = parseInt(end[1]);
	add_day = add_day_e = 0;
	
	if(document.getElementById('start_date_drop') && document.getElementById('end_date_drop')) {
		add_day = parseInt(document.getElementById('start_date_drop').value,10);
		add_day_e = parseInt(document.getElementById('end_date_drop').value,10);
	}
	
	start_hour += (24 * add_day);
	end_hour += (24 * add_day_e);
	
	if(start_hour > end_hour) {
		if(!no_err) {
			alert('Invalid times: End time must come after start time.');
		}
		return false;
	}
	else if(start_hour < end_hour && start[1] < 60 && end[1] >= 0) {
		return true;
	}
	else if(start[1] > end[1]) {
		if(!no_err) {
			alert('Invalid times: End time must come after start time.');
		}
		return false;
	}
	if(!no_err) {
		alert('Invalid times: End time must come after start time.');
	}
}

function formize(id, memo_id) {
	var obj = document.getElementById(id).childNodes[0];
	var p_obj = document.getElementById(id);
	
	if(!document.getElementById('storage_'+memo_id)) {
		obj.innerHTML = '<form method="post"><textarea style="margin-left:10%; margin-right:10%; width:80%; height:200px;" name="edit_body" id="eb_'+memo_id+'">'+obj.innerHTML+'</textarea><br/><div style="width:98%; text-align:right;"><input type="hidden" name="memo_id" value="'+memo_id+'" /><input type="submit" value=" " class="save_button" />&nbsp;<input type="button" class="cancel_button" value=" " onclick="formize(\''+id+'\','+memo_id+');" /></form><div style="display:none;" id="storage_'+memo_id+'">'+obj.innerHTML+'</div>';
		//p_obj.style.display = '';
	}
	else  {
		//p_obj.style.display = 'none';
		obj.innerHTML = document.getElementById('storage_'+memo_id).innerHTML;
	}
}

function weekCopy(dte, ct) {
	document.getElementById('back_link').href = back_link+'&cb='+dte;
	document.getElementById('next_link').href = next_link+'&cb='+dte;
        i=1;	
	//for(i=1; i < 4; i++) {
        while(document.getElementById('paste_'+i)){
		if(i != ct) {
			document.getElementById('paste_'+i).style.display = '';
		}
		else {
			document.getElementById('paste_'+i).style.display = 'none';
		}
                i++;
	}

}

function toggleSeqVis(cbox, obj_id) {
	ct = 0;
	if(!cbox.checked) {
		while(tmp_obj = document.getElementById(obj_id+ct)) {
			tmp_obj.style.display = 'none';
			ct++;
		}
	}
	else {
		while(tmp_obj = document.getElementById(obj_id+ct)) {
			tmp_obj.style.display = '';
			ct++;
		}
	}
}

function readMemo(id) {
	js_id = 'memo_'+id+'_';
	if(document.getElementById(js_id+'0').className == 'bold_td') {
		url = 'read_memo.html?m='+id;
		//document.getElementById('sm_if').src= url;
		fjax = new Gjax();
		res = fjax.getResponse('get',url,false);
		for(i=0; i<5; i++) {
			for_obj = document.getElementById(js_id+i);
			for_obj.className = '';
			if(for_obj.borderBottom) {
			
			}
			else {
			
			}
		}
		tmp_obj = document.getElementById('head_memos');
		tmp_obj_num = parseInt(tmp_obj.innerHTML,10);
		if(tmp_obj_num > 0) {
			tmp_obj.innerHTML = (tmp_obj_num - 1);
			tmp_files_num = parseInt(document.getElementById('head_files').innerHTML,10);
			if(tmp_obj_num == 1 && tmp_files_num == 0) {
				document.getElementById('head_nav').className = 'top_nav';
			}
		}
	}
	for(i=0; i<5; i++) {
		for_obj = document.getElementById(js_id+i);
		if(parseInt(for_obj.style.borderBottom,10) == 0) {
			for_obj.style.borderBottom = '1px solid #CCCCCC';
			if(i) {
				for_obj.style.borderLeft = '1px dashed #CCCCCC';
			}
		}
		else {
			for_obj.style.borderBottom = '0px';
			for_obj.style.borderLeft = '0px';
		}
	}
}

function passwordStr(str) {
	lower = upper = num = punct = 0;
	lg = str.length;
	if(lg > 5) {
		for(i=0; i<lg; i++) {
			c = str.charAt(i);
			if(c.search('[^a-zA-Z0-9]') != -1) {
				punct = 1;
			}
			else if(c.search('[a-z]') != -1) {
				lower = 1;
			}
			else if(c.search('[A-Z]') != -1) {
				upper = 1;
			}
			else if(c.search('[0-9]') != -1) {
				num = 1;
			}
		}
	
		score = lower + upper + num + punct + (lg / 3);
	
		if(score < 4) {
			return 'Strength: <span style="color:red;">Weak</span>';
		}
		else if(score >= 4 && score < 6) {
			return 'Strength: <span style="color:#a0a926;">Average</span>';
		}
		else {
			return 'Strength: <span style="color:green;">Strong</span>';
		}
	}
	else {
		return '<span style="color:red;">Too Short</span>';
	}
}

function validateEmail(addr) {
	if(addr.search(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/) != -1) {
		return true;
	}
	return false;
}

function checkUserName(u, id) {
	if(u) {
		lg = u.length;
		if(lg > 5) {
			oc_text = "if(parseInt(responseText,10) == 1) { document.getElementById('"+id+"').innerHTML = '<img src=\"/lifefyle/new_images/greencheck.gif\" /><span style=\"color:green; position:relative; left:4px; top:-2px;\">Available</span>'; } else { document.getElementById('"+id+"').innerHTML = '<img src=\"/lifefyle/new_images/redno.gif\" /><span style=\"color:red; position:relative; top:-2px; left:4px; \">Taken</span>'; }";
			fjax = new Gjax2('get','verify_username.html?u='+u,true, oc_text);
		}
		else {
			document.getElementById(id).innerHTML = '<span style="color:red;">Too Short</span>';
		}
	}
	else {
		document.getElementById(id).innerHTML = '';
	}
}

//heavily adapted showtime.
//orginal showtime made by: Nicolas - http://www.javascript-page.com

var timerID = null;
var timerRunning = false;
function stopclock (){
        if(timerRunning)
        	clearTimeout(timerID);
        timerRunning = false;
}

function startclock (id, nowish) {
		if(!nowish) {
			nowish = 0;
		}
        stopclock();
        showtime(id, nowish);
}

function showtime (id, nowish) {
	obj = document.getElementById(id);
	if(nowish) {
        /*var now = new Date();
        now.setTime(nowish);
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();*/
        //orig_nowish = nowish;
        //nowish /= 1000;
        var seconds = nowish%60;
        var minutes = ((nowish-seconds)%3600)/60;
        var hours = (nowish-(minutes*60)-seconds)/3600;
        
        var timeValue = "" + ((hours >12) ? hours -12 :hours);
        timeValue += ((minutes < 10) ? ":0" : ":") + minutes;
        timeValue += ((seconds < 10) ? ":0" : ":") + seconds;
        timeValue += (hours >= 12) ? " PM" : " AM";
        obj.innerHTML = timeValue;
        timerID = setTimeout("showtime('"+id+"',"+(nowish+1)+")",1000);
        timerRunning = true;
	}
	else {
		obj.innerHTML = 'Clock error.  Reload page.';
	}
}

/* NEED TO CONVERT THIS TO ASYNC!!! Use Gjax2 for time being. */
function freshTime (id) {
	oc_text = "now_in = parseInt(responseText,10); stopclock(); startclock('"+id+"', now_in);";
	
	fjax = new Gjax2('get','get_time.html',true, oc_text);
/*
	timejax = new Gjax();
	now_in = timejax.getResponse('get','get_time.html',false);
	now_in = parseInt(now_in,10);
	stopclock();
	startclock(id, now_in);
*/
}

function checkPos(var_in) {
	posjax = new Gjax();
	var_out = posjax.getResponse('get','verify_pos.html?t='+var_in,false);
	if(var_out == '0' || var_out == 0) {
		var_out = false;
	}
	return var_out;
}

function changePresetName(p_id, obj) {
	oc_text = "document.getElementById('span_"+p_id+"').innerHTML = responseText; toggleVis('text_"+p_id+"'); toggleVis('span_"+p_id+"'); toggleVis('add_"+p_id+"'); toggleVis('del_"+p_id+"')";
	
	fjax = new Gjax2('get','change_preset.html?id='+p_id+'&name='+obj.value,true, oc_text);
}

function updateOnlineStatus(skip) {
	if(update_ol_status == 1 && !skip) {
		var useless = new Gjax2('get','status_update.html',true);
		update_ol_status = 0;
		skip = false;
	}
	if(skip) {
		update_ol_status = 0;
	}
	setTimeout("updateOnlineStatus()", 150000);
}

function boxBlur(box, text) {
	if(box.value == '') {
		box.className = 'box_unsel';
		box.value = text;
	}
}

function boxFocus(box, text) {
	if(box.value == text) {
		box.className = '';
		box.value = '';
	}
}

function pwboxBlur(box) {
	if(box.value == '') {
		box.className = 'pw_box_unsel';
	}
}

function pwboxFocus(box) {
	box.className = '';
}

function unboxBlur(box) {
	if(box.value == '') {
		box.className = 'un_box_unsel';
	}
}

function unboxFocus(box) {
	box.className = '';
}

function toggleEditVis(tb, eb) {
	i = 0;
	tb_obj_id = tb+'_'+i;
	eb_obj_id = eb+'_'+i;
	tb_obj = document.getElementById(tb_obj_id);
	eb_obj = document.getElementById(eb_obj_id);
	
	while(tb_obj && eb_obj) {
		toggleVis(tb_obj_id);
		toggleVis(eb_obj_id);
		i++;
		tb_obj_id = tb+'_'+i;
		eb_obj_id = eb+'_'+i;
		tb_obj = document.getElementById(tb_obj_id);
		eb_obj = document.getElementById(eb_obj_id);
	}
}

function noSaveWarning(href_in) {
	if(unsavedchanges) {
		var html = '<div style="height:100px;">You are about to leave this page without saving the changes you\'ve made to this shift. <BR/><BR/> Are you sure you want to continue without saving?</div><div class="dialog-buttons"><input type="button" class="continue_button" value=" " onclick="location.href=\''+href_in+'\';" />&nbsp;<input type="button" class="cancel_button" value=" " onclick="popup.closeWindow();" /></div>';
	
		popup.setWidth(300);
		popup.blankDialog(html,'Continue Without Saving?',1);
	}
	else {
		location.href=href_in;
	}
}

//getScroll written by: Mark Wilton-Jones
//http://www.howtocreate.co.uk/tutorials/javascript/browserwindow

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return {x: myWidth, y:myHeight};
}
