// JavaScript Document

numberOfItems = 0;

// this function is responsible for updating the dynamic order form based on
// the quantities entered next to any particular product number
function updateOrder ( e )
	{
	// first we need to find out which element was teh target of the event
	var targ;
	if (!e) 
		{
		var e = window.event;
		}
	if (e.target) 
		{
		targ = e.target;
		}
	else if (e.srcElement)
		{
		targ = e.srcElement;
		}
	if (targ.nodeType == 3) // defeat Safari bug
		{
		targ = targ.parentNode;
		}	
	
	// grab the quantity from the quantity box
	quantity = targ.value;
	
	// now we need to find the part number that is associated with this particular order box
	// first grab the parent table row that the current quantity box is in
	tableRow = targ;
	while ( tableRow.nodeType == 3 || tableRow.tagName != "TR" )
		{
		tableRow = tableRow.parentNode;	
		}
	
	// now step through the table cells until we get to the 2nd one...
	modelCell = tableRow;
	x = 0;
	cells = 0;
	while( cells < 2 )
		{
		modelCell = tableRow.childNodes[x];
		if(modelCell.nodeType != 3 && modelCell.tagName == "TD")
			{
			cells++;	
			}
		x++;
		}
		
	// now grab the text node that represents the model number
	modelNumber = trim( modelCell.firstChild.nodeValue );
		
	// now we need to find the price that is associated with this particular order box
	// first grab the parent table row that the current quantity box is in
	tableRow = targ;
	while ( tableRow.nodeType == 3 || tableRow.tagName != "TR" )
		{
		tableRow = tableRow.parentNode;	
		}
	
	// now step through the table cells until we get to the 2nd one...
	priceCell = tableRow;
	x = 0;
	cells = 0;
	while( cells < 5 )
		{
		priceCell = tableRow.childNodes[x];
		if(priceCell.nodeType != 3 && priceCell.tagName == "TD")
			{
			cells++;	
			}
		x++;
		}
		
	// now grab the text node that represents the price
	price = trim( priceCell.firstChild.nodeValue );
	
	
	// check to see if there is already a dynamic form created for this part number
	if( document.getElementById( modelNumber ))
		{
		// if the new quantity is greater than 0 than we need to simply update the quantity
		if( quantity > 0 )
			{
			document.getElementById( modelNumber + "_quantity" ).value = quantity;
			}
		// if the quantity is 0 then we need to remove the item from the order
		else
			{
			itemToRemove = 	document.getElementById( modelNumber + "_div" );
			itemToRemove.parentNode.removeChild( itemToRemove );
			numberOfItems--;
			document.getElementById("num_of_items").value = numberOfItems;
			}
		}
	// if there is no dynamic form already created for this part number then we need to make one
	else if( quantity > 0 )
		{
		partDiv = document.createElement("div");
		partDiv.id = modelNumber + "_div";
		partDiv.className = "item_container";
		
		modelBox = document.createElement("input");
		modelBox.type = "text";
		modelBox.value = modelNumber;
		modelBox.readOnly = "readonly";
		modelBox.id = modelNumber;
		modelBox.name = modelNumber;
		modelBox.className = "order_item";
		partDiv.appendChild( modelBox );
		
		priceBox = document.createElement("input");
		priceBox.type = "text";
		priceBox.value = price;
		priceBox.readOnly = "readonly";
		priceBox.id = modelNumber + "_price";
		priceBox.name = modelNumber + "_price";
		priceBox.className = "order_item_price";
		partDiv.appendChild( priceBox );
		
		multiply = document.createTextNode(" x ");
		partDiv.appendChild( multiply );
		
		quantityBox = document.createElement("input");
		quantityBox.type = "text";
		quantityBox.value = quantity;
		quantityBox.id = modelNumber + "_quantity";
		quantityBox.name = modelNumber + "_quantity";
		quantityBox.className = "order_quantity";
		partDiv.appendChild( quantityBox );
		
		document.getElementById("order_container").appendChild( partDiv );
		numberOfItems++;
		document.getElementById("num_of_items").value = numberOfItems;
		}
	}


// this function simply return every element that has a classname matching to the one supplied
function getElementsByClassName(classname, node) 
	{
	if( !node ) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for( var i=0,j=els.length; i<j; i++ )
		{
		if( re.test(els[i].className )) 
			{
			a.push(els[i]);
			}
		}
	return a;
	}
	
function trim( str, charlist ) 
	{ 
    var whitespace;
    
    if(!charlist)
		{
        whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
    	}
	else
		{
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
		}
  
	for (var i = 0; i < str.length; i++) 
  		{
    	if (whitespace.indexOf(str.charAt(i)) === -1) 
			{
			str = str.substring(i);
			break;
			}
		}
	for (i = str.length - 1; i >= 0; i--)
	{
    if (whitespace.indexOf(str.charAt(i)) === -1) 
		{
    	str = str.substring(0, i + 1);
    	break;
    	}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}


// this function attaches the event handlers to the individual quantity boxes
function orderInit ()
	{
	orderBoxes = getElementsByClassName( "quantity_box" );
	for( x = 0, ArrayLength = orderBoxes.length; x < ArrayLength; x++ )
		{
		orderBoxes[x].onchange = updateOrder;
		}
		
	// create a hidden field which will contain the current page's URL
	hiddenField = document.createElement("input");
	hiddenField.type = "hidden";
	hiddenField.name = "page_url";
	hiddenField.value = window.location.href;
	
	// attach the field to the page
	document.getElementById("order_container").appendChild( hiddenField );
	}

if( window.onload )
	{
	oldOnLoad2 = window.onload;
	window.onload = function ()
		{
		orderInit();
		oldOnLoad2();
		}
	}
else
	{
	window.onload = orderInit;
	}