var mainForm = null;


function getElement(theForm, id)
{
	var element = null;
	if(document.all)
	{
		 element = document.all[id];
		 if(element != null)
		 {
			return(element);
		 }
	}
	else
	{
		if(document.getElementById)
		{
			element = document.getElementById(id);
		}
		else
		{
			if(document.layers)
			{
				element = document.layers[id];
			}
		}
	}
	if(mainForm == null)
	{
		if(document.forms[1])
		{
			mainForm = document.forms[1];
		}
		else
		{
			mainForm = document.forms[0];
		}
	}
	if(mainForm)
	{
		if(element == null)
		{
			element = mainForm[id];
		}
		if(element == null)
		{
			for(var i = 0; i < mainForm.length; i++)
			{
				if(mainForm[i].name == id)
				{
					element = mainForm[i];
				}
			}
		}
	}
	if(element == null)
	{
		//alert('Unsupported Browser: ' + id);
	}
	return(element);
}

function buttonBlinker(element)
{
	if(element.className == 'buttonOff')
	{
		element.className = 'buttonOn';
	}
	else
	{
		element.className = 'buttonOff';
	}
	setTimeout('buttonBlinker(blinkButton)', 1000);
}

function parseMixedFraction(theField, denominators, fieldName)
{
	var strNumber = theField.value;
	var parsedNumber = "";
	var wholeNumber = 0;
	var numerator = 0;
	var denominator = 1;
	if(strNumber.indexOf(' ') >= 0)
	{
		wholeNumber = parseInt(strNumber.substring(0, strNumber.indexOf(' ')));
		parsedNumber = parseInt(wholeNumber);
	}
	else
	{
		if(theField.value.indexOf('/'))
		{
			wholeNumber = 0;
			parsedNumber = 0;
		}
		else
		{
			wholeNumber = parseInt(strNumber);
			parsedNumber = strNumber;
		}
	}
	if(strNumber.indexOf('/') >= 0)
	{
		numerator = strNumber.substring(strNumber.indexOf(' '), strNumber.indexOf('/'));
		denominator = strNumber.substring(strNumber.indexOf('/') + 1,  strNumber.length);
		if(numerator >= denominator)
			return(-1);
		parsedNumber += " " + numerator + "/" + denominator; 
	}
	return(wholeNumber + numerator/denominator);
}

function parseNumber(field, tolerance)
{
	var value = field.value;
	//alert(value);
	if(field.value.indexOf("/") >= 0)
	{
		value = parseMixedFraction(field);
	}
	if(parseInt(value * tolerance) != parseFloat(value * tolerance))
	{
		alert("Must be in increments of 1/" + tolerance + "th of an inch.");
		return(-1);
	}
	return(parseFloat(value));
}

function convertUnit(theForm, value)
{
	if(theForm.measurement_unit_id.type == 'hidden')
	{
		if(theForm.measurement_unit_id.value == 1)
		{
			return(value);
		}
		else
		{ 
			return(parseInt(25.4 * value));
		}
	}
	else
	{
		if(theForm.measurement_unit_id[0].checked)
		{
			return(value);
		}
		else
		{ 
			return(parseInt(25.4 * value));
		}
	}
}

function convertToInches(theForm, value)
{
	if(theForm.measurement_unit_id.type == 'hidden')
	{
		if(theForm.measurement_unit_id.value == 1)
		{
			return(value);
		}
		else
		{ 
			return(parseInt(value / 25.4));
		}
	}
	else
	{
		if(theForm.measurement_unit_id[0].checked)
		{
			return(value);
		}
		else
		{ 
			return(parseInt(value / 25.4));
		}
	}
}

