
	function formatPercentage(percentage)
	{
		var num = new Number(percentage);
		return num.toFixed(3);
	}
	
	function validateCropCoords(image, width, height)
	{
		var cropper = YAHOO.widget.ImageCropper.getCropperById(image);
		
		var region = cropper.getCropCoords();
		
		var originalRatio = new Number(width / height);
		var cropRatio1 = new Number(region.width / region.height);
		var cropRatio2 = new Number(region.height / region.width);
		
		//We are allowing the difference between ratio to be within 0.01 accuracy
		//we check against both as the rotation will affect the comparison
		if(Math.abs(originalRatio - cropRatio1) <= 0.01 || Math.abs(originalRatio - cropRatio2) <= 0.01)
		{
//			alert("Crop region is fine " + originalRatio + " " + cropRatio1 + " " + cropRatio2);
			return true;
		}
//		alert("Invalid crop region " + originalRatio + " " + cropRatio1 + " " + cropRatio2);
		return false;
	}
	
	function addManualCrop(image, width, height, cropFromLeft, cropFromTop, cropWidth, cropHeight, handleCropDimensions)
	{
//		if(typeof(crop) !== 'undefined')
//			crop.destroy();
		
		//reposition and resize crop overlay
		crop = new YAHOO.widget.ImageCropper(image, {
	        initialXY: [cropFromLeft * width, cropFromTop * height],
	        initHeight: cropHeight * height,
	        initWidth: cropWidth * width,
	        minHeight: cropHeight * height,
	        minWidth: cropWidth * width,
	        ratio: true,
	        status: false
	    });
	
		var region = crop.getCropCoords();
		
		handleCropDimensions(
			formatPercentage(region.left / width), 
			formatPercentage(region.top / height), 
			formatPercentage(region.width / width), 
			formatPercentage(region.height / height));
	    
		crop.on('moveEvent', function()
		{
			var region = crop.getCropCoords();
		
	    	handleCropDimensions(
	    		formatPercentage(region.left / width), 
	    		formatPercentage(region.top / height), 
	    		formatPercentage(region.width / width), 
	    		formatPercentage(region.height / height));
		});
	
	 	//Need to figure this out in order to fix the resizing of crop region bug
//	    crop.getResizeObject().on('endResize', function()
//		{
//			var region = crop.getCropCoords();
//			if(region.width > newWidth || region.height > newHeight)
//			{
//				crop.destroy();
//				setCrop(image, width, height, cropWidthInInches, cropHeightInInches, restrictResizing);
//			}
//		});
	}
	
	function setCrop(image, width, height, cropWidthInInches, cropHeightInInches, restrictResizing, handleCropDimensions, doRotate)
	{
		if(doRotate == 1)
		{
			tmp = cropWidthInInches;
			cropWidthInInches = cropHeightInInches;
			cropHeightInInches = tmp;
		}
		
		var newWidth = width;
		var newHeight = height;
		
		//Comment out below when not debugging
//		$('idHeight').update(height);
//		$('idWidth').update(width);
//		$('idCropHeightInInches').update(cropHeightInInches);
//		$('idCropWidthInInches').update(cropWidthInInches);
		
//		alert(width + ' x ' + height);
		
		//portait amd square image
		if(height >= width)
		{
//			alert('height > width');
			// Check that the inch width and height are of the same orientation.
			if(cropHeightInInches >= cropWidthInInches)
			{
//				alert('cropHeightInInches > cropWidthInInches');
				
				//First try to keep the height the same and adjust the width
				newWidth = (newHeight / cropHeightInInches) * cropWidthInInches;
				if(newWidth > width)
				{
					//if the Width is too large, then we go back to the max width and adjust the height
					newWidth = width;
					newHeight = (newWidth / cropWidthInInches) * cropHeightInInches;
				}
			}
			else
			{
//				alert('cropHeightInInches < cropWidthInInches');
				newHeight = (newWidth / cropWidthInInches) * cropHeightInInches;
				if(newHeight > height)
				{
					newHeight = height;
					newWidth = (newHeight / cropHeightInInches) * cropWidthInInches;
				}
			}
		}
		//landscape image
		else if(width > height)
		{
//			alert('width > height');
			// Check that the inch width and height are of the same orientation.
			if(cropWidthInInches > cropHeightInInches)
			{
//				alert('cropWidthInInches > cropHeightInInches');
				
				newHeight = (newWidth / cropWidthInInches) * cropHeightInInches;
				if(newHeight > height)
				{
					newHeight = height;
					newWidth = (newHeight / cropHeightInInches) * cropWidthInInches;
				}
			}
			else
			{
//				alert('cropWidthInInches < cropHeightInInches');
				//First try to keep the height the same and adjust the width
				newWidth = (newHeight / cropHeightInInches) * cropWidthInInches;
				
//				alert('newWidth: ' + newWidth);
				
				if(newWidth > width)
				{
					//if the Width is too large, then we go back to the max width and adjust the height
					newWidth = width;
					newHeight = (newWidth / cropWidthInInches) * cropHeightInInches;
					
//					alert('newWidth: ' + newWidth + ' newHeight: ' + newHeight);
				}
			}
		}
		
		//Comment out below when not debugging
	//	$('idRatio').update(cropWidthInInches / cropHeightInInches);
	//	$('idRatioCrop').update(newWidth / newHeight);
	//	$('idCropHeight').update(newHeight);
	//	$('idCropWidth').update(newWidth);
	
		//position in the center
		var xLoc = Math.ceil((width - newWidth ) / 2);
		var yLoc = Math.ceil((height - newHeight ) / 2);
		
//		if(typeof(crop) !== 'undefined')
		if(doRotate == 0 || doRotate == 1)
		{
//			alert('Destroying previous crop');
			crop.destroy();
		}
	
		//reposition and resize crop overlay
		crop = new YAHOO.widget.ImageCropper(image, {
	        initialXY: [xLoc, yLoc],
	        initHeight: newHeight,
	        initWidth: newWidth,
	        minHeight: restrictResizing == true ? newHeight : 10,
	        minWidth: restrictResizing == true ? newWidth : 10,
	        ratio: true,
	        status: false
	    });
	
		var region = crop.getCropCoords();
		
		handleCropDimensions(
			formatPercentage(region.left / width), 
			formatPercentage(region.top / height), 
			formatPercentage(region.width / width), 
			formatPercentage(region.height / height));
	    
		crop.on('moveEvent', function()
		{
			var region = crop.getCropCoords();
		
	    	handleCropDimensions(
	    		formatPercentage(region.left / width), 
	    		formatPercentage(region.top / height), 
	    		formatPercentage(region.width / width), 
	    		formatPercentage(region.height / height));
		});
	
//	 	//Need to figure this out in order to fix the resizing of crop region bug
//		crop.getResizeObject().on('endResize', function()
//		{
//			var region = crop.getCropCoords();
//			if(region.width > newWidth || region.height > newHeight)
//			{
//				setCrop(image, width, height, cropWidthInInches, cropHeightInInches, restrictResizing, doRotate);
//			}
//		});
	}

	//Comment out below when not debugging
//	function handleCropDimensionsInDebug(percentageFromLeft, percentageFromTop, percentageWidth, percentageHeight)
//	{	
//		$('idPercentageCropFromLeft').update(percentageFromLeft);
//		$('idPercentageCropFromTop').update(percentageFromTop);
//		$('idPercentageCropWidth').update(percentageWidth);
//		$('idPercentageCropHeight').update(percentageHeight);
//	}

	function handleCropDimensions(percentageFromLeft, percentageFromTop, percentageWidth, percentageHeight)
	{	
		$('percentage_crop_from_left').value = percentageFromLeft;
		$('percentage_crop_from_top').value = percentageFromTop;
		$('percentage_crop_width').value = percentageWidth;
		$('percentage_crop_height').value = percentageHeight;
		
		//Comment out below when not debugging
//		handleCropDimensionsInDebug(percentageFromLeft, percentageFromTop, percentageWidth, percentageHeight);
	}