	// slider positioning constants - must be entered here
	// must be set here, in index.php (for onload settings) and in goal_view.css
	var SLIDER_PROMPT_WIDTH = 60 // width of slider prompt (range plus null position offsets)
	var SLIDER_RANGE_WIDTH = 50; // range of slider prompt only
	var SLIDER_NULL_POSITION_OFFSET = 5; // pixel offset for null handle position (must be half difference prompt - width)
	var NUM_VALUES = 5;
	var HANDLE_HEIGHT = 12;
	var HANDLE_WIDTH = 10;
	
	var INCREMENT = (SLIDER_RANGE_WIDTH - HANDLE_WIDTH) / (NUM_VALUES - 1);
	var HALF_HANDLE_WIDTH = Math.floor(HANDLE_WIDTH / 2);
	
	var MIN_HANDLE_1_X = 0;
	var MAX_HANDLE_1_X = SLIDER_PROMPT_WIDTH - HANDLE_WIDTH - SLIDER_NULL_POSITION_OFFSET;
	var MIN_HANDLE_2_X = SLIDER_NULL_POSITION_OFFSET;
	var MAX_HANDLE_2_X = SLIDER_PROMPT_WIDTH - HANDLE_WIDTH;

	var BOUND_LEFT_NULL = 1;
	var BOUND_RANGE_ONE = SLIDER_NULL_POSITION_OFFSET + Math.floor(INCREMENT / 2);
	var BOUND_RANGE_TWO = SLIDER_NULL_POSITION_OFFSET + Math.floor((INCREMENT * 3) / 2);
	var BOUND_RANGE_THREE = SLIDER_NULL_POSITION_OFFSET + Math.floor((INCREMENT * 5) / 2);
	var BOUND_RANGE_FOUR = SLIDER_NULL_POSITION_OFFSET + Math.floor((INCREMENT * 7) / 2);
	var BOUND_RANGE_FIVE = SLIDER_NULL_POSITION_OFFSET + Math.floor((INCREMENT * 9) / 2);
	var BOUND_RIGHT_NULL = SLIDER_PROMPT_WIDTH - HANDLE_WIDTH + 1;

	var RANGE_LEFT_NULL = 0;
	var RANGE_ONE = 1;
	var RANGE_TWO = 2;
	var RANGE_THREE = 3;
	var RANGE_FOUR = 4;
	var RANGE_FIVE = 5;
	var RANGE_RIGHT_NULL = 6;

	var selectedHandle = '';
	var selectedResizer = '';
	var selectedCaption = '';
	var topOrBottom = -1;
	var lastMouseX = 0;

	function getMouseX(event)
	{
		var mouseX = 0;
                if (event.pageX == null) // IE
                {
                        var d = (document.documentElement && document.documentElement.scrollLeft != null) ? document.documentElement : document.body;
                        mouseX= event.clientX + d.scrollLeft;
                }
                else mouseX = event.pageX; // all other browsers
		return mouseX;
	}

	function handleGrab(event, topOrBottomCode, relatedGoalID)
	{
		if(event.preventDefault) event.preventDefault();
		else event.returnValue = false;	
		
		var mouseX = getMouseX(event);
		lastMouseX = mouseX;
		
		selectedHandle = 'handle' + topOrBottomCode + '_' + relatedGoalID;
		selectedResizer = 'resizer' + topOrBottomCode + '_' + relatedGoalID;
		selectedCaption = 'caption' + topOrBottomCode + '_' + relatedGoalID;		
		topOrBottom = topOrBottomCode;

		if(topOrBottom == 1) document.getElementById(handleID).setAttribute('class', 'handle rightHandle');
		else document.getElementById(handleID).setAttribute('class', 'handle leftHandle');
	}

	function determineTopOrBottom()
	{
		var underscoreIndex = selectedCaption.indexOf('_');
		var topOrBottom = selectedCaption.substring(underscoreIndex-1, underscoreIndex);
		return topOrBottom;
	}

	function getSelectedHandleLeftInt()
	{
		var handle = document.getElementById(selectedHandle);
                var handleLeftNumPixels = handle.style.left;
                var handleLeftNum = handleLeftNumPixels.substring(0, handleLeftNumPixels.length-2); // removes 'px'
                var handleLeftInt = parseInt(handleLeftNum);
		return handleLeftInt;
	}

	function handleRelease(userID, primaryGoalID, relatedGoalID)
	{
                var handleLeftInt = getSelectedHandleLeftInt();

		if(handleLeftInt < BOUND_LEFT_NULL) setWeight(RANGE_LEFT_NULL, userID, primaryGoalID, relatedGoalID);
		else if(handleLeftInt < BOUND_RANGE_ONE) setWeight(RANGE_ONE, userID, primaryGoalID, relatedGoalID);
		else if(handleLeftInt < BOUND_RANGE_TWO) setWeight(RANGE_TWO, userID, primaryGoalID, relatedGoalID);
		else if(handleLeftInt < BOUND_RANGE_THREE) setWeight(RANGE_THREE, userID, primaryGoalID, relatedGoalID);
		else if(handleLeftInt < BOUND_RANGE_FOUR) setWeight(RANGE_FOUR, userID, primaryGoalID, relatedGoalID);
		else if(handleLeftInt < BOUND_RANGE_FIVE) setWeight(RANGE_FIVE, userID, primaryGoalID, relatedGoalID);
		else setWeight(RANGE_RIGHT_NULL, userID, primaryGoalID, relatedGoalID);		

		selectedHandle = '';
		selectedResizer = '';
		selectedCaption = '';
		topOrBottom = -1;
	}	

	function setSelectedElementPositions(handleLeftInt)
	{
		// handle positioning
		var handleLeftNumPx = handleLeftInt + 'px';
                document.getElementById(selectedHandle).style.left = handleLeftNumPx;
		
		// resizer positioning
		if(topOrBottom == 1)
		{
			var resizerWidthInt = handleLeftInt + 1;
			var resizerWidthPx = resizerWidthInt + 'px'; 
			document.getElementById(selectedResizer).style.width = resizerWidthPx;
		}
		else
		{
			var resizerWidthInt = handleLeftInt + HANDLE_WIDTH - 1;
			var resizerWidthPx = resizerWidthInt + 'px'; 
			document.getElementById(selectedResizer).style.width = resizerWidthPx;		
		}

		// caption positioning
		var offsetCount = 0; // used for spacing caption away from pointer
		if(topOrBottom == '1') offsetCount = 2;
		else offsetCount = 4;

		var captionLeftInt = handleLeftInt + (offsetCount*HALF_HANDLE_WIDTH);
		var captionLeftNumPx = captionLeftInt + 'px';
		document.getElementById(selectedCaption).style.left = captionLeftNumPx; 
	}

	function setWeight(range, userID, primaryGoalID, relatedGoalID)
	{
		// set handle location to discrete value
		if(range == RANGE_LEFT_NULL)
		{
			handleLeftInt = 0;
			document.getElementById(selectedHandle).setAttribute('class','handle rightHandleNull');
		}
		else if(range == RANGE_RIGHT_NULL)
		{
			handleLeftInt = SLIDER_PROMPT_WIDTH - HANDLE_WIDTH;
			document.getElementById(selectedHandle).setAttribute('class','handle leftHandleNull');
		}
		else handleLeftInt = SLIDER_NULL_POSITION_OFFSET + (range-1)*INCREMENT;

		setSelectedElementPositions(handleLeftInt);

		if(topOrBottom == 1)
		{
			var weight = range;
			updateWeighting(weight, userID, primaryGoalID, relatedGoalID);
		}
		else
		{
			var weight = 6 - range;
			updateWeighting(weight, userID, relatedGoalID, primaryGoalID);
		}
	}

	function updateWeighting(weight, userID, effectingGoalID, effectedGoalID)
        {
                // Init AJAX OBJ
                var $AJAX = null;

                if (window.XMLHttpRequest) {
                   $AJAX=new XMLHttpRequest();
                } else {
                   $AJAX=new ActiveXObject("Microsoft.XMLHTTP");
                }
                if ($AJAX==null) {
                   alert("Your browser doesn't support AJAX.");
                   //return false;
                }

//                $AJAX.onreadystatechange = function() {
  //                 if ($AJAX.readyState==4 || $AJAX.readyState=="complete") {
  //			alert($AJAX.responseText);        
//              confirmUpdate($AJAX.responseText);
    //                   }
      //          }

                var $url='http://4dnetwork.org/resources/weight_editing_proxy.php?';
                $url += 'action=update&weight=' + weight+ '&userID=' + userID;
                $url += '&effectingGoalID= ' + effectingGoalID + '&effectedGoalID=' + effectedGoalID;

                $AJAX.open("GET", $url, true);       // Open the url this object was set-up with.
                $AJAX.send(null);                    // Send the request.
        }