function validateDimensions(theForm, offSet, j, constructionTypeID, quantityOffset, heightOffset, widthOffset, depthOffset)
{

	if(theForm[offSet + quantityOffset].value.length != 0)
	{
		if(isNaN(parseInt(theForm[offSet + quantityOffset].value)) || parseInt(theForm[offSet + quantityOffset].value) != theForm[offSet + quantityOffset].value || parseInt(theForm[offSet + quantityOffset].value) == 0)
		{
			alert('The quantity for item ' + (j + 1) + ' is invalid.');
			theForm[offSet + quantityOffset].value = '';
			theForm[offSet + quantityOffset].focus();
			return(false);	
		}
	}
	if(theForm[offSet + heightOffset].value.length != 0)
	{
		var height = parseNumber(theForm[offSet + heightOffset], 4);
		var minHeight = convertUnit(theForm, 2);
		var maxHeight;
		if(theForm.construction_type_id[0])
		{
			if(constructionTypeID == 1)
			{
				maxHeight = convertUnit(theForm, 15);
			}
			else
			{
				maxHeight = convertUnit(theForm, 22.5);
			}
		}
		else
		{
			if(theForm.construction_type_id.value == 1)
			{
				maxHeight = convertUnit(theForm, 15);
			}
			else
			{
				maxHeight = convertUnit(theForm, 22.5);
			}
		}	
		var minWidth = convertUnit(theForm, 5);
		var maxWidth = convertUnit(theForm, 60);
		var minDepth = convertUnit(theForm, 5);
		var unit = 'inches';
		if(convertUnit(theForm, 5) != 5)
		{
			unit = 'mm';
		}
		if(height == -1)
		{
			alert('The height for item ' + (j + 1) + ' is invalid. Please use the format ** */* or **.***');
			theForm[offSet + heightOffset].value = '';
			theForm[offSet + heightOffset].focus();
			return(false);
		}
		if(height < minHeight)
		{
			alert('The height for item ' + (j + 1) + ' must be at least ' + minHeight + ' ' + unit + '.');
			theForm[offSet + heightOffset].value = minHeight;
			theForm[offSet + heightOffset].focus();
			return(false);
		}
		else
		{
			if(height > maxHeight)
			{
				alert('The height for item ' + (j + 1) + ' must be ' + maxHeight + ' ' + unit + ' or less.');
				theForm[offSet + heightOffset].value = maxHeight;
				theForm[offSet + heightOffset].focus();
				return(false);
			}
		}
		theForm[offSet + heightOffset].value = height;
	}
	if(theForm[offSet + widthOffset].value.length != 0)
	{
		var width = parseNumber(theForm[offSet + widthOffset], 16);
		if(parseFloat(width) == parseFloat(-1))
		{
			alert('The width for item ' + (j + 1) + ' is invalid. Please use the format ** */* or **.***');
			theForm[offSet + widthOffset].value = '';
			theForm[offSet + widthOffset].focus();
			return(false);
		}
		if(width < minWidth)
		{
			alert('The width for item ' + (j + 1) + ' must be at least ' + minWidth + ' ' + unit + '.');
			theForm[offSet + widthOffset].value = minWidth;
			theForm[offSet + widthOffset].focus();
			return(false);
		}
		else
		{
			if(width > maxWidth)
			{
				alert('The width for item ' + (j + 1) + ' must be ' + maxWidth + ' ' + unit + ' or less.');
				theForm[offSet + widthOffset].value = maxWidth;
				theForm[offSet + widthOffset].focus();
				return(false);
			}
		}
		theForm[offSet + widthOffset].value = width;
	}
	if(theForm[offSet + depthOffset].value.length != 0)
	{
		var depth = parseNumber(theForm[offSet + depthOffset], 16);
		if(parseFloat(depth) == parseFloat(-1))
		{
			alert('The depth for item ' + (j + 1) + ' is invalid. Please use the format ** */* or **.***');
			theForm[offSet + depthOffset].value = '';
			theForm[offSet + depthOffset].focus();
			return(false);
		}
		if(depth < minDepth)
		{
			alert('The depth for item ' + (j + 1) + ' must be at least ' + minDepth + ' ' + unit + '.');
			theForm[offSet + depthOffset].value = minDepth;
			theForm[offSet + depthOffset].focus();
			return(false);
		}
		theForm[offSet + depthOffset].value = depth;
	}
	return(true);
}

