function checkfields(frmobj)
{
	mp1 = frmobj.mp1.value;
	ar1 = frmobj.ar1.value;
	mp2 = frmobj.mp2.value;
	ar2 = frmobj.ar2.value;
	ppiv = frmobj.ppi.value;
		
	if (mp1 == '' || mp2 == '' || ar1 == '' || ar2 == '')		
	{
		alert('All fields must be entered.');
		return false;
	}
	
	if (mp1 == '0' || mp2 == '0' || ar1 < 1 || ar2 < 1)
	{
		alert('Aspect ratios can\'t be less than 1, and megapixel amounts must be greater than zero.');
		return false;
	}
	
	if (ppiv == '' || ppiv <= 0)
	{
		alert('Please enter a PPI value. If you are not sure what to use enter 300 as default');
		return false;
	}
	
	return true;
}

function calcMP(frmobj)
{
	if (checkfields(document.mpform) == true)
	{
		mp1 = document.mpform.mp1.value;
		ar1 = document.mpform.ar1.value;
		mp2 = document.mpform.mp2.value;
		ar2 = document.mpform.ar2.value;
		ppiv = document.mpform.ppi.value;
		
		mp1L = calcLen(mp1, ar1)
		mp1W = calcWidth(mp1, ar1)
		mp2L = calcLen(mp2, ar2)
		mp2W = calcWidth(mp2, ar2)
		
		str = 'Approximate Image Size: \n';
		str = str + 'Camera #1 ~ ' + mp1L + ' x ' + mp1W + '\n';
		str = str + 'Camera #2 ~ ' + mp2L + ' x ' + mp2W + ' ('+ Math.round((mp2/mp1)*10)/10 +'x the pixels of camera #1)\n';
		str = str + '\n';
		
		str = str + 'Resolution Gain:\n';
			
		if (Math.round( (Math.pow(mp2/mp1,0.5)-1) * 1000) / 10 > 0 )
		{
			str = str + 'Camera #2 has ' +  Math.abs((Math.round( (Math.pow(mp2/mp1,0.5)-1) * 1000) / 10)) + '% more overall resolution than camera #1 \n';
		}
		else
		{
			str = str + 'Camera #2 has ' +  Math.abs((Math.round( (Math.pow(mp2/mp1,0.5)-1) * 1000) / 10)) + '% less overall resolution than camera #1 \n';
		}
		
		if ( ar1 != ar2)
		{
			if ((Math.round(((mp2L - mp1L)/ mp1L)*1000) / 10) > 0 )
			{
				str = str + 'Camera #2 has ' +  (Math.round(((mp2L - mp1L)/ mp1L)*1000) / 10) + '% more horizontal resolution than camera #1 \n';
			}
			else
			{
				str = str + 'Camera #2 has ' +  Math.abs((Math.round(((mp2L / mp1L)-1)*1000) / 10)) + '% less horizontal resolution than camera #1 \n';
			}
			if ((Math.round(((mp2W - mp1W)/ mp1W)*1000) / 10) > 0 )
			{				
				str = str + 'Camera #2 has ' +  (Math.round(((mp2W - mp1W)/ mp1W)*1000) / 10) + '% more vertical resolution than camera #1 \n';
			}
			else
			{
				str = str + 'Camera #2 has ' +  Math.abs((Math.round(((mp2W / mp1W)-1)*1000) / 10)) + '% less vertical resolution than camera #1 \n';
			}
		}
		
		str = str + '\n';
		str = str + 'What does this mean?\n';
	
		if (mp1L/1.5 > mp1W)
		{
			L = mp1W *1.5;
			W = mp1W;
		}
		else
		{
			W = mp1L /1.5;
			L = mp1L;
		}
		size30032 = L/ppiv;
		
		ppi = L/size30032;
		
		if (mp2L/1.5 > mp2W)
		{
			L = mp2W *1.5;
			W = mp2W;
		}
		else
		{
			W = mp2L /1.5;
			L = mp2L;
		}
		
		size1 = Math.round(W/ppi*100)/100;
		size2 = Math.round(L/ppi*100)/100;
		
		
		if (mp1L/1.3333333 > mp1W)
		{
			L = mp1W *1.3333333;
			W = mp1W;
		}
		else
		{
			W = mp1L /1.3333333;
			L = mp1L;
		}
		size30043 = L/ppiv;
		ppi = L/size30043;
				
		if (mp2L/1.3333333 > mp2W)
		{
		
			L = mp2W *1.3333333;
			W = mp2W;
		}
		else
		{
			W = mp2L /1.3333333;
			L = mp2L;
		}
				
		size3 = Math.round(W/ppi*100)/100;
		size4 = Math.round(L/ppi*100)/100;
		str = str + 'At '+ppiv+' PPI the two cameras would make the following 3:2 and 4:3 sizes:\n';
		str = str + 'Camera #2 would print at ' + size1 + '" x ' + size2 + '" whereas Camera #1 at '+ Math.round((size30032/1.5)*100)/100 +'" x ' + Math.round(size30032*100)/100 +'" \n';
		str = str + 'Camera #2 would print at ' + size3 + '" x ' + size4 + '" whereas Camera #1 at '+ Math.round((size30043/1.3333333)*100)/100 +'" x ' + Math.round(size30043*100)/100 +'" \n';
		document.mpform.output.value = str;
	}
}

function calcLen(mps, ratio)
{
	l = Math.floor(Math.pow((mps * 1000000 * ratio),0.5));
	w =  Math.floor(Math.pow((mps * 1000000 * (1 / ratio)),0.5));
	return l;
}

function calcWidth(mps, ratio)
{
	w =  Math.floor(Math.pow((mps * 1000000 * (1 / ratio)),0.5));
	return w;
}

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);
	}
}