
function getObj(name)
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function IsEmpty(aTextField) 
{
	if ((aTextField.value.length==0) || (aTextField.value==null)) 
		return true;
	else 
		return false;
}

function IsNumeric(sText)
{
	var ValidChars = "0123456789";
	var IsNumber = true;
	var Char;
	
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			IsNumber = false;
	}
	
	return IsNumber;  
}

function submitForm(oElemID)
{
	var oElem = new getObj(oElemID);
	oElem.obj.submit();	
}

var mixNMatchMin = 0;
function modifyQuantity(unit_weight, unit_price, unit, qd_qty, qd_price, product_id, min)
{	
	/*var total_id = 'total';
	var quantity_id = 'quantity';
	
	if (product_id > 0)
	{
		//total_id = 'total_' + product_id;		
		quantity_id = 'quantity_' + product_id;	
	}
	
	var oElemQuantity = document.getElementById(quantity_id);
	var oElemWeight = document.getElementById('weight');
	var oElemTotal = document.getElementById(total_id);
	
	if (oElemWeight)
		oElemWeight.value = (Math.round(parseInt(oElemQuantity.value) * unit_weight * 100) / 100).toFixed(2) + " " + unit;
	
	if (oElemTotal)
	{
		oElemTotal.innerHTML = ''; // Mac IE fix
		oElemTotal.innerHTML = (Math.round(parseInt(oElemQuantity.value) * unit_price * 100) / 100).toFixed(2);
	}
	*/
	var total_id = 'total';
	var weight_id = 'weight';
	var quantity_id = 'quantity';
	
	if (product_id > 0)
	{
		total_id = 'total_' + product_id;		
		quantity_id = 'quantity_' + product_id;	
		weight_id = 'weight_' + product_id;
	}
	
	var oElemQuantity = document.getElementById(quantity_id);
	var oElemWeight = document.getElementById(weight_id);
	var oElemTotal = document.getElementById(total_id);

	if(oElemQuantity.value < min)
		oElemQuantity.value = min;

	if(oElemQuantity)
	{
		if(product_id)
		{
			//var thisLimit = eval('discLimit_' + product_id);
			//alert(thisLimit);
			//alert();
			if(discProds[product_id])
			{
				tmpLimit = eval(eval('discLimit_' + product_id));
				tmpLimit = tmpLimit * 1;

				if(tmpLimit < oElemQuantity.value)
					var tQTY = tmpLimit;
				else
					var tQTY = oElemQuantity.value;

				var tmpPercent = eval('discPercent_' + product_id + '_' + tQTY);
				var tmpDollar = eval('discDollar_' + product_id + '_' + tQTY);

				//alert(tmpPercent+" test Percent");
				//alert(tmpDollar+" test Dollar");
			}
		}
		else
		{
			if(discLimit)
			{
				if(discLimit < oElemQuantity.value)
					var tQTY = discLimit;
				else
					var tQTY = oElemQuantity.value;

				var tmpPercent = eval('discPercent_' + tQTY);
				var tmpDollar = eval('discDollar_' + tQTY);
			}
		}
	}
	
	if (qd_qty > 0 && parseInt(oElemQuantity.value) >= qd_qty)
	{
		//
		// OLD QUANTITY DISCOUNT
		//
		unit_price = qd_price;
	}
	else if(tmpPercent)
	{
		//
		// NEW QUANTITY DISCOUNT PERCENT
		//
		tmpDiscount = unit_price * tmpPercent;
		unit_price -= tmpDiscount;
	}
	else if(tmpDollar)
	{
		//
		// NEW QUANTITY DISCOUNT DOLLAR
		//
		tmpDiscount = tmpDollar;
		unit_price -= tmpDiscount;
	}
	//alert(unit_price);
	unit_price = Math.round(unit_price * 100) / 100;

	if (oElemWeight)
		oElemWeight.value = (Math.round(parseInt(oElemQuantity.value) * unit_weight * 100) / 100).toFixed(2) + " " + unit;
	
	//alert(oElemTotal.value);
	if (oElemTotal)
	{
		//alert((Math.round(parseInt(oElemQuantity.value) * unit_price * 100) / 100).toFixed(2));
		//oElemTotal.innerHTML = ''; // Mac IE fix
		if(oElemTotal.value)
			oElemTotal.value = "$"+(Math.round(parseInt(oElemQuantity.value) * unit_price * 100) / 100).toFixed(2);
		else
			oElemTotal.innerHTML = ""+(Math.round(parseInt(oElemQuantity.value) * unit_price * 100) / 100).toFixed(2);
	
	}

	var tVal = parseInt(oElemQuantity.value);

	if(mixNMatchMin > 0)
		if(tVal >= mixNMatchMin)
			document.getElementById('center_left').action = "/threads/process.html";
		else
			document.getElementById('center_left').action = "/productsMixNMatch.html";
}	

function upQuantity(unit_weight, unit_price, unit, qd_qty, qd_price, product_id)
{	
	var quantity_id = 'quantity';
	
	if (product_id > 0)
		quantity_id = 'quantity_' + product_id;	
	
	var oElemQuantity = document.getElementById(quantity_id);

	if (parseInt(oElemQuantity.value) + 1 < 100)	
		oElemQuantity.value = parseInt(oElemQuantity.value) + 1;
		
	if (qd_qty > 0 && parseInt(oElemQuantity.value) >= qd_qty)
		unit_price = qd_price;
	
	modifyQuantity(unit_weight, unit_price, unit, qd_qty, qd_price, product_id);
}	

function downQuantity(min, unit_weight, unit_price, unit, qd_qty, qd_price, product_id)
{	
	var quantity_id = 'quantity';
	
	if (product_id > 0)
		quantity_id = 'quantity_' + product_id;	
	
	var oElemQuantity = document.getElementById(quantity_id);

	if (parseInt(oElemQuantity.value) - 1 >= min)
		oElemQuantity.value = parseInt(oElemQuantity.value) - 1;
		
	if (qd_qty > 0 && parseInt(oElemQuantity.value) >= qd_qty)
		unit_price = qd_price;
	
	modifyQuantity(unit_weight, unit_price, unit, qd_qty, qd_price, product_id, min);	
}	

function validateQuantity(min, unit_weight, unit_price, unit, qd_qty, qd_price, product_id)
{
	var quantity_id = 'quantity';
	
	if (product_id > 0)
		quantity_id = 'quantity_' + product_id;
			
	var oElemQuantity = document.getElementById(quantity_id);

	if ((IsEmpty(oElemQuantity)) || (! IsNumeric(oElemQuantity.value)) || (parseInt(oElemQuantity.value) < min))
		oElemQuantity.value = min;
	
	if (parseInt(oElemQuantity.value) > 99)
		oElemQuantity.value = 99;
	
	modifyQuantity(unit_weight, unit_price, unit, qd_qty, qd_price, product_id, min);	
}

function clearCheckbox(oElemID)
{
	var oElem = new getObj(oElemID);
	oElem.obj.checked = false;
}

function setCheckbox(oElemID)
{
	var oElem = new getObj(oElemID);
	oElem.obj.checked = true;
}