//	function confirmUpdate(responseText)
//      {
//		//alert(responseText);
//      }

	function handleDrag(event)
	{
		if(selectedHandle != '')
		{
			var mouseX = getMouseX(event);
			var dx = mouseX - lastMouseX;

			//printError(mouseX + " dx:" + dx);

			var handleLeftInt = getSelectedHandleLeftInt();
			//printError("handlelI: " + handleLeftInt);
			var nextX = handleLeftInt + dx;			
	
			//printError(nextX);

			// determine drag constraints for handle
			var minX = MIN_HANDLE_1_X;
			var maxX = MAX_HANDLE_1_X;

			if(topOrBottom == 2) // handle constraints for handle1
			{
				minX = MIN_HANDLE_2_X;
				maxX = MAX_HANDLE_2_X;
			}

			if(nextX > minX && nextX < maxX)
			{
				lastMouseX = mouseX;
				if(topOrBottom == 1) document.getElementById(selectedHandle).setAttribute('class','handle rightHandle');
				else document.getElementById(selectedHandle).setAttribute('class','handle leftHandle');
			}
			else if(nextX <= minX)
			{
			 	nextX = minX;
				if(topOrBottom == 1) document.getElementById(selectedHandle).setAttribute('class','handle rightHandleNull');
			}
			else if(nextX >= maxX)
			{
				nextX = maxX;
				if(topOrBottom == 2) document.getElementById(selectedHandle).setAttribute('class','handle leftHandleNull');	
			}

			if(nextX < BOUND_LEFT_NULL) updateCaption(RANGE_LEFT_NULL)
			else if(nextX < BOUND_RANGE_ONE) updateCaption(RANGE_ONE);
			else if(nextX < BOUND_RANGE_TWO) updateCaption(RANGE_TWO);
			else if(nextX < BOUND_RANGE_THREE) updateCaption(RANGE_THREE);
			else if(nextX < BOUND_RANGE_FOUR) updateCaption(RANGE_FOUR);
			else if(nextX < BOUND_RANGE_FIVE) updateCaption(RANGE_FIVE);
			else updateCaption(RANGE_RIGHT_NULL);
		
			setSelectedElementPositions(nextX);

		
//			//printError("dragged");
		}
	}

	function updateCaption(range)
	{
		var caption = document.getElementById(selectedCaption);

		if(topOrBottom == '1')
		{
			if(range == RANGE_LEFT_NULL) caption.innerHTML = "-- No rating given --";
			else if(range == RANGE_ONE) caption.innerHTML = "0.1 (Minimal effect)";
			else if(range == RANGE_TWO) caption.innerHTML = "1 (Slight effect)"; 
			else if(range == RANGE_THREE) caption.innerHTML = "10 (Moderate effect)"; 
			else if(range == RANGE_FOUR) caption.innerHTML = "100 (Strong effect)"; 
			else caption.innerHTML = "1000 (Very strong effect)";
/*
			if(range == RANGE_ONE) caption.innerHTML = "minimally effects";
			else if(range == RANGE_TWO) caption.innerHTML = "slightly effects"; 
			else if(range == RANGE_THREE) caption.innerHTML = "moderately effects"; 
			else if(range == RANGE_FOUR) caption.innerHTML = "strongly effects"; 
			else caption.innerHTML = "very strongly effects";
*/
		}
		else
		{
			if(range == RANGE_ONE) caption.innerHTML = "1000 (Very strong effect)";
                        else if(range == RANGE_TWO) caption.innerHTML = "100 (Strong effect)";
                        else if(range == RANGE_THREE) caption.innerHTML = "10 (Moderate effect)";
                        else if(range == RANGE_FOUR) caption.innerHTML = "1 (Slight effect)";
                        else if(range == RANGE_FIVE) caption.innerHTML = "0.1 (Minimal effect)";
			else caption.innerHTML = "-- No rating given --";
/*
			if(range == RANGE_ONE) caption.innerHTML = "is very strongly effected by";
                        else if(range == RANGE_TWO) caption.innerHTML = "is strongly effected by";
                        else if(range == RANGE_THREE) caption.innerHTML = "is moderately effected by";
                        else if(range == RANGE_FOUR) caption.innerHTML = "is slightly effected by";
                        else caption.innerHTML = "is minimally effected by";
*/
		}		
	}

	function getElementX(element)
	{
		var elementX = 0;
                if (element.offsetParent)
                {
                        var obj = element;
                        while (obj.offsetParent)
                        {
                                elementX += obj.offsetLeft
                                obj = obj.offsetParent;
                        }
                }
                else if (element.x) elementX += element.x;
		return elementX;
	}

	function handleClick(event, trackElement, topOrBottomCode, userID, primaryGoalID, relatedGoalID)
	{
		selectedHandle = 'handle' + topOrBottomCode + '_' + relatedGoalID;
		selectedResizer = 'resizer' + topOrBottomCode + '_' + relatedGoalID;
		selectedCaption = 'caption' + topOrBottomCode + '_' + relatedGoalID;
		topOrBottom = topOrBottomCode;

		var mouseX = getMouseX(event);
		var trackElementX = getElementX(trackElement);

		lastMouseX = mouseX;
	
		var trackX = mouseX - trackElementX; // difference between mouse click x-coordinate and x-coordinate of track element
		
		centeringX = trackX - HALF_HANDLE_WIDTH;

		// determine drag constraints for handle
    		var minX = MIN_HANDLE_1_X;
              	var maxX = MAX_HANDLE_1_X;

               	if(topOrBottom == 2) // handle constraints for handle1
		{
 			minX = MIN_HANDLE_2_X;
                       	maxX = MAX_HANDLE_2_X;
          	}

		var handleX = 0;

		// in case where centerX beyond movable range, want to move arrow but not leave user with active handle on it
		var forceReleaseAfterMove = false; 
		if(centeringX > minX && centeringX < maxX)
		{
			handleX = centeringX;
			if(topOrBottom == 1) document.getElementById(selectedHandle).setAttribute('class','handle rightHandle');
			else document.getElementById(selectedHandle).setAttribute('class','handle leftHandle');
		}
		else if(centeringX <= minX)
		{
			handleX = minX;
			if(topOrBottom == 1) document.getElementById(selectedHandle).setAttribute('class','handle rightHandleNull');		
			else
			{
				document.getElementById(selectedHandle).setAttribute('class','handle leftHandle');
				forceReleaseAfterMove = true;
			}
		}
		else if(centeringX >= maxX)
		{
			handleX = maxX;
			if(topOrBottom == 2) document.getElementById(selectedHandle).setAttribute('class','handle leftHandleNull');
			else
			{
				document.getElementById(selectedHandle).setAttribute('class','handle rightHandle');
				forceReleaseAfterMove = true;
			}
		}
		setSelectedElementPositions(handleX);

		if(handleX < BOUND_LEFT_NULL) updateCaption(BOUND_LEFT_NULL)
		else if(handleX < BOUND_RANGE_ONE) updateCaption(RANGE_ONE);
                else if(handleX < BOUND_RANGE_TWO) updateCaption(RANGE_TWO);
              	else if(handleX < BOUND_RANGE_THREE) updateCaption(RANGE_THREE);
            	else if(handleX < BOUND_RANGE_FOUR) updateCaption(RANGE_FOUR);
               	else if(handleX < BOUND_RANGE_FIVE) updateCaption(RANGE_FIVE);
		else updateCaption(BOUND_RIGHT_NULL)

		if(forceReleaseAfterMove == false) showCaption(selectedCaption); // okay to show caption and leave active handle
		else handleRelease(userID, primaryGoalID, relatedGoalID); // do not show caption after updating it, remove active handle

		//printError('ok');
	}

	function printError(msg)
	{
		document.getElementById('error').innerHTML = document.getElementById('error').innerHTML + " " + msg;
	}

	function showCaption(captionID, handle)
	{
		var caption = document.getElementById(captionID);
                if(handle)
                {
                        var underscoreIndex = handle.id.indexOf('_');
                        var topOrBottom = handle.id.substring(underscoreIndex-1, underscoreIndex);

                        var handleLeftNumPixels = handle.style.left;
                        var handleLeftNum = handleLeftNumPixels.substring(0, handleLeftNumPixels.length-2); // removes 'px'
                        var handleLeftInt = parseInt(handleLeftNum);

                        var offsetCount = 0; // used for spacing caption away from pointer
                        if(topOrBottom == '1') offsetCount = 2;
                        else offsetCount = 3;

                        var captionLeftInt = handleLeftInt + (offsetCount*HALF_HANDLE_WIDTH);
                        var captionLeftNumPx = captionLeftInt + 'px';
                        caption.style.left = captionLeftNumPx;
                }
                caption.style.display = "block";
	}

	function hideCaption(captionID)
	{
	        document.getElementById(captionID).style.display = "none";
	}


