function checkfields(frmobj)
{
	imgL = frmobj.length.value;
	imgW = frmobj.width.value;
	mps = frmobj.mps.value;
	ratio = frmobj.ratio.value;
	printL = frmobj.length2.value;
	printW = frmobj.width2.value;
	
	if (parseFloat(printL) < parseFloat(printW))
	{
		frmobj.width2.value = printL;
		frmobj.length2.value = printW;
	}
	
	if (imgL == '' && imgW != '' && mps =='' && ratio == '') 
	{
		alert('Please enter a length.')
		return false;
	}
	if (imgW == '' && imgL != '' && mps =='' && ratio == '') 
	{
		alert('Please enter a width.');
		return false;
	}
	
	if (imgL == '' && imgW == '' && mps !='' && ratio == '') 
	{
		alert('Please enter a aspect ratio.');
		return false;
		
	}
	if (imgW == '' && imgL == '' && mps =='' && ratio != '') 
	{
		alert('Please enter a megapixel amount.');
		return false;
	}
	
	if ((imgL != '' || imgW != '') && (mps !='' || ratio != ''))
	{
		if (imgL != '' && imgW !='' && mps !='' & ratio != '')
		{
		}
		else
		{
			alert('Please only enter the lenght and width, or the megapixels and aspect ratio, but not both.');
			return false;
		}
	}
	if ( (printL !='' && printW == '') || (printL =='' && printW !='') )
	{
		alert('Please enter both the option custom print size length and width');
		return false;
	}
	
	if (imgL < imgW)
	{
		alert('Image size width must be smaller than the length');
		return false;
	}
	
	return true;
}

function calcPPI(frmobj)
{
	if (checkfields(document.ppiform) == true)
	{
		imgL = document.ppiform.length.value;
		imgW = document.ppiform.width.value;
		mps = document.ppiform.mps.value;
		ratio = document.ppiform.ratio.value;
		printL = document.ppiform.length2.value;
		printW = document.ppiform.width2.value;
		
		if (imgL !='' && imgW != '')
		{
			document.ppiform.mps.value = (imgL * imgW) / 1000000;
			document.ppiform.ratio.value = Math.round( (imgL / imgW)*10000) /10000;
		}
		else
		{
			document.ppiform.length.value = Math.floor(Math.pow((mps * 1000000 * ratio),0.5));
			document.ppiform.width.value =  Math.floor(Math.pow((mps * 1000000 * (1 / ratio)),0.5));
			imgL = document.ppiform.length.value;
			imgW = document.ppiform.width.value;
		}
		
		str = 'PPI for the following sizes:\n\n';
		if (printW != '') str = str + printW + ' x ' + printL + ' = ' + findPPI(printL, printW, imgL, imgW) + '\n\n'; 
		str = str + '4 x 6 = ' + findPPI(6, 4, imgL, imgW) + '\n'; 
		str = str + '5 x 7 = ' + findPPI(7, 5, imgL, imgW) + '\n'; 
		str = str + '6 x 8 = ' + findPPI(8, 6, imgL, imgW) + '\n'; 
		str = str + '6 x 9 = ' + findPPI(9, 6, imgL, imgW) + '\n'; 
		str = str + '8 x 10 = ' + findPPI(10, 8, imgL, imgW) + '\n'; 
		str = str + '8 x 12 = ' + findPPI(12, 8, imgL, imgW) + '\n'; 
		str = str + '8.5 x 11 = ' + findPPI(11, 8.5, imgL, imgW) + '\n'; 
		str = str + '10 x 13 = ' + findPPI(13, 10, imgL, imgW) + '\n'; 
		str = str + '10 x 15 = ' + findPPI(15, 10, imgL, imgW) + '\n'; 
		str = str + '11 x 14 = ' + findPPI(14, 11, imgL, imgW) + '\n'; 
		str = str + '12 x 16 = ' + findPPI(16, 12, imgL, imgW) + '\n'; 
		str = str + '12 x 18 = ' + findPPI(18, 12, imgL, imgW) + '\n'; 
		str = str + '13 x 19 = ' + findPPI(19, 13, imgL, imgW) + '\n'; 
		str = str + '16 x 20 = ' + findPPI(20, 16, imgL, imgW) + '\n'; 
		str = str + '16 x 24 = ' + findPPI(24, 16, imgL, imgW) + '\n'; 
		str = str + '18 x 24 = ' + findPPI(24, 18, imgL, imgW) + '\n'; 
		str = str + '20 x 30 = ' + findPPI(30, 20, imgL, imgW) + '\n'; 
		str = str + '24 x 36 = ' + findPPI(36, 24, imgL, imgW) + '\n'; 
		str = str + '30 x 40 = ' + findPPI(40, 30, imgL, imgW) + '\n'; 
		
		document.ppiform.output.value = str;
	}
}

function findPPI(l, w, imgL, imgW)
{
	len = imgL / l;
	wid = imgW / w;
	
	if (len > wid)
	{
		return Math.round(wid*(100))/(100);
	}
	else
	{
		return Math.round(len*(100))/(100);
	}
}